function formatPounds(str)
{
	str = str.replace('*pound*',"£");
	return(str);
}

function ltrim(str)
{
	var trimlist = " \t\n\r";
	var t = str;
	
	// trim from start...
	while ((t.length > 0) && (trimlist.indexOf(t.charAt(0)) != -1))
	{
		t = t.substring(1);
	}
	
	return(t);
}

function rtrim(str)
{
	var trimlist = " \t\n\r";
	var t = str;
	
	// trim from end...
	while ((t.length > 0) && (trimlist.indexOf(t.charAt(t.length - 1)) != -1))
	{
		t = t.substring(0, t.length - 1);
	}
	
	return(t);
}

function trim(str)
{
	// trim both...
	return(ltrim(rtrim(str)));
}

function setAttributes(domObject, strAttributes)
{
	var arrTemp				= strAttributes.split(",");
	var intTotalAtt			= arrTemp.length;
	var arrAttribute		= new Array(intTotalAtt);
	var arrAttributeValue	= new Array(intTotalAtt);
	
	for(counter = 0; counter < intTotalAtt; counter++)
	{
		arrAttribute[counter]	   = arrTemp[counter].slice(0,arrTemp[counter].indexOf("@"));
		arrAttributeValue[counter] = arrTemp[counter].slice(arrTemp[counter].indexOf("@") + 1, arrTemp[counter].length);
		if(arrAttribute[counter].length > 0)
		{
			//fix for Firefox and class's
			if((arrAttribute[counter] == 'class') && (isIE == true))
				arrAttribute[counter] = 'className';
			domObject.setAttribute(arrAttribute[counter], arrAttributeValue[counter], false);
		}
	}
	return(domObject);
}
		
function writeCellContent(strData)
{
	//FUNCTION TO CREATE TEXT IN A CELL
	var cellContent = document.createTextNode(strData);
	return(cellContent);
}

function writeCellContentStyles(strData, strClass)
{
	//FUNCTION TO CREATE TEXT IN A CELL
	var strAttributes = 'class@' + strClass;
	
	var mySpan = insertHTMLElement('SPAN', strAttributes)
	var cellContent = document.createTextNode(strData);
	mySpan.appendChild(cellContent);
	
	return(mySpan);
}
	
function createImage(strImgSource, strID, strAttributes)
{
	//FUNCTION TO INSERT AN IMAGE INTO A CELL
	var newImage;
	var now = new Date();

	newImage = document.createElement("img");
	
	if(strImgSource != "")
		newImage.setAttribute("src", strImgSource, false);
	
	if(strID != "")
		newImage.setAttribute("id", strID);
	
	newImage = setAttributes(newImage, strAttributes);
	return(newImage);
}
	
function insertHTMLElement(strHTML, strAttributes)
{
	var newHTMLElement = document.createElement(strHTML);
	
	if(strAttributes != '')
		newHTMLElement = setAttributes(newHTMLElement, strAttributes);
	
	return newHTMLElement;
}

function insertCell(newRow, strText, strAttributes)
{
	var newCol = document.createElement("TD");
	newCol = setAttributes(newCol, strAttributes);
	newCol.appendChild(writeCellContent(strText));
	
	newRow.appendChild(newCol);
	return(newRow);
}
	
function insertCell1(newRow, NETWORK_IMAGE, NETWORK_IMAGE_ALT, TARIFF_NAME, CONTRACT_LENGTH, strAttributes)
{
	//insert 'Your Contract' cell
	
	var newCol = document.createElement("TD");
	
	
	newCol = setAttributes(newCol, strAttributes);
	newCol.appendChild(createImage(NETWORK_IMAGE, '', 'border@0,alt@' + NETWORK_IMAGE_ALT));
	newCol.appendChild(insertHTMLElement("BR", ''));
	newCol.appendChild(writeCellContentStyles(TARIFF_NAME, "bold"));
	newCol.appendChild(insertHTMLElement("BR", ''));
	newCol.appendChild(writeCellContent(CONTRACT_LENGTH));
	
	newRow.appendChild(newCol);
	return(newRow);
}

function insertCell2(newRow, HANDSET_IMAGE, HANDSET_IMAGE_ALT, HANDSET_MANUFACTURER, HANDSET_NAME, strAttributes)
{
	//insert 'Your Mobile Phone' cell
	
	var newCol = document.createElement("TD");
	newCol = setAttributes(newCol, strAttributes);
	newCol.appendChild(createImage(HANDSET_IMAGE, '', 'border@0,alt@' + HANDSET_IMAGE_ALT));
	newCol.appendChild(insertHTMLElement("BR", ''));
	newCol.appendChild(writeCellContentStyles(HANDSET_MANUFACTURER, "bold"));
	newCol.appendChild(insertHTMLElement("BR", ''));
	newCol.appendChild(writeCellContent(HANDSET_NAME));
	
	newRow.appendChild(newCol);
	return(newRow);
}
	
function insertCell3(newRow, data, strAttributes)
{
	var array  = data.split(" ");
	var frag   = "";
	var newCol = document.createElement("TD");
	newCol = setAttributes(newCol, strAttributes);
	for (i = 0; i < array.length; i++)
	{
		frag = array[i];
		if (frag.length > 0)
		{
			if ("0123456789£$".indexOf(frag.charAt(0)) != -1)
				newCol.appendChild(writeCellContentStyles(frag, "DisplayMinutes"));
			else
				newCol.appendChild(writeCellContent(frag));
			if (i != (array.length - 1))
				newCol.appendChild(writeCellContent(" "));
		}
	}
	newRow.appendChild(newCol);
	return(newRow);
}
	
function insertCell4(newRow, data, strAttributes)
{
	var array  = data.split(" ");
	var frag   = "";
	var newCol = document.createElement("TD");
	newCol = setAttributes(newCol, strAttributes);
	for (i = 0; i < array.length; i++)
	{
		frag = array[i];
		if (frag.length > 0)
		{
			if ("0123456789£$".indexOf(frag.charAt(0)) != -1)
				newCol.appendChild(writeCellContentStyles(frag, "DisplayMinutes"));
			else
				newCol.appendChild(writeCellContent(frag));
			if (i != (array.length - 1))
				newCol.appendChild(writeCellContent(" "));
		}
	}
	newRow.appendChild(newCol);
	return(newRow);
}
	
function insertCell5(newRow, MONTHLY_COST_A, MONTHLY_COST_B, STANDARD_LINE_RENTAL, strAttributes)
{
	//insert 'Monthly Cost' cell
	
	var newCol = document.createElement("TD");
	newCol = setAttributes(newCol, strAttributes);
	newCol.appendChild(writeCellContentStyles(MONTHLY_COST_A, "DisplayMonthlyCost"));
	newCol.appendChild(writeCellContent(' ' + MONTHLY_COST_B + ', '));
	//newCol.appendChild(insertHTMLElement("BR", ''));
	newCol.appendChild(writeCellContent(STANDARD_LINE_RENTAL));
	
	newRow.appendChild(newCol);
	return(newRow);
}
	
function insertCell6(newRow, DISPLAY_DEAL, DISPLAY_DEAL_TERMS, strAttributes)
{
	//insert 'Cash Back' cell
	
	var newCol = document.createElement("TD");
	newCol = setAttributes(newCol, strAttributes);
	newCol.appendChild(writeCellContentStyles(DISPLAY_DEAL, "DisplayDeal"));
	newCol.appendChild(insertHTMLElement("BR", ''));
	newCol.appendChild(writeCellContent(DISPLAY_DEAL_TERMS));
	
	newRow.appendChild(newCol);
	return(newRow);
}

/*
function insertCell7(newRow, APPLY_URL, strAttributes)
{
	//insert 'Buy Online' cell
	
	var newCol = document.createElement("TD");
	var image  = "";
	
	newCol	= setAttributes(newCol, strAttributes);
	newLink = insertHTMLElement('A', 'href@' + APPLY_URL)

	image	= createImage(imagePath + 'buy.gif', '', 'border@0,alt@Buy Now Online');
	
	newLink.appendChild(image);
	newCol.appendChild(newLink);
	newRow.appendChild(newCol);
	return(newRow);
}
*/


function insertCell7(newRow, APPLY_URL, RETAILER_NAME, RETAILER_IMAGE, strAttributes)
{
	//insert 'Buy Online' cell
	
	var newCol  = document.createElement('TD');
	var content = "";
	var alt     = "Buy now";
	
	//alert(APPLY_URL);
	
	if (RETAILER_NAME.length > 0)
		alt += " from " + RETAILER_NAME;
	
	newCol	= setAttributes(newCol, strAttributes);
	
	// create the link element...
	newLink = insertHTMLElement('A', 'href@' + APPLY_URL)
	//alert(newLink);
	
	// create the BUY image and add it to the link...
	content	= createImage(imagePath + 'buy.gif', '', 'border@0,alt@' + alt);	
	newLink.appendChild(content);
	
	// if we've been supplied retailer details...
	if (RETAILER_IMAGE.length > 0)
	{
		// create the formatting and retailer image and add it to the link...
		newLink.appendChild(document.createElement('BR'));
		newLink.appendChild(document.createElement('BR'));
		content	= createImage(defaultImagePath + 'images/retailers/' + RETAILER_IMAGE, '', 'border@0,alt@' + alt);	
		newLink.appendChild(content);
	}
	
	// populate...
	newCol.appendChild(newLink);
	newRow.appendChild(newCol);
	return(newRow);
}

function insertNewRow(myTable, myResultsArray)
{
	var NETWORK_IMAGE			= myResultsArray[0];
	var NETWORK_IMAGE_ALT		= myResultsArray[1];
	var TARIFF_NAME				= myResultsArray[2];
	var CONTRACT_LENGTH			= myResultsArray[3];
	var HANDSET_IMAGE			= myResultsArray[4];
	var HANDSET_IMAGE_ALT		= myResultsArray[5];
	var HANDSET_MANUFACTURER	= myResultsArray[6];
	var HANDSET_NAME			= myResultsArray[7];
	var DISPLAY_MIN_A			= myResultsArray[8];
	var DISPLAY_MIN_B			= myResultsArray[9];
	var DISPLAY_SMS				= myResultsArray[10];
	var MONTHLY_COST_A			= myResultsArray[11];
	var MONTHLY_COST_B			= myResultsArray[12];
	var STANDARD_LINE_RENTAL	= myResultsArray[13];
	var DISPLAY_DEAL			= myResultsArray[14];
	var DISPLAY_DEAL_TERMS		= myResultsArray[15];
	var APPLY_URL				= myResultsArray[16];
	var RETAILER_ID				= "";
	var RETAILER_NAME			= "";
	var RETAILER_IMAGE			= "";
	
	if (myResultsArray.length > 16)	RETAILER_ID		= myResultsArray[17];
	if (myResultsArray.length > 17)	RETAILER_NAME	= myResultsArray[18];
	if (myResultsArray.length > 18)	RETAILER_IMAGE	= myResultsArray[19];
	
	TARIFF_NAME				= formatPounds(TARIFF_NAME);
	MONTHLY_COST_A			= formatPounds(MONTHLY_COST_A);
	STANDARD_LINE_RENTAL	= formatPounds(STANDARD_LINE_RENTAL);
	DISPLAY_MIN_A			= formatPounds(DISPLAY_MIN_A);
	DISPLAY_MIN_B			= formatPounds(DISPLAY_MIN_B);
	DISPLAY_DEAL			= formatPounds(DISPLAY_DEAL);
	DISPLAY_DEAL_TERMS		= formatPounds(DISPLAY_DEAL_TERMS);
	HANDSET_NAME			= trim(HANDSET_NAME.replace(HANDSET_MANUFACTURER, ""));
	HANDSET_NAME			= HANDSET_NAME.replace(HANDSET_MANUFACTURER, "");
	
	var myTableBody		= document.createElement("TBODY");
	newRow				= document.createElement("TR");
	newRow				= insertCell1(newRow, NETWORK_IMAGE, NETWORK_IMAGE_ALT, TARIFF_NAME, CONTRACT_LENGTH, 'class@pc15');		//Your Contract
	newRow				= insertCell2(newRow, HANDSET_IMAGE, HANDSET_IMAGE_ALT, HANDSET_MANUFACTURER, HANDSET_NAME, "class@pc15");	//Your Mobile Phone
	newRow				= insertCell3(newRow, DISPLAY_MIN_B, "class@pc12");															//Minutes
	newRow				= insertCell4(newRow, DISPLAY_SMS, "class@pc10 centred");													//Text Messages
	newRow				= insertCell5(newRow, MONTHLY_COST_A, MONTHLY_COST_B, STANDARD_LINE_RENTAL, "class@pc13 yellowhighlight");	//Monthly Cost
	newRow				= insertCell6(newRow, DISPLAY_DEAL, DISPLAY_DEAL_TERMS, "class@pc24");										//Cashback
//	newRow				= insertCell7(newRow, APPLY_URL, "class@pc11 resultsborderleft centred");									//Buy Online
	newRow				= insertCell7(newRow, APPLY_URL, RETAILER_NAME, RETAILER_IMAGE, "class@pc11 resultsborderleft centred");	//Buy Online
	
	//append new cols to row
	myTableBody.appendChild(newRow);
	myTable.appendChild(myTableBody);
}
	
	
function generateResults(ResultsXML)
{
	var myTable = document.all ? document.all["resultstable"] : document.getElementById("resultstable");
	
	xmlDoc = loadXML(ResultsXML);

	// documentElement always represents the root node
	var myDoc = xmlDoc.documentElement;
	var ResultCount = myDoc.getElementsByTagName("DEAL").length;

	var NETWORK_IMAGE			= "";
	var NETWORK_IMAGE_ALT		= "";
	var TARIFF_NAME				= "";
	var CONTRACT_LENGTH			= "";
	var HANDSET_IMAGE			= "";
	var HANDSET_IMAGE_ALT		= "";
	var HANDSET_MANUFACTURER	= "";
	var HANDSET_NAME			= "";
	var HANDSET_COST			= "";
	var DISPLAY_MIN_A			= "";
	var DISPLAY_MIN_B			= "";
	var DISPLAY_SMS				= "";
	var MONTHLY_COST_A			= "";
	var MONTHLY_COST_B			= "";
	var MONTHLY_COST_C			= "";
	var STANDARD_LINE_RENTAL	= "";
	var DISPLAY_DEAL			= "";
	var DISPLAY_DEAL_TERMS		= "";
	var APPLY_URL				= "";
	var RETAILER_ID				= "";
	var RETAILER_NAME			= "";
	var RETAILER_IMAGE			= "";
	var myResultsArray			= null;
	var val                     = 0;
	var cashback                = "";

	for(a=0; a<ResultCount; a++)
	{

		NETWORK_IMAGE			= myDoc.getElementsByTagName("NETWORK_IMAGE")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		NETWORK_IMAGE_ALT		= myDoc.getElementsByTagName("NETWORK_IMAGE")[a].getElementsByTagName("ALT_A")[0].childNodes[0].nodeValue;
		TARIFF_NAME				= myDoc.getElementsByTagName("TARIFF_NAME")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		CONTRACT_LENGTH			= myDoc.getElementsByTagName("CONTRACT_LENGTH")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		CONTRACT_LENGTH_VAL		= myDoc.getElementsByTagName("CONTRACT_LENGTH")[a].getElementsByTagName("VALUE_B")[0].childNodes[0].nodeValue;
		HANDSET_IMAGE			= myDoc.getElementsByTagName("HANDSET_IMAGE")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		HANDSET_IMAGE_ALT		= myDoc.getElementsByTagName("HANDSET_IMAGE")[a].getElementsByTagName("ALT_A")[0].childNodes[0].nodeValue;
		HANDSET_MANUFACTURER	= myDoc.getElementsByTagName("HANDSET_MANUFACTURER")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		HANDSET_NAME			= myDoc.getElementsByTagName("HANDSET_NAME")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		HANDSET_COST			= myDoc.getElementsByTagName("HANDSET_COST")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		DISPLAY_MIN_A			= myDoc.getElementsByTagName("DISPLAY_MIN")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		DISPLAY_MIN_B			= myDoc.getElementsByTagName("DISPLAY_MIN")[a].getElementsByTagName("VALUE_B")[0].childNodes[0].nodeValue;
		DISPLAY_SMS				= myDoc.getElementsByTagName("DISPLAY_SMS")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		MONTHLY_COST_A			= myDoc.getElementsByTagName("MONTHLY_COST")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		MONTHLY_COST_B			= myDoc.getElementsByTagName("MONTHLY_COST")[a].getElementsByTagName("VALUE_B")[0].childNodes[0].nodeValue;
		MONTHLY_COST_C			= myDoc.getElementsByTagName("MONTHLY_COST")[a].getElementsByTagName("VALUE_C")[0].childNodes[0].nodeValue;
		STANDARD_LINE_RENTAL	= myDoc.getElementsByTagName("STANDARD_LINE_RENTAL")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
		RETAILER_ID				= myDoc.getElementsByTagName("RETAILER")[a].getElementsByTagName("ID")[0].childNodes[0].nodeValue;
		RETAILER_NAME			= myDoc.getElementsByTagName("RETAILER")[a].getElementsByTagName("NAME")[0].childNodes[0].nodeValue;
		RETAILER_IMAGE			= myDoc.getElementsByTagName("RETAILER")[a].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue;
		APPLY_URL				= myDoc.getElementsByTagName("APPLY_URL")[a].childNodes[0].nodeValue;

		if (HANDSET_COST == "0.00")
			HANDSET_COST = "FREE";
			
		STANDARD_LINE_RENTAL   += ". Handset is £" + HANDSET_COST;

		try {
			val = CONTRACT_LENGTH_VAL * MONTHLY_COST_C;
			if (isNaN(val))	val = 0;
		}
		catch(e) {val = 0;}
		
		if (val < 0.01)		val = "FREE";
		
		if (val != "FREE")	val = val.toFixed(2);

		try
		{
			DISPLAY_DEAL		= myDoc.getElementsByTagName("DISPLAY_DEAL")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
			DISPLAY_DEAL_TERMS	= myDoc.getElementsByTagName("DISPLAY_DEAL_TERMS")[a].getElementsByTagName("VALUE_A")[0].childNodes[0].nodeValue;
			DISPLAY_DEAL_TERMS += " Total cost of the contract after cashback: £" + val;
		}
		catch(e)
		{
			MONTHLY_COST_B		= "including handset";
			DISPLAY_DEAL		= "";
			DISPLAY_DEAL_TERMS	= "Total cost of the contract: £" + val;
		}
		
		if (DISPLAY_DEAL.length > 0) 
		{
	//		cashback  = Replace(LCase(DISPLAY_DEAL), "£", "")
			cashback = DISPLAY_DEAL.toLowerCase();
			cashback = cashback.replace("£", "");		
			
	//		If InStr(1, CASHBACK, "cashback") Then
	//			i = InStr(1, CASHBACK, " ")
	//			If i > 0 Then CASHBACK = Left(CASHBACK, i - 1)
	//		Else
	//			CASHBACK = "uncalculated"
	//		End If
	//		APPLY_URL = Replace(APPLY_URL, "applynow.asp?", "applynow.asp?cashback=" & CASHBACK & "&")
			if (cashback.indexOf("cashback") != -1)
			{
				var i = cashback.indexOf(" ");
				if (i != -1)
					cashback = cashback.substr(0, i);
			}
			else
				cashback = "uncalculated";
			APPLY_URL = APPLY_URL.replace("applynow.asp?", "applynow.asp?cashback=" + cashback + "&");
		}

		myResultsArray = new Array(	NETWORK_IMAGE,NETWORK_IMAGE_ALT,TARIFF_NAME,CONTRACT_LENGTH,HANDSET_IMAGE,
									HANDSET_IMAGE_ALT,HANDSET_MANUFACTURER,HANDSET_NAME,DISPLAY_MIN_A,DISPLAY_MIN_B,
									DISPLAY_SMS,MONTHLY_COST_A,MONTHLY_COST_B,STANDARD_LINE_RENTAL,DISPLAY_DEAL,
									DISPLAY_DEAL_TERMS,APPLY_URL,RETAILER_ID,RETAILER_NAME,RETAILER_IMAGE
								);

		insertNewRow(myTable, myResultsArray);	
	}
}
	
function loadXML(XML)
{
	if (window.ActiveXObject)
	{
		var doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = "false";
		doc.loadXML(XML);
	}
	else
	{
		// code for Mozilla, Firefox, Opera, etc.
		var parser = new DOMParser();
		var doc = parser.parseFromString(XML,"text/xml");
	}
	return(doc);
}
