﻿// JavaScript Document
var ActivComboBox = null;
function InitComboBox(elem){
	//alert("InitComboBox"+elem.id);
	//if(document.all["test"])
	//	document.all["test"].value="InitComboBox "+elem.id;
	var cmb=new Cit_WebComboBox(elem);
	if(typeof(window.init_combobox)=="undefined"){
		if (document.all)
			document.attachEvent("onmousedown",document_mouseDown);
		else
			document.addEventListener("mousedown", document_mouseDown, false); 
		window.init_combobox=true;
	}
	//alert("end"+elem.id);
	//if(document.all["test"])
	//	document.all["test"].value="End "+elem.id;
}
function document_mouseDown(e){
	if (document.all)
		elem = event.srcElement;
	else
		elem = e.target;
	if(ActivComboBox){
		if(!ActivComboBox.IsChild(elem) && !ActivComboBox.List.IsChild(elem)){
			ActivComboBox.setSelectedItem(ActivComboBox.List.getSelectedItem());
			ActivComboBox.Hide();
		}
	}
}

function Cit_WebComboBox(elem){

	this.Element=elem;
	this.TextFieldIndex = elem.getAttribute('TFIndex')||0;
	this.Type =elem.getAttribute('Type')||"CB";
	
	this.DropDown=false;
	this.ValueFieldIndex = elem.getAttribute('VFIndex')||0;
	this.SelectedItem=null;
	this.size=new Size(elem.offsetWidth,elem.offsetHeight);
	
	this.EditElement=document.getElementById(this.Element.id+"Edit");
	if(!this.EditElement) return ;
	this.NumberFormat=this.EditElement.num!=null?true:false;
	
	this.ValueElement=document.getElementById(this.Element.id+"Val");
	
	this.DropDownButton= document.getElementById(this.Element.id+"Button"); 


	elem.SelIndex=elem.getAttribute('SelIndex');
    //alert(elem.SelIndex);
	if(!elem.attributes['List']) return ;
	this.ListElem=document.getElementById(elem.attributes['List'].value); 
	
	if(!this.ListElem) return ;
	
	if(this.Type=="DP"){
		this.List=new Cit_WebCalendar(this.ListElem);
		oAttrib=elem.SelIndex;
		d=oAttrib && oAttrib!="-1"? new Date(Date.parse(oAttrib)):-1;
		this.setSelectedItem(d);
	
	}else if(this.Type=="TR"){
		this.List=new Cit_WebTreeBox(this.ListElem);
		oAttrib=elem.SelIndex;
		
		if(oAttrib)
		{
			var tNodes=this.List.tree.Nodes;
			var narr=oAttrib.split("_");
			var nind=0;
			var n=null;
			for(var i=0;i<narr.length;i++){
				nind=narr[i];
				n=tNodes[nind];
				if(n){
					n.expand();
					n.select();
					tNodes=n.Nodes;
				}else
					break;
			}
			if(n)this.setSelectedItem(n);
		}
	}else{
	
		this.List=new Cit_WebListBox(this.ListElem);
		this.setSelectedItem(elem.SelIndex?elem.SelIndex:-1);		
	}
	
	this.addEvent("onSelectedItemChange",this,this.onSelectedItemChange);
	this.attachEvent(this.DropDownButton,"onclick",this.onButtonClick);
	
	if(this.EditElement.readOnly){
		this.EditElement.style.cursor="pointer";
		this.attachEvent(this.EditElement,"onclick",this.onButtonClick);
		if(this.Type=="CB"){
			this.attachEvent(this.EditElement,"onkeypress",this.onListKeypress);
			this.attachEvent(this.EditElement,"onkeydown",this.onListKeydown);
			//this.EditElement.onKeyDown="return Cit_WebComboBox.prototype.onListKeydown(event);
			//this.attachEvent(this.EditElement,"onkeyup",this.onListKeyup);
		}
	}else{
		this.attachEvent(this.EditElement,"onpaste",this.onPaste);
		if(this.Type=="DP"){
			this.attachEvent(this.EditElement,"onkeyup",this.onCalKeyup);
			this.attachEvent(this.EditElement,"onkeydown",this.onCalKeydown);
		}else{
			this.attachEvent(this.EditElement,"onkeydown",this.onListKeydown);
			this.attachEvent(this.EditElement,"onkeyup",this.onListKeyup);
		    if(this.Type=="CB"){
			    this.attachEvent(this.EditElement,"onkeypress",this.onListKeypress);
			    this.attachEvent(this.EditElement,"onfocus",this.onEditFocus);
		    }
		}
	}
}
Cit_WebComboBox.prototype=new EventObject();
Cit_WebComboBox.prototype.onSelectedItemChange=function(){
	//alert(this.Element.outerHTML);
	if(this.Element.onchange)
		eval(this.Element.onchange);
}
Cit_WebComboBox.prototype.onButtonClick=function(e){
	if(!this.DropDown)this.Show();else this.Hide();
//	event.cancelBubble=true;
}
Cit_WebComboBox.prototype.onPaste=function(){
	var value=window.clipboardData.getData("Text");	
	if(this.NumberFormat){
		value =	parseFloat(value);
		if(isNaN(value)){
			event.returnValue=false;
			return;
		}
	}
	var index=this.List.FindItem(value,this.TextFieldIndex);
	this.Show();
	this.List.setSelectedItem(index);	
}
Cit_WebComboBox.prototype.onListKeyup=function(e){
	e = document.all ? event : e;
	var keyCode=(e.keyCode);
	switch(keyCode){case 38:case 40:case 35:case 36:case 13:return;}
	index=this.List.FindItem(this.EditElement.value,this.TextFieldIndex);
	this.Show();
	this.List.setSelectedItem(index);
}

Cit_WebComboBox.prototype.onEditFocus=function(e){
	var str = this.EditElement.value;
//	if(str.charAt(0) == " ");
//	    str = str.substr(1);
//	this.EditElement.value = str;
//	this.EditElement.value = str.replace(/^ /, "");
	this.EditElement.value = str.replace(/^\s*/, "");
}
Cit_WebComboBox.prototype.onListKeypress=function(e){
	e = document.all ? event : e;
	var keyCode=(document.all ? e.keyCode : e.charCode);
	//switch(keyCode){case 38:case 40:case 35:case 36:case 13:return;}
	//alert(keyCode);
	//switch(keyCode){case 38:case 40:case 35:case 36:case 13:case 0:return;}
	//switch(keyCode){case 38:case 40:case 35:case 36:case 13:case 0:return false;}

switch(keyCode){
	case 0:
	    return;
	
		e.returnValue=false;
		e.cancelBubble=true;
		
		if(e.preventDefault) e.preventDefault(); 
		if(e.stopPropagation) e.stopPropagation();
		return false;
}
	if(this.Type=="CB"){
/////////////////////////////////////
// [AA: 2009-04-07]
// ref: requirement that was not included in BugList, 
//		but requsted by OB staff, concerning full functionality,
//		when using type-ahead feature in combo-boxes.
//
//  - following line of code is commented, instead the item in the combo-list
//    is found using the current text of Edit Element, but not the entered char
/////////////////////////////////////
		//index=this.List.FindItem(String.fromCharCode(keyCode),this.TextFieldIndex);
		index=this.List.FindItem(this.EditElement.value + String.fromCharCode(keyCode), this.TextFieldIndex);
	}
	else {
		index=this.List.FindItem(this.EditElement.value,this.TextFieldIndex);
	}
	this.Show();
	this.List.setSelectedItem(index);
	return;
}
Cit_WebComboBox.prototype.onListKeydown=function(e){
	e = document.all ? event : e;
	var keyCode=(e.keyCode);
switch(keyCode){
	case 38: 
		this.Show();
		this.List.SelectedPrev();
		return;
	case 40:
		if(!this.DropDown)
			this.Show();
		else
			this.List.SelectedNext();
		return;
	case 35:
		this.Show();
		this.List.SelectedLast();
		return;
	case 36:
		this.Show();
		this.List.SelectedFirst();
		return;
	case 13:
		this.setSelectedItem(this.List.getSelectedItem());
		this.Hide();
			
		e.returnValue=false;
		
		//e.stopPropagation();
		//e.cancelBubble=true;
		
		e.cancelBubble=true;
		if(e.preventDefault) e.preventDefault(); 
		if(e.stopPropagation) e.stopPropagation();
		return false;
}
if(this.NumberFormat)
{
	if(keyCode==46 || keyCode==8 
	||(keyCode>46 && keyCode<58) 
	|| (keyCode>95 && keyCode<106)
	|| (e.ctrlKey && (keyCode==88 || keyCode==67|| keyCode==86))
	)return;
	e.returnValue=false;
}
}
Cit_WebComboBox.prototype.onCalKeydown=function(e){
	e = document.all ? event : e;
	var keyCode=(e.keyCode);
	if(this.DropDown)
		switch(keyCode){
			case 38:case 40:case 39:case 37:
			event.returnValue=false;
		}
	if(keyCode==13){e.returnValue=false;e.cancelBubble=true;}
		
}
Cit_WebComboBox.prototype.onCalKeyup=function(e){
e = document.all ? event : e;
var keyCode=(e.keyCode);
switch(keyCode){
	case 38: 
		if(this.DropDown)this.List.MoveUp();
		return;
	case 40:
		if(this.DropDown)this.List.MoveDown();
		return;
	case 39:
		if(this.DropDown)this.List.MoveNext();
		return;
	case 37:
		if(this.DropDown)this.List.MovePrev();
		return;
	case 13:
		if(this.DropDown)this.Hide();
		this.setSelectedItem(this.List.getSelectedItem());
		return;
}	
edit=this.EditElement;
value=edit.value;
if(value.length==0)
	this.setSelectedItem(-1);
else
	this.List.ParseToDate(value);
this.Show();	
}
Cit_WebComboBox.prototype.IsChild=function(elm){ 
		var e=elm;
		while(e!=null)
		{
			if(this.Element==e)	return true;
			e=document.all ? e.parentElement : e.parentNode;
		}
		return false;
	}
Cit_WebComboBox.prototype.Show=function(){
	if(this.DropDown)return;
	
	var le=this.ListElem;
	//alert(this.getSelectedItem());
	//this.List.Show();
	var value =this.getSelectedItem();
	this.List.setSelectedItem(value);
	this.List.Show();
	this.EditElement.focus();
//	this.EditElement.parentElement.click();

	var e=document.documentElement||document.body;
	var p=getElementPosition(this.Element);
	var sz=this.getSize();
	var es=new Point(e.scrollLeft,e.scrollTop);
	var ec=new Size(e.clientWidth,e.clientHeight);

	if(es.equals(Point.ORIGIN))
		es=new Point(document.body.scrollLeft,document.body.scrollTop);	
	if(ec.equals(Size.ZERO))
		ec=new Size(e.offsetWidth-20,e.offsetHeight-20);
	
	var mtop =(p.y-es.y);
	var mleft=(p.x-es.x);
	var mbottom=(ec.height+es.y)-(p.y+sz.height)-3;
	var mright=(ec.width+es.x)-(p.x+sz.width)-3;

	if(sz.width > le.offsetWidth)
		le.style.width=Unit.Pixsel(sz.width);
	
	var d=sz.width+mright-le.offsetWidth;
	if(d>0)d=0;
	le.style.left=Unit.Pixsel(p.x+d); 
	
	var d=mbottom-le.offsetHeight ;
	le.style.border="solid 1px black";
	if(d<0 && mtop-le.offsetHeight>0)
	{
		d=p.y-le.offsetHeight;
    }
	else
	{
		d=p.y+sz.height;
    }	  
	le.style.top=Unit.Pixsel(d);
	this.DropDown=true;
	ActivComboBox=this;
	
	if(value!=-1 && this.Type=="CB"){
	    try {
		    var row=this.List.oTable.rows[value];
//		    row.scrollIntoView(true);

	        le.scrollTop= row.offsetTop;
		}
		catch(e) {}
	}
}

Cit_WebComboBox.prototype.Hide=function(){
	if(!this.DropDown)return;
	
	this.DropDown=false;
	this.List.Hide();
	ActivComboBox=null;
}
Cit_WebComboBox.prototype.setSelectedItem=function(item){
	var txt=this.List.getText(item,this.TextFieldIndex);
	var val=this.List.getValue(item);
	var flag=this.EditElement.value!=txt ;
	if(this.ValueElement!=null)
		flag=flag || this.ValueElement.value!=val;
	if(flag)
	{
		if(this.Element.title)this.Element.title=txt;
		this.EditElement.value=txt;
		if(this.ValueElement!=null)
			this.ValueElement.value=val;
		
		this.SelectedItem=item;
		this.fire("onSelectedItemChange");
	}
}
Cit_WebComboBox.prototype.getSelectedItem=function(){return this.SelectedItem;}
Cit_WebComboBox.prototype.getSize=function(){ return this.size};

function Cit_WebListBox(elem){
	this.Element = elem;
	this.Hide();
	var m_id=this.Element.id;
	var listname=m_id.substring(0,m_id.length-9);
	this.oTable=document.getElementById(listname);
	this.rows=this.oTable.rows;
	this.selectedStyle=this.Element.SelStyle||"BACKGROUND: #000066; COLOR: #ffffff";
	this.initFilters();
	if(!this.oTable.onmousedown){
		this.oTable.onmousedown=Cit_WebListBox.mouseDown;
		var aRows=this.oTable.rows; 
		for(var i=0;i<aRows.length;i++){ 
			aRows[i].onmouseover=new Function("Cit_WebListBox.rowMouseOver(this)");
			aRows[i].onmouseout=new Function("Cit_WebListBox.rowMouseOut(this)");
		} 
	}
	
}
Cit_WebListBox.rowMouseOver=function(e){
	//e.runtimeStyle.cssText="BACKGROUND: #000088; COLOR: #ffffff";
	e.style.cssText="BACKGROUND: #000088; COLOR: #ffffff";
	for(idx=0;idx<e.cells.length;idx++)
		e.cells[idx].style.cssText="BACKGROUND: #000088; COLOR: #ffffff";
		//e.cells[idx].runtimeStyle.cssText="BACKGROUND: #000088; COLOR: #ffffff";
}
Cit_WebListBox.rowMouseOut=function(e){	
	//e.runtimeStyle.cssText="";
	e.style.cssText="";
	for(idx=0;idx<e.cells.length;idx++)
		e.cells[idx].style.cssText="";
		//e.cells[idx].runtimeStyle.cssText="";
}
Cit_WebListBox.mouseDown=function(e){
	src = document.all ? event.srcElement : e.target;
	row =FindInChildsTag("TR",src);
	if(row!=null && ActivComboBox!=null){
		ActivComboBox.setSelectedItem(row.rowIndex);
		ActivComboBox.Hide();
		//event.cancelBubble=true;
	}
}

Cit_WebListBox.prototype=new Cit_WebDropDownList();
Cit_WebListBox.prototype.getText=function (item){
	var colomn=0;
	if(arguments.length>1)
		colomn=arguments[1];
		
	if(item>-1 && item<this.oTable.rows.length )
		if (document.all) {
			//alert("a");
			return this.oTable.rows[item].cells[colomn].innerText;
		}
		else {
			//alert("b");
			return this.oTable.rows[item].cells[colomn].childNodes[0].nodeValue;
		}
		
	return "";
}
Cit_WebListBox.prototype.getValue=function (item){
	if(item>-1 && item<this.oTable.rows.length)
		return this.oTable.rows[item].getAttribute("val");
	return "";
}
Cit_WebListBox.prototype.FindItem=function (itemText,colIndex){
	if(itemText.length==0) return -1;
	itemText = itemText.toLowerCase();
	var currow;
	var aRows=this.oTable.rows; 
	var txt;
	for(var i=0;i<aRows.length;i++){ 
		txt= document.all ? aRows[i].cells[colIndex].innerText : aRows[i].cells[colIndex].childNodes[0].nodeValue;
		txt = txt.toLowerCase();
		if(txt.indexOf(itemText,0)==0)
			return i;
	} 
	return -1;
}
Cit_WebListBox.prototype.getSelectedItem=function(){
	var v=parseInt(this.oTable.getAttribute("SelIndex"));
	if(isNaN(v))return -1;
	return v;
}
Cit_WebListBox.prototype.setSelectedItem=function(value){
	last_value=this.getSelectedItem();
	if(value==null)value=-1;
	if(value<-1 || value >= this.oTable.rows.length )return;
	if(value==last_value)return;
	if(last_value!=-1 ){
		oldrow=this.oTable.rows[last_value];
		for(idx=0;idx<oldrow.cells.length;idx++)
		{
			var c=oldrow.cells[idx];
			var cs=c.getAttribute("rStyle");	
			c.style.cssText=cs;
		}
	}
	if(value!=-1 ){
		row=this.oTable.rows[value];
		for(idx=0;idx<row.cells.length;idx++)
		{
			var c=row.cells[idx];
			c.setAttribute("rStyle",c.style.cssText);	
			c.style.cssText=this.selectedStyle;
		}
//		alert(row.scrollIntoView);
//		row.scrollIntoView(true);
	    if(ActivComboBox){
	        le = ActivComboBox.ListElem;
    	    le.scrollTop= row.offsetTop;
    	}    

	}

	this.oTable.setAttribute("SelIndex",value);	
}
Cit_WebListBox.prototype.SelectedNext=function(){this.setSelectedItem(this.getSelectedItem()+1);}
Cit_WebListBox.prototype.SelectedLast=function(){this.setSelectedItem(this.oTable.rows.length-1);}
Cit_WebListBox.prototype.SelectedFirst=function(){this.setSelectedItem(0);}
Cit_WebListBox.prototype.SelectedPrev=function(){this.setSelectedItem(this.getSelectedItem()-1);}




