function SR_isValidNum(passedVal) {
  if (passedVal == "") {
      return false;
    }
    for (i=0; i<passedVal.length; i++) {
      if (((passedVal.charAt(i) >= "0") || (passedVal.charAt(i) <= "9")) && (!passedVal.charAt(i) == ".")) {
        return false;
        }
    }
  if (passedVal <= 0) {
      return false;
    }
    return true;
}


function ConvUnits(val,units) {

  if (units==1) return val;        //metres
  if (units==2) return val/100;    //centimetres
  if (units==3) return val/1000;    //millimetres
  if (units==4) return Math.round(val*0.9144*100)/100;    //yards
  if (units==5) return Math.round(val*0.3048*100)/100;    //feet
  if (units==6) return Math.round(val*0.0254*100)/100;    //inches
	
	return 0;
}


function calc(theForm) {

  if ((!SR_isValidNum(theForm.lengthval.value)) || (theForm.lengthval.value.length = 0) || (theForm.lengthval.value <= 0))
  {
    alert("Please enter a valid value in the \"Length\" field.");
    theForm.lengthval.focus();
    return (false);
  }
  if ((!SR_isValidNum(theForm.widthval.value)) || (theForm.widthval.value.length = 0) || (theForm.widthval.value <= 0))
  {
    alert("Please enter a valid value in the \"Width\" field.");
    theForm.widthval.focus();
    return (false);
  }
  if ((!SR_isValidNum(theForm.depthval.value)) || (theForm.depthval.value.length = 0) || (theForm.depthval.value <= 0))
  {
    alert("Please enter a valid value in the \"Depth\" field.");
    theForm.depthval.focus();
    return (false);
  }
	
  var lengthvalm=ConvUnits(theForm.lengthval.value,theForm.lengthunits.value);
  if (lengthvalm == 0)
  {
    alert("Please select units for the \"Length\" field.");
    theForm.lengthunits.focus();
    return (false);
  }
  var widthvalm=ConvUnits(theForm.widthval.value,theForm.widthunits.value);
  if (widthvalm == 0)
  {
    alert("Please select units for the \"Width\" field.");
    theForm.widthunits.focus();
    return (false);
  }
  var depthvalm=ConvUnits(theForm.depthval.value,theForm.depthunits.value);
  if (depthvalm == 0)
  {
    alert("Please select units for the \"Depth\" field.");
    theForm.depthunits.focus();
    return (false);
  }

	theForm.volumeval.value = Math.round(((lengthvalm*widthvalm*depthvalm)*100)/100)*1.1 +" cubic metres"
        checkCookie(theForm.volumeval.value);

}		


function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
} 


function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function deleteCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())

}



function checkCookie(amountval)
{
amount=getCookie('amount')
if (amount!=null && amount!="")
  {deleteCookie('amount',amount,-2)}
else 
  {
  amount=amountval
  if (amount!=null && amount!="")
    {
    setCookie('amount',amount,1)
    }
  }
}	



function checkfirst()
{
amount=getCookie('amount')
if (amount!=null && amount!="")
  {deleteCookie('amount',amount,-2)}
}						