var staffBlockIDCounter = 1;
var staffBlockCounterMax = 20;

//Open window function
function openWin( windowURL, windowName, windowFeatures ) { 
	return window.open( windowURL, windowName, windowFeatures ) ; 
} 

function printPage(){
	if (window.print) window.print();
	else alert("Your web browser does not support this function.n\Use the print option located under the File menu to print this page.");
	}

function submitSortForm(sortTypeArg, sortDirArg){
	var pageWrapperDiv = document.getElementById("pageWrapper");
	var sortForm = document.createElement('form');	
	sortForm.action = '/counselor/applicant_inquiry/app_inquiry_handler.cfm';
	sortForm.method = 'post';
	pageWrapperDiv.appendChild(sortForm);
	var sortType = document.createElement('input');
	sortType.setAttribute('type', 'hidden');
	sortType.setAttribute('name', 'sort');
	sortType.setAttribute('value', sortTypeArg);
	sortForm.appendChild(sortType);	
	var sortDir = document.createElement('input');
	sortDir.setAttribute('type', 'hidden');
	sortDir.setAttribute('name', 'sortdir');
	sortDir.setAttribute('value', sortDirArg);
	sortForm.appendChild(sortDir);	
	sortForm.submit();
}
	
function displayAppSearch(searchType){
	document.getElementById('searchTerm').value = '';
	document.getElementById('SearchTermField').style.display = 'none';
	document.getElementById('statusField').style.display = 'none';
	document.getElementById('SearchButton').style.display = 'none';
	
	if ((searchType == 'Applicant ID') || (searchType == 'Last Name')) {
		document.getElementById('SearchTermField').style.display = 'block';
		document.getElementById('SearchButton').style.display = 'block';
	}
	else if (searchType == 'Current Status') {
		document.getElementById('statusField').style.display = 'block';
		document.getElementById('SearchButton').style.display = 'block';
	}

}	


function toggleSearchForm(linkObj, contID){
	var currentElement = document.getElementById(contID);

	if (currentElement.style.display =='block'){
		currentElement.style.display = 'none';
		linkObj.innerHTML = 'Show Search Options';
	}
	else {
		currentElement.style.display = 'block';
		linkObj.innerHTML = 'Hide Search Options';
	}
	
}

function toggleSearch(searchType){ 
	var lastNameSearch = document.getElementById('lastNameBox');
}

function validateSearchForm(theForm){
	
	if (theForm.searchType.value.length == 0){
		alert("You must select a search type.");
		theForm.searchType.focus();
		return false;
	}
	else if ((theForm.searchType.value == 'Applicant ID') || (theForm.searchType.value == 'Last Name'))  {
		if (theForm.searchTerm.value.length == 0){
		alert("You must enter a search term.");
		theForm.searchTerm.focus();
		return false;
		}
	}
	else if (theForm.searchType.value == 'Current Status') {
		if (theForm.statusType.value.length == 0){
		alert("You must select a status type to search on.");
		theForm.statusType.focus();
		return false;
		}	
	}
		
	return true;
}

function postRangeForm(){
		var message = 'Refreshing applicant view.  Please wait...';
		var width = 370;
		var height = 50;
		refreshMessage(message, width, height); 
		document.viewForm.submit();
	
	}

function loadStateCountyDropDown(stateID, countyID){
	
	var countyDiv = document.getElementById('countyField');
	
	if (stateID == ''){
		countyDiv.innerHTML = '<em>Select a state first.</em>';
	}
	else{
		countyDiv.innerHTML = '<img src="/counselor/images/loading.gif" align"left"><em>Loading counties...</em>';
		var xmlHttp = getHTTPObject();
			if (xmlHttp) 
			{
				xmlHttp.onreadystatechange = function()
					{
					if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
						{
							var obj = xmlHttp.responseXML;
							var countyIDArray = obj.getElementsByTagName('countyId');	
							var countyNameArray = obj.getElementsByTagName('countyName');
							var countySelect = document.createElement('select');
							countySelect.setAttribute('name', 'HsCounty');
							var newOption = document.createElement('option');
							var newOption = document.createElement('option');							
							newOption.setAttribute('value', '');
							newOption.innerHTML = 'Select One';
							countySelect.appendChild(newOption);
								
							for (i = 0; i <= countyIDArray.length - 1; i++){
								var newOption = document.createElement('option');
								newOption.setAttribute('value', countyIDArray[i].firstChild.data);
								if (countyID != ''){
										if (countyID == countyIDArray[i].firstChild.data){
										newOption.setAttribute('selected', 'selected');
										}
									}
								newOption.innerHTML = countyNameArray[i].firstChild.data;
								countySelect.appendChild(newOption);	
							}
							countyDiv.innerHTML = "";
							countyDiv.appendChild(countySelect);
							
						}
					}
						sendGetRequest(xmlHttp, 'survey_get_county.cfm?stid=' + stateID +'&uq=' + new Date().getTime());
			} else {
				countyDiv.innerHTML = 'Problem loading counties.';
			}
		
		}
	
	}


function createNewStaffBlock(parentDiv) {

	var blockIDListArray = getBlockIDListArray();
	var staffBlockCounter = blockIDListArray.length + 1;
	if (staffBlockCounter > staffBlockCounterMax) {
		 alert('Currently the system allows a max of ' + staffBlockCounterMax +' staff members to be listed. \n Please contact us if you require more to be added.');
		 return false;
		}
		
	staffBlockCounter = staffBlockCounter + 1;
	staffBlockIDCounter = staffBlockIDCounter + 1 + new Date().getTime();
	blockID = staffBlockIDCounter;
	//Create fieldset block via the DOM methods
	var fieldSetBlock = document.createElement('fieldset');	
	fieldSetBlock.setAttribute('id', 'staff' + blockID);
	var staffBlock = '';
	staffBlock = staffBlock + '<table align="left">';
	staffBlock = staffBlock + '<tr>';
	staffBlock = staffBlock + '<td nowrap><span class="requiredAst">*</span>First Name:</td>';
	staffBlock = staffBlock + '<td><input name="HSCfirstName' + blockID + '" size="20" maxlength="50"></td>';
	staffBlock = staffBlock + '<td>&nbsp;</td>';
	staffBlock = staffBlock + '<td><span class="requiredAst">&nbsp;</span>Areas of Responsibility:</td>';
	staffBlock = staffBlock + '<td><input type="checkbox" name="HSCAreaOfResp' + blockID + '" value="9">9 <input type="checkbox" name="HSCAreaOfResp' + blockID + '" value="10">10 <input type="checkbox" name="HSCAreaOfResp' + blockID + '" value="11">11 <input type="checkbox" name="HSCAreaOfResp' + blockID + '" value="12">12</td>';
	staffBlock = staffBlock + '</tr><tr>';
	staffBlock = staffBlock + '<td><span class="requiredAst">*</span>Last Name:</td>';
	staffBlock = staffBlock + '<td><input name="HSClastName' + blockID + '" size="20" maxlength="50"></td>';
	staffBlock = staffBlock + '<td></td>';
	staffBlock = staffBlock + '<td><span class="requiredAst">*</span>Position:</td>';
	staffBlock = staffBlock + '<td><select name="HSCPosition' + blockID + '"><option value="">Select One</option><option value="D">Director</option><option value="C">Counselor</option><option value="S">Staff</option></select></td>';
	staffBlock = staffBlock + '</tr><tr>';
	staffBlock = staffBlock + '<td><span class="requiredAst">*</span>Email:</td>';
	staffBlock = staffBlock + '<td><input name="HSCemail' + blockID + '" size="20" maxlength="50"></td>';
	staffBlock = staffBlock + '<td></td>';
	staffBlock = staffBlock + '<td nowrap></td>';
	staffBlock = staffBlock + '<td></td>';
	staffBlock = staffBlock + '</tr>';
	staffBlock = staffBlock + '</table>';
	staffBlock = staffBlock + '<div style="clear:left; padding-top:7px; padding-left:4px;float:left; width:100%;"><span class="requiredAst">*</span>Allow access to secure areas of CounselorConnect? <input type="radio" value="1" name="HSCCounselorConnect' + blockID + '" checked>Yes&nbsp;<input type="radio" value="0" name="HSCCounselorConnect' + blockID + '">No</div>';
	
	staffBlock = staffBlock + '<div style="clear:both;padding-left:7px;padding-top:4px;padding-bottom:4px;"><input type="button" name="newBlockButton" value="Remove this staff member" onclick="deleteStaffBlock(' + blockID + ')" class="formButton" style="background-color:#AE0000; color:#ffffff; font-size:.8em;" /></div>';	
	
	fieldSetBlock.innerHTML = staffBlock;
	var mainStaffDiv = document.getElementById(parentDiv);	
	mainStaffDiv.appendChild(fieldSetBlock);	
	createStaffBlockList('add', blockID);	
}

function deleteStaffBlock(blockID){	
	var staffDiv = document.getElementById('staff' + blockID);
	var cForm = document.hsCounInfoForm;
	var firstName = trimAll(eval('cForm.HSCfirstName' + blockID + '.value'));
	var lastName = trimAll(eval('cForm.HSClastName' + blockID + '.value'));
	if ((firstName.length == 0) && (lastName.length == 0)){
		staffDiv.style.display = 'none';
		}
	else{
		staffDiv.innerHTML = '<strong>' + firstName + ' ' + lastName + ' has been removed.</strong>';
	}
	createStaffBlockList('delete', blockID);
}

function getBlockIDListArray(){
	var blockIDListArray = new Array();
	var blockIDList = document.hsCounInfoForm.staffIDList;		
	blockIDListArray = blockIDList.value.split(',');	
	return blockIDListArray;
	}

function createStaffBlockList(actionType, blockID){	
	var blockIDListArray = getBlockIDListArray();
	if (actionType == 'add'){
		blockIDListArray[blockIDListArray.length] = blockID;			
		}
	else if(actionType == 'delete'){
		for (i=0; i < blockIDListArray.length; i++){
			if (blockIDListArray[i] == blockID){
				blockIDListArray.splice(i, 1);
			}
		}
	}
	document.hsCounInfoForm.staffIDList.value = blockIDListArray;
	}
