// 
// RobotLogic.com Javascript
// begun Jan 20,2002
// 

//
// rlPrintDate
// Print the current date into HTML at the location of the called code
//
function rlPrintDate()
{
	var now = new Date();
	var dayNames = new Array( "Sunday", "Monday", "Tuesday", 
				  "Wednesday", "Thursday", "Friday", "Saturday");
	var monNames = new Array( "January", "February", "March", 
				  "April", "May", "June", "July", 
                                  "August", "September", "October", 
                                  "November", "December");
	document.write(	dayNames[now.getDay()] + 
			" " + monNames[now.getMonth()] + 
			" " + now.getDate() + ", "   + 
			now.getFullYear());
}

//
// Show our e-mail address in a way that is safe from the web spam harvesters!
//
function rlShowContactAddress()
{
	var name = "support";
	var host = "robotlogic.com";
	var delim = "\@";
	var href = 'mailto:';
 	var text = '\<a href="' + href + name + delim + host + '">';
	document.write(text); 
	text = name + delim + host;
	document.write(text);
	document.write("</a>");
}


//------------------------------------------------------------------------------------------------- 
// 
// RobotLogic Debugging Code
// Functions useful in debugging...
// 
//------------------------------------------------------------------------------------------------- 

//
// rlDebugDumpForm0
// Debugging, dump the description of the first form on the page.  Useful for testing
// whether your javascript created html is really what you wanted...
//
function rlDebugDumpForm0()
{
	if (document.forms[0] != null) {
		document.write('<TABLE border=2>');
		document.write('<TR><TD>index</TD><TD>name</TD><TD>type</TD><TD>value</TD></TR>');
		for (var i=0;i<document.forms[0].length;i++) {
			current = document.forms[0].elements[i];
			document.write('<TR><TD>' + i);
			document.write('<TD>' + current.name);
			document.write('<TD>' + current.type);
			document.write('<TD>' + current.value + '</TR>');
		}
		document.write('</TABLE>');
	}
}


//------------------------------------------------------------------------------------------------- 
// 
// RobotLogic Cookie Handling Code
// In javascript, we access this data through "document.cookie".  This is a string containing of
// name=value pairs separated by semicolons.  
// 
//------------------------------------------------------------------------------------------------- 

// 
// rlFixDate
// This function is apparently needed to work around a bug in old versions of netscape and Mac browsers...
//
function rlFixDate(date) 
{
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) date.setTime(date.getTime() - skew);
}

// 
// rlSetCookie
// This function sets a "sub-cookie" (my term) of the given name.  
//
function rlSetCookie(name, value, expdate) 
{
	// If no expiration is specified, all of our cookies expire in 10 days
	if (!expdate) {
		expdate = new Date();
		expdate.setTime(expdate.getTime() + 1 * 24 * 60 * 60 * 1000);
	}
	document.cookie = name + "=" + escape (value) + "; expires=" + expdate.toGMTString() +  "; path=/";
}

//
// rlGetCookie
// This function returns the value of the specified "sub-cookie" (my term again).
//
function rlGetCookie(name) 
{
	var doc_cookie = document.cookie; 
	var doc_cookie_len = doc_cookie.length;
	var sub_cookie = name + "=";

	// sub_cookie_start - index of the first character of the sub-cookie name
	// val_start - index of the first character of the value
	// val_end - index of the end of the value
	var sub_cookie_start=0;
	
	while (sub_cookie_start < doc_cookie_len) {
        	var val_start = sub_cookie_start + sub_cookie.length;
		if (doc_cookie.substring(sub_cookie_start,val_start) == sub_cookie) { 
			var val_end = doc_cookie.indexOf (";", val_start);
			if (val_end == -1) val_end = doc_cookie_len;
            		return unescape(doc_cookie.substring(val_start,val_end));
		}
		sub_cookie_start = doc_cookie.indexOf(" ", sub_cookie_start) + 1;
		if (sub_cookie_start == 0) break;
	}
	return null;
}

//
// rlDeleteCookie
// This function deletes the specified "sub-cookie" (my term again).
//
function rlDeleteCookie(name) 
{
	document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}


// ---------------------------------------------------------------------------------------------
//
// RobotLogic Shopping Cart
// The state of the shopping cart will stored in several cookies associated with our web page.
//
// We will use a cookie named "ItemCount" and cookies named "Item_0".."Item_N" to encode the
// contents of the shopping cart.
//
// ---------------------------------------------------------------------------------------------

var ORDER_COUNT_COOKIE = "RL_Order_Count";
var ORDER_PREFIX = "RL_Order_";

//
// rlFormatCurrencyString 
// Cleans up a string to have two digits after the decimal, etc
//
function rlFormatCurrency(input) 
{
	// Round to number of cents
	var tmp = Math.round(input*100);
	var dollars = "" + Math.floor(tmp/100);
	var cents = "" + tmp;
	cents = cents.substring(cents.length-2,cents.length);

	return dollars + "." + cents;
}

//
// rlGetOrderCount
// Get the order count cookie
//
function rlGetOrderCount()
{
	var order_count = rlGetCookie(ORDER_COUNT_COOKIE);
	if (parseInt(order_count) > 0) {
		order_count = parseInt(order_count);
	} else {
		order_count = 0;
		rlSetCookie(ORDER_COUNT_COOKIE,order_count);
	}
	//alert("order_count = " + order_count);
	return order_count;
}

//
// rlAddItemToCart
// Add the specified item to the user's shopping cart
//
function rlAddItemToCart(add_name,add_description,add_price)
{
	var found_it = false;
	var message = "";
	var order_count = rlGetOrderCount();

	// If this item is already in the cart, just bail out
	for (i=0; i<order_count; i++) {
		var cookie_name = ORDER_PREFIX + i;
		var cookie = rlGetCookie(cookie_name);

		var item_end = cookie.indexOf("|",0);
		var desc_end = cookie.indexOf("|",item_end+1);
		var count_end = cookie.indexOf("|",desc_end+1);
		var price_end = cookie.length;
		
		var item = cookie.substring(0,item_end);
		var description = cookie.substring(item_end+1,desc_end);
		var count = cookie.substring(desc_end+1,count_end);
		var price = cookie.substring(count_end+1,price_end);

		if ((item == add_name)/* && (price == add_price)*/) {
			found_it = true;
			break;
		}
	}

	// If this item wasn't found, add a new cookie
	if (found_it == false) {
		var cookie_name = ORDER_PREFIX + order_count;
		var cookie_val = add_name + "|" + add_description + "|" + "1" + "|" + add_price;
		rlSetCookie(cookie_name,cookie_val);
		order_count++;
		rlSetCookie(ORDER_COUNT_COOKIE,order_count);
		message = "1 " + add_name + " is in your shopping cart.";
	}

	// go to the shopping cart!
	location.href = "shopping_cart.html";
}

function rlDebugDumpCart()
{
	var order_count = rlGetOrderCount();
	document.write("rlDisplayCart debugging<br>");
	document.write(order_count + "<br>");
	for (i=0; i<order_count; i++) {
		var cookie_name = ORDER_PREFIX + i;
		var cookie = rlGetCookie(cookie_name);
		document.write(i + " = " + cookie + "<br>");
	}
}

function rlDisplayCart()
{
	var order_count = rlGetOrderCount();
	
	// Don't display the table if the cart is empty
	if ((order_count < 1) || (order_count == null)) {
		document.write("<center><p>Your shopping cart is currently empty.</center><br>");
		return;
	} 

	// Build the table for the shopping cart
	// Cart Header:
	document.write("<FORM name=\"Cart\" onsubmit=\"return false\">");
	document.write("<TABLE Width=75%>");
	document.write("<TR><TD class=\"CartHeader\"><B>Item</B></TD>");
	document.write("<TD class=\"CartHeader\"><B>Quantity</B></TD>");
	document.write("<TD class=\"CartHeader\"><B>Price</B></TD>");
	document.write("<TD class=\"CartHeader\"><B>Total</B></TD></TR>");
	var sub_total = 0;

	var paypal_cart_description = "";

	for (i=0; i<order_count; i++) {
		var cookie_name = ORDER_PREFIX + i;
		var cookie = rlGetCookie(cookie_name);

		var item_end = cookie.indexOf("|",0);
		var desc_end = cookie.indexOf("|",item_end+1);
		var count_end = cookie.indexOf("|",desc_end+1);
		var price_end = cookie.length;
		
		var item = cookie.substring(0,item_end);
		var description = cookie.substring(item_end+1,desc_end);
		var count = cookie.substring(desc_end+1,count_end);
		var price = cookie.substring(count_end+1,price_end);

		var item_total = price * count;
		sub_total += item_total;

		// Display this item.  
		document.write("<TR><TD class=\"CartItem\">" + description + "</TD>");
		document.write("<TD ALIGN=CENTER>");
		document.write("<input type=number size=4 value=");
		document.write(count);
		document.write(" name = \"Quantity_" + i + "\">");
		document.write("</TD>");
		document.write("<TD class=\"CartPrice\">" + rlFormatCurrency(price) + "</TD>");
		document.write("<TD class=\"CartPrice\">" + rlFormatCurrency(item_total) + "</TD></TR>");
		
		// add this item to our paypal cart description string.
		paypal_cart_description += count + " x " + description + ": $" + rlFormatCurrency(item_total) + "<br>";
	}
	
	var total = sub_total;

	// Bottom half of the table, sub-total, shipping, total
	document.write("<TR><TD COLSPAN=4><HR></TD></TR>");

	// Sub-Total:
	document.write("<TR><TD COLSPAN=3 class=\"CartItem\">SubTotal:</TD>");
	document.write("<TD class=\"CartPrice\">" + rlFormatCurrency(sub_total) + "</TD></TR>");
	paypal_cart_description += "Sub Total: " + sub_total + "\n";

	// Sales Tax:
/*
	document.write("<TR><TD COLSPAN=3 class=\"CartItem\">Sales Tax: ");
	document.write("<SELECT name=\"Tax\">");
	document.write("<OPTION SELECTED>Outside California</OPTION>");
	document.write("<OPTION>To California (8.25%)</OPTION>");
	document.write("</SELECT></TD>");
	document.write("<TD class=\"CartPrice\">$0.00</TD></TR>");
*/
	// Shipping:
	var shipping = 5.00;
	document.write("<TR><TD COLSPAN=3 class=\"CartItem\">Shipping: ");
//	document.write("<SELECT name=\"Shipping\"> ");
//	document.write("<OPTION SELECTED>Free - for a limited time</OPTION>");
//	document.write("</SELECT></TD>");
	document.write("<TD class=\"CartPrice\">" + rlFormatCurrency(shipping) + "</TD></TR>");
	paypal_cart_description += "Shipping: " + shipping + "\n";
	total += shipping;

	// Total Price:
	document.write("<TR><TD COLSPAN=3 class=\"CartItem\">Total:</TD>");
	document.write("<TD class=\"CartTotal\">" + rlFormatCurrency(total) + "</TD></TR>");
	paypal_cart_description += "Total Price: " + total + "\n";
	
	// Table is done!
	document.write("</TABLE>");
	
	// Update cart button
	document.write("<INPUT TYPE=\"button\" NAME=Update VALUE=\"Update Quantities\" onClick=\"rlRefreshCart(this)\">");
	
	// Form is done!
	document.write("</FORM>");

	// PayPal Purchase button!
/*
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="sales@robotlogic.com">
<input type="hidden" name="item_name" value="ITEM_NAME">
<input type="hidden" name="item_number" value="ITEM_ID">
<input type="hidden" name="amount" value="9.99">
<input type="hidden" name="return" value="http://www.renegaderobots.com/thankyou.html">
<input type="hidden" name="cancel_return" value="http://www.renegaderobots.com/temp/cancel.html">
<input type="image" src="http://images.paypal.com/images/x-click-but01.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
*/
	document.write("<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">");
	document.write("<input type=\"hidden\" name=\"cmd\" value=\"_xclick\">");
	document.write("<input type=\"hidden\" name=\"business\" value=\"sales@robotlogic.com\">");
	document.write("<input type=\"hidden\" name=\"item_name\" value=\"" + paypal_cart_description + "\">");
	document.write("<input type=\"hidden\" name=\"item_number\" value=\"0\">");
	document.write("<input type=\"hidden\" name=\"amount\" value=\"" + rlFormatCurrency(total) + "\">");
	document.write("<input type=\"hidden\" name=\"return\" value=\"http://www.renegaderobots.com/temp/thankyou.html\">");
	document.write("<input type=\"hidden\" name=\"cancel_return\" value=\"http://www.renegaderobots.com/temp/cancel.html\">");
	document.write("<input type=\"image\" src=\"http://images.paypal.com/images/x-click-but01.gif\" border=\"0\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\">");
	document.write("</form>");

}


function rlRefreshCart(cart_form)
{	
	var order_count = rlGetOrderCount();
	var write_count = 0;

	//
	// Loop over the orders, updating the quantities.  If a quantity is <=0
	// then delete the cookie for it, decrement the final count, and collapse
	// the remaining cookies
	//
	for (i=0; i<order_count; i++) {

		// Decode this item
		var cookie_name = ORDER_PREFIX + i;
		var cookie = rlGetCookie(cookie_name);

		var item_end = cookie.indexOf("|",0);
		var desc_end = cookie.indexOf("|",item_end+1);
		var count_end = cookie.indexOf("|",desc_end+1);
		var price_end = cookie.length;
		
		var item = cookie.substring(0,item_end);
		var description = cookie.substring(item_end+1,desc_end);
		var count = cookie.substring(desc_end+1,count_end);
		var price = cookie.substring(count_end+1,price_end);
		
		// Check the form for a new count for this item		
		var new_count = eval("document.Cart.Quantity_" + i).value;
		if (new_count > 0) {
			var updated_cookie = item + "|" + description + "|" + new_count + "|" + price;
			var new_cookie_name = ORDER_PREFIX + write_count;			
			rlSetCookie(new_cookie_name,updated_cookie);
			write_count++;
		}
	}

	// The new order count is equal to write index
	rlSetCookie(ORDER_COUNT_COOKIE,write_count);

	// Delete cookies between write_count and order_count
	for (i=write_count; i<order_count; i++) {
		rlDeleteCookie(ORDER_PREFIX + i);
	}

	// Now reload the page
	location.href = "shopping_cart.html";
}


