// JavaScript Document
//Preloading Images
var img1 = new Image();
img1.src = root+'images/01.gif';
var img2 = new Image();
img2.src = root+'images/02.gif';


var IE = document.all?true:false;
var tempX = 0;
var tempY = 0;
var comboLeft = 0;
var comboRight = 0;
var comboTop = 0;
var comboBottom = 0;
var comboCtrl;


var combo = function (){
	this.generateCombo = function (cname,opts,w,callback,cid){
		if(!cid) cid = cname;
		if(w) var wid = w+"px"; else wid = false;
		var str = "";
		str = '<div class="jsSelect_mainDiv" style="width:'+wid+';float:left;" id="'+cname+'_jsSelect_main" onclick="combo.showSelect(\''+cname+'_jsSelect_optionContainer\',\''+cname+'_jsSelect_selTxt\');" onmouseover="combo.toggleImage(\''+cname+'_jsSelect_selImage\');" onmouseout="combo.toggleImage(\''+cname+'_jsSelect_selImage\');">'+
				'<div class="jsSelect_selTxt" id="'+cname+'_jsSelect_selTxt" style="float:left;">'+
				  '<div class="jsSelect_textNode" id="'+cname+'_jsSelect_textNode" style="float:left; width:'+(parseInt(w)-combo.getImageWidth())+'px;">Select</div>'+
				  '<div class="jsSelect_imgNode" id="'+cname+'_jsSelect_imgNode" style="float:right;"><img id="'+cname+'_jsSelect_selImage" src="'+combo.getImage()+'" /></div>'+
				'</div>'+
				'<div class="jsSelect_innerDiv" style="float:left;" id="'+cname+'_jsSelect_innerDiv">'+
				 '<div id="'+cname+'_jsSelect_optionContainer" class="jsSelect_optionContainer" style="float:left; width:'+(parseInt(w)-2)+'px;">';
				 
			if(opts){
				for(i=0; i<opts.length; i++){	 
			str += '<div style="float:left" class="jsSelect_optionDiv" onmouseover="this.className=\'jsSelect_mouse-over\'" onmouseout="this.className=\'jsSelect_optionDiv\'"  onclick="combo.selectOpt(\''+cname+'_jsSelect_textNode\',\''+opts[i][0]+'\',\''+cid+'\',this.innerHTML);'+callback+'('+opts[i][0]+');">'+
					'<div class="jsSelect_leftOption" style="float:left;">'+opts[i][1]+'</div>'+
					'<div class="jsSelect_rightOption" style="float:right;">'+opts[i][2]+'</div>'+
				  '</div>';
				}
			}
				  
		   str += '</div>'+
				'</div>'+
			  '</div>'+
			  '<input type="hidden" name="'+cname+'" id="'+cid+'" value="" />'
			  ;
		return str;
	}
	///////////////////////////////////////////////////////
	this.toggleImage = function (elm){
		var elm = document.getElementById(elm);
		if(elm.src == img1.src)
			elm.src = img2.src;
		else
			elm.src = img1.src;
	}
	////////////////////////////////////////////////////
	this.showSelect = function (elmid,heightDiv){
		//alert("here");
		var elm = document.getElementById(elmid);
		//alert(elm);
		//alert(mainDiv.offsetLeft);
		comboCtrl = elm;
		
		if(elm.style.display == "block"){
			elm.style.display = "none";			
		}else{
			elms = document.getElementsByTagName("div");
			for(i=0; i<elms.length; i++){
				if(elms[i].className == 'jsSelect_optionContainer'){
					elms[i].style.display = 'none';
				}
			}
			elm.style.display = "block";
		}
		comboLeft = elm.offsetLeft;
		comboRight = comboLeft+elm.offsetWidth;
		comboTop = elm.offsetTop-(document.getElementById(heightDiv).offsetHeight);
		comboBottom = comboTop+elm.offsetHeight;

	}
	///////////////////////////////////////////////
	this.selectOpt = function (elm,val,ctrl_id,divTxt){
		//alert(elm);
		elm = document.getElementById(elm);
		document.getElementById(ctrl_id).value = val;
		elm.innerHTML = divTxt;
	}
	///////////////// For Default Selection//////////////////////////////
	this.defaultSelectOpt = function (elm,ctrl_id,val,leftVal,RightVal){
		if(!ctrl_id) ctrl_id = elm;
		if(!leftVal) lefVal = "";
		if(!RightVal) RightVal = "";
		elm += "_jsSelect_textNode";
		elm = document.getElementById(elm);
		//alert(elm.innerHTML);
		document.getElementById(ctrl_id).value = val;
		elm.innerHTML = '<div class="jsSelect_leftOption" style="float:left;">'+leftVal+'</div>'+
					'<div class="jsSelect_rightOption" style="float:right;">'+RightVal+'</div>';
	}
	/////////////////////////////////////////////
	this.getImage = function (){
		return img1.src;
	}
	////////////////////////////////////////
	this.getImageWidth = function (){
		var w = parseInt((img1.width > img2.width)?img1.width:img2.width)+16;
		return (w > 16)?w:24;
	}
	////////////////////////////////////////
}

document.onclick = getMouseY;
 function getMouseY(e) {
		if (!IE) {
			tempY = e.pageY;
			tempX = e.pageX;
		} else {
			tempY = event.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
			tempX = event.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
		}
	//	alert(tempY);
	//alert("tempX: "+tempX+"px tempY: "+tempY+"px ComboLeft: "+comboLeft+"px ComboRight: "+comboRight+"px comboTop: "+comboTop+"px comboBottom: "+comboBottom);
		if(tempX < comboLeft || tempX > comboRight || tempY > comboBottom || tempY < comboTop){
			if(comboCtrl)
			comboCtrl.style.display = 'none';
		}
	}
