/*

* Validates integer strings.

*

* Parameter: sInteger - a string to validate as an integer

* Returns:   true (is integer) or false (is not) boolean

*/

function isInteger(sInteger) {

	var isInt = true;

	inputStr = sInteger.toString(); // in case not a string already

	for (var i = 0; i < inputStr.length; i++) {

		var oneChar = inputStr.charAt(i);

		if (oneChar < "0" || oneChar > "9") {

			isInt = false;

			i = inputStr.length; // break out of loop when bad char found

		}

	}

	return isInt;

}



/*

* Validates date strings.

*

* Parameter: field    - field containing the date string

* Internal Calls: isInteger() and isNotBlank()

* Returns:   true (valid date) or false (not valid) boolean

*/

function isDate(field) {

	var valid = true;

	var sDate = field;

	//var sDate = field.value;

	var Slash1Pos = sDate.indexOf("/",0);

	var Slash2Pos = sDate.indexOf("/",Slash1Pos + 1);

	var mm = sDate.substring(0,Slash1Pos);

	var dd = sDate.substring(Slash1Pos + 1,Slash2Pos);

	var yyyy = sDate.substring(Slash2Pos + 1,sDate.length);



	// Validate date

	if (isInteger(mm) == false || isInteger(dd) == false || isInteger(yyyy) == false) {

		valid = false;

	}

	if (yyyy.length != 4) {

		valid = false;

	} else if (mm < 1 || mm > 12) {

		valid = false;

	} else if (dd < 1 || dd > 31) {

		valid = false;

	} else if (mm == 2) {

		if (dd > 29) {

			valid = false ;

		} else if (dd == 29) {

			if (yyyy % 100 == 0 && yyyy % 400 != 0) {

				valid = false;

			} else if (yyyy % 4 != 0) {

				valid = false;

			}

		}

	} else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {

		if (dd > 30) {

			valid = false;

		}

	}



	//if (valid == false) {

	//	alert("Data Entry Error:\n\nInvalid date entered.\nPlease enter the date in MM/DD/YYYY format.");

	//	field.focus();

	//	field.select();

	//}



	return valid;

}


//function isSSN() {
	//formLength = thisForm.elements.length;
	//for (var i = 0; i < formLength; i++) {
		//if(thisForm.elements[i].name == "ssn"){
			//if(thisForm.elements[i].value == "" || isNaN(thisForm.elements[i].value)) {
				//return true;
			//}
		//}					
	//}
//}

//function isSSN2() {
	//formLength = thisForm.elements.length;
	//for (var i = 0; i < formLength; i++) {
		//if(thisForm.elements[i].name == "ssn2"){
			//if(thisForm.elements[i].value == "" || isNaN(thisForm.elements[i].value)) {
				//return true;
			//}
		//}					
	//}
//}

//function isNum(formName) {

	//formLength = thisForm.elements.length;
	//for (var i = 0; i < formLength; i++) {
		//if(thisForm.elements[i].name == "memberNumber"){
			//if(thisForm.elements[i].value == "" || isNaN(thisForm.elements[i].value)) {
				//return true;
			//}
		//}					
	//}
//}

function validateLoanApp() {

	thisForm = eval("document.pageForm");
	
	errorMessage = "Error! The following fields are required.      \n\n";
	
	//var isLoanType = "none";
	
	if(thisForm.loanType[0].checked)
		isLoanType = "individual";
	if(thisForm.loanType[1].checked)
		isLoanType = "joint";
	
	if(thisForm.loanCategory.selectedIndex == 0) {
		errorMessage += "* Choose a loan type\n";
	}
	
	if(trim(thisForm.firstName.value).length == 0) {
		errorMessage += "* Primary Borrower First Name\n";
	}
	
	if(trim(thisForm.lastName.value).length == 0) {
		errorMessage += "* Primary Borrower Last Name\n";
	}
	
	var primaryDOB = thisForm.DOB_month.value + '/' + thisForm.DOB_day.value + '/' + thisForm.DOB_year.value;
	var validPrimaryDOB = isDate(primaryDOB);

	if(validPrimaryDOB != 1) {
		errorMessage += "* Primary Borrower's Date of Birth. (missing or invalid)\n";
	}
	
	if(	(thisForm.ssn1.value == "" || isNaN(thisForm.ssn1.value)) || (thisForm.ssn2.value == "" || isNaN(thisForm.ssn2.value)) || (thisForm.ssn3.value == "" || isNaN(thisForm.ssn3.value)) ||
		(thisForm.ssn4.value == "" || isNaN(thisForm.ssn4.value)) || (thisForm.ssn5.value == "" || isNaN(thisForm.ssn5.value)) || (thisForm.ssn6.value == "" || isNaN(thisForm.ssn6.value)) ||
		(thisForm.ssn7.value == "" || isNaN(thisForm.ssn7.value)) || (thisForm.ssn8.value == "" || isNaN(thisForm.ssn8.value)) || (thisForm.ssn9.value == "" || isNaN(thisForm.ssn9.value)) ){
			
			errorMessage += "* Primary Borrower Social Security Number. (missing or invalid)\n";
	}
		
	if(trim(thisForm.memberNumber.value).length == 0) {
		errorMessage += "* Primary Borrower Member Number. (missing or invalid)\n";
	}
	
	if(trim(thisForm.email.value).length == 0) {
		errorMessage += "* Primary Borrower E-mail Address.\n";
	}
	
	if(trim(thisForm.currentAddress.value).length == 0) {
		errorMessage += "* Primary Borrower Current Address.\n";
	}

	if(trim(thisForm.currentCity.value).length == 0) {
		errorMessage += "* Primary Borrower Current City.\n";
	}

	if(trim(thisForm.currentState.value).length == 0) {
		errorMessage += "* Primary Borrower Current State.\n";
	}

	if(trim(thisForm.currentZipCode.value).length == 0) {
		errorMessage += "* Primary Borrower Current Zip Code.\n";
	}
	
	if(trim(thisForm.currentDaytimePhone.value).length == 0) {
		errorMessage += "* Primary Borrower Daytime Phone.\n";
	}
	
	if(isLoanType == "joint") {
	
		if(trim(thisForm.cb_firstName.value).length == 0) {
			errorMessage += "* Co-Borrower's First Name\n";
		}
		
		if(trim(thisForm.cb_lastName.value).length == 0) {
			errorMessage += "* Co-Borrower's Last Name\n";
		}

		var cbDOB = thisForm.cb_DOB_month.value + '/' + thisForm.cb_DOB_day.value + '/' + thisForm.cb_DOB_year.value;
		var validcbDOB = isDate(cbDOB);
	
		if(validcbDOB != 1) {
			errorMessage += "* Co-Borrower's Date of Birth. (missing or invalid)\n";
		}
		
		if(	(thisForm.ssn21.value == "" || isNaN(thisForm.ssn21.value)) || (thisForm.ssn22.value == "" || isNaN(thisForm.ssn22.value)) || (thisForm.ssn3.value == "" || isNaN(thisForm.ssn23.value)) ||
			(thisForm.ssn24.value == "" || isNaN(thisForm.ssn24.value)) || (thisForm.ssn25.value == "" || isNaN(thisForm.ssn25.value)) || (thisForm.ssn6.value == "" || isNaN(thisForm.ssn26.value)) ||
			(thisForm.ssn27.value == "" || isNaN(thisForm.ssn27.value)) || (thisForm.ssn28.value == "" || isNaN(thisForm.ssn28.value)) || (thisForm.ssn9.value == "" || isNaN(thisForm.ssn29.value)) ){
			
				errorMessage += "* Co-Borrower's Social Security Number. (missing or invalid)\n";
		}	
	}

	if(trim(thisForm.amountRequested.value).length == 0) {
		errorMessage += "* Amount Requested.\n";
	}

	if(errorMessage.length > 50) {
		alert(errorMessage);
		return false;
	}
	else
		return true;
	

}

