// Form Validation JS
//
// Purpose: Checks form entries
//
// Author: TheEWord (www.theeword.co.uk)
// Developer: Alan Mackin (alan at theeword.co.uk)

//adapted for switch gas and electric by Bulbecks ltd

var xmlHttp=GetXmlHttpObject();

function clr(txt){
  txt.style.border="2px solid #ff0000";
}

function check_frm_all(limit,tfid,pcid) {
  ida=0;
  fail=0;
  for (ida=0;ida<limit;ida++) {
    j=ida;
    ret=check_frm(j,0,tfid,pcid);
    if (ret==1) {fail=1;}
  }
    
  if (fail==0) {return true;}
  else {alert("Please check your details");return false;}
}

function check_key(e,num,gpid,tfid,pcid){

  var keynum;
  var keychar;
  var numcheck;
  if(window.event)
  {keynum = e.keyCode;}
  else {
    if(e.which) 
    {keynum = e.which;}
  }
  
  
  keychar = String.fromCharCode(keynum);
    
  //if enter key has been pressed, move to next box
  if (keynum==13) {
    //firstly lets check to see if everything is working ok
    
   ida=0;
  fail=0;
  for (ida=0;ida<14;ida++) {
    j=ida;
    ret=check_frm(j,1,tfid,pcid);
    if (ret==1) {fail=1;}
  }
  
  //alert(fail);
  if (fail==0) {
  //document.enquiry.submit();
  document.all(gpid).click();
  }  else { 
    
    
    
    //which box shall we jump to?
    if (num==1) {domove=pcid;}
    if (num==2) {check_frm_all(3,tfid,pcid);}
    
       
    document.all(domove).focus();
    
    }
    
  }
  
}


function move_frm(id,tfid,pcid){
  domove='';
  if (id==1) {
    //if starts 07, then move after fifth digit
    if (document.getElementById(tfid).value.substring(0,2)=='07' && document.getElementById(tfid).value.length==11) {
    domove=pcid;}

    //if doesnt starts 07, then make sure it's five digits
    if (document.getElementById(tfid).value.substring(0,2)!='07' && document.getElementById(tfid).value.length==11) { 
    domove=pcid;}
    
    //strip out spaces and ()'s'
    document.getElementById(tfid).value=document.getElementById(tfid).value.replace(" ","");
    document.getElementById(tfid).value=document.getElementById(tfid).value.replace("(","");
    document.getElementById(tfid).value=document.getElementById(tfid).value.replace(")","");
  }
  
  if (domove!='') {document.all(domove).focus();}

}

function check_frm(id,mode,tfid,pcid){

if (id==2) { document.getElementById(pcid).style.border="1px solid #192f74";}
if (id==1) { document.getElementById(tfid).style.border="1px solid #192f74";} 

doneerror=0;

if (mode==0)  {save_half(tfid,pcid);}

ok=0;
errorcount=0;
var error = new Array (15);
var errormsg= new Array (15);

//telephoneA
if (id==1) {tf=1;

  //check to make sure not empty
  if (document.getElementById(tfid).value=='') { error[errorcount]=id;errormsg[id]="Please enter your telephone number";errorcount++;}
  else {tf=0;}
  
  //check to make sure it starts with 01 02 or 07. 
  if (document.getElementById(tfid).value.substr(0,2)!='07' && document.getElementById(tfid).value.substr(0,2)!='01' && document.getElementById(tfid).value.substr(0,2)!='02') { error[errorcount]=id;errormsg[id]="Please enter your telephone number";errorcount++;}
  else {tf=0;}




  //if starts 07, then make sure it's elevn digits
  if (document.getElementById(tfid).value.substr(0,2)=='07' && (document.getElementById(tfid).value.length>11 || document.getElementById(tfid).value.length<11)) { error[errorcount]=id;errormsg[id]="Please enter your telephone number";errorcount++;
  } else {tf=0;}
  
  //if starts 07, then make sure it's elevn digits
  if (document.getElementById(tfid).value.substr(0,2)!='07' && (document.getElementById(tfid).value.length>11 || document.getElementById(tfid).value.length<10)) { error[errorcount]=id;errormsg[id]="Please enter your telephone number";errorcount++;
  } else {tf=0;}

  //if any letters then fail
  if (!validateInt(document.getElementById(tfid).value)) { error[errorcount]=id;errormsg[id]="Please enter your telephone number";errorcount++;}
  else {tf=0;}

  if (tf==0) {ok=id;}

}


//postcode 
if (id==2) {tf=1;
  if (document.getElementById(pcid).value=='') { error[errorcount]=id;errormsg[id]="Please enter your postcode";errorcount++;}else {tf=0;}
  if (validateInt(document.getElementById(pcid).value.substring(0,1))) { error[errorcount]=id;errormsg[id]="Please enter your postcode";errorcount++;}else {tf=0;}
  if (document.getElementById(pcid).value.length>8) { error[errorcount]=id;errormsg[id]="Please enter your postcode";errorcount++;}else {tf=0;}
  if (document.getElementById(pcid).value.length<6) { error[errorcount]=id;errormsg[id]="Please enter your postcode";errorcount++;}else {tf=0;}
    
  if (tf==0) {ok=id;}
}

//only show ticks/crosses if mode = 0
if (mode==0) {

  //if (ok>0) {document.getElementById("ti1").src="/images/tick.png"; }
  
  }
  
if (mode==0 || id==11 || id==10){
  
  //now show errors on page
  for (i=0;i<errorcount;i++) {
  if (error[i]){

    if (error[i]>0) {
    //document.getElementById("ti1").src="/images/cross.png";
    doneerror=1;}
 
  
  }
  }
     
}
if (errorcount) {doneerror=1;}
return doneerror;



}

function validateInt(value)
{
  
  switch (isInteger(value))
  {
     case true:
        return true;
        break;
     case false:
        return false;
  }
}


function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function save_half(tfid,pcid){

    telephone=document.getElementById(tfid).value;
    postcode=document.getElementById(pcid).value;
    
    url="/save-halfway.aspx?";
    url=url+"&telephone="+telephone;
    url=url+"&postcode="+postcode;
    
    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);    
}

//create http object
function GetXmlHttpObject()
{try
{xmlHttp=new XMLHttpRequest();}
catch (e)
{try
{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e)
{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
}return xmlHttp;}