var curId = '';
var option_placeholder;

// This example simple populates a list of integers between the range specified
// params: max (top limit), id (id of target textbox), increment (increments the option value), begin (first option value), align (horizontal)
// in this example we will increment by five; so the values in a "minutes" textbox for a timepicker will be: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55
function alrt(){
	alert('DID GET HERE');
}

function onsel(dt1,qaz,option,ik1){
	// alert('in onsel');
	// alert(k1);
	  var k1 = ik1;
   if(k1 == undefined){
	   k1 = event.keyCode;
   }
	qaz.value = dt1.value;
	
	
	// document.form1.selidx.value = dt1.selectedIndex;
	// document.form1.f1.value = k1;
		if(k1==13){
		 	option_placeholder.innerHTML = '';
		}
		if((k1==38 && dt1.selectedIndex == 0) || ( k1 == 40 && dt1.selectedIndex == dt1.length - 1)){
			qaz.focus();
			option_placeholder.innerHTML = '';
		}
		 // alert('curId=' + curId);
		 curId = '';
}
function onclk(dt1,qaz,option){
	// alert('in onsel');
	// alert(k1);
	qaz.value = dt1.value;
	option_placeholder.innerHTML = '';
		 curId = '';
}

function buildOptions2( whichone,align,str1,oph,ikeyt,clk,subz) {
		subz.style.visibility = "visible";
		buildOptions( whichone,align,str1,oph,ikeyt,clk);
}

function buildOptions( whichone,align,str1,oph,ikeyt,clk) {

	if(navigator.appName != 'NetscapeZZZZ') {
		
	
		// alert('event.which =' +  keyt);
	var makeItMid = '';
   var id = whichone.id;
   var x1 =  whichone.value.toUpperCase();
    
   var keyt = ikeyt;
   if(keyt == undefined){
	   keyt = event.keyCode;
   }
   
   // document.form1.keyt.value = event.keyCode;
    // document.form1.keyt.value = event.keyCode;
   
   // alert(x1);
   // document.form1.f1.value = keyt;

    var box = document.getElementById(id,oph);
         option_placeholder = document.getElementById(oph);
        if (option_placeholder.style.display=='none' || curId != id  ) {
            option_placeholder.style.display = 'none';
            openCombo(id, oph);
          	
  				var sel = '';
                var i = 0;
				var xv = 0;
				var cnt = 0;
				var val = str1.split(",");
											
                while (i < val.length - 1) {
					
					if(val[i].toUpperCase().substring(0,x1.length) == x1.toUpperCase() || clk == 1 ) {
						if(cnt == 0 && keyt == 40){
							sel = '  selected="selected"  ';
						}else{
							sel = '';
						}
						makeItMid += '<option value="' + val[i].replace(/ \*/g,"") +  '"' + sel +  '>' +  val[i] + '</option>';
						cnt = cnt + 1;
					}
					i = i + 1;
                }
				cnt2 = cnt + 2;
				if(cnt > 11){
					cnt2 = 12;
				}
				
				var makeItStart = '<select name="listit" id="listit" size="' + cnt2 + '" onclick="onclk(this,' + whichone.form.name + "." +  whichone.name + ',' + '\'option_placeholder\'' + ')"  onkeyup="onsel(this,' + whichone.form.name + "." +  whichone.name + ',' + '\'option_placeholder\'' + ',event.which)" ><option value="">DELETE</option>';
                var makeItEnd = '<option value=""></option></select>';
				var makeIt = makeItStart + makeItMid + makeItEnd;
                option_placeholder.innerHTML = makeIt;
				// alert(clk + '=keyt=' + keyt);
				 if(keyt == 40 ){
					document.getElementById('listit').focus();
				 }				
				if(cnt == 0 || keyt == 13){
					 option_placeholder.innerHTML = '';
				}
				
				curId = '';
				
        } else {
            option_placeholder.style.display='none';
        }
	}
}

// hover effect on the dropdownlist arrow icon
function swapImage(img, src) {
    var img = document.getElementById(img);
    img.src = src;
}

// check the click events are not occurring on the controls
// if the mouse click event occurs outside the control, options should close
document.onclick=check; 
function check(e,oph){
    var target = (e && e.target) || (event && event.srcElement); 
    var obj1 = document.getElementById(oph); 
    checkParent(target)?obj1.style.display='none':null; 
} 
function checkParent(t,oph){ 
    while(t.parentNode){ 
        var myPlaceholder = document.getElementById(oph);
        var myTextbox = document.getElementById('text1');
        var myImage = document.getElementById('imgText1');
        var myTextbox2 = document.getElementById('text2');
        var myImage2 = document.getElementById('imgText2');
        if(t==myPlaceholder ||  t==myTextbox || t==myImage ||  t==myTextbox2 || t==myImage2){ 
            return false 
        } 
        t=t.parentNode 
    } 
    return false 
} 

// show the options where the event took place
 function openCombo(textbox, div) {
        var div = document.getElementById(div);
        var box = document.getElementById(textbox);
         div.style.position = 'absolute'; 
        if(div.style.display=='none') {
        var X = (findPosX(box));
        var Y = findPosY(box) + 19;
		// alert(X + ',' + Y);

		div.style.left =  (X - 1) + 'px'; 
        div.style.top =  (Y ) + 'px';
         div.style.display = '';
        } else {
            div.style.display = 'none';
        }
    }
    
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}