function Shop()
{
	this.oAjaxThreads = new Array();
	this.contactInfo = '';
}
Shop.prototype.getNextThread = function()
{
	var i = 0;
	
	if (this.oAjaxThreads.length == 0)
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}

	while (this.oAjaxThreads[i] && this.oAjaxThreads[i].working && i < this.oAjaxThreads.length )
	{
		i++;
	}
	
	if (!this.oAjaxThreads[i])
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}
	
	return this.oAjaxThreads[i];
}

Shop.prototype.confirmDeleteCart = function(id, index)
{
	if(confirm('Are you sure you want to delete this item?'))
	{
		$.ajax({
		   type: "POST",
		   url: '/shop/deleteFromCart/',
		   data: "packageId="+id+"&index="+index,
		   success: function(data)
		   {
		       $("#trShoppingCart_"+index).remove();
		       $("#shoppingCartTitle").after( "<p id='pTitle'></p>" );
		       $("#pTitle").addClass("notice");
		       $("#pTitle").css("text-align","center");
		       $("#pTitle").html("Product successfully removed from cart");
		       setTimeout("$(\"#pTitle\").slideUp();", 2000);
				
			   $("#shoppingCartTableContainer").html(data);
							   
		       var countProducts = $("#shoppingCartProducts").val();
		       
		       if(countProducts && countProducts==0)
		       {
		       	  $("#shoppingCartTotals").remove();
		       	  $(".shoppingCartDuration").remove();
		       	  $(".shoppingCartProductRelations").remove();
		       	  $(".shoppingCartFinish").remove();
		       }
		   }
		});
	}
}

Shop.prototype.addDelivery = function(deliveryObject,productId,deliveryDate,listener)
{
	var oAjax = this.getNextThread();
	oAjax.url = '/shop/addDelivery/';
	oAjax.method = 'post';
	oAjax.data = 'delivery='+deliveryObject+'&productId='+productId+'&deliveryDate='+deliveryDate;
	oAjax.open();
	
	if(listener)
	{
		oAjax.addRequestListener(this, this.finishAddDelivery, false);
	}
}
Shop.prototype.finishAddDelivery = function(xmlDocument, text)
{
	var oParser = new XMLParser(xmlDocument);
    var delivery = oParser.getNodeValue('delivery');
    var productId = oParser.getNodeValue('productId');
    window.location.href = '/shop/productInfoDelivery/'+productId;			
}
Shop.prototype.side = function(sideObject,sideProduct)
{
	document.getElementById('continue_button').style.display = '';
	/*var oAjax = this.getNextThread();
	oAjax.url = '/shop/addSide/';
	oAjax.method = 'post';
	oAjax.data = 'productSideId='+sideProduct+'&sideId='+sideObject;
	oAjax.open();*/
}

Shop.prototype.confirmDelete = function(controller, id)
{
	if(confirm('Are you sure you want to delete this item?'))
	{
		window.location.href = '/admin/'+controller+'/delete/'+id;
	}
}

Shop.prototype.setVariation = function(productId, variationId)
{
	window.open("/shop/details/"+productId+"/"+variationId,"_top","");
}

Shop.prototype.finishOrder = function(obj)
{
	if(obj == "continue_button")
	{
		document.getElementById('artwork_prices').style.display = 'none';
		document.getElementById('continue_button').style.display = '';
		document.getElementById('sideQty').value = '';
	}
	else if(obj == "artwork_prices")
	{
		document.getElementById('continue_button').style.display = '';
		document.getElementById('artwork_prices').style.display = '';
		document.getElementById('sideQty').value = '1';
	}
}

Shop.prototype.showHide = function(objId)
{
	var object = document.getElementById(objId);
	if(this.contactInfo)
	{
		document.getElementById(this.contactInfo).style.display = 'none';
		document.getElementById('newContactInfo').style.display = 'none';
	}
	if(object)
	{
		if (object.style.display == 'none')
		{
			object.style.display = '';
			this.contactInfo = objId;
		}
		else
		{
			object.style.display = 'none';
		}
	}
}

Shop.prototype.addNewContact = function(objId)
{
	var object = document.getElementById(objId);
	var newShipping = document.getElementById("newShipping")
	if(this.contactInfo)
	{
		document.getElementById(this.contactInfo).style.display = 'none';
	}
	if(object)
	{
		if (object.style.display == 'none')
		{
			object.style.display = '';
			newShipping.value = 'true';
		}
		else
		{
			object.style.display = 'none';
			newShipping.value = 'false';
		}
	}
}

Shop.prototype.validateContactInfo = function()
{
	var form = document.getElementById("order");
	var newShipping = document.getElementById("newShipping")
	var address = document.getElementById("address")
	var city = document.getElementById("city")
	var county = document.getElementById("county")
	var zipcode = document.getElementById("zipcode")
	var validated = true;
	
	if(newShipping.value == 'true')
	{
		if(address.value == '')
		{
			validated = false;
			document.getElementById('submitMsgAddress').style.display = '';
			document.getElementById('submitMsgAddress').innerHTML = 'Your Address cant be null.<br />';
		}
		else
		{
			document.getElementById('submitMsgAddress').style.display = 'none';
		}
		
		if(city.value == '')
		{
			validated = false;
			document.getElementById('submitMsgCity').style.display = '';
			document.getElementById('submitMsgCity').innerHTML = 'Your City cant be null.<br />';
		}
		else
		{
			document.getElementById('submitMsgCity').style.display = 'none';
		}
		
		if(county.value == '')
		{
			validated = false;
			document.getElementById('submitMsgCounty').style.display = '';
			document.getElementById('submitMsgCounty').innerHTML = 'Your County cant be null.<br />';
		}
		else
		{
			document.getElementById('submitMsgCounty').style.display = 'none';
		}
		
		if(zipcode.value == '')
		{
			validated = false;
			document.getElementById('submitMsgPostcode').style.display = '';
			document.getElementById('submitMsgPostcode').innerHTML = 'Your Postcode cant be null.<br />';
		}
		else
		{
			document.getElementById('submitMsgPostcode').style.display = 'none';
		}
		if(validated == true)
		{
			form.submit();
		}
	}
	else
	{
		address.value = '';
		form.submit();
	}
}

Shop.prototype.setDomainPrice = function(object, domainBase64, price)
{
	var year = new Number(object.value);
	var price = new Number(price);
	document.getElementById("domain_price_label_"+domainBase64).innerHTML = year*price;
}

Shop.prototype.checkDomainYears = function(domainBase64, price)
{
	var years = document.getElementById("domain_years_"+domainBase64);
	if(years && years.value<2)
	{
		years.value = 2;
		this.setDomainPrice(years, domainBase64, price);
	}
}

var oShop = new Shop();
