var _global = new Object();

function addToCart(theForm, o, single){
	var url 	= theForm.action;
	var method	= theForm.method;
	var data 	= jQuery("#" + theForm.id).serialize();

	data += "&xhr=1";
	
	jQuery.ajax({
		type: method,
		url: url,
		data: data,
		success: ajax_response
	});
	
	cartConfirm(theForm, theForm.code.value , o, single);
	return false;
}

function cartConfirm(obj, item, o, single){
	var product = "";
	var price = "";
	var confirm = document.getElementById("cartconfirm");
	var qty = 1;
	
	var error = '';
	if ($('#loggedout').html()) {
		error = $('#loggedout').html();
	}
	
	$.jGrowl.defaults.position = "center";
	$.jGrowl.defaults.life = 5000;
	$.jGrowl.defaults.theme = "hannants_notify";
	
	if(single){
		$.jGrowl("You have successfully added the following product to your cart:<div style='margin-top: 10px; padding: 5px'><div style='float: left; width'>" + product + "</div><div style='float: right; margin-right: 10px; font-weight: bold'>" + obj['code'].value + "</div><br style='clear: both' /></div>");
	} else {
		var row = obj.parentNode.parentNode;
		var qty = obj["quantity"].value;

		product = row.cells.item(1).innerHTML;
		price = row.cells.item(6).innerHTML;

		if(price.length){
			 price += " x " + qty;
		}
	
		$.jGrowl("You have successfully added the following product to your cart:<div style='margin-top: 10px; padding: 5px'><div style='float: left; width'>" + product + "</div><div style='float: right; margin-right: 10px; font-weight: bold'>" + price + "</div><br style='clear: both' />" + error + "</div>");
	}
	
	
	return false;
}

function watch_product(code){
	jQuery.ajax({
		type: "post",
		url: "/ajax-watch.php",
		data: "&wcode=" + code,
		success: ajax_response
	});
	
	watch_confirm(code);
	return false;
}

function watch_confirm(code){
	var confirm = document.getElementById("cartconfirm");
	
	$.jGrowl.defaults.position = "center";
	$.jGrowl.defaults.life = 5000;
	$.jGrowl.defaults.theme = "hannants_notify";		
	$.jGrowl("The product \"" + code + "\" has been added to your watching list");
}

function number_format(str){
	var n = str.toString();

	while(/(\d+)(\d{3})/.test(n)){
		n = n.replace(/(\d+)(\d{3})/, "$1,$2");
	}

	return n;
}

function ajax_response(originalRequest) {
  /*  if(!bHasRedirect) {
        //process originalRequest.responseText;
        alert( 'ok' );
    }
    else {
        bHasRedirect = false;
        ajax_request(originalRequest.responseText, "");
    }*/
}




// full details page - image gallery swapper thingy
function swapImage( image ) {
	
	var target = document.getElementById( "big_image" );
		
	target.src = "/images/small/" + image;
	
	return true;
}























function addToCartProduct(theForm, o, single){
	var url 	= theForm.action;
	var method	= theForm.method;
	var data 	= jQuery("#" + theForm.id).serialize();

	data += "&xhr=1";
	
	jQuery.ajax({
		type: method,
		url: url,
		data: data,
		success: ajax_response
	});
	
	cartConfirmProduct(theForm, theForm.code.value , o, single);
	return false;
}

function cartConfirmProduct(obj, item, o, single){
	var product = "";
	var price = "";
	var confirm = document.getElementById("cartconfirm");
	var qty = 1;
	
	$.jGrowl.defaults.position = "center";
	$.jGrowl.defaults.life = 5000;
	$.jGrowl.defaults.theme = "hannants_notify";
		
	if(single){
		$.jGrowl("You have successfully added the following product to your cart:<div style='margin-top: 10px; padding: 5px'><div style='float: left; width'>" + product + "</div><div style='float: right; margin-right: 10px; font-weight: bold'>" + document.title.split("- ")[1] + "</div><br style='clear: both' /></div>");
	} else {
		var row = obj.parentNode.parentNode;
		var qty = obj["quantity"].value;

		product = row.cells.item(0).innerHTML;
		price = row.cells.item(1).innerHTML;

		if(price.length){
			 price += " x " + qty;
		}
	
		$.jGrowl("You have successfully added the following product to your cart:<div style='margin-top: 10px; padding: 5px'><div style='float: left; width'>" + product + "</div><div style='float: right; margin-right: 10px; font-weight: bold'>" + price + "</div><br style='clear: both' /></div>");
	}
	
	
	return false;
}



