/* Fonctions permettant de manipuler les checkbox (jobstreamer, jobbasket) */

	var sources = new Array();
	var sectors = new Array();
	var selectSectorCode;
	var selectSourceCode;

// Initialisation du moteur de recherche
function init(metier, fonction, localisation, typecontrat, motclef, minsalary, maxsalary)
{
  selectSingleParam('document.forms.jobsearchform.selectMetier', metier);
  unSelectAll('document.forms.jobsearchform.selectFonction');
  selectSingleParam('document.forms.jobsearchform.selectFonction', fonction);
  unSelectAll('document.forms.jobsearchform.selectLocalisation');
  selectSingleParam('document.forms.jobsearchform.selectLocalisation', localisation);
  selectSingleParam('document.forms.jobsearchform.selectContratType', typecontrat);
  document.forms.jobsearchform.motclef.value = motclef;
  selectSingleParam('document.forms.jobsearchform.minSalary', minsalary);
  selectSingleParam('document.forms.jobsearchform.maxSalary', maxsalary);
}


function verifAllChecked(formName, checkboxName)
{
  var checkboxElement = eval('document.forms.' + formName + '.checkedAll');
  if (checkboxElement.checked)
  {
    checkAll(formName, checkboxName, true);
  }
  else
  {
    checkAll(formName, checkboxName, false);
  }
}

function checkAll(formName, checkboxName, bcheckedAll)
{
  var checkboxElement = eval('document.forms.' + formName + '.' + checkboxName);
  for (i = 0; i < checkboxElement.length; i++)
  {
    checkboxElement[i].checked = bcheckedAll;
  }
}

function verifSingleChecked(formName, checkboxName, element)
{
  var checkboxElement = eval('document.forms.' + formName + '.' + checkboxName);
  var checkboxAllElement = eval('document.forms.' + formName + '.checkedAll');
  if (element.checked)
  {
    var bAllChecked = true;
    for (i = 0; i < checkboxElement.length; i++)
    {
      if (!checkboxElement[i].checked)
      {
	bAllChecked = false;
      }
    }

    if (bAllChecked)
    {
      checkboxAllElement.checked = true;
    }
  }
  else
  {
    if (checkboxAllElement.checked)
    {
      checkboxAllElement.checked = false;
    }
  }
}


function verifyAllIndexedChecked(formName, checkboxName, checkAllCheckbox)
{
  var checkboxElement = eval('document.forms.' + formName + '.' + 
  	checkAllCheckbox.name);

    checkAllIndexed(formName, checkboxName, checkboxElement.checked);
}

function checkAllIndexed(formName, checkboxName, bcheckedAll)
{
	var element = eval('document.forms.' + formName);
	
	for (var i = 0; i < element.elements.length; i++)
      {
            var e = element.elements[i];
            if (e.type == "checkbox" && e.name.indexOf(checkboxName) >= 0)
            {
                  e.checked = bcheckedAll;
            }
      }
}


/* Liste des pays */
function setDisplayBlock(id)
{
  if (document.getElementById)
  {
    element = document.getElementById(id);
  }
  else if (document.all)
  {
    element = document.all[id];
  }
  else return;

  if (element.style)
  {
    if (element.style.display != 'block' )
    {
      element.style.display = 'block';
    }

  }
}
function setDisplayNone(id)
{
  if (document.getElementById)
  {
    element = document.getElementById(id);
  }
  else if (document.all)
  {
    element = document.all[id];
  }
  else return;

  if (element.style)
  {
    if (element.style.display != 'none' )
    {
      element.style.display = 'none';
    }

  }
}

function LoadLocationList(locations)
{
  var list = $("selectLocalisation").value;
  DWRUtil.removeAllOptions("selectLocalisation");
  DWRUtil.addOptions("selectLocalisation", locations, "code", "label");
  selectSingleParam('document.forms.jobsearchform.selectLocalisation', list);
}

function getSubsectorsListWithSelectAllItemsSearch()
{
  var sector = $("selectMetier").value;
  jobsearch.getSubsectorsBySectorCode(loadSubsectorsBySectorCodeSearch, sector, true);
}

function getSubsectorsListWithSelectAllItems()
{
  var sector = $("selectSector").value;
  jobsearch.getSubsectorsBySectorCode(loadSubsectorsBySectorCode, sector, true);
}

function getSubsectorsListWithoutSelectAllItems()
{
  var sector = $("selectSector").value;
  jobsearch.getSubsectorsBySectorCode(loadSubsectorsBySectorCode, sector, false);
}


function loadSubsectorsBySectorCodeSearch(data)
{
  var fonction = $("selectFonction").value;
  removeAllOptions("selectFonction");
  addOptionsWithOptgroup("selectFonction", data, "code", "title", "groupTitle");
  selectSingleParam('document.forms.jobsearchform.selectFonction', fonction);
}

function loadSubsectorsBySectorCode(data)
{
  var subsector = $("selectSubsector").value;
  removeAllOptions("selectSubsector");
  addOptionsWithOptgroup("selectSubsector", data, "code", "title", "groupTitle");
}

function getSalaryList()
{
  var jobType = $("selectContratType").value;
  jobsearch.getMinSalaryList(loadMinSalaryList, jobType);
  jobsearch.getMaxSalaryList(loadMaxSalaryList, jobType);
}

function loadMinSalaryList(data)
{
  var minSalary = $("selectMinSalary").value;
  DWRUtil.removeAllOptions("selectMinSalary");
  DWRUtil.addOptions("selectMinSalary", data, "code", "label");
  selectSingleParam('document.forms.jobsearchform.selectMinSalary', minSalary);
}

function loadMaxSalaryList(data)
{
  var maxSalary = $("selectMaxSalary").value;
  DWRUtil.removeAllOptions("selectMaxSalary");
  DWRUtil.addOptions("selectMaxSalary", data, "code", "label");
  selectSingleParam('document.forms.jobsearchform.selectMaxSalary', maxSalary);
}

function populateSalaryCentreIndustryAndLocation()
{
  var sector = $("selectSector").value;
  jobsearch.getSalaryCentreIndustryList(loadSalaryCentreIndustryList, sector);
  jobsearch.getSalaryCentreLocationList(loadSalaryCentreLocationList, sector);
}

function loadSalaryCentreIndustryList(data)
{
  DWRUtil.removeAllOptions("selectIndustry");
  DWRUtil.addOptions("selectIndustry", data, "value", "label");
  selectSingleParam('document.forms.salaryCentreForm.selectIndustry', 'ALL');
}

function loadSalaryCentreLocationList(data)
{
  DWRUtil.removeAllOptions("selectLocation");
  DWRUtil.addOptions("selectLocation", data, "value", "label");
  
  // Select default location only if there is only one
  if (DWRUtil._isArray(data) && data.length == 2) {
	  selectSingleParam('document.forms.salaryCentreForm.selectLocation', '1');
  }
}


function getJobCount(metierElement)
{
  jobsearch.getJobCount(loadJobCount, metierElement)
}
function loadJobCount(data)
{
  DWRUtil.setValue("jobcount", data);
}

// Selectionne l'?l?ment en param?tre dans la liste d?roulante en param?tre
function selectSingleParamByElement(select, defaultValue)
{
  for (i = 0; i < select.length; i++)
  {
    if (select.options[i].value == defaultValue)
    {
      select.options[i].selected = true;
      break;
    }
  }
}


// Selectionne l'?l?ment en param?tre dans la liste d?roulante en param?tre
function selectSingleParam(selectElement, defaultValue) {
	var select = eval(selectElement);
	if (select != null) {
		for (i = 0; i < select.length; i++) {
			if (select.options[i].value == defaultValue) {
				// RT: Sometimes there is an error while trying to set selected property.
				// It seems to be one of numerous IE6 bugs.
				// This code helps to avoid that.
				if (select.options[i].selected != true) {
					while(true) 
						try {
							select.options[i].selected = true; 
							break; 
						} catch(e) {}
				}
				break;
			}
		}
	}
}

// d?selectionne tous les ?l?ments
function unSelectAll(selectElement) {
	var select = eval(selectElement);
	if (select != null) {
		for (i = 0; i < select.length; i++) {
			select.options[i].selected = false;
		}
	}
}

function hkRefresh()
{
  var country = $("selectCountry").value;
  window.location.replace(country);
}

function getSourceByCategory()
{
  	var category = $("selectCategory");
  	if (category == null) 
  		return;  // not contained on this page
  	category = category.value;
  	var sourceElement = document.getElementById("selectSource");
  	var str = sourceElement.options[0].text; 
	sourceElement.options.length=0;
	j=1;
	sourceElement.options[0]=new Option(str,"");
	for(i=0;i<sources.length;i++)
	{	
		if (sources[i][0] == category)
		{	
			sourceElement.options[j]=new Option(sources[i][3],sources[i][2]);
			j++;
		}	
	}
  selectSingleParamByElement(sourceElement, selectSourceCode);
}

function getSectorByLocation()
{
   	var location = $("selectLocation");
	if (location == null) 
  		return;  // not contained on this page
	location = location.value;
  	var sectorElement = document.getElementById("selectSector");
   	var str = sectorElement.options[0].text; 
	sectorElement.options.length=0;
	j=1;
	sectorElement.options[0]=new Option(str,"");
	for(i=0;i<sectors.length;i++)
	{	
		if (sectors[i][0] == location)
		{	
			sectorElement.options[j]=new Option(sectors[i][3],sectors[i][2]);
			j++;
		}	
	}
 // selectSingleParamByElement(sectorElement, selectSectorCode);
}

function ContactUsPageFunctions() {
//	setContactUsDropDowns();
}


function openminisite(url,modifiers) {
  window.open(url,'',modifiers);
}

function openMapWindow(url) {
  window.open(url, '', 'width=505,height=540,resizable=no,scrollbars=no');
}


function storeTrackingParams(url, ac, apsp_n, apsp_s) {
	createCookie('WT-' + url, ac + '.' + ac + '.' + apsp_n + '.' + apsp_s, null);
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	} else 
		var expires = '';
	
	document.cookie = name+'='+value+expires+'; path=/';
}


