﻿
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
}

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

function getL(ele) {
		var x = 0;
		var y = 0;
		var p;
		for(p=ele; p; p=p.offsetParent) {
			// if(p.style.position == "relative" || p.style.position == "absolute") break;
			if(p.offsetLeft) {
				x += p.offsetLeft;
			}
			
			if(p.offsetTop) {
				y += p.offsetTop;
			}
		}
		return {left:x,top:y};
}

function getB(ele) {
	var offset = getL(ele);
	var width = ele.offsetWidth;
	var height = ele.offsetHeight;
	return {left:offset.left,top:offset.top,width:width,height:height};
}

addNamespace("MS.Web.AutoComplete");

MS.Web.AutoComplete = Class.create();

Object.extend(MS.Web.AutoComplete.prototype, {
	timer: null,
	count: 10,
	pos: -1,
	waitAfterInput: 100,
	minChars: 1,
	children: null,
	
	changeHighlight: function() {
		var l = this.getLength();
		for(var i=0; i<this.count && i<l; i++) {
			if(i == this.pos)
				this.div.childNodes[i].className = "item selected";
			else
				this.div.childNodes[i].className = "item"
		}
	},
	getItem: function(idx) {
		return null;
	},
	getLength: function() {
		return 0;
	},
	getDisplay: function(item) {
		return item;
	},
	getValue: function(item) {
		return item;
	},
	hide: function() {
		this.div.style.display = "none";
	},
	show: function() {
		this.changeHighlight();
		this.div.style.display = "inline";
	},
	focusTextbox: function() {
		this.ele.focus();
	},
	select: function() {
		if(this.pos > -1) {
			var v = this.getValue(this.getItem(this.pos));
			if(v != this.ele.value) {
				this.ele.value = v;
				if(this.children != null)
					for(var i=0; i<this.children.length; i++) {
						this.children[i].onparentchange(v);
					}
			}
		}
		this.hide();	
	},
	onkeydown: function(e) {
		var key = MS.Keys.getCode(e);
		switch(key) {
			case MS.Keys.TAB:
			case MS.Keys.ENTER:
				this.select();
				if(key == MS.Keys.ENTER) MS.cancelEvent(e);
				break;

			case MS.Keys.ESC:
				this.hide();
				break;

			case MS.Keys.KEYUP:
				if(this.pos > 0) this.pos--;
				this.changeHighlight();
				break;

			case MS.Keys.KEYDOWN:
				if(this.pos < this.getLength() -1 && this.pos < this.count -1) this.pos++;
				this.changeHighlight();
				break;
			case 8: {
			    break;
			}
		}
	},
	onkeyup: function(e) {
		switch(MS.Keys.getCode(e)) {
			case MS.Keys.TAB:
			case MS.Keys.ENTER:
			case MS.Keys.ESC:
			case MS.Keys.KEYUP:
			case MS.Keys.KEYDOWN:
			case MS.Keys.KEYLEFT:
			case MS.Keys.KEYRIGHT:
			case MS.Keys.ALT:
			case MS.Keys.SHIFT:
			case MS.Keys.CTRL:
				break;

			default:
				if(this.ele.value.length < this.minChars) {
				    this.hide();
					return false;
			    }
					
				if(this.timer != null) clearTimeout(this.timer);
				this.timer = setTimeout(this.getData.bind(this), this.waitAfterInput);
				break;
		}
	},
	onfocus: function(e) {
		this.changeHighlight();
	},
	onblur: function(e) {
	
	    var doHide = true;
        if(e.x) {
            
            var startX = parseInt(this.div.style.left);
	        var endX = parseInt(this.div.style.left) + parseInt(this.div.style.width);
	    
	        var startY = parseInt(this.div.style.top);
	        var endY = parseInt(this.div.style.top) + parseInt(this.div.style.height);
	        
	        if((e.x >= startX) && (e.x <= endX) && (e.y >= startY) && (e.y <= endY)) {
	            doHide = false;
	        }
	        
	        
//	        var d = document.getElementById("Debug");
//            if(d) {
//                d.style.visibility = "visible";
//                d.innerHTML = d.innerHTML + "<br/>-----------<br/>" + "e.x: " + e.x + "<br/>" + "e.y: " + e.y + "<br/>" + "startx: " + startX + "<br/>" + "endX " + endX + "<br/>" + "startY " + startY + "<br/>" + "endY: " + endY + "<br/>" + "doHide: " + doHide;      
//            }
        }
         	   
        
        
        if(doHide) {
	        setTimeout(this.hide.bind(this), 1000);	// 200 msec for onclick event of item
	    } else {
	        this.ele.focus();
	        return true;
	    }
	},
	onmouseover: function(e) {
		this.pos = MS.getTarget(e).idx;
		this.changeHighlight();
	},
	onclick: function(e) {
		this.pos = MS.getTarget(e).idx;
		this.changeHighlight();
		this.select();
	},
	ondblclick: function(e) {
		if(this.ele.value == "")
			this.getData();
	},
	onchange: function(e) {
		if(this.children != null)
			for(var i=0; i<this.children.length; i++) {
				this.children[i].onparentchange(this.ele.value);
			}
	},
	onparentchange: function(v) {
		this.ele.value = "";
	},
	updateList: function() {
		var l = this.getLength();
		if(l == 0) {
		    this.hide();
		    return;
		}
		
		for(var i=0; i<l && i<this.count; i++) {
			// MS.setText(this.div.childNodes[i], this.getDisplay(this.getItem(i)));
			this.div.childNodes[i].innerHTML = this.getDisplay(this.getItem(i));
			
			this.div.childNodes[i].style.display = "block";
		}
		if(this.pos > l) this.pos = this.getLength() -1;
		
		for(var i=l; i<this.count; i++) {
			this.div.childNodes[i].style.display = "none";
		}
		
		if(this.pos > l -1) {
			this.pos = l -1;
		}

		if(this.div.childNodes[0].clientHeight == 0) {
			this.show();
			this.updateList();
			return;
		}

		var h = (l < this.count ? l : this.count) * this.div.childNodes[0].clientHeight;
		
		if(h > 0) {
		
	        if (h > 200) {	
		        h = 200;
		        this.div.style.width = (this.ele.clientWidth + 1) + "px";
		        this.div.style.overflow = "auto";
		    } else {
		        this.div.style.overflow = "hidden";
		        this.div.style.width = (this.ele.clientWidth - 19) + "px";
		    }
		    
			this.div.style.height = h + 2 + "px";
			this.show();
		} else {
			this.hide();
		}
	},
	getData: function() {
	},
	ondata: function() {
		this.updateList();
	},
	onresize: function() {
		var r = getB(this.ele);
		this.ele.rect = r;
		r.top += r.height;
		MS.Position.setLocation(this.div, r);
	},
	initialize: function(id, count, parent) {
		this.ele = $(id);
		if(this.ele == null || this.ele.tagName != "INPUT") throw "Control could not be found.";
		this.count = count != null && !isNaN(count) ? count : this.count;

		if(parent != null) {	// no the parent control, instance of MS.Web.AutoComplete
			parent.children = [];
			parent.children.push(this);
		}
		
		addEvent(this.ele, "keydown", this.onkeydown.bind(this));
		addEvent(this.ele, "keyup", this.onkeyup.bind(this));
		addEvent(this.ele, "blur", this.onblur.bind(this));
		addEvent(this.ele, "focus", this.onfocus.bind(this));
		addEvent(this.ele, "change", this.onchange.bind(this));
		addEvent(this.ele, "dblclick", this.ondblclick.bind(this));
		
		// build list

		this.div = document.createElement("div");
		
		this.div.style.overflow = "auto";
		document.body.appendChild(this.div);			// see iframe ajaxpro

		this.div.id = this.ele.id + "_list";
		
		this.div.style.display = "none";
		this.div.style.border = "1px solid black";
		
		var r = getB(this.ele);
		this.ele.rect = r;
		r.top += r.height;
		r.width -= 2;
		MS.Position.setBounds(this.div, r);
		
        
		for(var i=0; i<this.count; i++) {
			var d = document.createElement("div");
			this.div.appendChild(d);
			d.style.width = this.ele.clientWidth - 21;
			d.style.whiteSpace = "nowrap";
			d.style.overflow = "hidden";
			d.className = this.ele.id + "_item";
			d.idx = i;
			
			addEvent(d, "mouseover", this.onmouseover.bind(this));
			addEvent(d, "click", this.onclick.bind(this));

			MS.setText(d, " ");
		}
		
		var l = this.getLength();
        
		
		addEvent(window, "resize", this.onresize.bind(this));
		
		delete r;
	}
}, false);


MS.Web.AutoCompleteDataTable = Class.create();
Object.extend(MS.Web.AutoCompleteDataTable.prototype, MS.Web.AutoComplete.prototype, true);

Object.extend(MS.Web.AutoCompleteDataTable.prototype, {
	dt: null,
	
	getItem: function(idx) {
		if(idx >= 0 && idx < this.dt.Rows.length)
			return this.dt.Rows[idx];
		return null;
	},
	getLength: function() {
		if(this.dt != null && this.dt.Rows != null) {
			return this.dt.Rows.length;
		}
		return 0;
	},
	callback: function(res) {
		if(res.error != null) {
			// alert(res.error.Message);
			return;
		}
		this.dt = res.value;
		this.ondata();
	}
}, true);