var productIDArray = new Array();
var producCatIDArray = new Array();
var productCatalogNoArray= new Array();
var productQuantityArray= new Array();
var productUnitePriceArray= new Array();
var userRegion = "";

function toggle( productID ){
   var productListObj = document.getElementById ( productID );
   var imgObj = document.getElementById ( "toggle_" + productID );

   if ( productListObj.style.display != "block" ) {
      imgObj.innerHTML = "<img src=\"images/minus.gif\" border=\"0\" width=\"12\" height=\"12\"/>";
      productListObj.style.display = "block";
   } else {
      imgObj.innerHTML = "<img src=\"images/plus.gif\" border=\"0\" width=\"12\" height=\"12\"/>";
      productListObj.style.display = "none";
   }
   var elementsListObj = document.getElementsByTagName( "DIV" );
   for ( p=0; p < elementsListObj.length; p++ ) {
      if ( elementsListObj[p].className == "ProductList" && elementsListObj[p].id != productID ) {
         imgObj = document.getElementById ( "toggle_" + elementsListObj[p].id  );
         imgObj.innerHTML = "<img src=\"images/plus.gif\" border=\"0\" width=\"12\" height=\"12\"/>";
         productListObj = document.getElementById ( elementsListObj[p].id  );
         productListObj.style.display = "none";
      }
   }
}

function addToCart(productCatID, productID, producCatalogNo, productQuantity, productUnitPrice){
   var found = false;      
   for ( o = 0; o < productIDArray.length && !found; o++ ) {
      if (  productIDArray[o] == productID ) {
         found = true;
         productQuantityArray[o] += 1;
      }
   }
   
   var index = 0;
   if ( productIDArray.length == 0 ){
      index = 0;
   } else {
      index = productIDArray.length;
   }

   if ( ! found ) {
      productIDArray[index] = productID;
      producCatIDArray[index] = productCatID;
      productCatalogNoArray[index] = producCatalogNo;
      productQuantityArray[index] = productQuantity;
      productUnitePriceArray[index] = productUnitPrice;
   }
   
   generateShopCart();
}

function addToShoppingCart(productCatID, productID, producCatalogNo, productQuantity, productUnitPrice){
  var existingScInfo = getCookie ( "ppSC", scInfo );
  var scInfo = "";
  while ( productCatID.indexOf ( "," ) != -1 )
    productCatID = productCatID.replace ( ",","~");
      
  if ( existingScInfo != "" && existingScInfo != "null" && existingScInfo != null ) {
     var existingSCInfoArr = existingScInfo.split("~~");
     var prodIDArr = existingSCInfoArr[1].split(",");
     var prodQuantityArr = existingSCInfoArr[3].split(",");
     var found = false;
     var quantityStr = "";
     for ( p = 0; p < prodIDArr.length; p++ ) {
        if ( p != 0 )
         quantityStr += ",";
         
        if ( prodIDArr[p] == productID ) {
            found = true;
            quantityStr += (parseInt( prodQuantityArr[p] ) + 1);
        } else {
           quantityStr += prodQuantityArr[p];
        }
     }
     if ( found )
        scInfo = existingSCInfoArr[0] + "~~" + existingSCInfoArr[1] + "~~" + existingSCInfoArr[2] + "~~" + quantityStr + "~~" + existingSCInfoArr[4];
     else
        scInfo = existingSCInfoArr[0] + "," + productCatID + "~~" + existingSCInfoArr[1] + "," + productID + "~~" + existingSCInfoArr[2] + "," + producCatalogNo + "~~" + existingSCInfoArr[3] + "," + productQuantity + "~~" + existingSCInfoArr[4] + "," + productUnitPrice;
  } else {
      scInfo = productCatID + "~~" + productID + "~~" + producCatalogNo + "~~" + productQuantity + "~~" + productUnitPrice;
  }   
  setCookie ( "ppSC", scInfo);
  window.location.href = "ShoppingCart.aspx";
}

function preGenerateShopCart() {
  var existingScInfo = getCookie ( "ppSC", scInfo );
  
  var scInfo = "";
  if ( existingScInfo != "" && existingScInfo != "null" && existingScInfo != null ) {
    var existingSCInfoArr = existingScInfo.split("~~");
    productIDArray = existingSCInfoArr[1].split(",");
    producCatIDArray = existingSCInfoArr[0].split(",");
    productCatalogNoArray= existingSCInfoArr[2].split(",");
    productQuantityArray= existingSCInfoArr[3].split(",");
    productUnitePriceArray= existingSCInfoArr[4].split(",");
  } else {
    productIDArray = new Array();
    producCatIDArray = new Array();
    productCatalogNoArray= new Array();
    productQuantityArray= new Array();
    productUnitePriceArray= new Array();
  }  
  userRegion = getCookie("ur");
  
  generateShopCart(); 
}
function generateShopCart() {
   var modularContentObj = document.getElementById ( "modularContent" );
   var showCart = false;
   for ( o = 0; o < productIDArray.length; o++ ) {
      if ( productQuantityArray[o] != 0 )
         showCart=true;         
   }
   var orderDetail = "";
/*   orderDetail += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
   orderDetail += "<tr>";
   orderDetail += "<td align=\"left\" class=\"heading\">Your Shopping Cart</td>";
   orderDetail += "</tr>";
   orderDetail += "</table>";
*/    
   orderDetail += "<table align=\"center\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" width=\"100%\">"
   orderDetail += "<tr valign=\"top\">";
   orderDetail += "<td class=\"orderDetailHeader\" width=\"100%\">Item</td>";
   orderDetail += "<td class=\"orderDetailHeader\" nowrap>Catalog #</td>";
   orderDetail += "<td align=\"center\" class=\"orderDetailHeader\" nowrap width=\"10%\">Quantity</td>";
   orderDetail += "<td align=\"center\" class=\"orderDetailHeader\" nowrap width=\"10%\">Remove</td>";
   orderDetail += "<td align=\"right\" class=\"orderDetailHeader\" nowrap>Amount</td>";
   orderDetail += "</tr>";

   var subtotal = 0;
   for ( o = 0; o < productIDArray.length; o++ ) {
      orderDetail += "<tr valign=\"top\" width=\"100%\">";
      while ( producCatIDArray[o].indexOf ( "~" ) != -1 )
         producCatIDArray[o] = producCatIDArray[o].replace ( "~", "," );
      orderDetail += "<td wrap>" + producCatIDArray[o] + "</td>";
      orderDetail += "<td nowrap>" + productCatalogNoArray[o] + "</td>";
      orderDetail += "<td nowrap align=\"center\"><input type=\"text\" size=\"1\" name=\"q"  + o + "\" value=\"" + productQuantityArray[o] + "\"></td>";
      var usd = productQuantityArray[o] * productUnitePriceArray[o];
      usd = usd.toFixed(2);
      subtotal += productQuantityArray[o] * productUnitePriceArray[o];
      orderDetail += "<td nowrap align=\"center\"><input type=\"checkbox\" name=\"d" + o + "\" value=\"1\"></td>";
      orderDetail += "<td nowrap align=\"right\" nowrap>$" + usd + " USD</td>";
      orderDetail += "</tr>";
   }
  // var sh = 34.95;
   orderDetail += "<tr class=\"summary\">";
   orderDetail += "<td width=\"100%\"></td>";
   orderDetail += "<td nowrap></td>";
   orderDetail += "<td class=\"button\" colspan=\"2\"><input type=\"Button\" name=\"updat\" value=\"Update Cart\" onClick=\"updateCart()\"></td>";
   orderDetail += "<td nowrap></td>";
   orderDetail += "</tr>";
   orderDetail += "<tr>";
   orderDetail += "<td class=\"spacerGrey\" colspan=\"5\"></td>";
   orderDetail += "</tr>";
   orderDetail += "<tr class=\"summary\">";
   orderDetail += "<td colspan=\"4\">Subtotal:</td>";
   orderDetail += "<td class=\"moneyColumn\">$" + subtotal.toFixed(2) + " USD</td>";
   orderDetail += "</tr>";
   
   if ( modularContentObj == null ) {
        userRegion = userRegion.toLowerCase();
          
        if ( userRegion == "ma" || userRegion == "massachusetts" ) {
            var tax = subtotal * 0.05;
            orderDetail += "<tr class=\"summary\">";
            orderDetail += "<td colspan=\"4\">Tax:</td>";
            orderDetail += "<td class=\"moneyColumn\">$" + tax.toFixed(2) + " USD</td>";
            orderDetail += "</tr>";
        }
        //orderDetail += "<tr class=\"summary\">";
        //orderDetail += "<td colspan=\"4\">Shipping &amp; Handling&nbsp;(Additional S&H charge may apply):</td>";
        //orderDetail += "<td class=\"moneyColumn\">$" + sh + " USD</td>";
        //orderDetail += "</tr>";
        orderDetail += "<tr class=\"summary\">";
        orderDetail += "<td colspan=\"4\">Total:</td>";
        var total = subtotal;
        if ( userRegion == "ma" || userRegion == "massachusetts" )
             total = total + tax;
        //total = total + sh;
        orderDetail += "<td class=\"moneyColumn\">$" + total.toFixed(2) + " USD</td>";
        orderDetail += "</tr>";
   }   
   orderDetail += "</table>";

   var modularContent = "<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
   modularContent += "<tr>";
   modularContent += "<td align=\"right\" width=\"100%\" class=\"globalButtons\">";
   modularContent += "<input type=\"button\" id=\"continue\" name=\"continue\" value=\"Continue Shopping\" onClick=\"continueShopping()\" alt=\"Continue Shopping\">";
   modularContent += "<input type=\"button\" id=\"checkout\" name=\"checkout\" value=\"Proceed to Checkout\" onClick=\"preCheckout()\" alt=\"Proceed to Checkout\">";
   modularContent += "</td>";
   modularContent += "</tr>";
   modularContent += "</table>";

   var orderDetailObj = document.getElementById ( "orderDetailsArea" );
   var modularContentObj = document.getElementById ( "modularContent" );
   if ( productIDArray != null && productIDArray.length != 0) {
      orderDetailObj.innerHTML = orderDetail;
      if ( modularContentObj != null )
	      modularContentObj.innerHTML  = modularContent;
   } else {      
      
      if ( modularContentObj != null ) {
          orderDetailObj.innerHTML = "Shopping Cart is empty";
	      modularContentObj.innerHTML = "";
	  } else {
	     var OrderReviewHeaderObj = document.getElementById ( "OrderReviewHeader" );
	     OrderReviewHeaderObj.style.display = "none";
	     orderDetailObj.innerHTML = "<br><b>There is no order for reviewing !</b>";
	  }
  }
}

function updateCart(){
  var thisForm = document.forms[0];
  var tmpProductIDArray = new Array();
  var tmpProducCatIDArray = new Array();
  var tmpProductCatalogNoArray= new Array();
  var tmpProductQuantityArray= new Array();
  var tmpProductUnitePriceArray= new Array();
  var count = 0;
  
  for ( o = 0; o < productIDArray.length; o++ ) {
     var qObj = thisForm.elements["q" + o ];
     var checkBoxObj = thisForm.elements["d" + o ];

     if (  parseInt ( qObj.value ) > 0 && !checkBoxObj.checked) {
        tmpProductIDArray[count] = productIDArray[o];
        tmpProducCatIDArray[count] = producCatIDArray[o];
        tmpProductCatalogNoArray[count] = productCatalogNoArray[o];
        tmpProductQuantityArray[count] = parseInt ( qObj.value );
        tmpProductUnitePriceArray[count] = productUnitePriceArray[o];
        count++;
     }
  }
  productIDArray = tmpProductIDArray;
  producCatIDArray = tmpProducCatIDArray;
  productCatalogNoArray = tmpProductCatalogNoArray;
  productQuantityArray = tmpProductQuantityArray;
  productUnitePriceArray = tmpProductUnitePriceArray;
  var scInfo = "";
  
  if ( productIDArray.length > 0 )
    scInfo = producCatIDArray + "~~" + productIDArray + "~~" + productCatalogNoArray + "~~" + productQuantityArray + "~~" + productUnitePriceArray;
  else
    scInfo = "";
  setCookie ( "ppSC", scInfo);

  //add hidden value after updating order info
  if ( thisForm.hidPID != null ) {
     thisForm.hidPID.value = productIDArray;
     thisForm.hidCatID.value = producCatIDArray;
     thisForm.hidCatlgNo.value = productCatalogNoArray;
     thisForm.hidUP.value = productUnitePriceArray;
     thisForm.hidQty.value = productQuantityArray;
  }
  generateShopCart();
}

function preCheckout(){
	var tmpURL = window.location.href;
	//tmpURL = tmpURL.replace("Order.aspx", "Login.aspx");
	tmpURL = tmpURL.replace("ShoppingCart.aspx", "Login.aspx");
	//tmpURL = "https://secure34.easycgi.com/phosphorex/phosphorex/Login.aspx";	
	window.location.href = tmpURL;
}

function setCart(productID, categoryID, catalogNo, quantity, unitPrice, region){
   productIDArray = productID.split ("," );
   producCatIDArray = categoryID.split(",");
   productCatalogNoArray= catalogNo.split(",");
   productQuantityArray= quantity.split(",");
   productUnitePriceArray= unitPrice.split(",");
   userRegion = region;
   setCookie("ur", region);
   generateShopCart();
}

function continueShopping(){
   history.back();
}

function setCookie(name, value, path) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var path = (argc > 2) ? argv[2] : null;
	var domain = (argc > 4) ? argv[4] : null;	
	var secure = (argc > 5) ? argv[5] : false;
	
	document.cookie = name + "=" + escape(value) +
 	  	((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
} // end setCookie

function getCookie(name) {
 var search = name + "="
 if (document.cookie.length > 0) {
     offset = document.cookie.indexOf(search)
     if (offset != -1) {
        offset += search.length
        end = document.cookie.indexOf(";", offset)
        if (end == -1) end = document.cookie.length
          return unescape(document.cookie.substring(offset, end))
     }
  }
  return null;
} // end getCookie

function deleteCookie(name) {
    var path = null;
    var domain = null;

    if (getCookie(name)) {
        document.cookie=name + "=null;"
    }
} // end delete Cookie

