﻿/* -----------------------------------------
- SELECTBOX extensions
- a pseudo selectbox class
------------------------------------------*/
function extendSelectBox(pobj, pobjCounter, pbtnAction){
	pobj.doBusy = sb_busy;
	pobj.doError = sb_error;
	pobj.doLoad = sb_load;
	pobj.doRefresh = sb_refresh;
	pobj.setActionButtonState = sb_actionButton;
	pobj.sortByText = sb_sortByText;
	pobj.addItem = sb_addItem;
	pobj.removeItem = sb_removeItem;
	pobj.copyItemAndRemove = sb_copyItemAndRemove;
	pobj.selectAll = sb_selectAll;
	pobj.selectNone = sb_selectNone;
	pobj.copyItem = sb_copyItem;
	
	//the item counter
	(pobjCounter) ?	pobj.itemCounter = pobjCounter : pobj.itemCounter = null;
	
	//the action button and image
	(pbtnAction) ? pobj.btnAction = pbtnAction : pobj.btnAction = null;
	(pobj.btnAction) ? pobj.imgAction = pobj.btnAction.src.replace("_d.gif", ".gif") : pobj.imgAction = null;
	
	callbackFunction = null;
	callbackScope = null;
}

function sb_load(){
	this.xmlResult = this.http.xmlRequest.responseXML;
	var serv_response = this.xmlResult.documentElement;
	var value; 
	var caption;
	
	//reset the box
	this.options.length = 0;
	
	try {
		var serv_head = serv_response.getElementsByTagName("head")[0];
	} catch(e) {
		this.doError(-1, "Error processing data: 'head' node.");
		return;
	}
	
	try {
		var serv_status = serv_head.getElementsByTagName("status")[0];
	} catch(e) {
		this.doError(-1, "Error processing data: 'status' node.");
		return;
	}
	
	if (serv_status.childNodes[0].data == "fail"){
		try {
			var serv_items = serv_head.getElementsByTagName("items")[0];
		} catch(e) {
			this.doError(-1, "Error processing data: 'items' node.");
			return;
		}
		for (var i = 0; i < serv_items.childNodes.length; i++){
			this.doError(serv_items.childNodes[i].getElementsByTagName("number")[0].childNodes[0].data, serv_items.childNodes[i].getElementsByTagName("description")[0].childNodes[0].data);
		}
		return;
	}
	
	try {
		var serv_body = serv_response.getElementsByTagName("body")[0];
	}
	catch(e) {
		throw ("Error parsing serv_response element.");
		return;
	}
	
	for (var i = 0; i < serv_body.childNodes.length; i++){
		if (serv_body.childNodes[i].nodeName == "selectbox"){
			for (var j = 0; j < serv_body.childNodes[i].childNodes.length; j++){
				if (serv_body.childNodes[i].childNodes[j].nodeName == "selectboxitem"){
					var item = serv_body.childNodes[i].childNodes[j];
					value = item.getElementsByTagName("value")[0].childNodes[0].data;
					caption = item.getElementsByTagName("caption")[0].childNodes[0].data;
					this.addItem (value, caption);
				}
			}
		}
	}
	
	if (this.options.length == 0){
		this.addItem (0, "No items.");
	} 
	
	this.doRefresh();
	
	if (this.callbackFunction){
		this.callbackFunction.call(this.callbackScope);
		this.callbackFunction = null;
		this.callbackScope = null;
	}
}

function sb_refresh(){
	//do disabling
	var t = 0;
	for (var i = 0; i < this.options.length; i++) {
		t = t + this.options[i].value;
	}
	(t == 0) ? this.disabled = true : this.disabled = false;
	
	//update counter if enabled
	if (this.itemCounter) {
		(this.disabled) ? this.itemCounter.innerHTML = "0" : this.itemCounter.innerHTML = this.options.length;
	}
	
	this.setActionButtonState();
}

function sb_actionButton(){
	if (this.btnAction) {
		if (this.disabled) {
			this.btnAction.disabled = true;
			this.btnAction.src = this.imgAction.replace(".gif", "_d.gif");
		} else {
			if (this.selectedIndex >= 0) {
				this.btnAction.disabled = false;
				this.btnAction.src = this.imgAction;
			} else {
				this.btnAction.disabled = true;
				this.btnAction.src = this.imgAction.replace(".gif", "_d.gif");
			}
		}
	}
}

function sb_busy(){
	this.options.length = 0;
	this.disabled = true;
	this.addItem("", "Busy. Please Wait.");
}

function sb_error(errNum, errDesc){
	this.addItem(0, "Error: " + errDesc);
	alert("Error: " + errNum + "\n\n" + errDesc);
	
	if (this.callbackFunction){
		this.callbackFunction.call(this.callbackScope);
		this.callbackFunction = null;
		this.callbackScope = null;
	}
}

function sb_sortByText(){
	var arr = new Array;
	
	//dump to array
	for(i = 0; i < this.options.length; i++) {
		arr[arr.length] = this.options[i].text + "~|~" + this.options[i].value;
	}
	
	//sort
	arr.sort(sb_theSortFunction);
	this.options.length = 0;
	
	//dump back
	for(i = 0; i < arr.length; i++){
		this.addItem(arr[i].split("~|~")[1], arr[i].split("~|~")[0]);
	}
}

function sb_theSortFunction(a, b) {
   if (a.toLowerCase() == b.toLowerCase()) {
      return -1;
   } else {
      return (a.toLowerCase() > b.toLowerCase() ? 1 : -1);
   }
}

function sb_addItem (val, txt){
	var no = new Option();
	no.value = val;
	no.text = txt;
	this[this.length] = no;
	this.doRefresh();
}

function sb_removeItem(){
	//remove selected items
	var arrVal = new Array;
	var arrTxt = new Array;
	
	for (var i = 0; i < this.options.length; i++) {
		if (!this.options[i].selected){
			arrVal[arrVal.length] = this.options[i].value;
			arrTxt[arrTxt.length] = this.options[i].text;
		}
	}
	
	this.options.length = 0;
	
	if (arrVal.length > 0){
		for (var i = 0; i < arrVal.length; i++) {
			this.addItem(arrVal[i], arrTxt[i]);
		}
	}
	
	this.doRefresh();
}

// ----------------------------------
// copies highlighted items to the destination selectbox
function sb_copyItem(d){
	for(i = 0; i < this.length; i++){
		if(this.options[i].selected){
			var ex = false; //exists?
			for(j = 0; j < d.length; j++){
				if (d[j].value == this.options[i].value){
					ex = true;
				}
			}
			if (!ex){
				d.addItem(this.options[i].value, this.options[i].text);
			}
			this.options[i].selected = false;
		}
	}
	d.sortByText();
}

// ----------------------------------
// copies highlighted items to the destination selectbox
function sb_copyItemAndRemove(d){
	for(i = 0; i < this.length; i++){
		if(this.options[i].selected){
			var ex = false; //exists?
			for(j = 0; j < d.length; j++){
				if (d[j].value == this.options[i].value){
					ex = true;
				}
			}
			if (!ex){
				d.addItem(this.options[i].value, this.options[i].text);				
			}
		}
	}
	
	this.removeItem();
	d.sortByText();
}

function sb_selectAll(){
	if (this.disabled){
		return;
	}
	
	if (this.options){
		for(var i = 0; i < this.options.length; i++) {
			this.options[i].selected = true;
		}
	}
	this.setActionButtonState();
}

function sb_selectNone(){
	if (this.options){
		for(var i = 0; i < this.options.length; i++) {
			this.options[i].selected = false;
		}
	}
	this.setActionButtonState();
}

