// Contents copyright 1999-2003 YukonSoft Corporation 
//
//Global Variables
var tax		= parseFloat(0),
itemsonOrder = '',
option		= 0,
shipoption  = parseFloat(0),
totalitems  = parseFloat(0),
totalweight = parseFloat(0),
shipping 	= parseFloat(0),
shipchrg	= parseFloat(0),
taxchrg 	= parseFloat(0),
taxtotal    = 0,
invoice		= parseFloat(0),
totprice	= parseFloat(0),
delivery	= parseInt(1),
firstname	= '',
lastname 	= '',
address 	= '',
city 		= '',
state 	 	= '',
zip 		= '',
country 	= '',
firstname2	= '',
lastname2 	= '',
address2 	= '',
city2 		= '',
state2 	 	= '',
zip2 		= '',
country2 	= '',
phone 		= '',
email 		= '',
company     = ''
totalnumitems = 0,
itemlist    = 0,
method      = '',
deliverynum = 0,
num         = 0;




function getValueName(itemindex) {

var vals = new Array(0);
var min = 0;
var max = 0;
var middle = 0;
max = vals.length - 1;
while (min <= max) {
middle = parseInt((max + min) / 2);
if (itemindex == vals[middle][0]) {
break;
} else if (itemindex < vals[middle][0]) {
max = middle - 1;
} else {
min = middle + 1;
}
}
return vals[middle][1];

}


function doesExist(theId) {
  if (document.getElementById && document.getElementById(theId)) return true;
  if (document.all) {
    if (theId != null) return true;
  }
  if (document.layers && document.layers[theId]) return true;
  return false;
}

// alterError - fixes a rounding bug in Netscape 2
function alterError(value) {
	if (value<=0.99) {
		newPounds = '0';
	} else {
		newPounds = parseInt(value);
	}
	newPence = parseInt((value+.0008 - newPounds)* 100);
	if (eval(newPence) <= 9) newPence='0'+newPence;
	newString = newPounds + '.' + newPence;
	return (newString);
}


function format1000 ( n ) {
    for (var i = n.indexOf('.')  -3; i > 0; i -= 3)
        n = n.substring(0, i) + ',' + n.substring(i);
    return n;
    }


function formatNum(expr,decplaces) {
var str = (Math.round(parseFloat(expr) * Math.pow(10,decplaces))).toString()
	while (str.length <= decplaces) {
		str = "0" + str;
		}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str,length);
	}


function addSpaceR(argvalue, numlength)
{
if (!numlength > 0)
	numlength = 10;

	if (argvalue.length < numlength) {
		for (var i = argvalue.length; i < numlength; i++)
		argvalue = argvalue + " ";
	}
	return argvalue;
}


function addSpaceL(argvalue, numlength)
{
if (!numlength > 0)
	numlength = 10;

	if (argvalue.length < numlength) {
		for (var i = argvalue.length; i < numlength; i++)
		argvalue = " " + argvalue;
	}
	return argvalue;
}


function shipit(totalitems, totalweight, delivery, totprice) {
	shipoption = 2;
   
   switch (shipoption) {

		case 0:	// never add shipping charges.
           return 0;
		   break;

		case 4:	//1 = Add shipping charges according to the number of items ordered.

			 	if (totalitems <= 1) 
				  	return arShip[4+delivery][2];

				if (totalitems >= 1.01 && totalitems <= 2)  
					return arShip[4+delivery][3];

				if (totalitems >= 2.01 && totalitems <= 3)  
					return arShip[4+delivery][4];

				if (totalitems >= 3.01) 
					return arShip[4+delivery][5];

				break;
	
		case 1:  // Non-Variable shipping 
	
			return arShip[11][2];
			break;
	
        case 2:  // Add shipping charges according to the weight of the items ordered.
	
		   if (totalweight <= 1) 
		   	return arShip[7+delivery][2] ;

		   if (totalweight >= 1.01 && totalweight<= 6) 
		   	return arShip[7+delivery][3] ;

		   if ( totalweight>= 6.01 && totalweight<= 8) 
		   	return arShip[7+delivery][4] ;

		   if ( totalweight >= 8.01) 
		   	return arShip[7+delivery][5];

			break;
	
		case 3: // Add shipping charges according to the total price of the items.
	
            if (totprice <= 20) 
					return arShip[delivery][2];

				if (totprice >= 20.01 && totprice<= 40) 
					return arShip[delivery][3];

				if (totprice >= 40.01 && totprice<= 60) 
					return arShip[delivery][4];

				if (totprice >= 60.01)
					return arShip[delivery][5];
					break;
		}
	}

function taxes(state, totals, shipping, country) {
   option = 1;
	switch (option) {

		case 0:  // no tax.
			tax = 0;
			break;

		case 1:  // tax only if customer is in my state or my applicable business states.
			tax = 0;

			// US and Canada
			for (var i = 1; i < arStates.length; i++) {
				if ((arStates[i][1] == state.toUpperCase()) && (arStates[i][3]== "1")) {
				  tax = (parseFloat(totals) + parseFloat(shipping)) * (arStates[i][2] / 100);
				  break;
				}
				if (state.toUpperCase() == arStates[i][1]) {
				  tax = totals * (arStates[i][2]) / 100;
				  break;
				}
			}

			// International
			for (var i = 1; i < arCountrys.length; i++) {
				if ((arCountrys[i][1] == country.toUpperCase()) && (arCountrys[i][3]== "1")) {
				  tax = (parseFloat(totals) + parseFloat(shipping)) * (arCountrys[i][2] / 100);
				  break;
				}
				if (country.toUpperCase() == arCountrys[i][1]) {
				  tax = totals * (arCountrys[i][2]) / 100;
				  break;
				}
			}
			break;

		case 2:  // always tax.
			tax = (eval(totals) ) * (7.75) / 100;
			break;

        case 3:  // apply internet tax rules
         tax = 0;

			// US and Canada
			for (var i = 1; i < arStates.length; i++) {
				if ((arStates[i][1] == state.toUpperCase()) && (arStates[i][3]== "1")) {
				  tax = (parseFloat(totals) + parseFloat(shipping)) * (arStates[i][2] / 100);
				  break;
				}
				if (state.toUpperCase() == arStates[i][1]) {
				  tax = totals * (arStates[i][2]) / 100;
				  break;
				}
			}

			// International
			for (var i = 1; i < arCountrys.length; i++) {
				if ((arCountrys[i][1] == country.toUpperCase()) && (arCountrys[i][3]== "1")) {
				  tax = (parseFloat(totals) + parseFloat(shipping)) * (arCountrys[i][2] / 100);
				  break;
				}
				if (country.toUpperCase() == arCountrys[i][1]) {
				  tax = totals * (arCountrys[i][2]) / 100;
				  break;
				}
			}
			break;
		}
	return tax;
	}
	
function showCustomer(TheCookieName) {
MakeCookieArray(GetCookie(TheCookieName));
	if (ckArray[0] != "*") {
	firstname = ckArray[0];
	lastname  = ckArray[1];
	address   = ckArray[2];
	city 	  = ckArray[3];
	state 	  = ckArray[4];
	zip 	  = ckArray[5];
	country   = ckArray[6];
    firstname2 = ckArray[7];
	lastname2  = ckArray[8];
	address2   = ckArray[9];
	city2 	  = ckArray[10];
	state2 	  = ckArray[11];
	zip2 	  = ckArray[12];
	country2   = ckArray[13];
    phone 	  = ckArray[14];
	email 	  = ckArray[15];
	if (ckArray[16] != "") {
		delivery  = parseInt(ckArray[16]);
	}
	else {
	delivery  = parseInt(1);
	}
    company = ckArray[17];
	}
}

function getSelectedButton(buttonGroup) 
{
	for (var i = 0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked) {
         return buttonGroup[i].value
        }
		}
	return 0
}

function shipvia(delivery) {
   switch (delivery) {
  
	 case 2:
       days= "2nd Day (BLUE)";
       return days;
	   break;
	 case 3:
       days= "Next Day (RED)";
       return days;
	   break;
     case 1:	
       days= "Regular";
       return days;
	   break;
   }
}


// clearBasket() - removes all items from the basket
function clearBasket() {

   index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=.";
	}


// showItems () - creates a table of items in the basket and
// creates the start of a form which sets information for
// basket items.

function showItems() {
  var pnptaxable ="";

  index = document.cookie.indexOf("TheBasket");
  countbegin = (document.cookie.indexOf("=", index) + 1);
   	countend = document.cookie.indexOf(";", index);
   	if (countend == -1) {
      countend = document.cookie.length;
   	}
  fulllist = document.cookie.substring(countbegin, countend);
  totprice = 0;
	
  document.writeln('<FORM NAME="test" TARGET="main">');
  document.writeln('<INPUT TYPE = "hidden" NAME = "info">');
  document.writeln('<TABLE BORDER="0" CELLPADDING=3>');
  document.writeln('<TR BGCOLOR="#008040"><TD><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >Cat.No.</font></TD><TD><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >Description</font></TD><TD><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><font size=-1>Quantity</font></font></TD><TD WIDTH=100><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><font size=-1>Cost Each</font></font></TD><td WIDTH=100><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><font size=-1>Total Cost</font></font></td></TR>');
  
  itemsonOrder += "Cat.No.              Description               Quantity    Cost Each\n";
  itemsonOrder += "====================================================================\n\n";

  var option1 = 0;
  var option2 = 0;
  var option3 = 0;
  var subItemID = 0;
  var taxable = 0;
  itemlist = 0;

  for (var i = 0; i <= fulllist.length; i++) {
  	if (fulllist.substring(i,i+1) == '[') {
  		thisitem = 1;
  		itemstart = i+1;
     } else if (fulllist.substring(i,i+1) == ']') {
        itemend = i;
		//thequantity = fulllist.substring(itemstart, itemend);
			
         itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			var actualprice = 0;
			var individualprice = 0;
			individualprice = eval(getPrice(theitem,thequantity) + eval(theprice));
			actualprice = eval(thequantity * individualprice);
			
         if (eval(taxable) == 1) {
            taxtotal = taxtotal + actualprice;
         }
         totprice = totprice + actualprice;
         itemlist=itemlist+1;
			totalweight += eval(thequantity)*eval(theweight);
         totalnumitems += eval(thequantity);
         itemend = i;
         
         var nameandvalues;
         var nameandoptions;
         nameandvalues = thename;         
         nameandoptions = thename;
         if (eval(option1) != 0) {
            nameandoptions = nameandoptions + "|" + option1;
            nameandvalues = nameandvalues + " (" + getValueName(eval(option1)) + ") ";
         }
         if (eval(option2) != 0) {
            nameandoptions = nameandoptions + "|" + option2;
            nameandvalues = nameandvalues + " (" + getValueName(eval(option2)) + ") ";
         }
         if (eval(option3) != 0) {
            nameandoptions = nameandoptions + "|" + option3;
            nameandvalues = nameandvalues + " (" + getValueName(eval(option3)) + ") ";
         }


         document.write('<tr BGCOLOR="#C0C0C0"><td><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >'+theitem+'</font></td><td><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >'+nameandvalues+'</font></td>');
         document.write('<td align=center><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >'+ thequantity +'</font></td><td align=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36;  '+ format1000(formatNum(individualprice,2)) +'</font></td><td align=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36;  '+ format1000(formatNum(actualprice,2)) +'</font></td></tr>');
         
         itemsonOrder += addSpaceR(theitem, 12) + addSpaceR(nameandoptions, 34);
         itemsonOrder += addSpaceL(format1000(formatNum(thequantity,2)), 10) +  addSpaceL( format1000(formatNum(individualprice,2)), 13) + "\n";

         // iTransact code
		   //document.write('<INPUT TYPE="hidden" NAME="'+itemlist+'-desc" VALUE="'+nameandoptions+'" > ');
		   //document.write('<INPUT TYPE="hidden" NAME="'+itemlist+'-qty" VALUE="'+thequantity+'"> ');
		   //document.write('<INPUT TYPE="hidden" NAME="'+itemlist+'-cost" VALUE="'+formatNum(individualprice,2)+'"> ');
  
         // plug 'n Pay code
			document.write('<INPUT TYPE="hidden" NAME="item'+itemlist+'" VALUE="'+theitem+'"> ');
			document.write('<INPUT TYPE="hidden" NAME="description'+itemlist+'" VALUE="'+nameandoptions+'" > ');
			document.write('<INPUT TYPE="hidden" NAME="quantity'+itemlist+'" VALUE="'+thequantity+'"> ');
			document.write('<INPUT TYPE="hidden" NAME="cost'+itemlist+'" VALUE="'+formatNum(individualprice,2)+'"> ');

			if (taxable == 1) {
              pnptaxable = "y";
            }
			else
			{
			  pnptaxable = "n";
			}

			document.write('<INPUT TYPE="hidden" NAME="taxable'+itemlist+'" VALUE="'+pnptaxable+'"> ');
            document.write('<INPUT TYPE="hidden" NAME="order-id" VALUE="'+theitem+'"> ');

		} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) thename = fulllist.substring(itemstart, i);
			if (thisitem==3) theprice = fulllist.substring(itemstart, i);
			if (thisitem==4) theweight = fulllist.substring(itemstart, i);
            if (thisitem==5) thequantity = fulllist.substring(itemstart, i);
            if (thisitem==6) option1 = fulllist.substring(itemstart, i);
			if (thisitem==7) option2 = fulllist.substring(itemstart, i);
			if (thisitem==8) option3 = fulllist.substring(itemstart, i);
            if (thisitem==9) subItemID = fulllist.substring(itemstart, i);
            if (thisitem==10) taxable = fulllist.substring(itemstart, i);
            if (thisitem==11) qtyavailable = fulllist.substring(itemstart, i);  // added 3/30/06 DLJ                     
			thisitem++;
			itemstart=i+1;
		}
	}
 
    document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Sub Total</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36; '+ format1000(formatNum(totprice,2))+'</font></td></tr>');
    itemsonOrder += "\n" + addSpaceL("Total: $ " + format1000(formatNum(totprice,2)),81) + "\n";
    document.writeln('<INPUT TYPE="hidden" NAME="Goods Total" VALUE="'+format1000(formatNum(totprice,2))+'" SIZE="40">');

    // Calculate Shipping and Taxes
    shipchrg = shipit(totalnumitems, totalweight, parseFloat(parent.banner.delivery), totprice);

  
    // test for Plug'n Pay
    if (doesExist(document.test.first_name)) {
    //if (document.test.first_name != null) {
      if (document.test.first_name.value != "") {
        taxchrg = taxes(state2, taxtotal, shipchrg, country);
        invoice = formatNum(totprice + tax + eval(shipchrg),2);
      }
    }

    // test for Purchase Order - Internet Explorer
    if ((is_ie) || (is_firefox)) {
      if  (document.junk != null) {
        taxchrg = taxes(state2, taxtotal, shipchrg, country);
        invoice = formatNum(totprice + tax + eval(shipchrg),2);
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Shipping Weight</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >'+ format1000(formatNum(totalweight,0)) + ' Lbs</font></td></tr>');
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Shipping Charges</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36; '+ format1000(formatNum(parseFloat(shipchrg),2)) + '</font></td></tr>');
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Taxes</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36; '+ format1000(formatNum(tax,2)) + '</font></td></tr>');
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Total</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36; '+ format1000(invoice) + '</font></td></tr>');
	  }
    }

    // test for Purchase Order - Netscape
	if ( document.submitForm != null) {
        taxchrg = taxes(state2, taxtotal, shipchrg, country);
        invoice = formatNum(totprice + tax + eval(shipchrg),2);
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Shipping Weight</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >'+ format1000(formatNum(totalweight,0)) + ' Lbs</font></td></tr>');
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Shipping Charges</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36; '+ format1000(formatNum(parseFloat(shipchrg),2)) + '</font></td></tr>');
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Taxes</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36; '+ format1000(formatNum(tax,2)) + '</font></td></tr>');
        document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Total</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" > &#36; '+ format1000(invoice) + '</font></td></tr>');
    }


    itemsonOrder += "====================================================================\n\n";
    itemsonOrder += addSpaceL("Total Shipping Weight of items: ",72) + format1000(formatNum(totalweight,0)) + " Lbs\n";
    itemsonOrder += addSpaceL("Total Shipping Charges: $ ",73) + format1000(formatNum(parseFloat(shipchrg),2))+ "\n";
    itemsonOrder += addSpaceL("Total Taxes: $ ",73) + format1000(formatNum(tax,2))+ "\n";
    itemsonOrder += addSpaceL("Total value of this Invoice is: $ ",73) + format1000(formatNum(invoice,2)) +"\n\n";
    itemsonOrder += "Thank You for Your Order\n";

  //  document.writeln('<TR BGCOLOR="#808080"><TD COLSPAN=3 ALIGN=right><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" ><b>Shipping Weight</B></font></TD><TD ALIGN=left COLSPAN=2><FONT FACE="Comic Sans MS"  CLASS="tablefont"  COLOR="#000000" >'+ format1000(formatNum(totalweight,0)) + ' Lbs</font></td></tr>');
    document.writeln('</TABLE>');
  
    // Submit Shipping and Tax information to Credit card processor
    document.write('<INPUT TYPE="hidden" NAME="shipping" value="0">');
	document.write('<BR>');
}

function updateshipping(delivery) {
  shipchrg = shipit(totalnumitems, totalweight, parseFloat(parent.banner.delivery), totprice);
  if (shipchrg != 0) {
    // Plug 'N Pay code
	if (parent.banner.days == "" ) {
	parent.banner.days = "Regular";
	}
    document.test.shipping.value = formatNum(shipchrg,2);
    document.test.shipvia.value  = parent.banner.days;
	taxchrg                      = taxes("ZZ", parseFloat(taxtotal), parseFloat(shipchrg), parent.banner.countrycode);
    document.test.tax.value      = taxchrg;
	return true;
   }
   if (delivery == 0) {
	taxchrg                      = taxes("ZZ", parseFloat(taxtotal), 0, parent.banner.countrycode);
    document.test.tax.value      = taxchrg;
  }

}


