<!-- // 
/************************************************************************************************
                                     						  	///   ///      ////  /      ///	
STORIS Management Systems						              	/    /  /      /     /     /  / 
Copyright 2009						                      		///  /  /  //  ///   /     /  / 
eStoris : Virtual Store						                	  /  /  /      /     /     /  / 
						                                      	///  ///       /     ////  ///
Tools Interfaces & eCommerce Applications

Program name:  sy599KitLogic.js
Program Desc.: 	Deals with the manipulation of quantities on the cart page. Accomodates nested 
				kit items...

Patch notes:

************************************************************************************************/

// format currencey without dollar sign or comas. Not currently used but was left in if needed in the future..
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

// format currencey with dollar sign or comas. Used on main price display after amount has been calculated...
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
			num.substring(num.length-(4*i+3));
			return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// sends back a array containing items. All items have their id attribute set to the value of the RollupKitID
// which indicates to us it is a nested kit. If no RollupKitID is found the KitId (prodNo) is used for the hidden 
// fields id attribute. In either case the id attribute exsist on all hidden fields that relate to Cart Items...
function getItemList(myForm){
	var arProdNo = new Array;
	var arProductDesc = new Array;
	var arProductPrice = new Array;
	var arQty = new Array;
	var arKitID = new Array;
	var arRollupKitID = new Array;
	var arRollupKitQty = new Array;
	var arReplacementProductID = new Array;
	var arAddon = new Array;
	var arTopLevelQty = new Array;
	var myTopLevelIds = new Array;
	var myTopLevelQtys = new Array;
	
	for (var i = 0;i<myForm.length;i++){
		if ((myForm.elements[i].name.toLowerCase()=="toplevelqty")){
			myTopLevelQtys.push(myForm.elements[i].value)
			myTopLevelIds.push(myForm.elements[i].id)
		}
	}
	
	// loop through unmodified kit ids and store their associated items in a new array...
	for (var k = 0;k<myForm.length;k++){
		switch(myForm.elements[k].name.toLowerCase()){
			case "prodno":
			  arProdNo.push(myForm.elements[k].value)
			  break;
			case "productdesc":
			  arProductDesc.push(myForm.elements[k].value)
			  break;
			case "productprice":
			  arProductPrice.push(myForm.elements[k].value)
			  break;
			case "qty":
			  arQty.push(myForm.elements[k].value)
			  break;
			case "kitid":
			  arKitID.push(myForm.elements[k].value)
			  break;
			case "rollupkitid":
			  arRollupKitID.push(myForm.elements[k].value)
			  break;
			case "rollupkitqty":
			  arRollupKitQty.push(myForm.elements[k].value)
			  break;
			case "replacementproductid":
			  arReplacementProductID.push(myForm.elements[k].value)
			  break;
			case "uniqueid":
			  	arAddon.push(myForm.elements[k].value)
				for (var m = 0;m<myTopLevelIds.length;m++){
					if ((myForm.elements[k].id == myTopLevelIds[m])){
						arTopLevelQty.push(myTopLevelQtys[m])
					}
				}
			  break;
			default:				  
		}

	}
	
	var arMyKitItemValues = new Array(8);
	arMyKitItemValues[0] = arProdNo
	arMyKitItemValues[1] = arProductDesc
	arMyKitItemValues[2] = arProductPrice
	arMyKitItemValues[3] = arQty
	arMyKitItemValues[4] = arKitID
	arMyKitItemValues[5] = arRollupKitID
	arMyKitItemValues[6] = arRollupKitQty
	arMyKitItemValues[7] = arReplacementProductID
	arMyKitItemValues[8] = arAddon
	arMyKitItemValues[9] = arTopLevelQty	
	return arMyKitItemValues	
}

function getCurrentCartItemList(CurrentCart){
	var arProdNo = new Array;
	var arProductDesc = new Array;
	var arProductPrice = new Array;
	var arQty = new Array;
	var arKitID = new Array;
	var arRollupKitID = new Array;
	var arRollupKitQty = new Array;
	var arReplacementProductID = new Array;
	var arAddon = new Array;
	var arTopLevelQty = new Array;
	var CurrentCartRows = CurrentCart.split("|")
	
	for (var i=0;i<CurrentCartRows.length;i++){
		var CurrentCartItems = CurrentCartRows[i].split('~')
		arProdNo.push(CurrentCartItems[0]);
		arProductDesc.push(CurrentCartItems[1]);
		arProductPrice.push(CurrentCartItems[2]);
		arQty.push(CurrentCartItems[3]);
		arKitID.push(CurrentCartItems[4]);
		arRollupKitID.push(CurrentCartItems[5]);
		arRollupKitQty.push(CurrentCartItems[6]);
		arReplacementProductID.push(CurrentCartItems[7]);
		arAddon.push(CurrentCartItems[8]);
		arTopLevelQty.push(CurrentCartItems[9]);
	}
	var arCurrentItemValues = new Array(8);
	arCurrentItemValues[0] = arProdNo
	arCurrentItemValues[1] = arProductDesc
	arCurrentItemValues[2] = arProductPrice
	arCurrentItemValues[3] = arQty
	arCurrentItemValues[4] = arKitID
	arCurrentItemValues[5] = arRollupKitID
	arCurrentItemValues[6] = arRollupKitQty
	arCurrentItemValues[7] = arReplacementProductID
	arCurrentItemValues[8] = arAddon
	arCurrentItemValues[9] = arTopLevelQty	
	return arCurrentItemValues
}

function refreshProductInfo(element){	
	var target = element; 
	var ProdNo, ProductDesc, ProductPrice, Qty, KitID, RollupKitID, KitIDToUse;
	var RollupKitQty, ReplacementProductID, Addon, myForm, myNewProductPrice;
	var NewId, OldId
	
	// check if we have the form or one of it's child elements and set the form we're working with based off that check...
	var myForm = (!target.form) ? target : target.form;
	
	var arCurrentItemValues, arMyItemValues;
	// returns values from what is currently displayed on the page...
	arMyItemValues = getItemList(myForm);
	
	// returns values of our current session cart...
	arCurrentItemValues = getCurrentCartItemList(element.hidCurrentCart.value);

	//ProdNo~ProductDesc~ProductPrice~Qty~KitID~RollupKitID~RollupKitQty~ReplacementProductID~Addon
	var myNewCartString = "";
	var myCartItem = ""
	var myNewProductPrice = parseFloat(0);
	var breakFlag = false;
	
	var myReplacementProductID;
	// sum up the none addon values if they have not been replaced...
	if (arMyItemValues[0].length > 0){	
		for (i=0;i<arMyItemValues[0].length;i++){
			ProdNo = arMyItemValues[0][i];
			ProductDesc = arMyItemValues[1][i];
			ProductPrice = CurrencyFormatted(arMyItemValues[2][i]);
			Qty = parseInt(arMyItemValues[3][i]);
			KitID = arMyItemValues[4][i];
			RollupKitID = arMyItemValues[5][i];
			RollupKitQty = arMyItemValues[6][i];
			ReplacementProductID = arMyItemValues[7][i];
			Addon = parseInt(arMyItemValues[8][i]);
			TopLevelQty = parseInt(arMyItemValues[9][i]);
			// we need to use the rollupID if it exist otherwise default to the ProdNum. Also check if our price changed
			// and adjust to accomodate. We will need to divide the kit current qty in the cart by it's rollup qty to
			// get the base level rollup qty before multiplying so we accomodate for if the user lowers the qty...
			KitIDToUse = ProdNo
			if ((RollupKitID!="")&&(RollupKitID!="0")){
				KitIDToUse = RollupKitID
				for(var j = 0;j<arCurrentItemValues[0].length;j++){
					if((j==i)&&(RollupKitID==arCurrentItemValues[5][j])){
						var CurrentQty = arCurrentItemValues[6][j]
						var OrigionalKitQty = (Qty / CurrentQty)
						break;
					}
				}
				
				//myNewProductPrice = (myNewProductPrice + (TopLevelQty * (Qty * ProductPrice)))
				myNewProductPrice = (myNewProductPrice + (TopLevelQty * (OrigionalKitQty * ProductPrice)))
				RollupKitQty = TopLevelQty
				Qty = (TopLevelQty * OrigionalKitQty)
			}else{
				myNewProductPrice = (myNewProductPrice + (TopLevelQty * ProductPrice))
				Qty = TopLevelQty
			}				
			
			/*
			if (RollupKitQty > 0){
				RollupKitQty = TopLevelQty
			}else{
				Qty = TopLevelQty
			}
			*/
			
			// cart string property positions...
			//ProdNo~ProductDesc~ProductPrice~Qty~KitID~RollupKitID~RollupKitQty~ReplacementProductID~Addon
			myCartItem = ProdNo + "~" + ProductDesc + "~" + ProductPrice + "~" + Qty + "~" + KitID + "~" + RollupKitID + "~" + RollupKitQty + "~" + ReplacementProductID + "~" + Addon
			if (myNewCartString == ""){
				myNewCartString = myCartItem;
			}else{
				myNewCartString = myNewCartString + "|" + myCartItem;
			}
		}
	}
	myForm.hidSessionCart.value = myNewCartString;
	return true;
}

function submitCart(myForm, cartAction){
	returnValue = refreshProductInfo(myForm)
	var formAction;
	switch(cartAction.toLowerCase()){
		case "recalc":
			showMessage('<img src="images/ajax-loader.gif" class="loader"><h1 class="loader">Please wait while we recalculate your cart</h1>', 400, 300)
		  	formAction = "reCalcCart.asp"
			hidNextLink = "sy599frm.asp"
			break;
		case "checkout":
			formAction = "reCalcCart.asp"
			hidNextLink = "te199terms.asp"
			break;
		case "requestquote":
			formAction = "reCalcCart.asp"
			hidNextLink = "te195frm.asp"
			break;	
		default:				  
	}
	
	myForm.hidNextLink.value = hidNextLink;
	if (myForm.hidSessionCart.value != ""){
		myForm.action = formAction;
		myForm.method = "post";
		returnValue = true;
	}else{
		returnValue = false;
	}
	myForm.submit();
	return returnValue;
}

// -->
