var showtotalprice = true;
var printerfriendly = false;
var expDays = 30;
var g_exp = new Date(); 
g_exp.setTime(g_exp.getTime() + (expDays*24*60*60*1000));
var ShowCount = 0;
var swapcolor = 1;
var totalprice = 0.0;
function addtowishlist(item) {
	wishlist = GetCookie('wishlist');
	if (wishlist==null) wishlist = '';
	if (wishlist.length>0)
		delim = "~";
	else
		delim = "";
	if (item != null) {
		itemid = item.substring(0,item.indexOf("|"));
		if (wishlist.indexOf(itemid + "|")==-1) {
			wishlist += delim + item + "|1";
			SetCookie('wishlist', wishlist);
			alert(item.split("|")[1].split("[link]")[0] + " has been added to your wishlist.");
		}
		else {
			alert(item.split("|")[1].split("[link]")[0] + " is already on your wishlist.");
		}
	}
}

function deletefromwishlist(deleteitem) {
	wishlist = GetCookie('wishlist');
	if (wishlist==null)
		wishlist = '';
	else {
		items = wishlist.split("~");
		wishlist = "";
		delim = "";
		for (var i=0; i<items.length; i++) {
			if (items[i].split("|")[0] != deleteitem) {
				wishlist += delim + items[i];
				delim = "~";
			}
		}
	}
	SetCookie('wishlist', wishlist);
	window.location = window.location;
}

function printtablerow(row,halign) {
	var color = "";
	swapcolor = 1 - swapcolor;
	if (swapcolor==1) {color = "bgcolor='#f0f0f0'"} ;
	if (ShowCount==0)
		valign = "bottom";
	else
		valign = "top";
	document.write("<tr " + color + " valign=\"" + valign + "\">");
	
	cells = row.split("|");
	a_halign = halign.split("|");
	if (ShowCount==0)
		document.write("<td class=\"wishlist\"></td>");
	else {
		document.write("<td class=\"wishlist\">" + ShowCount + "</td>");
		cells[cells.length] = formatnumber(cells[cells.length-1] * cells[cells.length-2]);
	}
	
	for (var i=0; i<cells.length; i++) {
		subcells = cells[i].split("[link]");
		
		if ((i==cells.length-1 || i==cells.length-3) && ShowCount>0) {
			subcells[0] = formatnumber(parseFloat(subcells[0]));
		}
		document.write("<td class=\"wishlist\" align=\"" + a_halign[i] + "\">");
		
		if (i==cells.length-2 && ShowCount>0 && !printerfriendly)
			document.write("<input style=\"text-align:right;width:25px\" type=\"text\" id=\"" +
				cells[0] + "\" value=\"" + cells[i] + "\" onBlur=\"javascript:checknumber(this)\">");
		else if (subcells.length==1 || printerfriendly)
			document.write(subcells[0]);
		else
			document.write("<a href='" + subcells[1] + "'>" + subcells[0] + "</a>");
		document.write("</td>");
	}
	if (ShowCount==0 || printerfriendly)
		document.write("<td></td>");
	else
		document.write("<td class=\"wishlist\">" +
			"<a href=\"javascript:deletefromwishlist('" + cells[0] +
			"')\" title=\"Delete this item (" + cells[0] +
			")\"><img border=\"0\" src=\"images/trash.gif\"></a></td>");
	document.write("</tr>");
	ShowCount++;
	
	//calculate total
	price = Math.round(parseFloat(cells[cells.length-1])*100)/100;
	if (!isNaN(price)) totalprice += price;
}

function wishlistform(formname,fieldname,fields,divider,totaldivider) {
	tempobj = eval("document." + formname + "." + fieldname);
	var out = "";
	var extendedprice = 0;
	var total = 0;
	wishlist = GetCookie('wishlist');
	if (wishlist==null)
		wishlist = '';
	else {
		a_fields = fields.split("|");
		items = wishlist.split("~");
		for (var i=0; i<items.length; i++) {
			cells = items[i].split("|");
			for (var j=0; j<a_fields.length-1; j++) {
				subcell = cells[j].split("[link]");
				out += a_fields[j] + subcell[0];
			}
			extendedprice = formatnumber(cells[cells.length-1] * cells[cells.length-2]);
			total += parseFloat(extendedprice);
			out += a_fields[a_fields.length-1] + extendedprice;
			out += divider;
		}
		out += totaldivider + formatnumber(total);
		
	}
	tempobj.value = out;
}
function recalculate() {
	wishlist = GetCookie('wishlist');
	if (wishlist==null)
		wishlist = '';
	else {
		items = wishlist.split("~");
		wishlist = "";
		delim = "";
		for (var i=0; i<items.length; i++) {
			a_item = items[i].split("|");
			tempobj = eval("frmupdate." + a_item[0]);
			a_item[a_item.length-1] = tempobj.value;
			items[i] = a_item.join("|");
			wishlist += delim + items[i];
			delim = "~";
		}
	}
	SetCookie('wishlist', wishlist);
	window.location = window.location;
}

function checknumber(obj) {
	if (isNaN(obj.value) || obj.value.indexOf(".")!=-1 || obj.value<1) {
		alert("'" + obj.value + "' is not a valid quantity.\nPlease delete this item or enter a whole number greater than 0.");
		obj.value = "1";
		obj.select();
	}
}

function formatnumber(num) {
	num = Math.round(num*100)/100;
	strnum = String(num);
	if (strnum.indexOf(".")==-1)
		strnum += ".00";
	else {
		strnum += "00";
		strnum = strnum.substring(0,strnum.indexOf(".")+3);
	}
	return strnum;
}

function clearwishlist() {
	SetCookie("wishlist", "");
	document.location = document.location;
}

function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
	endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	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;
}

function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : (g_exp);
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? ";path=/" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() -1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
