
var OPTION_FIELD_DELIM  = String.fromCharCode(7);
var OPTION_DOMAIN_DELIM = String.fromCharCode(9);

var dropdownOptionList =new Array();

function DropdownOption(){
	this.key ="";
	this.title ="";
}

function addDDOption(key, title){
	var option = new DropdownOption();
	option.key = key;
	
	option.title = unescape(title);
	dropdownOptionList[dropdownOptionList.length] = option;
}

function addDDOption2(key, level, title){
	var option = new DropdownOption();
	option.key = key;

	var myTitle = unescape(title);
	var leading ="";
	for (var i=1; i<=level; i++){
		leading +='___'; //String.fromCharCode(9,9,9)
	}
	myTitle =leading +myTitle;

	option.title = myTitle;
	dropdownOptionList[dropdownOptionList.length] = option;
}

//update the select object from parent document
//we do not update the parent select object directly.
//instead, we will use a hidden field for transfer, transfer 
//the current options to that hidden field.
//then meanwhile, on the parent form, there is a timer will discover this change,
//and make actions immediately
function updateParentDDOption(selName, initKey, initTitle){

	var parent = window.opener.document;
	if (!parent){
		return;
	}

	var selObj = parent.getElementById(selName+"_options");
	if (!selObj){
		return;
	}
	
	var optionText ="";	
	if (initKey || initTitle){
		optionText +=initKey+ OPTION_FIELD_DELIM +escape(initTitle) + OPTION_DOMAIN_DELIM;
	}

	if (dropdownOptionList){
		for (var i=0; i<dropdownOptionList.length; i++){
			var myOption = dropdownOptionList[i];
			optionText +=myOption.key+ OPTION_FIELD_DELIM +escape(myOption.title) + OPTION_DOMAIN_DELIM;
		}
	}		
	
	selObj.value = optionText;
	
	//updateDDOption(selObj, initKey,initTitle);
}


//selObj is the select object of a http select
//this is a function working on main window(parent window).
//it will check the 'xxxx_options' hidden field, and then make actions.
function updateCurrentDDOption(selName){
	
	//target select object
	var selObj = document.getElementById(selName);
	if (!selObj){
		return;
	}
	
	//template transferred hidden field
	//the value of this hidden field, is tranferred from popup window
	var selObj_options = document.getElementById(selName+"_options");
	if (!selObj_options || !selObj_options.value || selObj_options.value==""){
		return;
	}
	
	var originalVal = selObj.value;
	selObj.options.length	=0;
	
	var optionsArray = selObj_options.value.split(OPTION_DOMAIN_DELIM);
	for (var i=0; i<optionsArray.length; i++){
		var myOption = optionsArray[i];
		var pos = myOption.indexOf(OPTION_FIELD_DELIM);
		
	
		if (pos>=0){
			var myOptionKey = myOption.substring(0,pos);
			var myOptionVal = unescape(myOption.substring(pos+1));
			

			var optionObj = select_addOption(selObj, myOptionVal, myOptionKey);
			 
			if (optionObj && myOptionKey==originalVal){
				optionObj.selected = true;
			}		 	
		}	
	}
	
	//clear options
	selObj_options.value="";
}

/*
   we add a options timer here.
   this timer will check each options, to see if their options are changed.(by checking the hidden field xxx_options)
   once changed, will update the dropdown options immediately
*/

var options_timerID;
var options_selname_list;
function options_init_my_timer()
{
	options_timerID = setTimeout("run_my_timeout()",200);
}
function run_my_timeout(){
	if (options_timerID){
		clearTimeout(options_timerID);
	}
	if (options_selname_list && options_selname_list.length>0){
		for (var i=0; i<options_selname_list.length; i++){
			var selName =options_selname_list[i];
			updateCurrentDDOption(selName);
		}
	}
	options_init_my_timer();
}

options_init_my_timer();





//some operations to HTML Select object


function select_addOption(theSel, theText, theValue)
{

	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;

	try{
		theSel.options[selLength] = newOpt;
	}catch(e){
		//alert(e.message);
	}		

	return newOpt;
}

/*
function select_addOption(theSel, newText, newValue)
{

try{


	if (theSel.length == 0) {
		var newOpt1 = new Option(newText, newValue);
		theSel.options[0] = newOpt1;
		theSel.selectedIndex = 0;
		return newOpt1;
	} else if (theSel.selectedIndex != -1) {
		var selText = new Array();
		var selValues = new Array();
		var selIsSel = new Array();
		var newCount = -1;
		var newSelected = -1;
		var i;
		for(i=0; i<theSel.length; i++)
		{
			newCount++;
			selText[newCount] = theSel.options[i].text;
			selValues[newCount] = theSel.options[i].value;
			selIsSel[newCount] = theSel.options[i].selected;
			
			if (newCount == theSel.selectedIndex) {
				newCount++;
				selText[newCount] = newText;
				selValues[newCount] = newValue;
				selIsSel[newCount] = false;
				newSelected = newCount - 1;
			}
		}
		for(i=0; i<=newCount; i++)
		{
			var newOpt = new Option(selText[i], selValues[i]);
			theSel.options[i] = newOpt;
			theSel.options[i].selected = selIsSel[i];
		}
		
		return theSel.options[theSel.length-1];
	}

}catch(e){
	alert(e.message);
}
}
*/



function select_deleteOption(theSel, theIndex)
{	
	var selLength = theSel.length;
	if(selLength>0)
	{
		theSel.options[theIndex] = null;
	}
}

function select_clearSelectedOptions(selObj,initIt){
	selObj.options.length	=0;
	if (initIt){
		select_addOption(selObj,"","0");
	}	
}



