// Only safari detection is needed because this needs special handling for forms
var agt=navigator.userAgent.toLowerCase();
var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;

function cleanAndCapitalize(text){
	text = text.toLowerCase(); 
	//êéèë 
	text = text.replace(/[\xEA\xE9\xE8\xEB]/g, "e"); 
	//âáäà 
	text = text.replace(/[\xE2\xE1\xE4\xE0]/g, "a"); 
	//îï 
	text = text.replace(/[\xEE\xEF]/g, "i"); 
	//ô 
	text = text.replace(/[\xF4]/g, "o"); 
	//ç 
	text = text.replace(/[\xE7]/g, "c"); 
	//ùûúü 
	text = text.replace(/[\xF9\xFB\xFA\xFC]/g, "u"); 
	return text.toUpperCase(); 
}

function showStatus(sMsg) {
    window.status = sMsg ;
    return true ;
}

function putaction(window, element){
	if ( is_safari ) {
		var ref = unescape(window.location.href);
		//if ( ref.indexOf("?") > 0 ) {
		//	ref = ref.substring(0,ref.indexOf("?"));
		//}
		document.getElementById(element).action = ref;
	}
}

function submitFormByHiddenField(formfield,hiddenfield,name){
	var fieldObj = document.getElementById(hiddenfield);
	fieldObj.name = name;
	fieldObj.value = 'true';
	document.getElementById(formfield).submit();
}

function submitFormByHiddenField(formfield,hiddenfield,name,value){
	var fieldObj = document.getElementById(hiddenfield);
	fieldObj.name = name;
	fieldObj.value = value;
	document.getElementById(formfield).submit();
}

function submitFormByAction(form, action) {
	var actionField = document.getElementById("action");
	actionField.value = action;
	document.getElementById(form).submit();	
}

function submitFormBySubAction(form, action, subAction) {
	var actionField = document.getElementById("action");
	actionField.value = action;
	var subActionField = document.getElementById("subAction");
	subActionField.value = subAction;
	document.getElementById(form).submit();	
}

function submitAdd(form) {
	submitFormByAction(form, "add");
}

function submitCancel(form) {
	submitFormByAction(form, "cancel");
}

function submitEdit(form) {
	submitFormByAction(form, "edit");
}



function checkBrowser(){
	if ( is_safari ) {
		alert("Please choose another browser. The administration pages are not designed to work with Safari.");
		return false;
	}else{
		return true;
	}
}

function onEnter(event, form){
	if (event && ( event.keyCode == 13 || event.which == 13) ){
    	form.submit();
  	}else{
    	return true;
    }
}

function beforeDeleteCategory(message){
	return confirm(message);
}

function beforeDelete(message){
	return confirm(message);
}

function beforeDeletePublication(message){
	return confirm(message);
}

function postAgendaForm(action,actionid, suffix){
/*	fixPostalCode(suffix);*/
	document.agendaForm.action.value = action;
	document.agendaForm.actionid.value = actionid;
	document.agendaForm.submit();
}
/*function fixPostalCode(suffix){
	var select;
	if(MM_findObj("isPostalCodeDropDown_"+suffix).value == "true"){
		select = MM_findObj("orderByPostalcode_"+suffix);
	}
	else{
		select = MM_findObj("orderByName_"+suffix);
	}
	if(select.selectedIndex != -1){
		document.agendaForm.townId.value = select.options[select.selectedIndex].value;
	}
}*/
function postAgendaDetailForm(action,actionid){
	document.agendaForm.goto.value = "detail";
	document.agendaForm.detailActionId.value = actionid;
	document.agendaForm.submit();
}

function postAgendaDetailFormAnnouncement(action,actionid){
	document.agendaForm.goto.value = "detailAnnouncement";
	document.agendaForm.detailActionId.value = actionid;
	document.agendaForm.submit();
}

function postAgendaShoppingCartForm(activityID){
	document.agendaForm.goto.value = "add";
	document.agendaForm.detailActionId.value = activityID;
	document.agendaForm.submit();
}

function postTarifficationForm(action,actioncode){
	document.tarifficationForm.action.value = action;
	document.tarifficationForm.actionCode.value = actioncode;
	document.tarifficationForm.submit();
}

function postAdminTarafficationForm(action,actionid){
	document.adminTarifficationForm.action.value = action;
	document.adminTarifficationForm.rootid.value = actionid;
	document.adminTarifficationForm.submit();
}

function postAgendaPager(page, suffix){
/*	fixPostalCode(suffix);*/
	document.agendaForm.goto.value="list";
	document.agendaForm._action.value="search";
	document.agendaForm.currentPage.value = page;
	document.agendaForm.submit();
}

function postAgendaSortOrder(sortOrderColumn,asc, suffix){
/*	fixPostalCode(suffix);*/
	document.agendaForm.sortOrderColumn.value = sortOrderColumn;
	document.agendaForm.asc.value = asc;
	document.agendaForm.goto.value = "list";
	document.agendaForm._action.value = "sort";
	document.agendaForm.submit();
}

function postAdminAgendaSortOrder(sortOrderColumn,asc){
	document.adminEventForm.changeSortOrder.value = true;
	document.adminEventForm.sortOrderColumn.value = sortOrderColumn;
	document.adminEventForm.asc.value = asc;
	document.adminEventForm.submit();
}

function postPublicationForm(action,pubId){
	document.publicationForm.action.value = action;
	document.publicationForm.publicationId.value = pubId;
//	document.publicationForm.actionid.value = actionid;
	document.publicationForm.submit();
}

function postPublicationPager(page){
	document.publicationForm.currentPage.value = page;
	document.publicationForm.submit();
}

function selectTown(townID){
	document.regionSelectionForm.townID.value = townID;
	document.regionSelectionForm._action.value = "false";
	document.regionSelectionForm.submit();
}

/**
  * General form related functions
  */
  
function linkCheckBoxToControl(checkBoxId, controlId, exclusive) {
	var checkBox = $(checkBoxId);
	var control = $(controlId);
	if (!$defined(exclusive)) {
		exclusive = true;
	}
	if (!$defined(checkBox)) { 
		alert("checkbox with id '" + checkBoxId + "' not found");			
	}
	if (!$defined(control)) {
		alert("control with id '" + controlId + "' not found");			
	}
	checkBox.onclick = function(event) {
		if (checkBox.checked == exclusive) {
			clearControl(control);
		}
	}
	control.onchange = function(event) {
		if (exclusive) {
			checkBox.checked = false;
		} else {
			checkBox.checked = !isEmpty(control);
		}
	}			
}

function linkCheckBoxToControls(checkBoxId, linkedControlId, exclusiveControlId) {
	var checkBox = $(checkBoxId);
	var linkedControl = $(linkedControlId);
	var exclusiveControl = $(exclusiveControlId);			
	if (!$defined(checkBox)) { 
		alert("checkbox with id '" + checkBoxId + "' not found");			
	}
	if (!$defined(linkedControl)) {
		alert("control with id '" + linkedControlId + "' not found");			
	}
	if (!$defined(exclusiveControl)) {
		alert("control with id '" + exclusiveControlId + "' not found");			
	}
	checkBox.onclick = function(event) {
		if (checkBox.checked) {
			clearControl(exclusiveControl);
		} else {
			clearControl(linkedControl);
		}
	}
	exclusiveControl.onchange = function(event) {
		checkBox.checked = false;
		clearControl(linkedControl);
	}
	linkedControl.onchange = function(event) {
		checkBox.checked = !isEmpty(linkedControl);
		clearControl(exclusiveControl);
	}
}
			
		
function clearControl(control) {
	if ($type(control) == "string") {
		control = $(control);
	}
	switch(control.type) {
	    case "text":
	    case "hidden": {
	      control.value = "";
	      break;
	    }
	    case "radio": {
	      break;
	    }
	    case "checkbox": {
	      control.checked = false;
	      break;
	    }
	    case "select-one": {
	      if (control.size <= 1) {
		control.selectedIndex = 0;
	      }
	      else {
		control.selectedIndex = -1;
	      }
	      break;
	    }
	    case "select-multiple": {
	      if (control.size <= 1) {
		control.selectedIndex = 0;
	      }
	      else {
		for (iOption = 0; iOption < control.options.length; iOption++) {
		  control.options[iOption].selected = false;
		}
	      }
	      break;
	    }
	    default: {
	      alert("Unsupported type: " + aForm.elements[iElement].type);
	    }
	}		
}
		
function isEmpty(control) {
  switch (control.type) {
    case "text":
    case "hidden": {
      if (control.value == "") {
	return true;
      }
      break;
    }
    case "select-one":
    case "select-multiple": {
      if (control.selectedIndex < 1) {
	return true;
      }
      break;
    }
  }
  return false;
}		
		
  
  

/**
  * Makes the layer with id 'layerId' visible according to 'isVisible'.
  */
function setVisible(element,isVisible) {
	if (element != null) {
		if (isVisible) {
			element.style.display = "block";
		} else {
			element.style.display = "none";
		}
	}
}

/**
  * Disables the element with id "id" if "isDisabled" is 
  * true, otherwise enables the element.
  */
function setDisabled(id, isDisabled) {
	document.getElementById(id).disabled=isDisabled;
}

/**
  * Checks the elements with id "id" if "isChecked" is
  * true, otherwise uncheckes the element.
  */
function setChecked(id, isChecked) {
	if (isChecked){
		document.getElementById(id).checked='checked';
	} else {
		document.getElementById(id).checked='';
	}
}

/**
 * Functions for moving selected items from one to another list box.
*/
function moveFromList(fromList, toList){
	for (var i=0 ; i<fromList.options.length ; i++){
		if (fromList[i].selected){
			//In IE options can not be moved from one SELECT to another. A new option has to be created.
			var fromOption = fromList.options[i];
			var toOption = new Option(fromOption.text, fromOption.value);
			toList.options.add(toOption);
			fromList.options[i] = null;
			i--;
		}
	}
}
function getAllSuitableThemas(listbox, hiddenField){
	var suitableThemas = "";
	for (var i=0 ; i<listbox.options.length ; i++){
		if (suitableThemas != ""){
			suitableThemas += ",";
		}
		suitableThemas += listbox.options[i].value;
	}
	hiddenField.value = suitableThemas;
}
function addToList(from, to, listbox, hiddenField){
	moveFromList(from, to);
	getAllSuitableThemas(listbox, hiddenField);
}

/**
 *Jobs related javascript functions
 */
function cleanupJobsSearchForm(){
	if(MM_findObj('allregions').checked){
		MM_findObj('vacatureRegions_0').checked=false;
		MM_findObj('vacatureRegions_1').checked=false;
		MM_findObj('vacatureRegions_2').checked=false;
		MM_findObj('vacatureRegions_3').checked=false;
		MM_findObj('vacatureRegions_4').checked=false;
		MM_findObj('vacatureRegions_5').checked=false;
	}

	jobsFixMultipleSelect('jobs_functionlevel');
	jobsFixMultipleSelect('jobs_functioncategory');
	jobsFixMultipleSelect('jobs_experience');
	jobsFixMultipleSelect('jobs_training');
	return true;
}

function jobsFixMultipleSelect(selectBoxID){
	var selectBox = MM_findObj(selectBoxID);
	for (var i=selectBox.options.length-1; i > 0;i--) {
		if (selectBox.options[i].selected) {
			selectBox.options[0].selected = false;
			break;
		}
	}
}

function disableAllRegionsCheckbox(){
	 MM_findObj('allregions').checked=false;
}

function checkNationalRegion(){
	if(MM_findObj('regionDropdown').value == '' && MM_findObj('regionDropdown').selectedIndex > 0){
		MM_findObj('national').value='true';
	}
	else{
		MM_findObj('national').value='false';
	}
}

function resetNationalRegiondropdown(){
	if(MM_findObj('national').value != 'true' && MM_findObj('regionDropdown').selectedIndex ==12){
		MM_findObj('regionDropdown').selectedIndex = 0;
	} 
}
