

// Alan Simpson - www.coolnerds.com
function currency(anynum)
{
	//-- Returns passed number as string in $xxx,xxx.xx format.
	anynum=eval(anynum)
	workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
	if (workStr.indexOf(".")==-1){workStr+=".00"}
	dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
	pStr=workStr.substr(workStr.indexOf("."))
	while (pStr.length<3){pStr+="0"}

	//--- Adds comma in thousands place.
	if (dNum>=1000) {
	    dLen=dStr.length
	    dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
	}

	//-- Adds comma in millions place.
	if (dNum>=1000000) {
	    dLen=dStr.length
	    dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
	}
	retval = dStr + pStr 
	//-- Put numbers in parentheses if negative.
	if (anynum<0) {retval="("+retval+")"}
	return "$"+retval
}

var orderTotal = 0;
var orderSubTotal = 0;
var orderDiscount = 0;
var orderDiscountTotal = 0;
var numItems = 0;
var descItems = "";

var numScreensavers = 0;

var outputForm = 0; // set to 1 when we're ready to send the data to paypal
var formStr = "";
var itemCount = 0;
var applyDiscount = 0; // because the function isnt sure how many items there are, count first and set this if they bought more than 1

// paypal variables for sending the data in
var ppID = "paypal@uselesscreations.com";
var ppCur = "USD";
var ppCancel = "http://www.uselesscreations.com";
var ppReturn = "http://www.uselesscreations.com/registered/thankyou.php";
var ppNotify = "http://www.uselesscreations.com/download/ipn.php";

function CallPayPal()
{ 
	// products should already have been counted in the call to SelectionChange in CheckOut
	// so if we have more than one, lets give the discount
	if(numScreensavers > 1)
		applyDiscount = 1;

	// reset values
	outputForm = 1; // start outputting
	formStr = "";
	itemCount = 0;

	var winpar = "width=800,height=600,scrollbars," +
      	       "location,resizable,status,toolbar";
	formStr   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" +
             	"&upload=1" +
	            "&business=" + ppID;

    formStr = formStr + "&no_note=1";
    formStr = formStr + "&no_shipping=1";

    formStr = formStr + "&currency_code=" + ppCur;
    formStr = formStr + "&cancel_return=" + ppCancel;
    formStr = formStr + "&return=" + ppReturn;
    formStr = formStr + "&notify_url=" + ppNotify;

	AddProducts();

	outputForm = 0;
	applyDiscount = 0; // turn discount off incase they return to the page and change their options

	if (itemCount > 0) window.open (formStr, "paypal", winpar);

	return false;
}


function AddProd(name, abbrev, itmnum, price, discount)
{
	if(document.getElementById(abbrev).checked)
	{
		numScreensavers++;

		if(descItems != "")
		{
			descItems += ", "
		}
		descItems += name
		orderSubTotal += price;
		if(numItems > 0) // only apply the discount to each ADDITIONAL item purchased
			orderDiscountTotal += discount;
		numItems++;

// if outputForm is true we're building the data to be sent to paypal
// so we need to output the data for this product
		if(outputForm > 0) {
		      var itemPrice = price;
			  if(applyDiscount > 0 && itemCount > 0) itemPrice = price - discount;
			  
		      itemCount = itemCount + 1;
		      formStr = formStr + "&item_name_"    + itemCount + "=" + escape (name) +
            			        "&item_number_"  + itemCount + "=" + escape (itmnum) +
      	            		  "&quantity_"     + itemCount + "=" + "1" +
			                    "&amount_"       + itemCount + "=" + itemPrice;
		}
	}
}


function AddProdDesc(desc, num, type)
{
	if(num > 0)
	{
		if(desc != "")
			desc += ","
		desc += " " + num + type
		if(num > 1)
			desc += "s"
	}
	return desc;
}


function SelectionChange()
{
	orderTotal = 0;
	orderSubTotal = 0;
	orderDiscount = 0;
	orderDiscountTotal = 0;
	numItems = 0;
	descItems = "";
	numScreensavers = 0;

	AddProducts();
	if(numItems > 1)
		orderDiscount = Math.abs(orderDiscountTotal);
	orderTotal = orderSubTotal - orderDiscount;
	
	var st = document.getElementById("subtotal")
	if(st)
	{	
		st.innerHTML = currency(orderSubTotal);
	}
	var dis = document.getElementById("discount")
	if(dis)
	{
		dis.innerHTML = currency(orderDiscount);
	}
	document.getElementById("total").innerHTML = currency(orderTotal);

	var desc = ""

	desc = AddProdDesc(desc, numScreensavers, " screen saver")

	if(numItems == 0)
		desc = ""
	else
		desc = "You have selected" + desc + "."

	document.getElementById("item_description").innerHTML = desc;
}

function CheckOut()
{
	SelectionChange();
	if(descItems == "" || orderTotal == 0)
	{
		alert("Check the boxes next to the products you wish to buy, and then click Proceed To Checkout.");
		return false;
	}
	else
	{
		return CallPayPal();
	}
}


function SelectProduct(id)
{
	if(QueryString("pc") == id)
	{
		document.getElementById(id).checked = true;
	}

	if(QueryString("pc") != null)
	{
		if(QueryString("pc").toUpperCase() == id.toUpperCase())
		{
			document.getElementById(id).checked = true;
		}
	}
}

function OnLoad()
{
	SelectProducts();	
	SelectionChange();
}


//
// QueryString
//

function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	if(query == null)
		return;
	query = unescape(query);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}

QueryString_Parse();

