/* Javascript March 1, 2010*/                             //////////////////////
 ////////////////////////////   N E W  L O O K	  ////////
//                          //////////////////////   	 //
//	BHInput Version 0.8                  			     //
//  (c) Copyright 2007-2009 Howard Brown                     // 
//      			                                   //
///////////////////////////////////////////////////////
function BHInput(bh)
{			
	bhUI.createObject("Input", this);
	var img,w,e,a,m,t;
   	e=typeof(bh.id)=='string'?bhUI.element(bh.id):bh.id;
	if(!e)e=bhUI.createElement();
	this.wrap=div=e.addElement();
	this.skin=bh.skin?bh.skin+'-':'';
	div.className=this.skin+"inputbox";
	div.onclick=div.onmoveover=__EK;
	w=(bh.width?bh.width:e.offsetWidth);
	div.style.width=14+w+'px';
    if(bh.flags&0x10)w+=14;
	w+='px';
	if(bh.flags&0x08){		
		m=div.addElement({name:'INPUT',type:'text',size:'1',value:'',width:w,
		onkeydown:new Function(this.Tx+".event_keyDown()")});
		this.charLimit=bh.limit;
	}else{	
		m=div.addElement({name:bhUI.Browser.IE?'SPAN':'A',style:'readonly',href:'',onclick:__EK,width:w});
	}
	m.addEventListener("blur", new Function(this.Tx+".event_blur()"), false);
	if(!(bh.flags&0x10)){
		a=div.addElement({name:'A',style:'arrow',href:''},{name:'IMG',alt:'menu'});
		this.arrow=a[0];img=a[1];
		img.src="/newlook/theme/default/img/arrow/dropDown.gif";
		img.onmouseover=new Function(this.Tx+".show(this);");
		img.onmouseout=new Function(this.Tx+".autoHide()");
	}
	this.div=div=bhUI.createElement();
	div.style.position='absolute';
	div.style.visibility='hidden';
	div.style.top=div.style.left='-1000px';
	document.body.appendChild(div);
	this.flags=bh.flags;
	this.input=m;
	this.items=new Array();
	this.selectedIndex=-1;
	this.defaultValue='';
	div.onclick=bhUI.eventHandle(this.Tx+".event_click");
	div.onmouseover=new Function("clearTimeout("+this.Tx+".hideTimer);");
	div.onmouseout=new Function(this.Tx+".autoHide()");
	this.itemClick=bh.onclick?bh.onclick:new Function("return true");
	this.input.onclick=new Function(this.Tx+".clearNotice()");
	this.blinking=false;
	this.blinkOn=0;
	this.hideTimer=false;
	this.tag=null;
	this.pairing={};
	this.scroll=m=new BHScroll({skin:this.skin+'inputbox-'});
	div.className=this.skin+"inputbox-list";
	e=div.addElement();
	e.style.position='relative';
  	e.style.overflow='hidden';
	e.style.height='auto';
	this.div2=div=e.addElement();
	div.className='content';
	div.style.position='absolute';
	div.style.top='0px';
	div.style.left='0px';
	m.vscroll.content=m.hscroll.content=div;
	m.auto=true;
	e.parentNode.appendChild(m.vscroll.bar);
	e.parentNode.appendChild(m.hscroll.bar);
	this.tip=div=bhUI.createElement();
	div.style.display='none';
	div.style.position='absolute';
	document.body.appendChild(div);
	if(bh.items){
		this.addItems(bh.items);
		this.selectItem(0);
	}
}

BHInput.prototype.errNotice=function(s)
{
	this.clearNotice();
	this.pairing.lock=true;
	this.blinking=setInterval(this.Tx+".blinker();", 200);
	if(s)this.showTip('-',s);
}

BHInput.prototype.showTip=function(t,tip)
{
	clearTimeout(this.tip.bh_showing);
	var e=this.wrap,left=2,top=0-e.offsetHeight;
	while(e.offsetParent){
		top+=e.offsetTop;left+=e.offsetLeft;
		e=e.offsetParent;
	}
	this.tip.style.top=top+'px';
	this.tip.style.left=left+'px';
	switch(t){
	case "?": t ="ask"; break;
	case "!": t ="warning"; break;
	case "i": t ="inform"; break;
	case "-": t ="error";break;
	default : t ="inform";}
	this.tip.className=this.skin+'inputbox-tip-'+t;
	this.tip.innerHTML=tip;
	this.tip.style.display='block';
	this.tip.style.zIndex=10002;
	this.tip.bh_showing=setTimeout(this.Tx+".tip.style.display='none';",8000);
}

BHInput.prototype.blinker=function()
{
	this.input.style.backgroundColor=this.blinkStatus?'':'#EE0000';
	this.blinkStatus^=1;
}

BHInput.prototype.clearNotice=function()
{
	clearInterval(this.blinking);
	this.blinking=false;
	this.blinkStatus=0;
	this.input.style.backgroundColor='';
	clearTimeout(this.tip.timer);
	this.tip.style.display='none';
	this.pairing.lock=false;
}

BHInput.months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
BHInput.daysofweek = ['Sunday','Monday','Tuesday','Thursday','Friday','Satuaday'];
	
BHInput.prototype.clear=function()
{
	this.div2.innerHTML='';
	this.div2.parentNode.style.height="auto";
	delete this.items;
	this.items=new Array;
	if(this.flags&0x08){
		this.input.value='';
	}
	this.selectedIndex = -1;
}

BHInput.prototype.addItem=function(text,value,id)
{
	var itm=new Object();
	itm.no=this.items.push(itm)-1;
	itm.value=value;
	itm.div=this.div2.addElement("A");
	itm.div.href="";
	itm.div.onclick=__EP;
	itm.div.innerHTML=text;
	itm.div.bh_item=itm;
	if(id)itm.div.id=id;
	this.div2.parentNode.style.height="auto";
	return itm;
}

BHInput.prototype.getItem=function(index)
{
	var e;	
	if( (e=this.items[index]) )
	{
		return {text:e.div.innerHTML,node:e.div};	
	}
	return null;
}

BHInput.prototype.addItems=function(items)
{
	var a,n=1,f,id;
    if(typeof(items)=="string"){
		switch(items.toLowerCase()){
		case 'months':a=BHInput.months;break;
		case 'weekdays':a=BHInput.daysofweek;break;
		case 'numbers':
		var i,s,l,from,to,step;
		from=arguments[1];to=arguments[2];
		step=arguments.length>3?arguments[3]:1;
		s=to.toString();l=s.length;
		for(i=from;i<=to;i+=step){s='0000'+i;this.addItem(s.substring(s.length-l),i);}
		this.flags|=0x04;
		return;}
	}else a=items;
	f=arguments[1]?arguments[1]:0;
	id=arguments[2]?arguments[2]:0;
	if(typeof(a[0])=="string"){
		for(i=0;i<a.length;i++){
			this.addItem(a[i],n,id?id+(i?i:''):0);n++;
		}
	}else{
		for(i=0;i<a.length;i++)
			this.addItem(a[i][0],a[i][1],id?id+(i?i:''):0);
	}
}

BHInput.prototype.show=function()
{
	clearTimeout(this.hideTimer);
	this.pairing.lock=true;
	var div=this.div2.parentNode;
	if(div.style.height=='auto'){
		var h,l;
		this.div.style.width=(this.wrap.offsetWidth-2)+'px';
	    h=this.div2.offsetHeight;
		l=Math.floor(screen.availHeight/4)+12;
		div.style.height=(h>l?l:h)+'px';
		this.scroll.set(1,1);
		this.scroll.redraw();
	}
	var n=2,left=-1,top=this.wrap.offsetHeight,e=this.input;
	if(bhUI.Browser.IE){
		left=0;
		top+=3;
		n=0;
	}
	while(e.offsetParent){
		top+=e.offsetTop;
		left+=e.offsetLeft;
		if(e.cell){
			top+=n;
			left+=1;
		}
		e=e.offsetParent;
	}
	if(top+this.div.offsetHeight>(document.body.offsetHeight)){
	 	top-=((this.div.offsetHeight+this.wrap.offsetHeight+2));
	}
	this.div.style.top=top+'px';
	this.div.style.left=left+'px';
	this.div.style.width=(this.wrap.offsetWidth-2)+'px';
	this.div.style.zIndex=10000;
	this.div.style.visibility='visible';
	this.arrow.className='arrow-hovered';
}

BHInput.prototype.pair=function(e,callback,ref)
{
	if(this.wrap==e||this.pairing.lock)return;
	e.parentNode.insertBefore(this.wrap,e);
	if(this.pairing.linked!=e){
		this.pairing.node=e;
		this.setText(e.nodeName=='#text'?e.data:e.innerHTML);
	}
	this.pairing.linked=e;
	this.pairing.callback=callback;
	this.pairing.ref=ref;
	setTimeout(this.Tx+".input.focus();",10);
	if(this.flags&0x08){
		setTimeout(this.Tx+".input.select();",20);
	}
}

BHInput.prototype.hide=function()
{
	this.div.style.visibility='hidden'; 
  	this.arrow.className="arrow";
  	this.pairing.lock=false;
}

BHInput.prototype.autoHide=function()
{
	this.hideTimer = setTimeout(this.Tx+".hide()",100);
}


BHInput.prototype.getText=function()
{
	return this.flags&0x08?this.input.value:this.input.innerHTML;
}

BHInput.prototype.setText=function(text)
{
	if(this.flags&0x08){
		this.input.value=text;
		this.textChanged();
		this.clearNotice();
	}else{
		this.input.innerHTML=text;
	}
}

BHInput.prototype.selectItem=function(index)
{
	var e,s,itm=this.items[index];
	s=(this.flags&0x01)?itm.no:(this.flags&0x02)?itm.value:itm.div.innerHTML;
	if(this.flags&0x08){
		this.input.value=s;	
		this.clearNotice();
	}else{
		this.input.innerHTML=s;
	}
	if((e=this.pairing.node))e[e.nodeName=='#text'?'data':'innerHTML']=s;
	this.selectedIndex=itm.no;
	this.value=itm.value;
}

BHInput.prototype.setDefault=function(s)
{
	var i;
	if((i=this.indexOf(s))!=-1){
		this.defaultValue=i;
	}else this.defaultValue=s;
}

BHInput.prototype.getDefault=function()
{
	return this.defaultValue;
}

BHInput.prototype.setLimit=function(limit)
{
	this.charLimit=limit;
}

BHInput.prototype.indexOf=function(s)
{
	var i,value;
	if(this.items.length){
		switch((this.flags&0x04)?1:(this.flags&0x01)?3:(this.flags&0x02)?4:2){
		case 1:{
			value=(typeof(this.items[0].value)=='string')?s.toLowerCase():parseInt(s,10);
			for(i=0;i<this.items.length;i++)
			if(value==this.items[i].value)return i;
			break;}
		case 2:{
			value=s.toLowerCase();
			for(i=0;i<this.items.length;i++)
			if(value==this.items[i].div.innerHTML.toLowerCase())return i;
			break;}
		case 3:{	
			value=parseInt(s,10);
			for(i=0;i<this.items.length;i++)
			if(value==this.items[i].no)return i;
			break;}
		case 4:{
			value=s.toLowerCase();
			for(i=0;i<this.items.length;i++)
			if(value==this.items[i].value.toLowerCase())return i;
			break;}
		default:{
			}
		}
	}
	return -1;	
}

BHInput.prototype.textChanged=function()
{
	var i;
	if((i=this.indexOf(this.input.value))!=-1){
		this.selectItem(i);
	}else{
		this.selectedIndex=i;
		if(this.flags&0x20){
			if(this.items.length)
				this.selectItem(this.defaultValue+0);
			else
			if(!this.input.value.trim())
				this.input.value=this.defaultValue;
		}
	}
	if((i=this.pairing.node))i[i.nodeName=='#text'?'data':'innerHTML']=this.input.value;
}

BHInput.prototype.parseMonth=function()
{
	var value=this.input.value;
	if(isNaN(value)){
	value=value.toLowerCase();
	for(var i=0;i<BHInput.months.length;i++){
	if(value==BHInput.months[i].toLowerCase()){
	return (i+1);}}
	}else{
	value=parseInt(value,10);
	if(value>=1 && value<=12){
	return value;}}
	return false;
}

BHInput.prototype.parseDayOfWeek=function()
{
	var value=this.input.value;
	if(isNaN(value)){
	value=value.toLowerCase();
	for(var i=0;i<BHInput.daysofweek.length;i++){
	if(value==BHInput.daysofweek[i].toLowerCase()){
	return (i+1);}}
	}else{
	value=parseInt(value,10);
	if(value>=1 && value<=7){
	return value;}}
	return false;
}

BHInput.prototype.parseDay=function()
{
	var day=parseInt(this.input.value, 10);
	if(day >= 1 && day <= 31)
	    return day;
	return false;
}


BHInput.prototype.charChecker=function()
{
	var l=this.input.value.length;
	if(this.charLimit.max)
	{
		if(l>this.charLimit.max){
			l-=this.charLimit.max;
			this.showTip('i',l+' character'+(l>1?'s':'')+' over');
		}else{
			l=this.charLimit.max-l;
			this.showTip('i',l+' character'+(l>1?'s':'')+' remain');
		}
	}
}

BHInput.prototype.event_blur=function()
{
	if(this.pairing.lock)return;
	if(this.flags&0x08)this.textChanged();
	if(this.pairing.linked){
		this.clearNotice();
		this.pairing.linked.parentNode.removeChild(this.wrap);
		this.pairing.linked=null;
		if((this.f=this.pairing.callback))this.f(this.pairing.ref);
	}
}

BHInput.prototype.event_click=function(evt)
{	
	clearTimeout(this.hideTimer);
	var e=evt.target;
	while(e&&!e.bh_item)e=e.parentNode;if(!e)return;
	var value,itm=e.bh_item;
	value=(this.flags&0x01)?itm.no:(this.flags&0x02)?itm.value:e.innerHTML;
	if(this.itemClick(itm.no,value,itm.value)){
		setTimeout(this.Tx+".input.focus();",10);
		if(this.flags&0x08){
			this.input.value=value;	
			setTimeout(this.Tx+".input.select();",20);
		}else{
			this.input.innerHTML=value;
		}
		this.hide();
		this.selectedIndex=itm.no;
		if(this.pairing.node){
			this.pairing.node[this.pairing.node.nodeName=='#text'?'data':'innerHTML']=value;
			this.event_blur();
		}
	}
	this.clearNotice();
}

BHInput.prototype.event_keyDown=function()
{
	if(this.flags&0x80){
		clearTimeout(this.charChecking);
		this.charChecking=setTimeout(this.Tx+".charChecker()",30);
	}
}
