// JavaScript Document
function round(number,X) {
// rounds number to X decimal places, defaults to 2
X = (!X ? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function setprice(state) {
	if (isNaN(document.order.pottyMitts.value) || document.order.pottyMitts.value == '' || document.order.pottyMitts.value == ' '){
		document.order.pottyMitts.value = 0
	}
	if (isNaN(document.order.macAndCool.value) || document.order.macAndCool.value == '' || document.order.macAndCool.value == ' '){
		document.order.macAndCool.value = 0
	}
	if (isNaN(document.order.snackTrap.value) || document.order.snackTrap.value == '' || document.order.snackTrap.value == ' '){
		document.order.snackTrap.value = 0
	}
	if (isNaN(document.order.drinkDeputy.value) || document.order.drinkDeputy.value == '' || document.order.drinkDeputy.value == ' '){
		document.order.drinkDeputy.value = 0
	}
	var total = ((eval(document.order.macAndCool.value))+(eval(document.order.snackTrap.value))+(eval(document.order.drinkDeputy.value))+(eval(document.order.pottyMitts.value)))
	if(total == 0){
		alert("You haven't placed an order yet, please try again");
	}
	if (document.order.state[0].selected == true) {
		if(!state){
			alert("You must select a state before calculating a total");
		}
	} else { 
	//figure shipping based on total
		/*if (total > 3 ) {
			document.order.shipping.value = "0"
		} else if (total < 4 ) {*/
			document.order.shipping.value = 3.95
		//}
		
		//figure subtotal
		document.order.subtotal.value = ((5.99 * eval(document.order.macAndCool.value)) + (4.99 * eval(document.order.snackTrap.value)) + (9.99 * eval(document.order.drinkDeputy.value))+ (4.59 * eval(document.order.pottyMitts.value)))
		document.order.subtotal.value = round(eval(document.order.subtotal.value),2)
		if (document.order.state[3].selected == true) { // if arizona add tax
			document.order.tax.value = round(eval(document.order.subtotal.value) * .08,2);
			document.order.total.value = round(eval(document.order.subtotal.value) + eval(document.order.shipping.value) + eval(document.order.tax.value),2);
		} else { // if not arizona add NO tax
			document.order.tax.value = "0";
			document.order.total.value = round(eval(document.order.subtotal.value) + eval(document.order.shipping.value),2);
		}
		document.order.total.value = document.order.total.value;
	}
}

function getRandomNum(lbound, ubound) {
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
function shipnum() {
var rc = "";
var charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//GENERATE first as number
rc = rc + charSet.charAt(getRandomNum(0, 10))
for (var idx = 1; idx < 5; ++idx) { // now generate next 4 as anything
rc = rc + charSet.charAt(getRandomNum(0, charSet.length))
}
document.order.ordernumber.value = rc;
return rc;

}


// -->

function YY_CalculateForm() { //v3.01
//copyright (c)1999-2000 Yaromat.com
  var args=YY_CalculateForm.arguments;
  var myResult=""; var myField="0";
  if (document.all){eval("args[0]=args[0].replace(/.layers/gi, '.all');");}
  for (var i=3; i<args.length; i++){
    if(args[i].charAt(0)!="#"){
      myResult=myResult+args[i];
    }else{
      myField=eval(args[0]+'.'+ args[i].substring(1)+'.value'); if(myField=="")myField="0";
      myResult=myResult+myField;
    }
  }
  with (Math) {
    var myExpo = pow(10, args[2]);
    var myStr = round(myExpo*eval(myResult))/myExpo;
  }
  eval(args[0]+'.'+args[1]+'.value='+myStr);
}

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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}