//*******************************************************************************
function closeAllChoiceMenu(exept){
    arrDivs = new Array();
    arrDivs[0] = 'ChoiceNew';
    arrDivs[1] = 'ChoiceDelete';
    arrDivs[2] = 'ChoiceUpdate';
    for (i = 0; i < arrDivs.length; i++) {
        if ($('#'+arrDivs[i]).id != exept) {
            $('#'+arrDivs[i]).hide();
        }
    }
    if (exept=='ChoiceDelete') {
        if ($('#ChoiceDelete').css('display')!='block') {
            $('#search3').css('background','0px 23px');
        }else{
            $('#search3').css('background','0px 0px');
        }
    }
}

//*******************************************************************************
function updateSearchProfileList(searchProfileSelectBoxId, selectedItemValue, language){

    url = '/f1online/akibase/ajaxResponse/searchProfileList.cfm?language=' + language;
    
    onreadystatechange = function(responseText){
        searchProfileSelectBox = $(searchProfileSelectBoxId);
        objSearchProfiles = eval('(' + responseText + ')');
        //clear all curent option
        searchProfileSelectBox.innerHTML = '';
        itemToSelectIndex = 0;
        //add new options
        
        objOption = document.createElement("option");
        objOption.innerHTML = objSearchProfiles.translations.noSelection;
        objOption.value = -1;
        searchProfileSelectBox.append(objOption);
        
        if (objSearchProfiles.predefinedProfilesEnabled == true) {
            optGroup = document.createElement('optgroup');
            optGroup.label = objSearchProfiles.translations.predefinedSelection;
            
            for (i = 0; i < objSearchProfiles.qPredefinedProfiles.DATA.selValues.length; i++) {
                objOption = document.createElement("option");
                objOption.innerHTML = objSearchProfiles.qPredefinedProfiles.DATA.selText[i];
                objOption.value = objSearchProfiles.qPredefinedProfiles.DATA.selValues[i];
                objOption.setAttribute('predefined', 'true');
                if (objSearchProfiles.qPredefinedProfiles.DATA.selValues[i] == selectedItemValue) 
                    itemToSelectIndex = i;
                optGroup.append(objOption);
            }
            searchProfileSelectBox.append(optGroup);
        }
        
        if (objSearchProfiles.predefinedProfilesEnabled == true) {
            optGroup = document.createElement('optgroup');
            optGroup.label = objSearchProfiles.translations.userSelection;
        }
        else {
            optGroup = searchProfileSelectBox;
        }
        
        for (i = 0; i < objSearchProfiles.qUserProfiles.DATA.selValues.length; i++) {
            objOption = document.createElement("option");
            objOption.innerHTML = objSearchProfiles.qUserProfiles.DATA.selText[i];
            objOption.value = objSearchProfiles.qUserProfiles.DATA.selValues[i];
            objOption.setAttribute('predefined', 'false');
            if (objSearchProfiles.qUserProfiles.DATA.selValues[i] == selectedItemValue) 
                itemToSelectIndex = i + objSearchProfiles.qPredefinedProfiles.DATA.selValues.length;
            optGroup.append(objOption);
        }
        
        if (objSearchProfiles.predefinedProfilesEnabled == true) {
            searchProfileSelectBox.append(optGroup);
        }
        
        //find item to select
        searchProfileSelectBox.selectedIndex = itemToSelectIndex + 1; //+1 is bcz there is always first item = (no selection)
        loadSearchProfile(selectedItemValue, language);
    };
    
    var objAjax = new AJAXInteraction(url, onreadystatechange);
    objAjax.doGet();
}


//*******************************************************************************
function addNewSearchProfile(language){

    getSearchProfileName = $('#newChoiceName').attr('value');
    onreadystatechange = function(responseText){
        closeAllChoiceMenu();
        updateSearchProfileList('ChoiceNo', responseText, language);
    };
	
	var comNoList ='&comNoList=';
	$('#CheckboxFieldLeftBottom input[name=agency]:not(:hidden):checked').each(function(i,el) {
		comNoList=comNoList+$(el).attr('value')+',';
	});
	
    url = '/f1online/akibase/ajaxResponse/addSearchProfile.cfm?language=' + language + '&searchProfileName=' + getSearchProfileName + '&dummy=' + new Date().getTime();
    if ($('#expert_rm').attr('checked')) {
		url = url + '&rm=1';
	}
    if ($('#expert_rf').attr('checked')) {
		url = url + '&rf=1';
	}
    if ($('#expert_cd').attr('checked')) {
		url = url + '&cd=1';
	}
    if ($('#expert_mo').attr('checked')) {
		url = url + '&mo=1';
	}
    //add all checked agency
    url = url + comNoList
    
    var objAjax = new AJAXInteraction(url, onreadystatechange);
    objAjax.doGet();
}

//*******************************************************************************
function loadSearchProfile(pubAgeLisNo, language){
	if(pubAgeLisNo=='-1' || pubAgeLisNo==''){
		$('#search2').hide();
		$('#search3').hide();
		return false;		
	}
	$.ajax({
		dataType:'json',
		url:'/f1online/akibase/ajaxResponse/getSearchProfile.cfm?pubAgeLisNo='+pubAgeLisNo+'&language='+language,
		success: function(ret) {
			if (ret.pubAgeLisNo == -1) {
				//hide edit and delete
				$('#search2').hide();
				$('#search3').hide();
	        }else{
	            // check if search profile is one of predefined - if yes do not allow rename and delete
	            slectedInex = 0;
				$('#ChoiceNo option').each(function(e,i){
					if($(this).val()==pubAgeLisNo) slectedInex=i;
				});
				if ($('#ChoiceNo option[index='+slectedInex+']').attr('predefined')=='true') {
					$('#search2').hide();
					$('#search3').hide();
				}else{
					$('#search2').show();
					$('#search3').show();
				}
				switchFavoriteAgencyChooser(ret);
				var arrComNo=ret.comNoList.toString().split(',');
				$('#CheckboxFieldLeftBottom input[name=agency]:not(:hidden)').each(function(i,el) {
					if($.inArray($(el).attr('value'),arrComNo)!=-1) {
						$(el).attr('checked',true);
					}else{
						$(el).attr('checked',false);
					}
				});
	        }
	        if($('#ChoiceToRename')) $('#ChoiceToRename').attr('value',ret.title);
		    $("#ChoiceNo").addOption(ret.pubAgeLisNo, ret.title); //add new ones 
		}
	});
}

//*******************************************************************************
function deleteSearchProfile(language, caller){
    //selectedIndex = $('#ChoiceNo').selectedIndex;
	selectedIndex=$('#ChoiceNo option:selected').val();
    pubAgeLisNoOption = $('#ChoiceNo option[value='+selectedIndex+']'); //[selectedIndex];
    //pubAgeLisNo = pubAgeLisNoOption.value;
	pubAgeLisNo = pubAgeLisNoOption.val();
    $("#ChoiceNo option:selected").remove(); 
    
    if (caller == 'input3') $('#search3').css('background','0px 0px');
    
    onreadystatechange = function(responseText){
        closeAllChoiceMenu();
        updateSearchProfileList('ChoiceNo', responseText, language);
    };
    url = '/f1online/akibase/ajaxResponse/delSearchProfile.cfm?pubAgeLisNo=' + pubAgeLisNo + '&language=' + language + '&dummy=' + new Date().getTime();
    
    var objAjax = new AJAXInteraction(url, onreadystatechange);
    objAjax.doGet();
    
}

//*******************************************************************************
function updateSearchProfile(language){
    onreadystatechange = function(responseText){
        closeAllChoiceMenu();
        updateSearchProfileList('ChoiceNo', responseText, language);
    };
    
	var comNoList ='&comNoList=';
	$('#CheckboxFieldLeftBottom input[name=agency]:not(:hidden):checked').each(function(i,el) {
	  comNoList=comNoList+$(el).attr('value')+',';
	});
	
    //selectedIndex = $('#ChoiceNo').selectedIndex;
	selectedIndex=$('#ChoiceNo option:selected').val();
    //pubAgeLisNoOption = $('#ChoiceNo option')[selectedIndex];
	pubAgeLisNoOption = $('#ChoiceNo option[value='+selectedIndex+']');
    //pubAgeLisNo = pubAgeLisNoOption.value;
	pubAgeLisNo = pubAgeLisNoOption.val();
    
    //newSearchProfileName = $('#ChoiceToRename').value;
	newSearchProfileName = $('#ChoiceToRename').val();
	$("#ChoiceNo").addOption(pubAgeLisNo, newSearchProfileName); //udate direct the old element
    
    url = '/f1online/akibase/ajaxResponse/updateSearchProfile.cfm?pubAgeLisNo=' + pubAgeLisNo + '&dummy=' + new Date().getTime() + '&language=' + language + '&newSearchProfileName=' + encodeURI(newSearchProfileName);
    if ($('#expert_rm').attr('checked')) 
        url = url + '&rm=1';
    if ($('#expert_rf').attr('checked')) 
        url = url + '&rf=1';
    if ($('#expert_cd').attr('checked')) 
        url = url + '&cd=1';
    if ($('#expert_mo').attr('checked')) 
        url = url + '&mo=1';
	
    //add all checked agency
    url = url + '&comNoList='+comNoList;
   
    var objAjax = new AJAXInteraction(url, onreadystatechange);
    objAjax.doGet();
    
}

//*******************************************************************************