var form_was_submitted = 0;
function Trim(myval)
{
return myval.split(" ").join("");
}

function validateEmail(fld)
{
var my=fld.value;
var attherate=my.indexOf("@");
var lastattherate = my.lastIndexOf("@")
var dotpos=my.lastIndexOf(".");
var posspace = my.indexOf(" ");
var totallen = my.length;

if (attherate<=0 || dotpos<=0 || attherate > dotpos || (dotpos-attherate)<=1 || (dotpos == totallen-1) || posspace > -1 || attherate!=lastattherate)
return false;
else
return true;
}
function check_form(step)
{
	clearOldUserErrors();
	if(step==0)
	{
		if(document.getElementById("homeowener").value=="")	
		{
			alert("Please Select Homeowner?");
			document.getElementById("homeowener").focus();
			return false;
		}
		if(document.getElementById("homeowener").value=="yes")
		{
			if(document.getElementById("valueproperty").value=="")
			{
				alert("Please Enter Value Property");
				document.getElementById("valueproperty").focus();
				return false;
			}
		}	
		if(document.getElementById("disincome").value=="")	
		{
			alert("Please Enter Disposable Income?");
			document.getElementById("disincome").focus();
			return false;
		}
		
		
		document.getElementById("FormSubmitEnable").style.display="inline";
	}
	else
	{
		CompulsoryTxtFlds = Array("Title", "FirstName", "Surname",  "Telephone", "Email","HomePhone","Postcode","date_of_birth_day","date_of_birth_month","date_of_birth_year");
		FriendlyNames = Array("Title", "FirstName", "Surname",  "Telephone", "Email","Home Phone","Post code","Birth Day","Birth Month","Birth Year");
	
		for(i=0;i<CompulsoryTxtFlds.length;i++)
		{
			if(document.getElementById(CompulsoryTxtFlds[i]).value == "")
			{
				alert(FriendlyNames[i]+" can not be blank.");
				document.getElementById(CompulsoryTxtFlds[i]).focus();
				return false;
			}
		}
	
		if(!validateEmail(document.getElementById("Email")))
		{
			alert("Invalid Email.");
			document.getElementById("Email").focus();
			return false;
		}
		
		/*if(frm.Code.value != frm.CCode.value)
		{
			alert("Invalid Code");
			frm.Code.focus();
			frm.Code.select();
			return false;
		}*/
		
		
		return true;
	}

	if (form_has_errors) {
		scroll(0, 0);
		recordUserErrors($V("form_name"));
		return false;
	} else {
		return true;
	}
	
		return false;
}

function changeLeadType(){
	var list = $('lead_type_id');
	var listValue = list.options[list.selectedIndex].value;
	var partner_extra = $('partner_extra');
	var property_extra = $('property_extra');
	
    $("bad_credit_1").disabled = false;
    $("bad_credit_0").disabled = false;           	
    
    if( listValue == 13 ){ //Equity Release is Selected
      partner_extra.style.display = 'block';
      $("sup_right").style.display = 'none';
      $("how_long_question").style.display = 'none';          
      property_extra.style.display = 'none';   

    }else if(listValue == 1 && '1' == '<?=($user_came_from_affiliate)?>'){ //They have selected (adverse)Remortgage and they came from affiliate

        $("bad_credit_1").checked = true;
        $("bad_credit_0").checked = false;
        $("bad_credit_0").disabled = true;        
        property_extra.style.display = 'none';                   
    }else if(listValue == 6 ){ 
        property_extra.style.display = 'block';   
        partner_extra.style.display = 'none';
        $("sup_right").style.display = 'block';
        $("how_long_question").style.display = 'block';  
      
    }else{
      partner_extra.style.display = 'none';
      $("sup_right").style.display = 'block';
      $("how_long_question").style.display = 'block';          
      property_extra.style.display = 'none';                                 
    }        
}

/**
* Checks if a person reached the specified age by now or not based on a specified birth date and age.
* @param {Date} birthDate Date of birth. Simple.
* @param {Int} Age to query as an integer.
* @return Returns true if a person is old enough, false if not.
* @type Bool
*/
function isOldEnough(birthDate, age) {
	if (typeof birthDate == 'object' && typeof age == 'number') {
		birth_year = birthDate.getFullYear();
		birth_month = birthDate.getMonth() + 1;
		birth_day = birthDate.getDate();
		today = new Date();
		today.setHours(0,0,0,0); 
		anniversary = new Date(birth_year + age, birth_month - 1, birth_day);
		return (today >= anniversary);
	}else return false;
}