function SETPERS() {
  if (document.details.pers.options[document.details.pers.options.selectedIndex].value > 2)
  {
    if (document.details.ship.options[document.details.ship.options.selectedIndex].value == 1)
    {
      alert('Express shipping is needed for all rush personalization');
      document.details.ship.selectedIndex = 2 ;
    }
  }
}

function CHECKNEEDBY() {
  if (document.details.needbymonth.value != 99 && document.details.needbyday.value != 99 && document.details.needbyyear.value != 9999)
  {
    myneedbyyear = document.details.needbyyear.value ;
    myneedbymonth = document.details.needbymonth.value ;
    myneedbyday = document.details.needbyday.value ;
    needbydatecode = myneedbyyear  ;
    if (myneedbymonth < 10)
    {
    needbydatecode += "0" ;
    }
    needbydatecode += myneedbymonth ;
    if (myneedbyday < 10)
    {
    needbydatecode += "0" ;
    }
    needbydatecode += myneedbyday ;
    expresscutoffdatecode = document.details.expresscutoff.value ;
    rushcutoffdatecode = document.details.rushcutoff.value ;
    urgentcutoffdatecode = document.details.urgentcutoff.value ;
    if (needbydatecode <= urgentcutoffdatecode)
    {
      alert('Due to the short time frame you must place your order by phone. Please call 1-866-KETUBOT today.');
      document.details.needbyyear.selectedIndex = 2 ;
    }
    else
    {
      if (needbydatecode <= rushcutoffdatecode)
      {
        if (document.details.pers.options[document.details.pers.options.selectedIndex].value == 2)
        {
          alert('Rush personalization is needed for this time frame.');
          document.details.pers.selectedIndex = 3 ;
        }
	SETPERS() ;
      }
      if (needbydatecode <= expresscutoffdatecode)
      {
        if (document.details.ship.options[document.details.ship.options.selectedIndex].value == 1)
        {
          alert('Express shipping is needed for all rush orders');
          document.details.ship.selectedIndex = 2 ;
        }
      }
    }
  }
}

function OrderFormValidator() {
  if (document.details.contactname.value.length < 2)
  {
    alert("Please enter a valid contact name.")
    document.details.contactname.focus()
    return false
  }
  if (document.details.contactphone.value.length < 10)
  {
    alert("Please enter a valid contact phone number.")
    document.details.contactphone.focus()
    return false
  }
  if (document.details.contactemail.value.length < 8)
  {
    alert("Please enter a valid contact email address.")
    document.details.contactemail.focus()
    return false
  }
}

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_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_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.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];}
}

function addToCart()
{
	SETPERS() ;
	var details = document.details;
	var cart = document.cart;
	if (details.size.value == '')
	{
		alert('The required size must be selected.');
		details.size.focus();
	}
	else if (details.text.value == '')
	{
		alert('A text must be selected.');
		details.text.focus();
	}
	else if (details.needbymonth.value == '')
	{
		alert('A need by month must be selected.');
		details.needbymonth.focus();
	}
	else if (details.needbyday.value == '')
	{
		alert('A need by day must be selected.');
		details.needbyday.focus();
	}
	else if (details.needbyyear.value == '')
	{
		alert('A need by year must be selected.');
		details.needbyyear.focus();
	}
	else if (details.ship.value == '')
	{
		alert('A shipping method must be selected.');
		details.ship.focus();
	}
	else
	{
		SETPERS() ;
		// Looks like it is ready to add to the cart
		cart.ketid.value = details.ketid.value;
		cart.colour.value = details.colour.value;
		cart.custom.value = details.custom.value;
		cart.text.value = details.text.value;
		cart.pers.value = details.pers.value;
		cart.size.value = details.size.value;
		cart.ship.value = details.ship.value;
		cart.submit();
	}
}
