var wpcocontent = 1;

jQuery(document).ready(function() {
	jQuery("[id*=checkboxall]").click(function() {
		var status = this.checked;
		
		jQuery("[id*=checklist]").each(function() {
			this.checked = status;	
		});
	});
	
	jQuery("input[id*=checkinvert]").click(function() {
		this.checked = false;
	
		jQuery("input[id*=checklist]").each(function() {
			var status = this.checked;
			
			if (status == true) {
				this.checked = false;
			} else {
				this.checked = true;
			}
		});
	});
});

function wpco_scroll(selector) {
	var targetOffset = jQuery(selector).offset().top;
    jQuery('html,body').animate({scrollTop: targetOffset}, 500);
}

function wpcochangedisplay(number, value) {
	jQuery('#displayproducts' + number).hide();
	jQuery('#remoteproducts' + number).hide();
	jQuery('#displaycategories' + number).hide();
	jQuery('#displaycart' + number).hide();
	jQuery('#dropdown' + number).hide();
	
	if (value == "products" || value == "remoteproducts") {	
		jQuery('#displayproducts' + number).show();
		
		if (value == "remoteproducts") {
			jQuery('#remoteproducts' + number).show();
		}
	} else if (value == "cart") {
		jQuery ('#displaycart' + number).show();
	} else if (value == "categories") {
		jQuery('#displaycategories' + number).show();
	}
	
	if (value == "products" || value == "categories" || value == "suppliers") {
		jQuery('#dropdown' + number).show();	
	}
}

/* Dynamic Product Price Update */
function wpco_updateproductprice(productid) {
	var mytime = new Date().getTime();
	var formValues = jQuery('#addtocart' + productid).serialize();
	
	jQuery.post(wpcoAjax + "?cmd=product_price&mytime=" + mytime, formValues, function(response) {
		if (response != "") {
			if (jQuery('#productprice' + productid + ' .priceinside').length == 0) {
				jQuery('#productprice' + productid + '').html(wpcoCurrency + '<span class="priceinside">' + response + '</span>').hide().fadeIn();	
			} else {
				jQuery('#productprice' + productid + ' .priceinside').html(response).hide().fadeIn();
			}
		}
	});
}

/* Add to cart function */
function wpco_addtocart(form, productid, number) {
	var formvalues = jQuery("#addtocart" + productid).serialize();
	var mytime = new Date().getTime();
	
	jQuery('#submit' + productid).hide();
	jQuery('#message' + productid).hide();
	jQuery('#loading' + productid).show();
	jQuery('#added' + productid).hide();
	jQuery('#widget-cart-errors').hide();
	
	jQuery.post(wpcoAjax + "?cmd=add_to_cart&number=" + number + "&mytime=" + mytime + "", formvalues, function(data) {
		var success = jQuery(data).find('success').text();
		var message = jQuery("message", data).text();
		var html = jQuery("html", data).text();
		
		if (success == "Y") {
			var newmessage = '<img src="' + wpcoURL + '/images/accept.png" /> ' + message;	
		} else {
			var newmessage = '<img src="' + wpcoURL + '/images/deny.png" /> ' + message;
		}
																												
		if (jQuery("#" + number).length != 0) {
			if (success == "Y") {
				jQuery("#" + number).html(html);
			} else {
				jQuery("#widget-cart-errors").html(html).show();
			}
			
			wpco_scroll(jQuery('#' + number));
		}
		
		jQuery('#submit' + productid).show();
		jQuery('#loading' + productid).hide();
		
		jQuery('#added' + productid).html(newmessage).show();
	}, 'xml');
}

function wpco_gencouponcode() {
	var mytime = new Date().getTime();
	jQuery("#couponcodeloading").show();
	jQuery("#couponcodelink").hide();
	
	jQuery.post(wpcoAjax + "?cmd=gen_coupon_code&mytime=" + mytime, false, function(data) {
		jQuery("#couponcodecol").html(data);
		jQuery("#couponcodeloading").hide();
		jQuery("#couponcodelink").show();
	});
}

function wpco_emptycart(number) {
	wpco_scroll(jQuery("#" + number));
	var mytime = new Date().getTime();
	
	jQuery.post(wpcoAjax + "?cmd=empty_cart&number=" + number + "&mytime=" + mytime + "", false, function(data) {
		jQuery("#" + number).html(data);
	});
}