var xmlhttp=false;

function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
//    document.getElementById("SimonMsg").innerHTML =  "1. ajaxRequest failed. ";
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else {
//	 document.getElementById("SimonMsg").innerHTML =  "2. ajaxRequest failed. ";
  	return false;
 }
}

function CheckCode() {
	var promotion_code = "";
	var codeSent = "";
	var eventID = "";
	PromotionMsg = ""; // used in UKResults.js
	var frm = window.document.forms[0];
	
	var obj = window.document.forms[0].raceid;
	
	for (var m=0;  m < obj.length ; m++) {
		if (obj[m].checked) {
			var Race = obj[m].value.toLowerCase();
			if (Race.indexOf("coupon") == -1) {
				if (valid()) document.forms[0].submit(); // not selected the coupon discount race, so no need to check for coupon 
				return;
			} else break;
		}
	}  	//Closes the for Loop

	obj = document.getElementById("PromotionCode");
	if (obj) {
		promotion_code = trim(obj.value);
		if (promotion_code != "") promotion_code = encodeURIComponent(promotion_code);
	}
	obj = document.getElementById("EventID");
	if (obj) {
		eventID = trim(obj.value);
		if (eventID != "") eventID = encodeURIComponent(eventID);
	}
	if (promotion_code != "" && eventID != "") {
		var strURL = '../php/checkCode.php?EventID='+eventID+'&Code='+promotion_code + '&Random=' + Math.random(); // need random to prevent caching..
		xmlhttp=false;
		xmlhttp= new ajaxRequest();
		xmlhttp.open("GET", strURL, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				var returnTxt = xmlhttp.responseText;
				if (returnTxt == "Okay") {
					if (valid()) document.forms[0].submit();
				} else {
						PromotionMsg = ". Please enter a valid coupon code.\n"; 
						valid();
 				}
			}
		}
		xmlhttp.setRequestHeader('If-Modified-Since', 'Fri, 31 Dec 1999 00:00:00 GMT'); 
		xmlhttp.setRequestHeader('Cache-Control', 'no-cache');
		xmlhttp.send(null);
	} else {
		PromotionMsg = ". Please enter a valid coupon code (null value).";
		valid();
	}
}

