// Support Script (776)
function AddToValidateArray(strElementName)
{
    var strName = strElementName

    if (!document.ValidateArray) 
    {
        document.ValidateArray = new Array
    }

    document.ValidateArray[document.ValidateArray.length] = strName
}

// Support Script (655)
function ValidateNonBlank()
{
  var msg = "";
  var val = this.getText();  

  if (StripChars(" \n\t\r",val).length == 0)
  {
    if (Trim(this.ErrorMsg) != "")
      msg = "Required field. " + this.ErrorMsg
    else
      msg = "Required field. Please enter an appropriate value."
  }

  return msg;
}

// Support Script (800)
function StripChars(theFilter,theString)
{
	var strOut,i,curChar

	strOut = ""
	for (i=0;i < theString.length; i++)
	{		
		curChar = theString.charAt(i)
		if (theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar		
	}	
	return strOut
}

function AllInRange(x,y,theString)
{
	var i, curChar
	
	for (i=0; i < theString.length; i++)
	{
		curChar = theString.charAt(i)
		if (curChar < x || curChar > y) //the char is not in range
			return false
	}
	return true
}


function reformat (s)
{
    var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) 
           resultString += arg;
       else 
       {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function Trim(theString)
{
 var i,firstNonWhite

 if (StripChars(" \n\r\t",theString).length == 0 ) return ""

	i = -1
	while (1)
	{
		i++
		if (theString.charAt(i) != " ")
			break	
	}
	firstNonWhite = i
	//Count the spaces at the end
	i = theString.length
	while (1)
	{
		i--
		if (theString.charAt(i) != " ")
			break	
	}	

	return theString.substring(firstNonWhite,i + 1)

}
// Support Script (810)
function subAwithBinC(a,b,c)
{

	var i = c.indexOf(a);
	var l = b.length;

	while (i != -1)	{
		c = c.substring(0,i) + b + c.substring(i + a.length,c.length);
  i += l
		i = c.indexOf(a,i);
	}
	return c;

}
// Support Script (770)
function Validate(stopOnFailure)
{
	var ErrorMsg = "";
	var i
	var msg
	var tofocus = true;
	var ErrorMsg = "";
	
	// Go through the Validate Array that may or may not exist
	// and call the Validate function for all elements that have one.
	if (document.ValidateArray)
	{
		for (i = 0; i < document.ValidateArray.length; i ++)
		{
			msg = eval( document.ValidateArray[i] + ".Validate()")
			if (msg != "")
			{
				ErrorMsg += "\n\n" + document.ValidateArray[i] + ":  " + msg;
				if (tofocus) 
				{
					eval(document.ValidateArray[i] + ".focus()")
					tofocus = false;
				}
				
				if (stopOnFailure == "1") return ErrorMsg;
			}
  	}
  }
	return ErrorMsg;
}

function document_onLoad() {
if (String(shippingCookie.getValue("zipcookie"))!="null")
  zip3txt.setText(shippingCookie.getValue("zipcookie"));
if (String(shippingCookie.getValue("statecookie"))!="null")
  statelist.setSelectedByText(shippingCookie.getValue("statecookie"));
if (String(shippingCookie.getValue("wayofshippingcookie"))!="null")
  shiplist.setSelectedByText(shippingCookie.getValue("wayofshippingcookie"));
grandtot.Validate=ValidateNonBlank;
grandtot.ErrorMsg = "Please ReCalculate Shipping and Grand Total before you Submit Order, Thanks !"
AddToValidateArray("grandtot")
 }
function CalculateShip__onClick() {
shippingCookie.setValue("zipcookie",zip3txt.getText());
OrderSummaryCookie.setValue("SubTotalCookie",subtotalbox.getText());
OrderSummaryCookie.setValue("WayofShippingCookie", shiplist.getSelectedValue());

//shipping cost

wght=weightbox.getText();
upsZone=0;
shipcharge = 99999;
zip3=zip3txt.getText();
zip3=zip3.substring(0,3);
wght =eval(weightbox.getText());
wght=Math.ceil(wght);
box1 = 32;
box2 = (wght % 32);
box32 = 0;
insrate=0.50;
insinitial=1.00;
insinitialbox2=0;
residential=2.1;
residentialbox2=0;
ins = 0.0;
d1airadjustment=1.31;
d2airadjustment=1.20;
d3airadjustment=1.14;
fuelcharge=1.1;
rateadjustment=1.25;
//************ dotots
function dotots(newtot) 
{

sTax = newtot * 0.0975;
if (calcheck.getState() == true)
{

salestax.setText(trunc(sTax));
OrderSummaryCookie.setValue("SalesTaxCookie",trunc(sTax));
gTot = newtot + sTax + eval(shippingdisplaybox.getText());
}

else
{

salestax.setText("0.00");
OrderSummaryCookie.setValue("SalesTaxCookie","0.00");
gTot = newtot+ eval(shippingdisplaybox.getText());
}

grandtot.setText(trunc(gTot));

OrderSummaryCookie.setValue("GrandTotalCookie",trunc(gTot));
}


//***********************
function trunc(thisitem) 
{
thisitem = Math.floor(Math.round(Math.floor(thisitem * 1000)/10))/100;
thisitemstr = thisitem.toString();
len = thisitemstr.length;
dotindex =thisitemstr.indexOf(".",0);

if (dotindex == -1)
{
thisitemstr +=".00";
}
else
if (dotindex == (len-2))
{
thisitemstr +="0";
}
return thisitemstr;
}



//*********** cut
if (wght >= 32)
{
box32 = (wght - box2) / 32;
};


var d1m=[2.2,   2.2,    .86,      1,   1.9,   2.0,   2.05,    2.18,    2.2];
var d1=[35.25, 24.25,  14.25,  16,     19,     20.25,  21.5,   22.25,  22.75];

var d2m=[1.65,   1.65,    0.70,    0.93,    1.15,      1.51,    1.60,    1.65,    1.65];
var d2=[24.2,  13.2,   6.4,    6.7,    7.2,    7.8,    8.3,    8.8,    9.1];

var d3m=[1.65,   1.65,    0.4,    0.5,    0.7,    0.8,    1,      1.1,    1.2];
var d3=[24.2,  13.2,   5.4,    5.6,    5.9,    6.2,    7,      7.4,    7.6];

var gdm=[1.65,   1.65,    0.14,   0.18,   0.23,   0.31,   0.39,   0.47,   0.57];
var gd=[24.2,  13.2,   4.02,   4.12,   4.35,   4.4,    4.59,   4.63,   4.73];


if (statelist.getSelectedValue() == 1)
{
upsZone = 1;
}
else
if (statelist.getSelectedValue() == 2)
{
upsZone=0;
}

else
if (statelist.getSelectedValue() == 3)
{
upsZone=1;
}

else
if  (zip3 >="004" && zip3 <= "005" )
{
upsZone=8;
}
else
if (zip3 >="010"  && zip3 <= "089")
{
upsZone=8;
}

else
if (zip3 >="100"  && zip3 <= "199")
{
upsZone=8;
}

else
if (zip3 >="200"  && zip3 <= "299")
{
upsZone=8;
}

else
if (zip3 >="300"  && zip3 <= "339")
{
upsZone=8;
}

else
if (zip3 >="341"  && zip3 <= "349")
{
upsZone=8;
}

else
if (zip3 >="350"  && zip3 <= "358")
{
upsZone=7;
}

else
if (zip3 >="359"  && zip3 <= "364")
{
upsZone=8;
}

else
if (zip3 >="365"  && zip3 <= "367")
{
upsZone=7;
}

else
if (zip3 =="368")
{
upsZone=8;
}

else
if (zip3 >="369"  && zip3 <= "372")
{
upsZone=7;
}

else
if (zip3 >="373"  && zip3 <= "374")
{
upsZone=8;
}

else
if (zip3 =="375")
{
upsZone=7;
}

else
if (zip3 >="376"  && zip3 <= "379")
{
upsZone=8;
}


else
if (zip3 >="380"  && zip3 <= "384")
{
upsZone=8;
}


else
if (zip3 =="385")
{
upsZone=8;
}


else
if (zip3 >="386"  && zip3 <= "397")
{
upsZone=7;
}


else
if (zip3 =="399")
{
upsZone=8;
}

else
if (zip3 >="400"  && zip3 <= "418")
{
upsZone=8;
}

else
if (zip3 >="420"  && zip3 <= "424")
{
upsZone=7;
}


else
if (zip3 >="425"  && zip3 <= "459")
{
upsZone=8;
}

else
if (zip3 >="460"  && zip3 <= "466")
{
upsZone=7;
}

else
if (zip3 >="467"  && zip3 <= "468")
{
upsZone=8;
}

else
if (zip3 == "469")
{
upsZone=7;
}

else
if (zip3 >="470"  && zip3 <= "473")
{
upsZone=8;
}

else
if (zip3 >="474"  && zip3 <= "479")
{
upsZone=7;
}

else
if (zip3 >="480"  && zip3 <= "497")
{
upsZone=8;
}

else
if (zip3 >="498"  && zip3 <= "499")
{
upsZone=7;
}

else
if (zip3 >="500"  && zip3 <= "504")
{
upsZone=7;
}


else
if (zip3 =="505")
{
upsZone=6;
}

else
if (zip3 >="506"  && zip3 <= "507")
{
upsZone=7;
}

else
if (zip3 =="508")
{
upsZone=6;
}

else
if (zip3 =="509")
{
upsZone=7;
}

else
if (zip3 >="510"  && zip3 <= "516")
{
upsZone=6;
}

else
if (zip3 >="520"  && zip3 <= "560")
{
upsZone=7;
}

else
if (zip3 =="561")
{
upsZone=6;
}

else
if (zip3 >="562"  && zip3 <= "567")
{
upsZone=7;
}

else
if (zip3 >="570"  && zip3 <= "581")
{
upsZone=6;
}

else
if (zip3 =="582")
{
upsZone=7;
}

else
if (zip3 >="583"  && zip3 <= "588")
{
upsZone=6;
}

else
if (zip3 >="590"  && zip3 <= "591")
{
upsZone=5;
}

else
if (zip3 >="592"  && zip3 <= "593")
{
upsZone=8;
}

else
if (zip3 =="594")
{
upsZone=5;
}

else
if (zip3 =="595")
{
upsZone=6;
}

else
if (zip3 >="596"  && zip3 <= "599")
{
upsZone=5;
}

else
if (zip3 >="600"  && zip3 <= "639")
{
upsZone=7;
}

else
if (zip3 >="640"  && zip3 <= "649")
{
upsZone=6;
}

else
if (zip3 >="650"  && zip3 <= "655")
{
upsZone=7;
}

else
if (zip3 >="656"  && zip3 <= "676")
{
upsZone=6;
}


else
if (zip3 =="677")
{
upsZone=5;
}


else
if (zip3 =="678")
{
upsZone=6;
}


else
if (zip3 =="679")
{
upsZone=5;
}


else
if (zip3 >="680"  && zip3 <= "692")
{
upsZone=6;
}


else
if (zip3 == "693")
{
upsZone=5;
}


else
if (zip3 >="700"  && zip3 <= "709")
{
upsZone=7;
}


else
if (zip3 >="710"  && zip3 <= "711")
{
upsZone=6;
}


else
if (zip3 >="712"  && zip3 <= "717")
{
upsZone=7;
}


else
if (zip3 =="718")
{
upsZone=6;
}


else
if (zip3 >="719"  && zip3 <= "725")
{
upsZone=7;
}


else
if (zip3 >="726"  && zip3 <= "727")
{
upsZone=6;
}


else
if (zip3 =="728")
{
upsZone=7;
}



else
if (zip3 >="729"  && zip3 <= "738")
{
upsZone=6;
}


else
if (zip3 =="739")
{
upsZone=8;
}


else
if (zip3 >="740"  && zip3 <= "775")
{
upsZone=6;
}


else
if (zip3 >="776"  && zip3 <= "777")
{
upsZone=8;
}


else
if (zip3 >="778"  && zip3 <= "789")
{
upsZone=8;
}


else
if (zip3 >="790"  && zip3 <= "791")
{
upsZone=5;
}


else
if (zip3 =="792")
{
upsZone=6;
}


else
if (zip3 >="793"  && zip3 <= "794")
{
upsZone=5;
}


else
if (zip3 >="795"  && zip3 <= "796")
{
upsZone=8;
}


else
if (zip3 >="797"  && zip3 <= "799")
{
upsZone=5;
}


else
if (zip3 >="800"  && zip3 <= "838")
{
upsZone=5;
}


else
if (zip3 >="840"  && zip3 <= "853")
{
upsZone=4;
}


else
if (zip3 =="854")
{
upsZone=3;
}


else
if (zip3 >="855"  && zip3 <= "863")
{
upsZone=4;
}


else
if (zip3 =="864")
{
upsZone=3;
}


else
if (zip3 =="865")
{
upsZone=4;
}


else
if (zip3 >="870"  && zip3 <= "872")
{
upsZone=5;
}


else
if (zip3 >="873"  && zip3 <= "874")
{
upsZone=4;
}


else
if (zip3 >="875"  && zip3 <= "885")
{
upsZone=5;
}


else
if (zip3 >="889"  && zip3 <= "892")
{
upsZone=3;
}


else
if (zip3 >="893"  && zip3 <= "898")
{
upsZone=4;
}


else
if (zip3 >="900"  && zip3 <= "935")
{
upsZone=2;
}


else
if (zip3 >="936"  && zip3 <= "939")
{
upsZone=3;
}


else
if (zip3 >="940"  && zip3 <= "942")
{
upsZone=4;
}


else
if (zip3 =="943")
{
upsZone=3;
}


else
if (zip3 >="944"  && zip3 <= "949")
{
upsZone=4;
}


else
if (zip3 >="950"  && zip3 <= "953")
{
upsZone=3;
}


else
if (zip3 >="954"  && zip3 <= "961")
{
upsZone=4;
}


else
if (zip3 >="970"  && zip3 <= "974")
{
upsZone=5;
}


else
if (zip3 >="975"  && zip3 <= "976")
{
upsZone=4;
}


else
if (zip3 >="977"  && zip3 <= "994")
{
upsZone=5;
}

if (shiplist.getSelectedValue() == "grd")
{
shipcharge = (gd[upsZone]+gdm[upsZone]*box1)*box32;


if (box2 > 0)
{
      shipcharge = shipcharge+ gd[upsZone]+gdm[upsZone]*box2;
}

++shipcharge ;             
}

else
if (shiplist.getSelectedValue() == "3d")
{
shipcharge = (d3[upsZone]+d3m[upsZone]*box1)*box32;



if (box2 > 0)
{
      shipcharge = shipcharge+ d3[upsZone]+d3m[upsZone]*box2;
}


if (wght < 25)
{
shipcharge=shipcharge + 3;
}
shipcharge=shipcharge * d3airadjustment;
}

else
if (shiplist.getSelectedValue() == "2d")
{
shipcharge = (d2[upsZone]+d2m[upsZone]*box1)*box32;

if (box2 > 0)
{
      shipcharge = shipcharge+ d2[upsZone]+d2m[upsZone]*box2;
}


if (wght < 25)
{
shipcharge=shipcharge + 3;
}
shipcharge=shipcharge * d2airadjustment;
}

else
if (shiplist.getSelectedValue() == "1d")
{
shipcharge = (d1[upsZone]+d1m[upsZone]*box1)*box32;

if (box2 > 0)
{
      shipcharge = shipcharge+ d1[upsZone]+d1m[upsZone]*box2;
}

if (wght < 25)
{
shipcharge=shipcharge + 4;
}
shipcharge=shipcharge * d1airadjustment;
}

shipcharge = Math.ceil(shipcharge * rateadjustment * fuelcharge);
ins = eval(subtotalbox.getText());
ins = Math.floor(ins / 100);
ins = ins * insrate;

if (box2 > 0)
{
      residential2 = residential;
      insinitialbox2 = insinitial;
}

if (ins > 0)
{
ins = Math.ceil(ins + insinitial + insinitialbox2 + box32*insinitial);
}

shipcharge = Math.ceil((shipcharge + ins + residential + residentialbox2+ residential*box32)*1.15);
if (shipcharge <  14 )
{
shipcharge = 14;
} 


if (statelist.getSelectedValue() == 4)
{
shipcharge = 0;
}
if (wght == 0) 
{
shipcharge = 0;
}
shippingdisplaybox.setText(trunc(shipcharge));
OrderSummaryCookie.setValue("ShippingCookie",trunc(shipcharge));
dotots(eval(subtotalbox.getText()));
 }
function _CalculateShip__onClick() { if (CalculateShip) return CalculateShip.onClick(); }
function calcheck__onClick() {
shippingCookie.setValue("calcheckcookie", calcheck.getValue());
 }
function _calcheck__onClick() { if (calcheck) return calcheck.onClick(); }
function SubmitOrderAndCheckOutButton__onClick() {
var addChar = "?" 
var j
var okToSubmit = false;

if ("CheckOut.asp".length > 1)
{
    Form1.setAction(subAwithBinC(" ", "%20", "CheckOut.asp"));
}

// execute the onSubmit() event handler and try to 
// determine if it already validated the form
Result = Form1.onSubmit();

//   If there is no onSubmithander the return value is null
//   If there is a validation handler it returns true to submit
//   or false to not submit
if (Result==null)  // there is no validation already defined
{
    if ("1" == "1")
    {
        Result = Validate("0"); // don't stop on first error
        if (Result == "") okToSubmit = true;
        else alert("The form could not be submitted:" + Result);
    }
    else okToSubmit = true;
}
else // there is a validation already defined
{
    if (Result==true)
        okToSubmit = true;
}

if (okToSubmit) 
{
    // We have to
    // put the source in the query string so the generic database contracts
    // still work.

    // NOTE: this only works if the method of the form is POST


    act = Form1.getAction();
    if (act.indexOf("?") != -1)
    {    
        addChar = "&"
    }

    act += addChar + "SubmitOrderAndCheckOutButton=1"
    Form1.setAction(act);


    Form1.submit();
}
 }
function _SubmitOrderAndCheckOutButton__onClick() { if (SubmitOrderAndCheckOutButton) return SubmitOrderAndCheckOutButton.onClick(); }

