var check_button = function(ele, imgname){
	if(ele.src != "images/"+imgname){
		ele.src = "images/"+imgname;
	}
}

var show_button = function(ele, imgname){
	if(ele.src != "images/"+imgname){
		ele.src = "images/login_but.gif";
	}
}

var opencart = function(p,mode,c,page){
	$("cart_spinner_"+p).show();
	$("cart_but_"+p).hide();
	var bypass = true;
	if(page == 'cart.php'){
		bypass = false;
	}
	var page = base64_encode(page);
	if(mode == 'edit'){
		if(bypass){
			var cart_url = 'ajaxserver/user/window_cart_message.php?p='+p+'&c='+c+'&page='+page;
			create_modal(cart_url,'350','200',true);
		}else{
			cart_url = 'ajaxserver/user/editcart_modal.php?cart_id='+c+'&page='+page;
			create_modal_gagazu1(cart_url,'700','400',true,c);
		}
	}else{
		var cart_url = 'ajaxserver/user/addcart_modal.php?product_id='+p+'&page='+page;
		create_modal_gagazu2(cart_url,'700','400',true);
	}
	
}

var opencoupon = function(p,page){
	//alert($("coupon").value);
	$("cart_spinner").show();
	$("cart_but_").hide();	
	var page = base64_encode(page);
	var cart_url = 'ajaxserver/user/window_coupon_message.php?p='+p+'&page='+page;
	create_modal(cart_url,'550','300',true);
	return false;
}

/*var load_cart_services = function(c){
	///alert(c);
	var url = "ajaxserver/user/load_cart_services.php";
	new Ajax.Request(url,{
					 	method : 'post',
						parameters : { c:c },
						onSuccess : function(transport){
							var res = transport.responseText;
							//alert(res);
							$('service_html').update(res);
							$('rotater').hide();
							$('fire_button').show();
							$('fire_rotater').hide();
						},
						onFailure : function(transport){
							alert(transport.responseText+" Connection Failure");
						}
					 });
}

//update cart data with service
var update_shipping = function(serVal){
	var getVal = serVal.split("|");
	var shipping_price = parseFloat(getVal[1]);
	var product_quantity = parseInt($('quantity').value);
	var total_applicable_price = parseFloat($('total_applicable_price').value);
	
	
	if(!$('quantity').value.blank()){
		shipping_price = (shipping_price*product_quantity);
				
	}
	var rounded_price = shipping_price.toFixed(2);
	$('shipping_price').value = shipping_price;
	$('ship_price').update(rounded_price);
	total_applicable_price = (shipping_price+total_applicable_price);
	$('total_applicable_price').value = total_applicable_price;
	$('total_appl_price').update(total_applicable_price.toFixed(2));
}*/


/*var check_quantity = function(qVal){
	var stock = parseInt($('stock').value);
	var product_quantity = parseInt(qVal);
	var initial_price = parseInt($('initial_price').value);
	var initial_handling = parseFloat($('initial_handling').value);
	var shipping_price = '0.00';
	$('quantity_error').update("");
	
	if(qVal.blank()){
		$('quantity_error').update("Required");
	}else if(product_quantity <= 0){
		$('quantity_error').update("Quantity cannot be zero or less");
	}
	else if(product_quantity > stock){
		$('quantity_error').update("Quantity cannot be greater than "+stock);
	}else{
		if(!$('service_type').value.blank()){
			var spST = $('service_type').value.split("|");
			shipping_price = parseFloat(spST[1]);
			shipping_price = (product_quantity*shipping_price);
			$('ship_price').update(shipping_price.toFixed(2));
			$('shipping_price').value = shipping_price.toFixed(2);
		}
		//update handling
		var handling = (initial_handling*product_quantity);
		$('handling').value = handling;
		$('handle_dis').update(handling.toFixed(2));
		//update product price
		var product_price = (initial_price*product_quantity);
		$('product_price').value = product_price;
		$('point_dis').update(product_price.toFixed(2));
		//update total applicable price
		var total_applicable_price = (shipping_price+handling+product_price);
		$('total_applicable_price').value = total_applicable_price;
		$('total_appl_price').update(total_applicable_price.toFixed(2));	
		
	}
}*/

var check_quantity = function(qVal){
	var stock = parseInt($('stock').value);
	var product_quantity = parseInt(qVal);
	var initial_price = parseInt($('initial_price').value);
	var initial_handling = parseFloat($('initial_handling').value);
	
	$('quantity_error').update("");
	
	if(qVal.blank()){
		$('quantity_error').update("Required");
	}else if(product_quantity <= 0){
		$('quantity_error').update("Quantity cannot be zero or less");
	}
	else if(product_quantity > stock){
		$('quantity_error').update("Quantity cannot be greater than "+stock);
	}else{
		
		//update handling
		var handling = (initial_handling*product_quantity);
		$('handling').value = handling;
		$('handle_dis').update(handling.toFixed(2));
		//update product price
		var product_price = (initial_price*product_quantity);
		$('product_price').value = product_price;
		$('point_dis').update(product_price.toFixed(2));
		//update total applicable price
		var total_applicable_price = (handling+product_price);
		$('total_applicable_price').value = total_applicable_price;
		$('total_appl_price').update(total_applicable_price.toFixed(2));	
		
	}
}

//load service apis
var get_service = function(service, p){
	$('show_package').hide();
	$('package_name').update("");
	$('show_pickup').hide();
	$('pickup_name').update("");
	$('packaging_type').value = "";
	$('drop_type').value = "";
	$('shipping_price').value = "";
	
	if(service == ""){
		var html = "<select name='service_type' id='service_type'>";
		html += "<option value=''>Select</option>";
		html += "</select>";
		$('service_html').update(html);
	}
	if(service == 1){
		load_fedex_service(p);
	}
	if(service == 2){
		load_ups_service(p);
	}
}

//load fedex service
var load_fedex_service = function(p){
	$('rotater').show();
	$('fire_button').hide();
	$('fire_rotater').show();
	var url = "ajaxserver/user/load_fedex.php";
	new Ajax.Request(url,{
					 	method: 'get',
						parameters : { pid : p },
						onSuccess : function(transport){
							var res = transport.responseText;
							///alert(res);
							var spRes = res.split("~");
							$('service_html').update(spRes[0]);
							$('rotater').hide();
							$('fire_button').show();
							$('fire_rotater').hide();
							//packages and drops
							$('show_package').show();
							$('package_name').update(spRes[2]);
							$('show_pickup').show();
							$('pickup_name').update(spRes[4]);
							$('packaging_type').value = spRes[1];
							$('drop_type').value = spRes[3];
							
						},
						onFailure : function(transport){
							var res = transport.responseText;
							//alert(res);
							$('rotater').hide();
							$('fire_button').show();
							$('fire_rotater').hide();
						}
					 });
}

//load ups service
var load_ups_service = function(p){
	$('rotater').show();
	$('fire_button').hide();
	$('fire_rotater').show();
	var url = "ajaxserver/user/load_ups.php";
	new Ajax.Request(url,{
					 	method: 'get',
						parameters : { pid : p },
						onSuccess : function(transport){
							var res = transport.responseText;
							////alert(res);
							var spRes = res.split("~");
							$('service_html').update(spRes[0]);
							$('rotater').hide();
							$('fire_button').show();
							$('fire_rotater').hide();
							//packages and drops
							$('show_package').show();
							$('package_name').update(spRes[2]);
							$('show_pickup').show();
							$('pickup_name').update(spRes[4]);
							$('packaging_type').value = spRes[1];
							$('drop_type').value = spRes[3];
							
						},
						onFailure : function(transport){
							var res = transport.responseText;
							//alert(res);
							$('rotater').hide();
							$('fire_button').show();
							$('fire_rotater').hide();
						}
					 });
}




var addtocart = function(){
	var stock = parseInt($('stock').value);
	var bool = true;
	var qInt = parseInt($('quantity').value);
	var page = $('page').value;
	if($('quantity').value.blank()){
		$('quantity_error').update("Required");
		bool = false;
	}
	else
	{
		if(qInt <= 0){
			$('quantity_error').update("Zero not allowed");
			bool = false;
		}else{
			if(qInt > stock){
				$('quantity_error').update("Quantity cannot be greater than "+stock);
				bool = false;
			}
		}
	}
	/*if($('shipping_method').value.blank()){
		$('shipping_method_error').update("Required");
		bool = false;
	}
	if($('service_type').value.blank()){
		$('service_type_error').update("Required");
		bool = false;
	}*/
	
	if(bool){
		$('fire_button').hide();
		$('fire_rotater1').show();
		var P = Form.serialize('addtocart_frm');
		var url = "ajaxserver/user/addtocart.php";
		new Ajax.Request(url,{
						 	method : 'post',
							parameters : P,
							onSuccess : function(transport){
								var res = transport.responseText;
								//alert(res);
								//$('fire_button').show();
								$('fire_rotater1').hide();
								$('cart_message').show();
								setTimeout("window.location.href='"+page+"'", 3000);
							}
						 });
	}
}

var remove_cart = function(cart_id, cart_url){
	$("remcart_spinner_"+cart_id).show();
	$("remcart_but_"+cart_id).hide();
	var Params = "cart_id="+cart_id;
	var url = "ajaxserver/user/removecart.php";
	new Ajax.Request(url, {
					 	method : 'get',
						parameters : Params,
						onSuccess : function(transport){
							load_html('cart_list', cart_url);
						}
					 });
}





var process_checkout = function(){
	var mypoints = parseInt($('mypoints').value);
	var cart_points = parseInt($('cart_points').value);
	var bool = true;
	
	var P = Form.serialize('cart_frm');
	if(cart_points > mypoints){
		$('cart_error').update("Sorry checkout is not possible as less than "+cart_points+" points in your points account");
		bool = false;
	}else{
		$('chk_load').show();
		$('chk_but').hide();
		Dialog.info( {
						url: 'ajaxserver/user/process_checkout.php', 
						options: {
							method: 'post',
							parameters: P
						}
					 }, {
						 className: "alphacube", 
						 width:500, 
						 height:400, 
						 showEffect: Effect.Appear, 
						 hideEffect: Effect.Fade, 
						 showProgress: false,
						 onShow:function(){
							jQuery('.numfilter100').numeric();
							jQuery('.numfilter40').numeric();
						 }
					 });
	}
}

/*var validate_checkout = function(){
	var bool = true;
	if($('card_number').value.blank()){
		bool = false;
		$('card_number_error').update('Required');
	}else{
		if($('card_merchant').value == "Visa"){
			//$("card_number").maxlength = 16;
			if($('card_number').value.length != 16){
				bool = false;
				$('card_number_error').update('Visa requires 16 digit Card number');
			}
		}
		if($('card_merchant').value == "American Express"){
			if($('card_number').value.length != 15){
				bool = false;
				$('card_number_error').update('American Express requires 15 digit Card number');
			}
		}
		if($('card_merchant').value == "Discover"){
			if($('card_number').value.length != 16){
				bool = false;
				$('card_number_error').update('Discover requires 16 digit Card number');
			}
		}
	}
	if($('exp_mm').value.blank() || $('exp_yy').value.blank()){
		bool = false;
		$('card_expiry_err').update('Required');
	}else{
		if(isNaN($('exp_mm').value) || isNaN($('exp_yy').value)){
			bool = false;
			$('card_expiry_err').update('Numeric value required');
		}
		if(($('exp_mm').value == "00") || ($('exp_yy').value == "00")){
			bool = false;
			$('card_expiry_err').update('Invalid input');
		}
	}
	if($('card_csc').value.blank()){
		bool = false;
		$('card_csc_error').update('Required');
	}
	return bool;
}

var set_action = function(val){
	if(val == "ExpressCheckout"){
		$('final_chk_frm').action = "expresscheckout.php";
	}
}*/

var do_checkout = function(){
	var bool = true;
	if($('card_number').value.blank()){
		bool = false;
		$('card_number_error').update('Required');
	}else{
		if($('card_merchant').value == "Visa"){
			//$("card_number").maxlength = 16;
			if($('card_number').value.length != 16){
				bool = false;
				$('card_number_error').update('Visa requires 16 digit Card number');
			}
		}
		if($('card_merchant').value == "American Express"){
			if($('card_number').value.length != 15){
				bool = false;
				$('card_number_error').update('American Express requires 15 digit Card number');
			}
		}
		if($('card_merchant').value == "Discover"){
			if($('card_number').value.length != 16){
				bool = false;
				$('card_number_error').update('Discover requires 16 digit Card number');
			}
		}
	}
	if($('exp_mm').value.blank() || $('exp_yy').value.blank()){
		bool = false;
		$('card_expiry_err').update('Required');
	}else{
		if(isNaN($('exp_mm').value) || isNaN($('exp_yy').value)){
			bool = false;
			$('card_expiry_err').update('Numeric value required');
		}
		if(($('exp_mm').value == "00") || ($('exp_yy').value == "00")){
			bool = false;
			$('card_expiry_err').update('Invalid input');
		}
	}
	if($('card_csc').value.blank()){
		bool = false;
		$('card_csc_error').update('Required');
	}
	
	if(bool){
		$('final_chk_but').hide();
		$('final_chk_load').show();
		var P = Form.serialize('final_chk_frm');
		var url = "ajaxserver/user/do_checkout.php";
		new Ajax.Request(url, {
						 	method : 'post',
							parameters : P,
							onSuccess : function(transport){
								var res = transport.responseText;
								var spRes = res.split("|");
								//alert(res);
								if(spRes[0]==0){
									$('final_chk_load').update("<span style='color:#006600'>Checkout successfull, redirecting to order page in next 10 seconds</span>");
									setTimeout("window.location.href='order.php'", 10000);
								}else{
									$('final_chk_but').show();
									$('final_chk_load').hide();
									$('card_number_error').update(spRes[1]);
									$('card_number').value = "";
									$('card_number').focus();
									/*if(res==1){
										$('card_number_error').update("Card number should be unique");
										$('card_number_error').focus();
									}*/
								}
								
							},
							onFailure : function(){
								alert("Page not found");
								$('final_chk_but').show();
								$('final_chk_load').hide();
							}
						 });
	}
}
