var timerID;
var all_values_array;
     
	function createObject(){
		var http_request = false;

		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			
			http_request = new XMLHttpRequest();
			
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		}
		else if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {}
			}
		}

		if (!http_request) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
	
		return http_request;
	
	}

function alertContents(http_request, state) {

	if (http_request.readyState == 4) {
	
		if (http_request.status == 200) {
                
		if(state == 'checkid')
			document.getElementById('id_status').innerHTML = http_request.responseText;
            	else if(state == 'searchid')
			document.getElementById('search_results').innerHTML = http_request.responseText;
		else if(state == 'get_values'){

			eval(http_request.responseText);
			
		}
		else if(state == 'get_field'){
			
			return http_request.responseText;
			
		}
		
	    
	    } else {
                alert('There was a problem with the request.');
            }
        }

    }

    
    
    function checkID(id) {
	
	var http_request = createObject();

	http_request.onreadystatechange = function() { alertContents(http_request, "checkid"); };
        http_request.open('POST', '_includes/_functions.php', true);
       	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        http_request.send("sid="+sid+"&_state=check_id&id="+id);
    }


    function searchID(field, term, sort) {
	clearTimeout(timerID);
	timerID = setTimeout("searchID_begin('"+field+"','"+term+"','"+sort+"')", 500);	
    }

    function searchID_begin(field, term, sort){
    	
	var http_request = createObject();

	http_request.onreadystatechange = function() { 
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				document.getElementById('search_results').innerHTML = http_request.responseText;
			}
		}
	};
        
	http_request.open('POST', '_includes/_functions.php', true);
       	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        http_request.send("sid="+sid+"&_state=search_id&field="+field+"&term=" + trim( term ) + "&sortby="+sort);
    }
    
    function loadForm(id){
    	var http_request = createObject();

	http_request.onreadystatechange = function() {};
        http_request.open('POST', '_includes/_functions.php', true);
       	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        http_request.send("sid="+sid+"&_state=load_id&id="+id);
    }

    function getValues(id){
    	var http_request = createObject();

	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				eval(http_request.responseText);
			}
		}
	};
        http_request.open('POST', '_includes/_functions.php', true);
       	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        http_request.send("sid="+sid+"&_state=get_values&id="+id);
    }

    function getField(name){
    	var http_request = createObject();

	http_request.onreadystatechange = function() { 
	
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				display_cell_view( id, name, value );
			}
		}
	
	};

        http_request.open('POST', '_includes/_functions.php', true);
       	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        http_request.send("sid="+sid+"&_state=get_field&name="+name);
    }

    function getAllFields(editable){
    	var http_request = createObject();

	http_request.onreadystatechange = function() {};
        http_request.open('POST', '_includes/_functions.php', true);
       	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        http_request.send("sid="+sid+"&_state=get_all_fields&editable="+editable);
    }


////////////////////////////////////////////////////////



function SubHospital(vVal){

	if(vVal == ''){
	document.getElementById("dvDepartment").style.display = "none";
	//document.getElementById("dvSubCat2").style.display = "none";
	return;
	}else{
	
	//xmlHttp=GetXmlHttpObject();
	//if(xmlHttp==null){
	//	alert("Browser does not support HTTP Request");
	//	return;
	//}
	//var url="_includes/_functions.php";
	//url=url+"?q="+vVal;
	//url=url+"&sid="+Math.random();
	var http_request = createObject();

	http_request.onreadystatechange = function() { 
	
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {

				//document.write(http_request.responseText);
				//eval("all_values_array = (" + http_request.responseText + ")");

				//alert(http_request.responseText);
				//switch( status ){
				
					//case 1:
						//alert("poop?");
						 display_dv_SubCat("dvDepartment", escape( http_request.responseText) );
						// break;
				//}
				
			}
		}
		
	}
	http_request.open('POST', 'functions.php', true);
	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	http_request.send("sid="+sid+"&_state=subhospital&id="+vVal);
	//xmlHttp.onreadystatechange=stateChanged;
	//xmlHttp.open("GET",url,true);
	//xmlHttp.send(null);
	}
}

function SubDepartment(vVal){

	if(vVal == ''){
	document.getElementById("dvDivision").style.display = "none";
	//document.getElementById("dvSubCat2").style.display = "none";
	return;
	}else{
	
	//xmlHttp=GetXmlHttpObject();
	//if(xmlHttp==null){
	//	alert("Browser does not support HTTP Request");
	//	return;
	//}
	//var url="_includes/_functions.php";
	//url=url+"?q="+vVal;
	//url=url+"&sid="+Math.random();
	var http_request = createObject();

	http_request.onreadystatechange = function() { 
	
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {

				//document.write(http_request.responseText);
				//eval("all_values_array = (" + http_request.responseText + ")");

				//alert(http_request.responseText);
				//switch( status ){
				
					//case 1:
						//alert("poop?");
						 display_dv_SubCat("dvDivision", escape( http_request.responseText) );
						// break;
				//}
				
			}
		}
		
	}
	http_request.open('POST', 'functions.php', true);
	http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	http_request.send("sid="+sid+"&_state=subdepartment&id="+vVal);
	//xmlHttp.onreadystatechange=stateChanged;
	//xmlHttp.open("GET",url,true);
	//xmlHttp.send(null);
	}
}


function display_dv_SubCat( name, value ){

	//value = value.replace(/"/g, "`");
	//value = value.replace(/'/g, '`');
	
	//value = escape(value);
	
	//var classValue = ( name == "comments" ) ? "cell_view_textarea" : "cell_view";
	
	var str = unescape(value);
	//alert(value);
	//if( name == "comments" )
	//	alert( str );
	if (str != 'empty'){

		document.getElementById(name).innerHTML = str;

		document.getElementById(name).style.display="inline";
		//document.getElementById(name+"2").style.display="block"
	}else{
		//document.getElementById(name).innerHTML = str;
		document.getElementById(name).style.display="none";
		//document.getElementById(name+"2").style.display="none"
	}
	
}
////////////////////////////////////////////////////////

function trim( inputString ) {

	if ( typeof inputString != "string" ) {
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while ( ch == " " ) { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);

	while ( ch == " " ) { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while ( retValue.indexOf("  ") != -1 ) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user

}

