version=1;
online=true;

if(!online)
{
top.location="http://www.deliverynetworks.co.uk/offline.html"
}

// 	aUrl is used to contain the full domain name of the site to avoid permission problems between 
//	www.deliverynetworks.co.uk and www.delivery-net-works.com

var aSapResults = new Array;
var aSapServices = new Array;


var aBrowser="";
if (navigator.appName == 'Microsoft Internet Explorer') 
    aBrowser="msie";
else if (navigator.appName == 'Netscape') 
    aBrowser="nn";

//this whole section is to get around permission problems with the pavillion form. 
aUrl=document.location.href;
aUrl=aUrl.substr(0,aUrl.indexOf("/",10)+1);
if(document.location.href.search("buffer"))
	aUrl=aUrl+"buffer/";
//alert(aUrl);

//alert(aUrl);
function fnCalculateUrl()
{
	return top.aUrl;
	//return "http://www.deliverynetworks.co.uk/";
}


// preview window is the window that pops up for additional info, faq, help etc. 
var preview_window=null;
function fnOpenPreviewWindow(page)
{
	if((preview_window)&&(!preview_window.closed))
	{
		preview_window.close();
	}
	preview_window=window.open(page,'popupwindow', 'width=600,height=400,scrollbars=yes,screenX=0,screenY=0,top=0,left=0')
	preview_window.focus();
}

function fnClosePreviewWindow()
{
	if((preview_window)&&(!preview_window.closed))
	{
		preview_window.close();
	}
	preview_window=null;
}

function fnOpenPreviewWindow2(page)
{
	if((preview_window)&&(!preview_window.closed))
	{
		preview_window.close();
	}
	preview_window=window.open(page,'popupwindow', 'width=600,height=400,scrollbars=yes,menubar=yes,status=yes,toolbar=yes')
}

// aReferrer is a global variable in the top frame that stores the referrer code (set in welcome.html for the relevant partner directory
var aReferrer = "";

function fnGetParam(paramname, locationname)
{
	params = locationname.search.substring(1);
	stringarray = params.split('&');
	for(i=0;i<stringarray.length;i++)
	{
		thisparam=stringarray[i].split('=');
		if(thisparam[0]==paramname)
			return thisparam[1];
	}
}


// this function returns the full referrer name from the referrer id code.
function fnGetReferrer()
{
	returncode="Direct from Delivery Networks ";
	switch(top.aReferrer)
	{
	case "ws":
		returncode="WebStreet - ";
		break;
	case "wsp":
		returncode="WebSwappers - ";
		break;	
	case "cq":
		returncode="CQOut - ";
		break;			
	case "sh":
		returncode="SwapHut - ";
		break;	
	case "sw":
		returncode="SwapWorld - ";
		break;						
	case "as":
		returncode="AntiqueStreet - ";
		break;		
	case "fs":
		returncode="FSAuctions - ";
		break;
	case "cb":
		returncode="ClearlyBusiness - ";
		break;
	case "so":
		returncode="ScotlandOnline - ";
		break;		
	case "es":
		returncode="TheExportSite - ";
		break;			
	case "eb":
		returncode="eBay - ";
		break;		
	case "ebd":
		returncode="eBid - ";
		break;			
	case "dx":
		returncode="Dabs Exchange - ";
		break;			
	case "lt":
		returncode="Loot - ";
		break;			
	case "psc":
		returncode="PlaystationChips - ";
		break;									
	}
	return returncode;
}

//this function is built to avoid reference problems between frames, particularly on the clearly business site
function fnMainFrame()
{
	//if the top frame hasn't yet been set, then set it and return the result

	if(!top.aMainFrame) 
	{
		top.aMainFrame=top.main
	}
	return top.aMainFrame;
}


// ******* Here are all the functions related to processing and validating forms.

	

function fnSwapFields(field1,field2)
{
	temp = field1.value;
	field1.value = field2.value;
	field2.value =temp;
}

function fnVerifyField(field, alerttext)
{
	if(!fnCheckField(field))
	{	
		alert(alerttext);
		field.focus();
		return false;
	}

	return true;
}

function fnVerifyEmailField(field)
{
	string=field.value;
	if(string=="")
	{
		alert("Please enter your email address");
		field.focus();
		return false;
	}
	
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
    {
	 	alert("Please enter a valid email address");
		field.select();
	    return false;
	}
}

function fnCheckField(field) 
{
	string=field.value;
	
	if (!string) return false;

	for (var i = 0; i < string.length; i++) 
	{
		if (string.charAt(i) != " ")
			return true;
	}
	return false;
} 
     
function fnVerifyRdo(radiobutton, alerttext)
{                
	returncode=fnCheckRdo(radiobutton);
	
	if(returncode==-1)
	{
		alert(alerttext);
		radiobutton[0].focus();
		return false;
	} else {
		return true;
	}
		
}

function fnVerifyPostcodeField(field, alerttext)
{                
	if(!fnCheckPostcode(field.value))
	{
		alert(alerttext);
		field.focus();
		return false;
	} else {
		return true;
	}
		
}

function fnGetPostcodeZone(postcode)
{      
	//alert("Verify Postcode - "+postcode);          
	for(pcnum=0;pcnum<top.aPostcodes.length;pcnum++)
	{

		if(top.aPostcodes[pcnum].preletter==postcode.substr(0,top.aPostcodes[pcnum].preletter.length))	
		{
			if(top.aPostcodes[pcnum].postnum1!=-1)
			{	
				//alert(top.aPostcodes[pcnum].postnum1+" - "+top.aPostcodes[pcnum].postnum2);
				for(postnum=top.aPostcodes[pcnum].postnum1;postnum<=top.aPostcodes[pcnum].postnum2;postnum++)
				{
					//alert(top.aPostcodes[pcnum].preletter+postnum+" - "+postcode);
				
					if((postnum==parseInt(postcode.substr(top.aPostcodes[pcnum].preletter.length,1))) || (postnum==parseInt(postcode.substr(top.aPostcodes[pcnum].preletter.length,2))))
					{
						return top.aPostcodes[pcnum].zone;
					}
				}
			} else {

				return top.aPostcodes[pcnum].zone;
			}
		}
	}
	return 1;
		
}

function fnCheckRdo(radiobutton)
{         

	  
	returncode=-1;
	
	if(!radiobutton.length)
	{	
		if(radiobutton.checked) returncode = 0;
	} else {
	
		for(i=0;i<radiobutton.length;i++)
		{
			if(radiobutton[i].checked)
			{
				returncode=i;
			}
		}
	}	
	//alert(returncode);     
	return returncode;
		
}

function fnCheckPostcode(postcode) 
{
	postcode=postcode.toUpperCase();
    if (postcode.search(/^[A-Z]+[0-9]+[A-Z]*( )*[0-9]+[A-Z]+$/) != -1)
        return true;
    else
        return false;
}

function fnWriteField(docname,field)
{
	if(field!="")
	{
		docname.writeln(field+"<BR>");
	}
}

function fnReturnField(field, pad)
{
	returnstring="";
	if(field!="")
	{
		for(z=1;z<=pad;z++)
			returnstring+=" ";
		return (returnstring+field+"\n");
	} else {
	return "\n";
	}
}

	
function fnStripBR(string) {
// Replaces text with by in string
	return string.replace(/<BR>/gi,", ")
}
	
function fnBRtoCR(string) {
// Replaces text with by in string
	return string.replace(/<BR>/gi,"\n")
}

function fnCRtoBR(string) {
// Replaces text with by in string
	return string.replace(/\n/gi,"<BR>")
}

function fnCRLFtoBR(string) {
	returnstring=string;
	returnstring=returnstring.replace(/\n\r/gi,"<BR>");
	returnstring=returnstring.replace(/\r\n/gi,"<BR>");	
	returnstring=returnstring.replace(/\n/gi,"<BR>");	
	returnstring=returnstring.replace(/\r/gi,"<BR>");	
	return returnstring;
}


function fnEncodeSpaces(strSource)
{
	var nLength, nLooper, strDest, strTemp;
	
	strDest="";
	nLength=strSource.length;
	if(nLength ==0) return "";	
	for(nLooper=0;nLooper<nLength;nLooper++)
	{	
		strTemp=strSource.substring(nLooper, nLooper+1);
		if(strTemp == " ")
			strDest = strDest + "+";
		else
			strDest = strDest + strTemp;
	}
	return strDest;
}

// this function returns the number in words
function fnNumToWord(number)
{
	numnames = new Array();
	numnames[1]="one";
	numnames[2]="two";
	numnames[3]="three";
	numnames[4]="four";
	numnames[5]="five";
	numnames[6]="six";
	numnames[7]="seven";
	numnames[8]="eight";
	numnames[9]="nine";
	numnames[10]="ten";
	numnames[11]="eleven";
	numnames[12]="twelve";
	numnames[13]="thirteen";
	numnames[14]="fourteen";
	numnames[15]="fifteen";
	numnames[16]="sixteen";
	numnames[17]="seventeen";
	numnames[18]="eighteen";

	return(numnames[number]);
}	
function fnReturnDay(mydate)
{
	myday = mydate.getDay();
	days = new Array();
	days[0] = "Sun";
	days[1] = "Mon";	
	days[2] = "Tue";	
	days[3] = "Wed";	
	days[4] = "Thu";	
	days[5] = "Fri";	
	days[6] = "Sat";	
	
	return(days[myday]);
}	
function fnReturnMonth(mydate)
{
	mymonth = mydate.getMonth();
	months = new Array();
	months[0] = "Jan";
	months[1] = "Feb";	
	months[2] = "Mar";	
	months[3] = "Apr";	
	months[4] = "May";	
	months[5] = "Jun";	
	months[6] = "Jul";	
	months[7] = "Aug";		
	months[8] = "Sep";		
	months[9] = "Oct";		
	months[10] = "Nov";		
	months[11] = "Dec";		
	return(months[mymonth]);
}	

function fnDateFormat(mydate)
{
	datestring = fnReturnDay(mydate)+" "+mydate.getDate()+"/"+(mydate.getMonth()+1)+"/"+mydate.getYear();
	return datestring;
}

function fnCheckHoliday(mydate)
{
	returnboolean=false;
	for(chloop=0;chloop<aHolidays.length;chloop++)
	{
		if(fnDateFormat(aHolidays[chloop])==fnDateFormat(mydate))
		{
			returnboolean=true;
			//alert(fnDateFormat(aHolidays[chloop])+" "+fnDateFormat(mydate)+" check holiday - true!");
			//return returnstring;
		}
	}

	//alert(fnDateFormat(aHolidays[chloop])+" "+fnDateFormat(mydate)+" check holiday - true!");
	return returnboolean;
}




function fnSapGetIcon(icon)
{
	switch(icon)
	{
		case 'p':
		return '<img src="image/sap-parcel.gif" width=33 height=29 alt="" border="0">';
		case 'f':
		return '<img src="image/sap-fragile.gif" width=33 height=29 alt="" border="0">';
		case 'h':
		return '<img src="image/sap-heavy.gif" width=33 height=29 alt="" border="0">';
		case 'l':
		return '<img src="image/sap-loot.gif" width=34 height=30 alt="" border="0">';
	}
}


// used to calculate the delivery day, given the type of service and pick up day.
function fnSapSetDate(thissapservice)
{
	//if today is not a week day, go to the next weekday.
	
	while((thissapservice.pickupdate.getDay()==0)|(thissapservice.pickupdate.getDay()==6)|(fnCheckHoliday(thissapservice.pickupdate)))
	{
		thissapservice.pickupdate.setDate((thissapservice.pickupdate.getDate()+1));
	}

	thissapservice.deliverydate= new Date(thissapservice.pickupdate);

	// If it's a saturday delivery service, then find the next saturday, otherwise find the next weekday 
	// after the specified duration
	
	duration=thissapservice.duration; 
	if(thissapservice.duration==5) 
		{ duration=3; } 
		
	if(thissapservice.duration==6) 
		{ duration=5; } 

	
	if(thissapservice.duration==4)
	{
		while((thissapservice.deliverydate.getDay()!=6)|(fnCheckHoliday(thissapservice.deliverydate)))
		{
			thissapservice.deliverydate.setDate(thissapservice.deliverydate.getDate()+1);
		}
	} else 
	{
		for(x=0;x<duration;x++)
		{
			thissapservice.deliverydate.setDate((thissapservice.deliverydate.getDate()+1));
			
			while((thissapservice.deliverydate.getDay()==0)|(thissapservice.deliverydate.getDay()==6)|(fnCheckHoliday(thissapservice.deliverydate)))
			{
				thissapservice.deliverydate.setDate((thissapservice.deliverydate.getDate()+1));
			}
		}
	}

	
	return thissapservice;
}

// Adds open and close font tags for a given string
function fnAddFont(mystring,mycolour,mysize)
{
	if(mycolour==-1)
	{
		return "<font face='Arial, Helvetica, sans-serif' size='"+mysize+"'>"+mystring+"</FONT>";
	} else {
		return "<font color='"+mycolour+"' face='Arial, Helvetica, sans-serif' size='"+mysize+"'>"+mystring+"</FONT>";	
	}
}

function fnSapAddParcel()
{


	//top.aSapData[top.aSapNumber+1]= new top.sapdata();
	top.aSapData[top.aSapNumber+1] = new top.fnCloneObject(top.aSapData[top.aSapNumber]);
	top.aSapData[top.aSapNumber+1].sapservice =  new top.fnCloneObject(top.aSapData[top.aSapNumber].sapservice);
	top.aSapData[top.aSapNumber+1].pub=false;	
	//top.aSapData[top.aSapNumber+1].sapservice = new top.sapservice("","","","",top.aSapData[top.aSapNumber].sapservice.duration,0,0,0,top.aSapData[top.aSapNumber].sapservice.weight,"");
	top.aSapNumber++;
	
	//alert (top.aSapData[top.aSapNumber].pickup_day+" "+top.aSapData[top.aSapNumber].sapservice.duration+" "+top.aSapData[top.aSapNumber].sapservice.weight);
	
}

function fnSap1ClickOption(option)
{
	top.aSapData[top.aSapNumber].sapservice = new top.fnCloneObject(top.aSapResults[option]);
	//alert (top.aSapData[top.aSapNumber].pickup_day+" "+top.aSapData[top.aSapNumber].sapservice.duration+" "+top.aSapData[top.aSapNumber].sapservice.weight);
}

function fnSapGetWeight(weightband)
{
	switch(weightband)
	{
		case 1:
		return "10kg";
		case 2:
		return "30kg";
		case 3:
		return "60kg";
	}
}



function fnStripString(str, stripchar)
{
	returnstring="";
	for(i=0;i<str.length;i++)
	{
		if(str.substr(i,1)!=stripchar)
		{
			returnstring+=str.substr(i,1);
		}
	}
	return returnstring;
}



// pads out a string to the length specified with the specified string
// if the string is longer than the length required, it strips the leftmost characters.
function fnPadString(str, lngth, padchar)
{
	while(str.length < lngth)
	{
		str=padchar+str
	}
	while(str.length > lngth)
	{
		str=str.substring(1,str.length);
	}
	return str;
}

// ***** functions related to dates
// converts a date object to ddmmyy
function fnDateToString(dateobj)
{
	day=fnPadString(String(dateobj.getDate()),2,"0");
	month=fnPadString(String(dateobj.getMonth()+1),2,"0");
	year=fnPadString(String(dateobj.getYear()),2,"0");
	return (day+"."+month+"."+year);
}

// returns the inc vat value of a price given.
function fnAddVat(price)
{	
	incprice=(price*1.175);
	incprice=incprice-(incprice%1);
	return incprice;
}

// returns the inc vat value of a price given.
function fnCalculateVat(price)
{	
	vat=(price*0.175);
	vat=vat-(vat%1);
	return vat;
}

// converts a value in pence to a string: "£xx.xx"
function fnCurrency(number)
{	
	mystring = String(number);
	
	if(mystring.charAt(0)=="-")
	{
		minus=true;
		mystring=mystring.substring(1,mystring.length);
	} else
	{
		minus=false;
	}
	
	while (mystring.length<3)
	{
		mystring="0"+mystring;
	}
	
	n=mystring.length;
	
	left=mystring.substring(0,n-2);
	right=mystring.substring(n-2,n);
	
	if(!minus)
	{
		return "£"+left+"."+right;
	} else
	{
		return "-£"+left+"."+right;
	}	
}



// ***************************************************************
// ***************************************************************
// *********                                              ********
// *********                                              ********
// *********                SIMON'S CODE                  ********
// *********                                              ********
// *********                                              ********
// ***************************************************************
// ***************************************************************


//
// returns to the home page and resets the sService variable
/*function fnHome ()
{
	sService = "";
	top.fnMainFrame().location = "home.html";
}

// goes to the frequent delivery service section
function fnFds1 ()
{
	bRefresh = false;
	sService = "fds";
	top.fnMainFrame().location = "fds.html";
}
*/
// goes to the send a parcel section
/*function fnSend ()
{
	sService = "sap";
	top.fnMainFrame().location = "sap.html";
}*/
/*
// goes to the send a personal quotation section
function fnQuote ()
{
	sService = "pq";
	top.fnMainFrame().location = "quote.html";
}
*/

// opens one of two faq windows depending on whether you're in the send a parcel section or not.
function fnFAQ ()
{
	var sText = 'http:\/\/www.deliverynetworks.co.uk/html/faq.html';
	if (sService == "sap")
	{
		sText = 'http:\/\/www.deliverynetworks.co.uk/html/sapfaq.html'
	}
	fnOpenPreviewWindow(sText);
}

// opens one of two help windows depending on whether you're in the send a parcel section or not.
function fnHelp ()
{
	var sText = '../html/help.html';
	/*if (sService == "sap")
	{
		sText = '../html/saphelp.html';
	}*/
	fnOpenPreviewWindow(sText);
}




function fnQuoteInit ()
{
	//top.fnMainFrame().document.forms[0].txtCompany.value = aDetails[0];
	top.fnMainFrame().document.forms[0].txtContact.value = top.aUserData.contact_name;
	top.fnMainFrame().document.forms[0].txtTelephone.value = top.aUserData.contact_tel;
}
function fnQuoteValidate (frmQuote)
{
	//aDetails[0] = top.fnMainFrame().document.forms[0].txtCompany.value;
	//aDetails[6] = top.fnMainFrame().document.forms[0].txtContact.value;
	//aDetails[7] = top.fnMainFrame().document.forms[0].txtTelephone.value;
	//top.fnMainFrame().document.forms[0].redirect.value = top.fnCalculateUrl()+"html/thanks3.html";	
	//alert(top.fnMainFrame().document.forms[0].redirect.value);

	top.fnMainFrame().document.forms[0].subject.value = top.fnGetReferrer()+"Personal Quote";

	/*if (top.fnMainFrame().document.forms[0].txtCompany.value == "")
	{
		alert ("Please input your company name.");
	}*/
	if (top.fnMainFrame().document.forms[0].txtContact.value == "")
	{
		alert ("Please input a contact name.");
	}
	else if (top.fnMainFrame().document.forms[0].txtTelephone.value == "")
	{
		alert ("Please input your telephone number.");
	}
	else if (top.fnMainFrame().document.forms[0].txtPostcode.value == "")
	{
		alert ("Please input your postcode.");
	}
	else if (top.fnMainFrame().document.forms[0].txtEmail.value == "")
	{
		alert ("Please input your email address.");
	}	
	else if (top.fnMainFrame().document.forms[0].txtCarrier.value == "")
	{
		alert ("Please input your present carrier.");
	}
	else if (top.fnMainFrame().document.forms[0].txtService.value == "")
	{
		alert ("Please input your present service.");
	}
	else if (top.fnMainFrame().document.forms[0].txtPrice.value == "")
	{
		alert ("Please input your present price.");
	}
	else if (top.fnMainFrame().document.forms[0].txtVolume.value == "")
	{
		alert ("Please input your projected annual volume.");
	}
	else
	{
		return true;
	}
	return false;
}
/*
function fnContactInit (frmContact)
{
	frmContact.txtCompany.value = top.aSapData;
	frmContact.txtContact.value = aDetails[6];
	frmContact.txtTelephone.value = aDetails[7];
}
function fnContactDetails (frmContact)
{
	aDetails[0] = frmContact.txtCompany.value;
	aDetails[6] = frmContact.txtContact.value;
	aDetails[7] = frmContact.txtTelephone.value;
	return true;
}*/
function fnCallbackInit (frmCallback)
{
	frmCallback.txtCompany.value = aDetails[0];
	frmCallback.txtContact.value = aDetails[6];
	frmCallback.txtTelephone.value = aDetails[7];
}
function fnCallbackDetails (frmCallback)
{
	aDetails[0] = frmCallback.txtCompany.value;
	aDetails[6] = frmCallback.txtContact.value;
	aDetails[7] = frmCallback.txtTelephone.value;
	return true;
}
function _fnGetPartner (id)
{
	for (var i = 0; i < iNumPartners; i++)
	{
		if (aPartners[i][0].substring(0,2) == id.substring(0,2))
		{
			return (aPartners[i][1]);
		}
	}
	return ("");
}
function _fnGetService (id, field)
{
	var sText = "";
	for (var i = 0; i < iNumServices; i++)
	{
		if (aServices[i][0] == id)
		{
			if (field == "name")
			{
				sText = aServices[i][1];
			}
			else if (field == "ticks")
			{
				sText = aServices[i][2];
			}
			else if (field == "discount")
			{
				sText = aServices[i][3];
			}
			else if (field == "price")
			{
				sText = aServices[i][4];
			}
			else if (field == "ourprice")
			{
				sText = aServices[i][5];
			}
		}
	}
	return sText;
}
function fnPriceChart (sChart)
{
	fnOpenPreviewWindow("../pricecharts/"+sChart+".html");
}
function fnNoRefresh ()
{
	bRefresh = false;
}
function fnRefresh ()
{
	if (bRefresh == true)
	{
		//parent.location.href = "../welcome.html";
	}
	bRefresh = true;
}


function fnInsertImage()
{
var image;
//alert(top.aReferrer);
if(top.aReferrer == "ws")
	{
	var image = "<img src = '../webstreet/webstreetlogo.gif'>"
	}
else if(top.aReferrer == "as")
	{
	var image = "<img src = '../antiquestreet/aslogo.gif'>"
	}	
else
	{
	var image = "<img src = 'image/spacer.gif'>"
	}
return image
}

function fnCloneObject(what) {
    for (i in what) {
        this[i] = what[i];
    }
}

