var err_count = 1;

function displayErrors(str) {
	if (err_count==1) {
		document.getElementById("errors").innerHTML = "Error: <b>There were submission errors on the page:</b> <br>" + str.replace(/~l/g,"&lt;").replace(/!e/g,"&gt;") + "<br>"; err_count = 0;
	}
	else {
		document.getElementById("errors").innerHTML = "Error: <b>There were submission errors on the page.</b><br><br>"; err_count = 1;
	}
} /*displays the errors on forms that have post errors*/

function toogleElementDisplay(currentElement)
{	
	if (document.getElementById)	
		var currentElementStyles = document.getElementById(currentElement);
	else
		if (document.all)
			var currentElementStyles = document.all[currentElement];
		else
			var currentElementStyles = new Object();
	
	if (currentElementStyles.style) 
		currentElementStyles = currentElementStyles.style;	
	
	if (currentElementStyles.display=="none")
		currentElementStyles.display = "inline";
	else
		currentElementStyles.display = "none";
} /*hides or displays a current element based on its current display style*/

function toogleCurrentElementDisplay(currentElement, option)
{	
	if (document.getElementById)	
		var currentElementStyles = document.getElementById(currentElement);
	else
		if (document.all)
			var currentElementStyles = document.all[currentElement];
		else
			var currentElementStyles = new Object();
	
	if (currentElementStyles.style) 
		currentElementStyles = currentElementStyles.style;	
	
	if (option=="1")
		currentElementStyles.display = "inline";
	else
		currentElementStyles.display = "none";
} /*hides or displays a current element based on its current display style*/

function toogleCurrentElementWidth(currentElement, width)
{	
	if (document.getElementById)	
		var currentElementStyles = document.getElementById(currentElement);
	else
		if (document.all)
			var currentElementStyles = document.all[currentElement];
		else
			var currentElementStyles = new Object();
	
	if (currentElementStyles.style) 
		currentElementStyles = currentElementStyles.style;	
	
	currentElementStyles.width = width;
} /*hides or displays a current element based on its current display style*/

function startDateInsertion(str)
{
	window.opener.document.inputform.start_date.value = str;
	window.opener.document.inputform.start_limit.checked = false;
} /*insert start date into the parent form*/

function endDateInsertion(str)
{
	window.opener.document.inputform.end_date.value = str;
	window.opener.document.inputform.end_limit.checked = false;
} /*insert end date into the parent form*/

function remindDateInsertion(str)
{
	window.opener.document.inputform.remind_date.value = str;
	window.opener.document.inputform.remind_limit.checked = false;
} /*insert remind date into the parent form*/

function closeDateInsertion(str)
{
	window.opener.document.inputform.close_date.value = str;
	window.opener.document.inputform.close_limit.checked = false;
} /*insert close date into the parent form*/

function toggleStartDateInput()
{
	if (document.inputform.start_limit.checked==true)
		document.inputform.start_date.value = "";
} /*toggles the display in the start date field*/

function toggleEndDateInput()
{
	if (document.inputform.end_limit.checked==true)
		document.inputform.end_date.value = "";
} /*toggles the display in the end date field*/

function toggleRemindDateInput()
{
	if (document.inputform.remind_limit.checked==true)
		document.inputform.remind_date.value = "";
} /*toggles the display in the remind date field*/

function toggleCloseDateInput()
{
	if (document.inputform.close_limit.checked==true)
		document.inputform.close_date.value = "";
} /*toggles the display in the remind date field*/

function checkTimeSelections()
{
	if (document.forms["inputform"].all_day.checked==true)
	{
		for (i = 0; i < document.forms["inputform"].start_time.length; i++) 
	  		if (document.forms["inputform"].start_time.options[i].value=="--")
			{
				document.forms["inputform"].start_time.selectedIndex = i;
				break; 
			} 
		
		for (i = 0; i < document.forms["inputform"].start_minutes.length; i++) 
	  		if (document.forms["inputform"].start_minutes.options[i].value=="--")
			{
				document.forms["inputform"].start_minutes.selectedIndex = i;
				break; 
			} 
		
		for (i = 0; i < document.forms["inputform"].end_time.length; i++) 
	  		if (document.forms["inputform"].end_time.options[i].value=="--")
			{
				document.forms["inputform"].end_time.selectedIndex = i;
				break; 
			} 

		for (i = 0; i < document.forms["inputform"].end_minutes.length; i++) 
	  		if (document.forms["inputform"].end_minutes.options[i].value=="--")
			{
				document.forms["inputform"].end_minutes.selectedIndex = i;
				break; 
			} 
	}
} /*resets the start time, start minutes, end time, and end minutes to -- for an all day event*/

function GetXmlHttpObject()
{
	var xmlHttp = null;
	try {
  		xmlHttp = new XMLHttpRequest();
	} catch (e) {  
  		try {
    		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  		} catch (e) {
    		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
  	}
	return(xmlHttp);
} /*used to detect if the current browser supports the XMLHttpRequest object (used for Ajax functionality)*/

function loadCompanyDepartments(p_id, select_list)
{
	c_id = document.forms['inputform'].c_id.options[document.forms['inputform'].c_id.selectedIndex].value;
	if (c_id==0)
		return;
		
  	var xmlHttp;
	xmlHttp = GetXmlHttpObject();
		
	if (xmlHttp!=null)
	{
	 	document.getElementById("department_selection").innerHTML = "<b>loading...</b>";
		
		xmlHttp.onreadystatechange = function()
	    {
			if (xmlHttp.readyState==4)
				document.getElementById("department_selection").innerHTML = xmlHttp.responseText;
		}
			
	    xmlHttp.open("GET","_includes/ajax_department_select_field.php?c_id=" +c_id +"&p_id=" +p_id +"&select_list=" +select_list,true);
	    xmlHttp.send(null);			
	}
	else
		top.location = "../../error.php";
} /*loads the department select field with the current company's information. Ajax functionality encompassed in _includes/ajax_department_select_field.php*/

function loadDepartmentEmployeesCheckboxes() 
{
	p_id = document.forms['inputform'].p_id.options[document.forms['inputform'].p_id.selectedIndex].value;
		
  	var xmlHttp;
	xmlHttp = GetXmlHttpObject();
		
	if (xmlHttp!=null)
	{
	 	document.getElementById("employees_list").innerHTML = "<b>loading...</b>";
		
		xmlHttp.onreadystatechange = function()
	    {
			if (xmlHttp.readyState==4)
				document.getElementById("employees_list").innerHTML = xmlHttp.responseText;
		}
			
	    xmlHttp.open("GET","_includes/ajax_employees_multiple_list.php?p_id=" +p_id,true);
	    xmlHttp.send(null);			
	}
	else
		top.location = "../../error.php";
} /*loads the employees checkbox list with the current department's information. Ajax functionality encompassed in _includes/ajax_employees_multiple_list.php*/

function loadEmployeeNameRetrieval(e_id)
{
	var xmlHttp;
	xmlHttp = GetXmlHttpObject();
		
	if (xmlHttp!=null)
	{
		xmlHttp.onreadystatechange = function()
	    {
			if (xmlHttp.readyState==4)
				implementInviteeInformation(xmlHttp.responseText.replace(/^\s+|\s+$/g, ''),e_id);
		}
			
	    xmlHttp.open("GET","_includes/ajax_employees_name_retrieval.php?e_id=" +e_id,true);
	    xmlHttp.send(null);			
	}
	else
		top.location = "../../error.php";
} /*loads the employee name from the current employee's information. Ajax functionality encompassed in _includes/ajax_employees_name_retrieval.php*/

function loadCalendarEventsList(date, url,type)
{
	var xmlHttp;
	xmlHttp = GetXmlHttpObject();
		
	if (xmlHttp!=null)
	{
		document.getElementById("events_list").innerHTML = "<b>loading...</b>";
		
		xmlHttp.onreadystatechange = function()
	    {
			if (xmlHttp.readyState==4)
				document.getElementById("events_list").innerHTML = xmlHttp.responseText;
		}
		if (type==1)
	    	xmlHttp.open("GET","admin/_modules/calendar/_includes/ajax_calendar_events_list.php?date=" +date +"&url=" +url +"&type=1",true);
	    else
			xmlHttp.open("GET","_includes/ajax_calendar_events_list.php?date=" +date +"&url=" +url,true);
		xmlHttp.send(null);			
	}
	else
		top.location = "../../error.php";
} /*loads the employee name from the current employee's information. Ajax functionality encompassed in _includes/ajax_calendar_events_list.php*/

function removeChildInformation(parent_id)
{
	var owner = window.document;
	owner.getElementById('invitees_list').removeChild(parent_id);
	
	var node_count = owner.getElementById("invitees_list").childNodes.length;
	return false;
} /*remove a child node from a parent object*/

function implementInviteeInformation(employee_name, employee_id)
{
	var owner = window.document;
	var node_count = owner.getElementById("invitees_list").childNodes.length;
	var employee_parent = owner.createElement("div");
	var parent_id = "employee_" + node_count++;
	
	var label_employee = owner.createTextNode(employee_name);	
	var button_remove = owner.createElement("span"); 
	button_remove.innerHTML = "<input type=button onclick=\"removeChildInformation(" + parent_id + ")\" value=\" remove \" style=\"height: 17px; margin-right: 8px;\"/>";

	var employee_employee_id = owner.createElement("input");
	
	employee_parent.id = parent_id;
	employee_parent.style.marginBottom = "2px";
	owner.getElementById("invitees_list").appendChild(employee_parent);
	
	employee_employee_id.type = "hidden";
	employee_employee_id.name =  parent_id + "_employee_id";
	employee_employee_id.value = employee_id;
	
	owner.getElementById(parent_id).appendChild(button_remove);
	owner.getElementById(parent_id).appendChild(label_employee);
	owner.getElementById(parent_id).appendChild(employee_employee_id);
	
	if (node_count>0)
	{
		toogleCurrentElementDisplay('invitee_title','1');
		toogleCurrentElementDisplay('invitee_action','1');
	} /*displays the invitee title and submit button on the multiple invitees form*/
} /*adds the invitee information to the invitee list*/

function generateCurrentReport(number, caller, event)
{
	if (caller==1)
		q = document.getElementById("report_generation").value;
	
  	var xmlHttp;
	xmlHttp = GetXmlHttpObject();
		
	if (xmlHttp!=null)
	{
		xmlHttp.onreadystatechange = function()
	    {
			if (xmlHttp.readyState==4)
			{
				var page_output = new String(xmlHttp.responseText.replace(/^\s+|\s+$/g,''));
				//document.getElementById("output").innerHTML = page_output;
				if (page_output!="false")
				{
					download_window = window.open("calendar_reports_download.php?id=" +page_output,"download_window"); 
					download_window.focus();
				}
			}
		}
		if (caller==1)
	  		xmlHttp.open("GET","_includes/ajax_report_creation.php?number=" +number +"&q=" +q,true);
		else
			xmlHttp.open("GET","_includes/ajax_report_creation.php?number=" +number +"&event=" +event,true);
	    xmlHttp.send(null);		
	}
	else
		top.location = "../../error.php";
} /*generates the report of the given report number.  Ajax functionality encompassed in _includes/ajax_report_creation.php*/