// JavaScript Document
// Thanks to PayPalHelper for the base code http://members.aol.com/paypalhelper/
// Tweeked by Chris Horgan www.bottomup.com.au
var order = new Object();  // object to store order items in
var root  = new Object();  // selection criteria
var on    = true;
var off   = false;
var cntr  = 0;             // items in object
var opts  = 5;             // number of options to allow
var totalOrderPrice=0,totalQuantity=0;  // totals

// place for user-specific options
txtNotifyURL  = ""; // place for PayPal ipn path
txtCancelURL  = ""; // place for PayPal cancel return path
txtCurrency  = ""; // enter default currency code (or null)
txtPayPalID   = ""; // PayPal ID
txtImageURL  = ""; // image URL
txtLanguageCode   = ""; // enter default country code (or null)
txtFinishedURL  = ""; // place for PayPal return path
txtPayPalStyle  = ""; // place for PayPal page style
txtPayPalOther = ""; // place for other PayPal commands
txtCustom = ""; // place for custom argument not seen by buyer

function clearForms() // Function used to reset the form values if the user goes back. Otherwise the sums won't add up.
{
  var i;
  for (i = 0; (i < document.forms.length); i++) {
     document.forms[i].reset();
  }
}


function Dollar (val) {     // force to valid dollar amount
	var str,pos,rnd=0;
	  if (val < .995) rnd = 1;  // for old Netscape browsers
	  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
	  pos = str.indexOf (".");  // should be one, but OK if not
	  if (pos > 0) str = str.substring (rnd, pos + 3);
	  return str;               // return valid string
}

function GetOrder (id, descrip, priceEach) {
	// this function processes and calulates the order by line item
	var i,nr,val,quantity,pos;
	var op = new Array ();    // accumulate options here
	  totalOrderPrice=0,totalQuantity=0;   // zero totals
	  nr = id.substring (2);  // get number part of ID
	  quantity = document.formOrder["txtQuantity" + nr].value;  // get quantity
	  
	  // validate quantity
	  if (isNaN (quantity)) { 
		alert ("I need a quanity! How many would you like? ");
		return;
	  }
	  // look for options
	  for (i=1; i<=opts; i++) {    
		if (document.formOrder["op" + i + nr]) {
		  val = document.formOrder["op" + i + nr].value;  // get option
		  op[i] = val;
		  pos  = val.indexOf ("+"); // price increment?
		  if (pos > 0) priceEach = priceEach + val.substring (pos + 1)*1.0;
		  pos  = val.indexOf ("%"); // percent change?
		  if (pos > 0) priceEach = priceEach + (priceEach * val.substring (pos + 1)/100.0);
		}
		else op[i] = "";
	  }
	  
	  // write the line price total
	  document.formOrder["txtPrice" + nr].value = Dollar (quantity * priceEach);
	  
	  if (cntr == 0) order = new Object (); // zap object
	  cntr = cntr + 1;               // bump counter so no zap next time
	  
	  // create a order array and store details
	  order[id] = new Object (); 
	  for (i=1; i<=opts; i++)        // load options
		if (op[i] != "") descrip = descrip + ", OP" + i + "=" + op[i];
	  order[id].descrip = descrip;           // load up values
	  order[id].priceEach = Dollar (priceEach);
	  order[id].quantity = quantity;
	  
	  // calculated the Totals
	  for (i in order) {      
		quantity = order[i].quantity*1.0;
		totalOrderPrice = totalOrderPrice + order[i].priceEach * quantity;  // total amount
		totalQuantity = totalQuantity + quantity;                 // total quantity
	  }
	  
	  // write the Subtotal
	  document.formOrder["txtSubtotal"].value = Dollar (totalOrderPrice);
	  
}

function GetFeedback(){
	 //Get Custom arguments

	 txtDetailReferral= document.formOrder["detailReferral"].value;
	 
	 txtCustom = "";
	 if (document.formOrder.vetReferral.checked) txtCustom = txtCustom + "Vet_"; 
	 if (document.formOrder.magReferral.checked) txtCustom = txtCustom + "Mag_"; 
	 if (document.formOrder.friendReferral.checked) txtCustom = txtCustom + "Friend_"; 
	 if (document.formOrder.searchReferral.checked) txtCustom = txtCustom + "Search_"; 
	 if (document.formOrder.newsReferral.checked) txtCustom = txtCustom + "News_"; 
	 if (document.formOrder.otherwebReferral.checked) txtCustom = txtCustom + "Web_"; 
	 if (document.formOrder.tvReferral.checked) txtCustom = txtCustom + "TV_"; 
	 if (document.formOrder.otherReferral.checked) txtCustom = txtCustom + "Other_"; 
	 if (txtDetailReferral.length > 0) txtCustom = txtCustom + "Details:" + txtDetailReferral; 
	 
}

function SendCart () {  
	// this function sends the cart to PayPal
	var frst = true;  // 1st pass thru items.
	var winpar = "width=710,height=390,scrollbars," + "location,resizable,status";
	
	// Live site:
	var strn   = "https://www.paypal.com/cgi-bin/webscr?cmd=_cart" + "&upload=1" + "&business=" + txtPayPalID + txtPayPalOther;
	// Test site:
	//var strn   = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_cart" + "&upload=1" + "&business=" + txtPayPalID + txtPayPalOther;
	
	var i,j=0,descrip;
	
	//Send account info
	  if (txtCurrency.length > 0) 		strn = strn + "&currency_code=" + txtCurrency;
	  if (txtLanguageCode.length > 0) 	strn = strn + "&lc=" + txtLanguageCode;
	  if (txtNotifyURL.length > 0) 		strn = strn + "&notify_url=" + txtNotifyURL;
	  if (txtCancelURL.length > 0) 		strn = strn + "&cancel_return=" + txtCancelURL;
	  if (txtFinishedURL.length > 0)	strn = strn + "&return=" + txtFinishedURL;
	  if (txtPayPalStyle.length > 0)	strn = strn + "&page_style=" + txtPayPalStyle;
	  if (txtImageURL.length > 0)		strn = strn + "&image_url=" + txtImageURL;
	  if (txtCustom.length > 0)			strn = strn + "&custom=" + txtCustom;
	
	  //Send order line items
	  for (i in order) {  // send all valid data
		if (order[i].quantity > 0) {
		  j = j + 1;
		  descrip = order[i].descrip;
		  strn = strn + "&item_name_"    + j + "=" + escape (descrip) +
			//          "&item_number_"  + j + "=" + i +
						"&quantity_"     + j + "=" + order[i].quantity +
						"&amount_"       + j + "=" + order[i].priceEach;
		  frst = false;
		}
	  }
	  // Debug
	  // alert (strn)
	  //if (j > 0) window.open (strn, "paypal", winpar);  	// for a new window popup
	  if (j > 0) window.location=strn;						// use existing window
}


