function TOOLSCONTEXT_Local_CustomValidate_Birthday(formEl) {
	if (formEl.name != 'contact.dp.Birthday.value.year') {
		return TOOLSCONTEXT_Local_ValidateSucceed(formEl);
	}
	
	var year = formEl.form.elements['contact.dp.Birthday.value.year'].value;
	
	if (year == '') {
		TOOLSCONTEXT_Local_DisplayAlert('noBirthday');
		formEl.select();
		TOOLSCONTEXT_Local_CustomValidateResult[formEl.name] = false;
		return false;
	}
	var month = formEl.form.elements['contact.dp.Birthday.value.month'].value;
	var day = formEl.form.elements['contact.dp.Birthday.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 Birthday = new Date();
	Birthday.setFullYear(year, month - 1, day);
	var oneYear = 365 * 24 * 60 * 60 * 1000; //approximate - does not account for leap year etc
	
	var age = (today.getTime() - Birthday.getTime()) / oneYear;
	
	ageEl = document.getElementById('Birthday_age');
	if (ageEl) {
		ageEl.innerHTML = '(' + parseInt(age).toString() + ')';
	}
	if (age < 16) {
		TOOLSCONTEXT_Local_DisplayAlert('tooYoung');
		formEl.select();
		TOOLSCONTEXT_Local_CustomValidateResult[formEl.name] = false;
		return false;
	}
	
	if (age > 75) {
		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_CustomValidate_year(formEl) {
	return TOOLSCONTEXT_Local_ValidateVehicleAge(formEl);
}

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.tooYoung = 'You must be at least 16 years of age';
	alerts.es.tooYoung = 'Debes tener al menos 16 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';
	
	alert(alerts[TOOLSCONTEXT_Page_Language][alertKey]);
}

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