var value_name_delimiter="::";
var entity_delimiter="##";

function locationObj (regionId, regionName, locationId, locationName){
	this.RegionId = regionId;
	this.RegionName = regionName;
	this.LocationId = locationId;
	this.LocationName = locationName;
}

function sortByRegionName(a, b) {
	var x = a.RegionName.toLowerCase();
	var y = b.RegionName.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortByLocationName(a, b) {
	var x = a.LocationName.toLowerCase();
	var y = b.LocationName.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function sortByRegionNameThenLocationName(a, b) {
	var x = a.RegionName.toLowerCase();
	var y = b.RegionName.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByLocationName(a, b)));
}

function toggle(action) {
		var c = document.getElementById("advsearch_close");
	 	var o = document.getElementById("advsearch_open");
		if (action == 'open'){
			o.style.display = "block";
			c.style.display = "none";
		} else {
			o.style.display = "none";
			c.style.display = "block";
		}
}

function addOption(addSelObj, optText, optValue, sort)
{
  var newOpt = new Option(optText, optValue);
  var selLength = addSelObj.length;
  addSelObj.options[selLength] = newOpt;
  
  if (sort){
	  var sortArray = new Array;
	  var sel = addSelObj;
		 
	  for(i=0; i<sel.length; i++)  {
		sortArray[i] = new Array;
		sortArray[i][0] = sel.options[i].text;
		sortArray[i][1] = sel.options[i].value;
	  }
	  
	  sortArray.sort();
	
	  for(i=0; i<sel.length; i++)  {
	  	sel.options[i].text = sortArray[i][0];
	    sel.options[i].value = sortArray[i][1];
	  }
  }
}

function addLocationOption(selFrom, selTo, theText, theValue)
{
  var valueForMoving = new Array();
  //alert("theValue:" + theValue);
  var theSplitValue = theValue.split(entity_delimiter);
  //check if it is the first location
  var isfirst = true;
  for(i=0; i<selTo.length; i++)  {
	if (selTo.options[i].value.indexOf(theSplitValue[0]) != -1){
		isfirst = false;
		break;
	}		
   }
  if (theSplitValue.length > 1){
  	//selected a location
     
	 if (isfirst){
	 	 valueForMoving[0] = theSplitValue[0];
		 valueForMoving[1] = theValue;
	 }
	 else{
	 	 valueForMoving[0] = theValue;
	 }	
  }
  else{
  	//selected a region
  	var j = 0;
  	for(i=0; i<selFrom.length; i++)  {
		if (selFrom.options[i].value.indexOf(theValue) != -1){
			//alert("selFrom.options[i].value:" + selFrom.options[i].value);
			var tmpSplit = selFrom.options[i].value.split(entity_delimiter);
			if (tmpSplit.length > 1 || isfirst){
				valueForMoving[j] = selFrom.options[i].value;
				j++;
			}
			
	  }
	}  
  }
  
  var locationForMoving = new Array();
  for (i=0; i<valueForMoving.length; i++){
  	theSplitValue = valueForMoving[i].split(entity_delimiter);
 	if (theSplitValue.length > 0){
 		//alert ("first theSplitValue[0]:" + theSplitValue[0]);
  		var region = theSplitValue[0].split(value_name_delimiter);
  		if (theSplitValue.length > 1){
  			//alert ("first theSplitValue[1]:" + theSplitValue[1]);
  			var location = theSplitValue[1].split(value_name_delimiter);
  			locationForMoving[locationForMoving.length++] = 
  				new locationObj (region[2], region[0] + value_name_delimiter + region[1], 
  						location[2], location[0] + value_name_delimiter + location[1]);
  		}
  		else{
  			locationForMoving[locationForMoving.length++] = 
  				new locationObj (region[2], region[0] + value_name_delimiter + region[1], "", "");
  		}
  	}
  }
  
  for(i=0; i<selTo.length; i++)  {
	if (selTo.options[i].value != '-1')
	{
		theSplitValue = selTo.options[i].value.split(entity_delimiter);
		if (theSplitValue.length > 0){
			//alert ("second theSplitValue[0]:" + theSplitValue[0]);
			var region = theSplitValue[0].split(value_name_delimiter);
			if (theSplitValue.length > 1){
				//alert ("second theSplitValue[1]:" + theSplitValue[1]);
				var location = theSplitValue[1].split(value_name_delimiter);
				locationForMoving[locationForMoving.length++] = 
					new locationObj (region[2], region[0] + value_name_delimiter + region[1], 
  							location[2], location[0] + value_name_delimiter + location[1]);
			}
			else{
				locationForMoving[locationForMoving.length++] = 
  					new locationObj (region[2], region[0] + value_name_delimiter + region[1], "", "");
			}
		}
	}
	else{
		locationForMoving[locationForMoving.length++] = new locationObj ("-1", "", "", "");
	}
  }
  
  locationForMoving.sort(sortByRegionNameThenLocationName);

  for(i=0; i<locationForMoving.length; i++)  {
  	var lobj = locationForMoving[i];
  	if (lobj.RegionId == '-1'){
  		var newOpt = new Option('', '-1');
  		selTo.options[i] = newOpt;
  	}
  	else {
	  	if (lobj.LocationName != ""){
	  		theSplitValue = lobj.LocationName.split(value_name_delimiter);
	  		//alert ("location name:" + theSplitValue[1]);
			var newOpt = new Option(theSplitValue[1], lobj.RegionName + value_name_delimiter + lobj.RegionId + entity_delimiter + lobj.LocationName + value_name_delimiter + lobj.LocationId);
	  		selTo.options[i] = newOpt;
	  	}
	  	else{
	  		theSplitValue = lobj.RegionName.split(value_name_delimiter);
	  		//alert ("region name:" + theSplitValue[1]);
			var newOpt = new Option(theSplitValue[1], lobj.RegionName + value_name_delimiter + lobj.RegionId);
	  		selTo.options[i] = newOpt;
	  	}
    }
  }
  
}

function deleteOption(deleteSelObj, indexNum, optNum) { 
  	var selLength = deleteSelObj.length;
	// if optNum is greater than -1 the originating select list is the hidden select list
	if (optNum > -1) {
		// if selLength is greater than 0 then there is an option to delete other wise don't attempt to delete
	  	if(selLength>0)
	  	{
	    	// delete the option by setting to null
			deleteSelObj.options[optNum] = null;
	  	}
	} else {// the originating select list is the visible drop down select list
		// if selLength is greater than 0 then there is an option to delete other wise don't attempt to delete
	  	if(selLength>0)
	  	{
	    	// delete the option by setting to null
			deleteSelObj.options[indexNum] = null;
	  	} else {
			return;
		}
	}
}

function deleteLocationOption(theSel, indexNum, optNum, theValue) { 
  var selLength = theSel.length;
  var theIndex = indexNum;
  if (optNum > -1){
  	theIndex = optNum;
  }
  if(selLength>0)
  {
  	//alert ("for delete:" + theValue);
	var theSplitValue = theValue.split(entity_delimiter);
	if (theSplitValue.length > 1){
	  	//selected a location
	     //check if it is the last location
	     var islast = true;
	     for(i=0; i<theSel.length; i++)  {
			if (theSel.options[i].value.indexOf(theSplitValue[0]) != -1){
				var tmpSplit = theSel.options[i].value.split(entity_delimiter);
				if (tmpSplit.length > 1 && tmpSplit[1] != theSplitValue[1]){
					islast = false;
					break;
				}
			}		
		 }
		 if (islast){
		 	for(i=theSel.length-1; i>=0; i--)  {
			if (theSel.options[i].value.indexOf(theSplitValue[0]) != -1){
				theSel.options[i] = null;
		  	}
		 	}
		 }
		 else{
		 	theSel.options[theIndex] = null;
		 }
	}
	else{
	  	//selected a region
	  	for(i=theSel.length-1; i>=0; i--)  {
			//alert ("theSel: " + theSel.options[i].value);
			if (theSel.options[i].value.indexOf(theValue) != -1){
				theSel.options[i] = null;
		  }
		}  
	}
   
  }
}

function moveMultipleOption(selFrom, selTo, sort) {
  if (selFrom.length > 0){
  	  for (i=0; i<selFrom.options.length; i++) {
	  	  if (selFrom.options[i].selected) {	
			  var sval = selFrom.options[i].value;
			  var stext = selFrom.options[i].text;	  
			  addOption(selTo, stext, sval, sort);
			}
	  }
	  for (i=selFrom.options.length-1; i>=0; i--) {
		 if (selFrom.options[i].selected) {
			deleteOption(selFrom, i);
		 }
	  }
  } else {
  	return;
  }
}


// FUNCTION moveOption notes:
// this is the main function that calls all sub functions which enable the parameter
// adding functionality
//   - selFromId is a STRING which is the name of the select list from which an option is moved
//   - selToId is a STRING which is the name of the select list to move option to
//   - listId is a STRING which is the id attribute of the div to insert list items
//   - linkId is a STRING which is the id attribute of the button used to add parameters
//   - option is a NUMBER index of the option to move from the hidden select list if the value
//       is -1 it means the option is coming from the drop down select. If 0 or a positive integer
//       the option is coming from the hidded select list represented visually by the unordered list
function moveOption(selFromId, selToId, listId, linkId, option) {
  	// get the HTML objects represented by the string parameters of the function
	var selFromObj = document.getElementsByName(selFromId)[0];
	var selToObj = document.getElementsByName(selToId)[0];
	// initialize variables
	var sval = "";
	var stext = "";
	var si = "";
	// if the move request for selFromObj is an empty select list, do not proceed
	if (selFromObj.length > 0){
		// if OPTION is greater than -1 it means that the request is to add values to the variables
		// from the selected item in the drop down list otherwise the values will come from the hidden
		// select list and will be used to delete a search parameter from a select list
		if (option > -1){
			sval = selFromObj.options[option].value;
			stext = selFromObj.options[option].text;
		} else {
			si = selFromObj.selectedIndex;
			sval = selFromObj.options[si].value;
			stext = selFromObj.options[si].text;
		}
		//alert (sval);
		if (sval != '-1') {
			// first delete the option from the originating (selFromObj) select list
			deleteOption(selFromObj, si, option);
			// second add the option to the destingation (selToObj) select list
		  	addOption(selToObj, stext, sval, true);
			// third populate the lists with the correct options
		  	populateList(selFromObj, selToObj, listId, linkId);
			// for check to see if the originating select list and add button should be disabled
			disableSel(selFromObj, selToObj, linkId);
		} else {
			selFromObj.className = "alertborder";
			alert(emptyOptionAlert);
			
		}	
  	}
}

function moveLocationOption(selFromId, selToId, listId, linkId, option) {
  	// get the HTML objects represented by the string parameters of the function
	var selFromObj = document.getElementsByName(selFromId)[0];
	var selToObj = document.getElementsByName(selToId)[0];
	// initialize variables
	var sval = "";
	var stext = "";
	var si = "";
	// if the move request for selFromObj is an empty select list, do not proceed
	if (selFromObj.length > 0){
		// if OPTION is greater than -1 it means that the request is to add values to the variables
		// from the selected item in the drop down list otherwise the values will come from the hidden
		// select list and will be used to delete a search parameter from a select list
		if (option > -1){
			sval = selFromObj.options[option].value;
			stext = selFromObj.options[option].text;
		} else {
			si = selFromObj.selectedIndex;
			sval = selFromObj.options[si].value;
			stext = selFromObj.options[si].text;
		}
		if (sval != '-1') {	
			// second add the option to the destingation (selToObj) select list
		  	addLocationOption(selFromObj, selToObj, stext, sval);
			// first delete the option from the originating (selFromObj) select list
			deleteLocationOption(selFromObj, si, option, sval);
			// third populate the lists with the correct options
		  	populateList(selFromObj, selToObj, listId, linkId);
			// for check to see if the originating select list and add button should be disabled
			disableSel(selFromObj, selToObj, linkId);
		} else {
			selFromObj.className = "alertborder";
			alert(emptyOptionAlert);
		}		
  	}
}

// FUNCTION populateList notes:
// this function was created to accomodate the visual requirement to have a
// remove link in the select list.  This fakes a select list by populating
// list items in a DIV containing a UL with a specific ID.
//   - selFromObj is the select list to move option from
//   - selToObj is the select list to move option to
//   - listID is the id for the list containing the visible list items and is the name of the hidden select list
//   - linkId is used to create the remove item links and ensure disabling styles
function populateList(selFromObj, selToObj, listId, linkId) {
	// searchType is used to print out the correct parameter labels
	var searchType = "";
	var jsFunction = "moveOption";
	var menuName = "";
	
	switch (listId) {
		case 'submittedWebFunctions':
			searchType = jobFunctionLabel;
			menuName = "selectedWebFunction";
			break;
		case 'submittedLocations':
			jsFunction = "moveLocationOption";
			searchType = locationLabel;
			menuName = "selectedLocation";
			break;
		case 'submittedLOBs':
			searchType = specialisationLabel;
			menuName = "selectedLOB";
			break;
		default:
			searchType = "";
			break;
	}
	
	var commentId = listId + "Comment";
	// Find the UL to add the selected parameters
	theList = document.getElementById("ul" + listId);
	
	// determine the originating select list to move items and populate to or
	// delete items from the selected list taking the values from the hidden select list
	if (selFromObj.name != listId){
		if (selToObj.length < 1){
			// if empty return the following comment
			theList.innerHTML = "<li>" + searchall + " " + searchType + "<\/li>";
		}
		else{
			theList.innerHTML = "";
		}
		
		for (i=0; i<selToObj.length; i++){
			theList.innerHTML = theList.innerHTML + "<li><a href=\"javascript:" + jsFunction + "(\'" + listId + "\',\'" + menuName + "\',\'" + listId + "\',\'" + linkId + "\'," + i + ")\">&lt " + remove + "</a>" + selToObj.options[i].text + "<\/li>";
			document.getElementById(commentId).style.visibility = "visible";
		}
	} else if (selFromObj.name == listId){
		if (selFromObj.length < 1){
			// if empty return the following comment
			theList.innerHTML = "<li>" + searchall + " " + searchType + "<\/li>";
			document.getElementById(commentId).style.visibility = "hidden";
		}
		else{
			theList.innerHTML = "";
		}
		for (i=0; i<selFromObj.length; i++){
			theList.innerHTML = theList.innerHTML + "<li><a href=\"javascript:" + jsFunction + "(\'" + listId + "\',\'" + menuName + "\',\'" + listId + "\',\'" + linkId + "\'," + i + ")\">&lt " + remove + "</a>" + selFromObj.options[i].text + "<\/li>";
			document.getElementById(commentId).style.visibility = "visible";
		}
	}
	//alert (theList.innerHTML);
}

// FUNCTION disableSel notes:
// This function changes styles of the drop down menu and the add button
// to disable them when the drop down menu of possible parameters is empty
//   - selFromObj is the select list to move option from
//   - selTo is the select list to move option to
//   - linkid is used to create the remove item links and ensure disabling styles
function disableSel(selFromObj, selTo, linkid) {
	// find the button used to add parameters from the drop down menu
	var addButton = document.getElementById(linkid);
	// determine which form element is requesting to move items and if the drop down menu
	// and add button should be disabled
	if (selFromObj.multiple == false && selFromObj.length == 1) {
		selFromObj.disabled = true;
		addButton.disabled = true;
		addButton.className = "disabled";
	} else if (selFromObj.multiple == true && selTo.length > 1){
		selTo.disabled = false;
		addButton.disabled = false;
		addButton.className = "add";
	}
}

// FUNCTION resetFrom notes:
// this is combines a javascript call to reset all form elements such as checkboxes and text inputs
// and also restores the select lists to their original states
function resetForm() {
	// reset the form elements not affected by other javascripts
	//document.jobSearchForm.reset();
	// count the submittedWebFunctions hidden select list options
	var jobcount = document.jobSearchForm.submittedWebFunctions.length;
	// count the submittedLocations hidden select list options
	var loccount = document.jobSearchForm.submittedLocations.length;
	// count the submittedLOBs hidden select list options
	var speccount = document.jobSearchForm.submittedLOBs.length;
	// counting backwards, move all options from the hidden submittedWebFunctions select list
	// and move them back to the visible drop down select list
	while (jobcount>0){
		moveOption('submittedWebFunctions','selectedWebFunction','submittedWebFunctions','jobselectadd',jobcount-1);
		jobcount = jobcount - 1;
	}
	// counting backwards, move all options from the hidden submittedLocations select list
	// and move them back to the visible drop down select list
	while (loccount>0){
		//alert (document.jobSearchForm.submittedLocations.options[loccount-1].value);
		//we only need to remove all the regions cause it will carry all the cities.
		if (document.jobSearchForm.submittedLocations.options[loccount-1].value.split(entity_delimiter).length <= 1)
		{
			moveLocationOption('submittedLocations','selectedLocation','submittedLocations','locselectadd',loccount-1);
		}
		loccount = loccount - 1;
	}
	// counting backwards, move all options from the hidden submittedLOBs select list
	// and move them back to the visible drop down select list
	while (speccount>0){
		moveOption('submittedLOBs','selectedLOB','submittedLOBs','specselectadd',speccount-1);
		speccount = speccount - 1;
	}
	
	document.jobSearchForm.keywords.value = '';
	
	var jobTypes = document.jobSearchForm.submittedJobTypes;
	for (i = 0; i < jobTypes.length; i++)
		jobTypes[i].checked = true ;

}

function moveSelected (select, down) {
	
	if (select.selectedIndex != -1) {
		var si = select.selectedIndex;
		var sval = select.options[si].value;
	    var stext = select.options[si].text;
		if (down) {
			if (select.selectedIndex != select.options.length - 1){
				select.options[si].value=select.options[si+1].value;
				select.options[si].text=select.options[si+1].text;
				select.options[si].selected=false;
				select.options[si+1].value=sval;
				select.options[si+1].text=stext;
				select.options[si+1].selected=true;
			}
			else{
				return;
			}
		}
		else {
			if (select.selectedIndex != 0){
				select.options[si].value=select.options[si-1].value;
				select.options[si].text=select.options[si-1].text;
				select.options[si].selected=false;
				select.options[si-1].value=sval;
				select.options[si-1].text=stext;
				select.options[si-1].selected=true;
			}
			else{
				return;
			}
		}
}
}

var divArray = new Array;
var imgArray = new Array;
divArray = this.document.getElementsByTagName('div');
imgArray = this.document.getElementsByTagName('img');

function toggleJob(div, action) {
	var teaserDiv = "teaser" + div;
	if (action == "close"){
		document.getElementById(teaserDiv).style.display = "none";
	} else if (action == "open") {
		document.getElementById(teaserDiv).style.display = "block";
	}
	clickedOpenClose=1;
}

function openJobs(){
	for (i=0; i<imgArray.length; i++){
		if (imgArray[i].className == 'close job') {
			imgArray[i].style.display = 'inline';
		} else if (imgArray[i].className == 'open job') {
			imgArray[i].style.display = 'none';
		}
	}
	for (i=0; i<divArray.length; i++){
		if (divArray[i].className == 'openclose') {
			divArray[i].style.display = 'block';
		}
	}
}
function closeJobs(){
	for (i=0; i<imgArray.length; i++){
		if (imgArray[i].className == 'close job') {
			imgArray[i].style.display = 'none';
		} else if (imgArray[i].className == 'open job') {
			imgArray[i].style.display = 'inline';
		}
	}
	for (i=0; i<divArray.length; i++){
		if (divArray[i].className == 'openclose') {
			divArray[i].style.display = 'none';
		}
	}
}
