// List,Grid Functions

document.onselectstart=listSelectStart;


function listSelectStart() {
  if (event.srcElement.tagName=="TD")
    return false;
}

function listMouseOver() {
  var t=event.srcElement;
  
  while (t.tagName!="TR" && t.tagName!="TABLE")
    t=t.parentElement;
  
  if (t.tagName=="TR") t.className +=" listOver";
}
  
function listMouseOut() {
  var t=event.srcElement;
  
  while (t.tagName!="TR" && t.tagName!="TABLE")
    t=t.parentElement;
    
  if (t.tagName=="TR") removeClassName(t,"listOver");;
}

function listSort(sortid) {
  var f=document.getElementsByTagName("FORM");
  if (f.length==0) return;

  var s=null;
  var i=0;
  while (s==null && i<f.length) {s=document.getElementById(f[i].id+"xxSORT");i++;}
  
  if (s!=null) {
    if (event.ctrlKey) {
      if (s.value.indexOf(sortid)<0) {
        if (s.value.length>1) s.value=s.value+', '+sortid; else s.value=sotyid;
      }
      else {
        if (s.value.indexOf(sortid+' DESC')<0) s.value=s.value.replace(sortid, sortid+' DESC'); else s.value=s.value.replace(sortid+' DESC', sortid);
      }
    }    
    else {
      if (s.value.indexOf(sortid)<0 || s.value.indexOf(sortid+' DESC')>=0) s.value=sortid; else s.value=sortid+" DESC";    
    }
    event.cancelBubble=true;
    s.form.submit();
  }  
}

function listMarkAll(gridid) {
  var f=document.getElementsByTagName("FORM");
  if (f.length==0) return;
  
  var m=null;
  var i=0;
  while (m==null && i<f.length) {m=document.getElementById(f[i].id+"xxMARK");i++;}
  if (m==null) return;
  event.cancelBubble=true;

  var t=document.getElementById(gridid);
  if (t==null) return;
  var r=t.all.tags("TR");
  
  for(i=0; i<r.length; i++)
    if (r[i].id!=null && r[i].children[0].tagName=="TD") { 
      if (r[i].children[0].className=="mark") {
        r[i].children[0].className="markSelected";
        r[i].className +=" rowSelected";
        m.value=m.value+r[i].id;        
      }  
      else {
        r[i].children[0].className="mark";
        m.value=m.value.replace(r[i].id,"")
        removeClassName(r[i],"rowSelected");
      }  
    } 
}

function listPage(pid, a) {
  var p=document.getElementById(pid);
  if (p==null) return;
  var pval=new Number(p.value);
  if (pval==null) return;
  
  if (a=='F') p.value=1;else if (a=='P') p.value=pval-1;else if (a=='N') p.value=pval+1;else if (a=='L') p.value=99999;else p.value=a;
  p.form.submit();
}

function listClick(gridid) { 
  var e=event.srcElement;
  var t=e;
  
  while (t.tagName!="TR" && t.tagName!="BODY")
    t=t.parentElement;
  
  if (t.tagName!="TR") return;

  var f=document.getElementsByTagName("FORM");
  if (f.length==0) return;  
  var m=null;
  var i=0;
  while (m==null && i<f.length) {m=document.getElementById(f[i].id+"xxMARK");i++;}
  
  if (m!=null && (e.className=="mark" || e.className=="markSelected")) {
    if (e.className=="mark") {
      e.className="markSelected";
      t.className +=" rowSelected";
      m.value=m.value+t.id;
    }  
    else {
      e.className="mark";
      m.value=m.value.replace(t.id,"");
      removeClassName(t,"rowSelected");
    }
  }
  else {
    var a="";
    if (t.action!=null) a=t.action;  
    var g=document.getElementById(gridid);
   
    if (g!=null) if (g.action!=null) a=g.action;
    if (a!="" && t.id!=null) {
      if (t.id.length>1) a=a.replace(/#ID#/gi,t.id);
      if (a.indexOf('#ID#')<=0) eval(a);
    }  
  }  
}  

function listAction(fid, msg) {
  var f=document.getElementById(fid);
  if (f==null) return;
  var m=document.getElementById(f.id+"xxMARK")
  if (m==null) return;
  
  if (m==null || m.value=='' || m.value==null) {        
    if (msg==null || msg=='') msg='Please select records first.';
    alert(msg);
    f.val='N'
    return false;
  }
  else {
    f.val=null;
    return true;
  }  
}

function listSelect(fid, tid) {
  var t=document.getElementById(tid);
  if (t==null) return;
  
  var m=document.getElementById(fid+'xxMARK');
  if (m==null) return;  
  
  var i=0;
  for (i=0; i<t.parentElement.parentElement.rows.length; i++) {
    t.parentElement.parentElement.rows(i).cells(0).className="mark";
    removeClassName(t.parentElement.parentElement.rows(i),"rowSelected");  
  }

  t.className +=" rowSelected";
  t.cells(0).className="markSelected";
  m.value=t.id; 
}

function listCopy() {
  var m=getv("MARK");
  if (m==null || m=="") return; 

  var mid=m.split("}");
  var t="";
  
  for (d=0; d<mid.length; d++) {
    var r=document.getElementById(mid[d]+"}");
    if (r!=null)
      for (c=0; c<r.children.length; c++)
        if (r.children[c].innerText!="") t=t+r.children[c].innerText+String.fromCharCode(10)+String.fromCharCode(13);   
  }
  window.clipboardData.setData("Text",t);
  alert("Selected Information placed on Clipboard");
}
 
function postBack(id, cid) {
  var wo=window.opener;
  if (wo==null) return;
  if (!cid) cid=0;
  
  var bi=document.getElementById('SEARCHxxBACKID');  
  if (bi==null) return;
  var wbi=wo.document.getElementsByName(bi.value);
  for (i=0; i<wbi.length; i++) wbi[i].value=id;

  var bt=document.getElementById('SEARCHxxBACKTEXT');
  if (bt==null) return;
  
  var r=document.getElementById(id);
  var wbt=wo.document.getElementsByName(bt.value);
  for (i=0; i<wbt.length; i++) {
    wbt[i].value=r.children[cid].innerText;
    if (wbt[i].onchange!=null) wbt[i].onchange(); 
  }
  window.close();
}

function postBackMark(cid) {
  var wo=window.opener;
  if (wo==null) return;
  if (!cid) cid=0;

  var bi=document.getElementById('SEARCHxxBACKID');  
  if (bi==null) return;
  
  var id=document.getElementById('SEARCHxxMARK').value;  
  var wbi=wo.document.getElementsByName(bi.value);
  for (i=0; i<wbi.length; i++) wbi[i].value=id;

  var bt=document.getElementById('SEARCHxxBACKTEXT');
  if (bt==null) return;
  
  var t='';
  var rid=id.split('}');
  for (var x=0; x<rid.length; x++) {
    var r=document.getElementById(rid[x]+'}');
    if (r) t+=r.children[cid].innerText;
  };

  var wbt=wo.document.getElementsByName(bt.value);
  for (i=0; i<wbt.length; i++) {
    wbt[i].value=t;
    if (wbt[i].onchange!=null) wbt[i].onchange(); 
  }
  window.close();
};

function listGetText(cid) {
  if (!cid) cid=0;

  var t='';
  var id=document.getElementById('SEARCHxxMARK').value;
  var rid=id.split('}');
  
  for (var x=0; x<rid.length; x++) {
    var r=document.getElementById(rid[x]+'}');
    if (r) t+=r.children[cid].innerText;
  };

  return t;  
};

function postBackData() {
  return "ONLOAD=postBackAction('"+document.getElementById('SEARCHxxREPORTID').value+"','"+document.getElementById('SEARCHxxBACKID').value+"','"+document.getElementById('SEARCHxxMARK').value+"|"+listGetText(1)+document.getElementById('SEARCHxxBACKTEXT').value+"')";  
};
