/** Public Vars **/
var optionSet_Boolean = Array(
	'1|Is True',
	'0|Is False'
	);
var optionSet_Numeric = Array(
	'RANGE|Range',
	'>|Greater Than',
	'<|Less Than',
	'=|Equal To'
	);
var optionSet_Text = Array(
	'=|Equal To',
	'CONTAINS|Contains',
	'NOT CONTAINS|Does Not Contain',
	'STARTS WITH|Starts With',
	'IN LIST|In List',
	'NOT IN LIST|Not In List',
	'EXISTS|Exists',
	'NOT EXISTS|Does Not Exist'
	);
var optionSet_TextNum = Array(
	'=|Equal To',
	'CONTAINS|Contains',
	'NOT CONTAINS|Does Not Contain',
	'STARTS WITH|Starts With',
	'IN LIST|In List',
	'NOT IN LIST|Not In List',
	'EXISTS|Exists',
	'NOT EXISTS|Does Not Exist',
	'RANGE|Range'
	);	
var optionSet_PlanType = Array(
	'=|Equal To',
	'<>|Not Equal To'
	);
	
//option datatypes in DataItem select menu (txt is default type, so not defined here)
//var dataitem_yn_Options			= '|BOOKMARKED PLANS|BROKER HAS EMAIL|';
//var dataitem_int_Options		= '|POLICY SIZE|BROKER ZIP|SPONSOR ZIP|EMPLOYEE SIZE|';
//var dataitem_plantype_Options	= '|PLAN TYPE|';
var dataitem_yn_Options			= '|SEARCHBOOKMARK|BROKERHASEMAIL|';
var dataitem_int_Options		= '|CARPART|TOTPREM|BROKERZIP|SPONSORZIP|EMPLOYEES|';
var dataitem_plantype_Options	= '|PLANTYPE|';
var dataitem_textnum_Options = '|SPONSORNAICS|';

/** togglePlusMinus(obj, state)
 * Simple swap function for the + and - symbols preceding each criteria row.
 * obj = object; calling item
 * state = int; 1 for 'on', 0 for 'default/off'
 * returns nothing
 **/
function togglePlusMinus(obj,state) {
	objType = (obj.id.toLowerCase().indexOf('subtractcrit')!==-1) ? 'minus' : 'plus' ;
	
	icon_plus_off = bcms_root + 'img/icon_plus_off.gif';
	icon_plus_hi = bcms_root + 'img/icon_plus_hi.gif';
	icon_minus_off = bcms_root + 'img/icon_minus_off.gif';
	icon_minus_hi = bcms_root + 'img/icon_minus_hi.gif';
	
	//disabled icons have no operation
	if (obj.src.indexOf('_disabled')!==-1) { return; }
	
	//do the swap
	if (obj.src.indexOf('_hi')!==-1) {
		//turn to Off
		obj.src = (objType=='minus') ? icon_minus_off : icon_plus_off ;
	} else {
		//turn to Hi
		obj.src = (objType=='minus') ? icon_minus_hi : icon_plus_hi ;
	}
}

/** init_dataitemMenu(initCrit, itemValue)
 * Fire this function to look up the value selected in the Data Item select menu of a particular 
 * criteria set, and make adjustments to other elements of that criteria row based on the selection.
 * intCrit = int; position of criteria in form
 * returns nothing
 **/
function init_dataitemMenu(intCrit) {

	objDataitem = document.getElementById('dataitem_'+intCrit);
	objCompare = document.getElementById('comparison_'+intCrit);
	
	//selected value of Dataitem Menu(intCrit)
	dataitemValue = objDataitem.options[objDataitem.options.selectedIndex].value.toUpperCase();
	
	//change the option set on the Comparison Menu(intCrit)
	if (dataitem_yn_Options.indexOf('|'+dataitemValue+'|')!==-1) {
		exec_newOptionSet(objCompare, optionSet_Boolean, dataitemValue);
	} else if (dataitem_int_Options.indexOf('|'+dataitemValue+'|')!==-1) {
		exec_newOptionSet(objCompare, optionSet_Numeric, dataitemValue);
	} else if (dataitem_plantype_Options.indexOf('|'+dataitemValue+'|')!==-1) {
		exec_newOptionSet(objCompare, optionSet_PlanType, dataitemValue);
	} else if (dataitem_textnum_Options.indexOf('|'+dataitemValue+'|')!==-1) {
		exec_newOptionSet(objCompare, optionSet_TextNum, dataitemValue);
	}else {
		exec_newOptionSet(objCompare, optionSet_Text, dataitemValue);
	}
	
	//toggle the value element to either the text input or select, based on what is chosen
	//in the Dataitem Menu(intCrit)
	toggleValueElement(dataitemValue, intCrit);
}

function init_valueElement(intCrit) {
	objComparison = document.getElementById('comparison_'+intCrit);
	comparisonValue = objComparison.options[objComparison.options.selectedIndex].value.toUpperCase();
	if (comparisonValue.toUpperCase().indexOf('EXIST')!==-1)  {
		toggleValueElement('EXIST', intCrit);
	}
}


/** toggleValueElement(val, intCrit)
 * Fire this function to swap the value element to either the text input or the Plan Type 
 * select menu based on whether or not val="PLAN TYPE".
 * val = string; selected value of dataitem Menu(intCrit)
 * intCrit = int; position of criteria in form
 * returns nothing
 **/
function toggleValueElement(val, intCrit) {
	objValue_text = document.getElementById('value_'+intCrit+'_text');
	objValue_textShell = document.getElementById('valueshell_'+intCrit+'_text');
	objValue_select = document.getElementById('value_'+intCrit+'_select');
	objValue_selectShell = document.getElementById('valueshell_'+intCrit+'_select');
	
	//based on value, modify visibility of value entry element and clear value of other element
	//note that form element objects cannot be accessed until the shell is made visible
	if (val.toUpperCase()=='PLANTYPE') {
		//show the select menu
		objValue_selectShell.style.visibility = 'visible';
		objValue_selectShell.style.display = 'block';
		
		//hide the text input
		objValue_text.value = ''; 
		objValue_textShell.style.visibility = 'hidden';
		objValue_textShell.style.display = 'none';
		
	} else if (val.toUpperCase()=='EXIST') {
		objValue_text.value = ''; 
		objValue_textShell.style.visibility = 'hidden';
		objValue_textShell.style.display = 'none';
		
		objValue_select.selectedIndex = 0;
		objValue_selectShell.style.visibility = 'hidden';
		objValue_selectShell.style.display = 'none';
	}

	else {
		//show the text input
		objValue_textShell.style.visibility = 'visible';
		objValue_textShell.style.display = 'block';
		
		//hide the select menu
		objValue_select.selectedIndex = 0; 
		objValue_selectShell.style.visibility = 'hidden';
		objValue_selectShell.style.display = 'none';
	}
	
}

/** exec_newOptionSet(obj, optionSet, selectedValue)
 * Creates a new set of options for the target Object.
 * obj = object; target object (not calling object!)
 * optionSet = array; set of relevant options from array
 * selectedValue = string; value of selected option
 * returns nothing.
 **/
function exec_newOptionSet(obj, optionSet, selectedValue) {
	//first, remove all options in obj
	obj.options.length = 0;
	
	//add the new option set (label, value, defaultseleted, selected)
	for (i=0; i<optionSet.length; i++) {
		opt = optionSet[i].split('|');
		defSelected = (i==0) ? true : false ;
		isSelected = (selectedValue==opt[0]) ? true : false ;
		obj.options[i] = new Option(opt[1], opt[0], defSelected, isSelected);
	}
}

/** exec_addCrit()
 * Add a new criteria row to the advanced search form.
 * No parameters.
 * Returns nothing.
 **/
function exec__addCrit() {
	document.advsearch.mode.value = 'ADD_CRITERIA';
	document.advsearch.critID.value = '0';
	exec__searchSubmit(document.advsearch);
}

/** exec_removeCrit(intCrit)
 * Delete critera from the advanced search form.
 * intCrit = int; position of criteria in form
 * Returns nothing.
 **/
function exec__removeCrit(intCrit) {
	if (confirm("Are you sure you want to remove this criteria?")) {
		document.advsearch.mode.value = 'REMOVE_CRITERIA';
		//alert(intCrit);
		//alert(document.advsearch.elements['critID_'+intCrit]);
		//document.advsearch.critID.value = document.advsearch.getElementById('critID_'+intCrit).value;
		document.advsearch.critID.value = document.advsearch.elements['critID_'+intCrit].value;
		//alert("doodoo");
		exec__searchSubmit(document.advsearch);
	}
}

/** exec__setListBy(obj)
 * Sets the value of the 'listby' hidden form element before firing the submit function.
 * obj = object; calling object
 * returns nothing
 **/
function exec__setListBy(obj) {
	if (obj.value.indexOf('CARRIER')!==-1) { obj.form.listby.value = 'CARRIER'; }
	if (obj.value.indexOf('BROKER')!==-1) { obj.form.listby.value = 'BROKER'; }
	if (obj.value.indexOf('SPONSOR')!==-1) { obj.form.listby.value = 'SPONSOR'; }
	exec__searchSubmit(obj.form);
}

/** exec__searchSubmit(obj)
 * Handler function fired pre-submit().
 * obj = object; calling object
 * returns nothing
 **/
function exec__searchSubmit(obj) {
	obj.submit();
}