// JavaScript Document
var theForm = document.forms[0];
function Show_DIV(dispid,hide1,hide2,hide3) {
	var lth=dispid+"_link";
	document.getElementById(dispid).style.display='';
	document.getElementById(hide1).style.display='none';
	document.getElementById(hide2).style.display='none';
	document.getElementById(hide3).style.display='none';
	var lts1=hide1+"_link";lts2=hide2+"_link";lts3=hide3+"_link";

	document.getElementById(lth).style.display='none';
	document.getElementById(lts1).style.display='block';
	document.getElementById(lts2).style.display='block';
	document.getElementById(lts3).style.display='block';
}
function Links_DIV(hideid/*,hide1,hide2,hide3,hide4,hide5,hide6*/) {
	document.getElementById(hideid).style.display='none';
//	document.getElementById(hide1).style.display='none';
//	document.getElementById(hide2).style.display='none';
//	document.getElementById(hide3).style.display='none';
//	document.getElementById(hide4).style.display='none';
//	document.getElementById(hide5).style.display='none';
//	document.getElementById(hide6).style.display='none';
}

function show_step(sts){
	document.getElementById(sts).style.display='block';
	}
function hide_step(sth){
	document.getElementById(sth).style.display='none';
	}
function newWindow(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen,a_position) {
	var int_windowLeft;
	if(a_position==0){int_windowLeft=0;}
	if(a_position==1){int_windowLeft = (screen.width - a_int_windowWidth) / 2;}
  var int_windowTop = (screen.height - a_int_windowHeight) / 4;
  var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=' + a_bool_resizable + ',menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
  var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
    if (parseInt(navigator.appVersion) >= 4) {
      obj_window.window.focus();
    }
}	

function Step_1(){
	var Error1=false;
	var theForm = el_get('form2');
	var BudgetErrorMessages = ["Please enter a value for your Budget","Please enter numbers only for your Budget"];
	//var budval=theForm.Budget.value;
	var budErr=-1;
	//if(budval.match(/^\+?[0-9 ()-]+[0-9]$/) == null){budErr=1;}
	if(el_val('Budget').length<1){budErr=0;}
	var budval=el_fi('Budget');
	if(budval<1){budErr=0;}
	if(budErr!=-1){
		document.getElementById("Budget_Advice").firstChild.nodeValue=BudgetErrorMessages[budErr];
		field = theForm.Budget;
		highlite(field);
		show("Budget_Advice");
		Error1 = true;
		}else{hide("Budget_Advice");}
	if(!radio_sel('radioPlan')){
		Error1 = true;show('radioPlan_Advice');}else{hide('radioPlan_Advice');}
	
	if(!Error1){
		hide_step("FP_Form_Page1");
		show_step("FP_Form_Page2");
		}
}

function submit_form(id){
//	var id;msg="about to update"+id.name;
//	var theForm = id.name;
	var theForm = document.forms[0];
	var hasError=false;

//alert(theForm.Postcode.value);
	err_response=elmLoop();
		if(err_response){/*alert (err_response);*/show("Error_Advice");hasError=true;}else{hide("Error_Advice");}
	
	var valid_Name=/^\D{2,25}$/;var valid_address=/^([a-zA-Z0-9 _-]+){2,25}$/;

	var firstname=trim_input('Name');
	if(!valid_Name.test(firstname)){
		highlite(document.getElementById('Name'));
		//el_get('Name_Advice').innerHTML='Please enter your First Name';
		show("Name_Advice");
		hasError=true;}else{hide("Name_Advice");}
	var surname=trim_input('Surname');
	if(!valid_Name.test(surname)){
		highlite(document.getElementById('Surname'));
		//el_get('Surname_Advice').innerHTML='Please enter your Surname';
		show("Surname_Advice");
		hasError=true;}else{hide("Surname_Advice");}

	var email=trim_input('Email');
	if(!isValidEmail(email)){
		highlite(document.getElementById('Email'));
		//el_get('Surname_Advice').innerHTML='Please enter your Surname';
		show("Email_Advice");
		hasError=true;}else{hide("Email_Advice");}	
		
	var UkPhoneNumberErrorMessages = ["Please enter number without country code", "Telephone No. should be 10 or 11 digits", "Telephone No. should start with a zero", "06 / 070 numbers not accepted", "Enter home, work or mobile number","Telephone No. should contain numbers only"];
	errorcode=UkPhoneNumber(document.getElementById("Telephone").value);
	if(errorcode!=-1){
		//alert(UkPhoneNumberErrorMessages[errorcode]);
		highlite(document.getElementById("Telephone"));
		//alert(document.getElementById("Phone_Advice").nodeType) //DIV element. Alerts 1
		//alert(document.getElementById("Phone_Advice").firstChild.nodeValue) //B element. Alerts 1
		document.getElementById("Phone_Advice").firstChild.nodeValue=UkPhoneNumberErrorMessages[errorcode];
		show("Phone_Advice");
		hasError=true;
	}else{hide("Phone_Advice");}
	
	if(last_no==el_val('Telephone') && !dupnum){el_get('Phone_Advice').firstChild.nodeValue='Please try a different phone number';
		hasError=true;highlite(document.getElementById("Telephone"));show("Phone_Advice");}	
	

	
	if (checkPostCode(theForm.Postcode.value)){
		theForm.Postcode.value=checkPostCode(theForm.Postcode.value);	
	}else{
		field = theForm.Postcode;
		highlite(field);
		show("Postcode_Advice");
		hasError = true;
	}
		if(vs_natemp>=matemp || el_val('seesid')>=matemp){hasError = true;show_all(callerror);
		show_all(submitline);el_get(callerror).innerHTML="We are sorry but as too many invalid telephone numbers have<br /> been submitted we are unable to locate an advisor at this time<br /><br />";}
if(!hasError){call_inprog=false;makeVCALLRequest(vs_mode);
//theForm.submit();
}
return false;
}



function elmLoop(){

var theForm = document.forms[0];//alert(theForm.name);
var err_val=false;   var alertText = ""; var DobErr=false;
	for(i=0; i<theForm.elements.length; i++){
   /*alertText += "Element Name: " + theForm.elements[i].name + "\n";
   alertText += "Element Type: " + theForm.elements[i].type + "\n";*/
	if(theForm.elements[i].type == "text"){
		if(theForm.elements[i].name!= 'Title' && theForm.elements[i].name!= 'Town' && theForm.elements[i].name!= 'County'){
		
		theForm.elements[i].value=trimInput(theForm.elements[i].value);
		elem_advice=theForm.elements[i].name +"_Advice";
		if (repChars(theForm.elements[i].value) || theForm.elements[i].value==""){
				err_val=true;
				alertText += theForm.elements[i].name + "\n";
				highlite(theForm.elements[i]);
				//alert (theForm.elements[i].name +"_Advice");
				show(elem_advice);
				}else hide(elem_advice);
		
			}// end exceptions
		}// end text

	if(theForm.elements[i].type == "select-one"){
		//alert (theForm.elements[i].name + theForm.elements[i].value);
		if(theForm.elements[i].options[theForm.elements[i].selectedIndex].value=="null"){
			if(theForm.elements[i].name.substr(0,3)=="DOB"){DobErr=true;}
		alertText += theForm.elements[i].name + "\n";
		highlite(theForm.elements[i]);
		}
		if(DobErr){show('DOB_Advice');}else{hide('DOB_Advice');}
	
	}//end select
      /*if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
      alertText += "Element Value: " + theForm.elements[i].value + "\n"
      }
      else if(theForm.elements[i].type == "checkbox"){
      alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
      }
      else if(theForm.elements[i].type == "select-one"){
      alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
      }*/
   //alertText += "Element Name with err: " + theForm.elements[i].name + "\n";
   }
return alertText;
}



function UkPhoneNumber(field_val)
{
	// Get the phone number as a string from the text box
	var telephoneNumber = field_val;
	
	// Don't allow country codes to be included (assumes a leading "+")
	if ( telephoneNumber.match(/^\+.+$/) )
	{
		return 0;
	}

	if (telephoneNumber.match(/^\+?[0-9 ()-]+[0-9]$/) == null )
	{
		return 5;
	}

	// Remove everything but numeric digits from the telephone number to help validation
	telephoneNumber = telephoneNumber.replace(/[^0-9]*([0-9]*)/g, "$1");

	// Now check that it is 10 or 11 digits long
	if( telephoneNumber.match(/^[0-9]{10,11}$/) == null )
	{
		return 1;
	}

	// Now check that the first digit is 0
	if ( telephoneNumber.match(/^0[0-9]{9,10}$/) == null )
	{
		return 2;
	}

	// Finally check that the telephone number is appropriate.
	if ( telephoneNumber.match(/^(06|070)[0-9]+$/) )
	{
		return 3;
	}

	// Check that the telephone number is appropriate.
	if (telephoneNumber.match(/^(01|02|03|05|07|08)[0-9]+$/) == null )
	{
		return 4;
	}

	// Check for too many repeating digits
	if (telephoneNumber.match(/^\d*(\d)\1{7}\d*$/) )
	{
		return 4;
	}
	if (telephoneNumber.match(/^\+?[0-9 ()-]+[0-9]$/) == null )
	{
		return 4;
	}
	if (telephoneNumber== '01234567890' || telephoneNumber== '0123456789')
	{
		return 4;
	}	
	return -1;
}


function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var validPC = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      validPC = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (validPC) {return postCode;} else return false;
}
















function repChars(s){
    var s;var initChar=s.charAt(0);
	var i = 1;var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) == initChar))
    { 
    //alert (s.charAt(i));
	i++;
	}
	if (i==sLength){return true;}else{return false;}
}
function trimInput(sStr)
{
//return str.replace(/^\s*|\s*$/,"");

while (sStr.substring(0,1) == ' ')
{
sStr = sStr.substring(1, sStr.length);
}
while (sStr.substring(sStr.length-1, sStr.length) == ' ')
{
sStr = sStr.substring(0,sStr.length-1);
}
return sStr;
}

function highlite(field){
field.style.background="#FFB3B3";
}
function hide(id){
	elem=document.getElementById(id);
    if(elem)
    	elem.style.display='none';
}
function show(id){
    elem=document.getElementById(id);
    if(elem)
    	elem.style.display='block';
}