var G_phonenumlength = 11;
var G_combolen = 0;
var G_combotext = '';
var G_lastid = 0;
var G_FirstTime=1;
var G_FirstCart=1;
var G_lastcall='nothing yet';
var G_maxshow=26;
var G_ResultsPerPage=25;
var G_LoginOffered=false;
var G_numidlist='';
var G_lastargs='';

if(!window.G_imgdir) {var G_imgdir='';}
if(!window.G_np_directtocart) {var G_np_directtocart=false;}	//when click Add button, jump direct to cart (true) or stay on page (false)?

function CheckNumber(fromstring,sample) {
	var intReqLen=G_phonenumlength;	//Standard length of a phone number, including full prefix.
	var thisstring=document.getElementById('number').value;
	intLen = thisstring.length + G_combolen;
	document.getElementById('number').value=AllowedInput(thisstring);

	var temp=GetSelectedBands();
	if(G_lastcall==temp+" / "+G_combotext+thisstring) { return(false); }	//we already checked this, so don't bother to recheck it (especially since it might double-up the api calls)
	G_lastcall=temp+" / "+G_combotext+thisstring;

	var numtype=document.getElementById('id_0').value.split("/");

	var numtype2=new Array();
	if(document.getElementById('id_'+numtype[0])) { numtype2=document.getElementById('id_'+numtype[0]).value.split("/"); }

	if(!checkallowedtopurchase()) { return; }

	if (thisstring.length==0 && fromstring!="KeyUp") {
		document.getElementById('shortinfo').innerHTML = GetNumberTypeInfo(numtype[1]);
		ToggleRestrictionCombo(numtype[1]);
		document.getElementById('footerinfo').innerHTML = GetNumberExtraInfoText(numtype[1]);
		DisplayInfoHdr(G_combotext,0,sample);
	} else if (intLen==intReqLen) {
		ValidateNumber();
	} else if (intLen<intReqLen) {
		ValidateNumber();
	} else {
		document.getElementById('fullresults').innerHTML='<br />';
	}
	if(footertext) {
		if(footertext[numtype2[0]]) {
			document.getElementById('footerinfo').innerHTML=footertext[numtype2[0]];
		}else if(footertext[numtype[0]]) {
			document.getElementById('footerinfo').innerHTML=footertext[numtype[0]];
		}
	}
}

function ToggleRestrictionCombo(prefix) {
// ShowDebug("ToggleRestrictionCombo: "+prefix+"<br />");
	if(prefix=='09') {
		document.getElementById('promographic').style.visibility='hidden';	//we have to sacrifice the promo graphic to make room for the 09 restriction types combo.
		document.getElementById('promographic').style.display='none';
		document.getElementById('restriction09').style.visibility='visible';
		document.getElementById('restriction09').style.display='block';
	} else {
		document.getElementById('promographic').style.visibility='visible';	//if not 09, then we can have the promo graphic, as we don't need the restiction combo.
		document.getElementById('promographic').style.display='block';
		document.getElementById('restriction09').style.visibility='visible';
		document.getElementById('restriction09').style.display='none';
		document.getElementById('restrictioncombo').value='';
	}
}

function GetNumberTypeInfo(prefix) {
	var infostr1="Want more information? ";
	var infostr2="Click here";
	switch (prefix) {
		case "0800" : return "0800: Freephone numbers."+    "<br />"+infostr1+"<a href='"+G_pagename['numinfo_freefone']+"'>"+infostr2+"</a>";
		case "0808" : return "0808: Freephone numbers."+    "<br />"+infostr1+"<a href='"+G_pagename['numinfo_freefone']+"'>"+infostr2+"</a>";
		case "070"  : return "070:  Personal numbers."+     "<br />"+infostr1+"<a href='"+G_pagename['numinfo_personal']+"'>"+infostr2+"</a>";
		case "0843" :
		case "0844" : return "0844: Fixed-rate numbers."+   "<br />*No Monthly Service Fee<br />"+infostr1+"<a href='"+G_pagename['numinfo_fixed0844']+"'>"+infostr2+"</a>";
		case "0845" : return "0845: Local-rate numbers."+   "<br />*No Monthly Service Fee<br />*No call costs to you<br />"+infostr1+"<a href='"+G_pagename['numinfo_locall']+"'>"+infostr2+"</a>";
		case "0870" : return "0870: National-rate numbers."+"<br />Cost to caller is the same (or less) than calling standard 01 and 02 ranges.<br />"+infostr1+"<a href='"+G_pagename['numinfo_national']+"'>"+infostr2+"</a>";
		case "0871" : return "0871: Fixed-rate numbers."+   "<br />*No Monthly Service Fee<br />*Earn up to 4.5ppm peak revenue<br />"+infostr1+"<a href='"+G_pagename['numinfo_fixed0871']+"'>"+infostr2+"</a>";
		case "09"   : return "09:   Premium-rate numbers."+ "<br />*No Monthly Service Fee<br />"+infostr1+"<a href='"+G_pagename['numinfo_premium']+"'>"+infostr2+"</a>";
		case "056"  : return "056:  VoIP numbers."+         "<br />"+infostr1+"<a href='"+G_pagename['numinfo_056']+"'>"+infostr2+"</a>";
		case "03"   : return "03:   UK-wide numbers."+      "<br />Cost to caller is the same (or less) than calling standard 01 and 02 ranges.<br />"+infostr1+"<a href='"+G_pagename['numinfo_ukwide']+"'>"+infostr2+"</a>";
		case "01"   :
		case "02"   : return "01/02: Local area numbers."+  "<br />Standard UK local number ranges"+infostr1+"<a href='"+G_pagename['numinfo_geog']+"'>"+infostr2+"</a>";
		default     : return "Select a number type to see some samples.";
	}
}

function GetNumberExtraInfoText(prefix) {
	//nb: this is temporary -- these texts need to be made configurable.
	var infotext="";
	switch (prefix) {
		case "0800" : infotext = ""; break;
		case "0808" : infotext = ""; break;
		case "070"  : infotext = ""; break;
		case "0843" :
		case "0844" : infotext = "If you anticipate large call volumes, we may be able to offer you improved call<br />"
					+"revenue. Please contact us at <a href='mailto:sales@skycomuk.com'>sales@skycomuk.com</a> for more information.";
					break;
		case "0845" : infotext = ""; break;
		case "0870" : infotext = "Revenue displayed depends on monthly call volumes, breakdown below:<br /><br />"
					+"Standard Numbers (with no setup fee) generate no revenue for the first 250 minutes.<br />"
					+"All other customers get 2.5ppm for the first 9,999 peak minutes<br />"
					+"3ppm up to 99,999<br />"
					+"3.5ppm up to 199,999<br />"
					+"4ppm from 200,000 on.<br />"
					+"All off-peak revenue is paid at 0.5ppm."
					break;
		case "0871" : infotext = "If you anticipate large call volumes, we may be able to offer you improved call<br />"
					+"revenue. Please contact us at <a href='mailto:sales@skycomuk.com'>sales@skycomuk.com</a> for more information.";
					break;
// 		case "0871" : infotext = "Revenue displayed depends on monthly call volumes, breakdown below:<br /><br />"
// 					+"Standard Numbers (with no setup fee) generate no revenue for the first 250 minutes.<br />"
// 					+"All other customers get 3ppm for the first 9,999 peak minutes<br />"
// 					+"3.5ppm up to 99,999<br />"
// 					+"4ppm up to 199,999<br />"
// 					+"4.5ppm from 200,000 on.<br /><br />"
// 					+"For off-peak and weekend calculations, add 1ppm to the figures shown above."
// 					break;
		case "09"   : infotext = "NB - Please visit the PhonePayPlus <a href='http://www.phonepayplus.org.uk/' target='_blank'>website</a> for information on any certification requirements that may govern "
					+"your use of a premium rate telephone number.";
					break;
		case "056"  : infotext = ""; break;
		case "03"   : infotext = ""; break;
		case "01"   :
		case "02"   : infotext = ""; break;
		default     : infotext = ""; break;
	}
	document.getElementById('promographic').innerHTML="&nbsp;";
	return infotext;
}

function ValidateNumber() {
	var fullnumber = TranslateNumber(G_combotext+document.getElementById('number').value);
	var pack="";
	pack=GetSelectedBands();
	if(pack.length>0) {
		pack="&package="+pack;
	}
	if(G_combotext.length>0) {
		if(fullnumber.length < G_phonenumlength) {
			apilite_call('partinfo.php?picked_id='+G_lastid+pack+"&chosen="+TranslateNumber(document.getElementById('number').value), 'ShowPartInfo', fullnumber+",all", 'ShowPartFullError', fullnumber);
		} else {
			apilite_call('fullinfo.php?number='+fullnumber+pack, 'ShowFullInfo', fullnumber, 'ShowPartFullError', fullnumber);
		}
	} else {
		document.getElementById('shortinfo').innerHTML = "Please select a number range from the list.";
	}
	document.getElementById('promographic').innerHTML="&nbsp;";
}

function ShowSome(startat,limit,max) {
	if(!checkallowedtopurchase()) { return; }

	var fullnumber = TranslateNumber(G_combotext+document.getElementById('number').value);
	var pack="";
	if(G_combotext.length>0) {
		pack=GetSelectedBands();
		if(pack.length>0) { pack="&package="+pack;}
		apilite_call('fullinfo.php?number='+fullnumber+pack+'&startat='+startat+'&limit='+limit, 'DisplayFullInfo', fullnumber+","+startat+","+limit+","+max, 'ShowPartFullError', fullnumber);
	}
}

function ReloadCurrentNums() {
	if(!checkallowedtopurchase()) { return; }

	//call fullinfo for the same list of numbers as we're currently displaying. This is used if other circumstances change that might affect the details, such as the user logging in and wanting to see his discounts.
	var fullnumber = TranslateNumber(G_combotext+document.getElementById('number').value);
	var argv=G_lastargs.split(",");
	var order='&order=';
	if(parseInt(argv[4])>0) { order+="band"; } else {order+="id"; }
	apilite_call('fullinfo.php?number='+fullnumber+'&idlist='+G_numidlist+order, 'DisplayFullInfo', G_lastargs, 'ShowPartFullError', fullnumber);
}

function TranslateNumber(number) {
	//translate alpha characters to numerics so we can search for the number...
	var output="";
	var thiscar="";
	var translate=new Object;
	translate={A:2,B:2,C:2,D:3,E:3,F:3,G:4,H:4,I:4,J:5,K:5,L:5,M:6,N:6,O:6,P:7,Q:7,R:7,S:7,T:8,U:8,V:8,W:9,X:9,Y:9,Z:9};

	for(var count=0;count<number.length;count++) {
		thischar=number.substring(count,count+1);
		if(thischar>='A' && thischar<='Z') {
			output+=translate[thischar];
		} else if(thischar>='0' && thischar<='9') {
			output+=thischar;
		}
	}
	return output;
}

function AllowedInput(number) {
	//strips illegal chars from the string.
	var output="";
	var thiscar="";
	number=number.toUpperCase()
	for(var count=0;(count<number.length);count++) {
		thischar=number.substring(count,count+1);
		if((thischar>='A' && thischar<='Z') || (thischar>='0' && thischar<='9')) {
			output+=thischar;
		}
	}
	return output;
}

function ShowPartInfo(responsestring,args) {
	if(!checkallowedtopurchase()) { return; }

	var argv=args.split(",");
	var buildstring = '';
	var result=responsestring.split("|");
	var fullnumber = TranslateNumber(G_combotext+document.getElementById('number').value);

	DisplayInfoHdr(argv[0],result[1],0);

	if(G_combotext.length>0) {
		if(result[1]>0 && result[1]<=G_maxshow) {
			var pack="";
			pack=GetSelectedBands();
			if(pack.length>0) { pack="&package="+pack;}
			apilite_call('fullinfo.php?number='+fullnumber+pack, 'DisplayFullInfo', fullnumber, 'ShowPartFullError', fullnumber);
		}
	}
}

function ShowFullInfo(responsestring,args) {
	if(!checkallowedtopurchase()) { return; }

	var argv=args.split(",");
	var buildstring = '';

	DisplayInfoHdr(argv[0],1,0);
	DisplayFullInfo(responsestring,args);
}

function DisplayInfoHdr(number,numresults,sample) {
	var buildstring = '';
	var numtypestr = '';

	var shownum=number;
	while (shownum.length<G_phonenumlength) {
		shownum+="x";
	}

	buildstring='<table border="0"><tr><td align="center">';
	if(number.substring(0,2)=='09') {
		buildstring+='<p>';
		switch(document.getElementById('restrictioncombo').value) {
			case '' :        numtypestr=''; buildstring+='Since many 09 numbers have restricted usage regulations, you may want to filter the list with the "usage" option, to ensure you pick a number which matches your intended usage.'; break;
			case 'limited' : numtypestr=' capped'; buildstring+='Capped ranges have a fixed limit on either the duration or the cost of calls.'; break;
			case 'open' :    numtypestr=' uncapped'; buildstring+='Uncapped ranges do not have limits on call duration or cost.'; break;
			case 'adult' :   numtypestr=' adult usage'; buildstring+='Adult usage ranges are specifically intended for adult entertainment lines.'; break;
		}
		buildstring+='<br /><br />Note: With all orders for premium-rate numbers we require you to provide additional information relating to your company and the service you will be offering. We will send you an email requesting this when you place your order.<br /><br />';
		buildstring+='Please see the <a href="premium_rate_tel_numbers.php">09 info page</a> for more detail.';
		buildstring+='</p>';
	}
	if(sample) {
		buildstring+='<h2 class="num_avail_info">Sample'+numtypestr+' numbers beginning with '+number+'</h2>';
	} else {
		buildstring+='<h2 class="num_avail_info">Availability for number '+shownum+'</h2>';
	}
	buildstring+='</td></tr><tr><td id="numnums" align="center">';


	buildstring+='</tr></tr><tr><td>';
	buildstring+='<table border="0" align="center"><tr><td>';
	buildstring+="<span name='numberlist' id='numberlist'></span>";
	buildstring+='</td></tr></table>';
	buildstring+='</td></tr></table>';

	document.getElementById('fullresults').innerHTML=buildstring;
	ShowNumNums(numresults);
}

function ShowNumNums(numresults) {
	var buildstring='';
	switch(true) {
	case (numresults==0):
		if(G_combolen<6) {
			buildstring+="";
		} else {
			if(GetSelectedBands()=='') {
				buildstring+='(No numbers found)<br />\n';
			} else {
				buildstring+='No matching numbers found were found in the pricing band specified. Try selecting "All" pricing bands.<br />\n';
			}
		}
		break;
	case (numresults==1):
		buildstring+="(1 number found)<br />\n";
		break;
	case (numresults<=G_maxshow):		//javascript switch doesn't normally support range values, so we're having to do switch(true), and put the full condition in each case block.
		buildstring+="("+numresults+" numbers found)<br />\n";
		break;
	default:
		buildstring+="("+numresults+" numbers found - <a href='javascript:ShowSome(1,G_ResultsPerPage,"+numresults+")' class='list_some_nums'>Click here</a> to list some of them)<br />\n";
		break;
	}
	document.getElementById('numnums').innerHTML=buildstring;
}

function GetNumNums() {
	if(!checkallowedtopurchase()) { return; }

	//Similar to ValidateNumber, but called when the final part of the number has not been entered.
	var fullnumber = G_combotext;
	var pack="";
	if(G_combotext.length>0) {
		pack=GetSelectedBands();
		if(pack.length>0) { pack="&package="+pack;}

		apilite_call('partinfo.php?picked_id='+G_lastid+pack+"&chosen="+TranslateNumber(document.getElementById('number').value), 'ShowPartInfo', fullnumber+",numnums", 'ShowPartFullError', fullnumber);
	}
}

function DisplayFullInfo(responsestring,args) {
	if(!checkallowedtopurchase()) { return; }

	var buildstring_a='';
	var buildstring_b='';
	var tmp=responsestring.split("\n");	//split the returned lines into an array.
	var argv=args.split(",");
	var band='';
	var altcol=false;
	var anypercall=false;
	var anypermin=false;
	var anyownrcost=false;
	var anyownrrevenue=false;
	var anysetup=false;

	var cols=SetCols(argv[0]);
	G_numidlist='';
	G_lastargs=args;
	for(count=0;count<tmp.length-1;count++) {
		var rowobj=GetRowObj(tmp);
		if(rowobj.chargemeth=="per_call") {anypercall=true;} else {anypermin=true;}
		if(rowobj.ownrcostpeak>0) {anyownrcost=true;}
		if(rowobj.ownrcostpeak<0) {anyownrrevenue=true;}
		if(rowobj.setupfee!=0 || rowobj.band>=5) {anysetup=true;}
		buildstring_b+=ShowThisRow(cols,rowobj,count);
		G_numidlist+=(G_numidlist.length>0?",":"")+rowobj.id;
	}

	buildstring_b+="</table></div>";

ShowDebug("Args: "+args+"<br />");

	if(cols.bundled) { buildstring_b+="<p>* NOTE: Bundled minutes only apply when destination number is a UK landline.</p>"; }
	if(cols.callcost>0 || cols.ownrcost>0 || cols.bundled>1) {
		if(argv[0].match(/^070/)) {
			buildstring_b+="<p>&#8224; All call charge prices shown are in pence per minute and are standard BT retail prices.";
			var cpt=CheckPartnershipSetting();
			if(cpt) {buildstring_b+="<br /><br />Revenue generated as a result of a call to any of the 07 numbers in our ranges will be subject to a cap if the incoming call originates outside the UK. The capped revenue rates are 5ppm peak, 4ppm off-peak and 2ppm at weekends.";}
			buildstring_b+="</p>";
		} else {
			buildstring_b+="<p>&#8224; All call charges are shown in pence per minute; actual calls are charged on a per-second basis. Costs to caller shown are standard BT retail prices. Costs or revenue to you shown here apply only when destination number is a UK landlines unless otherwise stated. If a mobile or international number is specified as the destination number the shopping cart will recalculate the cost/revenue on calls accordingly.</p>";
		}
	}
	
	if(parseInt(argv[1])>0 && parseInt(argv[2])>0 && parseInt(argv[3])>0) {
		buildstring_b+=Pages(argv[1],argv[2],argv[3]);
		max=parseInt(argv[1])+parseInt(argv[2])-1;
		if(max>argv[3]) {max=argv[3];}
		document.getElementById('numnums').innerHTML="("+argv[3]+" numbers found; showing "+argv[1]+" to "+max+")";
	}

	var revstr="Cost";
	if(anyownrrevenue) {revstr="Revenue";}
	if(anyownrcost && anyownrrevenue) {revstr="Revenue or Cost";}	//both true or both false.

	//Lots of header stuff, which has to be done after the main loop so it knows whether to show 'Revenue' or 'Cost' in revstr.
	buildstring_a="<div id='numberresultstable'>";
	if(parseInt(argv[1])>0 && parseInt(argv[2])>0 && parseInt(argv[3])>0) {
		buildstring_a+=Pages(argv[1],argv[2],argv[3]);
	} else if(parseInt(argv[4])>0) {
		buildstring_a+="<a href='javascript:GetSamples(\""+G_combotext+"\")'>[More Sample Numbers]</a><br />&nbsp;";
	}
	buildstring_a+="<table id='resulttable' align='center' width='565' border='0' cellpadding='0' cellspacing='0' class='smallText pad15L resultTable'>";

	if(cols.splitcallcost || cols.splitownrcost) {	//can't fit all the columns on the table at once, so add space for the drop-down to show prices.
		buildstring_a+="<tr class='numlookup_hdr'><td colspan='3'>&nbsp;</td><td align='center' class='leftBorder' colspan='3' id='costtypeinfo' style='font-weight:bold;'>";
	}
	if(cols.splitcallcost || cols.splitownrcost) {
		//build the drop-down if required...
		var opt_revstr='';
		if(argv[0].match(/^070/)) {opt_revstr="Partner Revenue";} else {opt_revstr=revstr+" to You";}
		buildstring_a+="<select name='costtype' id='costtype' onChange='FlipPanels(this.value);'>";
		buildstring_a+="<option value='2' "+(argv[0]=='0870'?"":"selected='1'")+">Cost to Caller</option>";
		buildstring_a+="<option value='3' "+(argv[0]=='0870'?"selected='1'":"")+">"+opt_revstr+"</option>";
		buildstring_a+="</select>";
	}

	if(cols.splitcallcost || cols.splitownrcost) {	//can't fit all the columns on the table at once, so add space for the drop-down to show prices.
		var remcols=1+cols.setupfee+cols.servchg;
		buildstring_a+="</td><td class='leftBorder' colspan='"+remcols+"'>&nbsp;</td></tr>";
	}
	buildstring_a+=VariableColHdr(cols,revstr);

	document.getElementById('numberlist').innerHTML=buildstring_a+buildstring_b;
	if(!anysetup) { document.getElementById('promographic').innerHTML="<img src='"+G_imgdir+"images/setup-fee-waived2.gif' width='60' height='43' alt='No setup fees for any of the numbers shown.' />"; }	//note: we can't just change the visibility here, because we want the graphic to load fresh each time we need it, so it flashes.
	FlipPanels(argv[0]=='0870'?3:2);	//default to purchase price panel. (or the revenue panel for 0870)
	MainCartButton();
}

function SetCols(number) {
	var cols=new Object();	// Setup the column flags: Default values below, but will be changed as required by different number types.
	cols.callcost=3;	// need to show three columns for cost of call (ie peak, off-peak, weekend).
	cols.ownrcost=3;	// ditto for cost or revenue to you fields.
	cols.bundled=3;		// show bundled mins and excess mins fields.
	cols.setupfee=1;	// show setup fee column.
	cols.servchg=1;		// show service charge column
	cols.revcostshowboth=false;	// this type of number (currently only 0844/0843) will show both costs and revenues to the owner, possibly at the same time, so can't do abs() on the values later on.

	switch(number.substring(0,2)) {
		case '08' :
			cols.numtype=number.substring(0,4);
			switch(cols.numtype) {
				case '0844' :
				case '0843' :
// 					cols.revcostshowboth=true;
					cols.callcost=1;
					cols.ownrcost=2;
					cols.bundled=0;
					cols.servchg=0;		//SDC, 2008-01-17: No svc chg column, at req of WB.
					break;
				case '0845' :
					cols.callcost=2;
					cols.ownrcost=0;
					cols.bundled=0;
					cols.servchg=0;
					break;
				case '0800' :
					cols.callcost=0;
					cols.ownrcost=0;
					break;
				case '0808' :
					cols.callcost=0;
					cols.ownrcost=0;
					break;
				case '0870' :
				    //-+DCK
					//cols.callcost=3;
					//cols.ownrcost=2;
					//cols.bundled=0;
					//cols.servchg=0;
					//+-DCK
					//-
			        cols.numtype=number.substring(0,4);
			        cols.callcost=0;
			        cols.ownrcost=0;
			        cols.servchg=1;
			        //+
					break;
				case '0871' :
					cols.callcost=1;
					cols.ownrcost=2;
					cols.bundled=0;
					cols.servchg=0;
					break;
			}
			break;
		case '07' :
			cols.numtype='070';
			cols.bundled=0;
			var cpt=CheckPartnershipSetting();
			if(cpt) { cols.ownrcost=3 } else { cols.ownrcost=0; }
			break;
		case '09' :
			cols.callcost=1;
			cols.ownrcost=1;
			cols.numtype='09';
			cols.bundled=0;
			cols.servchg=0;
			break;
		case '03' :
			cols.numtype=number.substring(0,4);
			cols.callcost=0;
			cols.ownrcost=0;
			cols.servchg=1;
			break;
		case '01' :
		case '02' :
			cols.numtype=number.substring(0,3);
			cols.callcost=0;
			cols.ownrcost=0;
			cols.servchg=1;
			break;
	}

	cols.total=cols.callcost+cols.ownrcost+cols.bundled+cols.setupfee+cols.servchg;
	cols.splitcallcost=(cols.total>5 && cols.callcost>1);
	cols.splitownrcost=(cols.total>5 && cols.ownrcost>1);

	return cols;
}

function VariableColHdr(cols,revstr) {
	var output="";
	var th1="<th rowspan='1' class='leftBorder'>";
	var th2="<th rowspan='2' class='leftBorder'>";
	var th3="</th>";
	var finalcol=th2+"<span id='maincartbutton'></span>"+th3+"</tr><tr class='numlookup_hdr'>";	//end col for the Add To Cart buttons...

	var ppmtitle="title='&#8224; Price in pence per minute. Applies only when destination is a UK landline.'";


	output+="<tr class='numlookup_hdr'>";

	output+="<th rowspan='2' height='44'>Number</th><th rowspan='2' class='leftBorder'>Suggested Display<br />Format</th><th rowspan='2' class='leftBorder'>Pricing<br />Band</th>"

	if(cols.bundled>0) {
		output+=th2+"<span class='infohelp' title='NOTE: Bundled minutes only apply when destination number is a UK landline'>Bundled<br />Minutes*</span>"+th3;
	}
	if(cols.bundled>1) {
		output+=th2+"<span class='infohelp' "+ppmtitle+">Excess<br />Minutes &#8224;</span>"+th3;
	}
	if(cols.splitcallcost || cols.splitownrcost) {
		//doing some splitting!

		// Col 1: Peak rate cost  / Peak rate revenue
		output+=th2;
		if(cols.callcost==1) { output+="<span colspan='3' class='pricetype2 infohelp' "+ppmtitle+">Cost To<br />Caller&nbsp;&#8224;</span>"; }
		if(cols.callcost>1)  { output+="<span colspan='1' class='pricetype2 infohelp' "+ppmtitle+">Peak<br />Rate&nbsp;&#8224;</span>"; }
		if(cols.ownrcost==1) { output+="<span colspan='3' class='pricetype3 infohelp' "+ppmtitle+">"+revstr+" To<br />You&nbsp;&#8224;</span>"; }
		if(cols.ownrcost>1)  { output+="<span colspan='1' class='pricetype3 infohelp' "+ppmtitle+">Peak<br />Rate&nbsp;&#8224;</span>"; }
		output+=th3;

		// Col 2: Off-peak rate cost  / Off-peak rate revenue
		output+=th2;
		if(cols.callcost==2) { output+="<span colspan='2' class='pricetype2 infohelp' "+ppmtitle+">Off<br />Peak&nbsp;&#8224;</span>"; }
		if(cols.callcost>2)  { output+="<span colspan='1' class='pricetype2 infohelp' "+ppmtitle+">Off<br />Peak&nbsp;&#8224;</span>"; }
		if(cols.ownrcost==2) { output+="<span colspan='2' class='pricetype3 infohelp' "+ppmtitle+">Off<br />Peak&nbsp;&#8224;</span>"; }
		if(cols.ownrcost>2)  { output+="<span colspan='1' class='pricetype3 infohelp' "+ppmtitle+">Off<br />Peak&nbsp;&#8224;</span>"; }
		output+=th3;

		// Col 2: Off-peak rate cost  / Off-peak rate revenue
		output+=th2;
		if(cols.callcost==3) { output+="<span colspan='1' class='pricetype2 infohelp' "+ppmtitle+">W/end&nbsp;&#8224;</span>"; }
		if(cols.ownrcost==3) { output+="<span colspan='1' class='pricetype3 infohelp' "+ppmtitle+">W/end&nbsp;&#8224;</span>"; }
		output+=th3;

		if(cols.setupfee>0) {
			output+=th2+"<span class='infohelp' title='For some numbers, you may need to pay a setup fee to purchase the number.'>Setup<br />Fee</span>"+th3;
		}
		if(cols.servchg>0) {
			output+=th2+"<span class='infohelp' title='For some numbers, you may need to pay a monthly fee.'>Monthly<br />Service<br />Fee</span>"+th3;
		}
		if(cols.bundled>2) {
			output+=th2+"<span class='infohelp' title='Numbers with a bundled minutes package also offer a low usage option.'>Low<br />Usage<br />Option</span>"+th3;
		}
		output+=finalcol;
	} else {
		//no splitting required... so easy to place all the columns :-)
		if(cols.callcost>0) {
			if(cols.callcost==1) {
				output+=th2+"<span class='infohelp' "+ppmtitle+">Cost To<br />Caller&nbsp;&#8224;</span>"+th3;
			} else {
				output+="<th rowspan='1' colspan='"+cols.callcost+"' class='leftBorder'><span class='infohelp' "+ppmtitle+">Cost To Caller</span></th>";
			}
		}
		if(cols.ownrcost>0) {
			if(cols.ownrcost==1) {
				output+=th2+"<span class='infohelp' "+ppmtitle+">"+revstr+" To<br />You&nbsp;&#8224;</span>"+th3;
			} else {
				output+="<th rowspan='1' colspan='"+cols.ownrcost+"' class='leftBorder'><span class='infohelp' "+ppmtitle+">"+revstr+" To You</span></th>";
			}
		}
		if(cols.setupfee>0) {
			output+=th2+"<span class='infohelp' title='For some numbers, you may need to pay a setup fee to purchase the number.'>Setup<br />Fee</span>"+th3;
		}
		if(cols.servchg>0) {
			output+=th2+"<span class='infohelp' title='For some numbers, you may need to pay a monthly fee.'>Monthly<br />Service<br />Fee</span>"+th3;
		}

		if(cols.callcost>1 || cols.ownrcost>1) {
			output+=finalcol;
		}
		if(cols.callcost>1) {
			output+=th1+"<span class='infohelp' "+ppmtitle+">Peak Rate&nbsp;&#8224;</span>"+th3;
			output+=th1+"<span class='infohelp' "+ppmtitle+">Off-Peak&nbsp;&#8224;</span>"+th3;
			if(cols.callcost>2) {
				output+=th1+"<span class='infohelp' "+ppmtitle+">W/end&nbsp;&#8224;</span>"+th3;
			}
		}
		if(cols.ownrcost>1) {
			output+=th1+"<span class='infohelp' "+ppmtitle+">Peak Rate&nbsp;&#8224;</span>"+th3;
			output+=th1+"<span class='infohelp' "+ppmtitle+">Off-Peak&nbsp;&#8224;</span>"+th3;
			if(cols.ownrcost>2) {
				output+=th1+"<span class='infohelp' "+ppmtitle+">W/end&nbsp;&#8224;</span>"+th3;
			}
		}
		if(cols.bundled>2) {
			output+=th2+"<span class='infohelp' title='Numbers with a bundled minutes package also offer a low usage option.'>Low<br />Usage<br />Option</span>"+th3;
		}
		if(cols.callcost<=1 && cols.ownrcost<=1) {
			output+=finalcol;
		}
	}

	output+="</tr>";
	return output;
}

function GetRowObj(tmp) {
	result=tmp[count].split("|");
	var rowobj=new Object;
	rowobj.id=result[0];
	rowobj.number=result[1];
	rowobj.numstring=result[2];
	rowobj.band=result[3];
	rowobj.callcostpeak=parseInt(result[4]);
	rowobj.callcostoffpeak=parseInt(result[5]);
	rowobj.callcostweekend=parseInt(result[6]);
	rowobj.chargemeth=result[7];
	rowobj.incart=result[8];
	rowobj.mincred=result[9];
	rowobj.setupfee=result[10];
	rowobj.servchg=result[11];
	rowobj.annualfee=result[12];
	rowobj.ownrcostpeak=result[13];
	rowobj.ownrcostoffpeak=result[14];
	rowobj.ownrcostweekend=result[15];
	rowobj.bundledmins=result[16];
	rowobj.lowusagepermin=result[17];
	rowobj.lowusagemonthly=result[18];

ShowDebug(rowobj.incart+" / "+rowobj.lowusagepermin+" / "+rowobj.lowusagemonthly+"<br />\n");
	switch (parseInt(rowobj.band)) {
		case 0: { rowobj.bandstr='Standard'; break;}		//was 'Standard'.... then 'Free'.... now back to 'Standard' again.
		case 1: { rowobj.bandstr='Bronze'; break;}
		case 2: { rowobj.bandstr='Silver'; break;}
		case 3: { rowobj.bandstr='Gold'; break;}
		case 4: { rowobj.bandstr='Platinum'; break;}
		case 5: { rowobj.bandstr='Platinum +'; break;}
		case 6: { rowobj.bandstr='Platinum +'; break;}
		case 7: { rowobj.bandstr='Platinum +'; break;}
		case 8: { rowobj.bandstr='Platinum +'; break;}
		default:{ rowobj.bandstr='Band '+rowobj.band; break;}
	}

	return rowobj;
}

function PayAsYouGoTicked(id,checked) {
	//when 'pay as you go' checkbox (un)ticked, show the appropriate values in the various columns.
	if(checked) { show='low'; hide='std'; } else { show='std'; hide='low'; }

	document.getElementById(hide+'bundled_'+id).style.visibility='hidden';
	document.getElementById(hide+'bundled_'+id).style.display='none';
	document.getElementById(show+'bundled_'+id).style.visibility='visible';
	document.getElementById(show+'bundled_'+id).style.display='block';

	document.getElementById(hide+'excess_'+id).style.visibility='hidden';
	document.getElementById(hide+'excess_'+id).style.display='none';
	document.getElementById(show+'excess_'+id).style.visibility='visible';
	document.getElementById(show+'excess_'+id).style.display='block';

	document.getElementById(hide+'servchg_'+id).style.visibility='hidden';
	document.getElementById(hide+'servchg_'+id).style.display='none';
	document.getElementById(show+'servchg_'+id).style.visibility='visible';
	document.getElementById(show+'servchg_'+id).style.display='block';
}

function ShowMore(id) {
	document.getElementById('showmore_'+id).style.visibility='hidden';
	document.getElementById('showmore_'+id).style.display='none';

	document.getElementById('suggestions_'+id).style.visibility='visible';
	document.getElementById('suggestions_'+id).style.display='block';
}

function Pages(startat,limit,max) {
	var buildstring="";
	var nextstop=startat+limit;
	var prevstop=startat-limit;
	var page=parseInt((startat-1)/limit)+1;
	var maxpage=Math.ceil(max/limit);
	if(nextstop>max) {nextstop=max;}
	if(prevstop<1) {prevstop=1;}
	buildstring+="<table width='100%'><tr><td align='left'>";

	if(page>1) {
		buildstring+="<a href='javascript:ShowSome("+(((page-2)*limit)+1)+","+limit+","+max+")'>&lt;&lt; Prev Page</a>";
	} else {
		buildstring+="<span class='inactive_numlink'>&lt;&lt; Prev Page</span>";
	}
	var minpg=page-3;
	var maxpg=page+3;
	if(page<=3) {maxpg+=(4-page);}
	for(var count=minpg;count<=maxpg;count++) {
		if(count>0 && count<=maxpage) {
			if(count!=page) {
				buildstring+=" <a href='javascript:ShowSome("+(((count-1)*limit)+1)+","+limit+","+max+")'>["+count+"]</a> ";
			} else {
				buildstring+=" <b>["+count+"]</b> ";
			}
		}
	}
	if(page<maxpage) {
		buildstring+="<a href='javascript:ShowSome("+(((page)*limit)+1)+","+limit+","+max+")'>Next Page &gt;&gt;</a>"
	}
	buildstring+="</td>";

	var sel25 =""; if(G_ResultsPerPage==25) {sel25 ="selected='1'";}
	var sel50 =""; if(G_ResultsPerPage==50) {sel50 ="selected='1'";}
	var sel100=""; if(G_ResultsPerPage==100){sel100="selected='1'";}
	var sel250=""; if(G_ResultsPerPage==250){sel250="selected='1'";}

	buildstring+="<td align='right'>";
	buildstring+="Results per page: <select onchange='ChangeResultsPerPage(this.value,"+page+","+max+");'><option value='25' "+sel25+">25</option><option value='50' "+sel50+">50</option><option value='100' "+sel100+">100</option><option value='250' "+sel250+">250</option></select>";
	buildstring+="</td>";

	buildstring+="</tr></table>";

	return buildstring;
}

function ChangeResultsPerPage(numresults,page,max) {
	G_ResultsPerPage=numresults;
	var startat=(page-1)*numresults+1;
	if(startat>max-numresults+1) {startat=max-numresults+1;}
	if(startat<1) {startat=1;}
	ShowSome(startat,numresults,max);
}

function ShowPartFullError(responsestring,args) {
//	var tmp=responsestring.split("\n");	//split the returned lines into an array.
	var result=responsestring.split("|");
	var argv=args.split(",");
	var buildstring = '';

	if(result[0]==3) {
		DisplayInfoHdr(argv[0],0,0);
	} else {
		buildstring="<h2>Error</h2>An error was reported while trying to find pricing info for "+argv[0]+"<br />The error report was:<br />"+result[1]; //responsestring;
		document.getElementById('fullresults').innerHTML=buildstring;
	}
}

function AddToCart(numberID,fullnumber,lookupID,redir) {
	if(!G_username && !G_LoginOffered) {
		//var wi=350;
		//var hi=300;
		//pagecc_custform = window.open('loginpopup.php', 'cc_login', 'width='+wi+',height='+hi+',left='+((screen.width-wi)/2)+',top='+((screen.height-(hi*2))/2)+',toolbar=0,scrollbars=1,statusbar=0,menubar=0,resizable=0');
		//G_LoginOffered=true;
	}

	var payg=1;
	if(document.getElementById("usagecheck_"+numberID)) {
		if(document.getElementById("usagecheck_"+numberID).checked) { payg=2; }
	}

	var sucFunc='';
	if (redir==null || redir==true) { sucFunc='GoToCheckout'; } else { sucFunc='DisplayCartInfo'; }
	apilite_call('addtocart.php?number_id='+numberID+"&lookup_id="+lookupID+"&payg="+payg, sucFunc, numberID+","+fullnumber+",a", 'ShowCartError', numberID+","+fullnumber);
}

function RemoveFromCart(numberID,fullnumber) {
	apilite_call('removefromcart.php?number_id='+numberID, 'DisplayCartInfo', numberID+","+fullnumber+",r", 'ShowCartError', numberID+","+fullnumber);
}

function LookupCombo(lookup,calling_id,fixprice,fixtype) {
	var argv=lookup.split("/");
	document.getElementById('promographic').innerHTML="&nbsp;";
	if(calling_id==0) {document.getElementById('number').value='';}
	G_combolen=argv[1].length;
	G_combotext=argv[1]
	var restriction=(G_combotext=='09'?document.getElementById('restrictioncombo').value:'');
	G_lastid=argv[0];
	document.getElementById("number").maxLength=G_phonenumlength-G_combotext.length;
	if(document.getElementById("number").value.length>G_phonenumlength-G_combotext.length) {document.getElementById("number").value=document.getElementById("number").value.substring(0,G_phonenumlength-G_combotext.length);}

ShowDebug("LookupCombo: "+lookup+" | "+restriction+" | "+calling_id+" | "+fixprice+" | "+fixtype+" | "+restriction+"<br /><br />");
	if(argv[1]=='09' || fixprice>0) {
		document.getElementById('premiumpricepicker').style.visibility='visible';
		document.getElementById('premiumpricepicker').style.display='block';
		document.getElementById('selprefixinfo').style.visibility='hidden';
		document.getElementById('selprefixinfo').style.display='none';
	} else {
		document.getElementById('premiumpricepicker').style.visibility='hidden';
		document.getElementById('premiumpricepicker').style.display='none';
		document.getElementById('selprefixinfo').style.visibility='visible';
		document.getElementById('selprefixinfo').style.display='block';
	}

	var url='lookup.php?picked_id='+argv[0];
	url+="&restrict="+restriction;
	if(fixprice && fixtype) {url+="&fix_price="+fixprice+"&price_type="+fixtype;}
	apilite_call(url, 'UpdateForm', argv[0]+","+calling_id+",,,"+fixprice+","+fixtype, 'SomeSortOfError', argv[0]+","+calling_id+",,"+fixprice+","+fixtype);
	var sample=(calling_id==0);
	CheckNumber("Lookup",sample);
}

function GetSamples(number) {
	if(!checkallowedtopurchase()) { return; }

	var url='sampleinfo.php?number='+number;
	url+="&restrict="+document.getElementById('restrictioncombo').value;
	apilite_call(url, 'DisplayFullInfo', number+',,,,'+'1', 'ShowPartFullError', number+',,,,'+'1');
}

function LoggedIn(username) {
	G_username=username;
	if(document.getElementById('numberlist')) {
		ReloadCurrentNums();
	} else {
		DisplayInfoHdr(G_combotext,0,true);
		LookupCombo(document.getElementById('id_0').value,0);
	}
}

function SomeSortOfError(responsestring,args) {
	//This function is called when HandleResponse encounters an error.
	//This would usually just mean that the selected ID doesn't have any more IDs linked to it, which is perfectly valid, but we need to check for other error conditions as well...
	var tmp=responsestring.split("|");	//split the returned lines into an array.
	var argv=args.split(",");

	switch (tmp[0]) {
		case "4" : {
			alert(tmp[1]);
			document.getElementById("after_id_"+argv[1]).innerHTML="";	//get rid of any number blocks after the current one that are no longer valid.
		  	break;
		}
		case "3" : {		//No records. Don't report an error; just deal with it.
			//document.getElementById("after_id_"+argv[1]).innerHTML="&nbsp;";	//get rid of any number blocks after the current one that are no longer valid.
			document.getElementById("after_id_"+argv[1]).innerHTML="";	//get rid of any number blocks after the current one that are no longer valid.
			CheckNumber("No results",0);
			if(G_combotext.length>4) {
				GetNumNums();
			}
			break;
		}
		case "1","2": {
			alert("An error has occurred: "+responsestring);
			break;
		}
	}
	if(G_FirstTime==1) {
		AddToCart(0,'',0,G_np_directtocart);
	}
	G_FirstTime=0;
}

function GetSelectedBands() {
	var selected='';
	var sline='';
	var index = 0;
	for (var intLoop=0; intLoop < document.getElementById('bracket').length; intLoop++) {
		if (document.getElementById('bracket')[intLoop].selected) {
			sline=document.getElementById('bracket')[intLoop].value
			if(sline=='xx') {
				selected='';
				return selected;
			}
			if(selected.length>0) { selected+=","; }
			if(sline=="b5") {	//was "b4"
				//platinum includes all platinum and platinum+ codes
				selected+="5,6,7,8";	//was "4,5,6,7,8"
			} else {
				selected+=sline.substring(2,1);
			}
		}
	}
	return selected;
}

function FlipPanels(panel) {

	for(var count=1; count<=5; count++) {
		if(count==panel) {
			SetStyle('.pricetype'+count,'display','inline');
			SetStyle('.pricetype'+count,'visibility','visible');
		} else {
			SetStyle('.pricetype'+count,'display','none');
			SetStyle('.pricetype'+count,'visibility','hidden');
		}
	}
	if(panel==4 || panel==5) {
		SetStyle('.leftBorder2','borderLeft','0px');
	} else {
		SetStyle('.leftBorder2','borderLeft','1px solid #C9C9C9');
	}
}

function PoundsOrPence(currvalue,nullrtn,penceonly,bandflag) {
	if(bandflag>=5 && (currvalue==0 || isNaN(currvalue))) {return "---";}
	currvalue=parseFloat(currvalue);
	if(isNaN(currvalue)) {return (nullrtn==1?"---":POACode(1));}
	if(!penceonly && (currvalue>=100 || currvalue==0)) {
		currvalue=currvalue/100;
		currvalue="&#163;"+currvalue.toFixed(2);
	} else {
		currvalue=currvalue+"p";
	}
	return currvalue;
}

function POACode(type) {
	switch(type) {
		case 1: return "<acronym class='infohelp' title='Price on application; call costs and revenue may vary.'>POA</acronym>";
		case 2: return "<a href='javascript:POAMsg();' title='Price on application; please call 08000 848484 for a quote for this number.'><img src='"+G_imgdir+"images/poa1.gif' width='60' height='16' border='0'><a/>";
	}
}

function POAMsg() {
	var encradd="sales!skycomu?";
	var antispam="no@example.com";
	encradd=encradd.replace('!',antispam.charAt(2));
	encradd=encradd.replace('?','k.c');
	alert("Price on application: Please call 08000 848484 for a quote for this number, or email us on "+encradd+'om');
}

function MainCartButton() {
	var obj=document.getElementById('maincartbutton');
	var qlt=document.getElementById('quicklist_0');
	if(obj && qlt) {
 		var open=(document.getElementById('quicklist_0').innerHTML.match("empty")!='empty');
		if(open) {
			obj.innerHTML="<a href='"+showcartURL+"' title='Click here to go to the checkout area.'><img src='"+G_imgdir+"images/cart-big2.gif' width='27' height='27' border='0' /></a>";
		} else {
			obj.innerHTML="<img src='"+G_imgdir+"images/cart-big1.gif' width='27' height='27' border='0' title='Add items to your cart by clicking the Add to cart buttons below.' />";
		}
	}
}

function ShowThisRow(cols,rowobj,rownum) {
	if(!rowobj) {return "";}
	var output='';

	if(rownum%2==1) {
		output+="<tr class='numrow_1'>";
	} else {
		output+="<tr class='numrow_2'>";
	}

	numsplit=rowobj.numstring.split(",");
	output+="<td height='22' align='center'>&nbsp;"+rowobj.number+"&nbsp;</td>";
	output+="<td class='leftBorder' align='center'>"+numsplit[0];
	if(numsplit.length>1) {
		output+="<span id='showmore_"+result[0]+"'><br /><a href='javascript:ShowMore("+result[0]+")'>[more...]</a></span>";
		output+="<span style='display:none; visibility:hidden' id='suggestions_"+result[0]+"'>";
		for(var suggs=1; suggs<numsplit.length; suggs++) {
			output+=numsplit[suggs]+"<br />";
		}
		output+="</span>";
	}
	output+="</td>";
	output+="<td class='leftBorder' align='center'>"+rowobj.bandstr+"</td>";
    
    //DCK
    //var attrs;
    //for( var attr in rowobj) {
    //    attrs+= attr+', ';
    //}
    //console.log("y"+attrs);
    //console.log(rowobj.band);

	if(rowobj.chargemeth=="per_call") {
		rowobj.callcostpeakstring=PoundsOrPence(rowobj.callcostpeak/1000,1,1,0)+" per call";
		if(cols.numtype!='0843' && cols.numtype!='0844' && cols.numtype!='0845' && rowobj.ownrcostpeak==0 && rowobj.ownrcostoffpeak==0 && rowobj.ownrcostweekend==0) {
			rowobj.callownrpeakstring=POACode(1);
		}else {
			rowobj.callownrpeakstring=PoundsOrPence(Math.abs(rowobj.ownrcostpeak)/1000,1,1,0)+" per call";
		}
	} else {
		rowobj.callcostpeakstring=PoundsOrPence(rowobj.callcostpeak/1000,1,1,0); //+"/min";
		rowobj.callcostoffpeakstring=PoundsOrPence(rowobj.callcostoffpeak/1000,1,1,0); //+"/min";
		rowobj.callcostweekendstring=PoundsOrPence(rowobj.callcostweekend/1000,1,1,0); //+"/min";
		if(cols.revcostshowboth){
		    rowobj.callownrpeakstring=PoundsOrPence(rowobj.ownrcostpeak/1000,2,1,0); //+"/min";
		    rowobj.callownroffpeakstring=PoundsOrPence(rowobj.ownrcostoffpeak/1000,2,1,0); //+"/min";
		    rowobj.callownrweekendstring=PoundsOrPence(rowobj.ownrcostweekend/1000,2,1,0); //+"/min";
		}else {
			rowobj.callownrpeakstring=PoundsOrPence(Math.abs(rowobj.ownrcostpeak)/1000,2,1,0); //+"/min";
			rowobj.callownroffpeakstring=PoundsOrPence(Math.abs(rowobj.ownrcostoffpeak)/1000,2,1,0); //+"/min";
			rowobj.callownrweekendstring=PoundsOrPence(Math.abs(rowobj.ownrcostweekend)/1000,2,1,0); //+"/min";
		}
	}
	rowobj.lowusageperminstring=PoundsOrPence(rowobj.lowusagepermin/1000,2,1,0);

	if(cols.bundled>0) {
		//bundled mins type? then show bundled mins and excess mins (which comes from the cost to owner field)
		output+="<td align='center' class='leftBorder'><span align='center' id='stdbundled_"+rowobj.id+"' style='visibility:"+(rowobj.incart==2?"hidden":"visible")+"; display:"+(rowobj.incart==2?"none":"block")+";'>"+((rowobj.band>=5 && !rowobj.bundledmins)?"---":rowobj.bundledmins)+"</span><span align='center' id='lowbundled_"+rowobj.id+"' style='visibility:"+(rowobj.incart==2?"visible":"hidden")+"; display:"+(rowobj.incart==2?"block":"none")+";'>0"+"</span></td>";
	}
	if(cols.bundled>1) {
		output+="<td align='center' class='leftBorder'><span align='center' id='stdexcess_"+rowobj.id+"' style='visibility:"+(rowobj.incart==2?"hidden":"visible")+"; display:"+(rowobj.incart==2?"none":"block")+";'>"+((rowobj.band>=5 && !rowobj.callownrpeakstring)?"---":rowobj.callownrpeakstring)+"</span><span align='center' id='lowexcess_"+rowobj.id+"' style='visibility:"+(rowobj.incart==2?"visible":"hidden")+"; display:"+(rowobj.incart==2?"block":"none")+";'>"+((rowobj.band>=5 && !rowobj.lowusageperminstring)?"---":rowobj.lowusageperminstring)+"</span></td>";
	}
    
	if(cols.splitcallcost || cols.splitownrcost) {
		//got to split the columns...
		// Col 1: Peak rate cost  / Peak rate revenue
		output+="<td class='leftBorder' align='center'>";
		if(cols.callcost==1) { output+="<span colspan='3' class='pricetype2' align='center'>"+rowobj.callcostpeakstring+"</span>"; }
		if(cols.callcost>1)  { output+="<span colspan='1' class='pricetype2' align='center'>"+rowobj.callcostpeakstring+"</span>"; }
		if(cols.ownrcost==1) { output+="<span colspan='3' class='pricetype3' align='center'>"+rowobj.callownrpeakstring+"</span>"; }
		if(cols.ownrcost>1)  { output+="<span colspan='1' class='pricetype3' align='center'>"+rowobj.callownrpeakstring+"</span>"; }
		output+="</td>";

		// Col 2: Off-peak rate cost  / Off-peak rate revenue
		output+="<td class='leftBorder' align='center'>";
		if(cols.callcost==2) { output+="<span colspan='2' class='pricetype2' align='center'>"+rowobj.callcostoffpeakstring+"</span>"; }
		if(cols.callcost>2)  { output+="<span colspan='1' class='pricetype2' align='center'>"+rowobj.callcostoffpeakstring+"</span>"; }
		if(cols.ownrcost==2) { output+="<span colspan='2' class='pricetype3' align='center'>"+rowobj.callownroffpeakstring+"</span>"; }
		if(cols.ownrcost>2)  { output+="<span colspan='1' class='pricetype3' align='center'>"+rowobj.callownroffpeakstring+"</span>"; }
		output+="</td>";

		// Col 2: Off-peak rate cost  / Off-peak rate revenue
		output+="<td class='leftBorder' align='center'>";
		if(cols.callcost==3) { output+="<span colspan='1' class='pricetype2' align='center'>"+rowobj.callcostweekendstring+"</span>"; }
		if(cols.ownrcost==3) { output+="<span colspan='1' class='pricetype3' align='center'>"+rowobj.callownrweekendstring+"</span>"; }
		output+="</td>";
	} else {
		if(rowobj.chargemeth=="per_call") {
			if(cols.callcost>0) {
				output+="<td colspan='"+cols.callcost+"' class='leftBorder' align='center'>"+rowobj.callcostpeakstring+"</td>";
			}
			if(cols.ownrcost>0) {
				output+="<td colspan='"+cols.ownrcost+"' class='leftBorder' align='center'>"+rowobj.callownrpeakstring+"</td>";
			}
		} else {
			if(cols.callcost>0) {
				output+="<td class='leftBorder' align='center'>"+rowobj.callcostpeakstring+"</td>";
				if(cols.callcost>1) {
					output+="<td class='leftBorder' align='center'>"+rowobj.callcostoffpeakstring+"</td>";
				}
				if(cols.callcost>2) {
					output+="<td class='leftBorder' align='center'>"+rowobj.callcostweekendstring+"</td>";
				}
			}
			if(cols.ownrcost>0) {
				output+="<td class='leftBorder' align='center'>"+rowobj.callownrpeakstring+"</td>";
				if(cols.ownrcost>1) {
					output+="<td class='leftBorder' align='center'>"+rowobj.callownroffpeakstring+"</td>";
				}
				if(cols.ownrcost>2) {
					output+="<td class='leftBorder' align='center'>"+rowobj.callownrweekendstring+"</td>";
				}
			}
		}
	}
	if(cols.setupfee>0) {
		output+="<td class='leftBorder' align='center'>"+PoundsOrPence(rowobj.setupfee,2,0,rowobj.band)+"</td>";
	}
	if(cols.servchg>0) {
		output+="<td class='leftBorder' align='center'><span align='center' id='stdservchg_"+rowobj.id+"' style='visibility:"+(rowobj.incart==2?"hidden":"visible")+"; display:"+(rowobj.incart==2?"none":"block")+";'>"+PoundsOrPence(rowobj.servchg,2,0,rowobj.band)+"</span><span align='center' id='lowservchg_"+rowobj.id+"' style='visibility:"+(rowobj.incart==2?"visible":"hidden")+"; display:"+(rowobj.incart==2?"block":"none")+";'>"+PoundsOrPence(rowobj.lowusagemonthly,2,0,rowobj.band)+"</span></td>";
	}
	if(rowobj.band>=5 && (isNaN(parseFloat(rowobj.setupfee)) || parseFloat(rowobj.setupfee)==0) && (isNaN(parseFloat(rowobj.servchg)) || parseFloat(rowobj.servchg)==0) ) {
		if(cols.bundled>2) {
			output+="<td class='leftBorder' align='center'>&nbsp;</td>";
		}
		output+="<td class='leftBorder' align='center'>"+POACode(2)+"</td>";
	} else {
		if(cols.bundled>2) {
			if(rowobj.band<=2) {	//SDC, 20-Oct-2006.
			    //console.log("x");
				output+="<td class='leftBorder' align='center'><input type='checkbox' "+(rowobj.incart==2?"checked='1'":"")+" name='usagecheck_"+rowobj.id+"' id='usagecheck_"+rowobj.id+"' onclick='PayAsYouGoTicked("+rowobj.id+",this.checked);' /></td>";
			} else {
				output+="<td class='leftBorder' align='center'>&nbsp;</td>";
			}
		}
		if(rowobj.incart==1) {
			output+="<td title='"+rowobj.number+"' class='leftBorder' align='center' id='addlink_"+rowobj.id+"'><a href='"+showcartURL+"' title='Click here to go to the checkout area.'><img src='"+G_imgdir+"images/incart.gif' width='60' height='16' border='0' /></a></td>";
		} else {
			output+="<td title='"+rowobj.number+"' class='leftBorder' align='center' id='addlink_"+rowobj.id+"'><a href='javascript:AddToCart("+rowobj.id+",\""+rowobj.number+"\","+G_lastid+",G_np_directtocart);' title='Click to add this number to your shopping cart.'><img src='"+G_imgdir+"images/addtocart.gif' width='60' height='16' border='0' /></a></td>";
		}
	}
	output+="</tr>";

	return output;
}


function ShowCartError(responsestring,args) {
	var tmp=responsestring.split("|");	//split the returned lines into an array.
	var argv=args.split(",");
	var buildstring = '';

	switch (parseInt(tmp[0])) {
		case 3 : { buildstring="Sorry, this number ("+argv[1]+") is already allocated."; break; }
		case 4 : {
			buildstring=argv[1]+" is already in your cart.";
			document.getElementById("addlink_"+argv[0]).innerHTML="<a href='"+showcartURL+"' title='Click here to go to the checkout area.'><img src='"+G_imgdir+"images/incart.gif' width='60' height='16' border='0' /></a>";
			break;
		}
		case 5 : {
			buildstring="You don't have "+argv[1]+" in your cart.";
			document.getElementById("addlink_"+argv[0]).innerHTML="<a href='javascript:AddToCart("+argv[0]+",\""+argv[1]+"\","+G_lastid+",G_np_directtocart);' title='Click to add this number to your shopping cart.'><img src='"+G_imgdir+"images/addtocart.gif' width='60' height='16' border='0' /></a>";
			break;
		}
		case 7 : {
			alert(tmp[1]);
			return;
		}
		default :{ buildstring="<h2>Error</h2>An error was reported while trying to add an item to the shopping cart. The error report was:<br />"+responsestring; break; }
	}
	if(document.getElementById('cartinfo')) {
		document.getElementById('cartinfo').innerHTML=buildstring;
		document.getElementById("cartinfo").style.color='red';
	}
}

function UpdateForm(responsestring,args) {
	//This routine is called by HandleResponse if the HTTP call was successful.
	//Updates the combo boxes when a new one has to be displayed.
	var buildstring='';
	var tmp=responsestring.split("\n");	//split the returned lines into an array.
	var argv=args.split(",");
	var selected="";
	var selthis="";
	var force09="";

	buildstring ='<select ';
	if(argv[0]>0) { buildstring+='style="width:60px;" ';}	//stop the form re-arranging itself when you change top-level combos.
	buildstring+='id="id_'+argv[0]+'" name="'+argv[0]+'" onchange="LookupCombo(this.value,'+argv[0];
	if(argv[4]>0) { buildstring+=','+argv[4]+',\''+argv[5]+'\''; }
	buildstring+=');">';
	if(!argv[2]) {
		buildstring+='<option value="-1/'+G_combotext+'"> </option>';
	}
	for(count=0;count<tmp.length-1;count++) {
		result=tmp[count].split("|");
		if(G_level2) {
			if(result[1]==G_level2) { argv[2]=result[1]; }
			if(result[1]==G_maintype) { argv[2]=result[1]; }
		}
		selected='';
		if(argv[2] && argv[2]==result[1]) {selthis=result[0]+"/"+result[2]; selected=' selected="1"'; force09=result[2].substr(0,2);}
		if(argv[4]>0 && count==0) {selthis=result[0]+"/"+result[2]; selected=' selected="1"'; force09=result[2].substr(0,2);}
		buildstring+='<option value="'+result[0]+"/"+result[2]+'"'+selected+'>'+result[1]+'</option>';
	}
	buildstring+='</select>&nbsp;<span id="after_id_'+argv[0]+'"></span>';

	if(argv[1]==-1){
		document.getElementById("root_id").innerHTML=buildstring;
	} else {
		document.getElementById("after_id_"+argv[1]).innerHTML=buildstring;
	}
	if(selthis){//should only be passed from InitialLoad, and then only if the user arrived at the form asking for a specific number.
		document.getElementById('shortinfo').innerHTML = GetNumberTypeInfo(argv[2]);
		ToggleRestrictionCombo(force09);
		LookupCombo(selthis,argv[0],argv[4],argv[5]);
		return;
	}

	if(argv[1]==0) {	//selected the root number prefix, so we'll offer them some sample numbers from that block...
		GetSamples(G_combotext);
	}

	G_FirstTime=0;
}

//DCK
var sessionWarnedaboutFreeNumbers = 0;

function DisplayCartInfo(responsestring,args) {
	//This routine is called by HandleResponse if the HTTP call was successful.
	var buildstring='';
	var tmp=responsestring.split("\n");	//split the returned lines into an array.
	var argv=args.split(",");
	var cartlist='';

	var ci_obj=document.getElementById("cartinfo");
	var cm_obj=document.getElementById("cartclickme");

    var d_x = tmp[0].split("|");
    if (d_x[0]==7 && sessionWarnedaboutFreeNumbers==0){
        d_x2 = d_x[1].split("\n");
        alert(d_x2[0]);
        document.getElementById('nodisacountwarning').style.display='block';
        sessionWarnedaboutFreeNumbers =1;
        tmp[0] = tmp[2];//DCK
        tmp[1] = tmp[3];//DCK
    }
    else if(d_x[0]==7){
        tmp[0] = tmp[2];//DCK
        tmp[1] = tmp[3];//DCK
    }
    
	if(argv[0]>0) {
		if(argv[2]=='a') {
			document.getElementById("addlink_"+argv[0]).innerHTML="<a href='"+showcartURL+"' title='Click here to go to the checkout area.'><img src='"+G_imgdir+"images/incart.gif' width='60' height='16' border='0' /></a>";
		} else {
			document.getElementById("addlink_"+argv[0]).innerHTML="<a href='javascript:AddToCart("+argv[0]+",\""+argv[1]+"\","+G_lastid+",G_np_directtocart);' title='Click to add this number to your shopping cart.'><img src='"+G_imgdir+"images/addtocart.gif' width='60' height='16' border='0' /></a>";
		}
	} else {
		if(argv[2]=='r') {
			//removing item zero? that means we're removing all the items in the cart, so refresh the table to get rid of any 'remove' graphics.
			//(this is a slightly long-winded way of parsing through the table, but it does the job)
			var tblitems=document.getElementById("resulttable").getElementsByTagName("td");
			var idstr="";
			var tblitem=new Object;
			for(var count=0; count<tblitems.length; count++) {
				tblitem=tblitems.item(count);
				idstr=tblitem.id.split("_");
				if(idstr[0]=='addlink') {
					tblitem.innerHTML="<a href='javascript:AddToCart("+idstr[1]+",\""+tblitem.title+"\","+G_lastid+",G_np_directtocart);' title='Click to add this number to your shopping cart.'><img src='"+G_imgdir+"images/addtocart.gif' width='60' height='16' border='0' /></a>";
				}
			}
			if(ci_obj) {ci_obj.innerHTML="<span id='quicklist_0'>Your cart is thoroughly empty.</span><br />";}	//remove all the cart lines from the quicklist.
			if(ci_obj) {ci_obj.style.color='red';}
		}
	}


	if(tmp[0]==0) {
		if(ci_obj) {ci_obj.innerHTML="<span id='quicklist_0'>Your cart is thoroughly empty.</span><br />";}
		if(cm_obj) {cm_obj.innerHTML=cartclickme_off;}
		if(ci_obj) {ci_obj.style.color='red';}
	} else {
		var numberlist=tmp[1].split("|");
		cartlist="<span id='quicklist_0'>You have selected:</span><br /><br />";
		cartlist+=PartnerSpan();
		for(var count=0; count<numberlist.length; count++) {
			var thisnum=numberlist[count].split(":");
			if(thisnum[1]) {
				cartlist+="<span id='quicklist_"+thisnum[0]+"'><div>"+thisnum[1]+"<br /></div></span>";
			}
		}
		if(G_addlitems) {
			cartlist+="<span id='addlitems_0'><br />Additional Items:<br /><br /></span>";
			cartlist+=GetAddlItemsList();
		}
		if(ci_obj) {ci_obj.innerHTML=cartlist;}
		if(cm_obj) {cm_obj.innerHTML=cartclickme_on;}
		if(ci_obj) {ci_obj.style.color='#4ead5c';}
	}
	MainCartButton();

    


	if(G_level2 && G_FirstCart==1) {
		G_FirstCart=0;
		document.getElementById("number").value=G_remainingdigits;
		G_combotext=G_maintype+G_level2;
		document.getElementById("number").maxLength=G_phonenumlength-G_combotext.length;
		if(document.getElementById("number").value.length>G_phonenumlength-G_combotext.length) {document.getElementById("number").value=document.getElementById("number").value.substring(0,G_phonenumlength-G_combotext.length);}
		ValidateNumber();
	}
}

function checkallowedtopurchase() {
	//does this user have pre-arranged permission to order numbers in this range type? (applies to PRS ranges, etc)
	var type=document.getElementById('id_0').value.split('/');
	var blocktypes={'09':'Premium Rate','070':'Personal','0871':'0871'};

	var divstr="<div style='padding:5px; border:solid black 1px; margin:5px;'>"
	var divstr2="</div>";

	var allowed=readCookie('allocperms');
	if(!allowed) {allowed='';}

	for(var chktype in blocktypes) {
		if(chktype==type[1] && !allowed.match(chktype)) {
			var infostr="<b>"+blocktypes[chktype]+" Numbers</b><br />"
				+blocktypes[chktype]+" numbers can only be purchased via this site by approved customers.<br />"
				+"If you have been approved, please log into your account to gain access to this section.<br />"
				+"Other users should read our Premium Rate Service documentation to find out how to<br />"
				+"become an approved customer, or contact us for more information.";
			if(window.G_prsdocloc) {infostr+="<br /><a href='"+G_prsdocloc+"'>Click here for the documentation page</a>.";}
			document.getElementById('fullresults').innerHTML=divstr+infostr+divstr2;
			if(document.getElementById('footerinfo')) {document.getElementById('footerinfo').innerHTML='';}
			document.getElementById('shortinfo').innerHTML = GetNumberTypeInfo(type[1]);
			return false;
		}
	}

	return true;
}

