// JavaScript Document
//alert("testing department");
var GlobalDivId;
String.prototype.trim =      function() {
  return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}


function GetXmlHttpObject(handler)
{
    var objXMLHttp=null
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
}

function stateChanged()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {		
             if(xmlHttp.responseText!=null && xmlHttp.responseText!=''){
			    document.getElementById("getDep").innerHTML= xmlHttp.responseText;
				if(document.getElementById("continue")!=null){
				    document.getElementById("continue").style.display='block';
					document.getElementById("conMsg").style.display='block';
				}
			}else{
				if(document.getElementById("continue")!=null){
					document.getElementById("conMsg").style.display='none';
				    document.getElementById("continue").style.display='none';
				}
					document.getElementById("getDep").innerHTML= '<div align="left" style="width:300px; clear:none;  color:#FF0000; padding-top:1px;"><strong>No Department found, <a href="http://www.healthy-firefighter.org/atp/signupDepartment.php">click here</a> to register <br /> your department.</strong></div>';
			}
	}
    else {
            //alert(xmlHttp.status);
    }
}

// Will populate data based on input
function listDepartment(url)
{
	//alert("inside listDepartment");
	GlobalDivId=1;
	
	 var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if((document.frmSearchDept.deptState.value=='') && (document.frmSearchDept.zipcode.value.trim()=='') && (document.frmSearchDept.departmentName.value.trim()=='')){
		alert("Please enter at least one search criteria");
		document.frmSearchDept.deptState.focus();
		return false;
	}else if(document.frmSearchDept.zipcode.value !='') {
		if(!objRegExp.test(trim(document.frmSearchDept.zipcode.value)))
		{
		  alert("Please enter valid US zip code. EX 12345");
		  document.frmSearchDept.zipcode.focus();
		  return false;
		}
	}
	
	document.getElementById("getDep").innerHTML = "<img src=\"images/loading.gif\" width='16' height='16'>";
	document.getElementById("getDep").style.display='inline';
	if(document.getElementById("ddid")!=null){
	document.getElementById("ddid").style.display='inline';
	}
	if(document.getElementById("dtid")!=null){
	document.getElementById("dtid").style.display='inline';
	}
	var qStr = '';
	var deptState = document.frmSearchDept.deptState.value;
	var zipcode =  document.frmSearchDept.zipcode.value;
	var departmentName =  document.frmSearchDept.departmentName.value;
	qStr = "deptState="+deptState+"&zipcode="+zipcode+"&departmentName="+departmentName;
	//alert("testing department");
	//document.getElementById("dept").innerHTML = "<img src=\"images/loading.gif\" width='16' height='16'>";
		//document.getElementById("dept").style.display='inline';
    if (url.length==0)
    {
        document.getElementById("getDep").innerHTML=""; 
        return;
    }
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
	
		url=url+"?"+qStr; 
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true) ;
		xmlHttp.send(null);
	
	}
