function URLDecode(psEncodeString) 
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
}

function copydeliveryaddress_fn()
{
updatebasket = "shippingcountry='"+document.getElementById('shippingcountry')[document.getElementById('shippingcountry').selectedIndex].value+"'&deliveryclass='"+document.getElementById('deliveryclass')[document.getElementById('deliveryclass').selectedIndex].value+"'"
document.getElementById("shippingaddress1").value = document.getElementById("address1").value;
document.getElementById("shippingfirstname").value = document.getElementById("firstname").value;
document.getElementById("shippinglastname").value = document.getElementById("lastname").value;
document.getElementById("shippingtown").value = document.getElementById("town").value;
document.getElementById("shippingcounty").value = document.getElementById("county").value;

document.getElementById("shippingcountry").value = document.getElementById("billingcountry")[document.getElementById("billingcountry").selectedIndex].value;
document.getElementById("shippingpostcode").value = document.getElementById("postcode").value;
ajax('updatebaskettotals',updatebasket)

}

						
function addOption(selectbox,text,value,clearfirst,thisstyle)
{

text = text.replace('&pound;','£')
if (document.getElementById(selectbox))
{
	if (clearfirst){
	document.getElementById(selectbox).options.length = 0
	}
		if (text != "" && value != "")
		{
      if (document.getElementById(selectbox))
    	{
      	var optn = document.createElement("OPTION");
        optn.text = URLDecode(text);
        optn.value = value;
    //		optn.backgroundcolor="#676767";
      	document.getElementById(selectbox).options.add(optn);
    	}
		}
	}
	else
	{
	alert(selectbox + " not found ")
	}
}

incloaded = false;
function inc(filename,doafter)
{
var body = document.getElementsByTagName('body').item(0);
script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';
body.appendChild(script)

incloaded = true;
}


function changetocase(whatcase,what,text)
{

	if (whatcase == "p")
	{
		document.getElementById(what).value = pCase(text) 
	}
	else
	{
		document.getElementById(what).value = text.toUpperCase() 
	}

}


function pCase(s) 
    { 
        return s.replace(/(\w)(\w*)/g,function 
        ( 
            strMatch, 
            strFirst, 
            strRest, 
            intMatchPos, 
            strSource 
        ) 
        { 
            return strFirst.toUpperCase() 
            +strRest.toLowerCase(); 
        }); 
    } 


//**********************************product zoomer
 function changeimg(tothis,thisimage)
  {
  	document.getElementById(thisimage).src=tothis
  }
	
	function zoominout(whichway)
	{
		if (whichway == "in")
		{
			document.getElementById("bigimg").style.display = "block"
			document.getElementById("img").style.display = "none"
		}
		else
		{
			document.getElementById("bigimg").style.display = "none"
			document.getElementById("img").style.display = "block"
		}
	}
	
	function swap_apartment_tab(tothis)
	{
	document.getElementById("tab"+document.getElementById("currenttabon").value).style.display = "none";
	document.getElementById("tab"+tothis).style.display = "block";
	
	/*off*/
	document.getElementById("tablink"+document.getElementById("currenttabon").value).style.color ="#adadaf"
	document.getElementById("tablink"+document.getElementById("currenttabon").value).style.border ="1px solid #d6d2cf"
	styleswap("tablink"+document.getElementById("currenttabon").value,"off")
	styleswap("tablink"+tothis,"on")
	/*off*/
	
	document.getElementById("currenttabon").value = tothis
	document.getElementById("tablink"+document.getElementById("currenttabon").value).style.color ="#3697fe"
	document.getElementById("tablink"+document.getElementById("currenttabon").value).style.border ="1px solid #3697fe"
	}

	
	function styleswap(thisid,thisclass){
	document.getElementById(thisid).className=thisclass;
}
	
	function showzoom(imgsrc)
	{
		//alert(imgsrc)
		embed = "<object classid=~clsid:d27cdb6e-ae6d-11cf-96b8-444553540000~ codebase=~http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0~ width=~400~ height=~410~  >"
    embed += "<param name=~allowScriptAccess~ value=~sameDomain~ />"
    embed += "<param name='movie' value='imgviewer2.swf?"+imgsrc+"'>"
		embed += "<param name=~quality~ value=~high~ />"
		embed += "<param name=~bgcolor~ value=~#ffffff~ />"
		embed += "<embed src=~/imgviewer2.swf?"+imgsrc+"~ quality=~high~ bgcolor=~#ffffff~ width=~400~ height=~410~ name=~container~ align=~middle~ allowScriptAccess=~sameDomain~ type=~application/x-shockwave-flash~ pluginspage=~http://www.macromedia.com/go/getflashplayer~ />"
    embed += "</object><br /><br />"
		
		embed += "<a href='#' onclick=~zoominout('out');~ ><img src=~"+dotdotdash+"images/zoom-out.gif~ alt=~zoom out~></a>"
	
	
	
	writetopage('bigimg',embed)
	}
/////////////////////////////////////product zoomer



/*************************productOptions*/

function getprodoptions(theseoptions,productID,pID,optionids,textbox,optiontype,notavailablea)
{
	document.getElementById("unavailable").value="";
	savewhat = "&";
	optionslist = theseoptions.split("|")
	optionidslist = optionids.split("|")
	
  for (i=0;i<=(optionslist.length)-2;i++) 
  {
  	if (optionslist[i] == "Personalise")
  	{
  		/*if (textbox == True)
  		{
  			alert("personalise : " + textbox)
  			showdiv("personalise_text","block")
  		}
  		else
  		{
  			
  			showdiv("personalise_text","none")
  		}
  		*/
  	}
  	
  	savewhat += optionidslist[i]+"="+document.getElementById(optionidslist[i]).value+"&"
  }
	
	savewhat += "optionslength="+((optionslist.length*1)-1)
	selectedoptionslist = "optionids="+optionids+"&optiontype="+optiontype+"&textbox="+textbox+savewhat+"&productID="+productID+"&pID="+pID+"&productprice="+document.getElementById("productprice").value+"&notavailable="+notavailablea
	//alert(selectedoptionslist)
	ajax('getprodoptions',selectedoptionslist)
}
//////////////////////////////////productOptions*/

function writetopage(whatdiv,writethis)
{
  	writethis = writethis.replace(/~/g, '"');
  	tothis = whatdiv;
		if (document.getElementById(tothis))
		{
  	document.getElementById(tothis).innerHTML = writethis;
		}
		else
		{
		//alert("element "+tothis+" not found!")
		}
}
	
function changeimg(thisimage,tothis)
{
		if (document.getElementById(thisimage)){document.getElementById(thisimage).src=tothis}
		else
		{
			if (parent.document.getElementById(thisimage))	{	parent.document.getElementById(thisimage).src=tothis	}
			else{alert(thisimage + " not found" )}
		}
}


	
function pause(millis) 
{
  var date = new Date();
  var curDate = null;
  
  do { curDate = new Date(); } 
  while(curDate-date < millis);
} 



function showdiv(thisdiv,showhide)
{
thisparent = ""
	
  if (document.getElementById(thisdiv)){thisparent = "" }
  else{if (parent.document.getElementById(thisdiv)){thisparent = "parent."}else{if (top.document.getElementById(thisdiv)){thisparent = "top."}} }
	if (showhide != null && showhide != "")
  	{
		
    	if (eval(thisparent+"document.getElementById('"+thisdiv+"') != null") )
    		{
					if (navigator.userAgent.indexOf("Firefox")!=-1){if(showhide == "block"){showhide = "table-row"}	}
					if (showhide  == "block-block"){showhide = "block"	}eval(thisparent+"document.getElementById('"+thisdiv+"').style.display = '"+showhide+"'")
    		}
    }
  else
    {
      if (eval(thisparent+"document.getElementById('"+thisdiv+"').style.display == 'table-row' "))
      	{
					eval(thisparent+"document.getElementById('"+thisdiv+"').style.display = 'none' ")
      	}
      else
      	{	
				eval(thisparent+"document.getElementById('"+thisdiv+"').style.display = 'table-row' ")
      	}
    }
}

function emptyfield (doingthis, fieldname,defaultvalue)
{
  if (doingthis == "blur")
  {
  	if (document.getElementById(fieldname).value == ""){document.getElementById(fieldname).value = defaultvalue}
  }
  else
  {
  	if (document.getElementById(fieldname).value == defaultvalue){document.getElementById(fieldname).value = ""}
  }

}
	
function urlencode(str) {
str = escape(str);
str = str.replace(' ', '+');
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function check_tick(dothis,savewhat,saveid,tickon, tickoff)
{
 
 if (document.getElementById(savewhat).checked)
 {
 	savethis = "true"
 }
 else
 {
 	savethis = "false"
 }
	
	if (document.getElementById(savewhat).checked)
	{
		ajaxDothis(dothis,savewhat,saveid,savethis)
	}
	else
	{
		ajaxDothis(dothis,savewhat,saveid,savethis)
	}
}

function formatCurrency(num,symbol,chrcode) 
{
  if (num != null)
  {
  	if (symbol == "" || symbol == null){symbol = "\u00A3"}
  
    num = num.toString();
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
		
		if (chrcode == "&euro;")
		{
		
			return (num + '.' + cents + ((sign)?'':'-') + symbol);
		}
		else
		{
    	return (((sign)?'':'-') + symbol + num + '.' + cents);
		}
  }
}

function initprice(pricefrom,priceto,chr,chrcode)
{
		if (document.getElementById("lowpriceval"))
		{
		document.getElementById("lowpriceval").value = formatCurrency(pricefrom,chr,chrcode);
		document.getElementById("highpriceval").value = formatCurrency(priceto,chr,chrcode);
		}
}
	
function updateselected_options(pricefrom,priceto)
{
		pricefrom = document.getElementById("lowpriceval").value
		priceto = document.getElementById("highpriceval").value
		//document.getElementById("lowpricediv").innerHTML = formatCurrency(pricefrom);
		//document.getElementById("highpricediv").innerHTML = formatCurrency(priceto);
		document.getElementById("selected_options").value = "";
		document.getElementById("selected_cats").value = "";
		for (a=1;a<=(document.getElementById("optionno").value)*1;a++)
		{
			if (document.getElementById("option_"+a).checked)
			{
				document.getElementById("selected_options").value = document.getElementById("selected_options").value + "~" + document.getElementById("option_"+a).value
			}
		}
	
		for (a=1;a<=(document.getElementById("subcatno").value)*1;a++)
		{
			if (document.getElementById("subcat_"+a).checked)
			{
				document.getElementById("selected_cats").value = document.getElementById("selected_cats").value + "~" + document.getElementById("subcat_"+a).value
			}
		}
	
	ajax("updateproducts", "cats="+document.getElementById("selected_cats").value+"&pricefrom="+pricefrom+"&priceto="+priceto+"&products="+document.getElementById("productsresult").value+"&options="+document.getElementById("selected_options").value)	
	
	//initprice(pricefrom,priceto)
}
	
function ajax(dothis,vars)
 {
	
	var xmlHttp;
  try
    {    // Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();    
		}
  catch (e)
    {   
		 // Internet Explorer  
		try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e)
      { 
				try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
      catch (e){ alert("Your browser does not support AJAX!");return false;}
			 }
		}
		
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
							var result = xmlHttp.responseText
							//alert(result)
     						if (result.slice(2,12) == "<!DOCTYPE " || result.slice(2,12) == "font face=")
          			{
          				document.write(result)
          				alert("Sorry, there has been an error.")
          			}
          				else
          			{
         					if (dothis == "getprodoptions1"){alert(result)	}
									//		alert(result)
          				eval(result)
          			}
							}
  			}

thisURL = window.location.href
			if(thisURL.indexOf("mrwizard") > 0)
	{
		xmlHttp.open("GET","/4plates/ajax.asp?dothis="+dothis+"&"+vars,true);
	}
	else
	{
		xmlHttp.open("GET","/ajax.asp?dothis="+dothis+"&"+vars,true);
	}
			
  
	xmlHttp.send(null);  
}

