// threadsafe asynchronous XMLHTTPRequest code
function ajaxSend(url, divid, ref, cmd){

	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
		
	function ajaxBindCallback(){
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				if (ref != null)
					document.location.reload(true);
				else if (divid != null)
				{
					document.getElementById(divid).innerHTML = ajaxRequest.responseText;
					
					if(cmd != null)
						eval(cmd);
				}				
			}
		}
	}

	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	
	//if (divid != null)
	//	document.getElementById(divid).innerHTML = '<img src="/library/images/general/ajax-loader.gif">';
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}

function startNomination(url, divid, catid){
	var ajaxurl = url + 'ajax.cfc?method=getNominationForm&catid=' + catid;
	ajaxSend(ajaxurl, divid, null, null);
}

// Do a dynamic lookup on the nominator email, skip stage if found
function norLookup(url, divid, catid){
	var email = document.getElementById('email').value;
	
	var ajaxurl = url + 'ajax.cfc?method=norLookup&email=' + email + '&catID=' + catid;
	
	ajaxSend(ajaxurl, divid);
}

function isNomPresent(url, divid, catid){
	
	var email = document.getElementById('email').value;
	
	var ajaxurl = url + 'ajax.cfc?method=isNomPresent&email=' + email + '&catID=' + catid;
	
	ajaxSend(ajaxurl, divid);
}



// Save a nominator record and proceed to nomination
function saveNominator(url, divid, catid){
	
	var ajaxurl = url + 'ajax.cfc?method=saveNominator&catid=' + catid
		+ '&nor_email=' + document.getElementById('nor_email').value
		+ '&nor_name=' + document.getElementById('nor_name').value
		+ '&nor_title=' + document.getElementById('nor_title').value
		+ '&nor_company=' + document.getElementById('nor_company').value
		+ '&nor_add1=' + document.getElementById('nor_add1').value
		+ '&nor_add2=' + document.getElementById('nor_add2').value
		+ '&nor_add3=' + document.getElementById('nor_add3').value
		+ '&nor_town=' + document.getElementById('nor_town').value
		+ '&nor_county=' + document.getElementById('nor_county').value
		+ '&nor_postcode=' + document.getElementById('nor_postcode').value
		+ '&nor_country=' + document.getElementById('nor_country').value
		+ '&nor_phone=' + document.getElementById('nor_phone').value
		+ '&nor_fax=' + document.getElementById('nor_fax').value;
		
		ajaxSend(ajaxurl, divid);
}
