/*                  All content copyright © 2007-2009 Tatler Software.               */
/*                                                                                   */
/*  Permission granted to use these scripts to support input controls on Customer's  */
/*  web site when implemented in conjunction with QLR Manager report objects.        */

if (document.getElementById) chkHost = new Function("win","try{if(win&&win.location&&win.location.hostname)return win.location.hostname;return false}catch(e){}");
else chkHost = new Function("win","if(win&&win.location&&win.location.hostname)return win.location.hostname;return false");
var QLR = new Object();
var MSG = chkHost(parent) && parent.showMsg || window.location.href && window.location.href.indexOf("file://") != -1 && parent.showMsg;

/*  Set qlrMsg = MSG to use the QLR message frame when used inside the product.      */
/*  Set qlrMsg = 0 to always use Javascript alerts.                                  */

var qlrMsg = MSG;

function autoSel(nam,act) {
  if (QLR.error) return;
  var fld,cnt,frm,hid,tmp;
  cnt = 0;
  tmp = val = "";
  frm = document.forms;
  hid = document.forms.sub1Form && document.sub1Form.elements[nam];
  if (frm) for (var i=0; i<frm.length; i++) {
    fld = getField(0,i,nam);
    if (fld) break;
  }
  for (var i=0; i<fld.grp.length; i++) {
    if (act == "rev" || act == "sel") {
      tmp = (act=="sel") ? 1 : !fld.grp[i][fld.sel];
      fld.grp[i][fld.sel] = tmp;
      if (tmp) {
        if (val != "") val += ",";
        val += fld.grp[i].value;
      }
    }
    else if (act == "des") fld.grp[i][fld.sel] = 0;
    if (fld.grp[i][fld.sel]) cnt++;
  }
  if (act == "cnt") return cnt;
  if (hid) hid.value = val;
  setMaxim(fld.chk,cnt,fld);
  if (qlrMsg) chkLimit(fld,cnt,nam);
}

function setMaxim(chk,cnt,fld) {
  var evt,max;
  fld.cnt = cnt;
  evt = (chk) ? chk.onclick : fld.onmouseup;
  max = evt && evt.toString().match(/\d+/);
  if (max) {
    setCount(fld,cnt,max);
    fld.aut = (cnt>max) ? fld : 0;
  }
}

function getField(el,frm,nam) {
  var fld,obj;
  frm = frm || el && el.form.name;
  nam = nam || el && el.name;
  fld = document.forms[frm].elements[nam];
  obj = new Object(fld);
  obj.typ = fld && (fld.type && fld.type.toLowerCase() || fld[0] && fld[0].type && fld[0].type.toLowerCase());
  obj.chk = obj.typ == "checkbox" && fld[0];
  obj.opt = fld && fld.options;
  obj.grp = obj.opt || obj.chk && fld;
  obj.nam = nam || fld.name || fld[0].name;
  obj.put = obj.typ == "text";
  obj.sel = obj.opt && "selected" || obj.chk && "checked";
  obj.txt = obj.typ == "textarea";
  return obj;
}

function validMsg(chr) {
  if (chr) {
    var msg =  chr+" is not valid in this field.";
    if (qlrMsg) qlrMsg(msg,1,0,5);
    else alert(msg);
  }
  else if (qlrMsg) qlrMsg();
}

function chkValid(e,valid,rejKey,rejChr) {
  var chk,chr,ctl,evt,val
  ctl = "null|0|8|9|13|27";
  evt = (window.event) ? event.keyCode: (e) ? e.which: "";
  chr = String.fromCharCode(evt);
  chk = chr.toUpperCase();
  val = (valid == "attr") ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_ " : valid;
  if((evt && ctl.indexOf(evt)==-1) && (chk && val && val.indexOf(chk)==-1)) {
    validMsg(chr);
    return false;
  }
  if((evt && rejKey && rejKey.toString().indexOf(evt)!=-1) || (chk && rejChr && rejChr.indexOf(chk)!=-1)) {
    if (rejKey && chr.match(/\S/) || rejChr) validMsg(chr);
    return false;
  }
  validMsg();
  return true;
}

function setCount(fld,len,max,res) {
  var clr,cnt,lbl,msg,num;
  fld.nam = fld.nam.replace(/\[\]$/,"");
  cnt = document.getElementById && document.getElementById(fld.nam+"_count") || document.all && document.all[fld.nam+'_count'];
  lbl = document.getElementById && document.getElementById(fld.nam+"_limit") || document.all && document.all[fld.nam+'_limit'];
  if (cnt) {
    if (res && len!=max && fld.cnt<=max) userMsg();
    clr = (max-len<=0) ? "#990000" : "#033E66";
    num = (max-len>=0) ? max-len : len-max;
    cnt.innerHTML = "<nobr><b>"+num+"<\/b><\/nobr>";
    cnt.style.color = clr;
    if (lbl) {
      msg = (fld.txt) ? "Character" : "Selection";
      lbl.innerHTML = (len==0) ? "<nobr>"+msg+" limit:&nbsp;<\/nobr>" : (max-len<0) ? "<nobr>Limit exceeded by:&nbsp;<\/nobr>" : "<nobr>"+msg+"s remaining:&nbsp;<\/nobr>";
      lbl.style.color = clr;
    }
  }
}

function inputLen(el,max) {
  var brk,str;
  str = el.value;
  if (!str) return "0";
  brk = (str.match(/\r\n/)) ? "\r\n" : (str.match(/\r/)) ? "\r" : (str.match(/\n/)) ? "\n" : 0;
  str = (brk != "\r\n") ? str.replace (/\n|\r/g,"\r\n") : str;
  if (max) {
    setVoid(el);
    str = str.substr(0,max);
    str = (str.match(/(\r|\n)$/)) ? str.substr(0,str.length-1) : str;
    el.value = str.replace(/\r\n/g,brk);
    setCount(el,inputLen(el),max);
    setTimeout("focusFld(QLR.error)",10);
  }
  return str.length;
}

function chkLimit(el,len,nam) {
  var arr,cnt,err,exp,fld,foc,grp,frm,lbl,max,min,msg,reg;
  arr = window.minmaxChk;
  if (!arr || QLR.error) return true;
  for (var i=0; i<arr.length; i+=2) {
    frm = document.forms;
    if (frm) for (var j=0; j<frm.length; j++) {
      fld = getField(0,j,arr[i]);
      lbl = (arr[i+1][0]) ? " for: "+arr[i+1][0] : ".";
      min = arr[i+1][1];
      max = arr[i+1][2];
      exp = arr[i+1][3];
      msg = arr[i+1][4];
      msg = msg && msg.replace(/&quot;/g,'"');
      grp = fld || fld && fld[0];
      if (!el && (fld.put || fld.txt)) {
        cnt = inputLen(fld);
        if (min && cnt<min || max && cnt>max) {
          err = (min && cnt<min) ? (min>1) ? "A minimum of "+min+" characters are required"+lbl : "A minimum of 1 character is required"+lbl : (max && cnt>max) ? (max>1) ? "A maximum of "+max+" characters are permitted"+lbl : "Only 1 character permitted"+lbl : 0;
          break;
        }
        else if (exp && !regex(fld,exp,msg,1)) {
          err = QLR.regChk;
          reg = fld;
          break;
        }
      }
      else if (fld.grp && (!el || arr[i] == nam)) {
        cnt = autoSel(fld.nam,"cnt");
        setCount(fld,cnt,max);
        if (min && cnt<min || max && cnt>max) {
          err = (min && cnt<min) ? (min>1) ? "A minimum of "+min+" selections are required"+lbl : "A minimum of 1 selection is required"+lbl : (max && cnt>max) ? (max>1) ? "A maximum of "+max+" selections are permitted"+lbl : "Only 1 selection permitted"+lbl : 0;
          break;
        }
      }
    }
    if (err || reg) {
      QLR.multi = 0;
      if (!el)  {
        fld = fld.chk || fld;
        foc = (fld.put || fld.txt) ? 10 : 0;
        scrollEl(fld,foc);
        QLR.subMsg = fld;
        if (err) userMsg(err);
      }
      else if (err) userMsg(err,0,0,5);
      break;
    }
  }
  if (err || reg) return false;
  userMsg();
  return true;
}

function scrollEl(fld,foc,blr) {
  var y,off;
  setVoid(fld);
  if (window.scrollTo && fld.offsetParent) {
    y = 0;
    off = (fld.put) ? 2 : (fld.txt) ? 18 : 36;
    if (fld && fld.offsetParent) while(fld) {
      y += fld.offsetTop;
      fld = fld.offsetParent;
    }
    scrollTo(0,y-off);
  }
  if (foc) setTimeout("focusFld(QLR.field)",foc);
  if (blr) setTimeout("QLR.multi=0;QLR.field.blur()",blr);
}

function focusFld(el) {
  if (window.tryFocus) tryFocus(el);
  else if (el && el.focus) el.focus();
}

function hasVoid(el) {
  if (QLR.error && QLR.error != el) {
    if (!QLR.multi) scrollEl(QLR.error,10);
    return true;
  }
  if (qlrMsg) QLR.field = el;
  return false;
}

function setVoid(el) {
  QLR.error = QLR.field = el;
}

function userMsg(msg,scr,foc,tmr) {
  if (QLR.multi || QLR.abort) {
    QLR.abort = 0;
    return;
  }
  if (msg) {
    if (qlrMsg && !QLR.cancel) {
      QLR.alert = msg;
      QLR.delay = setTimeout("qlrMsg(QLR.alert,1,0,"+tmr+")",1);
    }
    else {
      if (msg == QLR.alert && !qlrMsg || QLR.focus) return;
      QLR.alert = msg;
      QLR.delay = setTimeout("alert(QLR.alert)",1);
      setTimeout("QLR.alert=''",100);
    }
  }
  else if (qlrMsg) qlrMsg();
  if (foc && QLR.field) setTimeout("focusFld(QLR.field)",foc);
  if (scr) setTimeout("scrollEl(QLR.field)",foc);
}

function getList(fld,max,cnt) {
  var len,sel;
  len = 0;
  if (!cnt) fld.lst = [];
  for (var i=0; i<fld.opt.length; i++) {
    sel = fld.opt[i][fld.sel];
    if (sel) len++;
    if (!cnt) fld.lst[i] = sel;
  }
  fld.cnt = len;
  fld.max = max;
  setCount(fld,len,max);
}

function hasError() {
  if (QLR.error && QLR.multi) {
    focusFld(window);
    scrollEl(QLR.error,10,20);
    return true;
  }
  return false;
}

function overMax(el,max,evt) {
  var chr,cnt,fld,len,msg,tmp;
  fld = getField(el);
  if (fld.txt) len = inputLen(el);
  else {
    len = 0;
    tmp = [];
    for (var i=0; i<fld.grp.length; i++) {
      if (fld.grp[i][fld.sel]) {
        len++;
        if (len>max || QLR.error) {
          if (fld.opt) {
            if (fld.lst && (fld.cnt<=max || QLR.error)) {
              cnt = 0;
              for (var i=0; i<fld.grp.length; i++) {
                fld.grp[i][fld.sel] = fld.lst[i];
                if (fld.lst[i]) cnt++;
              }
              break;
            }
            else if (!QLR.ctrl && !fld.aut) fld.grp[i][fld.sel] = 0;
          }
          else el[fld.sel] = 0;
        }
      }
      if (fld.opt) tmp[i] = fld.grp[i][fld.sel];
    }
  }
  if (fld.lst && tmp) for (i in tmp) fld.lst[i] = tmp[i];
  cnt = (cnt) ? cnt : (len==max+1 && fld.cnt<=max ) ? max : len;
  setCount(fld,cnt,max);
  if (len>max) {
    if (fld.txt) {
      setVoid(fld);
      chr = (len==max+1) ? "character" : "characters";
      msg = "Input is "+(len-max)+" "+chr+" over the limit.";
      userMsg(msg,el,10,3);
      fld.cnt = len;
    }
    else {
      if (hasError()) return false;
      if (evt != "mouseover") QLR.multi = 0;
      msg = (max>1) ? "A maximum of "+max+" selections are permitted." : "Only 1 selection permitted.";
      if (!fld.cnt || fld.cnt<=max) userMsg(msg,0,0,3);
      if (fld.chk) el.blur();
      fld.cnt = len;
    }
    return true;
  }
  if (fld == QLR.multi) {
    if (len<=max) fld.aut = 0;
    hasError();
  }
  fld.cnt = len;
  return false;
}

function canSub(el,e) {
  var key = window.event && event.keyCode || e && e.which;
  if (key && key == 13) return false;
  return true;
}

function clrMsg(fld) {
  if (QLR.subMsg && fld != QLR.subMsg) { 
    userMsg();
    QLR.subMsg = 0;
  }
}

function setFoc(el,max,e,exp,msg) {
  var fld,len;
  fld = getField(el);
  fld.max = max;
  len = fld.txt && inputLen(fld);
  QLR.input = (document.layers && QLR.alert) ? 0 : fld;
  if (len) setCount(fld,len,max);
  clrMsg(fld);  
}

function maxChk(el,max,e,exp,msg) {
  var chr,ctl,evt,fld,key,len,opr;
  evt = e && e.type || window.event && event.type || "blur";
  fld = getField(el);
  key = window.event && event.keyCode || e && e.which;
  len = fld.txt && inputLen(el);
  if (!fld.opt || fld.opt && evt != "mouseover") clrMsg();
  if (fld.chk && evt == "click" && QLR.error) el.checked = !el.checked;
  else if (fld.opt) {
    if (evt == "mouseup" && QLR.multi == fld) setTimeout("overMax(QLR.multi,"+max+")",10);
    else if (evt == "blur") {
      if (!QLR.multi) overMax(fld,max);
      QLR.multi = 0;
    }
    else if (evt == "mouseover" && fld != QLR.multi) {
      getList(fld,max,1);
      return true;
    }
    else if (evt == "mousedown") {
      QLR.multi = fld;
      getList(fld,max);
      QLR.ctrl = QLR.ctrl || e.ctrlKey;
      return false;
    }
  }
  if (hasVoid(el)) return false;
  if (evt=="keydown") {
    QLR.ctrl = e.ctrlKey || key == 17;
    return true;
  }
  else if (evt=="keyup") {
    QLR.ctrl = 0;
    if (len) {
      if (key==9) return false;
      QLR.field = el;
      fld.cnt = len;
      setCount(fld,len,max,1);
      return true;
    }
  }
  else if (evt == "keypress") {
    chr = (e && e.charCode && String.fromCharCode(e.charCode));
    ctl = "|null|0|8|9|16|17|18|19|20|27|45|46|";
    opr = "|33|34|35|36|37|38|39|40|";
    ctl+= (window.opera && key) ? (opr.indexOf("|"+key+"|") != -1) ? key+"|" : (key.toString() && key.toString().match(/57\d{3}/)) ? key.toString().match(/57\d{3}/)+"|" : "" : "";
    if (len >= max && ctl.indexOf("|"+key+"|") == -1 || key == 13 && len == max-1) {
      if (len && fld.cnt>max) overMax(el,max,evt);
      else if (len == max) {
        msg = "Character limit of "+max+" reached.";
        fld.lmt = 1;
        QLR.field = el;
        fld.cnt == len;
        userMsg(msg,0,10,3);
      }
      return false;
    }
  }
  else if (fld != QLR.multi) {
    if (max && !fld.opt && overMax(el,max,evt)) return false;
    if (qlrMsg) {
      if (fld.cnt>max) return false;
      else if (exp && !regex(el,exp,msg)) return false;
    }
    else if (exp && !fld.lmt && fld.cnt<=max && !regex(el,exp,msg)) return false;
    if (fld.put || fld.txt) QLR.error = 0;
  }
  fld.lmt = 0;
  return true;
}

function regex(el,str,msg,chk) {
  var exp,mod,rep,reg,scr,sub,val;
  if (hasVoid(el)) return false;
  val = el.value;
  if (!window.RegExp || !val) {
    QLR.error = 0;
    return true;
  }
  str = str.replace(/^\s+|\s+$/g,"");
  exp = str.replace(/^\/(.*)(\/$|\/\s*[gimsx,].*$)/,"$1");
  if (exp) {
    mod = /^.*\/\s*([gimsx]*)\s*,?.*$/;
    mod = str.match(mod) && str.match(mod)[1] || "";
    rep = /\/\s*[gimsx]*\s*,\s*(['"])?(.*)\1$/;
    sub = str.match(rep) && str.match(rep)[2] || "";
    reg = new RegExp(exp,mod);
    scr = (el.type == "textarea") ? el : 0;
    if (str.match(rep)) {
      if (val.match(reg)) {
        if (sub && sub.match(/function(.*)/)) sub = new Function("$1","$2","$3","$4","$5","$6","$7","$8","$9",sub.substring(sub.indexOf("{")+1,sub.lastIndexOf("}")));
        setVoid(el);
        QLR.regex = el;
        QLR.value = val.replace(reg,sub);
        setTimeout("QLR.field.value = QLR.value",1);
        if (msg) {
          if (chk) QLR.regChk = msg;
          else userMsg(msg,scr,10,3);
        }
        else if (QLR.error == el) QLR.error = 0;
        return false;
      }
    }
    else if (!val.match(reg)) {
      QLR.regex = el;
      setVoid(el);
      if (msg) {
        if (chk) QLR.regChk = msg;
        else userMsg(msg,scr,10,3);
      }
      return false;
    }
    QLR.error = QLR.regex = 0;
  }
  if (!QLR.error) userMsg();
  QLR.regChk = 0;
  return true;
}

function getOption(opt) {
  var arr,txt,val;
  arr = (opt.constructor.toString().toLowerCase().indexOf("array") != -1);
  txt = (arr) ? opt[0] : opt;
  val = (arr) ? opt[1] : opt;
  return [txt,val];
}

function dynlist(own,tgt,arr) {
  var obj,opt,pop,val;
  obj = window[arr];
  val = own.options[own.options.selectedIndex].value;
  if (obj) {
    for (var i=0; i<obj.length; i+=2) if (obj[i] == val && obj[i+1].length) {
      pop = 1;
      tgt.length = 0;
      for (var j=0; j<obj[i+1].length; j++) {
        opt = getOption(obj[i+1][j]);
        if (opt) tgt[tgt.length] = new Option(opt[0],opt[1]);
      }
    }
  }
  if (!pop) {   
    tgt.length = 0;
    tgt[0] = new Option("     ","");
  }
}

if (document.getElementById) tryFocus = new Function("el","try{el.focus()}catch(e){}");

if (document.layers) {
  if (window.captureEvents) window.captureEvents(Event.BLUR|Event.MOUSEDOWN|Event.MOUSEUP);
  window.onmousedown = function(e) { if(e.target.type||e.target.href)return true;return false; }
  window.onmouseup = function(e) { if(e.target.type||e.target.href)return true;return false; }
  window.onblur = function(e) { if(e.target==window)return false;if(e.target==QLR.error&&!QLR.input)return false;routeEvent(e);return true; }
}