function getLoader(){
	
	var loadingHTML = '<div class="Loading">Loading...</div>';
	return loadingHTML;

}

function getStates(){
	
	$('BillingStateSelector').update('<label><span class="Req">*</span>State/Province</label>' + getLoader());
	ajax("getStates", { countryid:$('BillingCountryId').value }, resultStates);
	
}

function resultStates(r){
	
	$('BillingStateSelector').update(r.html);
	
}

function getShippingStates(){
	
	$('DeliveryStateSelector').update('<label><span class="Req">*</span>State/Province</label>' + getLoader());
	ajax("getShippingStates", { countryid:$('DeliveryCountryId').value }, resultShippingStates);
	
}

function resultShippingStates(r){
	
	$('DeliveryStateSelector').update(r.html);
	
}

function getProductColour(productId,colourId){
	
	$('ProductImage' + productId).update('');
	ajax("getProductColour", { productId:productId, colourId:colourId }, resultProductColour);
	
}

function resultProductColour(r){

	var productId = r.productId;
	var productImageTemplate = new Template('<a href="#{large}" class="lightwindow" title="#{imagename}"><img src="#{productImage}" alt="#{imagename}" /></a>');
	$('ColourId' + productId).value = r.colourId;
	$('ProductImage' + productId).update(productImageTemplate.evaluate({ productImage:r.objImages[0][2], large:r.objImages[0][3] }));
	$('Size' + productId).update(r.sizeOptions);
	
	if(r.styleNumber.length){
		$('StyleNumber' + productId).update(r.styleNumber);
	}
	
	thumbnailViewsString = "";
	
	if(r.objImages.length > 1){
		
		// Loop over images array
		for(i=0; i<r.objImages.length; i++){
			imagename = r.objImages[i][0];
			thumbnail = r.objImages[i][1];
			image = r.objImages[i][2];
			large = r.objImages[i][3];
			className = 'Left';
			
			if((i % 2)){
				className = '';
			}
			
			// Add to string
			thumbnailString = '<div class=" ' + className + '"><a href="javascript:changeImage(\'ProductImage' + productId + '\',\'' + imagename +'\',\'' + image + '\',\'' + large + '\');"><img src="' + thumbnail + '" alt="' + imagename +  '" style="width:86px;"></a></div>';
			thumbnailViewsString = thumbnailViewsString + thumbnailString;
	
		}
	
		// Update Container
		thumbnailViewsString = thumbnailViewsString + '<div class="Clear"></div>';
		
	}
	
	$('ProductImageViews').update(thumbnailViewsString);
	
	
}

function addToCart(productId){
	
	var colourId = 0;
	var sizeId = 0;
	if($('ColourId' + productId)) colourId = $F('ColourId' + productId);
	if($('SizeId' + productId)) sizeId = $F('SizeId' + productId);
	$('AddToCart').hide();
	$('Loading').show();
	ajax("addToCart", { productid:productId, colourid:colourId, sizeId:sizeId }, resultAddtoCart);
	
}

function resultAddtoCart(r){

	$('Loading').hide();
	$('AddToCart').show();

	if(r.isStock){
		if(r.basketTotalQty > 0) {
			
			strBasketTotalQty = r.basketTotalQty;
	
			$('BasketTotalQty').update(strBasketTotalQty);

		}
	}
	else {
		
		alert('Out of stock.');	
	}
	
}

function setIsDeliverySame(obj){
	
	if(obj.checked){
		$('DeliveryCountryId').value = $('BillingCountryId').value;
		$('DeliveryStateSelector').update('<label><span class="Req">*</span>State/Province</label>' + getLoader());
		ajax("getShippingStates", { countryid:$('DeliveryCountryId').value }, resultSetIsDeliverySame);
	}
	
}

function resultSetIsDeliverySame(r){
	
	$('DeliveryStateSelector').update(r.html);
	
	$('DeliveryFirstName').value = $('FirstName').value;
	$('DeliverySurname').value = $('Surname').value;
	$('DeliveryEmail').value = $('Email').value;
	
	objEls = new Array();
	objEls = ["State","StateId","Postcode","Suburb","Address1","Address2","Phone"];
	
	for(i=0; i<objEls.length; i++){
		if($('Delivery'+objEls[i]))
			$('Delivery'+objEls[i]).value = $('Billing'+objEls[i]).value;
	}
	
}

//////////////////////////////////
//    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;
		ajax("insertVote", { competitionid:competitionid, rating:rating, designid:designid, ipaddress:ipaddress}, getVoteResult);
	}
}

function getVoteResult(r){
	if (r.rating =='10')
		alert("You have already voted for this design");
	else
		alert("Thanks for giving this design " + r.rating + " stars");
}

function getDesignTemplates(designid){
	var id = designid;
	ajax("getTemplateImage", { designid:designid }, getDesignTemplatesResult);
}

function getDesignTemplatesResult(r) {
	var productImage = r.image;
	var productHiRes = "download.htm?id="+r.id;
	var objImage = document.getElementById("ProductImage");
	var objHiRes = document.getElementById("HiResImage");
	objImage.src = productImage;
	objHiRes.href = productHiRes;

}

function getDYOTemplates(designid){
	var id = designid;
	ajax("getNewTemplateImage", { designid:id }, getDYOTemplateResult);
}

function getDYOTemplateResult(r) {
	var productImage = r.image;
	var productHiRes = "download.htm?id="+r.id;
	var objImage = document.getElementById("ProductImage");
	var objHiRes = document.getElementById("HiResImage");
	objImage.src = productImage;
	objHiRes.href = productHiRes;
}

function getFlickrPhotos(directionType){
	currentPageNo = parseInt($('pageNo').value);
	newPageNo = currentPageNo - 1;
	maxPageNo = parseInt($('pageMax').value);
	
	if(directionType == "next"){
		newPageNo = newPageNo + 1;
		currentPageNo = currentPageNo + 1;
	}
	else if(directionType == "back"){
		newPageNo = newPageNo - 1;
		currentPageNo = currentPageNo - 1;
	}

	if(newPageNo >= 0 & currentPageNo <= maxPageNo){
		$('bigLoader').setStyle({display: 'block'});
		$('flickrPhotos').update("");
		
		ajax("getFlickrPhotos", { pageNo:newPageNo }, resultFlickrPhotos);
		
		$('pageNo').value = currentPageNo;
	}
}

function resultFlickrPhotos(r){
	$('pageMax').value = r.pageMax;
	$('bigLoader').setStyle({display: 'none'});
	$('flickrPhotos').update(r.html);
}
