function open_window(link,w,h) //opens new window
{
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'newWin',win);
	newWin.focus();
}

// if we have a refID and can set it as a cookie = true, else=false
function cookieCheck () {
	var sRefID = queryString("refID");
	//alert("cookieCheck " + sRefID);
	// only process if we'd want to set a refid
	if (sRefID != "" && sRefID != "false")
	{
	  document.cookie = "cookieTest=test; expires=; path=/";
	  if (document.cookie == "")
	  {
	      alert("COOKIES need to be enabled when being referred from another site.");
	  }
	  else
	  {
	    document.cookie = "refID=" + sRefID + "; expires=; path=/";
	    return true;
	  }
	}
	return false;
}

function cookieRefIDCheck() {
  //alert("cookieRefIDCheck " + document.cookie);
  var page = new PageQuery(document.cookie, "; ");
  var refCheck = page.getValue("refID");
  return refCheck;
}

function cookieLinkVar() {
  //alert("cookieLinkVarCheck " + document.cookie);
  var page = new PageQuery(document.cookie, "; ");
  var refCheck = page.getValue("linkVar");
  //alert("came from: " + refCheck);
  return refCheck;
}

function PageQuery(q, delim) {
  //alert(q);
  if(q.length > 1) this.q = q.substring(1, q.length);
  else this.q = null;
  this.keyValuePairs = new Array();
  if(q) {
	for(var i=0; i < this.q.split(delim).length; i++) {
	  this.keyValuePairs[i] = this.q.split(delim)[i];
	  //alert(this.keyValuePairs[i]);
	}
  }
  this.getKeyValuePairs = function() { return this.keyValuePairs; }
  this.getValue = function(s) {
	for(var j=0; j < this.keyValuePairs.length; j++) {
	  if(this.keyValuePairs[j].split("=")[0] == s)
		return this.keyValuePairs[j].split("=")[1];
	}
	return "";
  }
  this.getValueAt = function(i) {
    return this.keyValuePairs[i].split("=")[1];
  }
  this.getKeyAt = function(i) {
    return this.keyValuePairs[i].split("=")[0];
  }
  
  this.getParameters = function() {
	var a = new Array(this.getLength());
	for(var j=0; j < this.keyValuePairs.length; j++) {
	  a[j] = this.keyValuePairs[j].split("=")[0];
	}
	return a;
  }
  this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key){
  var page = new PageQuery(window.location.search, "&");
  return unescape(page.getValue(key));
}

function writeCartData(itemName, itemNumber, itemPrice)
{
  //alert("writeCartData " + document.cookie);
  var refCheck = "1";
  //var tempRefCheck = cookieRefIDCheck();
  //if (tempRefCheck != "" && tempRefCheck != "false")
  //{
  //  refCheck = tempRefCheck;
  //}
  //alert("writeCartData: " + refCheck);
  var writeMe = ("<input type='hidden' name='add' value='1'>\n" 
	+ "<input type='hidden' name='cmd' value='_cart'>\n"
	+ "<input type='hidden' name='business' value='paypal@bearmo.com'>\n"
	+ "<input type='hidden' name='item_name' value='" + itemName + "'>\n");

  //alert("writeCartData " + itemNumber);
  if (itemNumber.indexOf("=") > -1)
  {
      writeMe = writeMe + "<br>Size:<select onChange='addToPrice(amount, this.value, " + itemPrice + ");' name='item_number'>";
      var pairs = new PageQuery(itemNumber, ",");
      //alert(pairs.getLength());
      for (var i=0; i < pairs.getLength(); i++) 
      {
        var sOption = "<option value='" + pairs.getKeyAt(i).replace("<refID>", refCheck) + "'>" + pairs.getValueAt(i);
        //alert(sOption);
        writeMe = writeMe + sOption;
      }
      writeMe = writeMe + "</select>";
  }
  else
  {
    writeMe = writeMe + "<input type='hidden' name='item_number' value='" + itemNumber.replace("<refID>", refCheck) + "'>\n";
  }
  
  writeMe = writeMe + "<input type='hidden' name='amount' value='" + itemPrice + "'>\n"
	+ "<input type='hidden' name='page_style' value='Booples'>\n"
	+ "<input type='hidden' name='no_shipping' value='2'>\n"
	+ "<input type='hidden' name='return' value='http://www.booples.com/'>\n"
	+ "<input type='hidden' name='notify_url' value='http://www.booples.com/cgi-bin/notify_paypal.pl'>\n"
	+ "<input type='hidden' name='cancel_return' value='http://www.booples.com'>\n"
	+ "<input type='hidden' name='shopping_url' value='http://www.booples.com/booples_products.html'>\n"
	+ "<input type='hidden' name='cn' value='Comments'>\n"
	+ "<input type='hidden' name='currency_code' value='USD'>\n"
	+ "<input type='hidden' name='lc' value='US'>\n"
	+ "<input type='hidden' name='bn' value='PP-ShopCartBF'>\n";
  //alert(writeMe);
  document.write(writeMe);
}

function addToPrice (field, nameWithExtraPrice, basePrice)
{
  //alert(nameWithExtraPrice);
  //alert(field.value);
  if (nameWithExtraPrice.indexOf("+") > -1)
  {
    basePrice = parseFloat(basePrice) + parseFloat(nameWithExtraPrice.substring(nameWithExtraPrice.length-1, nameWithExtraPrice.length));  
    basePrice = basePrice.toFixed(2)
    //alert(basePrice);
  }
  field.value = basePrice;
}

function writeCartDataWithForm(itemName, itemNumber, itemPrice)
{
  //alert(itemNumber);
  document.write("<center>\"" + itemName + "\"<br><b>$" + itemPrice + "</b><br><form target='paypal' action='https://www.paypal.com/cgi-bin/webscr' method='post' name='" + itemName + "'>");
  document.write("<input type='image' src='https://www.paypal.com//en_US/i/btn/sc-but-03.gif' name='submit' alt='Make payments with PayPal - it's fast, free and secure!'>");
   
  writeCartData(itemName, itemNumber, itemPrice);
   
  document.write("</form></center>");
}

function writeLink(linkURL, linkText, nOrder)
{
  if (nOrder != "0")
  {
    document.write (" | " );
  }
  document.write ("<a class=light href='" + linkURL + ".html'>" + linkText + "</a>");
}

function writeExternalLink(linkURL, linkText, nOrder)
{
  if (nOrder != "0")
  {
    document.write (" | " );
  }
  document.write ("<a class=light href='" + linkURL + "'>" + linkText + "</a>");
}

function writeLinkRO(linkURL, linkName, altText)
{
  document.write("<A HREF='" + linkURL + "'\n  ");
  document.write("    onMouseOver = \"document." + linkName + ".src='http://www.booples.com/b_images/booples_" + linkName + "_ro.jpg'\";\n   ");
  document.write("    onMouseOut = \"document." + linkName + ".src='http://www.booples.com/b_images/booples_" + linkName + "_ro.jpg'\";\n   ");
  document.write (">\n   ");
  document.write ("<IMG alt='" + altText + "' title='" + altText + "' SRC='http://www.booples.com/b_images/booples_" + linkName + "_ro.jpg' ALT='Booples! " + linkName + "' name='" + linkName + "' BORDER=0></A>\n");
}

function writeLinkROInfo(linkURL, linkName, imgHeight, altText)
{
  document.write("<A HREF='" + linkURL + "'\n  ");
  document.write("    onMouseOut = \"document." + linkName + ".src='http://www.booples.com/b_images/booples_" + linkName + "_reg.jpg'\";\n   ");
  document.write("    onMouseOver = \"document." + linkName + ".src='http://www.booples.com/b_images/booples_" + linkName + "_ro.jpg';");
  document.write("    document.getElementById('InfoDiv').innerHTML = '" + altText + "';\";\n   ");
  document.write (">\n   ");
  document.write ("<IMG alt='" + altText + "' title='" + altText + "' height='" + imgHeight + "' SRC='http://www.booples.com/b_images/booples_" + linkName + "_reg.jpg' ALT='Booples! " + linkName + "' name='" + linkName + "' BORDER=0></A>\n");
}

function writeLinkROHeight(linkURL, linkName, imgHeight, altText)
{
  preloadROImg(linkName);
  document.write("<A HREF='" + linkURL + "'\n  ");
  document.write("    onMouseOver = \"document." + linkName + ".src='http://www.booples.com/b_images/booples_" + linkName + "_ro.jpg'\";\n   ");
  document.write("    onMouseOut = \"document." + linkName + ".src='http://www.booples.com/b_images/booples_" + linkName + "_ro.jpg'\";\n   ");
  document.write (">\n   ");
  document.write ("<IMG alt='" + altText + "' title='" + altText + "' height='" + imgHeight + "' SRC='http://www.booples.com/b_images/booples_" + linkName + "_ro.jpg' ALT='Booples! " + linkName + "' name='" + linkName + "' BORDER=0></A>\n");
}

function preloadROImg (imgName)
{
    if (document.images)
    {
      preload_image = new Image(1,1); 
      preload_image.src="http://www.booples.com/b_images/booples_" + imgName + "_ro.jpg"; 
    }   
}

function paypalForm(itemName, itemNumber, itemPrice)
{
  var refName = locVar;
  var theName = refName + "-" + itemNumber;
//  var linkLoc = cookieLinkVar();
//  if (linkLoc != "")
//  {
//    theName = "Link-" + linkLoc + "-" + theName;
//  }
  //alert(theName);
//  document.write("<center><a target='_blank' href='http://www.booples.com/bible_verse_song.html'>Why so inexpensive?</a></center>");
  writeCartDataWithForm(itemName, theName, itemPrice);
}

function paypalDVD1()
{
  paypalForm("Booples DVD", "booplesdvd", "4.99");
}

function paypalDVDYB()
{
  paypalForm("Booples 2: Yikity Blar! DVD", "booplesdvdYB", "6.99");
}

function paypalDVDCombo()
{
  paypalForm("Booples DVD Combo", "booplesdvd1AndYB", "9.99");
}

function paypalCDSongs()
{
  paypalForm("Booples Songs CD", "booplescdsongs", "5.99");
}

function paypalDVDComboTSTSongs()
{
  paypalForm("Booples DVD Combo 2", "booplesdvdTSTAndSongs", "14.99");
}

function paypalDVDSongs()
{
  paypalForm("Booples!: The Songs! DVD", "booplesdvdSongs", "7.99");
}

function paypalDVDTST()
{
  paypalForm("Booples 3: The Silly Truth! DVD", "booplesdvdTST", "8.99");
}

function paypalBookBAS()
{
  paypalForm("Booples!: We're Brothers and Sisters", "booplesBookBAS", "9.99");
}

