// Promo code javascript for main registration form
function codeCheck(){
	
	// make sure 'Submit more' button is visible
	document.getElementById("buttons_standard").style.display = 'block';
	document.getElementById("buttons_submit_only").style.display = 'none';
					
	// check that a package has been selected
	
	if (radioSetCheck("delegate_package")){
		document.getElementById("error_package").style.display = 'none';		
	}else{
		document.getElementById("error_package").style.display = 'block';
		return;		
	}
	
	// get code from input box
	var promo_code = document.getElementById("promo_code").value.toUpperCase().replace(/^\s+|\s+$/g, '');

	if (promo_code != ''){
		
		// display 'Checking...' text and hide button
		document.getElementById("check_button").style.display = 'none';
		document.getElementById("checking_text").style.display = 'inline';
		
		// ajax request
		var url = "scripts/codecheck_main.aspx";
		request.open("POST", url, true);
		request.onreadystatechange = function(){codeAction(promo_code);};
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		
		var sendString = "code=" + promo_code;
		request.send(sendString);
	}
	
}

var xmldoc;
var element;

function codeAction(promo_code){
	if (request.readyState == 4){
		if (request.status == 200) {
			xmldoc = request.responseXML;
			element = xmldoc.documentElement;
			var discount = element.firstChild.firstChild.nodeValue;		
		
			// hide any remaining info messages
			document.getElementById("fee_paragraph").style.display = 'none';
			document.getElementById("error_paragraph").style.display = 'none';
			
			if (discount != 'NONE'){
				// display registration fee info
				var fee_holder = document.getElementById("fee_holder");
				while(fee_holder.hasChildNodes()){
					fee_holder.removeChild(fee_holder.lastChild);
				}
				
				// populate hidden 'promotional_code' field with entered code
				document.getElementById("promotional_code").value = promo_code;
					
				if ((discount == 'FREE')||(discount == 'SPEAKER')){
					var fee_text = document.createTextNode("FREE");
					reduced_rate = 0;
					document.getElementById("amount").value = reduced_rate;
					// hide 'Submit more' button
					document.getElementById("buttons_standard").style.display = 'none';
					document.getElementById("buttons_submit_only").style.display = 'block';
				}else if (discount == 'DISCOUNT'){
					reduced_rate = 690;
					var fee_text = document.createTextNode("\u00A3690.00");					
					document.getElementById("amount").value = reduced_rate;
					// hide 'Submit more' button
					document.getElementById("buttons_standard").style.display = 'none';
					document.getElementById("buttons_submit_only").style.display = 'block';
				}
				fee_holder.appendChild(fee_text);
				document.getElementById("fee_paragraph").style.display = 'block';
			}else{				
				document.getElementById("error_paragraph").style.display = 'block';
			}
			
			// hide 'Checking...' text and show button
			document.getElementById("check_button").style.display = 'inline';
			document.getElementById("checking_text").style.display = 'none';
		}
	}
}
