var _divisionID = '';
var adminFlag = false;
var collectionHTML = new Array();

var moz = true;
if (typeof document.all == 'object')
{
	moz = false;
}

function CloseScriptTag()
{
	return "<" + "/script" + ">";
}

function Close_Popup(e)
{
	if (document.getElementById('testpopup'))
	{
		var div = document.getElementById('testpopup');
		while (div.firstChild)
		{
			div.removeChild(div.firstChild);
		}
	}
}

function InitializeDivisions(divisionID)
{
	var selCompany = document.forms[0].company;
	var selDivision = document.getElementById('division');
	_divisionID = divisionID;
	selCompany.onchange = ChangeCompany;
	if (selCompany.value != '')
	{
		var url = "/portal/divisions.php";
		var pars = "id=" + selCompany.value;
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: pars,
				onComplete: PopulateDivisions
			}
		);
	}
}

function ChangeCompany(e)
{
	if (moz) var sel = e.target;
		else var sel = window.event.srcElement;

	var companyID = sel.value;
	if (companyID != '')
	{
		var url = "/portal/divisions.php";
		var pars = "id=" + companyID;
		var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: pars,
				onComplete: PopulateDivisions
			}
		);
	}
	else
	{
		ResetDivisions();
	}
}

function PopulateDivisions(xmlRequest)
{
	ResetDivisions();
	
	var selDivision = document.getElementById('division');
	
	var xmlDoc = xmlRequest.responseXML;
	var parentTag = xmlDoc.getElementsByTagName('company_divisions');
	var divisions = parentTag[0].getElementsByTagName('division');
	if (divisions.length == 0)
	{
		selDivision.options[0].text = 'No Divisions available';
	}
	else
	{
		selDivision.disabled = false;
		
		for (var i = 0; i < divisions.length; i++)
		{
			var opt = document.createElement('option');
			opt.text = divisions[i].firstChild.nodeValue;
			opt.value = divisions[i].getAttribute('id');
			if (_divisionID == opt.value) opt.selected = true;
			selDivision.options.add(opt);
		}

		selDivision.options[0].text = 'No Division selected';
		
		selDivision.value = _divisionID;
		_divisionID = '';
	}
}

function ResetDivisions()
{
	var selDivision = document.getElementById('division');
	selDivision.options.length = 0;
	var opt = document.createElement('option');
	opt.text = 'Please select a company.';
	opt.value = '';
	selDivision.options.add(opt);
	selDivision.disabled = true;
}

function ViewTest(_id)
{
	var url = "/portal/test.php";
	var pars = "id=" + _id;
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: pars,
			onComplete: TestPopup
		}
	);
}

function TestPopup(xmlRequest)
{
	var div = document.getElementById("testpopup");
	while (div.firstChild)
	{
		div.removeChild(div.firstChild);
	}
	
	var xmlDoc = xmlRequest.responseXML;
	var testView = xmlDoc.getElementsByTagName("testview")[0];
	var _id = testView.getAttribute("id");
	var division = testView.getElementsByTagName("division")[0];
	var company = division.getElementsByTagName("company")[0];
		
	var testDiv = document.createElement("div");
	div.id = "testpopup";
	div.className = "test-popup";

	var lblTitle = document.createElement("label");
	if (moz) lblTitle.textContent = "MIHS Test Details";
		else lblTitle.innerText = "MIHS Test Details";
	div.appendChild(lblTitle);
	
	var btnClose = document.createElement("button");
	if (moz) btnClose.textContent = "X";
		else btnClose.innerText = "X";
	btnClose.className = "test-close-button";
	btnClose.onclick = Close_Popup;
	div.appendChild(btnClose);
	
	div.appendChild(document.createElement("br"));
	
	var lblDivision = document.createElement("label");
	if (moz) lblDivision.textContent = "Division: ";
		else lblDivision.innerText = "Division: ";
	div.appendChild(lblDivision);
	
	var Division = document.createElement("span");
	if (moz) Division.textContent = division.getElementsByTagName('name')[0].firstChild.nodeValue;
		else Division.innerText = division.getElementsByTagName('name')[0].firstChild.nodeValue;
	div.appendChild(Division);
	
	div.appendChild(document.createElement("br"));
	
	var lblEmployer = document.createElement('label');
	if (moz) lblEmployer.textContent = "Employer: ";
		else lblEmployer.innerText = "Employer: ";
	div.appendChild(lblEmployer);
	
	var Employer = document.createElement("span");
	if (moz) Employer.textContent = company.getElementsByTagName('name')[0].firstChild.nodeValue;
		else Employer.innerText = company.getElementsByTagName('name')[0].firstChild.nodeValue;
	div.appendChild(Employer);
	
	div.appendChild(document.createElement("br"));
	
	if (company.getElementsByTagName('address')[0].firstChild)
	{
		var lblAddress = document.createElement("label");
		if (moz) lblAddress.textContent = "Address: ";
			else lblAddress.innerText = "Address: ";
		div.appendChild(lblAddress);
		
		var Address = document.createElement("span");
		if (moz) Address.textContent = company.getElementsByTagName('address')[0].firstChild.nodeValue;
			else Address.innerText = company.getElementsByTagName('address')[0].firstChild.nodeValue;
		div.appendChild(Address);
		
		div.appendChild(document.createElement("br"));
	}
	
	var lblContact = document.createElement("contact");
	if (moz) lblContact.textContent = "Contact: ";
		else lblContact.innerText = "Contact: ";
	div.appendChild(lblContact);
	
	var Contact = document.createElement("span");

	if (company.getElementsByTagName('contact_last_name')[0].firstChild && company.getElementsByTagName('contact_first_name')[0].firstChild)
	{
		var ContactName = document.createElement("span");
		if (moz) ContactName.textContent = (company.getElementsByTagName("contact_last_name")[0].firstChild.nodeValue + ", " + company.getElementsByTagName("contact_first_name")[0].firstChild.nodeValue);
			else ContactName.innerText = (company.getElementsByTagName("contact_last_name")[0].firstChild.nodeValue + ", " + company.getElementsByTagName("contact_first_name")[0].firstChild.nodeValue);
		Contact.appendChild(ContactName);
		
		Contact.appendChild(document.createElement("br"));
	}
	
	if (company.getElementsByTagName('contact_phone')[0].firstChild)
	{
		var ContactPhone = document.createElement("span");
		if (moz) ContactPhone.textContent = company.getElementsByTagName("contact_phone")[0].firstChild.nodeValue;
			else ContactPhone.innerText = company.getElementsByTagName("contact_phone")[0].firstChild.nodeValue;
		Contact.appendChild(ContactPhone);
		
		Contact.appendChild(document.createElement("br"));
	}
	
	if (company.getElementsByTagName('contact_email')[0].firstChild)
	{
		var ContactEmail = document.createElement("a");
		if (moz) ContactEmail.textContent = company.getElementsByTagName("contact_email")[0].firstChild.nodeValue;
			else ContactEmail.innerText = company.getElementsByTagName("contact_email")[0].firstChild.nodeValue;
		ContactEmail.href = "mailto:" + company.getElementsByTagName("contact_email")[0].firstChild.nodeValue;
		Contact.appendChild(ContactEmail);
		
		div.appendChild(Contact);

		div.appendChild(document.createElement("br"));
	}
	
	var lblEmployee = document.createElement("label");
	if (moz) lblEmployee.textContent = "Employee: ";
		else lblEmployee.innerText = "Employee: ";
	div.appendChild(lblEmployee);
	
	var Employee = document.createElement("span");
	if (moz) Employee.textContent = testView.getElementsByTagName("employee")[0].firstChild.nodeValue;
		else Employee.innerText = testView.getElementsByTagName("employee")[0].firstChild.nodeValue;
	div.appendChild(Employee);
	
	div.appendChild(document.createElement("br"));
	
	if (testView.getElementsByTagName('reason')[0].firstChild)
	{
		var lblReason = document.createElement("label");
		if (moz) lblReason.textContent = "Reason for test: ";
			else lblReason.innerText = "Reason for test: ";
		div.appendChild(lblReason);
		
		var Reason = document.createElement("span");
		if (moz) Reason.textContent = testView.getElementsByTagName("reason")[0].firstChild.nodeValue;
			else Reason.innerText = testView.getElementsByTagName("reason")[0].firstChild.nodeValue;
		div.appendChild(Reason);
		
		div.appendChild(document.createElement("br"));
	}
	
	if (testView.getElementsByTagName('date')[0].firstChild)
	{
		var lblDate = document.createElement("label");
		if (moz) lblDate.textContent = "Date of test: ";
			else lblDate.innerText = "Date of test: ";
		div.appendChild(lblDate);
		
		var _Date = document.createElement("span");
		if (moz) _Date.textContent = testView.getElementsByTagName("date")[0].firstChild.nodeValue;
			else _Date.innerText = testView.getElementsByTagName("date")[0].firstChild.nodeValue;
		div.appendChild(_Date);
		
		div.appendChild(document.createElement("br"));
	}
	
	if (testView.getElementsByTagName('mihs_id')[0].firstChild)
	{
		var lblMIHS = document.createElement("label");
		if (moz) lblMIHS.textContent = "MIHS ID: ";
			else lblMIHS.innerText = "MIHS ID: ";
		div.appendChild(lblMIHS);
		
		var MIHS_ID = document.createElement("span");
		if (moz) MIHS_ID.textContent = testView.getElementsByTagName("mihs_id")[0].firstChild.nodeValue;
			else MIHS_ID.innerText = testView.getElementsByTagName("mihs_id")[0].firstChild.nodeValue;
		div.appendChild(MIHS_ID);
		
		div.appendChild(document.createElement("br"));
	}
	
	if (testView.getElementsByTagName('drug_result')[0].firstChild)
	{
		var lblDrugResult = document.createElement("label");
		if (moz) lblDrugResult.textContent = "Drug test result: ";
			else lblDrugResult.innerText = "Drug test result: ";
		div.appendChild(lblDrugResult);
		
		var DrugResult = document.createElement("span");
		if (moz) DrugResult.textContent = testView.getElementsByTagName("drug_result")[0].firstChild.nodeValue;
			else DrugResult.innerText = testView.getElementsByTagName("drug_result")[0].firstChild.nodeValue;
		div.appendChild(DrugResult);
		
		div.appendChild(document.createElement("br"));
	}
	
	if (testView.getElementsByTagName('alcohol_result')[0].firstChild)
	{
		var lblAlcoholResult = document.createElement("label");
		if (moz) lblAlcoholResult.textContent = "Alcohol test result: ";
			else lblAlcoholResult.innerText = "Alcohol test result: ";
		div.appendChild(lblAlcoholResult);
		
		var AlcoholResult = document.createElement("span");
		if (moz) AlcoholResult.textContent = testView.getElementsByTagName("alcohol_result")[0].firstChild.nodeValue;
			else AlcoholResult.innerText = testView.getElementsByTagName("alcohol_result")[0].firstChild.nodeValue;
		div.appendChild(AlcoholResult);
	
		div.appendChild(document.createElement("br"));
	}
	
	if (adminFlag == true)
	{
		div.appendChild(document.createElement("br"));
		
		var lnkTest = document.createElement("a");
		if (moz) lnkTest.textContent = "Edit test";
			else lnkTest.innerText = "Edit test";
		lnkTest.href = "test_edit.php?id=" + _id;
		div.appendChild(lnkTest);
	}
}

function Collection_Delete(e, _name)
{
	if (moz) var lnk = e.target;
		else var lnk = window.event.srcElement;

	if (confirm('Are you sure you wish to delete this ' + _name + '?'))
	{
		lnk.parentNode.parentNode.removeChild(lnk.parentNode);
	}
}

function Collection_Add(e, _id)
{
	var div = document.getElementById(_id + '-collection');
	var fieldset = document.createElement('fieldset');
	fieldset.innerHTML = collectionHTML[_id];
	div.appendChild(fieldset);
}
