var please_wait = null;

function open_url(url, target) {
 	if ( ! document.getElementById) {
  		return false;
 	}
	
 	//if (please_wait != null) 
 	//{
  		if (target == 'flowerdivcontainer')
  		{
  			document.getElementById(target).innerHTML = "<p align=\"center\">&nbsp;</p><p align=\"center\"><img src=\"images/ajaxloader_tab.gif\" alt=\"\" /></p>";
  		}
  		else
	  		if (target == 'thresholdAddResult')
	  		{
				document.getElementById(target).innerHTML = "Saving Record..."; 
	  		}
	  		else
	  		{
	  			if ((target == 'div_animation_section') || (target == 'selectThresholdForm') || (target == 'thresholdAddResult') || (target == 'thresholdSettings') || (target == 'hard_count_result') || (target == 'soft_count_result') || (target == 'result_div'))
	  			{
	  				document.getElementById(target).innerHTML = "<p align=\"center\">&nbsp;</p><p align=\"center\"><img src=\"images/ajax_small_loader.gif\" alt=\"\" /></p>";
	  			}
	  			else
	  			{
					document.getElementById(target).innerHTML = "<p align=\"center\">&nbsp;</p><p align=\"center\"><img src=\"images/ajaxloader.gif\" alt=\"\" /></p>"; 
	  			}
	  		}
 	//}

	try
		{
	  		// Firefox, Opera 8.0+, Safari
			//alert("XMLHttpRequest");
	  		link=new XMLHttpRequest();
	  	}
	catch (e)
	  	{
	  		// Internet Explorer
	  	try
			{
				//alert("Msxml2.XMLHTTP");
				link=new ActiveXObject("Msxml2.XMLHTTP");
			}
	  	catch (e)
			{
			try
		  		{
					//alert("Microsoft.XMLHTTP");
		  			link=new ActiveXObject("Microsoft.XMLHTTP");
		  		}
			catch (e)
		  		{
					//alert("No Support");
		  			alert("Your browser does not support AJAX!");
		  			return false;
		  		}
			}
	  	}
	/*
 	if (window.ActiveXObject) {
  		link = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.ActiveXObject)
	{
  		link = new ActiveXObject("Microsoft.Msxml2.XMLHTTP");
 	} else if (window.XMLHttpRequest) {
  		link = new XMLHttpRequest();
 	}
	*/

 	if (link == undefined) {
  		return false;
 	}
 	link.onreadystatechange = function() { response(url, target); }
 	link.open("GET", url, true);
 	link.send(null);
}

function response(url, target) 
{
	/*
	if (link.readyState != 4)
	{
       	document.getElementById(target).innerHTML = '<img src="ajaxloader.gif"/>';
	}
	*/	
	
 	if (link.readyState == 4) {
		//alert(url);
	 	document.getElementById(target).innerHTML = (link.status == 200) ? link.responseText : "No page available!! Please contact the webmaster of this website ASAP and give him the following errorcode: " + link.status;
		if (url.substring(0,20) == 'csm_tab_selector.php')
		{
			adc = url.substring(21);
			adc_url = "csm_histogram.php?"+adc;
			open_url(adc_url,'flowerdivcontainer');
		}

		if (url.substring(0,20) == 'csm_alerts_grid.html')
		{
			document.mainForm.checkedRows.value = 0;
			adc = url.substring(25);
			startGrid(adc);
			load_alert_combo();
		}
		
		if (url.substring(0,26) == 'csm_set_adc_thresholds.php')
		{
			adc = url.substring(31);
			startThresholdsGrid(adc);
		}
		
		if (url.substring(0,27) == 'csm_save_adc_thresholds.php')
		{
			endADC = url.indexOf("&");
			adc = url.substring(32,endADC);
			open_url('csm_set_adc_thresholds.php?adc='+adc,'flowerdivcontainer');
		}
		
		if (url.substring(0,17) == 'csm_histogram.php')
		{
			if ((adcNum == 0) || (adcNum == 5))
			{
				document.getElementById("tempModeDiv").style.visibility = "hidden";
			}
			else
			{
				document.getElementById("tempModeDiv").style.visibility = "visible";
			}
			open_url('csm_graph_area.php','div_animation_section');
		}
		
		if (url.substring(0,18) == 'csm_graph_area.php')
		{
			load_charts();
		}
		
		if (url.substring(0,13) == 'testmaps.html')
		{
			loadMap();
		}
		
		if (url.substring(0,26) == 'csm_set_adc_thresholds.php')
		{
			min = document.alertscombo.defaultMin.value;
			max = document.alertscombo.defaultMax.value;
			
			open_url('csm_hard_thresholds_form.php?min='+min+'&max='+max,'selectThresholdForm');
		}
		
		if (url.substring(0,32) == 'csm_save_adc_soft_thresholds.php')
		{
			startSoftThresholdsGrid(document.mainForm.selectedID.value);
		}
		
		if (url.substring(0,28) == 'csm_hard_thresholds_form.php')
		{
			count = document.alertscombo.hard_count.value;
			open_url('csm_hard_thresholds_settings.php?count='+count,'thresholdSettings');
		}
		
		if (url.substring(0,28) == 'csm_soft_thresholds_form.php')
		{
			time_limit = document.alertscombo.soft_time_limit.value;
			open_url('csm_soft_thresholds_settings.php?time_limit='+time_limit,'thresholdSettings');
		}
	}
}

function set_loading_message(msg) 
{
 	please_wait = msg;
}
