
function validatePage1(form){
	
	var errCount = 0;

	if( checkBlank(form.appFirstName.value) ){
		markRed(form.appFirstName);
		errCount++;
	}
	else { markWhite(form.appFirstName); }
	
	if( checkBlank(form.appLastName.value) ){
		markRed(form.appLastName);
		errCount++;
	}
	else { markWhite(form.appLastName); }
	
	//if( checkBlank(form.appDOB.value) ){
	//	markRed(form.appDOB);
	//	errCount++;
	//}
	
	if( !checkDate(form.appDOB.value,"mm/dd/yyyy") ){
		markRed(form.appDOB);
		errCount++;
	}
	else { markWhite(form.appDOB); }
	
	if( checkBlank(form.appCurrAddress.value) ){
		markRed(form.appCurrAddress);
		errCount++;
	}
	else { markWhite(form.appCurrAddress); }
	
	
	if( checkBlank(form.appCurrCity.value) ){
		markRed(form.appCurrCity);
		errCount++;
	}
	else { markWhite(form.appCurrCity); }
	
	
	if( checkBlank(form.appCurrState.value) ){
		markRed(form.appCurrState);
		errCount++;
	}
	else { markWhite(form.appCurrState); }
	
	
	if( checkBlank(form.appCurrZip.value) ){
		markRed(form.appCurrZip);
		errCount++;
	}
	else { markWhite(form.appCurrZip); }
	
	if( checkEmail(form.appEmail.value) ){
		markRed(form.appEmail);
		errCount++;
	}
	else { markWhite(form.appEmail); }
	
	if( checkGender(form.appGender) ) {
		markTextRed(document.getElementById("appGenderText"));
		errCount++;
	}
	else { markTextBlack(document.getElementById("appGenderText")); }
	
	if( checkCitizen(form.appIsCitizen) ) {
		markTextRed(document.getElementById("appIsCitizenText"));
		errCount++;
	}
	else { markTextBlack(document.getElementById("appIsCitizenText")); }

	if(errCount == 0){
		Pg1ToPg2();
	}
}

function validatePage2(form){
	
	var errCount = 0;
		
	//if(form.appFirstName.value == ""){
	//	form.appFirstName.style.background="#ffc0c0";
	//}
	
	//if( notBlank(form.appFirstName.value) ){
	//	markRed(form.appFirstName);
	//	errCount++;
	//}
	
	
	if(errCount == 0){
		Pg2ToPg3();
	}
}

function validatePage3(form){
	
	var errCount = 0;

	if( checkBlank(form.appParFirstName.value) ){
		markRed(form.appParFirstName);
		errCount++;
	}
	else { markWhite(form.appParFirstName); }
	
	if( checkBlank(form.appParLastName.value) ){
		markRed(form.appParLastName);
		errCount++;
	}
	else { markWhite(form.appParLastName); }
	
	if( checkBlank(form.appParCurrAddress.value) ){
		markRed(form.appParCurrAddress);
		errCount++;
	}
	else { markWhite(form.appParCurrAddress); }
	
	if( checkBlank(form.appParCurrCity.value) ){
		markRed(form.appParCurrCity);
		errCount++;
	}
	else { markWhite(form.appParCurrCity); }
	
	if( checkBlank(form.appParCurrState.value) ){
		markRed(form.appParCurrState);
		errCount++;
	}
	else { markWhite(form.appParCurrState); }
	
	if( checkBlank(form.appParCurrZip.value) ){
		markRed(form.appParCurrZip);
		errCount++;
	}
	else { markWhite(form.appParCurrZip); }
	
	// Emergency Contacts
	
	if( checkBlank(form.appEmerFirstName.value) ){
		markRed(form.appEmerFirstName);
		errCount++;
	}
	else { markWhite(form.appEmerFirstName); }
	
	if( checkBlank(form.appEmerLastName.value) ){
		markRed(form.appEmerLastName);
		errCount++;
	}
	else { markWhite(form.appEmerLastName); }
	
	if( checkBlank(form.appEmerCurrAddress.value) ){
		markRed(form.appEmerCurrAddress);
		errCount++;
	}
	else { markWhite(form.appEmerCurrAddress); }
	
	if( checkBlank(form.appEmerCurrCity.value) ){
		markRed(form.appEmerCurrCity);
		errCount++;
	}
	else { markWhite(form.appEmerCurrCity); }
	
	if( checkBlank(form.appEmerCurrState.value) ){
		markRed(form.appEmerCurrState);
		errCount++;
	}
	else{ markWhite(form.appEmerCurrState); }
	
	if( checkBlank(form.appEmerCurrZip.value) ){
		markRed(form.appEmerCurrZip);
		errCount++;
	}
	else { markWhite(form.appEmerCurrZip); }
	

	if(errCount == 0){
		Pg3ToPg4();
	}
}

function validatePage4(form){
	
	var errCount = 0;
		
	//if(form.appFirstName.value == ""){
	//	form.appFirstName.style.background="#ffc0c0";
	//}
	
	//if( notBlank(form.appFirstName.value) ){
	//	markRed(form.appFirstName);
	//	errCount++;
	//}
	
	
	if(errCount == 0){
		Pg4ToPg5();
	}
}

function validatePage5(form){
	
	var errCount = 0;
	
	if( checkBlank(form.appSignature.value) ){
		markRed(form.appSignature);
		errCount++;
	}
		
	if( checkLength(form.appSSN4.value,4) ){
		markRed(form.appSSN4);
		errCount++;
	}
	

	
	
	if(errCount == 0){
		form.submit();
	}
}

// CLIENT SIDE VALIDATION HELPER FUNCTIONS

function markRed(errElement){
	errElement.style.background="#ffc0c0";
}

function markTextRed(errElement){
	errElement.style.color="red";
}

function markWhite(okElement){
	okElement.style.background="#ffffff";
}

function markTextBlack(okElement){
	okElement.style.color="black";
}

function checkBlank(appElement){
	var testBlank = 0;
	if(appElement == "")
		testBlank++;
	else
		testBlank=0;
		
	return(testBlank);
}

function checkLength(appElement,chkLength) {

	var chkLenResult=0;
	if(	appElement.length == chkLength )
		chkLenResult=0;
	else
		chkLenResult=1;
		
	return(chkLenResult);
}

function checkGender(genderElement) {

	var chkGenderResult = 0;
	var selectedGender = "";
	
	for (var i=0; i<genderElement.length; i++)
	{
		if(genderElement[i].checked)
		{
			selectedGender = genderElement[i].value;
		}
	}
	
	if(selectedGender == 'male') { chkGenderResult = 0; }
	else if(selectedGender == 'female') { chkGenderResult = 0; }
	else { chkGenderResult = 1; }
	
	return(chkGenderResult);	
}

function checkCitizen(citizenElement) {

	var chkCitizenResult = 0;
	var selectedCitizen = "";
	
	for (var i=0; i<citizenElement.length; i++)
	{
		if(citizenElement[i].checked)
		{
			selectedCitizen = citizenElement[i].value;
		}
	}
	
	if(selectedCitizen == 'yes') { chkCitizenResult = 0; }
	else if(selectedCitizen == 'no') { chkCitizenResult = 0; }
	else { chkCitizenResult = 1; }
	
	return(chkCitizenResult);	
}

function checkEmail(appElement){

	var chkEmailResult = 0;
	var emailRegEx = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(emailRegEx.test(appElement))
		chkEmailResult = 0;
	else
		chkEmailResult = 1;
		
	return(chkEmailResult);	
}

function checkDate(date_string, format) {
    
	var days = [0,31,28,31,30,31,30,31,31,30,31,30,31];
    var year, month, day, date_parts = null;
    var rtrn = false;
    var decisionTree = {
        'm/d/y':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/,
            'month': 1,'day': 2, year: 3
        },
        'mm/dd/yy':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2})$/,
            'month': 1,'day': 2, year: 3
        },
        'mm/dd/yyyy':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/,
            'month': 1,'day': 2, year: 3
        },
        'y/m/d':{
            're':/^(\d{2}|\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,
            'month': 2,'day': 3, year: 1
        },
        'yy/mm/dd':{
            're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{1,2})$/,
            'month': 2,'day': 3, year: 1
        },
        'yyyy/mm/dd':{
            're':/^(\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,
            'month': 2,'day': 3, year: 1
        }
    };
    var test = decisionTree[format];
    if (test) {
        date_parts = date_string.match(test.re);
        if (date_parts) {
            year = date_parts[test.year];
            month = date_parts[test.month];
            day = date_parts[test.day];

            test = (month == 2 && 
                    isLeapYear() && 
                    29 || 
                    days[month] || 0);
			
            rtrn = 1 <= day && day <= test;
        }
    }

    function isLeapYear() {
        return (year % 4 != 0 ? false : 
            ( year % 100 != 0? true: 
            ( year % 1000 != 0? false : true)));
    }
    return rtrn;
}//eof isValidDate

// Reset form field background colors

function resetFieldColor() {
	

	
	
	
}



// FORM SECTION VISIBILITY CHANGES

// FORWARD MOVING FUNCTIONS
function Pg1ToPg2(){
	document.getElementById("appPage1").style.visibility = "hidden";	
	document.getElementById("appPage2").style.visibility = "visible";	
}

function Pg2ToPg3(){
	document.getElementById("appPage2").style.visibility = "hidden";	
	document.getElementById("appPage3").style.visibility = "visible";	
}

function Pg3ToPg4(){
	document.getElementById("appPage3").style.visibility = "hidden";	
	document.getElementById("appPage4").style.visibility = "visible";	
}

function Pg4ToPg5(){
	document.getElementById("appPage4").style.visibility = "hidden";	
	document.getElementById("appPage5").style.visibility = "visible";	
}


// BACKWARD MOVING FUNCTIONS
function Pg2ToPg1(){
	document.getElementById("appPage1").style.visibility = "visible";	
	document.getElementById("appPage2").style.visibility = "hidden";
}

function Pg3ToPg2(){
	document.getElementById("appPage2").style.visibility = "visible";	
	document.getElementById("appPage3").style.visibility = "hidden";
}

function Pg4ToPg3(){
	document.getElementById("appPage3").style.visibility = "visible";	
	document.getElementById("appPage4").style.visibility = "hidden";
}

function Pg5ToPg4(){
	document.getElementById("appPage4").style.visibility = "visible";	
	document.getElementById("appPage5").style.visibility = "hidden";
}

// CLOSE POPUP WINDOW
function ExitWindow(){
	document.getElementById("appPage1").style.visibility = "hidden";
	document.getElementById("appPage2").style.visibility = "hidden";
	document.getElementById("appPage3").style.visibility = "hidden";
	document.getElementById("appPage4").style.visibility = "hidden";
	document.getElementById("appPage5").style.visibility = "hidden";
	document.getElementById("appContainer").style.visibility = "hidden";
	document.getElementById("appScreenFade").style.visibility = "hidden";
}

function OpenApp(){
	document.getElementById("appScreenFade").style.visibility = "visible";
	document.getElementById("appContainer").style.visibility = "visible";
	document.getElementById("appPage1").style.visibility = "visible";
	document.getElementById("appPage2").style.visibility = "hidden";
	document.getElementById("appPage3").style.visibility = "hidden";
	document.getElementById("appPage4").style.visibility = "hidden";
	document.getElementById("appPage5").style.visibility = "hidden";
}