/*                  All content copyright © 2007 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.        */

function checkIt(nam,act) {
  var frm,obj;
  frm = document.forms;
  if (frm) for (var i=0; i<frm.length; i++) {
    obj = document.forms[i];
    if (obj.elements[nam]) {
      autoSel(act,obj.elements[nam],nam,"checked");
      break;
    }
  }
}

function autoSel(act,grp,nam,sel) {
  var hid = (document.forms.sub1Form && document.sub1Form.elements[nam]);
  var chk = tmp = "";
  for (var i=0; i<grp.length; i++) {
    if (act == "rev" || act == "sel") {
      chk = (act=="sel") ? 1 : !grp[i][sel];
      grp[i][sel] = chk;
      if (chk) {
        if (tmp != "") tmp += ",";
        tmp += grp[i].value;
      }
    }       
    else if (act == "des") grp[i][sel] = 0;
  }
  if (hid) hid.value = tmp;
}

function selectIt(nam,act) {
  var frm,obj;
  frm = document.forms;
  if (frm) for (var i=0; i<frm.length; i++) {
    obj = document.forms[i];
    if (obj.elements[nam] && obj.elements[nam].options) {
      autoSel(act,obj.elements[nam].options,nam,"selected");
      break;
    }
  }
}

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

function chkValid(e,valid,rejKey,rejChr){
  var chk,chr,ctl,evt,val
  ctl = "null|0|8|9|13|27";
  evt = (window.event) ? window.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)) {
 
    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;
  }

  return true;
}

function scrollEl(fld) {
  if (fld.scrollHeight) fld.scrollTop = fld.scrollHeight;
  else if (fld.offsetHeight) fld.scrollTop = fld.offsetHeight; 
}

function focusFld(fld,sel) {
  if (window.focus && fld) {
    fld.focus();
    if (sel) fld.select(); 
    QLR_field = 0; 
  }
}

function setCount(fld,len,max) {
  var cnt,lbl,msg,nam,typ,val;
  typ = (fld.type && fld.type.toLowerCase() || fld[0].type && fld[0].type.toLowerCase());
  nam = (typ == "checkbox" && fld && fld[0]) ? fld[0].name : fld.name;
  nam = nam.replace(/\[\]$/,"");
  cnt = (document.getElementById) ? document.getElementById(nam+"_count") : (document.all)?document.all[nam+'_count'] : 0;
  lbl = (document.getElementById) ? document.getElementById(nam+"_limit") : (document.all)?document.all[nam+'_limit'] : 0;
  if (cnt) {
    val = (cnt.innerHTML) ? cnt.innerHTML.replace(/<b>/i,"").replace(/<\/b>/i,"") : 0;
    if (+val == max) cnt.style.width = cnt.offsetWidth;          
    msg = (max-len>0) ? max-len : 0;   
    cnt.innerHTML = "<b>"+msg+"</b>";  
    cnt.style.color = (msg==0) ? "#990000" : "#033E66";
    if (lbl) lbl.style.color = cnt.style.color; 
  }
}

function inputLen(fld,max) {
  var brk,str;
  str = fld.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) {
    QLR_field = fld;
    QLR_limit = 0;  
    str = str.substr(0,max);
    str = (str.match(/(\r|\n)$/)) ? str.substr(0,str.length-1) : str;
    fld.value = str.replace(/\r\n/g,brk);
    scrollEl(fld); 
    setCount(fld,inputLen(fld),max);
    setTimeout("focusFld(QLR_field)",10);
  }
  return str.length;
}

function underMax(el,max,evt) {
  var chk,chr,fld,grp,len,msg,num,opt,sel,txt,typ;
  fld = document.forms[el.form.name].elements[el.name];
  typ = (fld.type && fld.type.toLowerCase() || fld[0].type && fld[0].type.toLowerCase());
  chk = (typ == "checkbox");
  txt = (typ == "textarea");
  opt = (typ.indexOf("select")>-1);
  sel = (opt) ? "selected" : "checked";
  if (txt) len = inputLen(fld);
  else {
    len = 0; 
    grp = (opt) ? fld.options : fld;
    for (var i=0; i<grp.length; i++) if (grp[i][sel]) {
      len++;
      if (opt && len>max || window.QLR_limit || window.QLR_regex) {
        grp[i][sel] = 0;
        num = len--; 
      }
    } 
    if (chk && len>max) {
      el[sel] = 0;
      num = len--; 
    } 
  }
  setCount(fld,len,max);
  if (len>max || num>max) {
    if (txt && len>max) {
      QLR_field = QLR_void = fld;
      QLR_limit = [fld,max];
      chr = (len==max+1) ? "character" : "characters";    
      msg = "Input is "+(len-max)+" "+chr+" over the limit and being trimmed.";
      userMsg(msg,0,5);
      if (txt) setTimeout("inputLen(QLR_void,'"+max+"')",10);
    }
    else if (num>max) msg = (max>1) ? "A maximum of "+max+" selections are permitted." : "Only 1 selection permitted."; 
    if (window.QLR_alert != msg) userMsg(msg,0,5);
    return false; 
  }
  return true;
}


function maxChk(el,max,e,exp,msg) {
  var chr,ctl,evt,key,len,opr,typ,sel;
  key = (window.event && event.keyCode || e && e.which);
  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}/)+"|" : "" : "";
  typ = (el.type && el.type.toLowerCase());
  len = (typ == "textarea" && inputLen(el));
  evt = (e && e.type || window.event && event.type || "blur");
  sel = (typ.indexOf("select") > -1);
  if (typ == "checkbox" && evt == "click" && (window.QLR_limit || window.QLR_regex)) el.checked = !el.checked;
  if (sel) {
    QLR_multi = el;
    setTimeout("underMax(QLR_multi,"+max+")",10);
  }
  if (window.QLR_field || (window.QLR_void && QLR_void != el && !sel)) {
    if (!window.QLR_field) setTimeout("focusFld(QLR_void)",10);
    return false; 
  }
  if (len && (evt == "focus" || evt == "keyup")) setCount(el,len,max);
  else if (evt == "keypress") {
    if (len >= max && ctl.indexOf("|"+key+"|") == -1 || key == 13 && len == max-1) {
      if (len > max) underMax(el,max,evt);
      else {
        setCount(el,len,max);
        msg = "Character limit of "+max+" reached.";
        QLR_field = el; 
        userMsg(msg,0,5);
        setTimeout("focusFld(QLR_field)",10);
      }
      return false;
    }
  }
  else { 
    if (len && window.QLR_limit) inputLen(QLR_limit[0],QLR_limit[1]);
    else if (max && !underMax(el,max,evt)) return false;
    else if (exp && !window.QLR_field && !regex(el,exp,msg)) return false;  
  }  
  QLR_alert = QLR_regex = QLR_void = 0;
  return true;
}

function canSub(fld,e) {
  var key = (window.event) ? window.event.keyCode : (e) ? e.which : "";
  if (key && key == 13) return false;
  return true;
}

function regex(el,str,msg) {
  var exp,mod,rep,reg,sub,txt,val;
  if (window.QLR_void && QLR_void != el) {
    QLR_void.focus();
    return false; 
  }
  txt = (el.type && el.type.toLowerCase() == "textarea");
  val = el.value;
  if (!window.RegExp || !val) {
    QLR_void = 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);
    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("}"))); 
        QLR_field = QLR_void = el; 
        QLR_regex = val.replace(reg,sub);
        setTimeout("QLR_field.value = QLR_regex",1);
        if (msg) {
          userMsg(msg,1,5);
          setTimeout("focusFld(QLR_field)",10);
        }
        else if (window.QLR_void == el) QLR_void = 0;
        if (txt) scrollEl(el);
        return false;
      }
    }
    else if (!val.match(reg)) {
      QLR_field = QLR_void = el; 
      if (msg) {
        userMsg(msg,1,5);
        setTimeout("focusFld(QLR_field,1)",10);
      }
      if (txt) scrollEl(el); 
      return false;  
    }
    if (window.QLR_void == el) QLR_field = QLR_void = 0;
  }
  return true;
}