// JavaScript Document

//Get XmlHttp Object
function GetXmlHttpObject() {	
	try {
		// IE7+, Firefox, Chrome, Opera 8.0+, Safari
		return new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return null;
}

//Initiate Age PHP Server query for Gender Change
function ajaxSetAge($gen) {	
	//If No Gender then Return
	if ($gen.length==0) {
		return;
  }
	
	//Get new XMLHTTP Object
	$xmlhttp=GetXmlHttpObject()
	if ($xmlhttp==null) {
		alert ("Your browser does not support XML HTTP Request");
		return;
  }
	
	//Setup PHP Call
	var $url="functions/setage.php";
	$url=$url+"?gen="+$gen;
	
	$xmlhttp.onreadystatechange=function() {
		actionAge($xmlhttp);
	}
	$xmlhttp.open("GET",$url,true);
	$xmlhttp.send(null);
}

//Load Age Groups on Page for Gender PHP Query
function actionAge($xmlhttp) {	
	if (($xmlhttp.readyState==4) && ($xmlhttp.status==200))	{
		//If there is something in the response
		if($xmlhttp.responseText.length > 0) {
			var $ageArray=eval($xmlhttp.responseText);
			var $tmpAge = '';
			//Gets current Age if Set
			if (window.document.getElementById('drpAge').disabled != true)
				$tmpAge = window.document.getElementById('drpAge').value;
		
			//Remove Old Elements from Array
			for(j=document.frmClass.age.options.length-1;j>=0;j--) {
				document.frmClass.age.remove(j);
			}
			
			//Load New Elements into Array
			for (i=0;i<$ageArray.length;i++) {
				var $ageVal = document.createElement("OPTION");
				$ageVal.text = $ageArray[i];
				$ageVal.value = $ageArray[i];
				
				//Sets Age Group if tmpAge set
				if ($tmpAge == $ageArray[i])
					$ageVal.selected = true;
				
				//Add compiled Element to Form
				document.frmClass.age.options.add($ageVal);
			}
			
			//Check for Disabled Select
			if (window.document.getElementById('drpAge').disabled == true)
				window.document.getElementById('drpAge').disabled = '';
		}
	}	
}

//Initiate PHP Server query for Outdoor Handicap Calculator
function ajaxCalc(){
	//Get new XMLHTTP Object
	$xmlhttp=GetXmlHttpObject()
	if ($xmlhttp==null) {
		alert ("Your browser does not support XML HTTP Request");
		return;
 	}
 
	//Setup PHP Call
	//Gender
	$gen = getRadioValue('radGender')
	if ($gen != -1)
		$gen = "?gen=" + $gen;
	else
		$gen = "?gen=";		
	//Age
	$age = "&age="+window.document.getElementById('drpAge').value;
	//Discipline
	$dis = getRadioValue('radDiscipline')
	if ($dis != -1)
		$dis = "&dis=" + $dis;
	else
		$dis = "&dis=";
	//Round
	$rnd = "&rnd="+window.document.getElementById('drpRound').value;
	//Score
	$scr = "&scr="+window.document.getElementById('txtScore').value;
	
	var $url="functions/class.php";
	$url=$url+$gen+$age+$dis+$rnd+$scr;
	
	$xmlhttp.onreadystatechange=function() {
		actionCal($xmlhttp);
	}
	$xmlhttp.open("GET",$url,true);
	$xmlhttp.send(null);	
}

//Load Age Groups on Page for Gender PHP Query
function actionCal($xmlhttp) {	
	if (($xmlhttp.readyState==4) && ($xmlhttp.status==200))	{
		//If All OK
		if ($xmlhttp.responseText.indexOf("Array")!=-1) {
			var $rsltArray=eval($xmlhttp.responseText);
			
			window.document.getElementById('calcerror').style.display="none";
			window.document.getElementById('txtClass').value=$rsltArray[0];
			window.document.getElementById('txtHandicap').value=$rsltArray[1];
			window.document.getElementById('imgPin').src=$rsltArray[2];
		}		
		//Else If Error Returned
		else if ($xmlhttp.responseText.indexOf("Error")!=-1) {
			var $rsltString=$xmlhttp.responseText.substr(6);

			window.document.getElementById('txtClass').value=""
			window.document.getElementById('txtHandicap').value=""
			window.document.getElementById('imgPin').src="/images/spacer.gif";			
			window.document.getElementById('calcerror').style.display="inline";			
			window.document.getElementById('calcerror').innerHTML=$rsltString;
		}
		//Else Another Error
		else {
			window.document.getElementById('txtClass').value=""
			window.document.getElementById('txtHandicap').value=""
			window.document.getElementById('imgPin').src="/images/spacer.gif";			
			window.document.getElementById('calcerror').style.display="inline";			
			window.document.getElementById('calcerror').innerHTML="<font color=\"#FF0000\"><strong>An error has occurred!</strong></font>";
		}
	}	
}

//Initiate Age PHP Server query for Gender Change
function ajaxSetFace($rnd) {	
	//If No Gender then Return
	if ($rnd.length==0) {
		return;
  }
	
	//Get new XMLHTTP Object
	$xmlhttp=GetXmlHttpObject()
	if ($xmlhttp==null) {
		alert ("Your browser does not support XML HTTP Request");
		return;
  }

	//Setup PHP Call
	var $url="functions/setface.php";
	$url=$url+"?rnd="+$rnd;

	$xmlhttp.onreadystatechange=function() {
		actionFace($xmlhttp);
	}
	$xmlhttp.open("GET",$url,true);
	$xmlhttp.send(null);
}

//Load Age Groups on Page for Gender PHP Query
function actionFace($xmlhttp) {	
	if (($xmlhttp.readyState==4) && ($xmlhttp.status==200))	{
		//If there is something in the response
		if (($xmlhttp.responseText.length > 0) && ($xmlhttp.responseText.indexOf("Valid")!=-1)) {
			var $rsltString=$xmlhttp.responseText.substr(6);
			var $collection=document.forms[0].elements['fce'];
			var $select=false;
			
			//Loop through All Radio buttons to Enable/Disable
			for ($i=0; $i < $collection.length; $i++) {
				if ($rsltString.search($collection[$i].value)!=-1) {
					$collection[$i].disabled = false; 
					if ($select==false) {
						$collection[$i].checked = true;
						$select=true;
					}
				}
				else
					$collection[$i].disabled = true;
			}
		}
	}	
}

//Initiate PHP Server query for Outdoor Handicap Calculator
function ajaxiCalc(){
	//Get new XMLHTTP Object
	$xmlhttp=GetXmlHttpObject()
	if ($xmlhttp==null) {
		alert ("Your browser does not support XML HTTP Request");
		return;
 	}
 
	//Setup PHP Call
	//Gender
	$gen = getRadioValue('radGender')
	if ($gen != -1)
		$gen = "?gen=" + $gen;
	else
		$gen = "?gen=";		

	//Discipline
	$dis = getRadioValue('radDiscipline')
	if ($dis != -1)
		$dis = "&dis=" + $dis;
	else
		$dis = "&dis=";
		
	
	//Face
	$fce = getRadioValue('radFace')
	if ($fce != -1)
		$fce = "&fce=" + $fce;
	else
		$fce = "&fce=";

	//Round
	$rnd = "&rnd="+window.document.getElementById('drpRound').value;
	
	//Score
	$scr = "&scr="+window.document.getElementById('txtScore').value;
	
	var $url="functions/iclass.php";
	$url=$url+$gen+$dis+$fce+$rnd+$scr;
	
	$xmlhttp.onreadystatechange=function() {
		actionCal($xmlhttp);
	}
	$xmlhttp.open("GET",$url,true);
	$xmlhttp.send(null);	
}

//Load Age Groups on Page for Gender PHP Query
function actionCal($xmlhttp) {	
	if (($xmlhttp.readyState==4) && ($xmlhttp.status==200))	{
		//If All OK
		if ($xmlhttp.responseText.indexOf("Array")!=-1) {
			var $rsltArray=eval($xmlhttp.responseText);
			
			window.document.getElementById('calcerror').style.display="none";
			window.document.getElementById('txtClass').value=$rsltArray[0];
			window.document.getElementById('txtHandicap').value=$rsltArray[1];
			window.document.getElementById('imgPin').src=$rsltArray[2];
		}		
		//Else If Error Returned
		else if ($xmlhttp.responseText.indexOf("Error")!=-1) {
			var $rsltString=$xmlhttp.responseText.substr(6);

			window.document.getElementById('txtClass').value=""
			window.document.getElementById('txtHandicap').value=""
			window.document.getElementById('imgPin').src="/images/spacer.gif";			
			window.document.getElementById('calcerror').style.display="inline";			
			window.document.getElementById('calcerror').innerHTML=$rsltString;
		}
		//Else Another Error
		else {
			window.document.getElementById('txtClass').value=""
			window.document.getElementById('txtHandicap').value=""
			window.document.getElementById('imgPin').src="/images/spacer.gif";			
			window.document.getElementById('calcerror').style.display="inline";			
			window.document.getElementById('calcerror').innerHTML="<font color=\"#FF0000\"><strong>An error has occurred!</strong></font>";		
		}
	}	
}