//run jquery no conflict mode to not conflict with the menu
jQuery.noConflict();
jQuery(document).ready(function(){
	var locations_select = jQuery('#locations').val();
	if(locations_select == 6)
	{
		jQuery('.international_country').attr('style','display:block;');
	}else{
		jQuery('.international_country').attr('style','display:none;');
	}
	//add click event to date inputs
	jQuery('.date_day').click(function(){			
		this.value='';
	});
	jQuery('.date_month').click(function(){			
		this.value='';
	});
	jQuery('.date_year').click(function(){			
		this.value='';
	});
	//products checkbox hidden inputs
	var hiddenDivs = new Array();
	hiddenDivs[0] = "#motor_insurance_extra";
	hiddenDivs[1] = "#travel_insurance_extra";
	hiddenDivs[2] = "#home_insurance_extra";
	hiddenDivs[3] = "#marine_insurance_extra";
	hiddenDivs[4] = "#business_insurance_extra";
	hiddenDivs[5] = "#health_insurance_extra";
	hiddenDivs[6] = "#pensions_extra";
	hiddenDivs[7] = "#international_health_extra";
	hiddenDivs[8] = "#international_pensions_extra";
	hiddenDivs[9] = "#international_pensions_extra";
	
	if(document.getElementById('general_enquiry_form')){
		//checkboxes show hidden areas, this is for if a client clicks the back button on thier browsers
		//if motor selected make sure its extra fields are shown
		var motor_box = document.getElementById('motor_checkbox');
		show_extra(motor_box,hiddenDivs[0]);
		//if home selected make sure its extra fields are shown
		var home_box = document.getElementById('home_checkbox');
		show_extra(home_box,hiddenDivs[2]);
		//if marine selected make sure its extra fields are shown
		var marine_box = document.getElementById('marine_checkbox');
		show_extra(marine_box,hiddenDivs[3]);
		//if business selected make sure its extra fields are shown
		var business_box = document.getElementById('business_checkbox');
		show_extra(business_box,hiddenDivs[4]);
		//if health selected make sure its extra fields are shown
		var health_box = document.getElementById('health_checkbox');
		show_extra(health_box,hiddenDivs[5]);
		//if pensions selected make sure its extra fields are shown
		var pensions_box = document.getElementById('pensions_checkbox');
		show_extra(pensions_box,hiddenDivs[6]);
		//if international health selected make sure its extra fields are shown
		var international_health_box = document.getElementById('international_health_checkbox');
		show_extra(international_health_box,hiddenDivs[7]);
		//if international pensions selected make sure its extra fields are shown
		var international_pensions_box = document.getElementById('international_pensions_checkbox');
		show_extra(international_pensions_box,hiddenDivs[8]);
	}
	
});
//show and hide location text input box
function check_international(el)
{
	if(el.value == 6)
	{
		jQuery('.international_country').attr('style','display:block;');
	}else{
		jQuery('.international_country').attr('style','display:none;');
	}		
}
//show the hidden extra boxes when the checkboxes are clicked
function show_extra(chkbox,extra_id)
{
	if(chkbox.checked){
		var extra_div = jQuery(extra_id);
		if(extra_div.hasClass('hidden')){
			extra_div.removeClass('hidden');
		}
	}else{
		var extra_div = jQuery(extra_id);
		if(!extra_div.hasClass('hidden')){
			extra_div.addClass('hidden');
		}
	}
}
//validate text field
var errors = new Array();
function validate_required(field,alerttxt)
{
	with (field)
	{
		if (trim(value)==null || trim(value)=="")
	  	{
			errors.push(alerttxt);				
			style.backgroundColor = "#FFC0CB";
			return false;
		}else {
			return true
		}
	}
}
//check if text is a numeric value
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	 
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
   
}		
//validate date day field
function validate_date_day_required(field,alerttxt)
{
	with (field)
	{
		if (!IsNumeric(value) || trim(value)==null || trim(value)=="" || trim(value)=="DD" || Number(trim(value)) < 1 || Number(trim(value)) > 31)
	  	{
			errors.push(alerttxt);
			style.backgroundColor = "#FFC0CB";
			return false;
		}else {
			return true
		}
	}
}
//validate date month field
function validate_date_month_required(field,alerttxt)
{
	with (field)
	{
		if (!IsNumeric(value) || trim(value)==null || trim(value)=="" || trim(value)=="MM" || Number(trim(value)) < 1 || Number(trim(value)) > 12)
	  	{
			errors.push(alerttxt);
			style.backgroundColor = "#FFC0CB";
			return false;
		}else {
			return true
		}
	}
}
//validate date year field
function validate_date_year_required(field,alerttxt)
{
	with (field)
	{
		if (!IsNumeric(value) || trim(value)==null || trim(value)=="" || trim(value)=="YYYY" || trim(value).length < 4 || Number(trim(value)) < 1900 || Number(trim(value)) > 3000)
	  	{
			errors.push(alerttxt);
			style.backgroundColor = "#FFC0CB";
			return false;
		}else {
			return true
		}
	}
}
//validate email
function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		{
			errors.push(alerttxt);
			style.backgroundColor = "#FFC0CB";
			return false;
		}
		else {
			return true;
		}
	}
}
//validate the form
function validate_form(thisform)
{		
	errors = new Array();
	with (thisform)
	{
		validate_required(name,"Name must be filled out!");
		validate_required(surname,"Surname must be filled out!");
		validate_email(email,"Not a valid e-mail address!");
		validate_required(phone_1,"A phone number is required!");
		if(locations.value == 0){
			locations.style.backgroundColor = "#FFC0CB";
			errors.push('Please select your location');
		}			
		if(locations.value == 6){
			validate_required(international_country,"Please supply your location");
		}
		//checkboxes validation
		//if motor selected make sure its extra fields are filled
		var motor_box = jQuery('#motor_checkbox');
		if(motor_box.attr('checked'))
		{
			validate_date_day_required(motor_insurance_renewal_date_day,"Motor Insurance - Please supply valid date day");
			validate_date_month_required(motor_insurance_renewal_date_month,"Motor Insurance - Please supply valid date month");
			validate_date_year_required(motor_insurance_renewal_date_year,"Motor Insurance - Please supply valid date year");
		}
		//if home selected make sure its extra fields are filled
		var home_box = jQuery('#home_checkbox');
		if(home_box.attr('checked'))
		{
			validate_date_day_required(home_insurance_renewal_date_day,"Home Insurance - Please supply valid date day");
			validate_date_month_required(home_insurance_renewal_date_month,"Home Insurance - Please supply valid date month");
			validate_date_year_required(home_insurance_renewal_date_year,"Home Insurance - Please supply valid date year");
		}
		//if marine selected make sure its extra fields are filled
		var marine_box = jQuery('#marine_checkbox');
		if(marine_box.attr('checked'))
		{
			validate_date_day_required(marine_insurance_renewal_date_day,"Marine Insurance - Please supply valid date day");
			validate_date_month_required(marine_insurance_renewal_date_month,"Marine Insurance - Please supply valid date month");
			validate_date_year_required(marine_insurance_renewal_date_year,"Marine Insurance - Please supply valid date year");
		}
		//if business selected make sure its extra fields are filled
		var business_box = jQuery('#business_checkbox');
		if(business_box.attr('checked'))
		{
			validate_date_day_required(business_insurance_renewal_date_day,"Business Insurance - Please supply valid date day");
			validate_date_month_required(business_insurance_renewal_date_month,"Business Insurance - Please supply valid date month");
			validate_date_year_required(business_insurance_renewal_date_year,"Business Insurance - Please supply valid date year");
		}
		//if health selected make sure its extra fields are filled
		var health_box = jQuery('#health_checkbox');
		if(health_box.attr('checked'))
		{
			if(health_insurance_num_employees.value == 0){
				errors.push('Health Insurance - Please supply the number of employees');
				health_insurance_num_employees.style.backgroundColor = "#FFC0CB";
			}
		}
		//if pensions selected make sure its extra fields are filled
		var pensions_box = jQuery('#pensions_checkbox');
		if(pensions_box.attr('checked'))
		{
			if(pensions_num_employees.value == 0){
				pensions_num_employees.style.backgroundColor = "#FFC0CB";
				errors.push('Pensions - Please supply the number of employees');
			}
		}
		//if international health selected make sure its extra fields are filled
		var international_health_box = jQuery('#international_health_checkbox');
		if(international_health_box.attr('checked'))
		{
			if(international_health_num_employees.value == 0){
				international_health_num_employees.style.backgroundColor = "#FFC0CB";
				errors.push('International Health - Please supply the number of employees');
			}
		}
		//if international pensions selected make sure its extra fields are filled
		var international_pensions_box = jQuery('#international_pensions_checkbox');
		if(international_pensions_box.attr('checked'))
		{
			if(international_pensions_num_employees.value == 0){
				international_pensions_num_employees.style.backgroundColor = "#FFC0CB";
				errors.push('International Pensions - Please supply the number of employees');
			}
		}			
	}
	//checkboxes validation
	//make sure at least 1 product is selected
	var one_checked = false;
	var checkboxes = jQuery(':checkbox');
	for(var i=0;i < checkboxes.length;i++)
	{
		if(checkboxes[i].checked){
			one_checked = true;
		}
	}
	//select at least one product
	if(!one_checked){
		errors.push('Select at least one product');
	}
	// verification code
	validate_required(thisform.security_code,"Please re-type the verification code");
	
	var error_string = "";
	for(var i=0;i < errors.length;i++)
	{
		error_string += errors[i] + "\n";
	}
	if(errors.length > 0){
		alert(error_string);
		return false;
	}else{
		return true;
	}
		
}
//trim chars from str
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
//left trim chars from str
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
//right trim chars from str
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
