
function TOOLSCONTEXT_Local_CustomValidate_bday(formEl, minAge, maxAge) {
	if (!minAge) {
		minAge = 18;
	}
	if (!maxAge) {
		maxAge = 75;
	}
	if (formEl.value == '') {
		return;
	}
	if (formEl.name != 'seguorders.dp.bday.value.year') {
		return TOOLSCONTEXT_Local_ValidateSucceed(formEl);
	}
	
	var year = formEl.form.elements['seguorders.dp.bday.value.year'].value;
	
	if (year == '') {
		TOOLSCONTEXT_Local_DisplayAlert('noBirthday');
		formEl.select();
		TOOLSCONTEXT_Local_CustomValidateResult[formEl.name] = false;
		return false;
	}
	
	var month = formEl.form.elements['seguorders.dp.bday.value.month'].value;
	var day = formEl.form.elements['seguorders.dp.bday.value.day'].value;
	
	if (!(TOOLSCONTEXT_Local_IsInteger(year) && TOOLSCONTEXT_Local_IsInteger(month) && TOOLSCONTEXT_Local_IsInteger(day))) {
		return TOOLSCONTEXT_Local_ValidateFail(formEl, 'birthdayInvalid');
	}
	
	var today = new Date();
	
	var bday = new Date();
	bday.setFullYear(year, month - 1, day);
	
	var oneYear = 365 * 24 * 60 * 60 * 1000; //approximate - does not account for leap year etc
	
	var age = (today.getTime() - bday.getTime()) / oneYear;
	
	ageEl = document.getElementById('bday_age');
	ageEl.innerHTML = '(' + parseInt(age, 10).toString() + ')';
	
	if (age < minAge) {
		TOOLSCONTEXT_Local_DisplayAlert('tooYoung' + minAge);
		formEl.select();
		TOOLSCONTEXT_Local_CustomValidateResult[formEl.name] = false;
		return false;
	}
	
	if (age > maxAge + 1) {
		TOOLSCONTEXT_Local_DisplayAlert('tooOld');
		formEl.select();
		TOOLSCONTEXT_Local_CustomValidateResult[formEl.name] = false;
		return false;
	}
	
	return TOOLSCONTEXT_Local_ValidateSucceed(formEl);
	
}

function TOOLSCONTEXT_Local_CustomValidate_email_fail() {
	TOOLSCONTEXT_Local_DisplayAlert('invalidEmail');
	TOOLSCONTEXT_Local_CustomValidateResult['email'] = false;
	return false;
}

function TOOLSCONTEXT_Local_CustomValidate_zip(formEl) {
	if (!(TOOLSCONTEXT_Local_IsInteger(formEl.value) && (formEl.value.toString().length == 5))) {
		return TOOLSCONTEXT_Local_ValidateFail(formEl, 'zipInvalid');
	}
	return TOOLSCONTEXT_Local_ValidateSucceed(formEl);
}

function TOOLSCONTEXT_Local_ValidateVehicleAge(formEl) {
	if (!(TOOLSCONTEXT_Local_IsInteger(formEl.value) && (formEl.value.toString().length == 4))) {
		return TOOLSCONTEXT_Local_ValidateFail(formEl, 'yearInvalid');
	}
	var today = new Date();
	var thisYear = today.getFullYear();
	if ((thisYear - parseInt(formEl.value)) > 12) {
		return TOOLSCONTEXT_Local_ValidateFail(formEl, 'vehicleTooOld');
	}
	return TOOLSCONTEXT_Local_ValidateSucceed(formEl);
}

function TOOLSCONTEXT_Local_checkVehicleAge(formEl,maxAge) {
	if (!(TOOLSCONTEXT_Local_IsInteger(formEl.value) && (formEl.value.toString().length == 4))) {
		return TOOLSCONTEXT_Local_ValidateFail(formEl, 'yearInvalid');
	}
	var today = new Date();
	var thisYear = today.getFullYear();
	if ((thisYear - parseInt(formEl.value)) > maxAge) {
		return TOOLSCONTEXT_Local_ValidateFail(formEl, 'vehicleOld'+maxAge);
	}
	return TOOLSCONTEXT_Local_ValidateSucceed(formEl);
}

function TOOLSCONTEXT_Local_CustomValidate_year(formEl,maxAge) {
	return TOOLSCONTEXT_Local_checkVehicleAge(formEl,maxAge);
}

function TOOLSCONTEXT_Local_CustomValidate_traileryear(formEl) {
	return TOOLSCONTEXT_Local_ValidateVehicleAge(formEl);
}

function TOOLSCONTEXT_Local_CustomValidate_boatyear(formEl) {
	return TOOLSCONTEXT_Local_ValidateVehicleAge(formEl);
}

function TOOLSCONTEXT_Local_ValidateFail(formEl, message) {
	TOOLSCONTEXT_Local_DisplayAlert(message);
	TOOLSCONTEXT_Local_CustomValidateResult[formEl.name] = false;
	return false;
}

function TOOLSCONTEXT_Local_ValidateSucceed(formEl) {
	TOOLSCONTEXT_Local_CustomValidateResult[formEl.name] = true;
	return true;
}

function TOOLSCONTEXT_Local_CustomValidate_email(formEl) {
	str = formEl.value;
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return TOOLSCONTEXT_Local_CustomValidate_email_fail();
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return TOOLSCONTEXT_Local_CustomValidate_email_fail();
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return TOOLSCONTEXT_Local_CustomValidate_email_fail();
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return TOOLSCONTEXT_Local_CustomValidate_email_fail();
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return TOOLSCONTEXT_Local_CustomValidate_email_fail();
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return TOOLSCONTEXT_Local_CustomValidate_email_fail();
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return TOOLSCONTEXT_Local_CustomValidate_email_fail();
		 }
		return TOOLSCONTEXT_Local_ValidateSucceed(formEl);					

}

function TOOLSCONTEXT_Local_DisplayAlert(alertKey) {
	var alerts = new Array();
	alerts.en = new Array();
	alerts.es = new Array();
	
	alerts.en.noBirthday = 'Please enter your birth day';
	alerts.es.noBirthday = 'Por favor introduce tu fecha de nacimiento';
	
	alerts.en.birthdayInvalid = 'Birthday not valid - please enter it in numeric form';
	alerts.es.birthdayInvalid = 'Fecha de nacimiento no valida, por favor introduce solo numeros';
	
	alerts.en.tooOld = 'You must 75 years old or younger';
	alerts.es.tooOld = 'Debes tener 75 años o menos';
	
	alerts.en.tooYoung16 = 'You must be at least 16 years of age';
	alerts.es.tooYoung16 = 'Debes tener al menos 16 años de edad';
	
	alerts.en.tooYoung18 = 'You must be at least 18 years of age';
	alerts.es.tooYoung18 = 'Debes tener al menos 18 años de edad';
	
	alerts.en.invalidEmail = 'Invalid email address';
	alerts.es.invalidEmail = 'Dirección de correo electrónico no válida';
	
	alerts.en.zipInvalid = 'Please enter a 5-digit zipcode with numbers only';
	alerts.es.zipInvalid = 'Por favor introduce un código postal de 5 caracteres numéricos';
	
	alerts.en.yearInvalid = 'Please enter a 4-digit year with numbers only';
	alerts.es.yearInvalid = 'Por favor introduce el año con 4 caracteres numéricos';
	
	alerts.en.vehicleTooOld = 'You may only insure vehicles 12 years old or newer';
	alerts.es.vehicleTooOld = 'Solo puedes asegurar vehículos con 12 años de antiguedad o menos';

	alerts.en.vehicleOld12 = 'You may only insure vehicles 12 years old or newer';
	alerts.es.vehicleOld12 = 'Solo puedes asegurar vehículos con 12 años de antiguedad o menos';

	alerts.en.vehicleOld20 = 'Sorry we are not able to cover vehicles older than 20 years';
	alerts.es.vehicleOld20 = 'Lo sentimos, no son capaces de cubrir los vehículos mayores de 20 años';

	alerts.en.vehicleOld30 = 'You may only insure vehicles 30 years old or newer';
	alerts.es.vehicleOld30 = 'Solo puedes asegurar vehículos con 30 años de antiguedad o menos';
	
	
	alert(alerts[TOOLSCONTEXT_Page_Language][alertKey]);
}

function TOOLSCONTEXT_Local_IsInteger(testVar) {
	return (testVar.toString().search(/^[0-9]+$/) == 0);
}