var questions = new Array(-1,-1,-1);
var step1ok;
	
function changedisplay(id,type){
	document.getElementById(id).style.display = type;
}
function Init()
{
	if( document.application.Isstep1over.value!= 'true'){
		changedisplay("step2_info","none");		
	}
	else {
		changedisplay('apply',"none");	
		for( var i=1; i<= questions.length; i++){			
			if( document.getElementById('q'+i).value ==1 ){
				document.getElementById('q'+i+'y').src= "image/tickbox2.gif";
			}
			else{
				document.getElementById('q'+i+'n').src= "image/tickbox2.gif";
			}
		}		
	    //show step2
	    	changedisplay('step2_info',"");	
				
		if( document.application.showpreviousaddr.value == 'true'){
			changedisplay('previousaddr_info',"");			
		}else{
			changedisplay('previousaddr_info',"none");	
		}		
		
/*		
		if( document.application.showpreviousjob.value == 'true'){
			changedisplay('previousjob_info',"");			
		}else{
			changedisplay('previousjob_info',"none");	
		}
*/		
		if( document.application.showpartner.value == 'true'){
			changedisplay('partner_info',"");
		}else{
			changedisplay('partner_info',"none");
		}
		
		if( document.application.showmortgage.value == 'true'){			
			changedisplay('mortgage_info',"");
		}else{
			changedisplay('mortgage_info',"none");
		}
				
	}
}

function buttonover(bid,imgpath)
{
	if( step1ok !='true' && document.application.Isstep1over.value!= 'true')
		bid.src=imgpath;
}
function buttonout(bid,imgpath)
{
	if( step1ok !='true' && document.application.Isstep1over.value!= 'true')
		bid.src=imgpath;
}
function buttondownon(no,value)
{
	var id1=document.getElementById('q'+no+'y');
	var id2=document.getElementById('q'+no+'n');
	if(value=='y'){
		id1.src='image/tickbox2.gif';
		id2.src='image/tickbox1.gif'; 		
		switch(no)
		{
			case 1: questions[0]=1; break;
			case 2: questions[1]=1; break;
			case 3: questions[2]=1; break;
		}
	}
	else if(value=='n'){
		id2.src='image/tickbox2.gif';
		id1.src='image/tickbox1.gif'; 
		switch(no)
		{
			case 1: questions[0]=0;break;
			case 2: questions[1]=0;break;
			case 3: questions[2]=0;break;
		}
	}
}
function onButtonDown(no,value)
{	
	if( step1ok !='true' && document.application.Isstep1over.value!= 'true')
		buttondownon(no,value);
}

function checkstep1()
{
	var qLength = questions.length;
	for( var i=0; i< qLength; i++)
	{
		if( questions[i] == -1 ){
			alert("Please answer all the 3 questions first!");
			return false;
		}
	}
	for( var i=0; i< qLength; i++)
	{
		if( !questions[i]){
			changedisplay('applyfail',"");
			return false;
		}
	}
	step1ok = true; 
	// record the form data  	
	document.application.Isstep1over.value = 'true';
	document.application.q1.value = questions[0];
	document.application.q2.value = questions[1];
	document.application.q3.value = questions[2];
	changedisplay( 'apply',"none");
	changedisplay( 'applyfail',"none");
	changedisplay( 'step2_info', "");
	document.application.loanamount.focus();
	return true;	
}

function toggleclasshomeowner(val){
	if(val=="Joint Home Owner" || val=="Home Owner" || val=="Mortgage Payer"){	 
		changedisplay('mortgage_info',""); 
        	document.application.showmortgage.value = 'true';
        	document.application.showtenant.value = 'true';
	}else if(val==""){
    	changedisplay('mortgage_info',"none");  
    	document.application.showmortgage.value = 'false'; 
    	document.application.showtenant.value = 'false';  
    }
	else {
    	changedisplay('mortgage_info',"none"); 
    	document.application.showtenant.value = 'true';  
    	document.application.showmortgage.value = 'false';    
	}  
}

function toggleclasspreviousaddress(val) {
	if(val=="2" || val=="1" || val=="Less than 1"){	 
		changedisplay('previousaddr_info',"");   
        	document.application.showpreviousaddr.value = 'true';
    	} else {
        	changedisplay('previousaddr_info',"none");   
        	document.application.showpreviousaddr.value = 'false';    
    	}
}

/*
function toggleclasspreviousjob(val) {
	if(val=="2" || val=="1" || val=="Less than 1"){	 
		changedisplay('previousjob_info',"");   
        	document.application.showpreviousjob.value = 'true';
    	} else {
        	changedisplay('previousjob_info',"none");   
        	document.application.showpreviousjob.value = 'false';    
    	}
}
*/

function toggleclass(val) {
    if(val=="Married" || val=="LivingTogether") {
        changedisplay('partner_info', "");
        document.application.showpartner.value = 'true';
    } else {
        changedisplay('partner_info', "none");
        document.application.showpartner.value = 'false';
    }
}


function checkNum(str){
	return str.match(/\D/)==null;
}

function checkemail(str)
{ 
	var i=str.length;
 	var temp = str.indexOf('@');
 	var tempd = str.indexOf('.');
 	if (temp > 1) {
  		if ((i-temp) > 3){   
    		if ((i-tempd)>0){
     			return true;
    		}   
  		}
 	}
 	return false;
}

function checkstep2(theForm)
{
  if (theForm.loanamount.value == "")
  {
    alert("Please enter the amount you wish to borrow in the loan amount box");
    theForm.loanamount.focus();
    return (false);
  }
  
  /*
  if ( !checkNum (theForm.loanamount.value) )
  {
  	  alert("Please enter the amount you wish to borrow in the loan amount field.");
  	  theForm.loanamount.focus();
      return (false);
  }
  if (theForm.loanamount.value < 1000)
  {
    alert("Loan amount must be 1,000 or more.");
    theForm.loanamount.focus();
    return (false);
  }
  */
  if (theForm.loanpurpose.value == "")
  {
    alert("please select why you want this loan(loan purpose).");
    theForm.loanpurpose.focus();
    return (false);
  }

  if (theForm.loanperiod.selectedIndex == "")
  {
    alert("Please select how long you wish to take to repay the loan.");
    theForm.loanperiod.focus();
    return (false);
  }
  
   if (theForm.homeownerstatus.value == "")
  {
    alert("Please select your homeowner or tenant status.");
    theForm.homeownerstatus.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 last name.");
    theForm.lastn1.focus();
    return (false);
  }

  if (theForm.occupation1.value == "")
  {
    alert("Please enter your occupation.");
    theForm.occupation1.focus();
    return (false);
  }
  
  if (theForm.std.value == "" || theForm.phone.value == "")
  	  var fixedphone = "false";
  else
  	  var fixedphone = "true";
  
  if ( fixedphone == "false" && theForm.mobile.value == "" ){
  	  alert("You must give either your Mobile phone number or a fixed line phone number so that we may contact you regarding your application");
  	  theForm.std.focus();
  	  return (false);
  }
  
  if ( fixedphone == "true" ){
  	  if ( !checkNum(theForm.std.value)|| theForm.std.length <2 ){
  	  	  alert("Please check your entry about STD code.");
	  	  theForm.std.focus();
	      return (false);
  	  }
  	  if ( !checkNum(theForm.phone.value) || theForm.std.length <5 ){
  	  	  alert("Please check your entry in phone number field.");
	  	  theForm.phone.focus();
	      return (false);
  	  }
  }
  
  if ( !checkNum (theForm.mobile.value) )
  {
  	  alert("Please check your entry in mobile number field.");
  	  theForm.mobile.focus();
      return (false);
  }
 
  if (theForm.email.value == "")
  {
    alert("Please enter your email address.");
    theForm.email.focus();
    return (false);
  }
  
  if ( !checkemail(theForm.email.value) )
  {
    alert("Sorry, your email format is incorrect.");
    theForm.email.focus();
    return (false);
  }
  
  if (theForm.streetn1.value == "")
  {
    alert("Please enter your number and street name.");   
    theForm.streetn1.focus();
    return (false);
  }

  if (theForm.city1.value == "")
  {
    alert("Please enter your City/Town.");
    theForm.city1.focus();
    return (false);
  }
   
  if (theForm.postcode.value == "")
  {
    alert("Please enter your Postcode.");
    theForm.postcode.focus();
    return (false);
  }  
   
  if (theForm.dobyear1.value == "")
  {
    alert("Please enter your birthday.");
    theForm.dobyear1.focus();
    return (false);
  }
  
  if( !checkNum(theForm.dobyear1.value))
  {
    alert("Please check your entry in birthday field.");
    theForm.dobyear1.focus();
    return (false);
  }
  
  if( theForm.dobyear1.value >=2004 || theForm.dobyear1.value <= 1900)
  {
    alert("Sorry, your birth year is unreasonable.");
    theForm.dobyear1.focus();
    return (false);
  }
  
  if (theForm.timeataddress.selectedIndex == "")
  {
    alert("Please select the amount of time you have stayed at this address.");
    theForm.timeataddress.focus();
    return (false);
  }
  
  
	 // Previous address field
	//	var timeataddr = document.getElementById('timeataddress');
    //  var timeselect = timeataddr.options[timeataddr.selectedIndex].value;
  var timeselect = theForm.timeataddress.value;
	if( timeselect == "Less than 1" || timeselect == "1" || timeselect == "2")
	{
	  if (theForm.streetn2.value == "")
	  {
	    alert("Please enter your previous number and street name.");
	    theForm.streetn2.focus();
	    return (false);
	  }
	  if (theForm.city2.value == "")
	  {
	    alert("Please enter your previous City/Town.");
	    theForm.city2.focus();
	    return (false);
	  }
	  if (theForm.postcode2.value == "")
	  {
	    alert("Please enter your previous Postcode.");
	    theForm.postcode2.focus();
	    return (false);
	  }
	
	  if (theForm.timeataddress2.value == "")
	  {
	    alert("Please select the amount of time you stayed at your previous address.");
	    theForm.timeataddress2.focus();
	    return (false);
	  }
	}
    
  if (theForm.employmentstatus.value == "")
  {
    alert("Please select your employment status.");
    theForm.employmentstatus.focus();
    return (false);
  }    
  
  if (theForm.employn1.value == "")
  {
    alert("Please enter your employer\'s name.");
    theForm.employn1.focus();
    return (false);
  }  
  
  if (theForm.employeraddr.value == "")
  {
    alert("Please enter your employer\'s address.");
    theForm.employeraddr.focus();
    return (false);
  }  
  
  if (theForm.employerpostcode.value == "")
  {
    alert("Please enter your employer\'s postcode.");
    theForm.employerpostcode.focus();
    return (false);
  }  
  
  if (theForm.joblength.value == "")
  {
    alert("Please select the amount of years you have been at this job.");
    theForm.joblength.focus();
    return (false);
  }
  
  if (theForm.grossincome.value == "")
  {
    alert("Please enter your gross income.");
    theForm.grossincome.focus();
    return (false);
  } 
  
  if (theForm.timecurrentbank.value == "")
  {
    alert("Please select amount of time with current bank account.");
    theForm.timecurrentbank.focus();
    return (false);
  }   
   
  if (theForm.maritalstatus.value == "")
  {
    alert("Please choose your marital status.");
    theForm.maritalstatus.focus();
    return (false);
  }
  
  
  // check previous job experience details
  // var timeselect2 = theForm.joblength.value;
//	if( timeselect2 == "Less than 1" || timeselect == "1" || timeselect == "2")
//	{
//	  if (theForm.previousemployer.value == "")
//	  {
//	    alert("Please enter your previous employer name.");
//	    theForm.previousemployer.focus();
//	    return (false);
//	  }
//	  if (theForm.previousjoblength.value == "")
//	  {
//	    alert("Please select how many years for your previous job.");
//	    theForm.previousjoblength.focus();
//	    return (false);
//	  }
//	  if (theForm.previousjobtitle.value == "")
//	  {
//	    alert("Please enter your previous job title.");
//	    theForm.previousjobtitle.focus();
//	    return (false);
//	  }	 
//	}

// check partner's details 
//var maritalstatus = document.getElementById('maritalstatus');
//var statusselect = maritalstatus.options[maritalstatus.selectedIndex].value;
var statusselect = theForm.maritalstatus.value;
if(statusselect == "Married" || statusselect == "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 last name.");
        theForm.lastn2.focus();
        return (false);
    }
    if (theForm.occupation2.value == "")
    {
        alert("Please enter your partner\'s occupation.");
        theForm.occupation2.focus();
        return (false);
    }
     if (theForm.employn2.value == "")
	  {
	    alert("Please enter your partner\'s employer\'s name.");
	    theForm.employn2.focus();
	    return (false);
	  }  
	  
	if (theForm.joblength2.value == "")
	 {
	   alert("Please select the amount of years your partner has been at this job.");
	   theForm.joblength2.focus();
	   return (false);
    }
    if (theForm.dobyear2.value == "")
    {
        alert("Please enter your partner\'s birthday.");
        theForm.dobyear2.focus();
        return (false);
    }
    if ( !checkNum (theForm.dobyear2.value))
  {
    alert("Please check your partner's birthday.");
    theForm.dobyear2.focus();
    return (false);
  }
    if ( theForm.dobyear2.value >=2005 || theForm.dobyear2.value <=1900 )
  {
    alert("Sorry, your partner's birth year is unreasonable.");
    theForm.dobyear2.focus();
    return (false);
  }
}

//check mortgage/tenant details

	if( theForm.homeownerstatus.value == "Joint Home Owner" || theForm.homeownerstatus.value == "Home Owner" || theForm.homeownerstatus.value == "Mortgage Payer")
	{
	  if (theForm.purchasedate.value == "")
	  {
	    alert("Please enter the date of purchase.");
	    theForm.purchasedate.focus();
	    return (false);
	  }
	  if (theForm.purchaseprice.value == "")
	  {
	    alert("Please enter the purchase price.");
	    theForm.purchaseprice.focus();
	    return (false);
	  }
	  
	  if (theForm.marketvalue.value == "")
	  {
	    alert("Please enter the estimated market value right now.");
	    theForm.marketvalue.focus();
	    return (false);
	  }
	  if (theForm.ownership.value == "")
	  {
	    alert("Please select who owns the property.");
	    theForm.ownership.focus();
	    return (false);
	  }
	   if (theForm.mortgagetype.value == "")
	  {
	    alert("Please select the mortgage type.");
	    theForm.mortgagetype.focus();
	    return (false);
	  }
	  if (theForm.propertytype.value == "")
	  {
	    alert("Please select the property type.");
	    theForm.propertytype.focus();
	    return (false);
	  }
	  
	  if (theForm.constructfrom.value == "")
	  {
	    alert("Please select the material constructed from.");
	    theForm.constructfrom.focus();
	    return (false);
	  }
	  if (theForm.mortgagelender.value == "")
	  {
	    alert("Please enter the name of mortgage lender.");
	    theForm.mortgagelender.focus();
	    return (false);
	  } 	  
	  
	  if (theForm.mortgage1.value == "")
	  {
	    alert("Please enter your amount oustanding on mortgage.");
	    theForm.mortgage1.focus();
	    return (false);
	  }
	  
	  if (theForm.monthpay1.value == "")
	  {
	    alert("Please enter your monthly payments for mortgage.");
	    theForm.monthpay1.focus();
	    return (false);
	  }	
	 }
	
	// check tenant & mortgage details

	if(theForm.annualincome.value == "")
	{
		alert("Please enter your annual net income after tax");
		theForm.annualincome.focus();
		return (false);			
	}			
	if(theForm.maidenname.value == "")
	{
		alert("Please enter your mother's maiden name");
		theForm.maidenname.focus();
		return (false);			
	}

  return (true);
}

function checkcontactform(theForm)
{
	if(theForm.name.value=="")
	{
		alert("Please enter your name");
		theForm.name.focus();
		return (false);			
	}
	  if (theForm.email.value == "")
	  {
	    alert("Please enter your email address.");
	    theForm.email.focus();
	    return (false);
	  }
	  
	  if ( !checkemail(theForm.email.value) )
	  {
	    alert("Sorry, your email format is incorrect.");
	    theForm.email.focus();
	    return (false);
	  }
	  if (theForm.comments.value=="")
	  {
	    alert("Please enter your comments.");
	    theForm.comments.focus();
	    return (false);	  	
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////
