// JScript File

// Implements the shopCart object
function shopCart(rootElId){

    // Constructor.
    var tbodyEl = document.getElementById(rootElId);
    var item = new Array();
    var tableClassName = 'shoppingCart';
    var totalPrice = 0;
    var totalWeight = 0;
    var qty = 0;
    
	function shopCartItem() {
		this.sku = null;
		this.desc = null;
		this.price = null;
		this.weight = null;
	}    
    
    this.addItem = function(sku,desc,price,weight){
    	var newShopCartItem = new shopCartItem();
    	newShopCartItem.sku=sku;
		newShopCartItem.desc=desc;
		newShopCartItem.price=price;    
		newShopCartItem.weight=weight;    
    	item.push(newShopCartItem);   
    	// add to total.
    	totalPrice = totalPrice + parseFloat(price);
    	totalWeight = totalWeight + parseFloat(weight);
    	qty++;
    
    }
    
    this.removeItem = function(sku){  
    	for(var i=0;i<item.length;i++){	
    		if(item[i].sku == sku){
    			totalPrice = totalPrice - parseFloat(item[i].price);
    			totalWeight = totalWeight - parseFloat(item[i].weight);
    			qty--;
    			item.splice(i,1);
    			reset();
    			this.redraw();
    			// Remove from session.
				var parameters = "sku=" + sku 
				theRequest.post("/removefromcart", this.removeItemOnComplete, parameters);  //,this.showError); //for debugging
				break;
    		}
    	}
    }
    this.removeItemOnComplete = function(jsonObj){
    
    }
    
   	this.redraw = function(){
   		// Erase whatever is there.
   		reset();
   		// Draw the header
   		var currRow = addRow();
   		var currCol = addCol(currRow,"",null,true);
   		var currCol = addCol(currRow,"Item",null,true);
   		var currCol = addCol(currRow,"Description",null,true);
   		var currCol = addCol(currRow,"Price",null,true);
   		// Add all the items
   		for(var i=0;i<item.length;i++){	
   			var currRow = addRow();
   			var currCol = addCol(currRow,"",null);
   			// Create link to delete the line item.
   			var newAnchor = document.createElement('a');
			newAnchor.href = "javascript:shopCartObj.removeItem('" + item[i].sku + "')";
			currCol.appendChild(newAnchor);
			// Add the image
			var imgEl = document.createElement('img');
			imgEl.src = '/assets/images/trashcan.gif';
   			newAnchor.appendChild(imgEl);
   			// Add all other info      
/*
="item_name_1" value="Peanut Butter"/>
  <br/><input type="text" name="item_description_1" value="Chunky peanut butter."/>
  <br/><input type="text" name="item_quantity_1" value="1"/>
  <br/><input type="text" name="item_price_1
*/
   			
            var currCol = addCol(currRow,item[i].sku,null);
            addInput(currCol,"item_name_"+(i+1),item[i].sku);
   			var currCol = addCol(currRow,item[i].desc,null);
   			addInput(currCol,"item_description_"+(i+1),item[i].desc);
   			var currCol = addCol(currRow,item[i].price,null,null,true);
   			addInput(currCol,"item_price_"+(i+1),item[i].price);
   			addInput(currCol,"item_quantity_"+(i+1),"1");
   		}
   		// Add total.
   		if(item.length>0){
			var currRow = addRow();
			var currCol = addCol(currRow,"Total",3,true,true);
			var currCol = addCol(currRow,totalPrice.toFixed(2),null,true,true);
		}
   	}
   
   	function reset() {
   		// As we delete the nodes the length of oNodeList will vary so we
   		// set now how many iterations we want to have.
   		var cnt = tbodyEl.childNodes.length
   		for (var i=0; i<cnt; i++) {
   			tbodyEl.removeChild(tbodyEl.firstChild);
		}
	}
	
    // DOM support.       
	function addRow(){
		var newRow = document.createElement('tr');
		newRow.className = tableClassName;  
		tbodyEl.appendChild(newRow);
		return newRow;
	}// addRow
	function addCol(currRow,txt,colSpan,isTH,alignRight){
		var elName = 'td';
		if(isTH!=null) elName = 'th';
		var newCol = document.createElement(elName);
		newCol.className = tableClassName; 
		if(colSpan!=null) newCol.colSpan=colSpan;
		if(alignRight!=null) newCol.align="right";
		currRow.appendChild(newCol);
		var txtNode = document.createTextNode(txt);
		newCol.appendChild(txtNode);
		return newCol;
	}//addCol
	function addInput(currCol,iName,v){
		var newTextInput = document.createElement('input');
		newTextInput.type = "hidden";
		newTextInput.name = iName; //globalObjectName + "_input_" + item.unid;
		newTextInput.value = v;
		currCol.appendChild(newTextInput);;
	}//addTextInput
	
	this.setShipping = function(){
//alert('setShipping -  totalPrice=' + totalPrice + ' - totalWeight=' + totalWeight + " - qty=" + qty);	
/*
	Shipping
  <br/><input type="text" name="ship_method_name_1" value="UPS Ground"/>
  <br/><input type="text" name="ship_method_price_1" value="10.00"/>
  <br/><input type="text" name="ship_method_name_2" value="UPS Next Day"/>
  <br/><input type="text" name="ship_method_price_2" value="20.00"/>
  
  
  1 to 2 pieces:  4.8 oz (padded envelope)
  3 to 5 pieces:  8 oz  (5 x 5 x 5 box)
over 10 pieces:  12 oz (8 x 5 x 5 box)


PRIORITY Shipping (2 - 3 days) to 48 Continental 
0 to 1 lb $ 4.00
1 to 2 lb $ 6.00
2 to 3 lb $ 10.00
over 3 lb $ 12.00

NEXT DAY Shipping to 48 Continental 
0 to 1 lb $ 18.80
1 to 2 lb $ 18.80
2 to 3 lb $  23.00
over 3 lb $ 25.50

4)  Determine insurance rates based on total retail value: 
    $0 to $50                $1.35
    $50.01 to $100 ......... $2.30
    $100.01 to $200 ........$3.35
    $200.01 to $300 ........$4.40 
    $300.01 to $400 ........$5.45
    $400.01 to $500        $6.50
    $500.01 to $600        $7.55
    $600.01 to $700        $8.60
    $700.01 to $800        $9.65
    $800.01 to $900        $10.70
    $900.01 to $1000       $11.75
    $1000.01 to $1100    $12.80
    $1100.01 to $1200    $13.85
    $1200.01 to $1300    $14.90
    $1300.01 to $1400    $15.95
    $1400.01 to $1500    $17.00 
*/

		var currRow = addRow();
		var currCol = addCol(currRow,"",null);
	
		if(totalPrice >= 150){	
			addInput(currCol,"ship_method_name_1","Free Shipping");
			addInput(currCol,"ship_method_price_1","0.00");
		}else{
			// Add the weight of the box
			if(qty <= 2){
				totalWeight += 4.8;
			}else if(qty <= 5){
				totalWeight += 8;
			}else{
				totalWeight += 12;
			}
			lbWeight = totalWeight/16;
			// Calculate the shipping based on weight
			var PriorityShipping = 0;
			if(lbWeight <= 1){
				PriorityShipping = 4.60;
			}else if(lbWeight <= 2){
				PriorityShipping = 7.50;
			}else if(lbWeight <= 3){
				PriorityShipping = 10.65;
			}else{
				PriorityShipping = 15.85;
			}
			var NextDayShipping = 0;
			if(lbWeight <= 1){
				NextDayShipping = 19.50;
			}else if(lbWeight <= 2){
				NextDayShipping = 21.40;
			}else if(lbWeight <= 3){
				NextDayShipping = 24.50;
			}else if(lbWeight <= 4){
				NextDayShipping = 27.60;				
			}else{
				NextDayShipping = 31.00;
			}
			// Determine insurance rates based on total retail value: 
			if(totalPrice <= 50){
				PriorityShipping += 5.10;
				NextDayShipping += 5.10;
			}else if(totalPrice <= 100){
				PriorityShipping += 6.25;
				NextDayShipping += 6.25;
			}else if(totalPrice <= 200){
				PriorityShipping += 7.40;
				NextDayShipping += 7.40;
			}
			
			/* all these are free because over $150.00
			else if(totalPrice <= 300){
				PriorityShipping += 4.40;
				NextDayShipping += 4.40;
			}else if(totalPrice <= 400){
				PriorityShipping += 5.45;
				NextDayShipping += 5.45;
			}else if(totalPrice <= 500){
				PriorityShipping += 6.50;
				NextDayShipping += 6.50;
			}else if(totalPrice <= 600){
				PriorityShipping += 7.55;
				NextDayShipping += 7.55;
			}else if(totalPrice <= 700){
				PriorityShipping += 8.60;
				NextDayShipping += 8.60;
			}else if(totalPrice <= 800){
				PriorityShipping += 9.65;
				NextDayShipping += 9.65;
			}else if(totalPrice <= 900){
				PriorityShipping += 10.70;
				NextDayShipping += 10.70;
			}else if(totalPrice <= 1000){
				PriorityShipping += 11.75;
				NextDayShipping += 11.75;
			}else if(totalPrice <= 1100){
				PriorityShipping += 12.80;
				NextDayShipping += 12.80;
			}else if(totalPrice <= 1200){
				PriorityShipping += 13.85;
				NextDayShipping += 13.85;
			}else if(totalPrice <= 1300){
				PriorityShipping += 14.90;
				NextDayShipping += 14.90;
			}else if(totalPrice <= 1400){
				PriorityShipping += 15.95;
				NextDayShipping += 15.95;
			}else if(totalPrice <= 1500){
				PriorityShipping += 17.00;
				NextDayShipping += 17.00;
			} */			
			// Set the data.
			addInput(currCol,"ship_method_name_1","PRIORITY (2-3 days)");
			addInput(currCol,"ship_method_price_1",PriorityShipping);
			addInput(currCol,"ship_method_name_2","NEXT DAY");
			addInput(currCol,"ship_method_price_2",NextDayShipping);
		}
	}// setShipping
}

