/*
 * Created on May 19, 2007 by nhorvath
 *
 * NXWEB Technologies
 * All Rights Reserved - Unauthorized Use Prohibited
 * For information contact licensing@nxwebtechnologies.com
 * 
 */ 
 
 
var showCartAgain;
var ns4 = document.layers;
var ie4 = document.all ;
var nn6 = document.getElementById && !document.all ;
 
 
function checkout(theFormId) {
	if (verifyForm(document.getElementById(theFormId))) {
		theForm = document.getElementById(theFormId);
  	    var poststr = '';
		for(i=0; i<theForm.elements.length; i++){
		   poststr += theForm.elements[i].name+"="+encodeURI(theForm.elements[i].value)+"&";
		}
		fadeBackgroundShow();
	    checkoutProgressShow();
		
		makePOSTRequest('checkout.php', poststr, 'checkout', 'checkoutProgressHide()');
	}
	return false;
}


 // calls shopping cart script and places it's span output into the shoppingCart span on the page
function addToCart(productId) {
// onplugin call

    if (typeof plugin_addToShoppingCart!="undefined") {
   		 plugin_addToShoppingCart(productId);
    }
	var volume;
	if (document.getElementById(productId+"_volume")) {
		volume = document.getElementById(productId+"_volume").value;
	} else {
		volume = 1;
	}
    var poststr = "productId=" + productId + "&display=span" + "&volume="+ encodeURI( volume );
 
    //TODO: find way for same product be applied to multiple sizes.  Use other attribs to determine uniqueness on php side
    
	makePOSTRequest('shoppingcart.php?action=addToCart', poststr, 'shoppingCart', 'setPageTotal()');
}

function removeFromCart(productId) {
	volume = 1;
	var poststr = "productId=" + productId + "&display=span" + "&volume="+ encodeURI( volume );
	makePOSTRequest('shoppingcart.php?action=removeFromCart', poststr, 'shoppingCart', 'setPageTotal()');
}

function deleteItem(productId) {
	volume = 1;
	var poststr = "productId=" + productId + "&display=span" + "&volume="+ encodeURI( volume );
	makePOSTRequest('shoppingcart.php?action=deleteItem', poststr, 'shoppingCart', 'setPageTotal()');
}


function setPageTotal() {
    var poststr = "";
	makePOSTRequest("shoppingcart.php?action=getTotal", "", "shoppingCartTotalAmount", "");
    if (typeof plugin_setPageTotal!="undefined") {
   		 plugin_setPageTotal();
    }	
}


function displayCartSpan() {

    	//hideCart();
        var poststr = "display=span";
    	makePOSTRequest('shoppingcart.php?action=showCart', poststr, 'shoppingCart', 'setPageTotal()');
}


// displays and shows the shopping cart

function hideCart() {
   document.getElementById('spanShoppingCart').style.visibility = "hidden";
   fadeBackgroundHide();
}

function showCart() {

   if (document.getElementById('spanShoppingCart')) {
       document.getElementById('spanShoppingCart').style.visibility = "visible";
       fadeBackgroundShow();
   }
}

function hideBillingAddressForm() {
   document.getElementById('frmBillingFirstName').value = "";
   document.getElementById('frmBillingLastName').value = "";
   document.getElementById('frmBillingAddressOne').value = "";
   document.getElementById('frmBillingAddressTwo').value = "";
   document.getElementById('frmBillingCity').value = "";
   document.getElementById('frmBillingZip').value = "";
   document.getElementById('frmBillingState').selectedIndex = 0;
   document.getElementById('billingAddressForm').style.visibility = "hidden";
}

function showBillingAddressForm() {
   document.getElementById('billingAddressForm').style.visibility = "visible";
}

function cartIsVisible() {
   return (document.getElementById('spanShoppingCart').style.visibility == "visible");
}

function copyToBillingForm(firstName, lastName, addressOne, addressTwo, city, state, zip, country) {
   document.getElementById('frmBillingFirstName').value = firstName;
   document.getElementById('frmBillingLastName').value = lastName;
   document.getElementById('frmBillingAddressOne').value = addressOne;
   document.getElementById('frmBillingAddressTwo').value = addressTwo;
   document.getElementById('frmBillingCity').value = city;
   document.getElementById('frmBillingZip').value = zip;
   stateIndex = getIndex(document.getElementById('frmBillingState'), state)
   countryIndex = getIndex(document.getElementById('frmBillingCountry'), country)
   document.getElementById('frmBillingState').selectedIndex = stateIndex;
   document.getElementById('frmBillingCountry').selectedIndex = countryIndex;

}

function copyToShippingForm(firstName, lastName, addressOne, addressTwo, city, state, zip, country) {
   document.getElementById('frmShippingFirstName').value = firstName;
   document.getElementById('frmShippingLastName').value = lastName;
   document.getElementById('frmShippingAddressOne').value = addressOne;
   document.getElementById('frmShippingAddressTwo').value = addressTwo;
   document.getElementById('frmShippingCity').value = city;
   document.getElementById('frmShippingZip').value = zip;
   stateIndex = getIndex(document.getElementById('frmShippingState'), state)
   countryIndex = getIndex(document.getElementById('frmShippingCountry'), country)
   document.getElementById('frmShippingState').selectedIndex = stateIndex;
   document.getElementById('frmShippingCountry').selectedIndex = countryIndex;
}


function getIndex(menu, value){
  result = -1;
  index = 0;
  while(index < menu.length && result == -1) {
    if(menu[index].value == value) {   result = index;  }
    else {  index++;  }
  }
  return result;
}

function forwardToCheckout() {
  if (document.getElementById('shoppingCartTotalAmount').innerHTML == '') {
  	showCart();
  } else {
  	window.location.href = 'register.php?action=initializeCheckout';
  }
}






