var xmlHttp;
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 stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 { 
	 document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
	 } else {
	 document.getElementById("txtHint").innerHTML="<strong><em>Retrieving Requests... </strong></em><img src=/images/wait.gif alt=Processing>";
	 } 
	 
}
function lookupStage() {
	$.get("/js/reg/stage.php", {id: "test" },
		function(data) {
			if(data == "new") {
				loadLookup();
				
			} else if(data == "userdata") {
				userLookup();
			} else if(data == "confoptions") {
				confOptions();
			}
		}  
	);
}
function loadLookup()
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	var url="/js/reg/lookup.php";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function lookupSubmit() {
	$.post("/js/reg/lookup_submit.php", $("#lookup").serialize(),
		function(data) {
			var error = data.substr(0,4);
			var errorval = "fail";
			if(errorval == error) {
				alert("Unable to process request.  \n"+data.slice(4,data.length));
				return false;
			} else {
				userLookup(data);
			}
		}  
	);
}
function userdataSubmit() {
	$.post("/js/reg/userdata_submit.php", $("#userdata").serialize(),
		function(data) {
			var error = data.substr(0,4);
			var errorval = "fail";
			
			if(errorval== error) {
				alert(data.slice(4,data.length));
				return false;
			} else {
				confOptions();
			}
		}  
	);
}
function confOptions(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	var url="/js/reg/confoptions.php";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function userLookup()
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	var url="/js/reg/userdata.php";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function memberFee()
{
	var member = getCheckedValue(document.payment.membership);
	var nonmember = document.payment.nonmember.value;
	if(nonmember == "yes" && member == 0) {
		document.payment.nonmember_fee.checked=true;
		calculateTotal();
	} else {
		document.payment.nonmember_fee.checked=false;
		calculateTotal();
	}
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function calculateTotal() {
	var total = 0;
	var membership = getCheckedValue(document.payment.membership);
	if(membership == 1) { 
		membership = 30; 
	} else if(membership == 2) {
		membership = 50;
	} else {membership = 0; }
	total +=  parseFloat(membership);
	total += parseFloat(document.payment.conf.value);
	if(document.payment.meac.checked) { total +=  parseFloat(document.payment.meac.value); }
	if(document.payment.acnm.checked) {total +=  parseFloat(document.payment.acnm.value); }
	if(document.payment.nonmember_fee.checked) {total +=  parseFloat(document.payment.nonmember_fee.value); }
	if(document.payment.addnight.checked) {total += parseFloat(document.payment.addnight.value); }
	document.payment.totalcost.value = pad_with_zeros(total,2);
}
function pad_with_zeros(rounded_value, decimal_places) {
    // Convert the number to a string
    var value_string = rounded_value.toString()
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")
    // Is there a decimal point?
    if (decimal_location == -1) {
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
function confOptionsSubmit() {
	$.post("/js/reg/confoptions_submit.php", $("#payment").serialize(),
		function(data) {
			var error = data.substr(0,4);
			var errorval = "fail";
			if(errorval== error) {
				alert(data.slice(4,data.length));
				return false;
			} else if(data == "paypal") {
				loadPaypal();
			} else if(data == "check") {
				loadCheck();
			}
		}  
	);
}
function loadCheck() {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	var url="/js/reg/check.php";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function loadPaypal() {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request");
	 return;
	 }
	var url="/js/reg/paypal.php";
	url=url+"?sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}