$(function(){
	$.ajaxSetup({
  		cache: false
	})	
})


updateCart = function(id,quantity,discountcode){
	var objSelf = this;
	$( document ).trigger("cartCalculate");
	$.ajax({
			url: "/ehio/plugins/cart/handlers/ajax.cfm",
			global: false,
			type: "POST",
			cache: false,
			data: {
				method: "update",
				id: id,
				quantity: quantity,
				discountcode: discountcode
				},
			dataType: "html",
			success: function(msg){
				if (msg == 'true'){
					$( document ).trigger("cartUpdated");
				} else {
					alert(msg + ' er is iets fout gegaan');
				}
			}
		}
	);
	$('#ehiocart_shipping').load('/views/cart/cartShipping.cfm');
}

deleteItem = function(id){
	var objSelf = this;
	$( document ).trigger("cartCalculate");
	$.ajax({
			url: "/ehio/plugins/cart/handlers/ajax.cfm",
			global: false,
			type: "POST",
			cache: false,
			data: {
				method: "delete",
				id: id
				},
			dataType: "html",
			success: function(msg){
				if (msg == 'true'){
					$( document ).trigger("cartUpdated");
				} else {
					alert(msg + ' er is iets fout gegaan');
				}
			}
		}
	);
	$('#ehiocart_shipping').load('/views/cart/cartShipping.cfm');
}


doPayment = function(msg){
	//$('#buttonpayment').attr("disabled", true);
};

cartMessage = function(msg) {
	if ($('#ehiocart_cartcalculate').is(':hidden')) {
		$('#ehiocart_cartcalculate').html(msg);
		$('#ehiocart_cartcalculate').fadeIn("slow");
		$('#ehiocart_cartcalculate').animate({opacity: 1.0}, 1500);
		$('#ehiocart_cartcalculate').fadeOut("slow");
	} else {
		$('#ehiocart_cartcalculate').fadeOut("slow");
	}
}

cartError = function(msg) {
	if ($('#ehiocart_error').is(':hidden')) {
		$('#ehiocart_error').html(msg);
		$('#ehiocart_error').fadeIn("slow");
		$('#ehiocart_error').animate({opacity: 1.0}, 5000);
		$('#ehiocart_error').fadeOut("slow");
	} else {
		$('#ehiocart_error').fadeOut("slow");
	}
}

cartErr = function(msg) {
	$('#ehiocart_error').hide().html(msg).fadeIn("slow").animate({opacity: 1.0}, 5000).fadeOut("slow");
}

$(function() {
	var menuId = Right(window.location.href,3);
	$('#ehiocart_content').load('/views/cart/cartContent.cfm');
	$('#ehiocart_checkout').load('/views/cart/cartCheckout.cfm');
	$('#ehiocart_summary').load('/views/cart/cartSummary.cfm');
	if (menuId == 851) {
		$('#ehiocart_invoice').load('/views/cart/cartInvoice.cfm');
	} else {
		$('#ehiocart_invoice').load('/views/cart/cartInvoiceView.cfm');
	}
	$('#ehiocart_summary').load('/views/cart/cartSummary.cfm');
	$('#ehiocart_login').load('/views/cart/cartLogin.cfm');
	$('#buttonpayment').removeAttr("disabled"); 
	$('#ehiocart_cartcalculate').hide();
	$(document).bind("cartUpdated",function(){
		$('#ehiocart_content').load('/views/cart/cartContent.cfm');
		$('#ehiocart_checkout').load('/views/cart/cartCheckout.cfm');
		$('#ehiocart_summary').load('/views/cart/cartSummary.cfm');
		$( document ).trigger("cartCalculate");
 	});
	
	$(document).bind("cartLogin",function(){
		$('#ehiocart_content').load('/views/cart/cartContent.cfm');
		$('#ehiocart_checkout').load('/views/cart/cartCheckout.cfm');
		$('#ehiocart_summary').load('/views/cart/cartSummary.cfm');
		$('#ehiocart_invoice').load('/views/cart/cartInvoiceView.cfm');
		$('#ehiocart_login').load('/views/cart/cartLogin.cfm');
		if ($('#ehiocart_shipping').is(":visible")) {
			$('#ehiocart_shipping').load('/views/cart/cartShipping.cfm');
		}
		$( document ).trigger("cartCalculate");
 	});
	
	$(document).bind("cartCalculate",function(){
 	});
	
	doLogin = function(){
		blockUI();
		$.ajax({
			url: "/ehio/plugins/user/login.cfm",
			global: false,
			type: "POST",
			cache: false,
			data: {
				username: $('input[name=username]').val(),
				password: $('input[name=password]').val()
			},
			dataType: "html",
			success: function(msg){
				if (msg == 'true'){
					$( document ).trigger("cartLogin");
				} else {
					$.unblockUI();
					alert(msg + ' er is iets fout gegaan');
				}
			}
			
		});
		
	};
	
	$('fieldset#shippingaddress').change(function(){
		$('input#checkshipping').attr('checked',false);	
	});
	
});

rememberForm = function(val){
	$(val + " input").each(function(){
		$(this).bind("change", function(){
			$.ajax({
				url: "/ehio/plugins/checkout/ajax/rememberUser.cfm",
				global: false,
				type: "POST",
				cache: false,
				data: {
					fieldname: $(this).attr('name'),
					fieldvalue: $(this).val()
				},
				dataType: "html"
			
			})
			
		});
	});
	
	
	$(val + " select").each(function(){
		$(this).bind("change", function(){
			$.ajax({
				url: "/ehio/plugins/checkout/ajax/rememberUser.cfm",
				global: false,
				type: "POST",
				cache: false,
				data: {
					fieldname: $(this).attr('name'),
					fieldvalue: $(this).val()
				},
				dataType: "html"
			
			})
			
		});
	});
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
