

function findwindowHeight() {
	if(window.XMLHttpRequest) {
		winheight = window.self.innerHeight;
	}
	if(window.ActiveXObject) {
	 	winheight = document.documentElement.clientHeight;
	}
	return winheight;	
}


function findwindowWidth() {
	if(window.XMLHttpRequest) {
		winlength = window.self.innerWidth;
	}
	if(window.ActiveXObject) {
		winlength = document.documentElement.clientWidth;
	}
	return winlength;	
}


function makeBlackWindow(locationWindow,presentationwindow, floatr,g_url) {
	var commonwin = document.getElementById(locationWindow);
	var preswin = document.getElementById(presentationwindow);
	
	
	
	if ((findwindowHeight()+0) > '600' ) {
		thisHeight = findwindowHeight() 
	} else {
		thisHeight = '600';
	}	
	
	if ((findwindowWidth()+0) > '800' ) {
		thisWidth = findwindowWidth() 
	} else {
		thisWidth = '800';
	}
	commonwin.style.height = thisHeight+"px";
	commonwin.style.width = thisWidth+"px";
	commonwin.style.display = "block";

//	alert(thisHeight + "   " + thisWidth);
	
	subHeight = thisHeight - (thisHeight* .13);
	subWidth = thisWidth - (thisWidth* .12);
//	alert(subHeight.toFixed(0) + "   " + subWidth.toFixed(0));
	
	sunXpos = thisHeight * .05;
	sunYpos = thisWidth * .05;
//	alert(sunXpos + "   " +sunYpos);
	
	preswin.style.height = subHeight.toFixed(0)+"px";
	preswin.style.width = subWidth.toFixed(0)+"px";
	preswin.style.top = sunXpos+"px";
	preswin.style.left = sunYpos+"px";

	
//	these open the change windows.
	Q =	presentationwindow;
	W = floatr;
	T = g_url
	clickypop(W);
	clickypop(Q);
//	change back after development
//	setTimeout('clickypop(Q)', 1000)
	
// populate the field	
	getdatafrompages(Q,T);
	
}

function clickypop (idofplace) {
	if (document.getElementById(idofplace).style.display == "block") {
		document.getElementById(idofplace).style.display = "none";
	} else {
		document.getElementById(idofplace).style.display = "block";
	}
	
}

function getdatafrompages (placetogetfrom, url) {
	
if(window.XMLHttpRequest)
	req = new XMLHttpRequest(); 
		else if (window.ActiveXObject)
			req  = new ActiveXObject('Microsoft.XMLHTTP'); 
		req.onreadystatechange = function()
		{ if(req.readyState == 4)
			{ if(req.status == 200){
					document.getElementById(placetogetfrom).innerHTML= req.responseText;
				}else {
					document.getElementById(placetogetfrom).innerHTML="Error: returned status <br />code " + req.status + " " + req.statusText;
				}
			} 
		};
		req.open("GET", url , true); 
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		req.send(null); 

	
}

function getdata (areatoplace, areatogetdata, fromwhere) {
	pacer = document.getElementById(fromwhere).selectedIndex
	thisURL = areatogetdata + "&hospital=" + pacer;
	thisBOX = areatoplace;
	getdatafrompages(thisBOX, thisURL);
	
}

function openpop (sendurl, hosp, formID) {
	spacer = document.getElementById(hosp).selectedIndex
	fobj = document.getElementById(formID);
	stringser = getFieldData(fobj);

	thisURL = sendurl + "&hospital=" + spacer + "&" + stringser;
	window.open (thisURL, 'patient_list' , 'width=800px, height=500px, scrollbars=yes', false );
	

}

function newWindow (url)
{
	//alert ("reached"); 
	window.open (url, 'patient_list' , 'width=800px, height=500px, scrollbars=yes', false );
}

function cleardata (thisplace) {
	document.getElementById(thisplace).innerHTML= '';
}

function senddata(formname, urltwo) {

    var error = '';

    if (document.getElementById('hospital_name').selectedIndex =='0') 
    	error += "Please choose a Hospital<br />" ; 
    if (document.getElementById('patient_Fname').value == '')
    	error += "Patient First Name is required<br />" ; 
    
    if (document.getElementById('patient_Lname').value == '')
    	error += "Patient Last Name is required<br />" ; 
    	
    if (document.getElementById('status').value == '')
		error += "Please write a status<br />" ; 
		
	 //if (document.getElementById('discharge').value == '')
	 	//error += "Please select a discharge<br />" ; 
	
	if (error) { 
		document.getElementById('errorbox').innerHTML = "<div style='background-color:#FF7F00; color:#FFF; padding:2px; font:8pt verdana, arial, sans; '>"+error+"</div>";

	} else {

		fobj = document.getElementById(formname);
		str = getFieldData(fobj); 
		
		if (document.getElementById('discharge').checked)
			urltwo += "?discharge=Yes";
		else
			urltwo += "?discharge=No";
			
		//alert(urltwo);
		if(window.XMLHttpRequest)
			req = new XMLHttpRequest(); 
		else if (window.ActiveXObject)
			req  = new ActiveXObject('Microsoft.XMLHTTP'); 
		req.onreadystatechange = function() 
		{ 
			if(req.readyState == 4)
			{ 
				if(req.status == 200)
				{
					
					if (document.getElementById('validator').value=='2') 
					{
						document.getElementById(formname).innerHTML= req.responseText;
					} 
					else 
					{
						document.getElementById('errorbox').innerHTML ='';	
						document.getElementById('errorbox').innerHTML= req.responseText;
						clearFieldData(document.getElementById(formname));
						var d = new Date()
						document.getElementById('datetimefield').value= d.getFullYear()+'-'+d.getMonth()+'-'+d.getDate()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();	
						document.getElementById('validator').value= '1';
						document.getElementById('discharge').value= 'No';
					}

				}
				else 
				{
					document.getElementById('errorbox').innerHTML="Error: returned status <br />code " + req.status + " " + req.statusText;
				}
			} 
		};
		req.open("POST", urltwo, true); 
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		req.send(str); 
	}
}



function getFieldData (fobj) {

	   var str = "";
       var valueArr = null;
       var val = "";
       var cmd = "";
       for(var i = 0;i < fobj.elements.length;i++)
       {
           switch(fobj.elements[i].type)
           {
                case "text":
			    case "hidden":
                    str += fobj.elements[i].name +
                     "=" + escape(fobj.elements[i].value) + "&";
                    break;
                case "select-one":
                    str += fobj.elements[i].name +
                    "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
                    break;
                case "textarea":
                    str += fobj.elements[i].name +
                     "=" + escape(fobj.elements[i].value) + "&";
					break;
				case "checkbox":
					if (fobj.elements[i].checked == true) {
						//alert (fobj.elements[i].checked);
						str += fobj.elements[i].name +
						 "=" + escape(fobj.elements[i].value) + "&";
					} 
                    break;

           }

       }
       str = str.substr(0,(str.length - 1));
       return str;
}


function clearFieldData (fobjer) {

	   var str = "";
       var valueArr = null;
       var val = "";
       var cmd = "";
       for(var i = 0;i < fobjer.elements.length;i++)
       {
           switch(fobjer.elements[i].type)
           {
                case "text":
			    case "hidden":
                    escape(fobjer.elements[i].value = '' )
                    break;
                case "select-one":
                    fobjer.elements[i].options[0].value = '0'
                    break;
                case "textarea":
                     escape(fobjer.elements[i].value = '' )
					break;

           }

       }
       str = str.substr(0,(str.length - 1));
       return str;
}

function archiveAll(reM) {
	var cof = confirm("this will delete all current records and archive them")
	if (cof == true) {
		
		getdatafrompages(reM,'../process.php?fetch=archive')
		alert('All Records Archived');
		
	} else {
		alert('nothing deleted nothing archived.');
		
	}
	
}

/*
  New Physician Assignment Function, works with the form entry in physician_assignment.php
  action - 1 (clears the entries)
  action - 2 (saves the entries)
  action - 3 (archive the entries)
  action - 4 (view archived list)
*/
function assign(form, action)
{
	var stringser = getFieldData(form);
	//alert(stringser);
	
	//creates appropriate objects based on the browser
	if(window.XMLHttpRequest)
		req = new XMLHttpRequest(); 
	else if (window.ActiveXObject)
		req  = new ActiveXObject('Microsoft.XMLHTTP'); 
		
	req.onreadystatechange = function() { 
		//display loading image
		if(req.readyState == 0)
			document.getElementById('response').innerHTML = '<img style="margin-left:150px" src="ajax-loader.gif" />';
		if(req.readyState == 1)
			document.getElementById('response').innerHTML = '<img style="margin-left:150px" src="ajax-loader.gif" />';
		if(req.readyState == 2)
			document.getElementById('response').innerHTML = '<img style="margin-left:150px" src="ajax-loader.gif" />';
		if(req.readyState == 3)
			document.getElementById('response').innerHTML = '<img style="margin-left:150px" src="ajax-loader.gif" />';
		
		//Complete Request
		if(req.readyState == 4) { 
			if(req.status == 200){
					
				//retrieve a list of patients enclosed in checkboxes
				var response = req.responseText;
				
				//display the list onto the page
				document.getElementById('response').innerHTML = response;

			}
			else {
				document.getElementById('errorbox').innerHTML="Error: returned status <br />code " + req.status + " " + req.statusText;
			}
		} 
	};
	req.open("POST", "patient_list.php?action="+action+"", true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(stringser);
}

//prompt the user if they really want to clear, and if so go ahead and pass the data to function assign
function clear_entry (form, action)
{
	//alert("reached");
	var con = confirm ('Do you want to clear these entries?');
	if (con == true)
	{
		//clear physician names
		for (var j=1; j<=5; j++)
		{
				document.getElementById('physician_1_'+j+'').value = "";
		}
		for (var j=1; j<=2; j++)
		{
				document.getElementById('physician_2_'+j+'').value = "";
		}
		for (var j=1; j<=2; j++)
		{
				document.getElementById('physician_3_'+j+'').value = "";
		}
		for (var j=1; j<=2; j++)
		{
				document.getElementById('physician_4_'+j+'').value = "";
		}
		
		//clear patient names
		for (var i=1; i<=5; i++)
		{
			for (var j=1; j<=16; j++)
			{
				document.getElementById('patient_1_'+j+'_'+i+'').value = "";
			}
		}
		for (var i=1; i<=2; i++)
		{
			for (var j=1; j<=6; j++)
			{
				document.getElementById('patient_2_'+j+'_'+i+'').value = "";
			}
		}
		for (var i=1; i<=2; i++)
		{
			for (var j=1; j<=10; j++)
			{
				document.getElementById('patient_3_'+j+'_'+i+'').value = "";
			}
		}
		for (var i=1; i<=2; i++)
		{
			for (var j=1; j<=8; j++)
			{
				document.getElementById('patient_4_'+j+'_'+i+'').value = "";
			}
		}
		
		assign (form, action);
	}
}




//Archive the physician assignment list: action 3 will archive entries
function archiveAssign (reM)
{
	var cof = confirm("this will delete the physician list and archive them");
	
	//archive the database, and clear
	if(cof == true)
	{
		getdatafrompages(reM,'./patient_list.php?action=3');
		alert('Physician List has been Archived');
	}
	else
	{
		alert('nothing archived.');
	}
}

//View archive for a specific date
function archiveView (reM,date)
{
	//show archive list
	if (date != 'none')
	{
		document.getElementById('archiveList').style.display = 'inline';
		document.getElementById('dateList').style.display = 'none';
		getdatafrompages(reM,'./patient_list.php?action=4&date='+date+'');
	}
	//show date list
	else
	{
		document.getElementById('archiveList').style.display = 'none';
		document.getElementById('dateList').style.display = 'inline';
		getdatafrompages(reM,'./patient_list.php?action=5');
	}
	//alert(date);
}


/*==============================================================================
definition: View the archived time sheets for a specific date
==============================================================================*/
function timesheetView(reM,date)
{
	//show archive list
	if (date != 'none')
	{
		document.getElementById('archiveList').style.display = 'inline';
		document.getElementById('dateList').style.display = 'none';
		getdatafrompages(reM,'./process_timesheet.php?action=1&date='+date);
	}
	//show date list
	else
	{
		document.getElementById('archiveList').style.display = 'none';
		document.getElementById('dateList').style.display = 'inline';
	}
}