// the javascript for the dslcheck form...

function handleKeyPress(evnt) {
  	var iKeyCode;
		
  	iKeyCode = event.keyCode;

  	if (iKeyCode == 13) {
  		ValidateDSLCheckForm();
  	}
  }
  
 function ValidPhoneNumberFormat(phone)
{
  var telnum;
  telnum = phone + " ";
  if (telnum.length == 1)  {
     return false
  }
  telnum.length = telnum.length - 1;
  
  exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     return false;
  }
  
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }
  
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }  
  
  exp = /^[0-9]{9,13}$/
  if (exp.test(telnum) != true) {
     return false;
  }
  
  exp = /^0[0-9]{9,13}$/
  if (exp.test(telnum) != true) {
     return false;
  }
  
  return telnum;
}
function ValidPostcodeFormat(pc)
{
  while (pc.indexOf(" ")!= -1)  {
    pc = pc.slice (0,pc.indexOf(" ")) + pc.slice (pc.indexOf(" ")+1)
  }
  var pcexp = new Array ();

  pcexp.push (/^([a-z]{1,2}[0-9]{1,2})(\s*)([0-9]{1}[abcdefghjklmnpqrstuvwxyz]{2})$/i);

  pcexp.push (/^([a-z]{1,2}[0-9]{1}[a-z]{1})(\s*)([0-9]{1}[abcdefghjklmnpqrstuvwxyz]{2})$/i);
  
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);

  var postCode = pc;

  var valid = false;
  
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      pcexp[i].exec(postCode);
      
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      valid = true;
      
      break;
    }
  }
  
  if (valid) {return postCode;} else return false;
}

function ValidateDSLCheckForm(theform)
{
//var chkbuttonstore=document.getElementById('chckbbbutton').innerHTML;
//document.getElementById('chckbbbutton').innerHTML = 'Hello'; 
	if(!ValidPhoneNumberFormat(theform.bbchk_phone.value))
	{
		alert("You must enter a valid UK telephone number (without country code) to check availability.");
		theform.bbchk_phone.focus();
//		document.getElementById('chckbbbutton').innerHTML = chkbuttonstore; 
		return false;
	}
	if(!ValidPostcodeFormat(theform.bbchk_postcode.value))
	{
		alert("You must enter a valid UK postcode to check availability.");
		theform.bbchk_postcode.focus();
//		document.getElementById('chckbbbutton').innerHTML = chkbuttonstore; 
		return false;
	}
//document.getElementById('chckbbbutton').innerHTML = '<img src="/home/broadband/images/98x21but_pleasewait.gif" alt="Please wait...">'; 
	return true;
//	document.dslcheckform.submit();
}

//document.onkeypress = handleKeyPress;