// JavaScript Document
/***********************form1 functions**********************************************/
var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function passportcheck(passport_number)
{
	if(isAlphanum(passport_number)==true)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isValid(parm,val) 
{
	var k= 0;
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) 
	{
		if (val.indexOf(parm.charAt(i),0) != -1) k++;
	}
	return k;
}

function isValidAlphanum(parm,val) 
{
	if (parm == "") return true;
	for (i=0; i<parm.length; i++)
	{
		if (val.indexOf(parm.charAt(i),0) == -1) return false;
	}
	return true;
}

function IsFloat(sText)
{
 var ValidChars = "0123456789.";
 var IsNumber=true;
 var Char;
 
 var check = 0;
  for (i = 0; i < sText.length && IsNumber == true; i++) 
 { 
  Char = sText.charAt(i);
  
  if( (Char == ".") && (check==1))
   IsNumber = false;
  if(Char == ".")  
   check = 1;
  
  if (ValidChars.indexOf(Char) == -1) 
  {
   IsNumber = false;
  }
 }
   return IsNumber;
   
}

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;
}

function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isNumber(parm) {return isValid(parm,numb);}
function isAlphanum(parm) {return isValidAlphanum(parm,lwr+upr+numb);}
function isNum(parm){return isValidAlphanum(parm,numb);}


/*function driverlicence(driver_licence)
{
	if(isAlphanum(driver_licence)==true)
	{
		alpha=isAlpha(driver_licence);
		num=isNumber(driver_licence);
		if(num>=4 && alpha<=2)
		{
			if(isNum(driver_license.charAt(2))&& isNum(driver_licence.charAt(3))==true)
			{
				return driver_license;
			}
			else
			{
				return false;
			}
		}
		else 
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}*/


/*function mobilecheck(home_phone_mobile)
{
	var exp1=/(^61[234578]\d{8}$)|(^[0][234578]\d{8}$)/;
	if(emptycell(home_phone_mobile)==false)
	{
		return true;
	}
	if(home_phone_mobile.match(exp1))
	{
		
		home_phone_mobile=home_phone_mobile.split(' ').join('');
		home_phone_mobile=home_phone_mobile.split('+').join('');
		home_phone_mobile=home_phone_mobile.split('-').join('');
		home_phone_mobile=home_phone_mobile.split('(').join('');
		home_phone_mobile=home_phone_mobile.split(')').join('');
		if(home_phone_mobile.charAt(0)==6)
		{
			var h = home_phone_mobile.substr(0,2);
			var e = home_phone_mobile.substr(3,home_phone_mobile.length);     
			home_phone_mobile = h+e;
		}
	 	return true;
	}
	else
	{
		return false;
	}
	
}*/

function emailCheck(email)
{
 var tmp = "" + email + "", s = tmp.replace(/^\s*|\s*$/g, "");
 var re = /^(\w|[^_]\.|[\-])+((\@){1}([^_]))(([a-z]|[\d]|[\-]|\.)+|([^_]\.[^_])*)+\.[a-z]{2,3}$/i
 if (!re.test(s)) 
 {
  return false;
 }
 re =  /\.(a[c-gil-oq-uwz]|b[a-bd-jm-or-tvwyz]|c[acdf-ik-orsuvx-z]|d[ejkmoz]|e[ceghr-u]|f[i-kmorx]|g[abd-ilmnp-uwy]|h[kmnrtu]|i[delm-oq-t]|j[emop]|k[eg-imnprwyz]|l[a-cikr-vy]|m[acdghk-z]|n[ace-giloprtuz]|om|p[ae-hk-nrtwy]|qa|r[eouw]|s[a-eg-ort-vyz]|t[cdf-hjkm-prtvwz]|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[admrw]|com|edu|net|org|mil|gov|biz)$/i
 if (!re.test(s)) 
 {
  return false;
 }
 re = /\@\@/
 return(!re.test(s));
}

function emptycell(value)
{
	if(value=='')
	{
		return false;
	}
	else
	{
		return true;
	}
}

function checkyear(DateOfBirth_Day,DateOfBirth_Month,DateOfBirth_Year)
{
today=new Date();
var d = new Date();	
var day1 = DateOfBirth_Day, day2 =d.getDate();
var month1 = DateOfBirth_Month , month2 = d.getMonth()+1;
var year1 = DateOfBirth_Year, year2 = d.getFullYear();
date1 = year1+"/"+month1+"/"+day1;
date2 = year2+"/"+month2+"/"+day2;
firstDate = Date.parse(date1);
secondDate= Date.parse(date2);
msPerDay = 24 * 60 * 60 * 1000;
dbd = ((secondDate.valueOf()-firstDate.valueOf())/ msPerDay)+1 ;
var bb = dbd/((30*4)+(31*7)+(28) + 0.25);
bbc = Math.floor(bb);
	if(bb<18)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/*function checkHomePhone(home_phone_day)
{
	
	var exp1=/(^[2-9][0-8][0-9][2-9][0-9][0-9]\d{4}$)/;
	var exp2=/(^[2-9][0-8][0-9]55501[0-9][0-9]$)/;
	if(home_phone_day.match(exp1))
	{
		if(!home_phone_day.match(exp2))
		{
			return true;
		}
		return false;
	}
	else
	{
		return false;
	}
}*/


/***********************form2 functions**********************************************/
/*function checkworkphone(work_phone)
{
	var exp1=/(\+61\s[0][234578]\s\d{4}\s\d{4}$)|(^61\s\d{4}$)|([(][0][234578][)]\s\d{4}\s\d{4}$)|(^[0][234578]-\d{4}-\d{4}$)|(^[0][234578]\d{8}$)|(^61[234578]\d{8}$)|(^1[38][0][0]\d{6}$)|(^13\d{4}$)|(^180\d{4})/;
	if(work_phone.match(exp1))
	{
		
		work_phone=work_phone.split(' ').join('');
		work_phone=work_phone.split('+').join('');
		work_phone=work_phone.split('-').join('');
		work_phone=work_phone.split('(').join('');
		work_phone=work_phone.split(')').join('');
		if(work_phone.charAt(0)==6)
		{
			var h = work_phone.substr(0,2);
			var e = work_phone.substr(3,work_phone.length);     
			work_phone = h+e;
		}
	 	return work_phone;
	}
	else
	{
		return false;
	}
}*/
function checkcomainnumber(employer_phone)
{
	
	var exp1=/(^0[0234578]\d{8}$)|(^61\d{9}$)|(^1[38]00\d{6}$)|(^13\d{4}$)|(^180\d{4}$)/;
	if(employer_phone.match(exp1))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function streetnumcheck(home_address_number)
{
	var exp1=/^\d+$/;
	if(home_address_number.match(exp1))
	{
		return true;
	}
	else
	{
		return false;
	}
}


function check_sort_code(sort_code)
{
	var exp1=/^\d{6}$/;
	if(sort_code.match(exp1))
	{
		return sort_code;
	}
	else
	{
		return false;
	}
}

function isLeapYear(yr) 
{
  return new Date(yr,2-1,29).getDate()==29;
}

function checkSsn(ssn)
{
	var exp1=/^([a-zA-Z]){2}( )?([0-9]){2}( )?([0-9]){2}( )?([0-9]){2}( )?([a-zA-Z]){1}?$/;
	if(exp1.test(ssn) == false){ return false; } else
	{
			return true;

	}
	return false
}

function checkBankRoutingNumber(sort_code)
{
	var exp1=/^\d{9}$/;
	if(sort_code.match(exp1))
	{
		return true;
	}
	return false;
}

function checkBankAccountNumber(bank_account_number)
{
	var exp1=/^\d{3}\d*$/;
	if(bank_account_number.match(exp1))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*************************form2 validation*****************************************************/

function Form2validation()
{
	var err='';
//	var expirationdatecheck=1;
//	ssn=document.form2.ssn.value;
//	driver_licence=document.form2.driver_licence_number.value;
//	expiration_Day=document.form2.expiration_Day.selectedIndex;
//	expiration_Month=document.form2.expiration_Month.selectedIndex;
//	expiration_Year=document.form2.expiration_Year.selectedIndex;
//	//driver_license_state= document.form2.driver_license_state.selectedIndex;
//	//active_military=document.form2.active_military.selectedIndex;
	income_type=document.form2.income_type.selectedIndex;
	income_net_monthly=document.form2.income_net_monthly.value;
	income_payment_type=document.form2.income_payment_type.selectedIndex;
	income_payment_period=document.form2.income_payment_period.selectedIndex;
	income_next_date1=document.form2.income_next_date1.value;
	income_next_date2=document.form2.income_next_date2.value;
	company_name=document.form2.company_name.value;
	work_time=document.form2.work_time.selectedIndex;
	loan_amount_requested=document.form2.loan_amount_requested.value;
//	employer_position=document.form2.employer_position.value;
//	employer_phone=document.form2.employer_phone.value;
	work_phone=document.form2.work_phone.value;
//	extension=document.form2.extension.value;
	bank_name=document.form2.bank_name.value;
	sort_code=document.form2.sort_code.value;
	bank_account_number=document.form2.bank_account_number.value;
	debit_card_verification=document.form2.debit_card_verification.selectedIndex;
//	
//	expiration = expiration_Year+"-"+expiration_Month+"-"+expiration_Day;
//	
//	if(expiration_Day<10)expiration_Day = '0'+expiration_Day;	
//	if(expiration_Month<10)expiration_Month = '0'+expiration_Month;
//	date1 = expiration_Day+"/"+expiration_Month+"/"+expiration_Year;
//	
//	if((emptycell(expiration_Day)==false)|(emptycell(expiration_Month)==false)|(emptycell(expiration_Year)==false))
//	{
//		expirationdatecheck=0;
//	}
//	
//	
//		if ((emptycell(ssn)==false)|(emptycell(driver_licence)==false)|(expirationdatecheck=0))err+='\nIndentification Details - ';
//		
//			if(emptycell(ssn)==false)err+= 'National Insurance Number,';
//			
//			if(emptycell(driver_licence)==false)err+= 'Driving Licence Number,';
//			
//			if(expirationdatecheck=0)err+= 'Expiration Date,';
//			
//			//if(emptycell(active_military)==false)err+= 'active UK military duty';
//		
//		if ((emptycell(ssn)==false)|(emptycell(driver_licence)==false)|(expirationdatecheck=0))err+=' is mandatory and should not be left empty\n';
//		
		if((emptycell(income_type)==false)|(emptycell(income_net_monthly)==false)|(emptycell(income_payment_type)==false)|(emptycell(income_payment_period)==false)|(emptycell(income_next_date1)==false)|(emptycell(income_next_date2)==false)|(emptycell(company_name)==false)|(emptycell(loan_amount_requested)==false)|(emptycell(work_phone)==false))err+='\nEmployment Details - ';
//		
		if(emptycell(income_type)==false)err+='Source of Income,';
//		
		if(emptycell(income_net_monthly)==false || isNaN(income_net_monthly))err+=' Net Pay per Pay,';
//
		if(emptycell(income_payment_type)==false)err+=' Payment Method,';
//		
		if(emptycell(income_payment_period)==false)err+=' Pay frequency,';
//
		if(emptycell(income_next_date1)==false)err+=' Next pay date,';
//
		if(emptycell(income_next_date2)==false)err+=' Following Pay Date,';
//		
		if(emptycell(company_name)==false)err+=' Company Name,';
		
		if(work_time==0)err+=' Work Time,';
		
		if(emptycell(loan_amount_requested)==false)err+=' Loan Amount,';
//		
//		if(emptycell(employer_position)==false)err+=' Job title,';
//				
//		if(emptycell(employer_phone)==false)err+=' Company Main Number,';
//				
		if(emptycell(work_phone)==false)err+= ' Work Number';
//				
		if((emptycell(income_type)==false)|(emptycell(income_net_monthly)==false)|(emptycell(income_payment_type)==false)|(emptycell(income_payment_period)==false)|(emptycell(income_next_date1)==false)|(emptycell(income_next_date2)==false)|(emptycell(loan_amount_requested)==false)|(emptycell(company_name)==false)|(emptycell(work_phone)==false))err+=' is mandatory and should not be left empty\n';
//		
//		
//	
	if((emptycell(sort_code)==false)|(emptycell(bank_account_number)==false))err+='\nBank Account Details - ';
//	
//	
//		
		if(emptycell(sort_code)==false)
		{
			err+='Sort code,';
		}
		if(sort_code.length<6 || isNaN(sort_code))
		{
			err+='Sort code should be 6 digits and all numbers,';
		}
		if(emptycell(bank_account_number)==false)
		{
			err+=' Bank Account Number,';
		}
		if(debit_card_verification==0){
			err+= ' Card Type';	
		}
//		
	if((emptycell(sort_code)==false)|(emptycell(bank_account_number)==false))err+=' is mandatory and should not be left empty\n';
//		
//		if(emptycell(ssn)==true)
//		{
//			if(checkSsn(ssn)==false)err+='\nInvalid National Insurance Number';
//		}
//		
//		// checking date for feb
//		if(expiration_Month=='02'){
//			if(expiration_Day>29){
//				err  += '\nPlease enter proper date';
//			}
//			else
//			if (!isLeapYear(expiration_Year))
//				{
//					if(expiration_Day>28){
//						err  += '\nPlease enter proper date';
//					}
//				}
//		}
//		if((expiration_Month=='04')|(expiration_Month=='06')|(expiration_Month=='09')| (expiration_Month=='11'))
//		{
//			if(expiration_Day>30){
//				err  += '\nPlease enter proper date';
//			}
//		}
//   
//		
///*		if((emptycell(sort_code)==true)&&(checkBankRoutingNumber(sort_code)==false))
//		{
//				err+='\nInvalid bank sort code';
//		}*/
//		
//		if((emptycell(bank_account_number)==true)&&(checkBankAccountNumber(bank_account_number)==false))
//		{
//				err+= '\nEntered Bank Account no invalid\n1. Max length 3 -17 digits\n2.  Numeric only';
//		}
//		
	if(err=='')
	{
		document.form2.dev_act2.value='submit2';
		document.form2.action='/registration/formprocess.php';
		document.form2.submit();
	}
	else
	{
		alert(err);
	}
}

/*************************form1 validation*****************************************************/

function Formvalidation()
{
	var err='';
	var datecheck=1;
	title_cd=document.form1.title_cd.selectedIndex;
	first_name=document.form1.first_name.value;
	middle_name= document.form1.middle_name.value;
	last_name= document.form1.last_name.value;
	email=document.form1.email.value;
	DateOfBirth_Day=document.form1.DateOfBirth_Day.selectedIndex;
	DateOfBirth_Month=document.form1.DateOfBirth_Month.selectedIndex;
	DateOfBirth_Year=document.form1.DateOfBirth_Year.selectedIndex;
	home_phone_day=document.form1.home_phone_day.value;
	home_phone_mobile=document.form1.home_phone_mobile.value;
	house_number=document.form1.house_number.value;
	house_name=document.form1.house_name.value;
	postal_town_city=document.form1.postal_town_city.value;
	county=document.form1.county.selectedIndex;
	zip=document.form1.zip.value;
	home_type=document.form1.home_type.selectedIndex;
	home_time=document.form1.home_time.selectedIndex;
	street=document.form1.street.value;
//	prev_house_name=document.form1.prev_house_name.value;
//	prev_home_city=document.form1.prev_home_city.value;
//	prev_home_county=document.form1.prev_home_county.selectedIndex;
//	prev_home_postcode=document.form1.prev_home_postcode.value;
//	
//	
	if(DateOfBirth_Day<10)DateOfBirth_Day = '0'+DateOfBirth_Day;	
	if(DateOfBirth_Month<10)DateOfBirth_Month = '0'+DateOfBirth_Month;
	date1 = DateOfBirth_Day+"/"+DateOfBirth_Month+"/"+DateOfBirth_Year;
//	
	if((emptycell(DateOfBirth_Month)==false)|(emptycell(DateOfBirth_Day)==false)|(emptycell(DateOfBirth_Year)==false))
	{
		datecheck=0;
	}
//	
///*******Contact details validation**********/	
//	
	if((emptycell(first_name)==false)|(emptycell(last_name)==false)|(emptycell(email)==false)|(datecheck==0)|(emptycell(home_phone_day)==false))err+='Contact Details - ';
	
	if(title_cd==0)err+= " Title,";
//	
	if(emptycell(first_name)==false)err+='First Name,';
//		
	if(emptycell(last_name)==false)err+=' Last name,';
//	
	if(emptycell(email)==false)err+=' Email,';
//	
	if(datecheck==0)err+=' Date of Birth,';
//	
	if(emptycell(home_phone_day)==false)err+= ' Home Phone';
	
	if(emailCheck(email)==false)err+=' Email Not Valid';
//	
	if((emptycell(first_name)==false)|(emptycell(last_name)==false)|(emptycell(email)==false)|(datecheck==0)|(emptycell(home_phone_day)==false))err+=' is mandatory and should not be left empty\n';
//	
//	
//	
	if(emptycell(DateOfBirth_Year)==true)
	{
		if(checkyear(DateOfBirth_Day,DateOfBirth_Month,DateOfBirth_Year)==false)
		{
			err+= '\nEntered Date of Birth invalid\nAge should be >= 18';
		}
	}
//	
//	/********************************/
//	// checking date for feb
	if(DateOfBirth_Month=='02'){
		if(DateOfBirth_Day>29){
			err  += '\nPlease enter proper date';
		}
		else
		if (!isLeapYear(DateOfBirth_Year))
			{
				if(DateOfBirth_Day>28){
					err  += '\nPlease enter proper date';
				}
			}
	}
	if((DateOfBirth_Month=='04')|(DateOfBirth_Month=='06')|(DateOfBirth_Month=='09')| (DateOfBirth_Month=='11'))
	{
		if(DateOfBirth_Day>30){
			err  += '\nPlease enter proper date';
		}
	}
	if(DateOfBirth_Month==0||DateOfBirth_Day==0||DateOfBirth_Year==0){
		err += ', please enter your date of birth ';	
	}
//   
//	
//	/*if(mobilecheck(home_phone_mobile)==false)
//	{
//		err+= '\ninsert mobile number properly';
//	}*/
//	
///**********address details validation**************/	
//
if((emptycell(postal_town_city)==false)|(emptycell(zip)==false)|(emptycell(street)==false)|(emptycell(home_type)==false)|(emptycell(home_time)==false))err+='\n\nAddress Details -  ';
//	
	if(emptycell(postal_town_city)==false)err+=' city,';
	
	if(emptycell(house_name)==false && emptycell(house_number)==false)err+= ' house number or name,'
	
	if(emptycell(county)==false)err+=' county,';
//
	if(emptycell(street)==false)err+=' street,';
//
	if(emptycell(zip)==false)err+=' postcode,';
//	
	if(emptycell(home_type)==false)err+=' Rent or Own,';
//
	if(emptycell(home_time)==false)err+=' Time at Address';
//
    if((emptycell(postal_town_city)==false)|(emptycell(zip)==false)|(emptycell(home_type)==false)|(emptycell(home_time)==false))err+=' is mandatory and should not be left empty\n';
//
	
	//if(document.getElementById('prevhomeid').style.display=='inline')
//	{
//		if(emptycell(prev_home_address_street)==false)err+='\n\nPrev Home Address is mandatory and should not be left empty\n';
////		
//		if(emptycell(prev_home_city)==false)err+='Prev Home City is mandatory and should not be left empty\n';
////		
//		if(emptycell(prev_home_county)==false)err+= 'Prev Home State is mandatory and should not be left empty\n';
////	
//		if(emptycell(prev_home_postcode)==false)err+='Prev Home Postcode is mandatory and should not be left empty\n';
//	}
//	
/*if((emptycell(zip)==true)&&(checkHomeZip(zip)==false))
{
	err+= '\nPlease Insert Proper Post Code\n';
}*/
//	
///*	if((emptycell(home_phone_day)==true)&&(checkHomePhone(home_phone_day)==false))
//	{
//			err+='\nInvalid Phone Number\n';
//	}*/
//	
//			
	if(err=='')
	{
		document.form1.dev_act.value='submit1';
		document.form1.action='step2.php';
		document.form1.submit();
	}
	else
	{
		alert(err);
	}
}
function prevhomeaddress()
	{
		if((document.form1.home_time.value== 'less_than_1_month')|(document.form1.home_time.value=='1_month')|(document.form1.home_time.value=='2_months')|(document.form1.home_time.value=='3_months')|(document.form1.home_time.value=='4_to_6_months'))
		{
			document.getElementById('prevhomeid').style.display='inline';
		}
		else
		{
			document.getElementById('prevhomeid').style.display='none';
		}
	}
function showDetails(showid)
{
	if(document.getElementById(showid).style.display=='none')
	{
		document.getElementById(showid).style.display = 'inline';
	}
	else
	{
		document.getElementById(showid).style.display='none';
	}
	if(document.getElementById(hideid).value != '')
	{
		hideid_var = document.getElementById(hideid).value;
		document.getElementById(hideid_var).style.display = 'none';
	}
	document.getElementById(hideid).value = showid;
}

