var g_Loc;
g_Loc = 0;

function CheckForm()
{
  iLoc = g_Loc;
  if (g_Loc == 0)
    return false;
    
  var iQty = document.forms['paypal'].quantity.value;
  
  if (!IsNumeric(iQty)) iQty = 0;
  
  if (iQty <1 )
  {
    alert('Please enter a valid quantity');
    return false;
  }

  var ship = 10;
  
  if (iLoc == '1')
  {
    if (iQty>8) ship=20; else
    if (iQty>4) ship=15; else
    if (iQty>2) ship=10; else
    if (iQty>1) ship=6.5; else
                ship=5;
  }
  else
  {
    if (iQty>8) ship=25; else
    if (iQty>4) ship=20; else
    if (iQty>2) ship=15; else
    if (iQty>1) ship=12; else
                ship=10;
  }
  document.forms['paypal'].shipping.value = ship;
  return true;  
}

function BuyNow(sName, iLoc)
{ 
  document.forms['paypal'].item_name.value=sName;
  g_Loc = iLoc;
  CheckForm();
  document.forms['paypal'].submit();   
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

// Generic PayPal form submit
function PayPal(item, amount) {	
	postToURL("https://www.paypal.com/cgi-bin/webscr",	{
      quantity: 1,
	  cmd: "_xclick",
	  business: "judith@birthbalance.com",
	  item_name: item,
	  amount: amount,
	  shipping: "0.00",
	  page_style: "Primary",
	  no_shipping: "2",
	  no_note: "0",
	  cn: "Alternative email / special instructions:",
	  currency_code: "USD",
	  bn: "PP-ShopCartBF",
	  "return": "http://www.birthbalance.com/thanks.asp",
	  cancel_return: "http://www.birthbalance.com"	
	});
	return false;
}


// Utility functions

function createElement (tagName, attributes)
{
    // Detect IE using conditional compilation
    if (/*@cc_on @*//*@if (@_win32)!/*@end @*/false)
    {
        // Translations for attribute names which IE would otherwise choke on
        var attrTranslations =
        {
            "class": "className",
            "for": "htmlFor"
        };

        var setAttribute = function(element, attr, value)
        {
            if (attrTranslations.hasOwnProperty(attr))
            {
                element[attrTranslations[attr]] = value;
            }
            else if (attr == "style")
            {
                element.style.cssText = value;
            }
            else
            {
                element.setAttribute(attr, value);
            }
        };

        
		attributes = attributes || {};

		// See http://channel9.msdn.com/Wiki/InternetExplorerProgrammingBugs
		if (attributes.hasOwnProperty("name") ||
			attributes.hasOwnProperty("checked") ||
			attributes.hasOwnProperty("multiple"))
		{
			var tagParts = ["<" + tagName];
			if (attributes.hasOwnProperty("name"))
			{
				tagParts[tagParts.length] =
					' name="' + attributes.name + '"';
				delete attributes.name;
			}
			if (attributes.hasOwnProperty("checked") &&
				"" + attributes.checked == "true")
			{
				tagParts[tagParts.length] = " checked";
				delete attributes.checked;
			}
			if (attributes.hasOwnProperty("multiple") &&
				"" + attributes.multiple == "true")
			{
				tagParts[tagParts.length] = " multiple";
				delete attributes.multiple;
			}
			tagParts[tagParts.length] = ">";

			var element =
				document.createElement(tagParts.join(""));
		}
		else
		{
			var element = document.createElement(tagName);
		}

		for (var attr in attributes)
		{
			if (attributes.hasOwnProperty(attr))
			{
				setAttribute(element, attr, attributes[attr]);
			}
		}

		return element;
	
    }
    // All other browsers
    else
    {
        
		attributes = attributes || {};
		var element = document.createElement(tagName);
		for (var attr in attributes)
		{
			if (attributes.hasOwnProperty(attr))
			{
				element.setAttribute(attr, attributes[attr]);
			}
		}
		return element;
	
    }
}

function postToURL(url, values)
{
    values = values || {};

    var form = createElement("form", {action: url,
                                      method: "POST",
                                      style: "display: none"});
    for (var property in values)
    {
        if (values.hasOwnProperty(property))
        {
            var value = values[property];
            if (value instanceof Array)
            {
                for (var i = 0, l = value.length; i < l; i++)
                {
                    form.appendChild(createElement("input", {type: "hidden",
                                                             name: property,
                                                             value: value[i]}));
                }
            }
            else
            {
                form.appendChild(createElement("input", {type: "hidden",
                                                         name: property,
                                                         value: value}));
            }
        }
    }
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
}

