///////////////////////////////
//          Common           //
///////////////////////////////

function formatURL(strURL) { // format database URL
	return strURL.replace(/#urldata#/, _urldata);
}

///////////////////////////////
//           Shop            //
///////////////////////////////

function addItem() {
	var colourid = document.getElementById("ColourId").value;
	var sizeid = document.getElementById("SizeId").value;
	var quantity = parseInt(document.getElementById("Quantity").value);
	
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getQuantity', {"colourid":colourid, "sizeid":sizeid }, resultQuantity);

	function resultQuantity(r){
		if(quantity > r.allocation && r.isallocation){ // Check quantity
			alert('Currently there are only ' + r.allocation + ' items avaliable.');
		}
		else { // Display Checkout option
			objFrm = document.getElementById('frmProduct');
			isCheckout = confirm("'OK' to checkout, 'Cancel' to continue shopping.");
			
			if(!isCheckout)
				objFrm.returnURL.value = 'true';
			
			objFrm.submit();
			
		}
	}
}

function deleteItem(id) {
	frm = document.getElementById('frmBasket');
	frm.action.value = 'delete';
	frm.itemId.value = id;
	frm.submit();
}

function getColour(id){
	DWRUtil.useLoadingMessage();
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getColourDetails', id, resultColourDetails);
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getColourImages', id, resultColourImages);
	
	function resultColourDetails(r) {
		var getValue = function (thisRow) { return thisRow.sizeid; }
		var getText = function (thisRow) { return thisRow.sizename;}
		document.getElementById("Code").innerHTML = r.code[0];
		DWRUtil.removeAllOptions("SizeId");
		DWRUtil.addOptions("SizeId", r, [getValue, getText], null);
	}
	
	function resultColourImages(r) {
		var strColourThumbs = "";
		var productImage = formatURL(r.image[0]);
		var objImage = document.getElementById("ProductImage");
		objImage.src = productImage;
	
		// Update thumbnails
		for(var i=0;i<r.thumbnail.length;i++)
			strColourThumbs = strColourThumbs + '<a href="javascript:swapProductImage(\''+formatURL(r.image[i])+'\')"><img src="'+formatURL(r.thumbnail[i])+'" alt="'+r.name[i]+'" /></a>';
		document.getElementById("ColourThumb").innerHTML = strColourThumbs;
	}
}

///////////////////////////////
//          Shipping         //
///////////////////////////////

function getStates(fld,addressType){
	var countryId = document.getElementById(fld).value;
	DWREngine.setAsync(false);
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getStates', countryId, resultStates);
	
	function resultStates(r) {
		
		StateName = document.getElementById(addressType+'State');
		StateId = document.getElementById(addressType+'StateId');
		
		if(r.getRowCount() > 0){
			StateName.value = "";
			StateName.style.display = "none";
			StateId.style.display = "";
			var getText = function (thisRow) { return thisRow.name; }
			var getValue = function (thisRow) { return thisRow.id; }
			DWRUtil.removeAllOptions(StateId);
			DWRUtil.addOptions(StateId, [{ name:'Please Select', value:'0' }], "value", "name"); 
			DWRUtil.addOptions(StateId, r, [getValue, getText], null);
		}
		else {
			DWRUtil.removeAllOptions(StateId);
			StateName.value = "";
			StateName.style.display = "";
			StateId.style.display = "none";
		}
	}
}

//////////////////////////////////
//    Design Submit and Vote    //
//////////////////////////////////

function setVote(islogin,competitionid,designid,rating,ipaddress){
	
	if(!islogin){
		alert("You must log in to vote");
	}
	else {
		var design = designid
		var width = (rating * 15)+"px" ;
		document.getElementById('vote_'+design).style.width = width;
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'insertVote', {"competitionid":competitionid, "rating":rating, "designid":designid, "ipaddress":ipaddress}, voteResult);
	}
	
	function voteResult(r){
		if (r=='10')
			alert("You have already voted for this design");
		else
			alert("Thanks for giving this design " + r + " stars");
	}

}

function getDesignTemplates(designid){
	var id = designid;
	DWRUtil.useLoadingMessage(); 
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getTemplateImage', designid, ImageResult);
	
	function ImageResult(r) {
		var productImage = formatURL(r.image[0]);
		var productHiRes = "download.htm?id="+id;
		var objImage = document.getElementById("ProductImage");
		var objHiRes = document.getElementById("HiResImage");
		objImage.src = productImage;
		objHiRes.href = productHiRes;

	}
}

function getDYOTemplates(designid){
	var id = designid;
	DWRUtil.useLoadingMessage(); 
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getNewTemplateImage', designid, ImageResult);
	
	function ImageResult(r) {
		var productImage = formatURL(r.image[0]);
		var productHiRes = "download.htm?id="+id;
		var objImage = document.getElementById("ProductImage");
		var objHiRes = document.getElementById("HiResImage");
		objImage.src = productImage;
		objHiRes.href = productHiRes;

	}
}