//**********************************************
//Extra Functions
//**********************************************

/* Jquery functions */
function showHide(hide_box){
	//alert("here");
	 $("#"+hide_box).toggle('slow');		
	}
	
function showHideAdditional(hide_box){
	//alert("here");
	 $("#"+hide_box).toggle(500);		
	}

/* Jquery functions end */

function cleanNumber(strNum) {
	if (!strNum) return strNum;
	strNum = replace(strNum, '$', '', 0);
	strNum = replace(strNum, ',', '', 0);
	strNum = replace(strNum, '%', '', 0);	
	return strNum;
}

function checkCountEmp(type){

	if(type == 'brw'){
		var count1;
		count1 = document.getElementById('countEmp').value;
		count1++;
		document.getElementById('countEmp').value = count1;
		if(count1 > 3){
			document.getElementById('verb').innerHTML = 'Were';
		}
	}else{	
		var count2;
		count2 = document.getElementById('countEmpCo').value;
		count2++;
		document.getElementById('countEmpCo').value = count2;
		if(count2 > 3){
			document.getElementById('verbCo').innerHTML = 'Were';
		}
	}
}

function replace(szBuf, szFind, szReplace, lStart) {
	var lFind = 0;
	if (!lStart) lStart = 0;
	
	while (lFind != -1) {
		lFind = szBuf.indexOf(szFind, lStart);
		if (lFind != 0 &&(lFind == '' || lFind == null)) lFind = -1;
		if (lFind != -1) {
			szBuf = szBuf.substring(0,lFind) + szReplace + szBuf.substring(lFind + szFind.length);
			lStart = lFind + szReplace.length;
		}
	}
	return szBuf;
}

function numFormat(field) {
	var lFind;
	var szBuf = field.value;

	lFind = szBuf.indexOf("k",0);
	if (lFind !=-1) {
	}
	field.value=szBuf;
	fvalue = parseFloat(cleanNumber(szBuf)); //kick out $ , %; returns a numerical value equal to the number or Nan
	if (0 > fvalue) {
		field.value= 0;
		alert("All values must be numeric.")
		field.focus();
		return 0;
	}
	if (isNaN(fvalue)) {
		field.value= '';
		//field.value = 0;
	} else {
		field.value = fvalue;
	}
	return 1;
}





function numFormatInt(field) {
	var lFind;
	var szBuf = field.value;

	lFind = szBuf.indexOf("k",0);
	if (lFind !=-1) {
	}
	field.value=szBuf;
	fvalue = parseFloat(cleanNumber(szBuf)); //kick out $ , %; returns a numerical value equal to the number or Nan
	if (0 > fvalue) {
		field.value= 0;
		alert("All values must be numeric.")
		field.focus();
		return 0;
	}
	if (isNaN(fvalue)) {
		field.value = 0;
	} else {
		field.value = fvalue;
	}
	return 1;
}
function UpdateDebtInfo(form)	{
	if (numFormatInt(form.totalMonthlyDebt)) {
		form.otherDebtMonthly.value = form.totalMonthlyDebt.value;		
		form.cCardMonthly.value = 0;
		form.autoMonthly.value = 0;
		form.personalMonthly.value = 0;
		form.mortgageMonhtly.value = 0;
		form.cCardUnpaidBal.value = 0;
		form.autoUnpaidBal.value = 0;
		form.personalUnpaidBal.value = 0;
		form.mortgageUnpaidBal.value = 0;
		return 1;
} else
		return 0;
}

function UpdateTotalDebt(form) {
if (numFormatInt(form.cCardMonthly) &&	
	numFormatInt(form.autoMonthly) && 
	numFormatInt(form.personalMonthly) && 
	numFormatInt(form.mortgageMonhtly) && 
	numFormatInt(form.otherDebtMonthly)) {
	form.totalMonthlyDebt.value = parseInt(form.cCardMonthly.value) +	
									parseFloat(form.autoMonthly.value) +	
									parseFloat(form.personalMonthly.value) +	
									parseFloat(form.mortgageMonhtly.value) +	
									parseFloat(form.otherDebtMonthly.value);
	return 1;
} else
		return 0;
}



function getProperty(){
	var thisPropNo;
	var newPropNo;
	thisPropNo = document.getElementById('thisProp').value;
	newPropNo = parseInt(thisPropNo) + 1;
	//alert(document.getElementById('thisProp').value);
	document.getElementById('thisProp').value = newPropNo;
	//alert(document.getElementById('thisProp').value);
	document.getElementById('property' + newPropNo).style.display = '';
	if(newPropNo == 10){
		//alert('helo');
		document.getElementById('addProperty').innerHTML = '';
		//document.getElementById("addProperty").innerHTML = '$' + form.BorrowerIncome.value;
	}
	
}

function initVal(){
	document.getElementById('thisProp').value = 4;
}


//**********************************************
//Checking form
//**********************************************

function formProcess(){
	if(checkForm()){
		document.signupForm.submit();
	}
}

function signinCheck(){		
	if(validSigninForm()){
		//document.signinForm.submit();
		return true;
	}else{
		return false;
	}
}

function checkForm(){	
	var email = document.getElementById("email").value;
	var password = document.getElementById("password").value;
	var cPassword = document.getElementById("cPassword").value;

	

	if (email == "" && password == "" && cPassword == ""){
			alert("Email and Password are required!");						
			return false;
	}	
		
	
	if (email==""){
			alert("Email is required!");			
			return false;
	}else if (password==""){
			alert("Please enter your password!");						
			return false;
	}else if (cPassword==""){
			alert("Please confirm your password!");						
			return false;
	} else if(!chkEmail(email)){
		return false;
	} else if (password != cPassword){
			alert("Password doesn't match.");						
			return false;
	}
	return true;
}

function validSigninForm(){

	
	var email = document.getElementById("loginEmail").value;
	var password = document.getElementById("loginPassword").value;

	

	if (email=="" && password==""){
			alert("Email and Password are required!");						
			return false;
	}		
	
	if (email==""){
			alert("Email is required!");			
			return false;
	}else if (password==""){
			alert("Please enter your password!");						
			return false;
	}else if (!chkEmail(email)){
		return false;
	}
	return true;
}

//**********************************************

//**********************************************
//Checking form
//**********************************************
function checkContactUsForm(){

	var fname = document.getElementById("fname").value;
	var email = document.getElementById("email").value;
	var subject = document.getElementById("subject").value;
	var message = document.getElementById("txtmessage").value;



	if (fname=="" && email==""&& subject==""&& message==""){
		alert("First Name,Email Address, Subject and Message are required fields!");
		return false;
	}
	
	if(fname==""){
		alert("Name is required!");		
		return false;
	}else if (email==""){
		alert("Email is required!");		
		return false;
	} else if(!chkEmail(email)){
		return false;
	}	else if(subject==""){
		alert("Subject is required!");			
		return false;
	}	else if(message==""){
		alert("Message is required!");			
		return false;
	}

}

//**********************************************

//**********************************************
//Validating Email address
//**********************************************

function chkEmail(formInput) {
	//formInput = document.getElementById("email").value;
	var message;

	if (stringEmpty(formInput)) {
		message = "Error! There is no input value entered.";
		alert(message);
		//return false;
	} else if (noAtSign(formInput)) {
		message = "Email address Incorrect! \nThe address \"" + formInput + "\" does not contain an '@' character.";
		alert(message);
	} else if (nothingBeforeAt(formInput)) {
		message = "Email address Incorrect! \nThe address \"" + formInput;
		message += "\" must contain at least one character before the '@' character";
		alert(message);
	} else if (noLeftBracket(formInput)) {
		message = "Email address Incorrect! \nThe address \"" + formInput;
		message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
		alert(message);
	} else if (noRightBracket(formInput)) {
		message = "Email address Incorrect! \nThe address \"" + formInput;
		message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
		alert( message);
	} else if (noValidPeriod(formInput)) {
		message = "Email address Incorrect! \nThe address \"" + formInput + "\" must contain a period ('.') character.";
		alert(message);
	} else if (noValidSuffix(formInput)) {
		message = "Email address Incorrect! \nThe address \"" + formInput;
		message += "\" must contain a two, three or four character suffix.";
		alert(message);
	} else {
		//message = "Success! The email address \"" + formInput + "\" validates OK.";
		//alert(message);
		return true;
	}

	var objType = typeof(formInput.focus);
	if (objType == "object" || objType == "function") {
		formInput.focus();
	}
	return (false);
}

function checkValid (formField) {
	if ( checkValid ( formField ) == true ) {
		alert ( 'E-Mail Address Validates OK' );
	}
	return true;
}

function stringEmpty (formField) {
	// CHECK THAT THE STRING IS NOT EMPTY
	if ( formField.length < 1 ) {
		return ( true );
	} else {
		return ( false );
	}
}

function noAtSign (formField) {
	// CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
	if (formField.indexOf ('@', 0) == -1) {
		return ( true )
	} else {
		return ( false );
	}
}

function nothingBeforeAt (formField) {
	// CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
	if ( formField.indexOf ( '@', 0 ) < 1 ) {
		return ( true )
	} else {
		return ( false );
	}
}

function noLeftBracket (formField) {
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
	if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
		return ( true )
	} else {
		return ( false );
	}
}

function noRightBracket (formField) {
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
	if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
		return ( true );
	} else {
		return ( false );
	}
}

function noValidPeriod (formField) {
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
	if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
	return ( false );

	// CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
	if (formField.indexOf ( '.', 0 ) == -1)
	return ( true );

	return ( false );
}

function noValidSuffix(formField) {
	// IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
	if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
		return ( false );
	}

	// CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
	var len = formField.length;
	var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
	if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
		return ( true );
	} else {
		return ( false );
	}
}
//**********************************************
// End script of email address validation
//**********************************************

//**********************************************
// start phone validation
//**********************************************
function phone_val(str){
	var str_array = str.split("-");
	if(str_array.length==3){
		if(str_array[0].length==3 && isInteger(str_array[0]) && str_array[1].length==3 && str_array[2].length==4
		&& isInteger(str_array[1]) && isInteger(str_array[2])){
			return true;
		}
		else
		return false;
	}
	else
	return false;

}

//**********************************************
// End phone validation
//**********************************************
////////////////////////////

//**********************************************
// start validating integar value
//**********************************************

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}
//**********************************************
// End validating integar value
//**********************************************

//**********************************************
// start Checking phone number length
//**********************************************
function phoneLength(phone){

	if(phone.length<13){
		alert("Error! Phone number incomplete.")
		return false;
	}else{
		return true;
	}
}
//**********************************************
// Start Validating Step 1 to 11 forms 
//**********************************************

function validForm(thisForm){	
// alert(thisForm);
// return false;
	if(thisForm == 'step1Form'){
		var fName;		
		var lName;
		var street;
		var city;
		var state;
		var zip;
		var homePhone;
		var required;
		
		required = "";
		
		fName = document.getElementById("txtFirstName").value;	
		lName = document.getElementById("txtLastName").value;
		street = document.getElementById("txtStreet").value;
		city = document.getElementById("txtCity").value;
		state = document.getElementById("drpState").value;
		zip = document.getElementById("txtZip").value;
			homePhone1 = document.getElementById("txtHomePhone1").value;
			homePhone2 = document.getElementById("txtHomePhone2").value;
			homePhone3 = document.getElementById("txtHomePhone3").value;
		homePhone = homePhone1 + homePhone2 + homePhone3;
		
		if (fName == ""){
				required += "-First Name\n";			
		}
		if (lName == ""){
				required += "-Last Name\n";			
		}
		if (street == ""){
				required += "-Street\n";			
		}
		if (city == ""){
				required += "-City\n";			
		}
		if (state == ""){
				required += "-State\n";			
		}
		if (zip == ""){
				required += "-Zip\n";			
		}
		if (homePhone == ""){
				required += "-Home Phone";			
		}	
		if(required){
			alert("Please check the required fields:\n" + required);
			return false;
		}else {
			return true;
		}		
	}else if(thisForm == 'step3Form'){	 
		var coFName;		
		var coLName;
		var emailBrw;
		var emailCoBrw;
		var ssn;
		var cossn;
		var required;
		required = "";
		coFName = document.getElementById("txtCoFirstName").value;	
		coLName = document.getElementById("txtCoLastName").value;
		emailBrw = document.getElementById("txtEmailBrw").value;
		emailCoBrw = document.getElementById("txtEmailAddressCo").value;
		ssn1 = document.getElementById("txtSSN1").value;
		ssn2 = document.getElementById("txtSSN2").value;
		ssn3 = document.getElementById("txtSSN3").value;
			ssn = ssn1 + ssn2 + ssn3;
		cossn1 = document.getElementById("txtCoSSN1").value;
		cossn2 = document.getElementById("txtCoSSN2").value;
		cossn3 = document.getElementById("txtCoSSN3").value;
			cossn = cossn1 + cossn2 + cossn3;
		if (coFName == ""){
				required += "-Co-Borrower First Name\n";			
		}
		if (coLName == ""){
				required += "-Co-Borrower Last Name\n";			
		}	
		if(ssn != '' && cossn != ''){
			if(ssn == cossn){
				required +="-Borrower and Co-Borrower Social security numbers cannot be same.\n";			
			}
		}
		if(emailBrw != '' && emailCoBrw != ''){
			if(emailBrw == emailCoBrw){
				required +="-Borrower and Co-Borrower email addresses cannot be same.\n";			
			}
		}
		if(required){
			alert("Please check the following requirements:\n" + required);
			return false;
		}
		else{
			return true;
		}
	}
}


function test(){
	alert('hello');
}


