var def_SelectBoxID = "AutoSelectBox";
var sel_status = new Array();
var def_ValList = "";
var def_ValArr = "";
var def_Maxwidth = "100";
var def_Maxchar = "30";
var def_Maxheight = "110";
var def_AutoTrunc = true;
var def_Freetype = false;
var def_DefaultValue = "";
var def_DefaultName = "";
var def_scrolling = true;
var def_OnSelect = "";
var def_MultiBoxID = "MultiSelectBox";
var def_Multiwidth = "100";
var def_MultiDefList = "";
var def_MultiDefArr = "";
var def_PathToImages = "/images/select/" 
var clientOpera = navigator.userAgent.indexOf("Opera") > -1;
var clientIE = navigator.userAgent.indexOf("MSIE") > 1 && !clientOpera;
var clientIE7 = navigator.userAgent.indexOf("MSIE 7") > 1 && !clientOpera;
var clientMozilla = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !clientOpera;
var isSelectingField;
function highlightText(textboxObjBnw, iStart, iEnd) {
  switch(arguments.length) {
    case 1:      textboxObjBnw.select();
    break;
    case 2:      iEnd = textboxObjBnw.value.length;
    case 3:      if (clientIE || clientIE7) {
      var oRange = textboxObjBnw.createTextRange();
      oRange.moveStart("character", iStart);
      oRange.moveEnd("character", -textboxObjBnw.value.length + iEnd);
      oRange.select();
    }
    else if (clientMozilla || clientOpera){
      textboxObjBnw.setSelectionRange(iStart, iEnd);
    }
  }
  try{
  textboxObjBnw.focus();
  return true;
	}catch(err){
	return false;
	}
}
function updateTextbox(textboxObjBnw, sText) {
  if (clientIE || clientIE7) {
    var oRange = document.selection.createRange();
    oRange.text = sText;
    oRange.collapse(true);
    oRange.select();
  }
  else if (clientMozilla || clientOpera) {
    var iStart = textboxObjBnw.selectionStart;
    textboxObjBnw.value = textboxObjBnw.value.substring(0, iStart) + sText + textboxObjBnw.value.substring(textboxObjBnw.selectionEnd, textboxObjBnw.value.length);
    textboxObjBnw.setSelectionRange(iStart + sText.length, iStart + sText.length);
  }
  textboxObjBnw.focus();
}


function checkForEmpty(textboxObjBnw, oEvent, BS) {
  if ((oEvent.keyCode==8 || oEvent.keyCode==46 ) ) {
    if (textboxObjBnw.value!="") {
      return true;
    }
    else {
      return popupSelectBox(textboxObjBnw, oEvent, BS);
    }
  }
}

function getMatchIndex(sText, valArr) {
  for (var i=0; i < valArr.length; i++) {
    thisValue = ""+valArr[i][0];
    if (thisValue.toLowerCase().indexOf(sText.toLowerCase()) == 0) {
      return i;
    }
  }
  return null;
}

function popupSelectBox(textboxObjBnw, oEvent, BS) {
 
 
 
  if (BS.onSelect=="") {
    popupSelectBoxFast(textboxObjBnw, oEvent, BS)
  }
  else {
    var n,i;
    var popupSelectTable;
    var popupSelectArr = new Array();
    i=0;
    n=0;
    if (BS.maxheight > 0) {
      selectBoxDiv = document.getElementById("p"+BS.selectBoxID);
      popupSelectArr[popupSelectArr.length] = "<table id='t"+BS.selectBoxID+"' cellpadding=0 cellspacing='0' border='1' class='autoSelectTable'>";
      re = new RegExp(/%20/ig);
      onSelectEsc = BS.onSelect.replace(re, "\"");
      nameFound = false;
      for (var i=0; i < BS.valArr.length; i++) {
        if (BS.autotrunc) {
          if (n <= BS.valArr.length) {
            thisValue = ""+BS.valArr[i][0];
            if (thisValue.toLowerCase().indexOf(textboxObjBnw.value.toLowerCase()) == 0) {
              nameFound = true;
            }
            if (nameFound==true) {
              n=n+1;
              popupSelectArr[popupSelectArr.length]= "<tr><td ";
              popupSelectArr[popupSelectArr.length]= " onMouseOver='return mouseOverSelectBoxField(this);' onMouseOut='return mouseOutSelectBoxField(this);' style='" + fontweight + BS.bfontcolor + ";padding-left:" + bpaddingleft + "px' onClick='clickOnSelectBoxField("+BS.name+", \""+BS.valArr[i][1]+"\");";
              if (onSelectEsc=="") {
                popupSelectArr[popupSelectArr.length]= "'";
              }
              else {
                popupSelectArr[popupSelectArr.length]= onSelectEsc + "'";
              }
              popupSelectArr[popupSelectArr.length]= ">" + BS.valArr[i][0] + "</td></tr>";
            }
          }
          else {
            break;
          }
        }
        else {
          popupSelectArr[popupSelectArr.length]= "<tr><td ";
          popupSelectArr[popupSelectArr.length]= " onMouseOver='return mouseOverSelectBoxField(this);' onMouseOut='return mouseOutSelectBoxField(this);' style='" + fontweight + BS.bfontcolor + "' onClick='clickOnSelectBoxField("+BS.name+", \""+BS.valArr[i][1]+"\"); ";
          if (onSelectEsc=="") {
            popupSelectArr[popupSelectArr.length]= "'";
          }
          else {
            popupSelectArr[popupSelectArr.length]= onSelectEsc + "'";
          }
          popupSelectArr[popupSelectArr.length]= ">" + BS.valArr[i][0] + "</td></tr>";
        }
      }
      popupSelectArr[popupSelectArr.length]= "</table>";
      var innerText = popupSelectArr.join("");
      selectBoxDiv.innerHTML = innerText;
      selectBoxDiv.style.width=textboxObjBnw.offsetWidth+14+'px';
      selectBoxDiv.style.display='block';
      selectBoxDiv.style.height=BS.maxheight+"px";
      selectBoxTable = document.getElementById("t"+BS.selectBoxID);
      selectBoxTable.style.width = textboxObjBnw.offsetWidth + 'px';
      if (parseInt(selectBoxTable.offsetHeight) < parseInt(BS.maxheight)) {
        selectBoxDiv.style.height = selectBoxTable.offsetHeight+1+"px";
      }
    }
  }
  return true;
}


function popupSelectBoxFast(textboxObjBnw, oEvent, BS) {
  var n,i;
  var popupSelectTable;
  i=0;
  n=0;
  if (BS.maxheight > 0) {
    selectBoxDiv = document.getElementById("p"+BS.selectBoxID);
    selectBoxDiv.innerHTML = "";
    oTable = document.createElement("TABLE");
    oTable.id = "t"+BS.selectBoxID;
    oTable.className = "autoSelectTable";
    oTable.setAttribute('cellPadding',0);
    oTable.setAttribute('cellSpacing',0);
    var trElem, tdElem, txtNode;
    re = new RegExp(/%20/ig);
    onSelectEsc = BS.onSelect.replace(re, "\"");
    nameFound = false;
    for (i=0; i < BS.valArr.length; i++) {
      if (BS.autotrunc) {
        if (n <= BS.valArr.length) {
          thisValue = ""+BS.valArr[i][0];
          if (thisValue.toLowerCase().indexOf(textboxObjBnw.value.toLowerCase()) == 0) {
            nameFound = true;
          }
          if (nameFound==true) {
            n=n+1;
            trElem = oTable.insertRow(oTable.rows.length);
            tdElem = trElem.insertCell(trElem.cells.length);
            tdElem.id = "t"+BS.selectBoxID+"_row_"+i;
            tdElem.onmouseover=function(){
              return mouseOverSelectBoxField(this);
            }
            ;
            tdElem.onmouseout=function(){
              return mouseOutSelectBoxField(this);
            }
            ;
            tdElem.onclick = function(){
              var rowcount = this.id;
              rowcount = rowcount.substring(rowcount.lastIndexOf("_")+1);
              clickedValue = BS.valArr[rowcount][1];
              hiddenValue = document.getElementById("h"+BS.selectBoxID);
              visibleValue = document.getElementById("v"+BS.selectBoxID);
              for (var z=0; z < BS.valArr.length; z++) {
                thisValue = ""+BS.valArr[z][1];
                if (thisValue.indexOf(clickedValue) == 0) {
                  visibleValue.value = BS.valArr[z][0];
                  hiddenValue.value = BS.valArr[z][1];
                  break;
                }
              }
              selectBoxDiv = document.getElementById("p"+BS.selectBoxID);
              selectBoxDiv.style.display='none';
              return true;
            }
            ;
            txtNode = document.createTextNode(BS.valArr[i][0]);
            tdElem.appendChild(txtNode);
          }
        }
        else {
          break;
        }
      }
      else {
        trElem = oTable.insertRow(oTable.rows.length);
        tdElem = trElem.insertCell(trElem.cells.length);
        tdElem.id = "t"+BS.selectBoxID+"_row_"+i;
        tdElem.onmouseover=function(){
          return mouseOverSelectBoxField(this);
        }
        ;
        tdElem.onmouseout=function(){
          return mouseOutSelectBoxField(this);
        }
        ;
        tdElem.onclick = function(){
        	
          var rowcount = this.id;
          rowcount = rowcount.substring(rowcount.lastIndexOf("_")+1);
          clickedValue = BS.valArr[rowcount][1];
          hiddenValue = document.getElementById("h"+BS.selectBoxID);
          visibleValue = document.getElementById("v"+BS.selectBoxID);
          for (var z=0; z < BS.valArr.length; z++) {
            thisValue = ""+BS.valArr[z][1];
            if (thisValue.indexOf(clickedValue) == 0) {
              visibleValue.value = BS.valArr[z][0];
              hiddenValue.value = BS.valArr[z][1];
              break;
            }
          }
          selectBoxDiv = document.getElementById("p"+BS.selectBoxID);
          selectBoxDiv.style.display='none';
         
          if (BS.onselect){
          eval(BS.onselect + '("' + visibleValue.value + '",' + hiddenValue.value + ')');
        	}
          return true;
        }
        ;
        txtNode = document.createTextNode(BS.valArr[i][0]);
       	tdElem.appendChild(txtNode);
       	if (BS.bold == 0){
       	tdElem.style.fontWeight = 'normal';
       	}
       	
       	if (BS.bfontcolor != ''){
       	tdElem.style.color = BS.bfontcolor;
       	}
      }
    }
    selectBoxDiv.appendChild(oTable);
    selectBoxDiv.style.width=textboxObjBnw.offsetWidth+(BS.scrolling ? 2 : 2) + (13) + 'px';
    selectBoxDiv.style.display='block';
    selectBoxDiv.style.height=BS.maxheight+"px";
    selectBoxTable = document.getElementById("t"+BS.selectBoxID);
    selectBoxTable.style.width = textboxObjBnw.offsetWidth-(BS.scrolling ? 4 : 0)+'px';
    if (parseInt(selectBoxTable.offsetHeight) < parseInt(BS.maxheight)) {
      selectBoxDiv.style.height = selectBoxTable.offsetHeight+1+"px";
    }
  }
  return true;
}
function hideSelectBox(BS,e) {
  //e = (e) ? e : ((window.event) ? window.event : "")
	//var tg = (window.event) ? e.srcElement : e.target;
  
 //alert(tg.tagName);

  if (!isSelectingField) {
    selectBoxDiv = document.getElementById("p"+BS.selectBoxID);
    selectBoxDiv.style.display='none';
    hiddenValue = document.getElementById("h"+BS.selectBoxID);
    visibleValue = document.getElementById("v"+BS.selectBoxID);
    if (!visibleValue.value) {
      hiddenValue.value = "";
      visibleValue.value = "";
    }
    else if (hiddenValue.value) {
      for (var i=0; i < BS.valArr.length; i++) {
        thisValue = ""+BS.valArr[i][1];
        if (thisValue.indexOf(hiddenValue.value) == 0) {
          visibleValue.value = BS.valArr[i][0];
          return true;
        }
      }
    }
    else {
      hiddenValue.value = "";
      visibleValue.value = "";
    }
  }
  return true;
}

function mouseOverSelectBox() {
  isSelectingField = true;
  return true;
}
function mouseOutSelectBox() {
  isSelectingField = false;
  return true;
}
function mouseOverSelectBoxField(oTableRow) {
  // clr = oTableRow.style.color;
  oTableRow.className = "autoSelectRowOn";
  // if (clr){
  oTableRow.style.color = '#FFFFFF';
  //}
  
  return true;
}
function mouseOutSelectBoxField(oTableRow) {
  // clr = oTableRow.style.color;
  oTableRow.className = "autoSelectRowOff";
//  if (clr){
  oTableRow.style.color = '#222222';
//	}
  return true;
}


function selectSelectBox(textboxObjBnw, oEvent, BS) {
  if (highlightText(textboxObjBnw, 0, textboxObjBnw.value.length)){
  return popupSelectBox(textboxObjBnw, oEvent, BS);
	}else{
	return false;	
	}	
}

function toggleChecked(oTableRow, checkBoxID) {
  checkBoxElm=document.getElementById(checkBoxID);
  if (checkBoxElm.checked) {
    oTableRow.className = "multiRowUnchecked";
    checkBoxElm.checked=false;
  }
  else {
    oTableRow.className = "multiRowChecked";
    checkBoxElm.checked=true;
  }
  return true;
}

var fontweight;
var bfontColor;
function createSelectBox(BS) { 
var selectBoxHTML; 


if (BS.bold == 0 )
{
	fontweight = "font-weight:normal;";
}else{
	 fontweight = "";
}


if (BS.bfontcolor != '' )
{
	bfontColor = "color:" + BS.bfontcolor;

}else{
	 bfontColor = "";
}


if (BS.lightv != '' )
{
	bgpdimg = ((BS.lightv == '#316AC5' || BS.lightv == 'blue') ? ';background: url(/skins/common/images/select/pulldownarrow_blue_light.gif);background-repeat' : ';background: url(/skins/common/images/select/pulldownarrow_pink_light.gif);') + ';no-repeat;background-position:right;';
}else{
 	bgpdimg = "";
}

if (BS.bordercolor != '' )
{

	bborderColor = ";border:1px solid " + BS.bordercolor;
	bborderColor2 = ";border-right:0px;"
	bborderColor3 = ";border-top:0px;"
	
}else{
	 bborderColor = "";
	 bborderColor2 = "";
	 bborderColor3 = "";
}

if (BS.paddingleft != '' )
{
	bpaddingleft = BS.paddingleft;
}else{
	bpaddingleft = 7;
}


if (BS.onselect != '' ){
ponselect = BS.onselect;
}else{
ponselect = '';
}


if (BS.selectBoxID==null||BS.selectBoxID=="") {
  BS.selectBoxID = def_SelectBoxID;
}
if (BS.valList==null||BS.valList=="") {
  BS.valList = def_ValList;
}
if (BS.valArr==null||BS.valArr=="") {
  BS.valArr = parseToArray(BS.valList);
}
if (BS.maxwidth==null||BS.maxwidth=="") {
  BS.maxwidth = def_Maxwidth;
}
if (BS.maxchar==null||BS.maxchar=="") {
  BS.maxchar = def_Maxchar;
}
if (BS.maxheight==null||BS.maxheight=="") {
  BS.maxheight = def_Maxheight;
}
if (BS.pathToImages==null||BS.pathToImages=="") {
  BS.pathToImages = def_PathToImages;
}
try {
  if (!BS.hasOwnProperty("autotrunc")) {
    BS.autotrunc = def_AutoTrunc;
  }
  if (!BS.hasOwnProperty("freetype")) {
    BS.freetype = def_Freetype;
  }
}
catch (e) {
}
if (BS.defaultName==null || BS.defaultName=="") {
  BS.defaultName = def_DefaultName;
}
if (BS.defaultValue==null || BS.defaultValue=="") {
  BS.defaultValue = def_DefaultValue;
}
if (BS.onSelect==null||BS.onSelect=="") {
  BS.onSelect = def_OnSelect;
  ponselect = '';
}
else {
 // re = new RegExp(/'/gi);
  //BS.onSelect = BS.onSelect.replace(re, "%20");
  
}


selectBoxDiv = document.getElementById(BS.selectBoxID);
selectBoxHTML = "<input  autocomplete='off'  type='text' class='autoSelectTextBox' id='v"+BS.selectBoxID+"' maxlength='"+BS.maxchar+"'  style='" + fontweight + bfontColor + ";padding-left:" + bpaddingleft + "px;" + bborderColor + bborderColor2 + "'  value='"+BS.defaultName+"' ";
selectBoxHTML = selectBoxHTML + " pevent=\"" + ponselect + "\" onclick='if(document.getElementById(\"p"+BS.selectBoxID+"\").style.display!=\"block\"){return selectSelectBox(this, event, "+BS.name+");}else{hideSelectBox("+BS.name+",event);this.blur()}' onfocus='' onKeyPress='return false' onblur='return hideSelectBox("+BS.name+",event);' onKeyUp='return checkForEmpty(this, event, "+BS.name+");' />";
selectBoxHTML = selectBoxHTML + "<img " + (clientIE ? "onmousedown" : "onclick") +  "='var thisTextBox=document.getElementById(\"v"+BS.selectBoxID+"\");return selectSelectBox(thisTextBox, event, "+BS.name+");' src='" + BS.pathToImages + "clear.gif' class='autoSelectDropImage' style='" + bborderColor + bgpdimg + "; " + (clientIE7 ? 'margin-top: 1px;' : 'margin-top: 0px;') + "' onclick='' />&nbsp;&nbsp;&nbsp;";
selectBoxHTML = selectBoxHTML + "<br /><div class='autoSelectDiv scrollbars' style='" + (BS.scrolling ? 'overflow:auto' : '') + ";" + fontweight + bfontColor +  bborderColor + bborderColor3 + "' id='p"+BS.selectBoxID+"'   onMouseOver='return mouseOverSelectBox();' onMouseOut='return mouseOutSelectBox();' onblur='return hideSelectBox("+BS.name+",event);'></div>";
selectBoxHTML = selectBoxHTML + "<input type='hidden' value='"+BS.defaultValue+"' name='"+BS.selectBoxID+"' id='h"+BS.selectBoxID+"' />";
selectBoxDiv.innerHTML = selectBoxHTML;
selectTextBoxDiv = document.getElementById("v"+BS.selectBoxID);
selectTextBoxDiv.style.width= parseInt(BS.maxwidth) +'px';
return true;

}



function parseToArray(sText) {
  var arr = new Array();
  if (sText == null) {
    sText = "";
  }
  arr = sText.split("|");
  for(i = 0; i < arr.length; i++)
    {
      arr2 = new Array();
      if (arr[i].indexOf(";")>0)
        {
          arr2 = arr[i].split( ";" );
        }
      else {
        arr2[0] = arr[i];
        arr2[1] = arr[i];
      }
      arr[i] = arr2;
    }
  return arr;
}

function flattenArray(sArray) {
  var sFlattened = "";
  if (sArray != null && sArray.length>0) {
    for (var i=0; i < sArray.length; i++) {
      if ((sArray[i][0] != null) && (sArray[i][0] != "")) {
        sFlattened += sArray[i][0] + ";";
        if ((sArray[i][1] != null) && (sArray[i][1] != "")) {
          sFlattened += sArray[i][1] + "|";
        }
        else {
          sFlattened += sArray[i][0] + "|";
        }
      }
    }
  }
  return sFlattened;
}
