
var DynamicJsonOptionsScriptSourcePath = '';
var DynamicJsonOptionsScriptSourceFileName = 'JsonOptions.asp';
var selectionListSettingList = new Object();

//============================================================================================================================
// SelectionListSettingObj object constructor
//============================================================================================================================
//function SelectionListSettingObj(parentControl, childControl, emptyOptionText, preselecetedDefault, isOptionGroupAtTop, selectBoxName){
function SelectionListSettingObj(parentControl, childControl, emptyOptionText, preselecetedDefault){
	this.EmptyOptionText = emptyOptionText;
	this.PreselecetedDefaultValue = preselecetedDefault;
	this.ChildSelectControl = childControl;
	this.ParentSelectControl = parentControl;
	this.TitleOptionBackgroundColor = '#003399';	 //blue
	this.TitleOptionTextColor = '#ffffff';			//white
	this.TitleOptionClassName = 'DynamicTitleOptionItem';
	this.OptionClassName = 'DynamicOptionItem';
}
//============================================================================================================================


//============================================================================================================================
// Use to initalize the Settings of the selection boxes for subsequence calls of the "RequestSelectionList" method
//============================================================================================================================
function SetSelectionListSetting(listID, parentControl, childControl, emptyOptionText, preselecetedDefault){
	var childID = '';
	if(typeof(childControl)=='object'){
		childID = childControl.id;
	} else {
		childID = childControl;
	}
	//useing [listID+childID] to create a unique key for settings of different select boxes
	var key = (listID+childID).toLowerCase();
	selectionListSettingList[key] = new SelectionListSettingObj(parentControl, childControl, emptyOptionText, preselecetedDefault);
	return selectionListSettingList[key];
}
//============================================================================================================================


//============================================================================================================================
// Use after explcitly declare <script> tag and created the JSON selection options
// To initalize the selection box's options 
//============================================================================================================================
function InitSelectionBox(SelectionList, childControl, emptyOptionText, preselecetedDefault){
	var tmpObj = new SelectionListSettingObj(null, childControl, emptyOptionText, preselecetedDefault);
	RefleshSelectBoxes(SelectionList, tmpObj);
}
//============================================================================================================================


//============================================================================================================================
// used as the event handler for the "onChange" event of selection box
// To dynamically retrieve option list of a child selection box
//============================================================================================================================
//function requestSelectionList(parentSelectBox, childSelectBox, listID, defaultValue, emptyOption) {	
function RequestSelectionList(parentSelectBox, childSelectBox, listID) {	

//alert(parentcontrol.options.length.toString() + ' - ' + parentcontrol.selectedIndex.toString());
	var rtn = true;
	var parentcontrol, childControl, childID;
	var parentValue = '';
	var noEmpty = true;
	listID = listID.toLowerCase();
	if(typeof(parentSelectBox)=='object'){
		parentcontrol = parentSelectBox;
		if (typeof(childSelectBox)=='string'){
			childControl = eval('parentcontrol.form.' + childSelectBox);
		} else {
			childControl = childSelectBox;
		}
//alert(childSelectBox + ' - ' + selectionListSettingList[listID])	;	
	} else {
		parentcontrol = document.getElementById(parentSelectBox);
		if (typeof(childSelectBox)=='string'){
			childControl = document.getElementById(childSelectBox);
		} else {
			childControl = childSelectBox;
		}
	}
	
	if(typeof(childControl)=='object' && childControl!=null){
	
		childID = childControl.id;
		var settingObj = selectionListSettingList[(listID+childID).toLowerCase()];
//alert(settingObj + ' - ' + listID+childID)	;	
	
		if(typeof(settingObj)=='undefined' || settingObj==null) 
		{
			settingObj = new SelectionListSettingObj(parentcontrol, childControl, ' ', null);
			//noEmpty = true;
		} else {
			if(typeof(settingObj.ChildSelectControl)=='undefined' || settingObj.ChildSelectControl==null) 
				settingObj.ChildSelectControl = childControl;			//used by the callback function to retrieve the target child select box 
			if(typeof(settingObj.ParentSelectControl)=='undefined' || settingObj.ParentSelectControl==null) 
				settingObj.ParentSelectControl = parentcontrol;		//used by the callback function to retrieve the parent select box
			noEmpty = (typeof(settingObj.EmptyOptionText)=='undefined' || settingObj.EmptyOptionText==null);
		}
		if(parentcontrol!=null){
			//Check for Option Group options
			if(parentcontrol.value=='' && parentcontrol.options.length>parentcontrol.selectedIndex && parentcontrol.selectedIndex>0) {
				parentcontrol.selectedIndex++;
			}
			parentValue = parentcontrol.value; 
		}
		//thisSelectBoxID = childSelectBox;
		var oScript = document.createElement('script');
//alert(DynamicJsonOptionsScriptSourcePath + DynamicJsonOptionsScriptSourceFileName);	
		oScript.src = DynamicJsonOptionsScriptSourcePath + DynamicJsonOptionsScriptSourceFileName + '?ListID=' + listID + '&ParentValue=' + parentValue + '&ChildID=' + childID + '&noEmpty=' + (noEmpty?'true':'');
		document.body.appendChild(oScript);
		
	} else {rtn = false}
	
	return rtn;
	
}
//============================================================================================================================


//============================================================================================================================
// Reflesh child settingObj box specified in the "settingObj" param or
//   the predefined child selection box defined in the global "selectionListSettingList" variable
// This is also used as a Callback function (similar to an "onLoad" event for script tag) 
//    that the "JsonOptions.asp" page used to notify the calling page when the dynamic option list is generated
// [SelectionList] - is the dynamic option list in JSON format
// [settingObj] - is a "SelectionListSettingObj" object that may not in the global "selectionListSettingList" 
//============================================================================================================================
function RefleshSelectBoxes( SelectionList, settingObj ){
	var rtn = true;
    var selectBox, options //, thisDefaultValue
    var isAtLevel = false;
    var listID;
		
    if(typeof(SelectionList)!='undefined'){
    
        options = SelectionList.ChildList;
        //var level = 0;
        var childID = SelectionList.SelectBoxControlName;
        listID = SelectionList.SelectionListID;
//alert(childID + ' - ' + typeof(selectionListSettingList[listID].ChildSelectControl))	;	
			if(typeof(settingObj)=='undefined' ||  settingObj==null) settingObj = selectionListSettingList[(listID+childID).toLowerCase()];

			if(typeof(settingObj)!='undefined' && settingObj!=null){			
				if(typeof(settingObj.ChildSelectControl)=='string'){
					selectBox = document.getElementById(settingObj.ChildSelectControl);
				} else {
					selectBox = settingObj.ChildSelectControl;
				}
				//thisDefaultValue = selectionListSettingList[listID].PreselecetedDefaultValue;
			} else {
				rtn = false;
				//selectBox = document.getElementById(childID);
				//if( typeof(SelectionList.PreselecetedDefaultValue)!='undefined' && SelectionList.PreselecetedDefaultValue.length>level) thisDefaultValue = SelectionList.PreselecetedDefaultValue[level];
			}
//alert(selectBox.id + '-' + SelectionList.ChildList.length);		
            if(selectBox!=null){
				thisSelectedIndex = fillSelectBox(selectBox, options, settingObj);
            } else {
				rtn = false;
				//break;
				//have problem setting the upper level default seleted option
				//since some option list may have null options in the middle level options list
            }

		//clear the default values for the subsequence selecting actions
		if( rtn && typeof(settingObj.PreselecetedDefaultValue)!='undefined' && settingObj.PreselecetedDefaultValue!=null ) settingObj.PreselecetedDefaultValue=null;
   
    } else rtn = false;
    
    return rtn;
}
//============================================================================================================================


//============================================================================================================================
//populate a specific select box
//param <selectBox> is the select box to be populated
//param <options> is an array of the ChileList object
//param <defaultValue> is the preselected default value for the select box
//============================================================================================================================
//function fillSelectBox(selectBox, options, defaultValue, emptyOption){
function fillSelectBox(selectBox, options, settingsObj){

    var selectedIndex = 0;
    var isTitleOption = false;
    var hasTitleOption = false;
    var optText = '';
    
    selectBox.length = 0; 
     
    var l = options.length;    
    
    if(l>0){
        
		for(var j=0; j<l; j++){
			if (typeof(options[j])!='undefined'){ //check this because IE sometime gets an odd error from allow the loop to run pass 0 even when options.length=0
				var opt = document.createElement('OPTION');
				isTitleOption = (typeof(options[j].IsTitleOption)!='undefined');
				opt.value = options[j].Value;
				opt.text = options[j].Text;// + '[' + opt.value + ']';
				if(isTitleOption) {
					hasTitleOption = true;
					if(settingsObj.TitleOptionBackgroundColor!='') opt.style['backgroundColor'] = settingsObj.TitleOptionBackgroundColor;
					if(settingsObj.TitleOptionTextColor!='') opt.style['color'] = settingsObj.TitleOptionTextColor;
					if(settingsObj.TitleOptionClassName!='') opt.className = settingsObj.TitleOptionClassName;
				} else {
					if(settingsObj.OptionClassName!='') opt.className = settingsObj.OptionClassName;						
					if (opt.value=='' && typeof(settingsObj.EmptyOptionText)!='undefined' && settingsObj.EmptyOptionText!=null){
						//Display the predefined empty option text
						opt.text = settingsObj.EmptyOptionText;
					} else {
						if(hasTitleOption) {
							// FireFox trim out spaces while IE ignors "margin" settings
							opt.text = '  ' + options[j].Text;// indent option text (works in IE not FirsFox) 
							opt.style['margin'] = '0px 5px'; //indent option text (works in FirsFox not IE)
						} 
					}
				}
				if (opt.value==settingsObj.PreselecetedDefaultValue) {
					//set preloaded default. This is used to work with ASP preset values
					opt.selected=true;
					selectedIndex = j;
				}
				
				// add the option object
				selectBox.options.add(opt);			
			}			
		}
		
	} else {
			var opt = document.createElement('OPTION');
			opt.text = '';
			opt.value = '';
			selectBox.options.add(opt);
	}
    return selectedIndex;
    
}
//============================================================================================================================

