var AJAXLock = false;
var AJAXLock = false;
//Disable right mouse click Script
//By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com
/*
var message="Permission denied.";


function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}

function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(message);return false") */
function CreateXMLHttpRequest()
{
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   alert("XMLHttpRequest not supported");
   return null;
}

var xhReq = CreateXMLHttpRequest();

function OnResponseIsFavorite()
{
	if (xhReq.readyState != 4) { return; }
	
	switch(xhReq.responseText)
	{
		case "T":
			document.getElementById("btnFavorite").value = "Remove from Favorites";
			break;
		case "F":
			document.getElementById("btnFavorite").value = "Add to Favorites";
			break;
		default:
			document.getElementById("btnFavorite").value = "Add to Favorites";
			break;
	}
	
	AJAXLock = false;
	return xhReq.responseText;
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function OpenOrder(num)
{
	window.open("./ordersheet.php?order=" + num, "orderSheet","location=1,status=0,scrollbars=0,width=600,height=600");
}


function OnResponseAddTotal(filePath, productId, description)
{
	if (xhReq.readyState != 4) { return; }
	if (xhReq.responseText == -1) { return; }
	var total = Math.round( ( parseFloat ( document.getElementById("moneytotal").innerHTML ) + parseFloat( xhReq.responseText ) ) * 100 ) / 100;
	cPreview = document.getElementById("currentorderpreview");
	cPrice = document.getElementById("currentorderprice");
	var newlistpreview = document.createElement('li');
	newlistpreview.setAttribute('id',"preview" + ++NextOrderImage);
	newlistpreview.setAttribute('style',"width: auto; height: auto; overflow: hidden;");
	newlistpreview.innerHTML = '<a href="#" onclick="OrderPreview(\'' + filePath + '\');">' + description + '</a>&nbsp;<span onclick="RemoveFromOrder(' + productId + ',\'' + filePath + '\',\'' + NextOrderImage + '\');">(<a href="#">X</a>)</span>';
	var newlistprice = document.createElement('li');
	newlistprice.setAttribute('id',"price" + NextOrderImage);
	newlistprice.innerHTML = '$' + parseFloat( xhReq.responseText ).toFixed(2);
	cPrice.appendChild(newlistprice);
	cPreview.appendChild(newlistpreview);
	document.getElementById("moneytotal").innerHTML = total.toFixed(2);
	AJAXLock = false;
	return xhReq.responseText;
}
function removechild(ctrl)
{
	while (ctrl.childNodes[0])
	{
		ctrl.removeChild(ctrl.childNodes[0]);
	}
}

function OnResponseFinalizeOrder () 
{
	if (xhReq.readyState != 4) { return; }
	document.getElementById("moneytotal").innerHTML = "0.00";
	removechild(document.getElementById("currentorderprice"));
	removechild(document.getElementById("currentorderpreview"));
	OpenOrder( xhReq.responseText );
	AJAXLock = false;
}

function OnResponseRemoveTotal( idNumber )
{
	if (xhReq.readyState != 4) { return; }
	if (xhReq.responseText == "-1") { return; }
	document.getElementById("preview" + idNumber).style['display'] = 'none';
	document.getElementById("price" + idNumber).style['display'] = 'none';
	var total = Math.round( ( parseFloat ( document.getElementById("moneytotal").innerHTML ) - parseFloat( xhReq.responseText ) ) * 100 ) / 100;
	document.getElementById("moneytotal").innerHTML = total.toFixed(2);
	AJAXLock = false;
	return xhReq.responseText;
}

function IsFavorite( pid )
{
	if (AJAXLock)
	{
	   return null;
	}
	AJAXLock = true;
	xhReq.open("GET","favorites.php?action=0&pid=" + pid, true);
	xhReq.onreadystatechange = OnResponseIsFavorite;
	xhReq.send(null);
}

function ToggleFavorite( pid )
{
	if (AJAXLock)
	{
	   return null;
	}
	AJAXLock = true;

	xhReq.open("GET","favorites.php?action=3&pid=" + pid, true);
	xhReq.onreadystatechange = OnResponseIsFavorite;
	xhReq.send(null);
}

function AddToOrder( productId, filePath, description )
{
	if (AJAXLock)
	{
	   return null;
	}
	AJAXLock = true;

	xhReq.open("GET","orderengine.php?action=1&productid=" + productId + "&filepath=" + filePath, true);
	xhReq.onreadystatechange = function() { OnResponseAddTotal(filePath, productId, description); } ;
	xhReq.send(null);	
}

function RemoveFromOrder( productId, filePath, idNumber )
{
	if (AJAXLock)
	{
	   return null;
	}
	AJAXLock = true;

	xhReq.open("GET","orderengine.php?action=2&productid=" + productId + "&filepath=" + filePath, true);
	xhReq.onreadystatechange = function() { OnResponseRemoveTotal(idNumber); } ;
	xhReq.send(null);	
}

function FinalizeOrder()
{
	window.open("./orderengine.php?action=3", "orderSheet","location=1,status=0,scrollbars=0,width=600,height=600");
	location.reload();
}

// new shopping cart code.

function product( id, name, description, price, elements )
{
	this.id = id;
	this.name = name;
	this.description = description;
	this.price = price;
	this.elements = new Array();
	this.element_pointer = 0;
	this.element_size = elements;
}

function shopping_cart( product_list, user_cart, update_callback )
{
	this.total = 0.00;
	this.sub_total = 0.00;
	this.currency_type = "USD";
	this.tax_rate = 0.06;

	this.product_list = product_list;
	this.user_cart = user_cart;
	this.update_callback = update_callback;
	
	this.selected_product = new product( -1, "","", 0, 0 );
	
	this.select_product = function( id ) {
		this.selected_product = this.product_list[ id ];
		this.selected_product.elements = new Array();
		this.selected_product.element_pointer = 0;
		this.update("select_product");
		return;
	}
	
	this.get_cart = function () {
		return this.user_cart;
	}
	
	this.select_element = function( id ) {
		if ( this.selected_product.elements.length < this.selected_product.element_size )
		{
			this.selected_product.elements.push(id);
			this.update("select_element");
		}
		return;
	}
	
	this.remove_element = function ( slot ) {
		if ( this.selected_product.elements.length <= this.selected_product.element_size )
		{
			this.selected_product.elements.splice(slot,1);
			this.update("remove_element");			
		}
	}
	
	this.add_product_to_cart = function() {
		if ( this.selected_product.elements.length != this.selected_product.element_size )
			alert('You need to select the correct amount of images.');
		else
		{
			var index = 0;
			var shopping_cart = this;
			$.ajax({
				type: "GET",
				url: "orderengine.php",
				data: "action=1&id=" + shopping_cart.selected_product.id + "&products=" +shopping_cart.selected_product.elements.join(','),
				success: function( msg ) {
					// parse messages from server, repeat.			
					index = msg;
					shopping_cart.user_cart[index] = [shopping_cart.selected_product.id, shopping_cart.selected_product.elements];
					shopping_cart.update("add_product_to_cart");
				}
			});
		}
		return;
	};
	
	this.finalize_order = function() {
			$.ajax({
				type: "GET",
				url: "orderengine.php",
				data: "action=3",
				success: function( msg ) {
					if (msg != "F")
						window.location.href = "./ordersheet.php?order=" + msg;
					else
						alert("Failed to create order.");
					// OpenOrder(msg);
				}
			});
			this.user_cart = new Array();
			this.update("finalize_order");
			
	}
	
	this.modify_product_in_cart = function( index ) {
		return;
	};
	
	this.remove_product_from_cart = function( index ) {
		//this.update_price(-1*this.user_cart[ index ].price);
		this.user_cart[index] = null;
		$.ajax({
				type: "GET",
				url: "orderengine.php",
				data: "action=2&id=" + index,
				success: function( msg ) {
					// parse messages from server, repeat.
							
					
					//alert(msg);
					
				}
		});
		//this.user_cart.push([this.selected_product.id, this.selected_product.elements]);
		//this.update("add_product_to_cart");
		this.update("remove_product_from_cart");
		return;
	};
		
	this.update = function( action ) { this.update_callback( this, action ); return; };
}


