$(document).ready(function() {		  
	$(".jqzoom").jqueryzoom({
		xzoom: 425, //zooming div default width(default width value is 200)
		yzoom: 300, //zooming div default width(default height value is 200)
		offset: 20 //zooming div default offset(default offset value is 10
	});
});
	
//IMAGE PREVIEW STUFF
function chgMainImg(who) {
	$("#mainImg").attr('src','images/products/img/' + $(who).attr('alt'));
	$("#mainImg").attr('jqimg','images/products/img/' + $(who).attr('alt'));
	$(".imgcaption").html($(who).attr('name'));
}

function selChgMainImg(who) {
	var newid = $('#' + who + ' option:selected').val();
	if ($('#attrimg_' + newid).attr('alt') != undefined) {
		var newthm = 'images/products/img/' + $('#attrimg_' + newid).attr('alt');
		var newimg = 'images/products/img/' + $('#attrimg_' + newid).attr('alt');
		$("#mainImg").attr('src',newthm);
		$("#mainImg").attr('jqimg',newimg);
		$(".imgcaption").html($('#attrimg_' + newid).attr('name'));
	}
}

//ADD TO CART

function addToCart(id) {
	
	var allInputs = $(":input");
	var pid = allInputs[0].value;
	var q = $("#prodqty").val();
	var arrAttr = new Array();
	$("select").each(
		function() {
			arrAttr.push($(this).val());
		}
	);
	
	$.post("ajx_cart.cfm", {'attrs': arrAttr.join(), pid: id, qty: q, mode: "add"},
		function(data) {
			updCartBox();			
		}								  
	);
}

//VIEW THE CART
function viewCart() {
	$.post("ajx_cart.cfm", {mode: "view"},
		  function(data) {
			  $("#viewCart").html(data);
		  }
	  );
}

//DELETE FROM CART
function delCart(arrayid) {
	$.post("ajx_cart.cfm", {pid: arrayid, mode: "del"});
	viewCart();
	updCartBox();
}

function chkQty(orig,who) {
	if(who.qty.value != orig) {
		$.post("ajx_cart.cfm", {mode: "mod", pos: who.pos.value, qty: who.qty.value});
		viewCart();
	}
}

function chgShipping() {
	//Get the value of the shipping & Add it to the subtotal
	var shpcost = parseFloat($("#shipping").val()) + parseFloat($("#frmSubTotal").val());
	//Change the ShippingCost on-screen
	$("#shipCost").html("$" + $("#shipping").val());
	//Change the Total on-screen
	$("#totalCost").html("$" + shpcost.toFixed(2));
	//Change the Form Total
	$("#frmTotal").val(shpcost.toFixed(2));
}

//CHECK CUSTOMER TYPE (LOGIN)


function oldCust() {
	$(".loginReg").hide();
	$(".loginBtn").val("SIGN IN");
	$(".loginBtn").unbind();
	$("#custpass_confirm").removeClass("required");
	$("#custfname").removeClass("required");
	$("#custlname").removeClass("required");
	$("#custaddress").removeClass("required");
	$("#custcity").removeClass("required");
	$("#custstate").removeClass("required");
	$("#custzip").removeClass("required");
}

function newCust() {
	$(".loginReg").show();
	$(".loginBtn").val("REGISTER");
	$("#cForm").validate();
	$("#custpass_confirm").addClass("required");
	$("#custfname").addClass("required");
	$("#custlname").addClass("required");
	$("#custaddress").addClass("required");
	$("#custcity").addClass("required");
	$("#custstate").addClass("required");
	$("#custzip").addClass("required");
	$(".loginBtn").bind("click", function(e) {
		return passMatch();
	});
}

function passMatch() {
	if ($("#custpass").val() == $("#custpass_confirm").val())
	{
		return true;
	}
	else
	{
		$(".loginPass").css({background: "red", border: "3px #900 solid"});
		return false;
	}
}

//Apply Coupon
function applyCoupon() {
	$.post("ajx_cart.cfm", {mode: "cpn", cpval: $("#couponcode").val()},
		function(data) {
			if (jQuery.trim(data) == "invalidcoupon") {
				alert("Coupon is invalid.");
				$("#couponcode").addClass("badInp");
			} else {
				viewCart();
				$("#couponblock").hide();
			}
		}
	);
}

function invalidCoupon(what) {
	alert(what + ' is not a valid coupon code.');	
}