// JavaScript Document
//=====================================================================================
// class iArray class 
//=====================================================================================
function iArray(strElement) {
	//=================================================================================
	// class iArrayItem 
	//=================================================================================
	function iArrayItem (index,value) {
	   this.index = index;
	   this.value = value;
	}
	//=================================================================================
	function sort() {
		function swap(item,x,y) {
			
			vValue = item[x].value;
			vIndex = item[x].index;
			
	        item[x].value = item[y].value;
	        item[x].index = item[y].index;

	        item[y].value = vValue;
	        item[y].index = vIndex;
		}
		
		function qqSort(item,iLo, iHi) {
			Lo = iLo;
			Hi = iHi;
			iMid = Math.floor((Lo+Hi)/2);
			Mid = item[iMid].value;
			do {
				while (item[Lo].value < Mid){Lo++;};
				while (item[Hi].value > Mid){Hi--;};
				if (Lo <= Hi) {
					swap(item,Lo,Hi);
					Lo++;
					Hi--;
				}
			} while (Lo < Hi);
			if (Hi > iLo) {qqSort(item,iLo, Hi);}
			if (Lo < iHi) {qqSort(item,Lo, iHi);}
		}
		qqSort(this.item,0,this.item.length-1);
	}

	//=================================================================================
	function addItem(newItem)  // Define a function to extend the list.
	{
	    aItem = new iArrayItem(maxIndex()+1,newItem);
	    var itemIndex = this.item.length;
		this.item[itemIndex] = aItem;
		this.length = this.item.length;
	    this.sort();
	}

	//=================================================================================
	function maxIndex () {
		var max = 0;
		for (var i=0;i<this.item.length;i++) {
			max = (this.item[i].index > max )? this.item[i].index:max;
		}
		return max;
	}

	//=================================================================================
	function indexPosOf (iValue,iPos) {
		var retIndex = -1;
		var fndIdx = 0;
		for (var i=0;i<this.item.length;i++) {
			if (iValue == this.item[i].value) {
				fndIdx++;
				if (fndIdx == iPos) {
					retIndex = this.item[i].index;
					break;
				}
			}
		}
		return retIndex;
	}

	//=================================================================================
	function indexOf (iValue) {
		var retIndex = -1;
		for (var i=0;i<this.item.length;i++) {
			if (iValue == this.item[i].value) {
				retIndex = this.item[i].index;
				break;
			}
		}
		return retIndex;
	}
    
    // Object iArray Body ==================================================
		this.addItem = addItem;
		this.sort = sort;
		this.maxIndex = maxIndex;
		this.item = new Array();
		this.length = 0;
		this.indexOf = indexOf;
		this.indexPosOf = indexPosOf;
		this.element = strElement.split(',');
		
		for (var i=0;i<this.element.length;i++) {
			var el = this.element[i];
			var itemIndex = this.item.length;
			this.item[itemIndex] = new iArrayItem(itemIndex,el);	
		}
		this.length = this.item.length; 
		this.sort();
}
//=====================================================================================
function array_indexOf(ObjArray,objValue) {
	var retIndex = -1;
	for (var i=0;i<ObjArray.length;i++) {
		if (ObjArray[i] == objValue) {
			retIndex = i;
			break;
		}
	}
	return retIndex;
}
//=====================================================================================

function listbox_sortList(listbox,sortby) {
	  listbox_SortList(listbox,sortby);
}

function listbox_SortList(listbox,sortby) {
	function swap(x,y) {
    	with (listbox) {
    		vText = options[x].text;
        	vValue = options[x].value;
        	
        	options[x].text = options[y].text;
        	options[x].value = options[y].value;        	

        	options[y].text = vText;
        	options[y].value = vValue;
      	}
	}
	function qqSort(iLo, iHi) {
		var Lo = iLo;
		var Hi = iHi;
		var iMid = Math.floor((Lo+Hi)/2);
		var Mid = "";
		if (sortbytext) {
			Mid = listbox.options[iMid].text;
		} else {
			Mid = listbox.options[iMid].value;
		}
		do {
			if (sortbytext) {
				while (listbox.options[Lo].text < Mid){Lo++;};
				while (listbox.options[Hi].text > Mid){Hi--;};
			} else  {
				while (listbox.options[Lo].value < Mid){Lo++;};
				while (listbox.options[Hi].value > Mid){Hi--;};
			}

			if (Lo <= Hi) {
				swap(Lo,Hi);
				Lo++;
				Hi--;
			}
		} while (Lo < Hi);
		if (Hi > iLo) {qqSort(iLo, Hi);}
		if (Lo < iHi) {qqSort(Lo, iHi);}
	}
	if (sortby == undefined) sortbytext = true;
		else sortbytext = (sortby.toUpperCase() == "TEXT");
		
	qqSort(0,listbox.options.length-1);
}

function listbox_addOption(listbox,text,value,sortList,sortBy) {
	if (value == undefined) value = text;
	if ( !listbox_Exist (listbox, text, value) ){
		var optionObject = new Option(text,value)
		var optionRank = listbox.options.length;
		listbox.options[optionRank]=optionObject;
		if ((sortList == undefined || sortList)) {
			listbox_SortList(listbox,sortBy);
		}
	} else{
		alert("\""+text+"\"\r\nÁÕÍÂÙèáÅéÇ");
	}
}

function listbox_addOptions(listbox,text,value) {
	var aText = text.split(",");
	var aValue = value.split(",");
	listbox_removeOption(listbox,0);
	for (var i=0; i < aText.length; i++) {
		listbox_addOption(listbox,aText[i],aValue[i],false);
	}	

}

function listbox_deleteOption(selectObject) {
	if (selectObject.options.length!=0) {
		for (var i=0; i < selectObject.options.length; i++) {
			if (selectObject.options[i].selected) {
				selectObject.options[i]=null ;
				i--;
			}
		}
	}
}

function listbox_cloneSelect(slSrc,slDest) {
	listbox_deleteOption(slDest); 	
	for (var i=0; i<slSrc.options.length; i++){
		var vText = slSrc.options[i].text;
		var vValue = slSrc.options[i].value;
		var optObj = new Option(vText,vValue);
		slDest.options[i]=optObj;
	}
}

function listbox_Exist (selectObject, optionText, optionValue){
	var sltbool = false;
	for (var i=0; i<selectObject.options.length; i++){
		if (optionText == selectObject.options[i].text ||  optionValue == selectObject.options[i].value ){
		sltbool = true;
		break
		}
	}
	return sltbool;
}

function listbox_selectAll(listBox) {
	for (var i=0; i<listBox.length; i++) {
		listBox.options[i].selected = true;
	}
}

function listbox_clearOption(listBox) {
	while (listBox.length > 0) {
			listBox.remove(0);
	}
}

function listbox_removeOption(listBox,index) {
	while (listBox.length > index) {
			listBox.remove(index);
	}
}

function listbox_getValue(listBox,index) {
	return listBox.options[index].value;
}

function listbox_getText(listBox,index) {
	return listBox.options[index].text;
}

function listbox_getSelectText(listBox) {
	return listbox_getText(listBox,listBox.selectedIndex);
}

function listbox_indexOf(listBox,value) {
	var result = -1;
	for (var i=0; i<listBox.options.length; i++){
		if (value == listBox.options[i].value){
			result = i;
			break
		}
	}
	return result;
}

function listbox_removeValue(listBox,value)  {
   var index = listbox_indexOf(listBox,value);
   if (index > -1) listBox.remove(index);	
}

function genTableFromList(workTable,listBox) {
	if (listBox.length > 0 ) {
		for (var i=0; i<workTable.rows.length; i=0) { workTable.deleteRow(0); }
		for (var r = 1; r < listBox.length; r++) {
			row = workTable.insertRow();
                        if (r % 2 == 0) {
                            row.className="odd";
                        } else {
                            row.className="even";
                        }
			newtd = row.insertCell();
			newtd.innerHTML = "<input type=checkbox name=cbCheckList id=cb"+r+" value='"+listBox.options[r].text+"' />";
			newtd = row.insertCell();
			newtd.className="text13";
			newtd.innerHTML = "<label for=cb"+r+" >"+listBox.options[r].text+" </label>";
		}
	}
}
//=====================================================================================
function checkbox_checkedFor(checkbox,valueList,stateList){
	var maxitem =  checkbox.length;
	
	var aStateList = new iArray(stateList.toUpperCase());
	var aValueList = new iArray(valueList.toUpperCase());
	
    if (! maxitem) {
		if (checkbox.value == valueList) {
			checkbox.checked = (stateList=='1'||stateList=='Y');
        }
        return;
    } 
	for(idx=0;idx<maxitem;idx++){
		var vCBValue = checkbox[idx].value;
		var vStateIdx = aValueList.indexOf(vCBValue);
		if (vStateIdx >= 0) {
			var vState = aStateList.element[vStateIdx];
			checkbox[idx].checked = (vState=='1'||vState=='Y');		
		}
	}
}

function checkbox_checkedAll(checkbox,condition) {
	var maxitem =  checkbox.length;
    if (! maxitem) {
        checkbox.checked = true;
        return;
    } 
	for(var idx=0;idx<maxitem;idx++){
		var isChecked = true;
		if (condition != undefined) {
			try {		
			   eval('isChecked = (checkbox[idx].'+condition+');');
			} catch (ex) {
			   alert(ex.message);
			}
		}
		if (isChecked) {
			checkbox[idx].checked = isChecked;
			if (checkbox[idx].onclick) {
				checkbox[idx].onclick();
			}
		}
	}
}

function checkbox_setEnabled(checkbox,Enabled) {
	var maxitem =  checkbox.length;
    if (! maxitem) {
        checkbox.disabled = !Enabled;
        return;
    } 
	for(var idx=0;idx<maxitem;idx++){
		checkbox[idx].disabled = !Enabled;;
	}
}

function checkbox_clearAll(checkbox,condition) {
	var maxitem =  checkbox.length;
    if (! maxitem) {
        checkbox.checked = false;
        return;
    } 
	for(var idx=0;idx<maxitem;idx++){
		var isClear = true;
		if (condition != undefined) {
			try {		
			   eval('isClear = (checkbox[idx].'+condition+');');
			} catch (ex) {
			   alert(ex.message);
			}
		}
		if (isClear) {
			checkbox[idx].checked = false;
			if (checkbox[idx].onclick) {
				checkbox[idx].onclick();
			}			
		}
	}
}

function checkbox_addToList(checkbox,listbox) {
	var maxitem =  checkbox.length;
    if (! maxitem) {
		if (checkbox.checked) {
			listbox_addOption(listbox,checkbox.value,checkbox.value);
        }
        return;
    } 
    for(var idx=0;idx<maxitem;idx++){
        if (checkbox[idx].checked) {
            listbox_addOption(listbox,checkbox[idx].value,checkbox[idx].value);
        }
    }
}

function checkbox_isCheckedAll(checkbox,ignoreList) {
	var maxitem =  checkbox.length;
    if (! maxitem) {
        return checkbox.checked;
    }
    result = true; 
	for(var idx=0;idx<maxitem;idx++){
		aIgnore = new iArray(ignoreList);
		if (!checkbox[idx].checked && !(aIgnore.indexOf(idx)> -1)){
			result = false;
			break;
		}
	}
	return result;
}

function checkbox_ToListbox(checkbox,listbox) {
	
	if (!(checkbox && listbox)) return;
	
	listbox_clearOption(listbox);
	var maxitem =  checkbox.length;
    if (! maxitem) {
		var Value = checkbox.value;
		var Caption = checkbox.value;
		if (checkbox.caption) {
			Caption = checkbox.caption;
		}
		listbox_addOption(listbox,Caption,Value);
        return;
    } 
    for(var idx=0;idx<maxitem;idx++){
		var Value = checkbox[idx].value;
		var Caption = checkbox[idx].value;
		if (checkbox[idx].caption) {
			Caption = checkbox[idx].caption;
		}
		listbox_addOption(listbox,Caption,Value);
    }
}

function checkbox_count(checkbox) {
	var maxitem =  checkbox.length;
    var result = 0; 
    if (! maxitem) {
		if (checkbox.checked) result = 1;
    } else {
		for(var idx=0; idx<maxitem; idx++){
			if (checkbox[idx].checked) result++;
		}
	}
	return result;
}
