// JavaScript Document


function setOutput(){
	if(httpObject.readyState == 4){
		
		if(displaySwitch==2) {
			
			if(httpObject.responseText=="2"){
				alert("Invalid Promo Code!");
			} else if (httpObject.responseText=="3") {
				alert("Promotion not started yet!");
			} else if (httpObject.responseText=="4") {
				alert("Expired Promotion Code!");
			} else if (httpObject.responseText=="5") {
				alert("Promotion is no longer available!");
			} else {
				proccessScreen ('processing_box');
				setTimeout( "$('#processing_box').slideUp(400);grayOut(false);", 2000); 
				document.getElementById('cart_item_list').innerHTML = httpObject.responseText;
			}
			
		} else {
			document.getElementById('cart_item_list').innerHTML = httpObject.responseText;
		}
		
		if(displaySwitch == -1){
			if(document.getElementById(layerstatus)) {
				if(eSN==""){
					document.getElementById(layerstatus).innerHTML = "&nbsp;";
					$('#'+layerstatus).slideUp(300);
				} else {
					document.getElementById(layerstatus).style.display="none";
					if(document.getElementById(layerName)){
						document.getElementById(layerName).disabled=false;
					}
					document.getElementById(layerstatus).innerHTML = "<ul class=\"btn_action\" style=\"width:100px;margin:0px auto;\"><li class=\"btn_add_to_cart\" style=\"margin:0px auto;\"><a class=\"ajax_add_to_cart_button\" id=\""+origepid+eSN+"_enddateURL\" href=\"/estore/ajax_cart.php?pid="+origepid+"&quantity="+equantity+"&sn="+eSN+"\">Add to cart</a></li></ul>";
					
					$('#'+layerstatus).slideDown(300);
				}
			}
		} else if(displaySwitch == 2){
			//alert(httpObject.responseText);
		}else {
			document.getElementById(layerstatus).innerHTML = "Added to Cart";
			$('#'+layerslide).slideDown(400);
			if(!(eSN=="")){
				$('#'+layerstatus).slideDown(300);
				
			}
		}
		httpObject = null;
	}
	ajaxCart.overrideButtonsInThePage();
} 
	
function eCart(eURL){

	epid = eURL.substring(eURL.indexOf("pid=")+4,eURL.indexOf("&quantity"));
	eSN="";
	
	if(eURL.search("&shipOption=")!=-1){
		alert("If you plan to purchase other AirMagnet \"software\" products (such as WiFi Analyzer Express or Survey Express), \"support renewals\" or \"web-based training\", you have two choices:\n\n* Include them with your current order that includes hardware, and you will receive the shipped version (software on CD) or a support renewal certificate or a web-based training voucher and is subject to taxes (software products only) and normal processing time.\n\n* Create a new separate order for software products, support renewals or web-based training, and you will receive the licenses/vouchers via e-mail immediately and tax free.\n\nThank you.");
	}
	
	if(eURL.search("&sn=")!=-1){
		eSN = eURL.substring(eURL.indexOf("sn=")+3,eURL.length);
		origepid = epid;
		epid = epid+eSN;
		layerName = epid+"_menu";
		if(document.getElementById(layerName)){
			equantity = document.getElementById(layerName).value;
		} else {
			equantity = 1;	
		}
	}
	
	if(epid.search("~")){
		epid = epid.replace("~","");
	}
	
	if(eURL.search("origPid=")!=-1){
		layerstatus = eURL.substring(eURL.indexOf("origPid=")+8,eURL.indexOf("&pid="))+"_status";
		layerslide = epid+"_slide";
	} else {
		layerstatus = epid+"_status";
		layerslide = epid+"_slide";
	}
	
	if(eURL.lastIndexOf("quantity=-1") == -1) {
		document.getElementById(layerstatus).innerHTML = "<em style=\"color:#333333;\">Adding to cart...</em>";
		$('#'+layerstatus).slideDown(300);
	}
	
	if(eURL.lastIndexOf("quantity=-1") == -1) {
		displaySwitch = 1;
		if(!(eSN=="")){
			$('#'+layerstatus).slideUp(300);
			if(document.getElementById(layerName)){
				document.getElementById(layerName).disabled=true;
			}
		}
	} else {
		displaySwitch = -1;
		$('#'+layerslide).slideUp(400);
	}
	
	
	var myNewString = eURL.replace("/estore/ajax_cart.php?", "")+"&a=1";
	posturl = myNewString+"&"+Date()+"&"+"a=1";
	
	//$.ajax({
   	//	type: "GET",
   	//	url: "/estore/ajax_cart.php",
    //	data: myNewString,
   	//	success: function(msg){
	//		document.getElementById('cart_item_list').innerHTML = msg;
	//		if(displaySwitch == -1){
	//			if(document.getElementById(layerstatus)) {
	//				document.getElementById(layerstatus).innerHTML = "&nbsp;";
	//				$('#'+layerstatus).slideUp(300);
	//			}
	//		} else {
	//			document.getElementById(layerstatus).innerHTML = "Added to Cart";
	//			$('#'+layerslide).slideDown(400);
	//		}
	//		ajaxCart.overrideButtonsInThePage();
   	//	}
 	//});


	
	httpObject = getHTTPObject();
	if (httpObject != null) {
		posturl = eURL+"&"+Date()+"&"+"a=1";
		httpObject.open("GET", posturl, true);
		httpObject.onreadystatechange = setOutput;
		httpObject.send(null);
	}
	
}

function renewalYearSelect(esProdID,newQuantity,newEndDate1,newEndDate2,newEndDatePrice1,newEndDatePrice2,prodSN) {
	
	layerEndDate = esProdID+prodSN+"_enddate";
	layerEndDatePrice = esProdID+prodSN+"_enddatePrice";
	layerEndDateURL = esProdID+prodSN+"_enddateURL";
	
	if(newQuantity == 1) {
		document.getElementById(layerEndDate).innerHTML = newEndDate1;
		document.getElementById(layerEndDatePrice).innerHTML = newEndDatePrice1;
		document.getElementById(layerEndDateURL).href = "/estore/ajax_cart.php?pid="+esProdID+"&quantity=1&sn="+prodSN;
	} else {
		document.getElementById(layerEndDate).innerHTML = newEndDate2;
		document.getElementById(layerEndDatePrice).innerHTML = newEndDatePrice2;
		document.getElementById(layerEndDateURL).href = "/estore/ajax_cart.php?pid="+esProdID+"&quantity=2&sn="+prodSN;
	}
	
}

function addSupport(newQuantity,esProdID0,supportYears0,newPrice0,esProdID1,supportYears1,newPrice1,esProdID2,supportYears2,newPrice2,shipOption) {
	layerPrice = esProdID0+"_Price";
	layerURL = esProdID0+"_URL";
	
	shipAppend="";
	if(shipOption == 1) shipAppend = "&shipOption=1";
	
	if(newQuantity == 0) {
		document.getElementById(layerPrice).innerHTML = newPrice0;
		document.getElementById(layerURL).href = "/estore/ajax_cart.php?pid="+esProdID0+"&quantity=1"+shipAppend;
	} else if(newQuantity == 1) {
		document.getElementById(layerPrice).innerHTML = newPrice1;
		document.getElementById(layerURL).href = "/estore/ajax_cart.php?origPid="+esProdID0+"&pid="+esProdID1+"&quantity=1"+shipAppend;
	} else if(newQuantity == 2) {
		document.getElementById(layerPrice).innerHTML = newPrice2;
		document.getElementById(layerURL).href = "/estore/ajax_cart.php?origPid="+esProdID0+"&pid="+esProdID2+"&quantity=1"+shipAppend;
	}
	
}

function addPromo(pCode) {
	if(pCode == ""){
		alert("You must enter a promotion code");
		document.promoCode.pCode.focus();
	} else {
		displaySwitch = 2;
		httpObject = getHTTPObject();
		if (httpObject != null) {
			posturl = "/estore/promo_code.php?pCode="+pCode+"&c=1&"+Date()+"&"+"a=1";
			httpObject.open("GET", posturl, true);
			httpObject.onreadystatechange = setOutput;
			httpObject.send(null);
		}
	}
}
	
	
var ajaxCart = {
	overrideButtonsInThePage : function(){
		$('.ajax_add_to_cart_button').unbind('click').click(function(){
			eCart($(this).attr('href'));
			return false;
		});
		
		$('.ajax_cart_block_remove_link').unbind('click').click(function(){
			eCart($(this).attr('href'));
			return false;
		});
		
		$('#promoCode').unbind('submit').submit(function(){
			addPromo(document.promoCode.pCode.value);
			return false;
		});
	}
}


$(document).ready(function(){
	ajaxCart.overrideButtonsInThePage();
});



