var xmlhttp;
var base = window.location.protocol+"//"+window.location.host+"/";
var clicked;
function doConfirm(){
	var res;
	res = confirm('Are you sure you want to delete this?');
	if (res){
		return true;
	}else{
		return false;
	}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function stateChanged()
{
	switch (clicked){
	case "addPatient":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('message').innerHTML=xmlhttp.responseText;
		  
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('message').innerHTML= "Loading...";
		}
	break;
	case "editPatient":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('message').innerHTML=xmlhttp.responseText;
		  
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('message').innerHTML= "Loading...";
		}
	break;
	case "searchPatients":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('patients').innerHTML=xmlhttp.responseText;
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('patients').innerHTML= "Loading...";
		}
	break;
	case "showinfo":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('infobox').innerHTML=xmlhttp.responseText;
		  
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('infobox').innerHTML= "Loading...";
		}
	break;
	case "addAppointment":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('message').innerHTML=xmlhttp.responseText;
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('message').innerHTML= "Loading...";
		}
	break;
	case "searchApp":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('s_apt').innerHTML=xmlhttp.responseText;
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('s_apt').innerHTML= "Loading...";
		}
	break;
	case "authenticate":
		if (xmlhttp.readyState==4)
		  {
		  //document.getElementById('lightbox-data').style.display = 'none';
				if (xmlhttp.responseText == 'ok'){
						window.location = base+"ptm";
				}else{
				  document.getElementById('err-con').innerHTML = xmlhttp.responseText;
				  //$("#msg-holder").fadeIn(1000);
				}
				document.getElementById('msg-holder').innerHTML=xmlhttp.responseText;
				  $("#msg-holder").fadeIn(1000);
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('err-con').innerHTML= "<p>Loading...</p>";
		}
	break;
	case "addFacility":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('message').innerHTML=xmlhttp.responseText;
		  
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('message').innerHTML= "Loading...";
		}
	break;
	case "editFacility":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('message').innerHTML=xmlhttp.responseText;
		  
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('message').innerHTML= "Loading...";
		}
	break;
	case "contactPTM":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('message').innerHTML=xmlhttp.responseText;
		  
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('message').innerHTML= "Loading...";
		}
	break;
	case "tellmore":
		if (xmlhttp.readyState==4)
		  {
		  //document.getElementById('message').innerHTML=xmlhttp.responseText;
		  document.getElementById('global').innerHTML= "";
		  alert(xmlhttp.responseText);
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('global').innerHTML= "Sending...";
		}
	break;
	case "autofill":
		if (xmlhttp.readyState==4)
		  {
		  document.getElementById('suggestion').innerHTML=xmlhttp.responseText;
		  document.getElementById('full').className = "nobg";
		  }
		if (xmlhttp.readyState !=4 ){
			document.getElementById('full').className = "showbg";
		}
	break;
	default:
	
	break;
	}
}


function doSignUp(){
	clicked = "signup";
	xmlhttp=GetXmlHttpObject();
	var name = encodeURIComponent(document.getElementById('name').value);
	var email = encodeURIComponent(document.getElementById('email').value);
	var password = encodeURIComponent(document.getElementById('password').value);
	var conf_password = encodeURIComponent(document.getElementById('conf_password').value);
	var param = "name="+name+"&email="+email+"&password="+password+"&conf_password="+conf_password;
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"sitepage/doSignup";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function doLogAdmin(){
	clicked = "authenticate";
	xmlhttp=GetXmlHttpObject();
	var username = encodeURIComponent(document.getElementById('username').value);
	var password = encodeURIComponent(document.getElementById('password').value);
	var param = "username="+username+"&password="+password;
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"authenticate/doLogin";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function doLogin(){
	clicked = "login";
	xmlhttp=GetXmlHttpObject();
	var username = encodeURIComponent(document.getElementById('username').value);
	var password = encodeURIComponent(document.getElementById('password').value);
	var param = "username="+username+"&password="+password;
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"login/doLogin";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function get(party_id){
	clicked = "default";
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url=base+"admin/passval/"+party_id;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
function addPatient(){
	clicked = "addPatient";
	xmlhttp=GetXmlHttpObject();
	var firstname = encodeURIComponent(document.getElementById('firstname').value);
	var lastname = encodeURIComponent(document.getElementById('lastname').value);
	var address1 = encodeURIComponent(document.getElementById('address1').value);
	var address2 = encodeURIComponent(document.getElementById('address2').value);
	var city = encodeURIComponent(document.getElementById('city').value);
	var state = encodeURIComponent(document.getElementById('state').value);
	var zip = encodeURIComponent(document.getElementById('zip').value);
	var phone = encodeURIComponent(document.getElementById('phone').value);
	var date_of_birth = encodeURIComponent(document.getElementById('date_of_birth').value);
	var insurance = encodeURIComponent(document.getElementById('insurance').value);
	var provider = encodeURIComponent(document.getElementById('provider').value);
	var status = encodeURIComponent(document.getElementById('status').value);
	var facility_id = encodeURIComponent(document.getElementById('facility_id').value);
	
	var param = "firstname="+firstname+"&lastname="+lastname+"&address1="+address1+"&address2="+address2+"&city="+city+"&state="+state+"&zip="+zip+"&phone="+phone+"&date_of_birth="+date_of_birth+"&insurance="+insurance+"&provider="+provider+"&status="+status+"&facility_id="+facility_id;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"ptm/doAddPatient";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function editPatient(p_id){
	clicked = "editPatient";
	xmlhttp=GetXmlHttpObject();
	var firstname = encodeURIComponent(document.getElementById('firstname').value);
	var lastname = encodeURIComponent(document.getElementById('lastname').value);
	var address1 = encodeURIComponent(document.getElementById('address1').value);
	var address2 = encodeURIComponent(document.getElementById('address2').value);
	var city = encodeURIComponent(document.getElementById('city').value);
	var state = encodeURIComponent(document.getElementById('state').value);
	var zip = encodeURIComponent(document.getElementById('zip').value);
	var phone = encodeURIComponent(document.getElementById('phone').value);
	var date_of_birth = encodeURIComponent(document.getElementById('date_of_birth').value);
	var insurance = encodeURIComponent(document.getElementById('insurance').value);
	var provider = encodeURIComponent(document.getElementById('provider').value);
	var status = encodeURIComponent(document.getElementById('status').value);
	var facility_id = encodeURIComponent(document.getElementById('facility_id').value);
	
	var param = "firstname="+firstname+"&lastname="+lastname+"&address1="+address1+"&address2="+address2+"&city="+city+"&state="+state+"&zip="+zip+"&phone="+phone+"&date_of_birth="+date_of_birth+"&insurance="+insurance+"&provider="+provider+"&status="+status+"&facility_id="+facility_id;
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"ptm/doEditPatient/"+p_id;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function checkUncheck(id){
	if(document.getElementById(id).checked == false){
		document.getElementById(id).checked = true;
	}else{
		document.getElementById(id).checked = false;
	}
}
function showInfo(p_id){
	clicked = "showinfo";
	document.getElementById('p_id').value = p_id;
	document.getElementById('suggestion').style.display = "none";
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url=base+"ptm/showinfo/"+p_id;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
function addAppointment(){
	clicked = "addAppointment";
	xmlhttp=GetXmlHttpObject();
	var message = encodeURIComponent(document.getElementById('message').value);
	var p_id = encodeURIComponent(document.getElementById('p_id').value);
	var status = encodeURIComponent(document.getElementById('status').value);
	var app_date = encodeURIComponent(document.getElementById('app_date').value);
	var provider_id = encodeURIComponent(document.getElementById('provider_id').value);
	var param = "message="+message+"&p_id="+p_id+"&provider_id="+provider_id+"&status="+status+"&app_date="+app_date;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"ptm/doaddAppointment";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function addFacility(){
	clicked = "addFacility";
	xmlhttp=GetXmlHttpObject();
	var facility_name = encodeURIComponent(document.getElementById('facility_name').value);
	var facility_type = encodeURIComponent(document.getElementById('facility_type').value);
	var address = encodeURIComponent(document.getElementById('address').value);
	var city = encodeURIComponent(document.getElementById('city').value);
	var state = encodeURIComponent(document.getElementById('state').value);
	var zip = encodeURIComponent(document.getElementById('zip').value);
	var contact_person = encodeURIComponent(document.getElementById('contact_person').value);
	var phone = encodeURIComponent(document.getElementById('phone').value);
	var fax = encodeURIComponent(document.getElementById('fax').value);
	var provider_id = encodeURIComponent(document.getElementById('provider').value);
	
	var param = "facility_name="+facility_name+"&facility_type="+facility_type+"&address="+address+"&city="+city+"&state="+state+"&zip="+zip+"&contact_person="+contact_person+"&phone="+phone+"&fax="+fax+"&provider_id="+provider_id;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"ptm/doAddFacility";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function editFacility(facility_id){
	clicked = "editFacility";
	xmlhttp=GetXmlHttpObject();
	var facility_name = encodeURIComponent(document.getElementById('facility_name').value);
	var facility_type = encodeURIComponent(document.getElementById('facility_type').value);
	var address = encodeURIComponent(document.getElementById('address').value);
	var city = encodeURIComponent(document.getElementById('city').value);
	var state = encodeURIComponent(document.getElementById('state').value);
	var zip = encodeURIComponent(document.getElementById('zip').value);
	var contact_person = encodeURIComponent(document.getElementById('contact_person').value);
	var phone = encodeURIComponent(document.getElementById('phone').value);
	var fax = encodeURIComponent(document.getElementById('fax').value);
	var provider_id = encodeURIComponent(document.getElementById('provider').value);
	
	var param = "facility_name="+facility_name+"&facility_type="+facility_type+"&address="+address+"&city="+city+"&state="+state+"&zip="+zip+"&contact_person="+contact_person+"&phone="+phone+"&fax="+fax+"&provider_id="+provider_id;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"ptm/doEditFacility/"+facility_id;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function doSearchApp(){
	clicked = "searchApp";
	xmlhttp=GetXmlHttpObject();
	var datefr = encodeURIComponent(document.getElementById('datefr').value);
	var dateto = encodeURIComponent(document.getElementById('dateto').value);
	var provider_id = encodeURIComponent(document.getElementById('provider_id').value);
	var status = encodeURIComponent(document.getElementById('status').value);
	
	var param = "datefr="+datefr+"&dateto="+dateto+"&provider_id="+provider_id+"&status="+status;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"ptm/doSearchAppointment";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function doSearchPatients(){
	clicked = "searchPatients";
	xmlhttp=GetXmlHttpObject();
	var firstname = encodeURIComponent(document.getElementById('firstname').value);
	var lastname = encodeURIComponent(document.getElementById('lastname').value);
	var provider = encodeURIComponent(document.getElementById('provider').value);
	var status = encodeURIComponent(document.getElementById('status').value);
	
	var param = "provider="+provider+"&status="+status+"&firstname="+firstname+"&lastname="+lastname;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"ptm/doSearchPatients";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function contactPTM(){
	clicked = "contactPTM";
	xmlhttp=GetXmlHttpObject();
	var fullname = encodeURIComponent(document.getElementById('fullname').value);
	var email = encodeURIComponent(document.getElementById('email').value);
	var phone = encodeURIComponent(document.getElementById('phone').value);
	
	var param = "fullname="+fullname+"&email="+email+"&phone="+phone;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"site/contactPTM";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function tellmore(){
	clicked = "tellmore";
	xmlhttp=GetXmlHttpObject();
	var firstname = encodeURIComponent(document.getElementById('firstname').value);
	var lastname = encodeURIComponent(document.getElementById('lastname').value);
	var email = encodeURIComponent(document.getElementById('email').value);
	var phone = encodeURIComponent(document.getElementById('phone').value);
	
	var param = "firstname="+firstname+"&lastname="+lastname+"&email="+email+"&phone="+phone;
	
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	var url=base+"site/tellmore";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlhttp.send(param);
	return false;
}
function autofill(val){
	if (document.getElementById('full') == ''){
	document.getElementById('suggestion').style.display = "none";	
	}else{
	document.getElementById('suggestion').style.display = "block";
	}
	clicked = "autofill";
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  }
	var url=base+"ptm/autofill/"+val;
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
