function placeFocus() {
if (document.forms.length > 0) {
for (i = 0; i < document.forms[0].length; i++) {
if (((document.forms[0].elements[i].type == "text") || (document.forms[0].elements[i].name == "textarea")) && !(document.forms[0].elements[i].disabled)) {
  document.forms[0].elements[i].focus();
break;
         }
      }
   }
}

function SiteformHandler(Siteform){
var URL = document.Siteform.Sites.options[document.Siteform.Sites.selectedIndex].value;
window.location.href = URL;
}

function templateformHandler(templateform){
var URL = document.templateform.TemplateGUID.options[document.templateform.TemplateGUID.selectedIndex].value;
window.location.href = URL;
}

function FieldformHandler(fieldform){
var URL = document.fieldform.FieldGUID.options[document.fieldform.FieldGUID.selectedIndex].value;
window.location.href = URL;
}

function TypeformHandler(typeform){
var URL = document.typeform.SearchType.options[document.typeform.SearchType.selectedIndex].value;
window.location.href = URL;
}

function AuthorityformHandler(authorityform){
var myindex=authorityform.TemplateGUID.selectedIndex
parent.hitlister.location.href=(authorityform.TemplateGUID.options[myindex].value);
}

function movepic(img_name,img_src) {
document[img_name].src=img_src;
}

function winreplace(page, uniname) {
OpenWin = window.open(page, uniname, "width=850,height=700,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes");
}

function ReturnFocusAndClose() {
  if (opener.blur) opener.focus();
  window.close(this.form);
}

function ReturnFocus() {
  if (opener.blur) opener.focus();
}

function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
   }
}
return params;
}
params = getParams();

function Start(page) {
OpenWin = this.open(page, "AddForm", "location=yes,menubar=yes,resizable=yes,scrollbars=yes,width=700,height=600,top=20,left=20");
}

function zoomhit(page, uniname) {
OpenWin = window.open(page, uniname, "width=700,height=600,top=20,left=20,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

function nohits ()
    {
        alert("No Hits Found!")
    }

function Go(web_page, window_name) {
  // fix this to detect the protocol by the entered url prefix.  If there is no prefix add http by default
  if (web_page.search('://') != -1)
    OpenWin = this.open(web_page, window_name, "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes");
  else
    OpenWin = this.open('http://' + web_page, window_name, "toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes");
}

function OpenAttachment(file_name, window_name, page) {
    if (file_name == "")
        alert("No file selected!");
    else
    {
        var strTarget
        strTarget = page + file_name
        OpenWin = this.open(strTarget, window_name, "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
    }
}

function LoadPage(form, url)
{
    form.action = url;
    form.submit();
}

function PickTemplate()
{
    var URL = document.xrefs.XRefTemplate.options[document.xrefs.XRefTemplate.selectedIndex].value;
    window.location.href = URL;
}

function PickXRefCode()
{
    var URL = document.xrefs.XRefCode.options[document.xrefs.XRefCode.selectedIndex].value;
    window.location.href = URL;
}

function sendValue(field, hrs, min, sec, am, date_val)
{
   var strHour = hrs.options[hrs.selectedIndex].value;
   var strMin = min.options[min.selectedIndex].value;
   var strSec = sec.options[sec.selectedIndex].value;
   var strAM  = am.options[am.selectedIndex].value;
   var strDate = date_val;
   if (strDate != ' ')
   {
      var strRet = strDate + ' ' + strHour + ':' + strMin + ':' + strSec + ' ' + strAM;
   }
   else
   {
      var strRet = strHour + ':' + strMin + ':' + strSec + ' ' + strAM;
   }
   field.value = strRet;
   window.close();
}

function CheckUserPreferenceForm()
{
    var StringValue = new String()
    StringValue = document.FORM_USER_PREFERENCE.TestFieldGUID.value
    StringValue = StringValue + " "
    if ( StringValue.slice(0, 1) == "Z")
    {
        document.FORM_USER_PREFERENCE.ParentTemplateGUID.value = StringValue.slice(1, 37)
        document.FORM_USER_PREFERENCE.LinkGUID.value = StringValue.slice(37, 73)
        document.FORM_USER_PREFERENCE.TemplateGUID.value = StringValue.slice(73, 109)
        document.FORM_USER_PREFERENCE.FieldGUID.value = StringValue.slice(109, 145)
        alert(document.FORM_USER_PREFERENCE.FieldGUID.value)
    }
    else
    {
        document.FORM_USER_PREFERENCE.FieldGUID.value = document.FORM_USER_PREFERENCE.TestFieldGUID.value
        if ( document.FORM_USER_PREFERENCE.ParentTemplateGUID.value != "" )
        {
           document.FORM_USER_PREFERENCE.TemplateGUID.value = document.FORM_USER_PREFERENCE.ParentTemplateGUID.value
        }
        document.FORM_USER_PREFERENCE.ParentTemplateGUID.value = ""
        document.FORM_USER_PREFERENCE.LinkGUID.value = ""
        alert(document.FORM_USER_PREFERENCE.TemplateGUID.value)
    }
}

// Sets up timeout before returning to login screen.
// This is to avoid the session timeout error page displayed
// by IIS when the session times out and you try to access data
// no longer available without logging back in.

function display(){
     rtime=etime-ctime;
  if (rtime>60)
     m=parseInt(rtime/60);
  else{
     m=0;
  }
     s=parseInt(rtime-m*60);
     if(s<10)
     s="0"+s
     window.setTimeout("checktime()",1000)
}

function settimes(){
     var time= new Date();
     hours= time.getHours();
     mins= time.getMinutes();
     secs= time.getSeconds();
     etime=hours*3600+mins*60+secs;
     etime+=1200;  // time in secs (ie. 1200 = 20 mins * 60 secs/min). Max time is 60 mins (3600secs)
     checktime();
}

function checktime(){
     var time= new Date();
     hours= time.getHours();
     mins= time.getMinutes();
     secs= time.getSeconds();
     ctime=hours*3600+mins*60+secs
   if(ctime>=etime){
     expired();
   } else
     display();
}

function expired(){
     location.href="login.asp";  // Page to return to after timeout
}

function IsRange(SearchForm){
   var TypeSelected = document.SearchForm.SearchType.options[document.SearchForm.SearchType.selectedIndex].value;
   var FieldInput   = document.SearchForm.SearchTerm.value;
   if (TypeSelected == 14)
     {
        //data = "<input class=textinput type=text name=SearchTerm size=13 onkeydown='if(window.event.keyCode == 13) { return false }'><input class=textinput type=text name=SearchToTerm size=13 onkeydown='if(window.event.keyCode == 13) { return false }'>";
          data = "<input class=textinput type=text name=SearchTerm size=13><input class=textinput type=text name=SearchToTerm size=13>";
     } else {
        //data = "<input class=textinput type=text name=SearchTerm size=30 onkeydown='if(window.event.keyCode == 13) { return false }'>"
          data = "<input class=textinput type=text name=SearchTerm size=30>"
     }

     if ( document.all )
     {
        CUST.innerHTML = data;
     }
     else if ( document.getElementById )
     {
        writeLayerW3C(document.getElementById('CUST'),data);
     }
     else if ( document.layers )
     {
        document.layers.CUST.document.write(data);
        document.layers.CUST.document.close();
     }

   document.SearchForm.SearchTerm.value = FieldInput;
}


function writeLayerW3C(node,txtHTML)
   {
       var newRange = document.createRange();
       newRange.selectNodeContents(node);
       newRange.deleteContents();
       var newHTML = newRange.createContextualFragment(txtHTML);
       node.appendChild(newHTML);
   }

function CancelDefault(myEvent)
    {
      myEvent.preventDefault();
    }

function disablekey(whichdiv)
   {
        if ( document.all ) {
              return false;
        }
        else if ( document.getElementById ) {
              document.getElementById(whichdiv).addEventListener("keypress",CancelDefault,true);
        }
        else
        {
              return false;
        }
    }

// force reload because some browser caching can't be prevented
function forceReload() {
    if (document.images)
        location.replace(location.href + '?' + (new Date()).getTime());
    else
        location.href = location.href + '?' + (new Date()).getTime();
}

function getCookie (name) {
    var dcookie = document.cookie;
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
            if (dcookie.substring(cbegin, vbegin) == cname) {
            var vend = dcookie.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
            if (cbegin == 0) break;
        }
    return null;
}

function GetTypes(rowid) {
    //var stype = document.getElementById("FieldGUID_" + rowid).options[document.getElementById("FieldGUID_" + rowid).selectedIndex].getAttributeNode("stype").value;
    var stype = document.getElementById("FieldGUID_" + rowid).options[document.getElementById("FieldGUID_" + rowid).selectedIndex].getAttribute("stype");
    var sfid  = document.getElementById("FieldGUID_" + rowid).options[document.getElementById("FieldGUID_" + rowid).selectedIndex].value;
    var prevval = document.getElementById("SearchTerm_" + rowid).value;

    for (i=0; i < arrFieldList.length; i++) {
       if (arrFieldList[i][1] == sfid) {
          var DefaultTypeList = arrFieldList[i][4].split("|");
          var CodeFieldList = arrFieldList[i][3].split("|");
       }
    }

    var newTD = document.createElement("td");
        newTD.setAttribute("id", "term" + rowid);

    var newSearchTypeTD = document.createElement("td");
        newSearchTypeTD.setAttribute("id", "srchtype" + rowid);

    switch (stype) {
      case "17":
        var fieldguid = document.getElementById("FieldGUID_"+rowid).options[document.getElementById("FieldGUID_"+rowid).selectedIndex].value;
        var newText = document.createElement("select");
            newText.setAttribute("name", "SearchTerm_" + rowid);
            newText.setAttribute("id", "SearchTerm_" + rowid);
            newText.setAttribute("class", "selectinput searchterm");

        var newOption = document.createElement("option");
            newOption.setAttribute("value", "");

        var OptValue = document.createTextNode("");

        newOption.appendChild(OptValue);
        newText.appendChild(newOption);

        for (i=0; i < CodeFieldList.length-1; i++) {
            var CodeTemp = CodeFieldList[i].split(",");

            var newOption = document.createElement("option");
                newOption.setAttribute("value", CodeTemp[0]);

            var OptValue = document.createTextNode(CodeTemp[1]);

            newOption.appendChild(OptValue);
            newText.appendChild(newOption);
        }

        var newSType = document.createElement("select");
            newSType.setAttribute("id", "SearchType_" + rowid);
            newSType.setAttribute("name", "SearchType_" + rowid);
            newSType.setAttribute("class", "selectinput notbool");
            newSType.setAttribute("onchange", "EnhIsRange('" + rowid + "');");

        for (i=0; i < DefaultTypeList.length-1; i++) {
           var strTemp = DefaultTypeList[i].split(",");

           var newOption = document.createElement("option");
               newOption.setAttribute("value", strTemp[0])

           var OptValue = document.createTextNode(strTemp[1]);

           newOption.appendChild(OptValue);
           newSType.appendChild(newOption);
        }
        break;

      default:
        var newText = document.createElement("input");
            newText.setAttribute("name", "SearchTerm_" + rowid);
            newText.setAttribute("id", "SearchTerm_" + rowid);
            newText.setAttribute("type", "text");
            newText.setAttribute("class", "textinput searchterm");
            newText.setAttribute("value", prevval);

        var newSType = document.createElement("select");
            newSType.setAttribute("id", "SearchType_" + rowid);
            newSType.setAttribute("name", "SearchType_" + rowid);
            newSType.setAttribute("class", "selectinput notbool");
            newSType.setAttribute("onchange", "EnhIsRange('" + rowid + "');");

        for (i=0; i < DefaultTypeList.length-1; i++) {
           var strTemp = DefaultTypeList[i].split(",");

           var newOption = document.createElement("option");
               newOption.setAttribute("value", strTemp[0])

           var OptValue = document.createTextNode(strTemp[1]);

           newOption.appendChild(OptValue);
           newSType.appendChild(newOption);
        }
    }
        newTD.appendChild(newText);
        newSearchTypeTD.appendChild(newSType);

        var trow = document.getElementById("row" + rowid);

        var tid = document.getElementById("term" + rowid);
        var replaced = trow.replaceChild(newTD,tid);

        var sid = document.getElementById("srchtype" + rowid);
        var replaced = trow.replaceChild(newSearchTypeTD,sid);

        /* Win/IE uses an incomplete DOM implementation so setAttribute doesn't set
           class or event properties.  The code below is a hack to work around this
           limitation after the child element has been appended to the parent element.
           Mac/IE doesn't have this problem. */
         if ( navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Windows') != -1 ) {
             if (stype == "17") {
               document.getElementById("SearchTerm_" + rowid).className = "selectinput searchterm";
             } else {
               document.getElementById("SearchTerm_" + rowid).className = "textinput searchterm";
             }
             document.getElementById("SearchType_" + rowid).className = "selectinput notbool";
             document.getElementById("SearchType_" + rowid).onchange = function () { EnhIsRange(rowid) };
          }
}

function EnhIsRange(rowid) {
    var stype = document.getElementById("SearchType_"+rowid).options[document.getElementById("SearchType_"+rowid).selectedIndex].value;
    var prevval = document.getElementById("SearchTerm_" + rowid).value;

    if ( stype == "14" ) {
        var newTD = document.createElement("td");
        newTD.setAttribute("id", "term" + rowid);

        var newTerm = document.createElement("input");
        newTerm.setAttribute("name", "SearchTerm_" + rowid);
        newTerm.setAttribute("id", "SearchTerm_" + rowid);
        newTerm.setAttribute("type", "text");
        newTerm.setAttribute("class", "textinput rangeterm");
        newTerm.setAttribute("value", prevval);

        newTD.appendChild(newTerm);

        var newToTerm = document.createElement("input");
        newToTerm.setAttribute("name", "SearchToTerm_" + rowid);
        newToTerm.setAttribute("id", "SearchToTerm_" + rowid);
        newToTerm.setAttribute("type", "text");
        newToTerm.setAttribute("class", "textinput rangeterm");

        newTD.appendChild(newToTerm);

        var trow = document.getElementById("row" + rowid);

        var tid = document.getElementById("term" + rowid);
        var replaced = trow.replaceChild(newTD,tid);

        /* IE uses an incomplete DOM implementation so setAttribute doesn't set
           class or event properties.  The code below is a hack to work around this
           limitation after the child element has been appended to the parent element.
           Mac/IE doesn't have this problem. */
        if ( navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Windows') != -1 ) {
           document.getElementById("SearchTerm_" + rowid).className = "textinput rangeterm";
           document.getElementById("SearchToTerm_" + rowid).className = "textinput rangeterm";
        }

    } else {
        var newTD = document.createElement("td");
        newTD.setAttribute("id", "term" + rowid);

        var newTerm = document.createElement("input");
        newTerm.setAttribute("name", "SearchTerm_" + rowid);
        newTerm.setAttribute("id", "SearchTerm_" + rowid);
        newTerm.setAttribute("type", "text");
        newTerm.setAttribute("class", "textinput searchterm");
        newTerm.setAttribute("value", prevval);

        newTD.appendChild(newTerm);

        var trow = document.getElementById("row" + rowid);

        var tid = document.getElementById("term" + rowid);
        var replaced = trow.replaceChild(newTD,tid);

        /* IE uses an incomplete DOM implementation so setAttribute doesn't set
           class or event properties.  The code below is a hack to work around this
           limitation after the child element has been appended to the parent element.
           Mac/IE doesn't have this problem. */
        if ( navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Windows') != -1 ) {
           document.getElementById("SearchTerm_" + rowid).className = "textinput searchterm";
        }
    }
}

