/*******************************************
  get browser and versions
*******************************************/
var isNav = (navigator.appName == "Netscape") ? true : false;
var isIE  = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
var isOp  = (navigator.appName.indexOf("Opera") != -1) ? true : false;
var isMac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
var isVersion = parseInt(navigator.appVersion.charAt(0));
var ns4 = (document.layers)? true:false
var ie4 = (document.all)? true:false
var ns6 = (!document.all && document.getElementById) ? true:false
var myDir; 

if (((isNav) && (isVersion < 3)) || ((isIE) && (isVersion < 2))) {alert("Warning: Requires at least a 3.0 browser!");}

/*******************************************
  start: confirm functions
*******************************************/
function confirmThis(message) {
 if(confirm(message)) return true;
 return false;
}

function confirmdelete(url,message) {
 if(confirmThis(message)) {
  window.location.href = url;
 }
}
/*******************************************
  end: confirm functions
*******************************************/

/*******************************************
  start: popup functions
*******************************************/
function popUpWin(url,windowName, windowOptions) {
  windowName = window.open(url,windowName,windowOptions);
  windowName.focus();
}
/*******************************************
  end: popup functions
*******************************************/

/*******************************************
  start: Dropdown automatic jump
*******************************************/
function DropDownAutoJump(field,baseURL) {
  var URL = field.options[field.selectedIndex].value;
  var myURL = baseURL + URL;
  if (URL != "") {
    popUpWin(myURL,'ProductDetails','fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=500,height=300');
    field.selectedIndex=0;
  }
}
/*******************************************
  end: Dropdown automatic jump
*******************************************/

/*******************************************
  start: Loan Form Validation
*******************************************/
function council_owned(val) {
    var council_owned = document.getElementById('council_owned');
    if(val=="yes") {
        council_owned.style.display = "block";
    } else {
        council_owned.style.display = "none";
    }
}

function mortgage_arrears(val) {
    var mortgage_arrears = document.getElementById('mortgage_arrears');
    if(val=="yes") {
        mortgage_arrears.style.display = "block";
    } else {
        mortgage_arrears.style.display = "none";
    }
}

function empstatus(val) {
    var selfemployed = document.getElementById('selfemployed');
    if(val=="Self-Employed") {
       selfemployed.style.display = "block";
    } else {
        selfemployed.style.display = "none";
    }
}

function empstatusFlip2(val) {
    var selfemployed2 = document.getElementById('selfemployed2');
    if(val=="Self-Employed") {
       selfemployed2.style.display = "block";
    } else {
        selfemployed2.style.display = "none";
    }
}

function additionalIncomeFlip1(val) {
    var additionalIncomeFlip1 = document.getElementById('additionalIncomeFlip1');
    if(val=="yes") {
        additionalIncomeFlip1.style.display = "block";
    } else {
        additionalIncomeFlip1.style.display = "none";
    }
}

function additionalIncomeFlip2(val) {
    var additionalIncomeFlip2 = document.getElementById('additionalIncomeFlip2');
    if(val=="yes") {
        additionalIncomeFlip2.style.display = "block";
    } else {
        additionalIncomeFlip2.style.display = "none";
    }
}

function maritalStatusFlip1(val) {
    var maritalStatusFlip1 = document.getElementById('maritalStatusFlip1');
    if(val=="Married"|| val=="LivingTogether") {
        maritalStatusFlip1.style.display = "block";
    } else {
        maritalStatusFlip1.style.display = "none";
    }
}

function addressyears(val) {
    var addressyearsFlip1 = document.getElementById('addressyearsFlip1');
    if(val=="Less than 3 years") {
        addressyearsFlip1.style.display = "block";
    } else {
        addressyearsFlip1.style.display = "none";
    }
}

//onload = function() {
    //keep things in sync 
    /*
	marital_status(document.getElementById('maritalstatus').options[document.getElementById('maritalstatus').selectedIndex].value);
    previous_address(document.getElementById('timeataddress').options[document.getElementById('timeataddress').selectedIndex].value);
    if(document.getElementById('second_mortgage_yes').checked) { second_mortgage("yes"); }
    */
//}

//email Validation Function
function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address");
		   return (false);
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID");
		   return (false);
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID");
		    return (false);
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID");
		    return (false);
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID");
		    return (false);
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID");
		    return (false);
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID");
		    return (false);
		 }

 		 return (true);					
	}


	//AGREEMENT Button VAl
		function valButton(btn) {
		var cnt = -1;
		for (var i=0; i < btn.length; i++) {
		   if (btn[i].checked) {cnt = i; i = btn.length;}
		   }
		if (cnt > -1) return btn[cnt].value;
		else return (false);
		}

		//Button VAl
		function valButton2(btn) {
		var cnt = 0;
		if (btn.checked) return btn.value;
		else return (false);
		}


//VALIDATE THE FORM 
function valueCheck(theForm) {

   if (theForm.homestatus.value == "") {
        alert("Please choose your housing status. Homeowner, or Tenant.");
        theForm.homestatus.focus();
        return (false);
    }
   //if (theForm.homestatus.value == "tenant") 
	if (theForm.homestatus.options[theForm.homestatus.selectedIndex].value=="tenant")
		{
        alert("Unfortunately, we cannot lend to tenants at this time.");
        document.location.href='http://www.lowcostfunding.co.uk/unsecured_loans.php';
        return (false);
    }
    
	if (theForm.propertyvalue.value == "") {
        alert("Please enter the Approximate Property Value.");
        theForm.propertyvalue.focus();
        return (false);
    }
   // if (theForm.loanperiod.selectedIndex == "") {
   //     alert("Please choose how many months you want the loan for.");
   //     theForm.loanperiod.focus();
   //     return (false);
   // }
    
    
	

    if (theForm.firstn1.value == "") {
        alert("Please enter your First Name.");
        theForm.firstn1.focus();
        return (false);
    }
    if (theForm.lastn1.value == "") {
        alert("Please enter your Surname.");
        theForm.lastn1.focus();
        return (false);
    }
    if (theForm.dobyear1.value == "") {
        alert("Please enter the Year you were born.");
        theForm.dobyear1.focus();
        return (false);
    }
	if (theForm.dobmonth1.value == "") {
        alert("Please enter the Month you were born.");
        theForm.dobmonth1.focus();
        return (false);
    }
	if (theForm.dobday1.value == "") {
        alert("Please enter the Day you were born.");
        theForm.dobday1.focus();
        return (false);
    }
if (theForm.grossincome1.selectedIndex == "") {
        alert("Please enter your Yearly Gross Income.");
        theForm.grossincome1.focus();
        return (false);
    }	
    if (theForm.street.value == "") {
        alert("Please enter a value for Address.");
        theForm.street.focus();
        return (false);
    }
	
    if (theForm.postcode.value == "") {
        alert("Please enter the Post Code where your home resides.");
        theForm.postcode.focus();
        return (false);
    }
theForm.phone.value = theForm.phone.value.replace(/[^0-9]/g, '');

   if (theForm.phone.value.length != 11) { 
		alert('Daytime phone number must be 11 digits.'); 
		theForm.phone.focus();
		return (false);			
    }
theForm.evephone.value = theForm.evephone.value.replace(/[^0-9]/g, '');

   if (theForm.evephone.value.length != 11) { 
		alert('Evening phone number must be 11 digits.'); 
		theForm.evephone.focus();
		return (false);			
    }
	
 //email string validation
    	if ((theForm.email.value==null)||(theForm.email.value=="")){
		alert("Please Enter your Email address");
		theForm.email.focus();
		return (false);
	}
	//email validation
	  if (echeck(theForm.email.value)==false){
	  theForm.email.value="";
	  theForm.email.focus();
	  return (false);
	}
	
    if (theForm.maritalstatus.selectedIndex == "") {
        alert("Please choose your Marital Status.");
        theForm.maritalstatus.focus();
        return (false);
    }
	
	//Marital Status POP UP VAL
    var maritalstatus = document.getElementById('maritalstatus');
    if(maritalstatus.options[maritalstatus.selectedIndex].value == "Married" || maritalstatus.options[maritalstatus.selectedIndex].value == "LivingTogether") 

{ 
        if (theForm.firstn2.value == "") {
            alert("Please enter your partner\'s First Name.");
            theForm.firstn2.focus();
            return (false);
        }	  
        if (theForm.lastn2.value == "") {
            alert("Please enter your partner\'s Surname.");
            theForm.lastn2.focus();
            return (false);
        }
		//if (theForm.mothersmaiden2.value == "") {
        //alert("Please enter your partner\'s Maiden Name, or enter n/a.");
        //theForm.mothersmaiden2.focus();
        //return (false);
        //}
        if (theForm.dobyear2.value == "") {
            alert("Please enter your partner\'s Birth Year.");
            theForm.dobyear2.focus();
            return (false);
        }
	    if (theForm.dobmonth2.value == "") {
        alert("Please enter your partner\'s birth Month.");
        theForm.dobmonth2.focus();
        return (false);
        }
	    if (theForm.dobday2.value == "") {
        alert("Please enter your partner\'s Birth Date.");
        theForm.dobday2.focus();
        return (false);
        }
		
		
		if (theForm.grossincome2.value == "") {
        alert("Please enter partner\'s Gross Income.");
        theForm.grossincome2.focus();
        return (false);
        }
		
        //if (theForm.occupation2.value == "") {
            //alert("Please enter your partner\'s Occupation.");
           // theForm.occupation2.focus();
           // return (false);
       // }
		
		
       
		
	//SelfEmployed2 YES VAL
	//var empstatus2 = document.getElementById('empstatus2');
    //if(empstatus2.options[empstatus2.selectedIndex].value == "Self\-Employed"){ 
  	   
	 //if (valButton(theForm.selfemployedproof2)==false){
	  //alert("Please select a Proof of Income for Self Employment for your partner.");
	  //return (false);
	//}
 //}

 	//Additional Income 2  YES VAL
	//var additionalincome2 = document.getElementById('additionalincome2');
    //if(additionalincome2.options[additionalincome2.selectedIndex].value == "yes"){ 
       
	   //if (theForm.addincomeamount2.value == "") {
        //alert("Please enter your partner\'s Annual Additional Income");
        //theForm.addincomeamount2.focus();
        //return (false);
    //}  
	   //if (theForm.addincomesource2.value == "") {
        //alert("Please enter the Source of your partner\'s Additioanl Income.");
        //theForm.addincomesource2.focus();
       // return (false);
    //}   	
	
//} 
		
      
        /*
        if (theForm.employersname2.value == "") {
            alert("Please enter your partner\'s employer\'s name.");
            theForm.employersname2.focus();
            return (false);
        }
        if (theForm.grossincome2.value == "") {
            alert("Please enter your partner\'s gross income.");
            theForm.grossincome2.focus();
            return (false);
        }
        if (theForm.joblength2.indexSelected == "") {
            alert("Please enter your partner.");
            theForm.joblength2.focus();
            return (false);
        }
        */
    }
	//number = theForm.number.value.replace(/[^0-9]/g, '');
	//number = document.theForm.number.value.replace(/[^0-9]/g, '');
    /* 
	if (theForm.number.value == "") {
        alert("Please enter the street number of your home.");
        theForm.number.focus();
        return (false);
    }
	*/
	/*phone = phone.replace(/[^0-9]/g, '');
    if(phone.length < 11) { 
		alert('Phone number must be 11 digits.'); 
		theForm.phone.focus();
	}*/
	
	/*
	if ((theForm.phone.value == "")&&(theForm.phone.value == "")) {
	       alert("Please enter at least one phone number.");
	       theForm.phone.focus();
           return (false);
	} else if ((theForm.phone.value != "")&&(IsNumeric(theForm.phone.value) == false)) {
			alert("Sorry, your phone number cannot contain letters.");
			theForm.phone.focus();
			return (false);
		} else if ((theForm.phone.value != "")&&(IsNumeric(theForm.phone.value) == false)) {
			alert("Sorry, your phone number cannot contain letters.");
			theForm.phone.focus();
			return (false);
			}
	
	*/
	

	
	
	{
    /*     if(timeataddress.options[timeataddress.selectedIndex].value == "Less than 1" || timeataddress.options[timeataddress.selectedIndex].value == "1" || 

         timeataddress.options[timeataddress.selectedIndex].value == "2") {
		if (theForm.number2.value == "") {
            alert("Please enter the street number of your previous home.");
            theForm.number2.focus();
            return (false);
        }
		*/
       /*
	    if (theForm.timeataddress2.value == "") {
            alert("Please enter the amount of time you have owned your previous home.");
            theForm.timeataddress2.focus();
            return (false);
        }
        if (theForm.yearbuilt.value == "") {
            alert("Please enter the year your previous home was built in.");
            theForm.yearbuilt2.focus();
            return (false);
        }
		*/

  }
	//AGREEMENT VAL
	if (!theForm.agreement.checked){
		alert("Please read Declaration, Terms and Conditions and select I AGREE so that your application can be processed.");
		return false;
	}
    return (true);
}
/*******************************************
  end: Loan Form Validation
*******************************************/