var progress = 0;

function download_img(size,imageid,userid,receipt){
	if(document.getElementById("agree").checked){
		msg = '<br/><br/><a href="/search/image/'+imageid+'"><img border="0" src="/images/button_back.jpg" alt="back"></a>';
		msg += '<h2>Processing your  request.... ,<br/>please wait for the download box</h2>';

		setTimeout('document.getElementById("download_buttons").innerHTML = msg',300);
		document.body.style.cursor = "wait";

		window.location = '/buy_image/'+imageid+'/'+size;

		progress = setInterval ("download_done("+imageid+",'"+size+"',"+userid+",'"+receipt+"')", 1000 );

	}else
		alert('You have to confirm the terms first!');
}

function downloadCart(size,imageid,userid){
	id = size.substr(0,1)+imageid;

	if(document.getElementById("agree").checked){
		msg = '<img src="/images/button_nodownload.jpg" alt="no download" />';

		window.location = '/buy_image/'+imageid+'/'+size;
		delfromcart(id);
		setTimeout('document.getElementById("dl"+id).innerHTML = msg',500);
	}else
		alert('You have to confirm the terms first!');
}

function download_again(imageid,size){
	window.location = '/buy_image/'+imageid+'/'+size;
}

function download_done(imageid,size,userid,receipt){
	var html = $.ajax({
			type: "GET" ,
  			url: "/ajax/download_progress.php" ,
			data: "imageid="+imageid+"&size="+size+"&userid="+userid+"&receipt="+receipt ,
  			async: false
	}).responseText;

	if(html){
		clearInterval (progress);
		document.body.style.cursor='default';
		document.getElementById("download_buttons").innerHTML = html;
	}
}

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++)
      		if(theForm[z].type == 'checkbox' && theForm[z].name != 'buyall'){
	  		theForm[z].checked = theElement.checked;

			size = theForm[z].value.substr(0,1);
			imageid = theForm[z].value.substr(1);

			if(theElement.checked)
				changeItem(imageid,size,'yes');
			else
				changeItem(imageid,size,'no');
		}
}

function changeBuy(obj){
	size = obj.value.substr(0,1);
	imageid = obj.value.substr(1);

	if(obj.checked)
		changeItem(imageid,size,'yes');
	else
		changeItem(imageid,size,'no');
}

function delfromcart(image){
	size = image.substr(0,1);
	imageid = image.substr(1);

	$.ajax({
		type: "GET" ,
  		url: "/ajax/add_cart.php" ,
		data: "imageid="+imageid+"&size="+size+"&del=del" ,
  		success: function(msg){
			if(msg){
				document.getElementById("my_shopping_cart").style.display = 'block';
			}else{
				document.getElementById("my_shopping_cart").style.display = 'none';

				if(document.getElementById("cart_receipt"))
					document.getElementById("cart_receipt").style.display = 'block';
			}

     			document.getElementById("my_shopping_cart").innerHTML =  msg;

			if(document.getElementById(image)){
				deleted = '<span style="color:red;font-size:12px;">this item is removed from your cart</span>';
				document.getElementById(image).innerHTML = deleted;
			}
			getTotal();
   		}
	});
}

function changeItem(imageid,size,buy){
	$.ajax({
		type: "GET" ,
  		url: "/ajax/add_cart.php" ,
		data: "imageid="+imageid+"&size="+size+"&buy="+buy ,
  		success: function(msg){
			document.getElementById("my_shopping_cart").style.display = 'block';
     			document.getElementById("my_shopping_cart").innerHTML =  msg;
			getTotal();
   		}
	});
}

function getTotal(){
	if(document.getElementById("total_with_tax"))
		$.ajax({
			type: "GET" ,
  			url: "/ajax/add_cart.php" ,
			data: "total=total" ,
  			success: function(msg){
				tax = msg*(VAT/100);
				if(document.getElementById("total"))
     					document.getElementById("total").innerHTML =  '$'+msg;

				if(document.getElementById("taxamount"))
					document.getElementById("taxamount").innerHTML =  '$'+tax;

				if(document.getElementById("total_with_tax") && document.getElementById("total"))
					document.getElementById("total_with_tax").innerHTML =  '$'+(tax+(+msg));
				else
					document.getElementById("total_with_tax").innerHTML = '$'+msg;

			}
		});
}
