
// Contents copyright 1999-2000 YukonSoft Corporation 
//
// Global Variables
var tax		= parseFloat(0),
itemsonOrder = parseFloat(0),
option		= 0,
shipoption  = parseFloat(0),
totalitems  = parseFloat(0),
totalweight = parseFloat(0),
shipping 	= parseFloat(0),
shipchrg	= parseFloat(0),
taxchrg 	= parseFloat(0),
invoice		= parseFloat(0),
totprice	= parseFloat(0),
delivery	= parseFloat(1),
firstname	= '',
lastname 	= '',
address 	= '',
city 		= '',
state 	 	= '',
zip 		= '',
country 	= '',
firstname2	= '',
lastname2 	= '',
address2 	= '',
city2 		= '',
state2 	 	= '',
zip2 		= '',
country2 	= '',
phone 		= '',
email 		= '',
company     = ''



function getValueName(itemindex) {

var vals = new Array(6);
vals[0] = new Array(2);
vals[0][0] = 3;
vals[0][1] = '1-10 Employee system';
vals[1] = new Array(2);
vals[1][0] = 4;
vals[1][1] = '11-50 Employee system';
vals[2] = new Array(2);
vals[2][0] = 5;
vals[2][1] = 'Unlimited Employee system';
vals[3] = new Array(2);
vals[3][0] = 6;
vals[3][1] = '5 Seat License';
vals[4] = new Array(2);
vals[4][0] = 7;
vals[4][1] = '10 Seat License';
vals[5] = new Array(2);
vals[5][0] = 8;
vals[5][1] = 'Full Seat License';
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];

}


// 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 <= 5) 
				  	return arShip[4+delivery][2];

				if (totalitems >= 5.01 && totalitems <= 10)  
					return arShip[4+delivery][3];

				if (totalitems >= 10.01 && totalitems <= 50)  
					return arShip[4+delivery][4];

				if (totalitems >= 50.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 <= 0) 
					return arShip[7+delivery][2] ;

				if (totalweight >= 0.01 && totalweight<= 3) 
					return arShip[7+delivery][3] ;

				if ( totalweight>= 3.01 && totalweight<= 25.02) 
					return arShip[7+delivery][4] ;

				if ( totalweight >= 25.03) 
					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) {
   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;
			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;
				}
			}
			break;

		case 2:  // always tax.
			tax = (eval(totals) ) * (0) / 100;
			break;

        case 3:  // apply internet tax rules
         tax = 0;
			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;
				}
			}
			break;
		}
	}
	
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 shipvia(delivery) {
    if (delivery==1) {
    days = "Regular UPS";
    return days;
    }
    if (delivery==2) {
    days = "2nd Day (BLUE)";
    return days;
    }
    if (delivery==3) {
    days = "Next Day (RED)";
    return days;
    }
	else {
    days = "Regular UPS";
    return days;
	}
}    

// 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() {
	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;
   var taxtotal = 0;
	
   document.writeln('<FORM NAME="test" TARGET="main">');
   document.writeln('<INPUT TYPE = "hidden" NAME = "info">');
   document.writeln('<CENTER><TABLE BORDER="0" CELLPADDING=3>');
   document.writeln('<TR BGCOLOR="#EFEFEF"><TD CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >Cat.No.</font></TD><TD CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >Description</font></TD><TD CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" ><font size=-1>Quantity</font></font></TD><TD CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" ><font size=-1>Cost Each</font></font></TD><TD CLASS="show" ><FONT FACE="Tahoma"  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;
   var totalnumitems = 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 CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >'+theitem+'</font></td><td CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >'+nameandvalues+'</font></td>');
         document.write('<td align=right CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >'+ thequantity +'</font></td><td align=right CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >$ '+ format1000(formatNum(individualprice,2)) +'</font></td><td align=right CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >$ '+ 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";

			document.writeln('<INPUT TYPE="hidden" NAME="'+itemlist+'-desc" VALUE="'+nameandoptions+'" > ');
			document.writeln('<INPUT TYPE="hidden" NAME="'+itemlist+'-qty" VALUE="'+thequantity+'"> ');
			document.writeln('<INPUT TYPE="hidden" NAME="'+itemlist+'-cost" VALUE="'+formatNum(individualprice,2)+'"> ');

  
		} 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);
			thisitem++;
			itemstart=i+1;
		}
	}
   document.writeln('<TR BGCOLOR="#C0C0C0"><TD COLSPAN=3 ALIGN=right CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" ><b>Sub Total</B></font></TD><TD ALIGN=left COLSPAN=2 CLASS="show" ><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >$'+ 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, delivery, totprice);
   taxchrg = taxes(state, taxtotal, shipchrg);
	invoice = formatNum(totprice + tax + eval(shipchrg),2);
    
   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="#C0C0C0"><TD CLASS="show"  COLSPAN=3 ALIGN=right><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" ><b>Shipping Weight</B></font></TD><TD CLASS="show" ALIGN=left COLSPAN=2><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >'+ format1000(formatNum(totalweight,0)) + ' Lbs</font></td></tr>');
      document.writeln('<TR BGCOLOR="#C0C0C0"><TD CLASS="show"  COLSPAN=3 ALIGN=right><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" ><b>Shipping Charges</B></font></TD><TD  CLASS="show" ALIGN=left COLSPAN=2><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >$'+ format1000(formatNum(parseFloat(shipchrg),2)) + '</font></td></tr>');
      document.writeln('<TR BGCOLOR="#C0C0C0"><TD CLASS="show"  COLSPAN=3 ALIGN=right><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" ><b>Taxes</B></font></TD><TD CLASS="show"  ALIGN=left COLSPAN=2><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >$'+ format1000(formatNum(tax,2)) + '</font></td></tr>');
      document.writeln('<TR BGCOLOR="#C0C0C0"><TD CLASS="show"  COLSPAN=3 ALIGN=right><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" ><b>Total</B></font></TD><TD CLASS="show"  ALIGN=left COLSPAN=2><FONT FACE="Tahoma"  CLASS="tablefont"  COLOR="#000000" >$'+ format1000(invoice) + '</font></td></tr>');

	   //document.write("<TR><TD CLASS="show"  colspan=5 align=right>Total Shipping Weight of items: </TD><TD align=right CLASS="show" >" + format1000(formatNum(totalweight,0)) + " Lbs</TD></TR>" );
      //document.write("<TR><TD CLASS="show"  colspan=5 align=right>Total Shipping Charges are: </TD><TD align=right CLASS="show" >$ <SPAN ID='shipper'>" + format1000(formatNum(parseFloat(shipchrg),2)) + "</SPAN></TD></TR>" );
//		document.write("<TR><TD CLASS="show"  colspan=5 align=right>Total Taxes are: </TD><TD CLASS="show"  align=right>$ <SPAN ID='tax'>" + format1000(formatNum(tax,2)) + "</SPAN></TD></TR>" );
		//document.write("<TR><TD CLASS="show"  colspan=5 align=right>Total value of this Invoice is: </TD><TD align=right CLASS="show" ><B>$ <SPAN ID='invoice'>" + format1000(invoice) + "</SPAN></B></TD></TR>" );
		document.writeln('</TABLE></CENTER>');

	// Submit Shipping and Tax information to Credit card processor
	if (shipchrg != 0) {
		document.write('<INPUT type="hidden" name="'+eval(itemlist+1)+'-desc" value="Ship Via '+ shipvia(delivery) +'">');
		document.write('<INPUT type="hidden" name="'+eval(itemlist+1)+'-qty" value="1">');
		document.write('<INPUT type="hidden" name="'+eval(itemlist+1)+'-cost" value="'+formatNum(eval(shipchrg),2)+'">');
	}
	if (tax != 0) {
		document.write('<INPUT type="hidden" name="'+eval(itemlist+2)+'-desc" value="Taxes">');
		document.write('<INPUT type="hidden" name="'+eval(itemlist+2)+'-qty" value="1">');
		document.write('<INPUT type="hidden" name="'+eval(itemlist+2)+'-cost" value="'+formatNum(tax,2)+'">');
	}
}

