// JavaScript Document
function roundNumber(number,decimals) {
	var newString;// The new rounded number
	//number=document.getElementById('dpTextfield').value;
	decimals = Number(decimals);
	if (decimals < 1) {
		newString = (Math.round(number)).toString();
	} else {
		var numString = number.toString();
		if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
			numString += ".";// give it one at the end
		}
		var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
		var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
		var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
		if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
			if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
				while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
					if (d1 != ".") {
						cutoff -= 1;
						d1 = Number(numString.substring(cutoff,cutoff+1));
					} else {
						cutoff -= 1;
					}
				}
			}
			d1 += 1;
		} 
		if (d1 == 10) {
			numString = numString.substring(0, numString.lastIndexOf("."));
			var roundedNum = Number(numString) + 1;
			newString = roundedNum.toString() + '.';
		} else {
			newString = numString.substring(0,cutoff) + d1.toString();
		}
	}
	if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
		newString += ".";
	}
	var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
	for(var i=0;i<decimals-decs;i++) newString += "0";
	//var newNumber = Number(newString);// make it a number if you like
	//document.getElementById('dpTextfield').value = newString; // Output the result to the form field (change for your purposes)
	return newString;
}

function xmasPrice(dateInput){
  var oriDate = document.getElementById("on1").value;
  var oriTime = document.getElementById('time-hour').value;
   var RtnoriTime = document.getElementById('RTNtime-hour').value;
  var getPrice =parseFloat(document.getElementById('hFPrice').value);
  var priceTwo=0,priceOne=document.getElementById('hFPrice').value;
   priceOne=Math.round((priceOne*100))/100;
  var xmasPrice =parseFloat(getPrice*0.5);
  var calPrice= parseFloat(document.getElementById('amount').value);
  var len,cDate=new Array(),cDateRtn=new Array(),i,y=0,z=0,j=0, finalPrice;  
  len=oriDate.length;
  /*    if(dateInput=="on1"){
	 priceOne=priceOne+parseFloat(priceTwo*0.5)
	 priceOne=Math.round((priceTwo*100))/100;
  }*/
   //alert("price"+xmasPrice);
  len=oriDate.length;
  cDate[0]= oriDate.substring(0,0);
  //alert(cDate[0]);
  for(i=1;i<=len;i++){	  
     if(oriDate.substring(y,i)=="/"){
		  z++; y++; i++;
		  cDate[z]= oriDate.substring(y,i);
		  y++; i++;
	  }	 
		 // alert(oriDate.substring(y,i));
	      cDate[z]+= oriDate.substring(y,i); 
	      y++;	  
	 }
	 i=0;y=0;z=0;
	 oriDate=document.getElementById("on7").value
	 cDateRtn[0]= oriDate.substring(0,0);
  //alert(cDate[0]);
  for(i=1;i<=len;i++){	  
     if(oriDate.substring(y,i)=="/"){
		  z++; y++; i++;
		  cDateRtn[z]= oriDate.substring(y,i);
		  y++; i++;
	  }	 
		 // alert(oriDate.substring(y,i));
	      cDateRtn[z]+= oriDate.substring(y,i); 
	      y++;	  
	 }
	 //alert("Date Is :"+cDate[0]+"/"+cDate[1]+"/"+cDate[2]);
	 //alert("Date Is :"+cDateRtn[0]+"/"+cDateRtn[1]+"/"+cDateRtn[2]);
	 
	 if((cDate[1]==12 && cDate[0]>=24 && cDate[0]<=27)||(cDate[1]==1 && cDate[0]>=1 && cDate[0]<=2)||(cDate[1]==12 && cDate[0]==31)||(cDate[1]==1 && cDate[0]>=1 && cDate[0]<=2)){
		if((cDate[0]==24 && oriTime>=18)||(cDate[0]>24 && cDate[0]<27)||(cDate[0]==27 && oriTime<=6)||(cDate[0]==31 && oriTime>=18)||(cDate[0]==1)||(cDate[0]==2 && oriTime<=6)){  
	 	  //alert("DEPATURE Date");
		  xmasPrice =parseFloat(getPrice*0.5);
		  alert("Extra charge for this period £"+xmasPrice);
		  xmasPrice=xmasPrice+getPrice;
          //xmasPrice=Math.round((xmasPrice*100))/100;
		  priceOne=xmasPrice;
		  priceOne=Math.round((priceOne)*100)/100;
		  //document.getElementById('amount').value=xmasPrice;
		 }
	 }
	 if(document.getElementById('radio2').checked==true){
	    if((cDateRtn[1]==12 && cDateRtn[0]>=24 && cDateRtn[0]<=27)||(cDateRtn[1]==1 && cDateRtn[0]>=1 && cDateRtn[0]<=2)||(cDateRtn[1]==12 && cDateRtn[0]==31)||(cDateRtn[1]==1 && cDateRtn[0]>=1 && cDateRtn[0]<=2)){
		if((cDateRtn[0]==24 && RtnoriTime>=18)||(cDateRtn[0]>24 && cDateRtn[0]<27)||(cDateRtn[0]==27 && RtnoriTime<=6)||(cDateRtn[0]==31 && RtnoriTime>=18)||(cDateRtn[0]==1)||(cDateRtn[0]==2 && RtnoriTime<=6)){
		
		//alert("RETURN Date");
		 xmasPrice =parseFloat((getPrice+6)*0.5);
         // xmasPrice=Math.round((xmasPrice*100))/100;
		 alert("Extra charge for Return Jurney £"+xmasPrice);
		  xmasPrice = xmasPrice+getPrice+6;
		  priceTwo=xmasPrice;
		   //document.getElementById('amount').value=xmasPrice;
		 }else{
			  priceTwo=getPrice+6;
					 }
	 }else{
			 priceTwo=getPrice+6;			
		 }
	    
	 }
	
	 //alert("Price 1 :"+priceOne+"Price 2 :"+priceTwo);
	//=Math.round(((priceOne+priceTwo)*100))/100;
	  document.getElementById('amount').value=roundNumber((priceOne+priceTwo),2);
	  /*if(((cDate[1]==12 && cDate[0]>=24 && cDate[0]<=27)||(cDate[1]==1 && cDate[0]>=1 && cDate[0]<=2))&& ((cDateRtn[1]==12 && cDateRtn[0]>=24 && cDateRtn[0]<=27)||(cDateRtn[1]==1 && cDateRtn[0]>=1 && cDateRtn[0]<=2))){
	 	alert("BOTH Date");
		xmasPrice=((((getPrice*2)+6)*0.5)+((getPrice*2)+6));
		xmasPrice=Math.round((xmasPrice*100))/100;
		document.getElementById('amount').value=xmasPrice;
	 }
	 alert(xmasPrice);
*/	 
	 /*if(cDate[1]==12 || cDate[1]==1 || cDateRtn[1]==12 || cDateRtn[1]==1){
		 if((cDate[0]==24 && oriTime>=18) ||(cDate[0]>24 && cDate[0]<27)||(cDate[0]==31 && oriTime>=18) ||(cDate[0]==27 && oriTime<=6) || (cDate[0]==1)|| (cDate[0]==2 && oriTime<=6)){
			
			alert("50% EXTRA FOR THIS PERIOD ");
			alert("One way Price: "+xmasPrice); 
			document.getElementById('amount').value=getPrice+xmasPrice;
			
		}
		if((cDateRtn[0]==24 && oriTime>=18) ||(cDateRtn[0]>24 && cDateRtn[0]<27)||(cDateRtn[0]==31 && oriTime>=18) ||(cDateRtn[0]==27 && oriTime<=6) || (cDateRtn[0]==1)|| (cDateRtn[0]==2 && oriTime<=6)){
			
			alert("50% EXTRA FOR THIS PERIOD ");
			alert("Return Price: "+xmasPrice); 
			document.getElementById('amount').value=getPrice+getPrice+6+xmasPrice;
			
		}
	 }*/
	 //document.getElementById('amount').value=getPrice;
/*	 if(cDate[1]==12 || cDate[1]==1){
	    //alert("December");
	if((cDate[0]==24 && oriTime>=18) ||(cDate[0]>24 && cDate[0]<27)||(cDate[0]==31 && oriTime>=18) ||(cDate[0]==27 && oriTime<=6) || (cDate[0]==1)|| (cDate[0]==2 && oriTime<=6)){
			
			alert("50% EXTRA FOR THIS PERIOD ");
			alert("PRICE 1 :"+getPrice+" PRICE 2:"+priceTwo); 
			document.getElementById('amount').value=priceOne;
			
		}
	 }*/
	 
	/*alert(cDate[0]);
	alert(cDate[1]);
	alert(cDate[2])*/;  
}
function changeJurneyType(){
	//alert(document.getElementById("hidTrip").value);
	if(document.getElementById('radio2').checked==true){
	document.getElementById('item_name').value=document.getElementById("hidTrip").value+" -RTN";
			//rtnPrice();
			xmasPrice(1);
			
	}else{
		document.getElementById('item_name').value=document.getElementById("hidTrip").value;
		document.getElementById('on7').value="";
		document.getElementById('os7').value="";
		document.getElementById('on8').value="";
		document.getElementById('os8').value="";
		//rtnPrice();
		//alert(val1);
		xmasPrice();
	}
	//document.getElementById('item_name').value=;

}
function getReff(){
	merchBox("HouseNo", "Postcode", "on6","");
	merchBox("on6", "postcode2", "on6","");
	
}
function merchBox(BoxOne, BoxTwo, Target,Spacer){
	var val1;
	if(BoxOne==""){
		document.getElementById(Target).value="";
	}else{
		if(BoxTwo==""){
		   val1=document.getElementById(BoxOne).value;
		}else{
		val1=document.getElementById(BoxOne).value+Spacer+document.getElementById(BoxTwo).value;
		}
	document.getElementById(Target).value=val1;
	}
	return val1; 
}
function changeDestination(){
	if(document.getElementById("hidflag").value=="1"){
		merchBox("HouseNo", "Address", "os0"," ");
		merchBox("os0", "City", "os0",",");
		merchBox("os0", "Postcode", "os0",",");
		merchBox("os0", "postcode2", "os0","");
		document.getElementById("on0").value=document.getElementById("from").value;
			
	}else{
		merchBox("HouseNo", "Address", "on0"," ");
		merchBox("on0", "City", "on0",",");
		merchBox("on0", "Postcode", "on0",",");
		merchBox("on0", "postcode2", "on0","");
		document.getElementById("os0").value=document.getElementById("To").value;
	}
/*	alert("Start: "+document.getElementById("on0").value);
	alert("End :"+document.getElementById("os0").value);*/
}
function insertForm(){
	document.getElementById("form1").action="https://www.paypal.com/cgi-bin/webscr";
	document.getElementById("subImage").src="images/paynow.png";
		 //alert(document.getElementById("form1").action);
		 MM_showHideLayers('e-mail','','hide','divE-mail','','hide');
		 //document.getElementById("").style.display = 'none';
		 //document.getElementById("").style.display = 'none';
	if(document.getElementById("radio4")!=null){
	   if(document.getElementById("radio4").checked==true){ 
	      document.getElementById("form1").action="price.php?";
		  document.getElementById("subImage").src="images/SUBMIT.png";
	      //alert(document.getElementById("form1").action);
	      MM_showHideLayers('e-mail','','show','divE-mail','','show');
		  
		  
	      //document.getElementById("e-mail").style.display = 'block';
	      //document.getElementById("divE-mail").style.display = 'block';
	
	    }
	}
}
/*function submitForm(){
	alert(document.getElementById("form1").action);
  document.getElementById("form1").action="https://www.paypal.com/cgi-bin/webscr";
  alert(document.getElementById("form1").action);
}*/
function rtnPrice(){

	if(document.getElementById('radio2').checked==true){
		
		document.getElementById('amount').value=((document.getElementById('hFPrice').value*2)+6);
	}else{
		document.getElementById('amount').value=document.getElementById('hFPrice').value;
		
	}
}

function enterAddress(){
	var getJourney=document.getElementById("homeFlag").value;
	var targetField="on0";
	//alert(getJourney);
	if(getJourney=="1"){
		targetField="os0";		
	}
document.getElementById(targetField).value=document.getElementById("HouseNo").value+" "+document.getElementById("Address").value+" "+document.getElementById("City").value+" "+document.getElementById("Postcode").value+" "+document.getElementById("postcode2").value;
	//merchBox(sendData,"postcode2", targetField);
	//sendData+','+=merchBox("Postcode", "postcode2", targetField);
	
}


function addList(name, fieldName){
	var opt = document.createElement("option");
    document.getElementById(fieldName).options.add(opt);
	opt.value = name;
	if(name=="CITY"){
		name="LONDON CITY";
	}
	opt.text = name;
}
function removeAll(remField){
	document.getElementById(remField).length = 0;
}
function airportList(){
	var my = document.getElementById('AtoA').value,x;
	var myAir = new Array("HEATHROW T1","HEATHROW T2","HEATHROW T3","HEATHROW T4","HEATHROW T5", "GATWICK S","GATWICK N","STANSTED","LUTON","CITY");
	if((my=="HEATHROW T1")||(my=="HEATHROW T2")||(my=="HEATHROW T3")||(my=="HEATHROW T4")||(my=="HEATHROW T5")){
		my="HEATHROW";
    }
   if((my=="GATWICK S") || (my=="GATWICK N")){
	   my="GATWICK";
   }
   switch(my){
	   case "HEATHROW" : removeAll("AtoAD");
                         for(x=0;x<myAir.length;x++){
	                        if(x>=5){
	                           addList(myAir[x], "AtoAD");
	                         }
						 }break;
 
	    case "GATWICK" : removeAll("AtoAD");
                         for(x=0;x<myAir.length;x++){
	                        if((x!=5) && (x!=6)){
								//alert(x+": IN");
	                           addList(myAir[x], "AtoAD");
	                         }
						 }break;
		  case "STANSTED" : removeAll("AtoAD");
                         for(x=0;x<myAir.length;x++){
	                        if(x!=7){
								//alert(x+": IN");
	                           addList(myAir[x], "AtoAD");
	                         }
						 }break;
		  case "LUTON" : removeAll("AtoAD");
                         for(x=0;x<myAir.length;x++){
	                        if(x!=8){
								//alert(x+": IN");
	                           addList(myAir[x], "AtoAD");
	                         }
						 }break;
		 case "CITY" : removeAll("AtoAD");
                         for(x=0;x<myAir.length;x++){
	                        if(x!=9){
								//alert(x+": IN");
	                           addList(myAir[x], "AtoAD");
	                         }
						 }break;
		}

}   
function changeLug(){
var i;
if(document.getElementById("on3").value=="MPV"){
   if(document.getElementById("pas").value>=5){
      removeAll("lug");
	  for(i=0;i<=2;i++){
	     addList(i, "lug");
	    }
	  }else{
		removeAll("lug");
		   for(i=0;i<=4;i++){
		   addList(i, "lug");
	      }
	   }
   }
}

function changeCase(inputVar){var frmObj=document.getElementById(inputVar); var index, tmpStr, tmpChar, preString, postString, strlen;tmpStr = frmObj.value.toLowerCase();strLen = tmpStr.length;if (strLen > 0){for (index = 0; index < strLen; index++){if (index == 0){tmpChar = tmpStr.substring(0,1).toUpperCase();postString = tmpStr.substring(1,strLen);tmpStr = tmpChar + postString;}else {tmpChar = tmpStr.substring(index, index+1);if (tmpChar == " " && index < (strLen-1)){tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();preString = tmpStr.substring(0, index+1);postString = tmpStr.substring(index+2,strLen);tmpStr = preString + tmpChar + postString;}}}}document.getElementById(inputVar).value = tmpStr;}

function caps(inputVar){var str=document.getElementById(inputVar).value;

document.getElementById(inputVar).value=str.toUpperCase();}

/*function checkDate(Input){
	var myDate=new Date(),i,y=0,len, get,flag=0,outDate, inDate;
	var cDate=new Array();
	outDate= document.getElementById('on2').value;
	inDate= document.getElementById('on6').value;
    get= document.getElementById(Input).value;
	var today = new Date();
	len=get.length;
	cDate[y]= get.substring(0,1);
	for(i=1;i<=len;i++){
		if((get.substring(i,i+1)=="/")&&(flag==0)){
			y=1;i++;cDate[y]= get.substring(i,i+1);
			i++;flag=1;
		}
		if((get.substring(i,i+1)=="/")&&(flag==1)){
			y=2;i++;cDate[y]= get.substring(i,i+1);
			i++;
		}
		cDate[y]+=""+get.substring(i,i+1);
	}
	cDate[1]=cDate[1]-1;
	myDate.setFullYear(cDate[2],cDate[1],cDate[0]);
	if ((myDate>today) && (get!=null)){
		;//alert("Valid date");
	}else{
		document.getElementById(Input).style.backgroundColor="#FF0000";
		document.getElementById(Input).value= "";
		alert("Entered Date is Invalid Try Again Please");
	}

}*/
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
/*function submitform(){
	alert("ins On");
  document.getElementbyId('form1').submit();
}
*/
function IPcheck(){
if((document.getElementById('avoid_IP').value!="") && (document.getElementById('IP_Description').value!="")){
var IPvalue=document.getElementById('avoid_IP').value;
errorString = "";
var theName = "IPaddress",flag=0;
var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern);

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == ""){
;//alert ("That is a valid IP address.");
}
else{
//return false;
alert (errorString);
flag=1;
}
}else{
	alert("Field Is Empty");
	flag=1;
}
if(flag==0){
	return true;
}else{
	return false;
}
}
function checkDriver(){
	var answer;
	//alert("WORKING "+document.getElementById("allocation").value);

	if((document.getElementById("allocation").value=="NO") || (document.getElementById("allocation").value=="")){

		alert("Enter Driver Name");
		return false;

	}

	else{

		window.print();
		answer = confirm ("Confirm the Print Job");
		 if (answer){
			 document.getElementById('jobStatus').value="YES";
		     document.getElementById("form1").submit();;
		 }else{
		    alert("Try Again");
		 }


	}

}
function cancelJob(){
	answer = confirm ("Confirm the Cancellation");
		 if (answer){
			 document.getElementById('jobStatus').value="CE";
		     document.getElementById("form1").submit();
		 }else{
		    alert("Request Cancelled");
		 }
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function babySeat(){
   var getInfo=document.getElementById('custom').value;
 if(document.getElementById('chkBoxBaby').checked== true){
	 if(getInfo=="N/A" || getInfo=="(Baby Seat Required)" || getInfo=="N/A(Baby Seat Required)"){getInfo="";}
	 document.getElementById('custom').value=getInfo+"(Baby Seat Required)";
 }else{
	 document.getElementById('custom').value="N/A";
 }

}
function fieldValidation(){
	var divBox=new Array("on1","time-hour","time-mini","on2","os2","Title","fName","HouseNo", "Address","City","Postcode","postcode2","os4","on7","RTNtime-hour","RTNtime-mini","on8","os8"),x,cycleNo=12,flag=0;
	changeDestination();//change destination
	if(document.getElementById('radio2').checked==true){
		cycleNo=17;
		//alert("cycle"+cycleNo);
	}	
	for(x=0;x<=cycleNo;x++){
		//alert(document.getElementById(divBox[x]).value);
		if(document.getElementById(divBox[x]).value==""||document.getElementById(divBox[x]).value=="--"){
		   /*document.getElementById("divPickdate"+x).innerHTML="**";
		   document.getElementById("divPickdate"+x).style.color="#FF0000";*/		
		   document.getElementById(divBox[x]).style.backgroundColor="#FF9F9F";
		   flag=1;
	   }else{
		   /*document.getElementById(divBox).style.color="#B8F5B1";*/
		   /*document.getElementById("divPickdate"+x).innerHTML="";*/
		   if(divBox[x]!="Postcode"){
		      document.getElementById(divBox[x]).style.backgroundColor="#B8F5B1";
			  if(divBox[x]=="os4"){
				  if(!(testTelNumber())){
					  document.getElementById(divBox[x]).style.backgroundColor="#FF9F9F";
		              flag=1;
				  }
				  
			  }
		   }
	   }
	}
	//alert(document.getElementById("radio4").checked);
	
	if(document.getElementById("radio4")!=null){
	   if(document.getElementById("radio4").checked==true){
		//alert(document.getElementById("e-mail").value);
	      if(document.getElementById("e-mail").value!=""){
			//alert("E-mail OK");
		     document.getElementById("e-mail").style.backgroundColor="#B8F5B1";
		    }else{
		//	alert("E-mail Error");
			document.getElementById("e-mail").style.backgroundColor="#FF9F9F";
			flag=1;
		    }
	    }
	}
	//alert(flag);
	if(flag==0){
	   if(document.getElementById('TerAndCon').checked==true){
		   //alert("T&C OK");
		   document.getElementById("subImage").src="images/pleasewait.png";
		}else{
			alert("Please Read the Terms And Condittions And Confirm");
			flag=2;
		}
	}
	if((flag==0)&&(flag!=2)){
		return true;
	}else{
		if(flag!=2){
		   alert("Please fill the field(s) marked in red");
		}
		return false
	}
	/*if(document.getElementById('Tr').checked==false){
		flag=1;
	}		
	alert(flag);*/
	
	//getCode=getCode+document.getElementById("divPickdate").innerHTML="TEST";
}

function testTelNumber () {
  var myTelNo = document.getElementById('os4').value;
  // If invalid number, report back error
  if (!checkUKTelephone (myTelNo)) {
	  if(telNumberErrorNo!=0){
        alert (telNumberErrors[telNumberErrorNo]);
	  }
	 return false;
  }
  // Otherwise redisplay telephone number on form in corrected format
  else {
     //document.getElementById('os4').value =  checkUKTelephone (myTelNo);
     //alert ("Telephone number appears to be valid");
	 return true;
  }
}


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;
  }
	
	// Disallow numbers allocated for dramas.
	 
  // Array holds the regular expressions for the drama telephone numbers
  var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) {
    if ( tnexp[i].test(telnum) ) {
      telNumberErrorNo = 5;
      return false;
    }
	}
  
  // Finally check that the telephone number is appropriate.
  exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Telephone number seems to be valid - return the stripped telehone number  
  return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";

