function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
	  }catch (e){
		  // Internet Explorer
		  try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e){
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
	  }
	return xmlHttp;
}

/* function to get items based on selected radio button */
function getItems(str)
{
	/* if the online radio button is checked
	# then show online form
	*/
  	if (str == "online"){
		  document.getElementById('cd-items').style.display = 'none';
		  document.getElementById('online-items').style.display = 'block';	  
      }
	/* if the CD radio button is checked
	# then show CDs form
	*/
	if (str == "cd"){
		  document.getElementById('online-items').style.display = 'none';
		  document.getElementById('cd-items').style.display = 'block';
      }
}


/* function to get return the values to the a page */
function getDiscount(pid, coupon){
	
	if(coupon == "" || coupon == null){
		alert("Please enter voucher code!");
		document.cartvoucher.coupon.focus;
	}
	if(pid == "" || pid == null){
		alert("Please add products to your basket!");
	}else{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }else{
		// if Ajax is supported, then send values to online-subscription.php via get method
		var url = "jcart/validate-code.php?pid="+pid+"&coupon="+coupon+"&sid=" +Math.random();
		xmlHttp.onreadystatechange=function(){
			if (xmlHttp.readyState==4)
			{ 
				document.cartvoucher.coupon.value = coupon;
				document.order.coupon.value = coupon;
				document.cartform.coupon.value = coupon;
				document.getElementById("showcode").innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	  }
	} // eof else if coupon is not empty
}

/* function to return values to the online-subscription.php page via get method */
function purchaseOnlineSubscription(nstudents,length,vchCode){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }else{
		// if Ajax is supported, then send values to online-subscription.php via get method
		var url = "online-subscription.php?nstudents="+nstudents+"&length="+length+"&vchcode="+vchCode+"&sid" +Math.random();
		xmlHttp.onreadystatechange=function(){
			if (xmlHttp.readyState==4)
			{ 
				document.online.vcode.value = vchCode;
				document.getElementById("online-sub").innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		
	  }
} // eof function

/* function to return values to the online-subscription.php page via get method */
function validateCode(coupon, productID, price, msg){
	if(coupon == "" || coupon == null){
		alert("Please enter voucher code!");
		document.voucher.coupon.focus;
	}else{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }else{
		// if Ajax is supported, then send values to online-subscription.php via get method
		var url = "validate-voucher.php?coupon="+coupon+"&productID="+productID+"&price="+price+"&msg="+msg+"&sid=" +Math.random();
		xmlHttp.onreadystatechange=function(){
			if (xmlHttp.readyState==4)
			{ 
				document.online.vcode.value = coupon;
				document.voucher.coupon.value = coupon;
				document.checkout.coupon.value = coupon;
				document.getElementById("showResponse").innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	  }
	} //eof else
} // eof function

function checkout(pid, coupon){
	if(pid == "" || pid === null){
		alert("You do not have products in your basket.");	
	}
	alert(pid+""+coupon);
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }else{
		// if Ajax is supported, then send values to online-subscription.php via get method
		var url = "order.php?processorder=1&product_id="+pid+"&coupon="+coupon+"&sid=" +Math.random();
		alert(url);
		xmlHttp.onreadystatechange=function(){
			if (xmlHttp.readyState==4)
			{
				document.order.coupon.value = coupon;
				document.getElementById("orderpage").innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	  }

}
/**
*
* this function is used to stope the from from submision 
* if the enter key is pressed if voucher code is entered.
* @param e: is the event
* @param pid: is the product id
 @param code: is the voucher code
*
* @return the key is not entere to stop form submission
*
*/
function disableEnterKey(e,code,pid,price,msg)
{
     var key;     
     if(window.event){
		 key = window.event.keyCode; //IE browser
		 // if enter key is pressed, then call validateCode function declared inside the getInfo.js file
		 if(key == 13){
			validateCode(code,pid,price,msg); 
		 }
	 }else{
          key = e.which; //firefox browser
		  if(key == 13){
			// if enter key is pressed, then call validateCode function declared inside the getInfo.js file
			validateCode(code,pid,price,msg); 
		}
	 }
     return (key != 13);
}

/**
*
* this function is used to stope the from from submision 
* if the enter key is pressed if voucher code is entered.
* @param e: is the event
* @param pid: is the product id
* @param code: is the voucher code by calling getDiscount function declared in the getInfo.js file
*
* @return the key is not entere to stop form submission
*
*/
function enterKeyPressed(e,pid,code)
{
     var key;     
     if(window.event){
		 key = window.event.keyCode; //IE
		 if(key == 13){
			getDiscount(pid,code);
		}
	 }else{
        key = e.which; //firefox 
		if(key == 13){
			getDiscount(pid,code);
		}
	 }
     return (key != 13);
}
function showHidDiv(id){
	if(document.getElementById(id).style.display == "block"){
		document.getElementById(id).style.display = "none";
		document.getElementById('cd-items').style.height = "auto";	
	}
	else if(document.getElementById(id).style.display == "none"){
		document.getElementById(id).style.display = "block";
		document.getElementById('cd-items').style.height = "auto";	
	}else{
		//document.getElementById(id).style.height = "180px";	
	}
}
