var max_weight = 40;
var max_weight_special = 24;
var ar_len = 0;
var loop_count = 0; //belongs to find_zip_ups and find_zip_usps functions
var user_zone = 0; //belongs to find_zip_ups and find_zip_usps functions
var ziph_1; //belongs to find_zip_ups and find_zip_usps functions
var ziph_2; //belongs to find_zip_ups and find_zip_usps functions
var carrier;
var shiprate;
var userzip;
var howfast;
var upss;
var no_con = false;
var current_tax = 0;
var current_s_t = 0;
var current_ship = 0;
var total_view;
var ex = 0.00; //insure price
var gowi = false;
var vtax = false;
var vship = false;
var vzip = false;
var current_surcharge = 0;
var surc_hold = 0;	
var handling = 0.75; //used in "calcrate();"
var sig_com_ups = 0.00; //used in "calcrate();"
var sig_com_usps = 0.00; //used in "calcrate();"
var st_total, st_tax, st_ship, st_insure;
	function roundit(num, rb){
		var result;
		rb = parseInt(rb);
		if(isNaN(num)){
			num = num.split(".");
			var bdp, adp;
			bdp = num[0];
			adp = num[1].substring(0,rb);
			bdp = parseInt(bdp);
			adp = parseInt(adp);
			adp = adp / Math.pow(10, rb);
			result = bdp + adp;
		}else{
			num = num * Math.pow(10, rb);
			num = parseInt(num);
			result = num / Math.pow(10, rb);
		}
		return result;
	}
	
	
	function roundit_string(num, rb){
		num ="" + num;
		rb = parseInt(rb);
		var rv = "" + Math.round(num * Math.pow(10, rb));
		var point = rv.length - rb;
		if (point != 0){
			var rt = rv.substring(0, point);
			rt =rt + ".";
			rt += rv.substring(point, rv.length);
		}else{
			rt ="." + rv;
		}
		return rt;
	}
	
	function calc_surcharge(){
		if(vzip == false){
			current_ship = 0.00
			add_all();
		}else if(vzip == true){
			if(carrier == 0){
				ex = 0.00;
				current_ship = 0.00;
				vship = false;
				add_all();
			}else if(carrier == 1){ //for USPS (no surcharge)
				handling = 0.00;
				current_ship += sig_com_usps;
				add_all();
			}else if(carrier == 2){ //adding the surcharge for UPS
				//Handling adjustment
				handling = 0.00;
				//Take handling out.
				current_ship = current_ship - handling;
				//add Signature Confirmation.
				current_ship += sig_com_ups;
				//Calculate surcharge.
				surc_hold = current_ship * current_surcharge / 100;
				//add shipping, surcharge, and handling.
				current_ship = current_ship + surc_hold + handling;
				add_all();
			}
		}
	}
	
	function calcinsure(insure){
		if(carrier == 1){
			if(insure == 0){
				ex = 0.00;
			}else if(insure == 100){
				ex = 2.20;
			}else if(insure == 200){
				ex = 3.20;
			}else if(insure == 300){
				ex = 4.20;
			}else if(insure == 400){
				ex = 5.20;
			}else if(insure == 500){
				ex = 6.20;
			}else if(insure == 600){
				ex = 7.20;
			}else if(insure == 700){
				ex = 8.20;
			}else if(insure == 800){
				ex = 9.20;
			}else if(insure == 900){
				ex = 10.20;
			}else if(insure == 1000){
				ex = 11.20;
			}else if(insure > 1000){
				insure = insure /100;
				insure = insure - 10;
				insure = insure * 1.00;
				ex = 11.20 + insure;
			}
			add_all();
		}else if(carrier == 2){
			if(insure == 0){
				ex = 0.00;
			}else if(insure == 100){
				ex = 0.00;
			}else if(insure == 200){
				ex = 1.20;
			}else if(insure == 300){
				ex = 1.20;
			}else if(insure > 300){
				insure = insure /100;
				insure = insure - 3;
				insure = insure * .40;
				ex = 1.2 + insure;
			}
			add_all();
		}
	}
	
	function set_begin(tt, rate1, rate2, rate3){
		current_s_t = tt;
		current_surcharge = rate1;
		sig_com_ups = rate2;
		sig_com_usps = rate3;
	}
	
	function set_tax(h, g){
		if(h==0){
			current_tax = 0.00;
			vtax = false;
		}
		if(h==1){
			current_tax = g;
			vtax = true;
		}
		if(h==2){
			current_tax = 0.00;
			vtax = true;
		}
	}
	
	function add_all(){
		if(current_tax == 0){
			st_tax = "0.00"
			if(current_ship == 0){
				st_ship = "0.00"
				if(ex == 0){
					st_insure = "0.00"
					vship = false;
					document.cartform.taxtxt.value = "$0.00";
					document.cartform.shtxt.value = "$0.00";
					document.cartform.insure.value = "$0.00";
					document.cartform.totaltxt.value = "$" + roundit_string(current_s_t, 2);
				}else if(ex > 0){
					st_insure = roundit_string(ex, 2);
					vship = false;
					document.cartform.taxtxt.value = "$0.00";
					document.cartform.shtxt.value = "$0.00";
					document.cartform.insure.value = "$" + roundit_string(ex, 2);
					document.cartform.totaltxt.value = "$" + roundit_string(ex + current_s_t, 2);
				}
			}else if(current_ship > 0){
				st_ship = roundit_string(current_ship, 2);
				if(ex == 0){
					st_insure = "0.00"
					vship = true;
					document.cartform.taxtxt.value = "$0.00";
					document.cartform.shtxt.value = "$" + roundit_string(current_ship, 2);
					document.cartform.insure.value = "$0.00";
					document.cartform.totaltxt.value = "$" + roundit_string(current_s_t + current_ship, 2);
				}else if(ex > 0){
					st_insure = roundit_string(ex, 2);
					vship = true;
					document.cartform.taxtxt.value = "$0.00";
					document.cartform.shtxt.value = "$" + roundit_string(current_ship, 2);
					document.cartform.insure.value = "$" + roundit_string(ex, 2);
					document.cartform.totaltxt.value = "$" + roundit_string(ex + current_s_t + current_ship, 2);
				}
				
			}
		}else if(current_tax > 0){
			st_tax = roundit_string(current_tax, 2);
			if(current_ship == 0){
				st_ship = "0.00";
				if(ex == 0){
					st_insure = "0.00";
					vship = false;
					document.cartform.taxtxt.value = "$" + roundit_string(current_tax, 2);
					document.cartform.shtxt.value = "$0.00";
					document.cartform.insure.value = "$0.00";
					document.cartform.totaltxt.value = "$" + roundit_string(current_s_t + current_tax, 2);
				}else if(ex > 0){
					st_insure = roundit_string(ex, 2);
					vship = false;
					document.cartform.taxtxt.value = "$" + roundit_string(current_tax, 2);
					document.cartform.shtxt.value = "$0.00";
					document.cartform.insure.value = "$" + roundit_string(ex, 2);
					document.cartform.totaltxt.value = "$" + roundit_string(ex + current_s_t + current_tax, 2);
				}
			}else if(current_ship > 0){
				st_ship = roundit_string(current_ship, 2);
				if(ex == 0){
					st_insure = "0.00"
					vship = true;
					document.cartform.taxtxt.value = "$" + roundit_string(current_tax, 2);
					document.cartform.shtxt.value = "$" + roundit_string(current_ship, 2);
					document.cartform.insure.value = "$0.00";
					document.cartform.totaltxt.value = "$" + roundit_string(current_s_t + current_ship + current_tax, 2);
				}else if(ex > 0){
					st_insure = roundit_string(ex, 2);
					vship = true;
					document.cartform.taxtxt.value = "$" + roundit_string(current_tax, 2);
					document.cartform.shtxt.value = "$" + roundit_string(current_ship, 2);
					document.cartform.insure.value = "$" + roundit_string(ex, 2);
					document.cartform.totaltxt.value = "$" + roundit_string(ex + current_s_t + current_ship + current_tax, 2);
				}
				
			}
		}		
	}
		
	function Shipper (whatvalue){ 
		howfast = whatvalue;
		if(howfast == 0){
			carrier = 0;
			vship = false;
		}
		if(howfast == 1){
			carrier = 1;
			upss = 0.00; 
			setarray = new Array(0,uspsPrz1,uspsPrz2,uspsPrz3,uspsPrz4,uspsPrz5,uspsPrz6,uspsPrz7,uspsPrz8);
		}	
		if(howfast==2){
			carrier = 2;
			upss = 1.75 + 1.00; //For residential price + 1.00 @ 9.25.05
			setarray = new Array(0, 0, upsZ2,upsZ3,upsZ4,upsZ5,upsZ6,upsZ7,upsZ8);
		}
	}
	
function find_zip_usps(){
	if(no_con == true){
		no_con = false;
	}
	ar_len = usps_zip_through.length - 1;
	loop_count = 0;
	user_zone = 0;
	while(loop_count <= ar_len){ 
		ziph_1 = usps_zip_from[loop_count];
		ziph_2 = usps_zip_through[loop_count];
		  if(userzip >= ziph_1){
			  if(userzip <= ziph_2){
			  	user_zone = usps_zip_zone[loop_count];
			  	break
			  }
		  }
		loop_count = loop_count + 1;
	}
	if(user_zone == 0 || user_zone == null){
		alert("The zip code entered is either invalid or is not covered by the carrier that you " +
			  "selected. Please try another carrier. If the problem still exists then please call " +
			  "1 + 423-902-0496 and we will try to remedy the problem.");
		no_con = true; //Stop everything else
	}
}

function find_zip_ups(){
	if(no_con == true){
		no_con = false;
	}
	ar_len = ups_zip_through.length - 1;
	loop_count = 0;
	user_zone = 0;
	while(loop_count <= ar_len){ 
		ziph_1 = ups_zip_from[loop_count];
		ziph_2 = ups_zip_through[loop_count];
		  if(userzip >= ziph_1){
			  if(userzip <= ziph_2){
			  	user_zone = ups_zip_zone[loop_count];
			  	break
			  }
		  }
		loop_count = loop_count + 1;
	}
	if(user_zone == 0 || user_zone == null){
		alert("The zip code entered is either invalid or is not covered by the carrier that you " +
			  "selected. Please try another carrier. If the problem still exists then please call " +
			  "1 + 423-902-0496 and we will try to remedy the problem.");
		no_con = true; //Stop everything else
	}
}

	function val_zip(uzi){
		if(uzi == ""){
			vzip = false;
			alert("You must enter the destination zip code first");
		}else{
			if(uzi.length < 5){
				vzip = false;
				alert("The zip code entered MUST be at least 5 digits in length");
			}else if(isNaN(uzi)){
				vzip = false;
				alert("The zip code entered was not a number");
			}else{
				vzip = true;
				userzip = uzi;
			}
		}
		if(vzip == false){

		}else if(vzip == true){
			if(carrier == 1){
				find_zip_usps();
			}else if(carrier == 2){
				find_zip_ups();
			}
		}
	}
	
		function calcrate(wte, wte2){
			if(wte2 > 0){
			if(vzip == true){
				//If weight set in wte is above max_weight then... 
				if(wte2 > max_weight_special){
					shiprate = 0; //reset shipping
					while(wte2 > max_weight_special){//Loop while wte is greater than max_weight.
						if(wte2 >= max_weight_special){
							wte2 = wte2 - max_weight_special; //the entered weight - the max_weight.
							//Add residential price + shipping + the max_weight.
							shiprate = upss + shiprate + setarray[user_zone][max_weight_special];
						}else{
							break
						}
					}
					if(wte2 + wte <= max_weight_special){
						shiprate = shiprate + upss + setarray[user_zone][wte2 + wte];
						wte = 0;
						wte2 = 0;
					}else if(wte + wte2 > max_weight_special){
						shiprate = shiprate + upss + setarray[user_zone][max_weight_special];
						wte = wte + wte2 - max_weight_special;
					}
				}else if(wte2 <= max_weight_special){ //If wte is not greater than max_weight then...
					shiprate = 0; //reset shipping.
					if(wte2 + wte <= max_weight_special){
						shiprate = shiprate + upss + setarray[user_zone][wte2 + wte];
						wte = 0;
						wte2 = 0;
					}else if(wte + wte2 > max_weight_special){
						shiprate = shiprate + upss + setarray[user_zone][max_weight_special];
						wte = wte + wte2 - max_weight_special;
					}
				}	
			}
			}
			if(wte > 0){
			if(vzip == true){
				//If weight set in wte is above max_weight then... 
				if(wte > max_weight){
					//shiprate = 0; //reset shipping
					while(wte > max_weight){//Loop while wte is greater than max_weight.
						if(wte >= max_weight){
							wte = wte - max_weight; //the entered weight - the max_weight.
							//Add residential price + shipping + the max_weight.
							shiprate = upss + shiprate + setarray[user_zone][max_weight];
						}else{
							//Add residential price + shipping + any extra pounds.
							shiprate = upss + shiprate + setarray[user_zone][wte];
						}
					}
					//Add residential price + shipping + any extra pounds for the last time.
					shiprate = upss + shiprate + setarray[user_zone][wte];
				}else if(wte <= max_weight){ //If wte is not greater than max_weight then...
					//shiprate = 0; //reset shipping.
					//Add shipping + residential addition.
					shiprate = shiprate + upss + setarray[user_zone][wte];
				}	
			}
			}
			if(shiprate > 0){
				current_ship = shiprate + handling;
			}else{
				current_ship = 0.00;
			}
		}

	function val_form(lsnum){
		if(no_con == true){
			gowi = false;
		}else{
			if(vtax == false){
				alert("Please select tax");
				gowi = false;
			}else{
				if(vship == false){
					alert("Please Select shipping");
					gowi = false;
				}
			}
			if(vtax == true && vship == true){		
				if(gowi = false){
			
				}else if(gowi = true){
					check_out(lsnum);
				}
			}
		}
	}
				
	function check_out(end_num){
		if(gowi == true){		
			var path, sizing;
			sizing = "width=850,height=550,scrollbars," 
			sizing += "toolbar,titlebar,resizable,left=75,top=70,status";
			path = "s_tax_plus.asp?tax=" + st_tax + "&shipping=";		
			path += st_ship + "&extra=" + st_insure; 
			if(carrier == 1){
				path += "&shipper=USPS"
			}	
			if(carrier == 2){
				path += "&shipper=UPS"
			}
			window.open(path, "payit", sizing);
		}else if(gowi == false){
			alert("Something on this form is incomplete");
		}
	}
