/** Function for Captilising field values **/
function firstCap(fieldVal1, fieldName, formName){
	fieldVal = trim(fieldVal1);
	if(fieldVal.length == 1){
		var newVal = fieldVal.charAt(0).toUpperCase() + fieldVal.slice(1);
		document.forms[formName].elements[fieldName].value = newVal;
	}
}

function initialCap(fieldVal, fieldName, formName){
	var words = fieldVal.split(' ');
	for(i = 0; i <= words.length - 1; i++){
		words[i] = words[i].substr(0, 1).toUpperCase() + words[i].substr(1).toLowerCase();
	}
	document.forms[formName].elements[fieldName].value = words.join(' ');
}
function allLower(fieldValue, fieldName, formName){
	document.forms[formName].elements[fieldName].value = fieldValue.toLowerCase();		
}
function allUpper(fieldValue, fieldName, formName){
	document.forms[formName].elements[fieldName].value = fieldValue.toUpperCase();		
}
function formText(fieldVal, fieldName, formName){
	fieldVal = fieldVal.toLowerCase();
	var Arr = fieldVal.split("");
	for(i = 0; i <= fieldVal.length - 1; i++){
		if(Arr[i] == '.' || Arr[i] == '?' || Arr[i] == '!'){
			if(Arr[i+1] == " ") Arr[i+2] = Arr[i+2].toUpperCase();
			if(fieldVal.length != i+1){
				if(Arr[i+1] != " "){
					Arr[i] = Arr[i] + " ";
					Arr[i+1] = Arr[i+1].toUpperCase();
				}
			}
		}
		if((Arr[i]=='m' || Arr[i]=='M') && (Arr[i+1]=='r' || Arr[i+1]=='R') && (Arr[i+2]==' ')){
			try
			{
				if(Arr[i-1]==' ' || i==0 || Arr[i-1]=='.'){
					Arr[i] = Arr[i].toUpperCase();
					Arr[i+1] = Arr[i+1].toLowerCase();
					Arr[i+3] = Arr[i+3].toUpperCase();				
				}
			}catch (e){}
		}
		if((Arr[i]=='m' || Arr[i]=='M') && (Arr[i+1]=='r' || Arr[i+1]=='R') && (Arr[i+2]=='s' || Arr[i+2]=='S') && (Arr[i+3]==' ')){
			try
			{
				if(Arr[i-1]==' ' || i==0 || Arr[i-1]=='.'){
					Arr[i] = Arr[i].toUpperCase();
					Arr[i+1] = Arr[i+1].toLowerCase();
					Arr[i+2] = Arr[i+2].toLowerCase();
					Arr[i+4] = Arr[i+4].toUpperCase();				
				}
			}catch (e){}
		}
		if((Arr[i]=='m' || Arr[i]=='M') && (Arr[i+1]=='i' || Arr[i+1]=='I') && (Arr[i+2]=='s' || Arr[i+2]=='S') && (Arr[i+3]=='s' || Arr[i+3]=='S')  && (Arr[i+4]==' ')){
			try
			{
				if(Arr[i-1]==' ' || i==0 || Arr[i-1]=='.'){
					Arr[i] = Arr[i].toUpperCase();
					Arr[i+1] = Arr[i+1].toLowerCase();
					Arr[i+2] = Arr[i+2].toLowerCase();
					Arr[i+3] = Arr[i+3].toLowerCase();
					Arr[i+5] = Arr[i+5].toUpperCase();			
				}
			}catch (e){}
		}
		if((Arr[i]=='d' || Arr[i]=='D') && (Arr[i+1]=='r' || Arr[i+1]=='R') && (Arr[i+2]==' ')){
			try
			{
				if(Arr[i-1]==' ' || i==0 || Arr[i-1]=='.'){
					Arr[i] = Arr[i].toUpperCase();
					Arr[i+1] = Arr[i+1].toLowerCase();
					Arr[i+3] = Arr[i+3].toUpperCase();				
				}
			}catch (e){}
		}
		if((Arr[i]=='p' || Arr[i]=='P') && (Arr[i+1]=='r' || Arr[i+1]=='R') && (Arr[i+2]=='o' || Arr[i+2]=='O') && (Arr[i+3]=='f' || Arr[i+3]=='F') && (Arr[i+4]==' ')){
			try
			{
				if(Arr[i-1]==' ' || i==0 || Arr[i-1]=='.'){
					Arr[i] = Arr[i].toUpperCase();
					Arr[i+1] = Arr[i+1].toLowerCase();
					Arr[i+2] = Arr[i+2].toLowerCase();
					Arr[i+3] = Arr[i+3].toLowerCase();
					Arr[i+5] = Arr[i+5].toUpperCase();				
				}
			}catch (e){}
		}
		if((Arr[i]=='m' || Arr[i]=='M') && (Arr[i+1]=='s' || Arr[i+1]=='S') && (Arr[i+2]==' ')){
			try
			{
				if(Arr[i-1]==' ' || i==0 || Arr[i-1]=='.'){
					Arr[i] = Arr[i].toUpperCase();
					Arr[i+1] = Arr[i+1].toLowerCase();
					Arr[i+3] = Arr[i+3].toUpperCase();				
				}
			}catch (e){}
		}
		if(Arr[i] == '\n')
			Arr[i+1] = Arr[i+1].toUpperCase();
		if(Arr[i] == 'i' && Arr[i+1] == ' ' && Arr[i-1] == ' ')
			Arr[i] = Arr[i].toUpperCase();
		if(i == 0)
			Arr[i] = Arr[i].toUpperCase();
	}
	document.forms[formName].elements[fieldName].value = Arr.join("");
}

function goToBeg(fieldValue, fieldName, formName){
	document.forms[formName].elements[fieldName].value = fieldValue;
}

/********* End ***********/

function tele_validate(value, name, formName){
	var iChars = "01234567890 ";
	for (var i = 0; i < value.length; i++) {
		if ((iChars.indexOf(value.charAt(i)) == -1) || (value.charAt(i)==" " && value.charAt(i-1)==" ")){
			alert("Invalid value");
			document.forms[formName].elements[name].value = "";
			document.forms[formName].elements[name].focus();
			return false;
		}
	}
}

function number_only(value, name, formName){
	if(value!="" && isNaN(value)){
		alert("Invalid Value");
		document.forms[formName].elements[name].value = "";
		document.forms[formName].elements[name].focus();
		return false;
	}
}

function textEntry(name, value, formName){
	var ereg = /^[A-Za-z ]*$/;
	if(ereg.test(value) == false){
		alert("Invalid Value");
		document.forms[formName].elements[name].value = "";
		document.forms[formName].elements[name].focus();
	}
}

/** Functions for Input Masking**/
	function textOnly(fieldValue){
		var Length = fieldValue.length;
		var iChars = "~`!@#$%^&*()_+|\=-\"':;?/<>,[]}{01234567890";
		var ereg = /^[\s ]*$/ ;
		for (var i = 0; i < Length; i++) {
			if ((iChars.indexOf(fieldValue.charAt(i)) != -1) || (ereg.test(fieldValue) == true)){
				return true;
			}
		}
	}

	function textOnly1(fieldValue){
		var Length = fieldValue.length;
		var iChars = "~`!@#$%^&*()_+|\=\":;?/<>[]}{";
		var ereg   = /^[\s ]*$/ ;
		for(var i = 0; i < Length; i++) {
			if((iChars.indexOf(fieldValue.charAt(i)) != -1) || (ereg.test(fieldValue) == true)){
				return true;
			}
		}
	}

	function telNumber(fieldValue){
		var ereg = /^\+?[\d\s]+\(?[\d\s]{5,}$/ ;
		telval   = fieldValue;
		if(ereg.test(telval) == false){
			return true;									
		}		
	}
	function validField(fieldValue){
		var chars  = "!@#$%^*()+=[]\\;/{}|\":<>?";
		var Length = fieldValue.length;
		for (var i = 0; i < Length; i++) {
			if(chars.indexOf(fieldValue.charAt(i)) != -1){
				return true;
			}
		}	
	}
	function validI(fieldValue){
		var chars = "@#$^*+=[]\\\;/{}|\<>";
		var Length = fieldValue.length;
		for (var i = 0; i < Length; i++) {
			if(chars.indexOf(fieldValue.charAt(i)) != -1){
				return true;
			}
		}
	}
	function validCard(fieldValue){
		var ereg = /^((4\d{3})|(5[1-5]\d{2})|(6011)|(3[68]\d{2})|(30[012345]\d))[ -]?(\d{4})[ -]?(\d{4})[ -]?(\d{4}|3[4,7]\d{13})$/ ;
		cardval = fieldValue;
		if(ereg.test(cardval) == false){
			return true;									
		}	
	}
/********************************/
function validateaddress(add1){	
	// This function is used to validate a given e-mail 
	// address for the proper syntax
	if (add1 == ""){
		return false;
	}
	badStuff = ";:/,' \"\\";
	for (i=0; i<badStuff.length; i++){
		badCheck = badStuff.charAt(i)
		if (add1.indexOf(badCheck,0) != -1){
			return false;
		}		
	}
	return true;
}

function validateaddress2(add1){
	badStuff = ";:/,' \"\\";
	for (i=0; i<badStuff.length; i++){
		badCheck = badStuff.charAt(i)
		if (add1.indexOf(badCheck,0) != -1){
			return false;
		}
	}
	return true;
}

function validate_frmbook1()
{
	var i = 0;
	while(i < document.forms['frmbook1'].elements.length){
		document.forms['frmbook1'].elements[i].value = trim(document.forms['frmbook1'].elements[i].value);
		i++;
	}
	frm = document.frmbook1;
	if(frm.Title.value==""){
		alert("Please select the title");
		frm.Title.focus();
		return false;
	}
	if(trim(frm.firstname.value)==""){
		frm.firstname.value = "";
		alert("Please enter the first name");
		frm.firstname.select();
		return false;
	}
	if(trim(frm.firstname.value) !="" && textOnly(frm.firstname.value)){
		alert("Please enter valid First Name");
		frm.firstname.focus();
		return false;
	}
	if(trim(frm.surname.value)==""){
		frm.surname.value = "";
		alert("Please enter the surname");
		frm.surname.select();
		return false;
	}
	if(trim(frm.surname.value) !="" && textOnly(frm.surname.value)){
		alert("Please enter valid Surname");
		frm.surname.focus();
		return false;		
	}
	if(trim(frm.phone.value)==""){
		frm.phone.value = "";
		alert("Please enter the telephone number");
		frm.phone.select();
		return false;
	}
	if(trim(frm.phone.value) !="" && telNumber(frm.phone.value)){
		alert("Please enter valid Telephone Number");
		frm.phone.focus();
		return false;	
	}
	if(trim(frm.extension.value) != "" && validField(frm.extension.value)){
		alert("Please enter valid Extension");
		frm.extension.focus();
		return false;	
	}
	if(trim(frm.email.value) == ""){
		frm.email.value = "";
		alert("Please enter the email address.");
		frm.email.select();
		return false;
	}
	emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!emailReg.test(frm.email.value) && frm.email.value != ""){
		alert("Please enter the email address correctly");
		frm.email.select();
		return false;
	}
	if(trim(frm.postcode.value)==""){
		frm.postcode.value = "";
		alert("Please enter the postal code");
		frm.postcode.select();
		return false;
	}
	if(trim(frm.postcode.value) !="" && validField(frm.postcode.value)){
		alert("Please enter Valid Postcode");
		frm.postcode.focus();
		return false;
	}
	/*
	if(frm.find_addr.value != "Yes"){
		if(frm.companyname.value !="" && validField(frm.companyname.value)){
			alert("Please enter Valid Company Name");
			frm.companyname.focus();
			return false;
		}
	}
	*/
	if(trim(frm.add1.value)==""){
		frm.add1.value = "";
		alert("Please enter the address1");
		frm.add1.select();
		return false;
	}
	if(trim(frm.add1.value) !="" && validField(frm.add1.value)){
		alert("Please enter Valid \"Address Line 1\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add1.focus();
		return false;	
	}
	if(trim(frm.add2.value) !="" && validField(frm.add2.value)){
		alert("Please enter Valid \"Address Line 2\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add2.focus();
		return false;
	}
	if(trim(frm.add3.value) !="" && validField(frm.add3.value)){
		alert("Please enter Valid \"Address Line 3\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add3.focus();
		return false;
	}
	if(trim(frm.city.value)==""){
		alert("Please enter the city");
		frm.city.select();
		return false;
	}
	if(trim(frm.city.value) !="" && textOnly1(frm.city.value)){
		alert("Please enter valid city");
		frm.city.focus();
		return false;
	}
}

function validateEmail(email){	
	if (email == ""){
		return false;
	}
	badStuff = ";:/,' \"\\";
	for (i=0; i<badStuff.length; i++){
		badCheck = badStuff.charAt(i)
		if (email.indexOf(badCheck,0) != -1){
			return false;
		}
	}
	posOfAtSign = email.indexOf("@",1)
	if (posOfAtSign == -1){
		return false;
	}
	if (email.indexOf("@",posOfAtSign+1) != -1){
		return false;
	}
	posOfPeriod = email.indexOf(".", posOfAtSign)
	if (posOfPeriod == -1){
		return false;
	}
	if (posOfPeriod+2 > email.length){
		return false;
	}
	return true
}

function validate_readystatus(val)
{
	spanObject = document.getElementById('dataform');
	if(val=="Specify Date")
		spanObject.style.display="block";
	else
		spanObject.style.display="none";	
}

function validate_frmbook2(nDate,curr_time)
{	
	var i = 0;
	while(i < document.forms['frmbook2'].elements.length){
		document.forms['frmbook2'].elements[i].value = trim(document.forms['frmbook2'].elements[i].value);
		i++;
	}
	frm = document.frmbook2;
	var ndate1=nDate;	
	var arrDate = String(nDate).split("-");
	var nDate = new Date(arrDate[2],arrDate[1],arrDate[0]);
	id=document.frmbook2.pickup_date[document.frmbook2.pickup_date.selectedIndex].value;
	var idarr=id.split('_');		

	c_time=curr_time;
	if(c_time>1230){
		if(idarr[2]==0){
			alert("Online orders must be booked by 12:30 for same-day collection. If you require a collection today please contact customer services on 0845 226 2994 or select another date to continue.");
			return false;
		}
	}
	if(trim(frm.reference.value) !="" && validField(frm.reference.value)){
		alert("Please enter valid Reference");
		frm.reference.select();
		return false;
	}
	if(trim(frm.content.value)==""){
		frm.content.value = "";
		alert("Please enter the Content of Consignment");
		frm.content.select();
		return false;
	}
	if(trim(frm.content.value) !="" && validField(frm.content.value)){
		alert("Please enter valid Content");
		frm.content.select();
		return false;		
	}
	if(trim(frm.cost_value.value)=="" && !(frm.no_value.checked==true)){
		alert("Please enter either cost value or check '"+"No Value"+"' check box");
		frm.cost_value.select();
		return false;
	}
	if(trim(frm.cost_value.value) !="" && validField(frm.cost_value.value)){
		alert("Please enter valid Cost Value");
		frm.cost_value.select();
		return false;
	}
	return true;
}

function validate_frmbook3()
{
	var i = 0;
	while(i < document.forms['frmbook3'].elements.length){
		document.forms['frmbook3'].elements[i].value = trim(document.forms['frmbook3'].elements[i].value);
		i++;
	}
	frm = document.frmbook3;
	if(trim(frm.content.value)==""){
		alert("Please enter the Content of Consignment");
		frm.content.select();
		return false;
	}
	if(trim(frm.cost_value.value)==""&&!(frm.no_value.checked==true)){
		alert("Please enter either cost value or check '"+"No Value"+"' check box");
		frm.cost_value.select();
		return false;
	}
	if(trim(frm.height.value)==""){
		frm.height.value = "";
		alert("Please enter the height");
		frm.height.select();
		return false;
	}
	if(frm.heightmeasure.value==""){
		alert("Please select the height measurement");
		frm.heightmeasure.focus();
		return false;
	}
	if(trim(frm.width.value)==""){
		frm.width.value = "";
		alert("Please enter the width");
		frm.width.select();
		return false;
	}
	if(frm.widthmeasure.value==""){
		alert("Please select the width measurement");
		frm.widthmeasure.focus();
		return false;
	}
	if(trim(frm.lengh.value)==""){
		frm.lengh.value = "";
		alert("Please enter the length");
		frm.lengh.select();
		return false;
	}
	if(frm.lenghmeasure.value==""){
		alert("Please select the length measure ");
		frm.lenghmeasure.focus();
		return false;
	}
	if(trim(frm.maxg.value)==""){
		frm.maxg.value = "";
		alert("Please enter max weight");
		frm.maxg.focus();
		return false;		
	}
	if(trim(frm.totalweight.value)==""){
		frm.totalweight.value = "";
		alert("Please enter the total weight");
		frm.totalweight.select();
		return false;		
	}
	if(isNaN(frm.maxg.value)){
		alert("Please enter correct weight of heaviest parcel");
		frm.maxg.select();
		return false;
	}
	if(isNaN(frm.totalweight.value)){
		alert("Please enter correct total weight");
		frm.totalweight.select();
		return false;
	}
	if(frm.totalweightmeasure.value==""){
		alert("Please select the totalweight measure");
		frm.totalweightmeasure.focus();
		return false;
	}
	if((!isempty(frm.height.value)) && (!isempty(frm.width.value)) && (!isempty(frm.lengh.value))){
		if(frm.heightmeasure.value=="cm")
			dim='cm';
		else
			dim='in';
		
		switch(dim) {
		case "in" : var narea = (frm.lengh.value * frm.width.value * frm.height.value)/1728; break;
		case "cm" : var narea = ((frm.lengh.value * frm.width.value * frm.height.value)/1000000)*35.31; break;
		}
		var farea = new ToFmt(narea);
		var darea = farea.fmtF(7,2);
		if(darea > 5.10) {
			alert("Your consignment exceeds our allowed size for online bookings. The maximum allowed length is 1.25 meters (4ft) and the combined dimensions of the package (length x width x height) must not exceed 4.5 cubic feet. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			return false;
		}
		if(isNaN(darea)) {
			alert("Your consignment exceeds our allowed size for online bookings. The maximum allowed length is 1.25 meters (4ft) and the combined dimensions of the package (length x width x height) must not exceed 4.5 cubic feet. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			return false;
		}		
	}		
	if(!isempty(frm.maxg.value)){
		if(frm.numberofitems.value <2 && frm.maxg.value >20 && frm.maxk.value=="Kilograms"){
			alert("Your consignment exceeds our allowed weight for individual parcels. The maximum weight for an individual parcel to be booked online is 20kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.maxg.focus();
			return false;
		}
		if(frm.numberofitems.value <2 && frm.maxg.value >20000 && frm.maxk.value=="Grams"){
			alert("Your consignment exceeds our allowed weight for individual parcels. The maximum weight for an individual parcel to be booked online is 20kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.maxg.focus();
			return false;
		}
		if(frm.numberofitems.value >1 && frm.maxg.value >80 && frm.maxk.value=="Kilograms"){
			alert("Your consignment exceeds our allowed combined weight limit. The maximum allowed combined weight for a consignment to be booked online is 80kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.maxg.focus();
			return false;
		}
		if(frm.numberofitems.value >1 && frm.maxg.value >80000 && frm.maxk.value=="Grams"){
			alert("Your consignment exceeds our allowed combined weight limit. The maximum allowed combined weight for a consignment to be booked online is 80kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.maxg.focus();
			return false;
		}
	}
	if(frm.maxk.value==""){
		alert("Please select weight measure");
		frm.maxk.focus();
		return false;		
	}
	if(frm.maxk.value=="Kilograms")
		weight = Number(frm.maxg.value) * 1000;	
	else	
		weight = Number(frm.maxg.value);
	
	if(!isempty(frm.maxg.value)){
		if(frm.numberofitems.value >1 && weight >20000){
			alert("Your consignment exceeds our allowed weight for individual parcels. The maximum weight for an individual parcel to be booked online is 20kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.maxg.focus();
			return false;
		}		
		if(frm.numberofitems.value <2 && weight >20000){
			alert("Your consignment exceeds our allowed weight for individual parcels. The maximum weight for an individual parcel to be booked online is 20kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.maxg.focus();
			return false;
		}
	}	
	if(frm.totalweightmeasure.value=="Kilograms")
		weight = Number(frm.totalweight.value) * 1000;	
	else	
		weight = Number(frm.totalweight.value);	
	if(!isempty(frm.totalweight.value)){
		if(frm.numberofitems.value >1 && weight >80000){
			alert("Your consignment exceeds our allowed combined weight limit. The maximum allowed combined weight for a consignment to be booked online is 80kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.totalweight.focus();
			return false;
		}
		if(frm.numberofitems.value <2 && weight >20000){
			alert("Your consignment exceeds our allowed weight for individual parcels. The maximum weight for an individual parcel to be booked online is 20kg. Please contact us on 0845 226 2994 with the size and weight of your consignment and we will be happy to provide you with a quotation. ");
			frm.totalweight.focus();
			return false;
		}
	}
}

function isempty(txtvalue)
{
txtvalue=txtvalue.replace(/\s/g,"")
if (txtvalue.length > 0)     return false; else return true;
}

function ToFmt(x){
 this.x=x;
 this.fmt00 = fmt00;
 this.fmtF = fmtF;
 this.fmtE=fmtE;
 this.fmtI=fmtI;
 this.spacer=" ";
 this.setSpacer=setSpacer;
}

function fmt00(){
 // fmt00: Tags leading zero onto numbers 0 - 9.
 // Particularly useful for displaying results from Date methods.
 //
 if (parseInt(this.x) < 0) var neg = true;
 if (Math.abs(parseInt(this.x)) < 10){
  this.x = "0"+ Math.abs(this.x);
 }
 if (neg) this.x = "-"+this.x;
 return this.x;
}

function fmtF(w,d){
 // fmtF: formats in a style similar to Fortran's Fw.d, where w is the
 // width of the field and d is the number of figures after the decimal
 // point. 
 // The result is aligned to the right of the field.  The default
 // padding character is a space " ". This can be modified using the 
 // setSpacer(string) method of ToFmt. 
 // If the result will not fit in the field , the field will be returned
 // containing w asterisks.
 var width=w;
 var dpls=d;
 var lt1=false;
 var len=this.x.toString().length;
 var junk;
 var res="";
// First check for valid format request
 if ( width < (dpls+2)){
  window.alert("Illegal format specified : w = " + d +
               " w = " + d +
                "\nUsage: [ToFmt].fmtF(w,d)" +
                "\nWidth (w) of field must be greater or equal to the number " +
                "\nof digits to the right of the decimal point (d) + 2");
  junk = filljunk(width);
  return junk;
 }
// Work with absolute value
 var absx=Math.abs(this.x);
// Nasty fix to deal with numbers < 1 and problems with leading zeros!
 if ((absx < 1) && (absx > 0)){
  lt1 = true;
  absx+=10;
 }
// Get postion of decimal point
 var pt_pos = absx.toString().indexOf(".");
 if ( pt_pos == -1){
  res+= absx;
  res+= ".";
  for (var i = 0; i < dpls; i++){
   res += 0;
  }  
 }
 else{
  res = Math.round(absx * Math.pow(10,dpls));
  res=res.toString();
  if (res.length == 
      Math.round(Math.floor(absx * Math.pow(10,dpls))).toString().length){ 
   res = res.substring(0,pt_pos) + "." + 
         res.substring(pt_pos,res.length);
  }
  else{
   pt_pos++;
   res = res.substring(0,pt_pos) + "." + 
          res.substring(pt_pos,res.length);
  } 
// Remove leading 1 from  numbers < 1 (Nasty fix!)
  if (lt1) {
   res=res.substring(1,res.length);
  }
 }
 // Final formatting statements
 // Reinsert - sign for negative numbers
 if (this.x < 0)res = "-"+res;
 // Check whether the result fits in the width of the field specified
 if (res.length > width){
  res=filljunk(width);
 }
 // If necessary, pad from the left with the spacer string
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
}

function fmtE(w,d){
 // fmtE: formats in a style similar to Fortran's Ew.d, where w is the
 // width of the field and d is the number of figures after the decimal
 // point. 
 // The result is aligned to the right of the field.  The default
 // padding character is a space " ". This can be modified using the 
 // setSpacer(string) method of ToFmt. 
 // If the result will not fit in the field , the field will be returned
 // containing w asterisks.
 //
 var width=w;
 var dpls=d;
 var e="E+";
 var len=this.x.toString().length;
 var pow10;
 var xp10;
 var junk;
 var res="";
// First check for valid format request
 if ( width < (dpls+5)){
  window.alert("Illegal format specified : w = " + d +
               " w = " + d +
                "\nUsage: [ToFmt].fmtE(w,d)" +
                "\nWidth (w) of field must be greater or equal to the number " +
                "\nof digits to the right of the decimal point (d) + 6");
  junk = filljunk(w);
  return junk;
 }
// Work with absolute value
 var absx=Math.abs(this.x);
// Get postion of decimal point
 var pt_pos = absx.toString().indexOf(".");
// For x=0
 if (absx == 0){
  res +="0.";
  for (var i=0; i< dpls; i++){
   res += "0";
  }
  res  += "E+00";
 }
// For abs(x) >= 1 
 else if (absx >= 1.0){
  pow10=1;
  xp10 = absx;
  while (xp10 >= 1.){
   pow10++;
   xp10 /= 10;
  }
  res = Math.round(xp10 * Math.pow(10,dpls));
  res=res.toString();
  if (res.length == 
      Math.round(Math.floor(xp10 * Math.pow(10,dpls))).toString().length){ 
    pow10--;
  }
  res = "0." + res.substring(0,dpls) + e + (new ToFmt(pow10)).fmt00();
 }
// For abs(x) < 1
 else if (absx < 1.0){
  pow10=1;
  xp10 = absx;
  while (xp10 < 1.){
   pow10--;
   xp10 *= 10;
  }
  res = Math.round(xp10/10 * Math.pow(10,dpls));
  res=res.toString();
  if (res.length != 
      Math.round(Math.floor(xp10/10 * Math.pow(10,dpls))).toString().length){ 
    pow10++;
  }
  if (pow10 < 0) e = "E-";
  res = "0." + res.substring(0,dpls) + e + (new ToFmt(Math.abs(pow10))).fmt00();
 }
 if (this.x < 0)res = "-"+res;
 if (res.length > width){
  res=filljunk(width);
 }
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
}

function fmtI(w){
 // fmtI: formats in a style similar to Fortran's Iw, where w is the
 // width of the field.
 // Floating point values are truncated (rounded down) for integer
 // representation.
 // The result is aligned to the right of the field.  The default
 // padding character is a space " ". This can be modified using the 
 // setSpacer(string) method of ToFmt. 
 // If the result will not fit in the field , the field will be returned
 // containing w asterisks.
 var width=w;
 var lt0=false;
 var len=this.x.toString().length;
 var junk;
 var res="";
// Work with absolute value
 var absx = Math.abs(this.x);
// Test for < 0
 if (parseInt(this.x) < 0){
  lt0 = true;
 }
 res = Math.round(Math.floor((absx))).toString();
 if (lt0){
  res = "-"+res;
 }
 if (res.length > width){
  res=filljunk(width);
 }
 else if (res.length < width){
  var res_bl="";
  for (var i = 0; i < (width - res.length); i++){
   res_bl += this.spacer ;
  } 
  res = res_bl + res;
 }
 return res;
}

function filljunk(lenf){
 // Fills field of length lenf with asterisks
 var str="";
 for (var i=0; i < lenf; i++){
  str +="*";
 }
 return str;
}

function setSpacer(spc){
 var spc;
 this.spacer=spc;
 return this.spacer;
}

function validate_frmbook4(){
	var i = 0;
	while(i < document.forms['frmbook4'].elements.length){
		document.forms['frmbook4'].elements[i].value = trim(document.forms['frmbook4'].elements[i].value);
		i++;
	}
	frm = document.frmbook4;
	if(frm.title1.value==""){
		alert("Please select the title");
		frm.title1.focus();
		return false;
	}
	if(trim(frm.firstname1.value)==""){
		frm.firstname1.value = "";
		alert("Please enter the first name");
		frm.firstname1.focus();
		return false;
	}
	if(trim(frm.firstname1.value) !="" && textOnly(frm.firstname1.value)){
		alert("Please enter valid First Name");
		frm.firstname1.focus();
		return false;
	}
	if(trim(frm.surname1.value)==""){
		frm.surname1.value = "";
		alert("Please enter the surname");
		frm.surname1.focus();
		return false;
	}
	if(trim(frm.surname1.value) !="" && textOnly(frm.surname1.value)){
		alert("Please enter valid Surname");
		frm.surname1.focus();
		return false;		
	}
	if(trim(frm.phone1.value)==""){
		frm.phone1.value = "";
		alert("Please enter the telephone number");
		frm.phone1.focus();
		return false;
	}
	if(trim(frm.phone1.value) !="" && telNumber(frm.phone1.value)){
		alert("Please enter valid Telephone Number");
		frm.phone1.focus();
		return false;
	}
	if(trim(frm.extension1.value) != "" && validField(frm.extension1.value)){
		alert("Please enter valid Extension");
		frm.extension1.focus();
		return false;	
	}
	if(trim(frm.postcode1.value)==""){
		frm.postcode1.value = "";
		alert("Please enter the postal code");
		frm.postcode1.focus();
		return false;
	}
	if(trim(frm.postcode1.value) !="" && validField(frm.postcode1.value)){
		alert("Please enter Valid Postcode");
		frm.postcode1.focus();
		return false;
	}
	/*
	if(frm.find_addr1.value != "Yes"){
		if(frm.companyname1.value !="" && validField(frm.companyname1.value)){
			alert("Please enter Valid Company Name");
			frm.companyname1.focus();
			return false;	
		}
	}
	*/
	if(trim(frm.add11.value)==""){
		frm.add11.value = "";
		alert("Please enter the address1");
		frm.add11.focus();
		return false;
	}
	if(trim(frm.add11.value) !="" && validField(frm.add11.value)){
		alert("Please enter Valid \"Address Line 1\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add11.focus();
		return false;	
	}
	if(trim(frm.add21.value) !="" && validField(frm.add21.value)){
		alert("Please enter Valid \"Address Line 2\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add21.focus();
		return false;
	}
	if(trim(frm.add31.value) !="" && validField(frm.add31.value)){
		alert("Please enter Valid \"Address Line 3\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add31.focus();
		return false;
	}
	if(trim(frm.city1.value)==""){
		frm.city1.value = "";
		alert("Please enter the city");
		frm.city1.focus();
		return false;
	}
	if(trim(frm.city1.value) !="" && textOnly1(frm.city1.value)){
		alert("Please enter valid city");
		frm.city1.focus();
		return false;	
	}
	if(trim(frm.instructions.value) !="" && validI(frm.instructions.value)){
		alert("Please enter Valid \"Special Instructions\". It contains invalid characters like @ # & $ ^ * ( ) + = [ ] \ ; / { } | < >");
		frm.instructions.focus();
		return false;	
	}else{
		return true;
	}
}

function movewin() { return; }

function movewin(win, width, height) 
{
	var fullWidth = screen.availWidth;
	var fullHeight = screen.availHeight;
	var x = (fullWidth/2)-(width/2);
	var y = (fullHeight/2)-(height/2);
	win.moveTo(x,y); 
}

function viewWin(filenames,val)
	{	
	if(val==1){
		frm=document.frmbook1;
		postcode=frm.postcode.value;
	}	
	if(val==2){
		frm=document.frmbook4;
		postcode=frm.postcode1.value;
	}
	if(val==3){
		frm=document.frmbook5;
		postcode=frm.postcode2.value;
	}
	filenames=filenames+"postcode=" + postcode + "&frmnumber=" + val;	
	var mybars='width=500,height=450,directories=no,location=no,menubar=no,status=no,screenX=0,screenY=0';
	mybars+=',titlebar=no,toolbar=no';
	myoptions='scrollbars=yes,resizeable=yes';
	myfeatures=mybars+ ','+myoptions	
	var newwin=window.open(filenames,'mydoc',myfeatures);
		movewin(newwin, 500, 450);	
	    if (navigator.appName=="Netscape") newwin.location=url;
		newwin.opener=window;
		newwin.focus();
		return false;
	}
 		    	
function clear_frmbook4()
{
	frm	= document.frmbook4;	
	frm.title1.value	= "";
	frm.firstname1.value= "";
	frm.surname1.value  = "";
	frm.phone1.value    = "";
	frm.extension1.value= "";	
	frm.companyname1.value = "";
	frm.add11.value = "";
	frm.add21.value = "";
	frm.add31.value = "";
	frm.city1.value = "";
	frm.postcode1.value = "";		
	frm.instructions.value = "";
	frm.find_addr1.value = "";
}

function fill_frmbook4(title,firstname,surname,phone,extension,email,companyname,add1,add2,add3,city,postcode,find_addr){
	frm = document.frmbook4;
	frm.title1.value = title;
	frm.firstname1.value = firstname;
	frm.surname1.value = surname;
	frm.phone1.value = phone;
	frm.extension1.value = extension;
	frm.postcode1.value = postcode;	
	frm.companyname1.value = companyname;
	frm.add11.value = add1;
	frm.add21.value = add2;
	frm.add31.value = add3;
	frm.city1.value = city;
	//frm.find_addr1.value = find_addr;
}

function clear_frmbook5(){
	frm = document.frmbook5;	
	frm.title2.value = "";
	frm.firstname2.value = "";
	frm.surname2.value = "";
	frm.phone2.value = "";
	frm.extension2.value = "";
	frm.companyname2.value = "";
	frm.add12.value = "";
	frm.add22.value = "";
	frm.add32.value = "";
	frm.city2.value = "";
	frm.postcode2.value = "";		
	frm.instructions2.value = "";	
	frm.marketing.value = "";
	frm.other.value = "";
	frm.find_addr2.value = "";
}

function fill_frmbook5(title,firstname,surname,phone,extension,email,companyname,add1,add2,add3,city,postcode,find_addr){
	frm = document.frmbook5;	
	frm.title2.value = title;
	frm.firstname2.value = firstname;
	frm.surname2.value = surname;
	frm.phone2.value = phone;
	frm.extension2.value = extension;
	frm.postcode2.value = postcode;		
	frm.companyname2.value = companyname;
	frm.add12.value = add1;
	frm.add22.value = add2;
	frm.add32.value = add3;
	frm.city2.value = city;
	frm.find_addr2.value = find_addr;
}

function validate_frmbook5()
{
	var i = 0;
	while(i < document.forms['frmbook5'].elements.length){
		document.forms['frmbook5'].elements[i].value = trim(document.forms['frmbook5'].elements[i].value);
		i++;
	}
	frm = document.frmbook5;
	if(frm.title2.value==""){
		alert("Please select the title");
		frm.title2.focus();
		return false;
	}
	if(trim(frm.firstname2.value)==""){
		frm.firstname2.value = "";
		alert("Please enter the first name");
		frm.firstname2.focus();
		return false;
	}
	if(trim(frm.firstname2.value) !="" && textOnly(frm.firstname2.value)){
		alert("Please enter valid First Name");
		frm.firstname2.focus();
		return false;		
	}
	if(trim(frm.surname2.value)==""){
		frm.surname2.value = "";
		alert("Please enter the surname");
		frm.surname2.focus();
		return false;
	}
	if(trim(frm.surname2.value) !="" && textOnly(frm.surname2.value)){
		alert("Please enter valid Surname");
		frm.surname2.focus();
		return false;		
	}
	if(trim(frm.phone2.value)==""){
		alert("Please enter the telephone number");
		frm.phone2.focus();
		return false;
	}
	if(trim(frm.phone2.value) !="" && telNumber(frm.phone2.value)){
		alert("Please enter valid Telephone Number");
		frm.phone2.focus();
		return false;	
	}
	if(trim(frm.extension2.value) != "" && validField(frm.extension2.value)){
		alert("Please enter valid Extension");
		frm.extension2.focus();
		return false;	
	}
	if(trim(frm.postcode2.value)==""){
		frm.postcode2.value = "";
		alert("Please enter the postal code");
		frm.postcode2.focus();
		return false;
	}
	if(trim(frm.postcode2.value) !="" && validField(frm.postcode2.value)){
		alert("Please enter Valid Postcode");
		frm.postcode2.focus();
		return false;		
	}
	/*
	if(frm.find_addr2.value != "Yes"){
		if(frm.companyname2.value !="" && validField(frm.companyname2.value)){
			alert("Please enter Valid Company Name");
			frm.companyname2.focus();
			return false;
		}
	}
	*/
	if(trim(frm.add12.value)==""){
		frm.add12.value = "";
		alert("Please enter the address1");
		frm.add12.focus();
		return false;
	}
	if(trim(frm.add12.value) !="" && validField(frm.add12.value)){
		alert("Please enter Valid \"Address Line 1\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add12.focus();
		return false;
	}
	if(trim(frm.add22.value) !="" && validField(frm.add22.value)){
		alert("Please enter Valid \"Address Line 2\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add22.focus();
		return false;
	}
	if(trim(frm.add32.value) !="" && validField(frm.add32.value)){
		alert("Please enter Valid \"Address Line 3\". It contains invalid characters like ~ ` ! @ # & $ % ^ * + | = ? ; } < > {");
		frm.add32.focus();
		return false;	
	}
	if(trim(frm.city2.value)==""){
		frm.city2.value = "";
		alert("Please enter the city");
		frm.city2.focus();
		return false;
	}
	if(trim(frm.city2.value) !="" && textOnly1(frm.city2.value)){
		alert("Please enter valid city");
		frm.city2.focus();
		return false;	
	}

	if(trim(frm.instructions2.value) !="" && validI(frm.instructions2.value)){
		alert("Please enter Valid \"Special Instructions\". It contains invalid characters like @ # & $ ^ * ( ) + = [ ] \ ; / { } | < >");
		frm.instructions2.focus();
		return false;
	}

	if(frm.marketing.value==""){
		alert("Please select how you heard about us.");
		frm.marketing.focus();
		return false;
	}
	if(frm.marketing.value=="Other" && trim(frm.other.value)==""){
		alert("Please enter how you heard about us.");
		frm.other.focus();
		return false;
	}
	if(trim(frm.other.value) !="" && validField(frm.other.value)){
		alert("Please enter Valid how you heard about us");
		frm.other.focus();
		return false;	
	}else{
		return true;
	}
}

function validate_frmbook6()
{
	if(frmbook6.terms.checked==false)
	{
		alert("Please agree to our term and conditions");
		frmbook6.terms.focus();
		return false;
	}
}

function changeall(val,val1)
{
	frm=document.frmbook3;		
	if(val=="cm")
		val2="inches";
	if(val=="inches")
		val2="cm";	
	if(val1==1)
	{
		while (frm.widthmeasure.length > 0)
			frm.widthmeasure.options[0]=null;
		while (frm.lenghmeasure.length > 0)
			frm.lenghmeasure.options[0]=null; 		
		len=frm.widthmeasure.length;
		frm.widthmeasure.options[len]=new Option(val,val);
		len=frm.widthmeasure.length;
		frm.widthmeasure.options[len]=new Option(val2,val2);		
		len=frm.lenghmeasure.length;
		frm.lenghmeasure.options[len]=new Option(val,val);
		len=frm.lenghmeasure.length;
		frm.lenghmeasure.options[len]=new Option(val2,val2);		
	}
	if(val1==2)
	{
		while (frm.heightmeasure.length > 0)
			frm.heightmeasure.options[0]=null; 
		while (frm.lenghmeasure.length > 0)
			frm.lenghmeasure.options[0]=null; 		
		len=frm.heightmeasure.length;
		frm.heightmeasure.options[len]=new Option(val,val);
		len=frm.heightmeasure.length;
		frm.heightmeasure.options[len]=new Option(val2,val2);		
		len=frm.lenghmeasure.length;
		frm.lenghmeasure.options[len]=new Option(val,val);
		len=frm.lenghmeasure.length;
		frm.lenghmeasure.options[len]=new Option(val2,val2);
	}
	
	if(val1==3)
	{
		while (frm.heightmeasure.length > 0)
			frm.heightmeasure.options[0]=null; 
		while (frm.widthmeasure.length > 0)
			frm.widthmeasure.options[0]=null;		
		len=frm.heightmeasure.length;
		frm.heightmeasure.options[len]=new Option(val,val);
		len=frm.heightmeasure.length;
		frm.heightmeasure.options[len]=new Option(val2,val2);		
		len=frm.widthmeasure.length;
		frm.widthmeasure.options[len]=new Option(val,val);
		len=frm.widthmeasure.length;
		frm.widthmeasure.options[len]=new Option(val2,val2);
	}
}

function convert(val,val1)
{			
	ivalue=val.toUpperCase();
	if(val1==1)
		frmbook1.postcode.value=ivalue;
	if(val1==2)
		frmbook4.postcode1.value=ivalue;
	if(val1==3)
		frmbook5.postcode2.value=ivalue;		
}

function changemonth(val)
{
	frm=document.frmbook2;	
	while (frm.pickupmonth.length > 0)
			frm.pickupmonth.options[0]=null;
	len=frm.pickupmonth.length;
	frm.pickupmonth.options[len]=new Option(val,val);	
}

function changeday(val)
{
	frm=document.frmbook2;	
	while (frm.pickupday.length > 0)
			frm.pickupday.options[0]=null;
	len=frm.pickupday.length;
	frm.pickupday.options[len]=new Option(val,val);
}

function changeyear(val)
{
	frm=document.frmbook2;	
	while (frm.pickupyear.length > 0)
			frm.pickupyear.options[0]=null;
	len=frm.pickupyear.length;
	frm.pickupyear.options[len]=new Option(val,val);
}

function changegram(val)
{
	frm=document.frmbook3;
	frm.totalweight.value=val;
	while (frm.totalweightmeasure.length > 0)
			frm.totalweightmeasure.options[0]=null;
	len=frm.totalweightmeasure.length;
	frm.totalweightmeasure.options[len]=new Option("Grams","Grams");	
}

function changekilo(val)
{
	frm=document.frmbook3;	
	if(frm.maxg.value==""){
		alert("Please enter max weight");
		frm.maxg.focus();
		return false;		
	}else{
		var numBoxes=frm.numberofitems.value;
		if(numBoxes=='1'){
		frm.totalweight.value=frm.maxg.value;
		}
		while (frm.totalweightmeasure.length > 0)
		frm.totalweightmeasure.options[0]=null;
		len=frm.totalweightmeasure.length;
		frm.totalweightmeasure.options[len]=new Option(val,val);
	}
}

	function checkvalue(val,fval)
	{
		var xval=val.split('.');		
		var xxval=xval[0];
		if(xval[1]>5)
			xxval=eval(xval[0])+1;		
		if(fval==1)
			frmbook3.height.value=xxval;
		if(fval==2)
			frmbook3.width.value=xxval;
		if(fval==3)
			frmbook3.lengh.value=xxval;
	}
	function unset_check()
	{
		if(document.frmbook3.cost_value.value!=""){
			document.frmbook3.no_value.checked=false;
			document.frmbook3.no_value.disabled=true;
		}
		else
		document.frmbook3.no_value.disabled=false;				
	}
	function unset_check1()
	{
		if(document.frmbook3.no_value.checked==true){
			document.frmbook3.cost_value.value="";
			document.frmbook3.cost_value.disabled=true;			
		}else{
			document.frmbook3.cost_value.value="";
			document.frmbook3.cost_value.disabled=false;
		}
	}
	
function setAll(val15,val16)
{
	boxlength= document.frmbook3.elements['lenghmeasure'+ val16];
	boxheight= document.frmbook3.elements['heightmeasure'+ val16];
	boxwidth= document.frmbook3.elements['widthmeasure'+ val16];	
	boxlength.value=val15;
	boxheight.value=val15;
	boxwidth.value=val15;		
}

function repeat_values(val6)
{
	val5=val6-1;
	boxlength= document.frmbook3.elements['lengh'+ val6];
	boxheight= document.frmbook3.elements['height'+ val6];
	boxwidth= document.frmbook3.elements['width'+ val6];		
	boxlength1= document.frmbook3.elements['lengh'+ val5];
	boxheight1= document.frmbook3.elements['height'+ val5];
	boxwidth1= document.frmbook3.elements['width'+ val5];	
	boxlength.value=boxlength1.value;
	boxwidth.value=boxwidth1.value;
	boxheight.value=boxheight1.value;		
	boxlength3= document.frmbook3.elements['lenghmeasure'+ val6];
	boxheight3= document.frmbook3.elements['heightmeasure'+ val6];
	boxwidth3= document.frmbook3.elements['widthmeasure'+ val6];		
	boxlength4= document.frmbook3.elements['lenghmeasure'+ val5];
	boxheight4= document.frmbook3.elements['heightmeasure'+ val5];
	boxwidth4= document.frmbook3.elements['widthmeasure'+ val5];	
	boxlength3.value=boxlength4.value;
	boxwidth3.value=boxwidth4.value;
	boxheight3.value=boxheight4.value;			
	boxlength1= document.frmbook3.elements['maxk'+ val6];
	boxlength2= document.frmbook3.elements['maxk'+ val5];	
	boxlength3= document.frmbook3.elements['maxg'+ val6];		
	boxlength4= document.frmbook3.elements['maxg'+ val5];			
	boxlength1.value=boxlength2.value;
	boxlength3.value=boxlength4.value;	
	return false;
}
function repeat_weight(val6)
{
	val5 = val6-1;
	boxlength1 = document.frmbook3.elements['maxk'+ val6];
	boxlength2 = document.frmbook3.elements['maxk'+ val5];	
	boxlength3 = document.frmbook3.elements['maxg'+ val6];		
	boxlength4 = document.frmbook3.elements['maxg'+ val5];			
	boxlength1.value = boxlength2.value;
	boxlength3.value = boxlength4.value;
	return false;
}

function LTrim( value ) {	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) {	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function trim( value ) {
	return LTrim(RTrim(value));
}