$(window).load(function () {
 var aid = $("#hidden_aid").val();
 checkexistpromo(aid);
/*	var aid = $("#hidden_aid").val();	
	$.ajax({
		type: "POST",
		url: "adm/ajax/deleteexpiredate.php",
		data: "aid="+aid,
		//dataType: "html",
		success: function(msg){
			setTimeout("checkexistpromo("+aid+")",200);
		}
	});*/
});

function checkexistpromo(aid){
		$.ajax({
		type: "POST",
		url: "/adm/ajax/checkissetpromo.php",
		data: "aid="+aid,
		//dataType: "html",
		success: function(msg){
				var reg=/show/
				result = reg.test(msg);
				if(result){
					$("#waiting_for_promo").fadeIn('slow');
					setTimeout('$("#waiting_for_promo").fadeOut("slow")',4000);
					setTimeout('$("#promocode_form").fadeIn("slow")',4500);
				}else{
					$("#FormName").removeAttr("onsubmit");
				}
			}
		});
}


var val = false;
function checkpromo(aid){
var promocode = $("#promocode").val();
	if(promocode!=''){
		$.ajax({
			type: "POST",
			url: "/adm/ajax/promocode.php",
			data: "promocode="+promocode+"&aid="+aid,
			//dataType: "html",
			success: function(msg){
				var reg=/Accepted/
					result = reg.test(msg);
					if(result){
						$("#hidden_promo").val(promocode);
						$("#FormName").removeAttr("onsubmit");
						$("#FormName").submit();
					}else{
						$("#promocode_error").html(msg);
						$("#promocode_error_layer").fadeIn("slow");
						setTimeout('$("#promocode_error_layer").fadeOut("slow")',3000);
						val = false;
					}
				}
			});
	}else{
			$("#promocode_error").html('Purchase code is required.');
			$("#promocode_error_layer").fadeIn("slow");
			setTimeout('$("#promocode_error_layer").fadeOut("slow")',3000);
			return false;
	}
}

function Buy(aid){
	checkpromo(aid);
	return val;
}