//		All javascript functions in this document are Copyright 2000 - 2004 by Greg Smith.
//		All rights are reserved.  For information on use of these functions please contact
//		the author: greg@iWonderInc.com


	function justChill() {}														  	//  This is a "do nothing" function...

	
	function setCookie(cookieName,ID) {												//	This function creates a ONE QTR lifetime cookie with the name 
																					//	provided in the "cookieName" parameter and the value provided
		var expire = new Date()														//	in the "ID" parameter
		var oneMinute = expire.getTime() + (60 * 1000)
		var threeMin = expire.getTime() + (3 * 60 * 1000)
		var oneHour = expire.getTime() + (60 * 60 * 1000)
		var oneDay = expire.getTime() + (24 * 60 * 60 * 1000)
		var oneQTR = expire.getTime() + (91 * 24 * 60 * 60 * 1000)

		expire.setTime(oneQTR)

		var exp = expire.toGMTString()
	
		var cookie_str = cookieName + "=" + ID + "; " + "expires=" + exp
	
		document.cookie=cookie_str	
	} 


	function deleteCookie(cookieName) {
	
		var expire2 = new Date()													//	in the "ID" parameter
		var hourAgo = expire2.getTime() - (60 * 60 * 1000)

		expire2.setTime(hourAgo)

		var exp2 = expire2.toGMTString()
	
		var cookie_str = cookieName + "=0; " + "expires=" + exp2
	
		document.cookie=cookie_str	
	} 
	

	function getCookieVal(offset) {													//	This function returns the value of a cookie when given
																					//	it's offset inside the cookie string.  The function
		var endstr = document.cookie.indexOf(";",offset)							//	getCookie(name) is used to find the offset inside the
		if (endstr == -1)															//	cookie string for a cookie with a given name
			endstr = document.cookie.length
		return unescape(document.cookie.substring(offset,endstr))
	}
	
		
	function getCookie(name) {														//	This function returns the offset within the cookie string
																					//	for the particular cookie whose name is provided to it.   
		var arg = name + "="														//	In order to get a value from a name, this function AND
		var alen = arg.length														//	getCookieVal(offset) must be used.
									
		var clen = document.cookie.length
		var i = 0
		while (i < clen) {
			var j = i + alen
			if (document.cookie.substring(i,j) == arg)
				return getCookieVal(j)
			i = document.cookie.indexOf(" ", i) + 1
			if (i == 0) break
		}
		return null
	}

	
	var brType ;

	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) brType="OP"; 		// Opera
	if (document.all) brType="IE"; 																// Internet Explorer e.g. IE4 upwards
	if (document.layers) brType="NN"; 															// For Netscape version 4
	if (!document.all && document.getElementById) brType="MO";									// Mozila e.g. Netscape 6 upwards
		
//	alert('the browser type is :' + brType);		


	function CollapseLayer(id, action){ 
		if (brType=="IE") eval("document.all." + id + ".style.display='" + action + "'"); 
		if (brType=="NN") eval("document." + id + ".display='" + action + "'"); 
		if (brType=="MO" || brType=="OP") 
		eval("document.getElementById('" + id + "').style.display='" + action + "'"); 
	}


	function toggleDiv(id){
//			alert('toggleDiv()')
//		if (brType=="IE") {
//			var el = "document.all." + id + ".style"
//			if (el.display == "none") { el.display = "" } else { el.display = "none" }
//		}
//		if (brType=="NN") {
//			var el = "document." + id 
//			if (el.display == "none") { el.display = "" } else { el.display = "none" } 
//		}
//		if (brType=="MO" || brType=="OP") {
			var el = document.getElementById(id).style
			if (el.display == "none") { el.display = "" } else { el.display = "none" }		
//		} 
	}


	function ShowLayer(id, action){ 
		if (brType=="IE") eval("document.all." + id + ".style.visibility='" + action + "'"); 
		if (brType=="NN") eval("document." + id + ".visibility='" + action + "'"); 
		if (brType=="MO" || brType=="OP") 
		eval("document.getElementById('" + id + "').style.visibility='" + action + "'"); 
	}
			

	function ChangeContent(id, str) { 
		if (brType=="IE") { 
			document.all[id].innerHTML = str; 
		} 
		if (brType=="NN") { 
			document.layers[id].document.open(); 
			document.layers[id].document.write(str); 
			document.layers[id].document.close(); 
		} 
		if (brType=="MO") { 
			document.getElementById(id).innerHTML = str; 
		} 
	}
	
	
	function updateCart() {
		
		var items_info;
		var ship_info;
		var tax_info;

		items_info 	= getCookie('o_items');
		items_array = items_info.split(',');
		new_info	= "";
		row			= 0;
		test_p_ttl	= 0;
		t_ppkit		= 0;
		t_lpkit		= 0;
		t_tools		= 0;
		t_care		= 0;
		t_bulkf		= 0;
				
		for (i=0; i<items_array.length; i++) {
			
			if (row>0) { new_info = new_info + ","; }
			item_fields 	= items_array[i].split('!');			
			test_item_cost	= parseFloat(item_fields[6]);
			item_type		= item_fields[8];
			
			var new_qty = eval('document.cart_form.line_' + i + '_qty.value');
			item_fields[9] 	= new_qty;			

			test_line_ttl	= test_item_cost * new_qty;				//	alert('test_line_ttl is now ' + test_line_ttl);
			test_p_ttl		= test_p_ttl + test_line_ttl;			//	alert('test_p_ttl is now ' + test_p_ttl);

			if (item_type=="ppkit") { t_ppkit = t_ppkit + test_line_ttl; }
			if (item_type=="lpkit") { t_lpkit = t_lpkit + test_line_ttl; }
			if (item_type=="tools") { t_tools = t_tools + test_line_ttl; }
			if (item_type=="care") { t_care = t_care + test_line_ttl; }
			if (item_type=="bulkf") { t_bulkf = t_bulkf + test_line_ttl; }

			this_item		= item_fields.join('!');
			new_info 		= new_info + this_item;			
			row 			= row + 1;
		}		
		setCookie("o_items",new_info);
		
		setCookie("o_ppkit",t_ppkit);
		setCookie("o_lpkit",t_lpkit);
		setCookie("o_tools",t_tools);
		setCookie("o_care",t_care);
		setCookie("o_bulkf",t_bulkf);

		ship_rates 	= new Array ("NOT",0,"GND",8.50,"BLU",17.00,"RED",34.00,"OS1",0);				/* ########### USE variables table in DB ############## */
		rate_code 	= document.cart_form.ship_type.value;
//		this_rate	= 1.0;
//		for (i=0; i<5; i++) {
		for (i=0; i<ship_rates.length/2; i++) {
			if (ship_rates[2*i]==rate_code) { this_rate = ship_rates[2*i+1] }
		}
		ship_cost	= this_rate;
		
		setCookie("o_ship_code",rate_code);
		setCookie("o_ship",ship_cost);
		
		tax_code 	= document.cart_form.tax_menu.value;
		tax_rates 	= new Array ("NOT",0,"TAX",0.0825,"OUT",0,"TST",0.1);							/* ########### USE variables table in DB ############## */

		for (i=0; i<tax_rates.length/2; i++) {
			if (tax_rates[2*i]==tax_code) { this_tax = tax_rates[2*i+1] }
		}
		
//		tax_amt		= (prod_ttl + ship_cost) * this_tax;
		
		taxable		= test_p_ttl + ship_cost;
		tax_amt		= taxable * this_tax;
		
		setCookie("o_tax_code",tax_code);
		setCookie("o_tax",tax_amt);


//		total = prod_ttl + ship_cost + this_tax;
		total = test_p_ttl + ship_cost + tax_amt;
		setCookie("o_total",total);

		location = "cart.php";
	}

		
	function deleteItem(item_line_no) {
	
		items_info 	= getCookie('o_items');
		items_array = items_info.split(',');
		new_info	= "";
		row			= 0;
		for (i=0; i<items_array.length; i++) {
			if (i != item_line_no) { 
				if (row>0) { new_info = new_info + ","; }
				new_info = new_info + items_array[i];
				row = row + 1;
			}			
		}
		setCookie("o_items",new_info);
		location = "cart.php";
	}


	function qtyChange (item_line_no) {
	
		var new_qty = eval('document.cart_form.line_' + item_line_no + '_qty.value');
		if (new_qty == 0) {
			deleteItem(item_line_no);
		} else {
			updateCart();
		}
	}		
	
	function add2cart(code, ver, year, make, model, subm, vers, desc, price, ship, type) {
		var new_item 		= "";
		var items_info 		= "";
		var new_items_info	= "";
		var v_def			= "";
		var alreadyExists   = 0;
		var recordNo		= 0;		 
		var list_new_item	= "";
		var qty =1;
		if(document.getElementById('qty_'+code+ver)){
			var temp_qty = document.getElementById('qty_'+code+ver).value;
			if(parseInt(temp_qty)==temp_qty && temp_qty > 0){
				var qty = temp_qty;
			}
				
		}
		new_item 			= code + '!' + ver + '!' + make + '!' + model + '!' + year + '!' + desc + '!' + price + '!' + ship + '!' + type + '!'+qty;
		if (getCookie('o_items')!="" && getCookie('o_items')!=null) {
			items_info 	= getCookie('o_items');
			///// Start Code added by qasim
			var listRecords = items_info.split(",");
			
			for(i=0; i<listRecords.length; i++){
				productInfo = listRecords[i].split("!");
				if(productInfo[0]==code && productInfo[1]==ver && productInfo[2]==make && productInfo[3]==model && productInfo[4]==year && productInfo[8]==type ){
					list_new_item = code + '!' + ver + '!' + make + '!' + model + '!' + year + '!' + desc + '!' + price + '!' + ship + '!' + type + '!'+ ((qty*1)+(productInfo[9]*1));		
					alreadyExists=1;
				}
				else{
					list_new_item = productInfo[0] + '!' + productInfo[1] + '!' + productInfo[2] + '!' + productInfo[3] + '!' + productInfo[4] + '!' + productInfo[5] + '!' + productInfo[6] + '!' + productInfo[7] + '!' + productInfo[8] + '!' + productInfo[9] ;	
				}
				if(recordNo==0){
					list_new_records = list_new_item;	
					recordNo++;
				}
				else{
					list_new_records = list_new_records+','+list_new_item;	
					recordNo++;
				}
				
			}////end for loop
			if(alreadyExists==1){
					items_info = list_new_records; 
			}
			else{
					items_info = list_new_records + ',' + new_item; 
			}
			new_items_info = items_info ;
			///alert(new_items_info);
			////End code added by qasim
			////new_items_info = items_info + ',' + new_item;
		} else { 
			new_items_info = new_item;
		}		
		setCookie("o_items",new_items_info);
		var msg = year + " " + model + " " + desc + " Added To Cart";
		var firstpos		= location.href.lastIndexOf('/')+1;
		var lastpos			= location.href.length;
		var filename 		= location.href.substring(firstpos,lastpos);				// alert('filename is now ' + filename);
		if (filename!="cart.php") { 
			alert(msg); 																// var we_are_here	= location.pathname + location.search;	alert('this page is ' + we_are_here);
			if (location.search=="") {													// if there is no query string... (there should be if we are in the vehicle_page...
				if (filename=="vehicle_page.php") {
					if (getCookie('v_def')!="" && getCookie('v_def')!=null) { 
						v_def 	= getCookie('v_def');									// alert('v_def is now + *' + v_def + '*');
						ret_addr = location.pathname + v_def;							// var we_are_here	= location.pathname + location.search;	alert('this page is ' + we_are_here);
					} else {
						ret_addr = location.pathname + "?make=" + make + "&model=" + model + "&subm=" + subm + "&vers=" + vers + "&year=" + year;
					}
				} else {
					ret_addr = location.pathname + "?make=" + make + "&model=" + model + "&subm=" + subm + "&vers=" + vers + "&year=" + year;
				}
			} else {
				ret_addr = location.pathname + location.search;
			}	
		} else {
			ret_addr = "cart.php";
		}
		location = ret_addr;
	}

	var image_was;
	
	function showMsg(message) {
		
		image_was = document.getElementById('vehicle_graphic').innerHTML
		document.getElementById('vehicle_graphic').innerHTML=message;
	}
	
	function hideMsg() {
		
		document.getElementById('vehicle_graphic').innerHTML=image_was;
	}
	
	

	
