// Validation
function Sitesearch(){
	
var SearchField = document.SearchForm.SearchField.value

if (SearchField.indexOf("Search") == -1){
	document.SearchForm.submit()
	}


}
 
function validate() {
	
	var txtForenames = document.getElementById("txtForenames");
	var txtSurname = document.getElementById("txtSurname");
	var txtHometelno = document.getElementById("txtHometelno");
	var txtMobiletelno = document.getElementById("txtMobiletelno");
	var txtEmail = document.getElementById("txtEmail");
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");

	
	if (chkPrivacyPolicy.checked==true && vrule_name(txtForenames.value) && vrule_name(txtSurname.value) && (vrule_telephone(txtHometelno.value) || vrule_telephone(txtMobiletelno.value)) && vrule_email(txtEmail.value))  {
		// tidy up the field content before submitting
		document.getElementById("txtHometelno").value = vtidy_telephone( txtHometelno.value );
		document.getElementById("txtMobiletelno").value = vtidy_telephone( txtMobiletelno.value );
		if(txtEmail.value == "Email"){
			document.getElementById("txtEmail").value  = ""
		}
		if(txtMobiletelno.value == "Mobile"){
			document.getElementById("txtMobiletelno").value  = ""
		}
		return true;	
	} else {
		var msg = "The following fields must be completed correctly before we can process your call back request\n\n";
		msg += (!vrule_name(txtForenames.value))?" - First name\n":"";
		msg += (!vrule_name(txtSurname.value))?" - Surname\n":"";
		msg += (!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value))?" - Telephone or Mobile number\n":"";
		msg += (!vrule_email(txtEmail.value))?" - A valid email address\n":"";
		msg += (!chkPrivacyPolicy.checked==true)?" - Agree to our Privacy Policy\n":"";
		msg += "\nPlease supply these missing details and then click the submit button";
		alert(msg);	
		return false;
	}
}

function vrule_telephone( value ) {
	
	value = vtidy_telephone( value );
	
	telNoPattern = /^\d{11}$/;
	return telNoPattern.test(value);
}

function vtidy_telephone( value ) {
	// strip out any spaces and other junk
	return value.replace( /[^0-9]/g, "");
}

function vrule_name( value ) {
	
	if (value.length == 0 || value == "First name" || value == "Surname") return false; // name is required
	
	telNoPattern = /^[a-zA-Z\-\' ]*$/;
	
	return telNoPattern.test(value);
}

function vrule_text( value ) {
	
	if (value.length == 0){return false; }
	else
	{return true}
}

function vrule_DOB( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
        return value
    }   
}

function vrule_DOB_day( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
       if(value > 0 && value <=31)
       {return true}
       else
       {return false}
       
    }   

}

function vrule_DOB_month( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
       if(value > 0 && value <= 12)
       {return true}
       else
       {return false}
       
    }   

}

function vrule_income( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
        if (value >= 800)
        {
            return true
        }
        else
        {
            return false
        }
    }   

}


function vrule_DateOfBirth(day, month, year, today,todaymonth,todayyear){

//one year in milliseconds
var oneyear = 1000*60*60*24*365.25

var datestring = new Date(year,month,day)
var todaysdate = new Date(todayyear,todaymonth,today)

var difference = (todaysdate.getTime() - datestring.getTime())/oneyear

if (difference > 21){
return true
}
else
{
return false
}
}

function vrule_email( value ) {
	
	if (value.length == 0 || value=="Email") return true; // email address is optional

	telNoPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	return telNoPattern.test(value);
}

// Tooltip show hide
function showdiv(name){
var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
if (obj.style.display=="none"){
obj.style.display="";
}else{
obj.style.display="none";
}
}

//Functions to toggle field contents on and off
function clearfield(fieldname,numeric) {

var field = document.getElementById(fieldname)
if (numeric != 'false'){
    if (isNaN(field.value) == true){
    field.value = ''
  }
}
else
{
    if (field.value == 'First name' || field.value == 'Surname' || field.value == 'Mobile' || field.value == 'Telephone' || field.value == 'Email' || field.value == ' Search Think Money')
    {
    field.value = ''
    }
}
}

function populate(original,fieldname,numeric) {
 
var field = document.getElementById(fieldname)

if (numeric != 'false'){
    if (field.value == '' || isNaN(field.value) == true){
    field.value = original
  }
}
else
{
    if (field.value == ''){
    field.value = original
}
}  
  
}
// Show details

function showDetails() {
	changeDisplay( "details", "block" );
}

function hideDetails() {
	changeDisplay( "details", "none" );
}

function changeDisplay( id, type ) {
	var sel = document.getElementById( id );
	sel.style.display = type;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



// Pop up window

function openpopup(popurl){
var winpops=window.open(popurl,"","width=600,height=400,scrollbars,resizable")
}


function popUp(theURL,theWidth,theHeight) {	
	window.open(theURL,'EditItem','status=yes,scrollbars=Yes,width='+ theWidth +',height=' + theHeight);	
}


// AJAX for streaming in web forms
var xmlhttp;
function formstream(FormCode,PageCode) {
	xmlhttp = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = false;
        	}
		}
    }
	if(xmlhttp) {
		var url = '/formstreamer.asp?formID=' + FormCode + '&pageID=' + PageCode
		xmlhttp.onreadystatechange = state_Change;
		xmlhttp.open("GET", url , true);
		//req.setRequestHeader('content-type', 'text/xml');
		xmlhttp.send(null);
	}
}

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {
   
     var formGrabber = xmlhttp.responseText
	 document.getElementById("Dynamic_Form").innerHTML = formGrabber
	 
    }
  }

}


function ShowAllAddress() {
	
	document.getElementById("AddressPopulate").style.display = "block"
	
	}
// AJAX for fillin in the address fields dynamically

function sendPcode(){
    
        var postCode = document.getElementById('txtPostcode').value
        
        if (postCode != ''){
        
            loadXMLDoc('/GetPostCode.asp?code=' + postCode)
        
        }
        else
       {
       alert('Please enter a post code')
       }   
    }


function loadXMLDoc(url) {
	xmlhttp = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = false;
        	}
		}
    }
	if(xmlhttp) {
		xmlhttp.onreadystatechange = state_ChangePCode;
		xmlhttp.open("GET", url , true);
		xmlhttp.send(null);
	}
}

function state_ChangePCode()
{
document.getElementById("AddressSelect").style.display=""
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = OK
     // check to see if the post code is valid, or returns a result
     var ErrorTrap = xmlhttp.responseXML.getElementsByTagName("retcode")
     
     if (ErrorTrap[0].childNodes[0].nodeValue==2){
     
     //Get all address details from the XML
     var StreetLevel = xmlhttp.responseXML.getElementsByTagName("addresses")
     
     //Get individual address item information from the XML
     var StreetName =  StreetLevel[0].getElementsByTagName("street")
     var District =  StreetLevel[0].getElementsByTagName("dependent_locality")
     var Town = StreetLevel[0].getElementsByTagName("post_town")
     var County = StreetLevel[0].getElementsByTagName("county")
     
     
     //Check to see if various address details exist, and assign them to variables
     if (District[0].childNodes[0])
         {
         var Districtvalue = District[0].childNodes[0].nodeValue
         }
     else
        {
        var Districtvalue = ''
        }
        
     if (StreetName[0].childNodes[0])
         {
         var StreetNamevalue = StreetName[0].childNodes[0].nodeValue
         }
     else
        {
        var StreetNamevalue = ''
        }
     
     if (Town[0].childNodes[0])
        {
        var Townvalue = Town[0].childNodes[0].nodeValue
        }
     else
        {
        var Townvalue  = ''
        }
     
     if (County[0].childNodes[0])
        {
        var Countyvalue = County[0].childNodes[0].nodeValue
        }
    else
        {
        var Countyvalue = ''
        }
     
     
     // Get a list of premise names and numbers on the post code   
     var list = xmlhttp.responseXML.getElementsByTagName("premise")
     var HouseList = list[0].getElementsByTagName("premise")
     var OrganisationList = list[0].getElementsByTagName("organisation")
     
     for (s = document.getElementById("HouseNo").length; s >= 0; s--)
	 {document.getElementById("HouseNo").options[s] = null;}
	 
      document.getElementById("HouseNo").options[0] = new Option ("Please select your address...");
	  document.getElementById("HouseNo").options[0].value = "";
   
     if (HouseList.length >= 0){
     for (i=0;i<HouseList.length;i++){ 
     
        var HouseName = ""
        var businessName = ""
        
        if (OrganisationList[i].childNodes[0]){
            var businessName = OrganisationList[i].childNodes[0].nodeValue + ", "
        }
        
        if (HouseList[i].childNodes[0]){
            var HouseName = HouseList[i].childNodes[0].nodeValue
        }        
       
        var valuedrop =  businessName + HouseName + "#" + StreetNamevalue + "#" + Districtvalue + "#" + Townvalue + "#" + Countyvalue
        var labeldrop =  businessName + HouseName + ", " + StreetNamevalue   
       
        document.getElementById("HouseNo").options[i+1] = new Option (labeldrop);
	    document.getElementById("HouseNo").options[i+1].value = valuedrop;

     }
     }
     
     }
     
     else
     
     {
      document.getElementById("HouseNo").options[0] = new Option ("Address not found, please enter manually");
	  document.getElementById("HouseNo").options[0].value = "";
     }
         
    }
  else
    {
      document.getElementById("HouseNo").options[0] = new Option ("Address not found, please enter manually");
	  document.getElementById("HouseNo").options[0].value = "";
    }
  }

}


//Function to apply the address details to the form fields, once found
function fieldApply (string){
	
document.getElementById("AddressPopulate").style.display = "block"
var AddressArray = string.split("#")

if (string != ""){
document.getElementById("txtAddress1").value = AddressArray[0]
document.getElementById("txtAddress2").value = AddressArray[1]
document.getElementById("txtAddress3").value = AddressArray[2]
document.getElementById("txtAddress4").value = AddressArray[3]
document.getElementById("txtAddress5").value = AddressArray[4]
}
else
{
document.getElementById("txtAddress1").value = ""
document.getElementById("txtAddress2").value = ""
document.getElementById("txtAddress3").value = ""
document.getElementById("txtAddress4").value = ""
document.getElementById("txtAddress5").value = ""
}

}
