// JavaScript Document



function updateLineItem(liID, type){
	
	var qty=(type=="update"?document.getElementById("product_qty_"+liID).value:0);
	
	if(qty<0){
		alert("<font color='red'>Please enter a valid quantity</font>");
		return false;
	}
	else if(!(/^-?\d+$/.test(qty))){
		//alert(qty);
	
		alert("<font color='red'>Please enter a valid quantity</font>");
		return false;
	}
	else{
		if(qty==0){
			
			if(confirm("Are you sure that you want to remove this item from your cart?"))
				updateLineItemAjax(liID, qty);
			else
				return false;
		}
		else
			updateLineItemAjax(liID, qty);
	}
}


function updateLineItemAjax(liID, qty){
	
	var page="storeViews/ajax/ajax_cart_update.php";
	new Request.JSON({
	
			url: page,
			method: 'post',
			data: {
					'qty': qty,
					'liID': liID,
					'crand': $('crand').value,
					'order_hash': $('order_hash').value				
			},
			
			onComplete: function(jsonObj){
				
				JSONHandler(jsonObj);
				storeMessage.show(writeAlert("Cart Updated", false));
				setTimeout("storeMessage.hide();", 3000);
			}
			 
	 }).send();
	
	
}

function addSingleProduct(model){

	var page="storeViews/ajax/ajax_cart_addProduct.php";
	
	
	var OGS=$$('select.og');
	
	var data=new Array();
	
	if(OGS!=null && OGS.length){
		
		for(var i=0; i<OGS.length; i++){
			
			//eval("data."+OGS[i].getProperty('ogID')+"=OGS[i].options[OGS[i].selectedIndex].value");
			
			var ogID=OGS[i].getProperty('ogID');
			var ddVal=OGS[i].options[OGS[i].selectedIndex].value;
			
			
			data.push({
						ogID: ogID,
						ddVal: ddVal
					  });
					
		}
		
	}
	
	model=escape(model);
	
	new Request.JSON({
	
			url: page,
			method: 'post',
			data: {
					'qty': 1,
					'model': model,
					'ops': (data.length?data:0)
			},
			
			onComplete: function(jsonObj){
				
				JSONHandler(jsonObj);	
				storeMessage.show(writeAlert("Cart Updated", false));
				setTimeout("storeMessage.hide();", 3000);
			}
			 
	 }).send();
		

}

function setShippingMethod(){
	
	var SM=$("shipping_methods_select");
	var page="storeViews/ajax/ajax_cart_updateShippingMethod.php";
	
	
	
	if(SM){		
		sVal=SM.options[SM.selectedIndex].value;		
	}
	else{
		sVal="0_0";
	}
	
	
	new Request.JSON({
	
			url: page,
			method: 'post',
			data: {
					'SM': sVal					
			},
			
			onComplete: function(jsonObj){
				
				JSONHandler(jsonObj);								
			}
			 
	 }).send();
		
	
}

function disableCheckout(){
	
	$$('.Checkout').each(function(el, i){
								  
								  	var tag=el.get('tag');
								  
								  	 if('input'==tag){										 
										 el.disabled=true;									 
									 }
									 else if('a'==tag){
										 el.removeEvents('click');
									 }
								})
	
}


function expressCheckout(e){
	
	disableCheckout();	
	//Validate.checkSubmit(e);
	var form=$('order_form');
	form.setProperty('action', KAPELLE_STORE_EXPRESS_CHECKOUT);
	form.submit();
	
	
}



function validateOrder(){
	
	disableCheckout();
	
	if($('num_items').value==0){
		alert("Your cart is empty");
		return false;
	}
	
	return true;
		
}

