var daysInMonth = new Array(12);
daysInMonth[1]=31;
daysInMonth[2]=29; // must programmatically check this
daysInMonth[3]=31;
daysInMonth[4]=30;
daysInMonth[5]=31;
daysInMonth[6]=30;
daysInMonth[7]=31;
daysInMonth[8]=31;
daysInMonth[9]=30;
daysInMonth[10]=31;
daysInMonth[11]=30;
daysInMonth[12]=31;

// *** Check if given day/month/year combination is a valid date
// *** day - integer - day
// *** month - integer - month
// *** year - integer - year
function isDate (day, month, year) {
	if (! (year.length==4 && (month>=1&&month<=12) && (day>=1&&day<=31) ) )
		return false;
	var intYear = parseInt(year);
	var intMonth = parseInt(month);
	var intDay = parseInt(day);
	
	if (intDay > daysInMonth[intMonth])
		return false;
		
	if ((intMonth == 2) && (intDay > daysInFebruary(intYear)))
		return false;
		
	return true;
}
function daysInFebruary (year) {
	return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

// *** validate creditamount
// *** obj - object - creditamount input element
function validate_creditAmount(obj) {
	var reAmount=/^((\d+(\,\d{1,2})?)|((\d*\,)?\d{1,2}))$/
 	if ($(obj).val()!=''&&!reAmount.test($(obj).val())) {
		$(obj).addClass("errorElement");
		$(obj).after("<label class=\"errorLabel\">Het veld 'gewenst leenbedrag' bevat een ongeldig bedrag.&nbsp;</label>");
		return false;
	}
	if ($(obj).val() < 2500) {
		$(obj).addClass("errorElement");
		$(obj).after("<label class=\"errorLabel\">Het veld 'gewenst leenbedrag' is te laag. Het minimumbedrag is &euro; 2.500&nbsp;</label>");
		return false;
	}
	if ($(obj).val() > 50000) {
		$(obj).addClass("errorElement");
		$(obj).after("<label class=\"errorLabel\">Het veld 'gewenst leenbedrag' is te hoog. Het maximumbedrag is &euro; 50.000&nbsp;</label>");
		return false;
	}
	return true;
}

// *** validate postcode
// *** obj - object - postcode input element
function validate_addressPostcode(obj) {
	var rePostcode=/^\d{4}[A-Za-z]{2}$/;
	if ($(obj).val()!=''&&!rePostcode.test($(obj).val())) {
		$(obj).addClass("errorElement");
		$(obj).after("<label class=\"errorLabel\">De postcode is ongeldig.&nbsp;</label>");
		return false;
	}
	return true;
}
function validate_previousAddressPostcode(obj) {
	return validate_addressPostcode(obj);
}

// *** validate email address
// *** obj - object - email address input element
function validate_emailaddress(obj) {
	var reEmailAddress=/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if ($(obj).val()!=''&&!reEmailAddress.test($(obj).val())) {
		$(obj).addClass("errorElement");
		$(obj).after("<label class=\"errorLabel\">Het emailadres is ongeldig.&nbsp;</label>");
		return false;
	}
	return true;
}

// *** validate telephonenumber
// *** obj - object - telephonenumber input element
function validateTelephoneNumber(obj) {
	var rePhone=/^0\d{9}$/
	if ($(obj).val()!=''&&!rePhone.test($(obj).val())) {
		$(obj).addClass("errorElement");
		$(obj).after("<label class=\"errorLabel\">Het telefoonnummer is ongeldig.&nbsp;</label>");
		return false;
	}
	return true;
}
// *** Check if either the phone number or mobile phone number field have been filled.
// *** phoneObj - object - input element for phone number
// *** mobileObj - object - input element for mobile phone number
function validatePhoneRequired(phoneObj, mobileObj){
	if ($(phoneObj) != null && $(mobileObj) != null) {
		var phoneNumber = $(phoneObj).val();
		var mobileNumber = $(mobileObj).val();

		if (phoneNumber == "" && mobileNumber == "") {
			$(phoneObj).addClass("errorElement");
			$(mobileObj).addClass("errorElement");
			$(mobileObj).after("<label class=\"errorLabel\">Het veld 'Vast telefoonnummer' of 'Mobiel telefoonnummer' moet een waarde bevatten.&nbsp;</label>");
			return false;
		}
	}
	return true;
}
function validate_telephoneNumber(obj) {
	var valid = true
	valid = (valid && validateTelephoneNumber(obj) ? true : false);
	valid = (valid && validatePhoneRequired($("#telephoneNumber"), $("#telephoneNumberMobile")) ? true : false);
	return valid;
}
function validate_telephoneNumberMobile(obj) {
	var valid = true
	valid = (valid && validateTelephoneNumber(obj) ? true : false);
	return valid;
}

// *** Validate bankaccountno ***
// *** banktypeobj - object - input element of banktype
// *** accnoobj - object - input element of bank accountno
function validateBankAccountno(banktypeobj, accnoobj) {
	var valid=true;

	if ($(banktypeobj) != null && $(accnoobj) != null) {
		var banktype = $(banktypeobj).val();
		var accno = $(accnoobj).val();
		
		if (banktype!="" && accno!="") {
			if (banktype!=5) { // Normal bankaccount
				if (accno!='') {
					var reBankAccountno=/^\d{9}$/
					valid=reBankAccountno.test(accno);
					
					if (accno=='123456789'||accno=='111111110'||accno=='222222220'||accno=='333333330'||accno=='444444440'||
						accno=='555555550'||accno=='666666660'||accno=='777777770'||accno=='888888880'||accno=='999999990'||accno=='000000000')
						valid=false;
						
					var checkno=0;
					for(var i=0;i<9;i++)
						checkno=checkno+((9-i)*accno.charAt(i));
					if (checkno%11!=0)
						valid=false;
				}
			} else { // Postbank account
				var rePostbankAccountno=/^\d{2,7}$/
				valid=rePostbankAccountno.test(accno);
			}
			if (!valid) {
				if (banktype==5) { // postbank account extra foutmelding
					$(accnoobj).addClass("errorElement");
					$(accnoobj).after("<label class=\"errorLabel\">Het bankrekeningnummer is ongeldig. Wanneer u een bankrekeningnummer heeft dat bestaat uit 9 cijfers, kiest u dan alstublieft voor ING bank (banknummer 9 cijfers).&nbsp;</label>");
				} else if (banktype==6){ // ING bank extra foutmelding
					$(accnoobj).addClass("errorElement");
					$(accnoobj).after("<label class=\"errorLabel\">Het bankrekeningnummer is ongeldig. Wanneer u een bankrekeningnummer heeft dat bestaat uit minder dan 9 cijfers (voorheen Postbank), kiest u dan alstublieft voor ING Bank (banknummer t/m 7 cijfers).&nbsp;</label>");
				} else { //normal bankaccount
					$(accnoobj).addClass("errorElement");
					$(accnoobj).after("<label class=\"errorLabel\">Het bankrekeningnummer is ongeldig.&nbsp;</label>");
				}
			}
		}
	}
	return valid;
}
function validate_bankAccountNumber(obj) {
	return validateBankAccountno($("#bankType"), obj);
}

// *** validate year/month combination 
// *** monthobj - object - month input element
// *** yearobj - object - year input element
// *** futurecheck - boolean - indicating if the mont/year elements must be in the future (true) or in the past (false).
// *** offset - integer - amount of day that the month/year elements may be in the future/past (depending on the setting of futurecheck.
function validateDate(monthobj, yearobj, futureCheck, offset) {
	var valid = true;
	
	if ($(yearobj).val()!='') {
		if ($(yearobj).val()<1900||$(yearobj).val()>2100) {
			$(yearobj).after("<label class=\"errorLabel\">Het veld bevat een ongeldig jaar.&nbsp;</label>");
			valid = false;
		} else {
			var tmpDate=new Date();
			var currDate=new Date(tmpDate.getFullYear(), tmpDate.getMonth(), tmpDate.getDate());
			var objDate=new Date($(yearobj).val(), ($(monthobj).val()-1), (currDate.getDate()+offset));
			if (futureCheck) {
				if (objDate<currDate) {
					$(yearobj).addClass("errorElement");
					$(monthobj).addClass("errorElement");			
					$(monthobj).after("<label class=\"errorLabel\">Deze datum kan niet in het verleden liggen.&nbsp;</label>");
					valid = false;
				}
			} else {
				if (objDate>currDate) {
					$(yearobj).addClass("errorElement");
					$(monthobj).addClass("errorElement");			
					$(monthobj).after("<label class=\"errorLabel\">Deze datum kan niet in de toekomst liggen.&nbsp;</label>");
					valid = false;
				}
			}
		}
	}
	return valid;
}

// *** Validate birthdate against the given year/month objects
// *** monthobj - object - input element containing the month
// *** yearobj - object - input element containing the year
// *** birthdatedayobj - object - birthdateday input element
// *** birthdatemonthobj - object - birthdatemonth input element
// *** birthdateyearobj - object - birthdateyear input element
function validateDateAgainstBirthdate(monthobj, yearobj, birthdatedayobj, birthdatemonthobj, birthdateyearobj){
	var valid=true;

	if($(monthobj) != null && $(yearobj) !=null && $(birthdatedayobj) !=null && $(birthdatemonthobj) !=null && $(birthdateyearobj) !=null){
		var day = $(birthdatedayobj).val();
		var month = $(birthdatemonthobj).val();
		var year = $(birthdateyearobj).val();
	
		if (day!='' && month!='' && year!='' && $(monthobj).val()!='' && $(yearobj).val()!='') {
			valid=isDate(day, month, year);
	
			if (!valid) {
				$(birthdatedayobj).addClass("errorElement");
				$(birthdatemonthobj).addClass("errorElement");
				$(birthdateyearobj).addClass("errorElement");
				$(birthdateyearobj).after("<label class=\"errorLabel\">De geboortedatum bevat een ongeldige datum.&nbsp;</label>");
			} else {
				var birthDate=new Date(year, month-1, day);
				var checkDate=new Date($(yearobj).val(), $(monthobj).val(), 1);
	
				if (birthDate>checkDate) {
					$(yearobj).addClass("errorElement");
					$(monthobj).addClass("errorElement");
					$(yearobj).after("<label class=\"errorLabel\">Deze datum dient later te zijn dan uw geboortedatum.&nbsp;</label>");
					valid=false;
				}
			}
		}
	}	
	return valid;
}

// *** validate year/month combination 
function validateEmploymentDate(yearobj, birthdatedayobj, birthdatemonthobj, birthdateyearobj) {
	valid = true;
	
	if ($(yearobj).val()!='') {
		if ($(yearobj).val()<1900||$(yearobj).val()>2100) {
			$(yearobj).after("<label class=\"errorLabel\">Het veld bevat een ongeldig jaar.&nbsp;</label>");
			valid = false;
		} else {
			if($(birthdatedayobj) != null && $(birthdatemonthobj) != null && $(birthdateyearobj) != null){
				var day = $(birthdatedayobj).val();
				var month = $(birthdatemonthobj).val();
				var year = $(birthdateyearobj).val();
	
				if (day!='' && month!='' && year!='' && $(yearobj).val()!='') {
					var minDate = new Date(Number(year) + 12, month, day)
					var objDate = new Date($(yearobj).val(), 1, 1);
					if (objDate<minDate) {
						$(yearobj).addClass("errorElement");
						$(yearobj).after("<label class=\"errorLabel\">De aanvangsdatum van het dienstverband is ongeldig in combinatie met het veld geboortedatum.&nbsp;</label>");
						valid = false;
					}
				}
			}
		}
	}
	return valid;
}

function validate_addressSinceYear(obj) {
	var valid = true
	valid = (valid && validateDate($("#addressSinceMonth"), obj, false, 0) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#addressSinceMonth"), obj, $("#dateOfBirthDay"), $("#dateOfBirthMonth"), $("#dateOfBirthYear")) ? true : false);
	return valid;
}
function validate_employmentYear(obj) {
	var valid = true
	valid = (valid && validateDate($("#employmentMonth"), obj, false, 0) ? true : false);
	valid = (valid && validateEmploymentDate(obj, $("#dateOfBirthDay"), $("#dateOfBirthMonth"), $("#dateOfBirthYear")) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#employmentMonth"), obj, $("#dateOfBirthDay"), $("#dateOfBirthMonth"), $("#dateOfBirthYear")) ? true : false);
	return valid;
}
function validate_benefitSinceYear(obj) {
	var valid = true
	valid = (valid && validateDate($("#benefitSinceMonth"), obj, false, 0) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#benefitSinceMonth"), obj, $("#dateOfBirthDay"), $("#dateOfBirthMonth"), $("#dateOfBirthYear")) ? true : false);
	return valid;
}
function validate_additionalBenefitSinceYear(obj) {
	var valid = true
	valid = (valid && validateDate($("#additionalBenefitSinceMonth"), obj, false, 0) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#additionalBenefitSinceMonth"), obj, $("#dateOfBirthDay"), $("#dateOfBirthMonth"), $("#dateOfBirthYear")) ? true : false);
	return valid;
}
function validate_employmentContractTillYear(obj) {
	var valid = true
	valid = (valid && validateDate($("#employmentContractTillMonth"), obj, true, 0) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#employmentContractTillMonth"), obj, $("#dateOfBirthDay"), $("#dateOfBirthMonth"), $("#dateOfBirthYear")) ? true : false);
	return valid;
}

function validate_employmentYearSpouse(obj) {
	var valid = true
	valid = (valid && validateDate($("#employmentMonthSpouse"), obj, false, 0) ? true : false);
	valid = (valid && validateEmploymentDate(obj, $("#dateOfBirthDaySpouse"), $("#dateOfBirthMonthSpouse"), $("#dateOfBirthYearSpouse")) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#employmentMonthSpouse"), obj, $("#dateOfBirthDaySpouse"), $("#dateOfBirthMonthSpouse"), $("#dateOfBirthYearSpouse")) ? true : false);
	return valid;
}
function validate_benefitSinceYearSpouse(obj) {
	var valid = true
	valid = (valid && validateDate($("#benefitSinceMonthSpouse"), obj, false, 0) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#benefitSinceMonthSpouse"), obj, $("#dateOfBirthDaySpouse"), $("#dateOfBirthMonthSpouse"), $("#dateOfBirthYearSpouse")) ? true : false);
	return valid;
}
function validate_additionalBenefitSinceYearSpouse(obj) {
	var valid = true
	valid = (valid && validateDate($("#additionalBenefitSinceMonthSpouse"), obj, false, 0) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#additionalBenefitSinceMonthSpouse"), obj, $("#dateOfBirthDaySpouse"), $("#dateOfBirthMonthSpouse"), $("#dateOfBirthYearSpouse")) ? true : false);
	return valid;
}
function validate_employmentContractTillYearSpouse(obj) {
	var valid = true
	valid = (valid && validateDate($("#employmentContractTillMonthSpouse"), obj, true, 0) ? true : false);
	valid = (valid && validateDateAgainstBirthdate($("#employmentContractTillMonthSpouse"), obj, $("#dateOfBirthDaySpouse"), $("#dateOfBirthMonthSpouse"), $("#dateOfBirthYearSpouse")) ? true : false);
	return valid;
}

// *** Validate birthdate against the given minimum and maximum age
// *** birthdatedayobj - object - birthdateday input element
// *** birthdatemonthobj - object - birthdatemonth input element
// *** birthdateyearobj - object - birthdateyear input element
// *** minYear - integer - containing minimum age 
// *** maxYear - integer - containing maximum age
function validateBirthDate(birthdatedayobj, birthdatemonthobj, birthdateyearobj, minYear, maxYear) {
	if($(birthdatedayobj) !=null && $(birthdatemonthobj) !=null && $(birthdateyearobj) !=null && minYear != 0 && maxYear !=0){
		var day = $(birthdatedayobj).val();
		var month = $(birthdatemonthobj).val();
		var year = $(birthdateyearobj).val();
	
		var valid = true;
		if (day!='' && month!='' && year!='') {
			valid=isDate(day, month, year);
			if (!valid) {
				$(birthdatedayobj).addClass("errorElement");
				$(birthdatemonthobj).addClass("errorElement");
				$(birthdateyearobj).addClass("errorElement");			
				$(birthdateyearobj).after("<label class=\"errorLabel\">De geboortedatum bevat een ongeldige datum.&nbsp;</label>");
			}
			
			if (valid) {
				var birthDate=new Date(year, month-1, day);
				var currDate=new Date();
				
				var minDate=new Date(currDate.getFullYear()-minYear, currDate.getMonth(), currDate.getDate());
				var maxDate=new Date(currDate.getFullYear()-maxYear-1, currDate.getMonth(), currDate.getDate());
				
				if (birthDate<=maxDate) {
					$(birthdateyearobj).addClass("errorElement");			
					$(birthdateyearobj).after("<label class=\"errorLabel\">Het veld geboortedatum bevat een te hoge waarde. Santander verstrekt geen leningen aan personen ouder dan "+maxYear+" jaar.&nbsp;</label>");
					valid=false;
				}
				if (birthDate>minDate) {
					$(birthdateyearobj).addClass("errorElement");			
					$(birthdateyearobj).after("<label class=\"errorLabel\">Het veld geboortedatum bevat een te lage waarde. Santander verstrekt geen leningen aan personen jonger dan "+minYear+" jaar.&nbsp;</label>");
					valid=false;
				}
			}
		}
	}
	return valid;
}
function validate_dateOfBirthDay(obj) {
	return validateBirthDate(obj, $("#dateOfBirthMonth"), $("#dateOfBirthYear"), 18, 73);
}
function validate_dateOfBirthDaySpouse(obj) {
	return validateBirthDate(obj, $("#dateOfBirthMonthSpouse"), $("#dateOfBirthYearSpouse"), 18, 73);
}

// *** Validate number of parttime hours ***
function validate_employmentParttimeHours(obj) {
	if ($(obj).val()!=''&&$(obj).val()<1||$(obj).val()>35) {
		$(obj).addClass("errorElement");			
		$(obj).after("<label class=\"errorLabel\">het veld bevat een ongeldig urental voor een parttime betrekking. Het minimum is 1 uur en het maximum is 35 uur. Selecteer bij meer dan 35 uur een fulltime dienstverband.&nbsp;</label>");
		return false;
	}
	return true;
}
function validate_employmentParttimeHoursSpouse(obj) {
	return validate_employmentParttimeHours(obj);
}

// *** check if either applicator or spouse have income ***
// *** appIncomeObj - object - input element containing applicator income type 
// *** spouseIncomeObj - object - input element containing spouse income type
// *** maritialStatusObj - object - input element relation between applicator and spouse
function validateIncome(appIncomeObj, spouseIncomeObj, maritialStatusObj) {
	if($(appIncomeObj) != null && $(spouseIncomeObj) !=null && $(maritialStatusObj) !=null){
		var maritialStatusType = $(maritialStatusObj).val();
		var appIncomeType = $(appIncomeObj).val();
		var spouseIncomeType = $(spouseIncomeObj).val();

		if ((maritialStatusType==2||maritialStatusType==3||maritialStatusType==5)&&appIncomeType==5) {
			$(appIncomeObj).addClass("errorElement");			
			$(appIncomeObj).after("<label class=\"errorLabel\">De aanvrager moet een inkomen hebben.&nbsp;</label>");
			return false;
		}
		if (appIncomeType==5&&spouseIncomeType==5) {
			$(appIncomeObj).addClass("errorElement");			
			$(appIncomeObj).after("<label class=\"errorLabel\">Uzelf óf uw partner moet een inkomen hebben om een lening te kunnen aanvragen.&nbsp;</label>");
			return false;
		}
	}
	return true;
}
// *** Check if Income Amount is larger than 0.
// *** Obj - object - input element for amount
function validateIncomeAmount(obj){
	if ($(obj) != null) {
		var incomeAmount = $(obj).val();

		if (incomeAmount <= 0) {
			$(obj).addClass("errorElement");
			$(obj).after("<label class=\"errorLabel\">U moet hier een bedrag invullen.&nbsp;</label>");
			return false;
		}
	}
	return true;
}
function validate_monthlyIncome(obj){
	return validateIncomeAmount(obj);
}
function validate_additionalBenefitAmount(obj){
	return validateIncomeAmount(obj);
}
function validate_monthlyIncomeSpouse(obj){
	return validateIncomeAmount(obj);
}
function validate_additionalBenefitAmountSpouse(obj){
	return validateIncomeAmount(obj);
}
function validate_benefitAmount(obj){
	return validateIncomeAmount(obj);
}
function validate_benefitAmountSpouse(obj){
	return validateIncomeAmount(obj);
}


function validate_occupationType(obj) {
	return validateIncome(obj, $("#occupationTypeSpouse"), $("#maritialStatusType"));
}
