var tbll;
var xhrq = null;
var nxtact = null;
 function bgpos(ob,ht) {
  ob.style.backgroundPosition='0% ' + (ht*-1) + 'px';
 }
 function bgp86() {
  bgpos(d_ge('h_car1'),86);
 }
 function bgp0() {
  bgpos(d_ge('h_car1'),0);
 }
 function brow() {
  dh_dia(300,'v','Browse subjects','',false,false,'browse.php',360);
 }
 function viewcart(mode) {
   if (mode=='x')
    dh_dia(600,'u','Shopping cart','',false,false,'viewcart.php?m=1',360);
   else
    dh_dia(600,'u','Shopping cart','',false,false,'viewcart.php',360);
 }
 function empcart() {
   if (!xhrq)
    xhrq = new Xhrwrap();
  xhrq.post('cartfuncs.php?fn=0','',empcart2);
 }
 function empcart2() {
  drawcart(0);
  dh_can();
  xhrq = null;
   if (nxtact || window.location.href.match(/checkout/))
    window.location.href = 'index.php';
 }
 function drawcart(noitems) {
  var cart = d_ge('h_car1');
   if (cart) {
     if (noitems && noitems!='0')
      cart.innerHTML = 'View cart ('+noitems+')';
	 else
	  d_rm('h_car1'); 
   } else {
    var div = d_ge('h_cart');
    cart = d_mk('div','','View cart ('+noitems+')','',{'id':'h_car1'});
	cart.onmouseover=bgp86;
	cart.onmouseout=bgp0;
	cart.onclick=viewcart;
    var sib = d_ge('h_acct');
     if (!sib)
      sib = d_ge('status');
    div.insertBefore(cart,sib);
   }	
 }
 function login(cde) {
   if (cde)
    nxtact = 1;
  tbll = dh_dia(240,'l','Log in','',[['Username','uname',14,3,{'cols':20,'maxlen':20}],['Password','pword',28,3,{'cols':20,'maxlen':20}]],[['Enter','login2'],['Cancel','dh_can'],['Help','gethelp'],['Forgotten your password?','forget'],['Register','regis']]);
 }
 function login2(){	
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('login.php',fm,login3);
 }
 function login3(rtxt) {
   if (rtxt.match(/OK/)) {
     if (nxtact)
	  viewcart('x');
	 else	// Hmmm....
      window.location.href=window.location.href;
   } else {	
    dh_can();
    d_tt('status',rtxt,'error',6000);
   }
  xhrq = null;
  nxtact = null;
 }
 function gethelp() {
 }
 function forget() {
  dh_can();
  tbll = dh_dia(240,'l','Forgotten password','For security, please enter your username and the e-mail address under which you registered',[['Username','uname',14,3,{'cols':20,'maxlen':20}],['E-mail','email',20,3,{'cols':20,'maxlen':100}]],[['Enter','forget2'],['Cancel','dh_can'],['Help','gethelp']]);
 }
 function forget2() {
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('forget.php',fm,forget3);
 }
 function forget3(rtxt) {
  dh_can();
   if (rtxt.match(/@/))
    alert("Your password has been reset.\nAn e-mail with your new password has been sent to\n"+rtxt);
   else	
    d_tt('status',rtxt,'error',6000);
 }
 function regis(nxt,tmp) {
  dh_can();
   if (nxt==1)
	nxtact = 1;
   if (tmp==1) {
    tbll = dh_dia(360,'l','Register','This creates a temporary account, valid for this purchase only. If you would like to receive information from us about new ICC publications and discounts then please tick the ‘Opt In’ box below.',[['Your full name','propername',14,3,{'cols':30,'maxlen':50}],['Organisation','orgname',14,1,{'cols':30,'maxlen':50}],['E-mail','email',20,3,{'cols':30,'maxlen':100}],['ICC UK member','member',40,1,{}],['Opt in','optin',40,1,{}]],[['Enter','regis2a'],['Cancel','dh_can'],['Help','gethelp']]);
   } else
    tbll = dh_dia(360,'l','Register','This creates a permanent account which you can use to log in to the bookshop in the future. If you would like to receive information from us about new ICC publications and discounts then please tick the ‘Opt In’ box below.',[['Your full name','propername',14,3,{'cols':30,'maxlen':50}],['Organisation','orgname',14,1,{'cols':30,'maxlen':50}],['E-mail','email',20,3,{'cols':30,'maxlen':100}],['User name (for log in)','username',12,3,{'cols':20,'maxlen':30}],['Password','password',28,3,{'cols':10,'maxlen':30}],['(confirm password)','password2',28,3,{'cols':10,'maxlen':30}],['ICC UK member','member',40,1,{}],['Opt in','optin',40,1,{}]],[['Enter','regis2'],['Cancel','dh_can'],['Help','gethelp']]);
 }
 function regis2() {
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('register.php',fm,regis3);
 }
 function regis3(rtxt) {
   if (rtxt.match(/OK/)) {
     if (nxtact)
	  getadd();
	 else 
      window.location.href='myacct.php?new=1';
   } else {	
    var di = d_ge('dia');
    var tp = di.firstChild.nextSibling;
    tp.innerHTML = '<span class="error">' + rtxt + '</span>';
   }
  xhrq = null;
 }
 function regis2a() {
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('register.php?tmp',fm,regis3a);
 }
 function regis3a(rtxt) {
   if (rtxt.match(/OK/)) {
    getadd();
   } else {	
    dh_can();
    d_tt('status',rtxt,'error',6000);
   }
  xhrq = null;
 }
 function getadd(btns,ttl) {
   if (!btns)
    btns = [['Enter as shipping address','add2s'],['Enter as billing address','add2b'],['Enter as both','add22']];
   if (!ttl)
    ttl = 'Address';	
  btns[btns.length] = ['Cancel','dh_can'];
  btns[btns.length] = ['Help','gethelp'];
  tbll = dh_dia(360,'l',ttl,'In the top field, please enter your address details as they should appear for postal purposes. Please also select a country.',[['Address','ad_fld',31,3,{'cols':40,'rows':5}],['Country','ctry_id',43,3,{'table':'country','fld':'ctry_name','script':'h43.php?c=1'}],['Telephone','phone',14,3,{'cols':30,'maxlen':50}],['Telefax','fax',14,1,{'cols':30,'maxlen':50}]],btns);
 }
 function add22() {
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('addfuncs.php?fn=3',fm,add3b);
 }
 function add2b() {
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('addfuncs.php?fn=1',fm,add3b);
 }
 function add3b(rtxt) {
   if (rtxt.match(/OK/)) {
     if (nxtact) {
      nxtact = null;
      viewcart('x');
	   } else
	    dh_can(); 
   } else if (rtxt.match(/VAT/)) {
    tbll = dh_dia(300,'l','VAT number','As your billing address is in the EU outside the UK, we must charge VAT on your purchases unless you have a valid VAT number. If you do, please enter it below.',[['VAT reg no','vatno',13,3,{'cols':20,'maxlen':20}]],[['Enter my VAT number','entvat'],['I don\'t have a VAT number','novat']]);
   } else if (rtxt.match(/GSA/)) {
    btns = [['Enter as shipping address','add2s']];
	  getadd(btns, 'Shipping address');
   } else {	
    dh_can();
    d_tt('status',rtxt,'error',6000);
   }
  xhrq = null;
 }
 function entvat() {
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('addfuncs.php?fn=2',fm,add3b);
 }
 function novat() {
   if (!xhrq)
    xhrq = new Xhrwrap();
  xhrq.post('addfuncs.php?fn=2','',add3b);
 }
 function add2s() {
  var fm = prep2(tbll);
   if (fm)
    xhrq.post('addfuncs.php?fn=0',fm,add3s);
 }
 function add3s(rtxt) {
   if (rtxt.match(/OK/)) {
     if (nxtact) {
      nxtact = null;
      viewcart('x');
	 } else
	  dh_can(); 
   } else if (rtxt.match(/GBA/)) {
    btns = [['Enter as billing address','add2b']];
	getadd(btns, 'Billing address');
   } else {	
    dh_can();
    d_tt('status',rtxt,'error',6000);
   }
  xhrq = null;
 }
 function prep2(tbl) {
   if (!xhrq)
    xhrq = new Xhrwrap();
  var fm = tbl.fmt.mkfm(tbl.tb);
   if (!fm)
    alert('Some fields incorrectly completed.\nMouse over fields to see errors!');
  return fm;	
 }
 function stprob(sp,tn) {
  btns = [['Wait for availability','stpro0'],['Cancel order','stpro1a'],['Close window','dh_can']]; 
   if (tn==1) {
    txt = 'The item you are ordering is not in stock. Would you prefer to wait until it is available, or cancel the order?';
   } else {	
    txt = 'The items you are ordering are not in stock. Would you prefer to wait until they become available, or cancel the order?'; 	
     if (sp==1) {
      btns = [['Wait for all items','stpro0'],['Make two orders','stpro2'],['Skip unavailable items','stpro1'],['Close window','dh_can']];
      txt = 'Some of the items you are ordering are not in stock. Would you prefer to wait until all become available, get available items immediately and others later, or cancel the order?'; 	
	 }  
   }	  
  tbll = dh_dia(300,'l','Out of stock',txt,[],btns);
 }
 function stpro0() {
   if (!xhrq)
    xhrq = new Xhrwrap();
  xhrq.post('cartfuncs.php?fn=3','',stpro3);
 }
 function stpro1() {
   if (!xhrq)
    xhrq = new Xhrwrap();
  xhrq.post('cartfuncs.php?fn=4','',stpro3);
 }
 function stpro1a() {
   if (!xhrq)
    xhrq = new Xhrwrap();
  xhrq.post('cartfuncs.php?fn=0','','');
  window.location.href='index.php';
 }
 function stpro2() {
   if (!xhrq)
    xhrq = new Xhrwrap();
  xhrq.post('cartfuncs.php?fn=5','',stpro3);
 }
 function stpro3(rtxt) {
  xhrq = null;  
   if (rtxt && rtxt!="0")
    window.location.href = 'checkout.php';
   else	{
    drawcart(0);
    dh_can();
   }	
 }