function trim(sString)
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}
function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function enterCountry()
{
    if(document.getElementById("selCountry").value == "")
    {
        document.getElementById("spMessage2").innerHTML = "Please select a country";
    }
    else
    {
        document.location.href= "products.php?c=" + document.getElementById("selCountry").value;
    }
    
    
}
function enterUSA()
{
    if(trim(document.getElementById("txtZipCode").value) != "")
    {
        document.location.href= "products.php?c=" + document.getElementById("selCountry").value + "&z=" + removeSpaces(document.getElementById("txtZipCode").value) ;
    }
    else
    {
        document.getElementById("message").innerHTML = "Please enter your zip code.";
    }
}

function addProduct()
{
  
  {
       new Ajax.Request
       (
           '../ajax/addproduct.php?rnd=' + Math.random(), {
			method: 'post', postBody: Form.serialize('frmNewProduct') , parameters:'productId=1',
			onSuccess:success, onFailure:errFunc}
       );
   }
}

function deleteProduct(pid)
{
	document.getElementById("spanMessage").innerHTML = "Do you want to delete this product? <a href=\"javascript:confirmDelete('" + pid + "');\">Yes</a> <a href=\"javascript:abortDelete();\">No</a>";
}

function confirmDelete(pid)
{
   {
       new Ajax.Request
       (
           '../ajax/deleteProduct.php?rnd=' + Math.random(), {
			method: 'post', parameters:'productId=' + pid,
			onSuccess:success, onFailure:errFunc}
       );
   }
}

function abortDelete()
{
	document.getElementById("spanMessage").innerHTML = "";
}

function modifyProduct()
{
  {
       new Ajax.Request
       (
           '../ajax/modifyproduct.php?rnd=' + Math.random(), {
			method: 'post', postBody: Form.serialize('frmModifyProduct') , parameters:'productId=1',
			onSuccess:changeLocation, onFailure:errFunc}
       );
   }
}

function changeLocation(t)
{
	document.location.href="/admin/products.php?modify=" + t.responseText;
	$("spanMessage").innerHTML = "Product has been updated";
	//setTimeout("resetSpanMessage()", 10000);
}

function addToCart(pid)
{
   var span = "spanAdd" + pid;
   {
       new Ajax.Request
       (
           '../../ajax/addToCart.php?rnd=' + Math.random(), {
			method: 'post', parameters:'productId=' + pid,
			onSuccess: function successAddToCart(t) { 
				$(span).innerHTML = t.responseText; 
				//setTimeout("resetSpanAddMessage(" + pid + ")", 5000); 
			},
			onFailure:errFunc}
       );
   }
}

function removeFromCart(pid)
{
   {
       new Ajax.Request
       (
           '../../ajax/removeFromCart.php?rnd=' + Math.random(), {
			method: 'post', parameters:'productId=' + pid,
			onSuccess:function successRemove(t) { 
				if(t.responseText != "none")
				{
					$("spanMessage").innerHTML = t.responseText;
					setTimeout("resetSpanMessage()", 5000); 
					$(pid).style.display = "none"; 
					calculatePrice(); 
				}
				else
				{
					$("divControls").style.display = "none";
					$("divCart").style.display = "none";
					$("divPrice").style.display = "none"; 
					$("divNoItems").style.display = "block";
				}
			}, 
			onFailure:errFunc}
       );
   }
}
function updateCartButton()
{   
    $("spanMessage").innerHTML = "Shopping Cart has been updated"; 
}
function updateCart(id)
{
    var element = "txtQty" + id;
    var s = document.getElementById(element);  
    var mySplitResult;
    {
       new Ajax.Request
       (
           '../../ajax/updateCart.php?id=' + id, {
			method: 'post', postBody: Form.serialize('frmCart'), parameters:'productId=1',
			onSuccess:function successUpdate(t) { 
			    var text = t.responseText;
			    if(text.indexOf("Error") > 0)
			    {
			        mySplitResult = text.split(";");
			        $("spanMessage").innerHTML = mySplitResult[1];
				    $("spanMessage2").innerHTML = mySplitResult[1];
				    for ( var i = 0; i < s.options.length; i++ ) {
                        if ( s.options[i].value == mySplitResult[0]) {
                            s.options[i].selected = true;
                        }
                    }		    
			    }
			    else
			    {
				    $("spanMessage").innerHTML = t.responseText; 
				    $("spanMessage2").innerHTML = t.responseText; 
				    calculatePrice(); 
				}
				//setTimeout("resetSpanMessage()", 5000);
				
			}, 
			onFailure:errFunc}
       );
     }
     return false;
}

function emptyCart()
{
{
       new Ajax.Request
       (
           '../../ajax/emptyCart.php?rnd=' + Math.random(), {
			method: 'post', parameters:'productId=1',
			onSuccess:window.location="/subpages/products/products.php", onFailure:errFunc}
			     
			    //window.location = "/subpages/products/products.php";
		        //function successEmpty(t) { 
				//$("spanMessage").innerHTML = t.responseText; 
				//setTimeout("resetSpanMessage()", 5000);
				//$("divControls").style.display = "none";
				//$("divCart").style.display = "none";
				//$("divPrice").style.display = "none"; 
				//$("divNoItems").style.display = "block";
			//}, 
			
       );
   }
}

function calculatePrice()
{
  {
       new Ajax.Request
       (
           '../../ajax/calculatePrice.php?rnd=' + Math.random(), {
			method: 'post', postBody: Form.serialize('frmCart'), parameters:'productId=1',
			onSuccess:printPrice, onFailure:errFunc}
       );
   }

}

function calculateDiscount()
{
   {
       new Ajax.Request
       (
           '../../ajax/calculateDiscount.php?rnd=' + Math.random(), {
			method: 'post', postBody:Form.serialize('frmCart'), parameters:'productId=1',
			onSuccess:printDiscount, onFailure:errFunc}
       );
   }

}
function checkout()
{
	if($("selShipping").value == "none")
	{
		$("spanMessage").innerHTML = "Please enter a shipping method";
		return false;
	}
	else
	{
		$("spanMessage").innerHTML = "Please enter a shipping method";
		return true;
	}
}


function success(t)
{
	$("spanMessage").innerHTML = t.responseText;
	setTimeout("resetSpanMessage()", 5000);
}

function errFunc(t)
{
	$("spanMessage").innerHTML = "Sorry, Your request couldn't be made";
	//setTimeout("resetSpanMessage()", 5000);
}

function printPrice(t)
{
	$("spanPrice").innerHTML = t.responseText;
}

function resetSpanMessage()
{
	$("spanMessage").innerHTML = "";
}

function resetSpanAddMessage(pid)
{
	var span = "spanAdd" + pid;
	$(span).innerHTML = "";
}

function sameAsShipping()
{
	if($("chkSameAsShipping").checked)
	{
		$("txtBillingAddress1").value = $("txtShippingAddress1").value;
		$("txtBillingAddress2").value = $("txtShippingAddress2").value;
		$("txtBillingApartment").value = $("txtShippingApartment").value;
		$("txtBillingCity").value = $("txtShippingCity").value;
		$("txtBillingState").value = $("txtShippingState").value;
		$("txtBillingCode").value = $("txtShippingCode").value;
		$("txtBillingCountry").value = $("txtShippingCountry").value;
	}
	else
	{
		$("txtBillingAddress1").value = "";
		$("txtBillingAddress2").value = "";
		$("txtBillingApartment").value = "";
		$("txtBillingCity").value = "";
		$("txtBillingState").value = "";
		$("txtBillingCode").value = "";
	}
}

function hideCreditCartInfo()
{
    if ( $("selPaymentType").value == "Fax" )
    {
        $("dvCreditCard").style.display = "none";
    }
    else if( $("selPaymentType").value == "Credit_Card" )
    {
        $("dvCreditCard").style.display = "block";
    }

}

function updateRates()
{
    {
       new Ajax.Request
       (
           '../../ajax/updateRates.php?rnd=' + Math.random(), {
			method: 'post', postBody: Form.serialize('frmRates') , parameters:'productId=1',
			onSuccess:printRates, onFailure:errFunc}
       );
   }
}

function printRates(t)
{
    $("spanPrice").innerHTML = t.responseText;
}