// JavaScript Document

var xmlHttp;

/* Function to display the phone information when the phone/fax button is clicked
*
*  userid - the CB record user_id
*	 phone - phone number for the CB record user
*	 tf - tollfree number for the CB record user, if blank set to false
*	 fax - fax number for the CB record user, if blank set to false
*
*/
function showPhone(city, userid, phone, tollfree, fax )
{ 
	var viewed = document.phoneInfo.viewed.value;
	if (viewed == 0 ) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 }
		var url="/templates/health/includes/getphone_info.php";
		url=url+"?id="+ escape(parseInt(userid));
		url=url+"&phone="+escape(phone);
		url=url+"&tf="+escape(tollfree);
		url=url+"&fax="+escape(fax);
		url=url+"&sid="+Math.random();
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=updatePage; 
		xmlHttp.send(null);
	}
}

function updatePage() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		if (xmlHttp.status == 200) {
	 		document.getElementById("contactDetails").innerHTML=xmlHttp.responseText;
			document.phoneInfo.viewed.value = 1; // Make sure we don't update the db more than once for the page view
		} else if (xmlHttp.status == 404) 
			alert ("Requested URL does not exist");
		else if (xmlHttp.status == 403)
      alert("Access denied.");
		else 
			alert("HTTP error = " + xmlHttp.status + "An error was encountered: "+ xmlHttp.statusText);
	} 
}

/* Function to update the email count in the CB record when the Email button is clicked
*
*  userid - the CB record user_id
*/
function emailCount(city, userid)
{ 
	var viewed = document.phoneInfo.emailviewed.value;
	if (viewed == 0 ) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 }
		var url="/templates/health/includes/emailCount.php";
		url=url+"?id="+ escape(parseInt(userid));
		url=url+"&sid="+Math.random();
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=updateCount; 
		xmlHttp.send(null);
	}
}

function updateCount() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		if (xmlHttp.status == 200) {
			document.phoneInfo.emailviewed.value = 1; // Make sure we don't update the db more than once for the page view
		} else if (xmlHttp.status == 404) 
			alert ("Requested URL does not exist");
		else if (xmlHttp.status == 403)
      alert("Access denied.");
		else 
			alert("HTTP error = " + xmlHttp.status + "An error was encountered: "+ xmlHttp.statusText);
	} 
}
function email2Count(city, userid)
{ 
	var viewed2 = document.emailInfo.email2viewed.value;
	if (viewed2 == 0 ) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 }
		var url="/templates/health/includes/emailCount.php";
		url=url+"?id="+ escape(userid);
		url=url+"&sid="+Math.random();
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange=updateCount2; 
		xmlHttp.send(null);
	}
}

function updateCount2() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		if (xmlHttp.status == 200) {
			document.emailInfo.email2viewed.value = 1; // Make sure we don't update the db more than once for the page view
		} else if (xmlHttp.status == 404) 
			alert ("Requested URL does not exist");
		else if (xmlHttp.status == 403)
      alert("Access denied.");
		else 
			alert("HTTP error = " + xmlHttp.status + "An error was encountered: "+ xmlHttp.statusText);
	} 
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
	 	xmlHttp=new XMLHttpRequest();
	} catch (trymicrosoft) {
		//Internet Explorer
	 	try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
  if (!xmlHttp) alert("Error initializing XMLHttpRequest!");
	return xmlHttp;
}