function showthehelpdiv(divname){
  document.getElementById(divname).style.display = '';
  document.getElementById(divname).style.visibility = 'visible';
}
function hidethehelpdiv(divname){
  document.getElementById(divname).style.display = 'none';
  document.getElementById(divname).style.visibility = 'hidden';
}
function CheckNumeric(e)
{
  var key //= (window.event) ? event.keyCode : e.which;
  if (window.event)
    key = event.keyCode
  else
    key = e.which

  // Was key that was pressed a numeric character (0-9) or backspace (8)?
  if ( key > 47 && key < 58 || key == 8 || key > 95 && key < 106 || key == 9)
    return; // if so, do nothing
  else // otherwise, discard character
 
    if (window.event) //IE
      window.event.returnValue = null;
    else //Firefox
      e.preventDefault();
}

function setadditional(theobj){
  var tmp = theobj.value;
  var thediv = document.getElementById('coverpricediv');
  var thefield = document.getElementById('coverprice');

  var tmp2 = tmp/100;
  var tmpprice=((35>tmp2)?35:tmp2);

  if(document.getElementById('addyes').checked == true){
    tmpprice = ((tmpprice>0)?tmpprice:35);
  tmpprice = 35;
  } else { tmpprice = 0; }


  thediv.innerHTML = "<b>&"+"pound; "+number_format(tmpprice,2,'.',',')+"</b>";
  thefield.value = tmpprice;
}
function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}
function change_additional(thevalue){
  var thediv = document.getElementById('coverpricediv');
  if(thevalue=='yes'){
    thediv.style.display = 'block';
  } else {
    thediv.style.display = 'none';
  }
}
function checkEMail(formName, fieldName)
		{
			var str = eval("document.forms."+formName+"."+fieldName+".value");
			re = new RegExp("","i");
			str=str.replace(/^\s*/,"");//????? ?????? ???????
			str=str.replace(/\s*$/,"");//????? ????????? ???????
			
			//check format validity
			re = new RegExp("","i");
			re=/^[a-zA-Z0-9]([\w.\-]*)@(([\w\-]+\.)+)([a-zA-Z]{2,4})$/;
			if (re.test(str)||!str.length)//????????? ???????????? ??????? e-mail ??????
				{
				 return true;
				}
			else
				{
				 alert('Incorrect email address!');
				 eval("document.forms."+formName+"."+fieldName+".focus()");
				 return false;
				}
		}


			
function checkForm()
	{//alert(document.forms.CollectionAddress.value);
	args=checkForm.arguments;
	fn=args[0]; 
	for(i=1;i<args.length;i++)
		{
		//str=document.forms(fn).elements(args[i]).value;
		str=eval("document.forms."+fn+"."+args[i]+".value");
		str=str.replace(/^\s*/,"");
		str=str.replace(/\s*$/,"");
		if (str=="")
			{
			alert("Please fill in the fields marked with an asterisk!");
			return false;
			}
		if ((args[i]=='Email')&&(!checkEMail(fn,args[i])))
			{
			return false;
			}
		}
		return true;
	}

function checkaddress(){
  var theForm = document.forms.theform;
  var fieldlist = ["addressfname","addresssname","addresshometel","addressaddress1","addresstown","addresscounty","addresspostcode","addresscountry"];
  var fielderrorlist = ["First Name","Last Name","Home Telephone","Address","Town","County","Post Code","Country"];
  for(var i=0;i<fieldlist.length;i++){
    if(theForm[fieldlist[i]].value.length < 3){
      alert('Please ensure you have filled in `'+fielderrorlist[i]+'` correctly.');
      theForm[fieldlist[i]].focus();
      return false;
    }
  }
}
function choseaid(theobj){
  window.location = 'index.php?p=addressbook&aid='.theobj.options[theobj.selectedIndex].value;
}
function checkregister(){
  var theForm = document.forms.theform;
  if(theForm['userpassword'].value != theForm['userpasswordconfirm'].value){
    alert('Please ensure both passwords are the same.');
    theForm['userpassword'].focus();
    return false;
  }
  var fieldlist = ["useremail","userpassword","userpasswordconfirm","userfname","usersname","userhometel","useraddress1","usertown","usercounty","userpostcode","usercountry"];
  var fielderrorlist = ["Email Address","Password","Confirmed Password","First Name","Last Name","Home Telephone","Address","Town","County","Post Code","Country"];
  for(var i=0;i<fieldlist.length;i++){
    if(theForm[fieldlist[i]].value.length < 3){
      alert('Please ensure you have filled in `'+fielderrorlist[i]+'` correctly.');
      theForm[fieldlist[i]].focus();
      return false;
    }
  }
}
function getVolume(thei){
  var theForm = document.forms.homeform;
  var qty = theForm.qty.value;
  var totalvolumetric=0;
  var volumetricweight=0;
  
  eval("var tmp = [theForm.weightselect_"+thei+",theForm.lengthselect_"+thei+",theForm.widthselect_"+thei+",theForm.heightselect_"+thei+"];");
  eval("var weight = tmp[0].options[tmp[0].selectedIndex].value;")
  eval("var length = tmp[1].options[tmp[1].selectedIndex].value;")
  eval("var width = tmp[2].options[tmp[2].selectedIndex].value;")
  eval("var height = tmp[3].options[tmp[3].selectedIndex].value;")
  volumetricweight = (length*width*height)/6000;
  tmp = Math.pow(10,1);
  volumetricweight = (Math.ceil(volumetricweight*tmp)) / tmp;
  if(volumetricweight > weight){
    eval("theForm.volume_"+thei+".value = volumetricweight/1;");
  } else {
    eval("theForm.volume_"+thei+".value = weight/1;");
  }
}
function copyall(){
  var theForm = document.forms.homeform;
  var tmps = [theForm.weightselect_0.selectedIndex,theForm.lengthselect_0.selectedIndex,theForm.widthselect_0.selectedIndex,theForm.heightselect_0.selectedIndex,theForm.volume_0.value];
  var fieldlist = ["weightselect","lengthselect","widthselect","heightselect"];
  var qty = theForm.qty.value;
  if(qty>1){
    for(var i=1;i<qty;i++){
      eval("theForm.weightselect_"+i+".selectedIndex = tmps[0];");
      eval("theForm.lengthselect_"+i+".selectedIndex = tmps[1];");
      eval("theForm.widthselect_"+i+".selectedIndex = tmps[2];");
      eval("theForm.heightselect_"+i+".selectedIndex = tmps[3];");
      eval("theForm.volume_"+i+".value = tmps[4];"); 
    }
  }
}
function gotoservice(thei){
  if(document.getElementById('tc').checked != true){
    alert('You must agree to the Terms & Conditions.');
    document.getElementById('tc').focus();
    return false;
  } else {
    document.location='index.php?p=step3&sid='+thei;
    return true;
  }
}
function getordinal(thedate){
  if(typeof thedate == "number"){
    return (
      (thedate % 10 == 1 && thedate % 100 != 11)? 'st':
      (thedate % 10 == 2 && thedate % 100 != 12)? 'nd':
      (thedate % 10 == 3 && thedate % 100 != 13)? 'rd' : 'th');
  }
}
function checkhome(){
  var theForm = document.forms.homeform;
  var fieldlist = ["countryselect","packagesselect"];
  var fielderrorlist = ["Destination","Number of Packages"];
  for(var i=0;i<fieldlist.length;i++){
    if(theForm[fieldlist[i]].value == ''){
      alert('Please ensure you have filled in `'+fielderrorlist[i]+'` correctly.');
      theForm[fieldlist[i]].focus();
      return false;
    }
  }
  var qty = theForm.packagesselect.value;
  var fieldlist2 = ["weightselect","lengthselect","widthselect","heightselect"];
  var fielderrorlist2 = ["Weight","Length","Width","Height"];
  for(var i=0;i<qty;i++){
    for(var j=0;j<fieldlist2.length;j++){
      eval("tmp = '"+fieldlist2[j]+"_"+i+"'");
      if(theForm[tmp].value == ''){
        alert('Please ensure you have filled in `'+fielderrorlist2[j]+'` correctly.');
        theForm[tmp].focus();
        return false;
      }
    }
  }
}
function checkdelivery(){
  var theForm = document.forms.thedelivery;
  var fieldlist = ["deliverycontact","pickupaddress1","pickuptown","pickupcounty","pickuppostcode","deliverytelephone","deliverydescription","deliveryvalue"];
  var fielderrorlist = ["Contact Name","Address","Town","County","Post Code","Telephone","Description of Contents","Value of Contents"];
  for(var i=0;i<fieldlist.length;i++){
    if(fieldlist[i] == 'pickuppostcode'){  
      if(!checkPostCode(theForm[fieldlist[i]].value)){
        alert("Postcode has invalid format")
        theForm[fieldlist[i]].focus();
        return false;
      }
    } else if(fieldlist[i] == 'deliveryvalue'){
      if(theForm[fieldlist[i]].value.length < 1){
        alert('Please ensure you have filled in `'+fielderrorlist[i]+'` correctly.');
        theForm[fieldlist[i]].focus();
        return false;
      }
    } else {
      if(theForm[fieldlist[i]].value.length < 3){
        alert('Please ensure you have filled in `'+fielderrorlist[i]+'` correctly.');
        theForm[fieldlist[i]].focus();
        return false;
      }
    }
  }
}
function checkpickup(){
  var theForm = document.forms.thepickup;
  var fieldlist = ["pickupcontact","pickupemail","pickupaddress1","pickuptown","pickupcounty","pickuppostcode","pickuptelephone"];
  var fielderrorlist = ["Contact Name","Your Email","Address","Town","County","Post Code","Telephone"];
  var length=0;
  for(var i=0;i<fieldlist.length;i++){
    if(fieldlist[i]=='pickupemail')
      length = 10;
    else
      length = 3;
    if(theForm[fieldlist[i]].value.length < length){
      alert('Please ensure you have filled in `'+fielderrorlist[i]+'` correctly.');
      theForm[fieldlist[i]].focus();
      return false;
    }
   /* if(fieldlist[i]=='pickupemail' && theForm['signmeup'].value=='yes'){
      if(theForm['password'].value.length < 3){
        alert('Please ensure Password is entered!');
        theForm['password'].focus();
        return false;
        break;
      }
    }*/
  }
  return checkEMail('thepickup','pickupemail');
}
function showservice(sid){
  try {
    document.getElementById('mainbox').innerHTML = document.getElementById('div_'+sid).innerHTML;
  } catch(e) {
    document.getElementById('mainbox').innerHTML = document.getElementById('blank').innerHTML;
  }
}

function removefrombasket(basketid)
{
window.location.href='eshop/ecom_data.php?dw=removefrombasket&basketid=' + basketid;
}


var _w = null;
function createNew(thispic) {
mypic = thispic;
  _w = window.open();
  _w.document.open();
_w.document.write('<html><head><title>Full Image Preview</title></head><body><a href="javascript:window.close();">Close Window</a><br>');
  _w.document.write('<img src = "');
  _w.document.write (mypic);
  _w.document.write ('">');
  
  _w.document.write('</body></html>');
  _w.document.close();
}

function LargePic(pic, x, y, prodid) {
  if (!x) x=800;
  if (!y) y=600;
  var settings="location=no,scrollbars=no,status=no,resizeable=no,top=30,left=100,width="+x+",height="+y;
  window.open("/eshop/ecom_largepic.php?pic="+pic+"&productid="+prodid, "", settings);
}


function chkcheckout1()
{
fieldlist=new Array();
fielderrorlist=new Array();

fieldlist = ["clientfname","clientsname","clientaddress1","clienttown","clientpostcode","clientemail"];
fielderrorlist = ["First Name","Surname","Address","Town","Postcode","Email"];

for (i=0; i < fieldlist.length; i++){
formitem=fieldlist[i];
if (document.forms.theform[formitem].value.length<3){
alert("Please ensure " + fielderrorlist[i] + " is entered!");
document.forms.theform[formitem].focus();
return false;
break;
}
}
}
function countryselect(thisone)
{
  var inc=document.searchbox.searchcountry.selectedIndex;
  var itemc=document.searchbox.searchcountry.options[inc].value;
parent.location.href=("index.php?p=<?php echo $p?>&opt=getcountys&searchcountry=" + itemc);
}
function countyselect(thisone)
{
  var inc=document.searchbox.searchcountry.selectedIndex;
  var itemc=document.searchbox.searchcountry.options[inc].value;
  var ind=document.searchbox.searchcounty.selectedIndex;
  var item=document.searchbox.searchcounty.options[ind].value;
if(item != ''){
parent.location.href=("index.php?p=<?php echo $p?>&op=gettowns&searchcounty=" + item + "&searchcountry=" + itemc );
}
}
function checkPostCode (toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}