/**********
 * This function accepts two parameters:
 *   available - which is an integer representing the available stock,
 *   theform - which is a reference to a form object, containing an input object "quantity" which is validated
 *             against the available stock.
 **********/
function helpWindow(topic, additional_query) {
  var link_url = "http://www.chibuku.com/shop/help.php?topic="+topic;
  if (additional_query.length>0) {
    link_url+="&"+additional_query;
  }
  window.open(link_url, "help", "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=300,height=500,left=50,top=50");
}

function quantityIsValid(available, stock_field, prodref, zeroallowed) {
  var mesg = '';
  var check = stock_field.value;
  if (check.length==0) { mesg = 'Please enter the quantity you require.';}
  if ( (zeroallowed==0) && (check==0) ) { mesg = 'You must purchase at least 1!';}
  if (check>available) { 
    helpWindow("allocation", "productref="+stock_field.value+"&quantity="+check+"&prod_string="+prodref);
    return false;
  }
  if (isNaN(check)) { mesg = 'Please enter a number for the quantity you require.';}
  
  if (mesg.length==0) {return true;} else { alert(mesg);return false;}
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

function checkoutIsValid(theform) {
  var mesg = '';
  var emailreg = /^[^@]+@[^@.]+\.[^@]*\w\w$/

  if (theform.bill_first_name.value.length==0) { mesg += 'Name\n';}
  if (theform.bill_last_name.value.length==0) { mesg += 'Name\n';}
  if (theform.bill_tel.value.length==0) { mesg += 'Telephone\n';}
  if (!emailreg.test(trim(theform.bill_email.value))) { mesg += 'Email - invalid\n';}
  if (theform.bill_addr_1.value.length==0) { mesg += 'Address line 1\n';}
  if (theform.bill_city.value.length==0) { mesg += 'City\n';}
  if (theform.bill_state.selectedIndex==0) { mesg += 'County\n';}
  if (theform.bill_country.selectedIndex==0) { mesg += 'Country\n';}
  if (theform.bill_post_code.value.length==0) { mesg += 'Post code\n';}
 
  if (mesg.length==0) {
    return true;
  }
  else { 
    alert('You forgot to complete:\n\n'+mesg);
    return false;
  }
}

function allocationRequestIsValid(theform) {
  var mesg = '';
  if (theform.name.value.length==0) { mesg += 'Name\n'; }
  if (theform.email.value.length==0) { mesg += 'Email address\n'; }
  if (theform.telephone.value.length==0) { mesg += 'Telephone\n'; }
  if (theform.desired_quantity.value.length==0) { mesg += 'Desired quantity\n'; }
  
  if (mesg.length==0) {
    return true;
  }
  else {
    alert('Please complete:\n\n'+mesg);
    return false;
  }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

