// Ajax functions

function getXMLHttpObject()
{
	var xmlHttp;
	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 contactRequestDetails(myForm, elementToChange, eventID, hideshow)
{
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/ajax_php/contact_request_details.php";
	url=url+"?id="+eventID;
	url=url+"&hideshow="+hideshow;
	url=url+"&sid="+Math.random();

	xmlHttp.onreadystatechange=function()
    {
		if(xmlHttp.readyState==4)
		{
			document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
			//window.open("document.writeln('" + xmlHttp.responseText + "');", 'OFFSCREEN','scrollbars=no,resizable=no,width=1024,height=768');
		}
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.send(null);
}

function eventDetails(myForm, elementToChange, eventID, hideshow)
{
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/ajax_php/event_details.php";
	url=url+"?id="+eventID;
	url=url+"&hideshow="+hideshow;
	url=url+"&sid="+Math.random();

	xmlHttp.onreadystatechange=function()
    {
		if(xmlHttp.readyState==4)
		{
			document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
		}
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.send(null);
}

function testPrint()
{
	alert('hi');
}

function showPreview(elementToChange)
{	

	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/admin_page_preview.php";
	
//	var poststr = "billing_address=" + encodeURI( document.getElementById("billing_address").value ) +
//		  		  "&billing_city=" + encodeURI( document.getElementById("billing_city").value ) +
//		  		  "&cc_zip=" + encodeURI( document.getElementById("cc_zip").value );
	

	var poststr = "thecontent=" +  encodeURI(document.getElementById("page_content").value);

	var t;

	xmlHttp.onreadystatechange=bleh;

	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
					//alert(xmlHttp.responseText);
				document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
				//window.open("javascript:document.writeln(" + xmlHttp.responseText + ");", 'OFFSCREEN','scrollbars=yes,resizable=yes,width=1024,height=768');
			 }
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr);
 
//   orderOK();
 //  alert("t is " + xmlHttp.responseText);
}

// Non-ajax functions - used for two lists manipulations (one is a main list, and the other is the new list)
function uncheckAll(obj) {
	for (var loop=0; loop < obj.options.length; loop++) {
	obj.options[loop].selected = false;
	}
}
function selectAll(listID)
{
	var newList = document.getElementById(listID);

	for (i = newList.length - 1; i>=0; i--)
	{
		newList.options[i].selected = true;
	}
}
function addSelectedClientsToList(mList, nList)
{
	var elSel = document.getElementById(mList);
	var newList = document.getElementById(nList);

	var i;
	for (i = elSel.length - 1; i>=0; i--)
	{
		if (elSel.options[i].selected)
		{
			addClientsToList(elSel.options[i].value, elSel.options[i].text, newList);
		}
	}

	uncheckAll(document.getElementById(mList));
}

function addClientsToList(optionValue, optionText, newList)
{
	var found = 0;

	for (i = newList.length - 1; i>=0; i--) {
		if (newList.options[i].value ==optionValue) {
		 found = 1;
		}
	}

	if (found == 0)
	{
		var elOptNew = document.createElement('option');
		elOptNew.text = optionText;
		elOptNew.value = optionValue;

		try {
		newList.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
		newList.add(elOptNew); // IE only
		}
	}

}

function removeOptionSelected(listID)
{
  var elSel = document.getElementById(listID);
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
	if (elSel.options[i].selected) {
	  elSel.remove(i);
	}
  }
}

function get_insurances(elementToChange, state, insuranceString)
{	
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 

	var url="/ajax_php/getInsurances.php";	
	var poststr = "state=" +  encodeURI(state) +
				  "&insurances=" +  encodeURI(insuranceString) +
				  "&sid="+Math.random();

	xmlHttp.onreadystatechange=bleh;
	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
				document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
			}
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr); 
}

function get_business_details(elementToChange, bid, slotID, hideshow)
{	
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 

	var url="/ajax_php/getBusinessDetails.php";	
	var poststr = "id=" +  encodeURI(bid) +
				  "&slotID="+  encodeURI(slotID) +
				  "&hideshow=" +  encodeURI(hideshow) +
				  "&sid="+Math.random();

	xmlHttp.onreadystatechange=bleh;
	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
				document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
			}
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr); 
}


function get_member_details(elementToChange, mID, slotID, hideshow)
{	
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 

	var url="/ajax_php/getMemberDetails.php";	
	var poststr = "id=" +  encodeURI(mID) +
				  "&slotID="+  encodeURI(slotID) +
				  "&hideshow=" +  encodeURI(hideshow) +
				  "&sid="+Math.random();

	xmlHttp.onreadystatechange=bleh;
	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
				document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
			}
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr); 
}


function get_business_location_map(elementToChange, businessID, hideshow)
{	
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 

	var url="/ajax_php/getBusinessLocationMap.php";	
	var poststr = "id=" +  encodeURI(businessID) +
				  "&hideshow=" +  encodeURI(hideshow) +
				  "&sid="+Math.random();

	xmlHttp.onreadystatechange=bleh;
	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
				document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
			}
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr); 
}

function mapsLoaded() {
  var map = new google.maps.Map2(document.getElementById("map"));
  map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
}

function loadMaps() {
  google.load("maps", "2", {"callback" : mapsLoaded});
}

function initLoader() {
  var script = document.createElement("script");
  script.src = "http://www.google.com/jsapi?key=ABQIAAAA7pAlS1WTUX3qcZWN5L3qCBSAPU7ypGtiIA7oyDY9VMwKXsL7_xTH1p1xt3geTpYfG1hhajtsKSH8Tg&callback=loadMaps&sensor=false";
  script.type = "text/javascript";
  document.getElementsByTagName("head")[0].appendChild(script);
}

function get_insurance_drop_down(elementToChange, insuranceID, specialtyID)
{	
	document.getElementById(elementToChange).innerHTML="Loading Insurance List. Please wait...";
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 

	var url="/ajax_php/getInsuranceDropDown.php";	
	var poststr = "spid=" +  encodeURI(specialtyID) +
				  "&iid=" +  encodeURI(insuranceID) +
				  "&sid="+Math.random();

	xmlHttp.onreadystatechange=bleh;
	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
				document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
			}
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr); 
}
