﻿// JScript File
// JS FILE FOR BROADBAND API
function rdo_onclick()
{         
    if(document.getElementById("ctl00_cnt_rdobYes").checked==true)
    {
      document.getElementById("span_switch").style.display="inline";
    }
    else
    {                
      document.getElementById("span_switch").style.display="none";
    }
                   
}
function KeyHandlingDate(event)
{
    if(navigator.appName == "Microsoft Internet Explorer")    	
    {
        if (!((window.event.keyCode >= 48) && (window.event.keyCode <= 57)) && !(window.event.keyCode == 47))
        { 
            window.event.keyCode = null;
        }
    }
    else 
    {
        if (!((event.which >= 48) && (event.which <= 57) || (event.which == 8) ||(event.keyCode == 46) || (event.which == 39) || (event.keyCode == 9) || (event.which == 47)))
        {
            event.preventDefault();
        }
    }		
}

//Validation For PostCode
function validatePostalCode(contents, objref)
{
    
 size = contents.length;
 contents = contents.toUpperCase(); //Change to uppercase
 while (contents.slice(0,1) == " ") //Strip leading spaces
  {
    contents = contents.substr(1,size-1);
    size = contents.length
  }
 while(contents.slice(size-1,size)== " ") //Strip trailing spaces
  {
    contents = contents.substr(0,size-1);
      size = contents.length
  }
 document.getElementById(objref).value = contents; //write back to form field
 if (size < 6 || size > 8)
 { //Code length rule
  alert(contents + " Bah! Invalid PostCode: Wrong Length!");
  document.getElementById(objref).focus();
  return false;
  }
 if (!(isNaN(contents.charAt(0))))
 { //leftmost character must be alpha character rule
   alert(contents + " Bah! Invalid PostCode: Cant start with a Number!");
   document.getElementById(objref).focus();
   return false;
  }
 if (isNaN(contents.charAt(size-3)))
 { //first character of inward code must be numeric rule
   alert(contents + " Bah! Invalid PostCode: Alphabet in wrong position!");
   document.getElementById(objref).focus();
   return false;
  }
 if (!(isNaN(contents.charAt(size-2))))
 { //second character of inward code must be alpha rule
   alert(contents + " Bah! Invalid PostCode: Number in wrong position!");
   document.getElementById(objref).focus();
   return false;
  }
 if (!(isNaN(contents.charAt(size-1))))
 { //third character of inward code must be alpha rule
   alert(contents + " Bah! Invalid PostCode: Number in wrong position!");
   document.getElementById(objref).focus();
   return false;
  }
 if (!(contents.charAt(size-4) == " "))
 {//space in position length-3 rule
   alert(contents + " Bah! Invalid PostCode: Wrong spacing!");
   document.getElementById(objref).focus();
   return false;
   }
 count1 = contents.indexOf(" ");
 count2 = contents.lastIndexOf(" ");
 if (count1 != count2)
  {//only one space rule
   alert(contents + " Bah! Invalid PostCode: Only one space allowed!");
   document.getElementById(objref).focus();
   return false;
  }
  
  return true;

}

function checkUKLandline (LandlineNumber) 
{
 // Convert into a string and check that we were provided with something
 var Phnum = LandlineNumber + " ";
 if (Phnum.length == 1)  
 {
    PhnumberErrorNo = 1;
    return false
 }
 Phnum.length = Phnum.length - 1;
 
 // Don't allow country codes to be included (assumes a leading "+")
 var exp = /^(\+)[\s]*(.*)$/;
 if (exp.test(Phnum) == true) 
 {
    PhnumberErrorNo = 2;
    return false;
 }
 
 // Remove spaces from the telephone number to help validation
 while (Phnum.indexOf(" ")!= -1)  
 {
   Phnum = Phnum.slice (0,Phnum.indexOf(" ")) + Phnum.slice (Phnum.indexOf(" ")+1)
 }
 
 // Remove hyphens from the telephone number to help validation
 while (Phnum.indexOf("-")!= -1)  
 {
   Phnum = Phnum.slice (0,Phnum.indexOf("-")) + Phnum.slice (Phnum.indexOf("-")+1)
 }  
 
 // Now check that all the characters are digits
 exp = /^[0-9]{10,11}$/
 if (exp.test(Phnum) != true) 
 {
    PhnumberErrorNo = 3;
    return false;
 }
 
 // Now check that the first digit is 0
 exp = /^0[0-9]{9,10}$/
 if (exp.test(Phnum) != true) 
 {
    PhnumberErrorNo = 4;
    return false;
 }
 
 // Finally check that the telephone number is appropriate.
 exp = /^(01|02)[0-9]+$/;
 if (exp.test(Phnum) != true) 
 {
    PhnumberErrorNo = 5;
    return false;
 }
 
 // Telephone number seems to be valid - return the stripped telehone number  
 return Phnum;
}
var PhnumberErrorNo = 0;
var PhnumberErrors = new Array ();
PhnumberErrors[0] = "Dude, enter a valid UK Telephone Number";
PhnumberErrors[1] = "What's your Telephone number?";
PhnumberErrors[2] = "Don’t enter COUNTRY CODE in your telephone number!";
PhnumberErrors[3] = "Hmm…you know a valid UK Landline number must be in digits.";
PhnumberErrors[4] = "Hey eagle eye! A valid UK Landline number begins with 0.";
PhnumberErrors[5] = "Mind the Gap! Telephone number is invalid or inappropriate.";


//Validation For Numeric Value
function VNum(contents)
{
  var checkOK = "0123456789";
  var checkStr = contents;
  var allValid = true;
  var ch;
  
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      { 
       if (ch == checkOK.charAt(j))
			{
			break;
			}
		if (j == 9)
			{
			allValid = false;
			break;
			}
      } 
        if (!allValid)
			{
			return (false);
		    }
   }
    return (true);
 }

 
 function checktext(Contents)
{
 var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 var checkStr = Contents;
 var allValid = true;

 for (i = 0;  i < checkStr.length;  i++)
 {
    ch = checkStr.charAt(i);
   for (j = 0;  j < checkOK.length;  j++)
    
     if (ch == checkOK.charAt(j))
       break;
   if (j == 53 )
   {
     allValid = false;
     break;
   }
 }

 if (!allValid)
 {
   return false;
 }
 else
 {
 return true;
 }
}

//Validation for Date DD/MM/YYYY
function ValidDate(str)
{
 validRegExp = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/; 

  // search date text for regular exp matches
   if (str.search(validRegExp) == -1) 
  {
   
     return false;
   } 
   return true; 
}




//Validation for SortCode
function ValidSordcode(str)
{
 validRegExp = /^[0-9]{2}[-][0-9]{2}[-][0-9]{2}$/; 

  // search sortcode text for regular exp matches
   if (str.search(validRegExp) == -1) 
  {
   
     return false;
   } 
   return true; 
}

//Validation for Email
function isValidEmail(strEmail)
{
 validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
 //strEmail = document.getElementById("ctl00_cnt_txtemail").value;

  // search email text for regular exp matches
   if (strEmail.search(validRegExp) == -1) 
  {
     //alert("A valid e-mail address is required.\nPlease amend and retry");
     return false;
   } 
   return true; 
}


//Validation For Email
function echeck(str)
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		
 	var checkOK = "~`!#$%^&*()=+|\}{][',?";
  	var checkStr = str;
  	var allValid = true;
   
   var s=0;
		for (i = 0;  i < checkStr.length;  i++)
  		{
			ch = checkStr.charAt(i);
    			if (ch==dot)
    			{
    			s=s+1
    			if (s==1)
    			   var first_dot=i;
    			if (s==2)
    			   var second_dot=i;    
    			}
    			
    			for (j = 0;  j < checkOK.length;  j++)
		   			if (ch == checkOK.charAt(j))
       				    allValid = false;
       				
    				if (j == 54 )
    				{
      				allValid = false;
     				 break;
   					}
 		 }

		if (!allValid)
        {
        alert("Invalid E-mail Address !")
        return false
        }
       	
		if (str.indexOf(at)==-1)
		{
		   alert("Invalid E-mail Address !")
		   return false
		}
		if (s>3)
		{
		   alert("Invalid E-mail Address !")
		   return false
		}


		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address !")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address !")
		    return false
		}
		//alert(str.indexOf(dot)+'+++'+ (str.length-2))
		if (str.indexOf(dot)>=(str.length-2))
		{
		  alert("Invalid E-mail Address !")
		    return false
		}


		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address !")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address !")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address !")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address !")
		    return false
		 }
		if (s>1)
		{
         	
          if (first_dot<str.indexOf(at))
          {
          
          if ((first_dot<str.indexOf(at)) && (second_dot>str.indexOf(at)))
		 	{
		     return true
		 	}
         	else
         	{
         	alert("Invalid E-mail Address !")
          	return false
         	}
		}}

 		 return true					
	}

//Validation For Mobile
function checkUKTelephone (telephoneNumber) 
{

 // Convert into a string and check that we were provided with something
 var telnum = telephoneNumber + " ";
 if (telnum.length == 1)  {
    telNumberErrorNo = 1;
    return false
 }
 telnum.length = telnum.length - 1;
 
 // Don't allow country codes to be included (assumes a leading "+")
 var exp = /^(\+)[\s]*(.*)$/;
 if (exp.test(telnum) == true) {
    telNumberErrorNo = 2;
    return false;
 }
 
 // Remove spaces from the telephone number to help validation
 while (telnum.indexOf(" ")!= -1)  {
   telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
 }
 
 // Remove hyphens from the telephone number to help validation
 while (telnum.indexOf("-")!= -1)  {
   telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
 }  
 
 // Now check that all the characters are digits
 exp = /^[0-9]{10,11}$/
 if (exp.test(telnum) != true) {
    telNumberErrorNo = 3;
    return false;
 }
 
 // Now check that the first digit is 0
 exp = /^0[0-9]{9,10}$/
 if (exp.test(telnum) != true) {
    telNumberErrorNo = 4;
    return false;
 }
 
 // Finally check that the telephone number is appropriate.
 exp = /^(01|02|02|070|08|077|078|079)[0-9]+$/;
 if (exp.test(telnum) != true) {
    telNumberErrorNo = 5;
    return false;
 }
 
 
 return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Hey mate, What's your UK Mobile number?";
telNumberErrors[1] = "OiOi! Enter your Mobile number, Please!";
telNumberErrors[2] = "OiOi! That doesn't seem like a valid Number!";
telNumberErrors[3] = "OiOi! That doesn't seem like a valid Number!";
telNumberErrors[4] = "OiOi! That doesn't seem like a valid Number!";
telNumberErrors[5] = "OiOi! That doesn't seem like a valid Number!";


//Validation For TExt
function VText(Contents)
{
  var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789";
  var checkStr = Contents;
  var allValid = true;

  for (i = 0;  i < checkStr.length;  i++)
  {
	ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
	
      if (ch == checkOK.charAt(j))
        break;
    if (j == 130)
    {
      allValid = false;
      break;
    }
  }

  if (!allValid)
  {
    return (false);
  }
  else
  {
  return (true);
  }  
 }
 function numbersonly(bffield, bfevent) {

    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (bfevent)
        key = bfevent.which;
    else
        return true;

    keychar = String.fromCharCode(key);

    // control keys
    if ((key==null) || (key==0) || (key==8) || 
        (key==9) || (key==13) || (key==27) )
        return true ;

    // numbers 
    else if ((("0123456789.").indexOf(keychar) > -1))
        return true ;

    else
        return false ;
}
   function frm_onsubmit()
    {
         //validation for postcode        
        if(document.getElementById("ctl00_cnt_txtuserpostcode").value!="")
        {
            if(!validatePostalCode(document.getElementById("ctl00_cnt_txtuserpostcode").value, "ctl00_cnt_txtuserpostcode"))
               return false;
        }
        else
        {
            alert("OiOi! Enter your postcode.");
            document.getElementById("ctl00_cnt_txtuserpostcode").focus();
            return false;
        }
        //validation for phone       
       
//        if(document.getElementById("ctl00_cnt_txttelnumber").value!="" || document.getElementById("ctl00_cnt_txttelnumber").value!=null)
//        {
//		    if(!checkUKLandline(document.getElementById("ctl00_cnt_txttelnumber").value)) 
//		    {
//                alert(PhnumberErrors[PhnumberErrorNo]);                
//	            document.getElementById("ctl00_cnt_txttelnumber").select();
//	            return false;
//            }
//        }
//        else
//        {
//            alert("What's your Telephone number?");
//            document.getElementById("ctl00_cnt_txttelnumber").focus();
//            return false;
//        }

 if(document.getElementById("ctl00_cnt_rdoyes").checked==true&&(document.getElementById("ctl00_cnt_rdoswy").checked==true||document.getElementById("ctl00_cnt_rdoswn").checked==true))
  {
    if(document.getElementById("ctl00_cnt_ddlmerchant").value=="0")
    {alert("Please  Select Your Current provider");
     return false;     
    }
    else
    {
    return true;
    }
    
}




    }
      function frm_onsubmitPost(that)
    {
         //validation for postcode   
     
      if(document.getElementById("ctl00_cnt_txtgvpostocde").value!="")
        {
            if(!validatePostalCode(document.getElementById("ctl00_cnt_txtgvpostocde").value, "ctl00_cnt_txtgvpostocde"))
               return false;
        }
        else
        {
            alert("OiOi! Enter your postcode.");
            document.getElementById("ctl00_cnt_txtgvpostocde").focus();
            return false;
        }
    }
function contract(that)
{
document.getElementById("ctl00_cnt_dvprovider").style.display="none" ;

}
function expand(that)
{
document.getElementById("ctl00_cnt_dvprovider").style.display="inline" ;
}
