// Copyright (C) 2004 Quicklaw Inc.
// DOCLIST.JS  MTM  April 27, 2004
// modified get_message -- June 25, 2004 - mtm
// Oct. 14/2005 - Position persistence - mtm

 
max_check =100;
no_more=false;

function setfr () {
if (window.opener) tw=window.opener.parent.top; else tw=parent.top;
if ("undefined" == typeof tw.iscf)
  FR_INT = false;
else
  FR_INT = parent.top.iscf;
} // setfr

function get_checked() {
	var cv = document.cookie;
	var dlist = '';
	if (cv != "") {
		var b = cv.indexOf("DLIST=");
    if (b != -1) {
	    dlist = cv.slice(b+6);
	    var e = dlist.indexOf(';');
	    if (e != -1)
	      dlist = dlist.slice(0,e);
    }    
  } // if cv not null
  if (dlist) {
    var b = dlist.indexOf('@');
		if (b == -1)
		  dlist = '';
		else
		  dlist = dlist.slice(b);  
  }	
  return dlist;
} // get_checked


function set_checked(dlist) {
    var cv = "DLIST=" + dlist + ';path=/';	 
    document.cookie = cv;
} // set_checked

function check_on(val) {
	var st = get_checked();
  var newd = "@" + val;
  if (st.indexOf(newd) == -1) {
    st += newd;
    set_checked(st);
  }
}	// check_on

function check_off(val) {
	var st = get_checked();
	st=st.replace('@'+val,'');
	set_checked(st);
} // check_off

function populate_doc_list() {
  var f = document.forms['DL'];
  if (f) {    
    var st = get_checked();
    for (var i=0; i<f.length;i++) {
	    var e = f.elements[i];
	    if (e.type == "checkbox") {
		    var dn = "@" + e.name;
		    if (st.indexOf(dn) != -1)
		      e.checked = true;
		    else
		      e.checked = false;  
	    }
    }
  }  
} // populate_doc_list

function main_action(t) {
  var si = t.selectedIndex;
  var val = t.options[si].value;
  if (val.indexOf("http:") != -1) {
    var b=val.indexOf("http:");
    val = val.slice(b);
    t.selectedIndex=0;
    top.record_pos();
	  top.window.location.href=val;
  }
  else if (val == "")
    t.selectedIndex = 0;
  else
    eval(val);  
} // main_action

function get_dl_element(frame,dn) {
	var nm = 'parent.top.' + frame;
	var fr = eval(nm);
	if (fr) {
		var d = fr.document;
		if (d) {
			if (d.getElementById) {
				var e=d.getElementById(dn);
				return e;
			} // if getElementById
		  var f = d.forms['DL'];
      if (f) { 
        for (var i=0; i<f.length;i++) {
	        var e = f.elements[i];
	        if (e.type == "checkbox" && e.name==dn) {
		        return e;
	        } // if
         } // for
      } // if f
    } // id f
  } // if fr
  return null;
} // get_dl_element    

function cleartagged() {
	var st = get_checked();
	var ns = st.split("@");
	if (!ns) return;
	for(var i=1; i<ns.length; i++) {
	  var dn=ns[i];
	  var e = get_dl_element('LEFT',dn);
	  if (e && e.checked) e.click();
	  else {
		  var e = get_dl_element('RIGHT',dn);
    	  if (e && e.checked) e.click();
	  }  
  } // for  
  var e = get_dl_element('LEFT','ALLDOCS');
  if (e && e.checked) e.click();
  var e = get_dl_element('RIGHT','ALLDOCS');
  if (e && e.checked) e.click();
  set_checked('');
} // cleartagged

function counttagged() {
	var st = get_checked();
	var ns = st.split("@");
	if (!ns) return 0;
  if (ns.length == 0) return 0;
	return(ns.length-1);
} // counttagged

function root_url() {
  var s = parent.top.location.href;
  var ls = s.toLowerCase();
  var i = ls.indexOf("qlcid=");
  if (i != -1) {
    for (var j=i+6; j < ls.length; j++) {
      if (ls.charAt(j) == '&') {
	      var k = j;
        break;
      }  
    } // for loop
    var h = s.substr(0,k);  // must use original
    return h;
  }
  alert("can't find qlcid");
  return '';
} // root_url
    
function new_window_msg(mno) {
	
	 var msgurl = root_url();
	 msgurl += '&qlvrb=dget&db=msgh&n=';
	 msgurl += mno;
	 
   var s = window.screen;
   if (s.availHeight) var h=s.availHeight; else var h=s.height;
   if (s.availWidth)  var w=s.availWidth;  else var w=s.width;

   var lf = window.Math.round(w*.04);
   var tp = window.Math.round(h*.04);
   w=window.Math.round(w*.80);
   h=window.Math.round(h*.70);

   var features  = "height=" + h.toString();
   features += ",width="+w.toString();
   features += ",menubar,resizable,scrollbars,toolbar";
   if (document.getElementById)
     features += ",top="+tp.toString()+",left="+lf.toString();
   else if (document.layers)
     features += ",screenY="+tp.toString()+",screenX="+lf.toString();

   window.open(msgurl,"_blank",features); // force a reload
} // new_window_msg

function get_message(mno) {
//  if (document.all) {
//  	var f = document.subform;
//    if (f) {
//	    var v = f.N;
//	    if (v) {
//		    v.value = mno;
//		    f.submit();
//	    } // if v
//   } // if f
//  } // if MSIE
//  else
    new_window_msg(mno);  
} // get_message

function dotagaction(act) {
	setfr();
  var nt = counttagged();
  if (nt == 0) {
	  if (FR_INT)
	    alert("Aucun document marqu\xE9");
	  else
	    alert("There are no documents tagged");
	  return;
  }
  if (act == "list") {
    var h=root_url();
    h += '&qlvrb=qlcmd&ignorecid=y&c=showdlst';
    var dl=get_checked();
    dl=dl.replace(/@/g,'&d=');
    h += dl;
    top.record_pos();
    top.window.location.href = h;
    return;
  }
  if (act == "clear") {
    var p = "Clear list of tagged documents, are you sure?";
    if (FR_INT)
      p = "Voulez-vous vider la liste des documents marqu\xE9s?";
    var cont = confirm(p);
    if (cont) cleartagged();
    return;  
  }
  if (act == "print") {
    get_message(2180);
    return;
  }
  if (act == "save") {
    get_message(2181);
    return;
  }
  if (act == "email") {
    get_message(2182);
    return;
  }
} // dotagaction

function tag_action(t) {
  var si = t.selectedIndex;
  var val = t.options[si].value;
  if (val.indexOf("http:") == 0) {
    t.selectedIndex=0;
    top.record_pos();
	  top.window.location.href=val;
  }
  else if (val == "")
    t.selectedIndex = 0;
  else if (val.indexOf("tag:") == 0) { 
    dotagaction(val.slice(4));  
    t.selectedIndex=0;
  }
  else
    eval(val);  
} // tag_action

function rclick(t) {
	setfr();
  if (max_check == counttagged() && t.checked) {
	  t.checked = false;
	  var msg  = "Sorry, you have tagged the maximum of " + max_check;
	      msg += " documents for delivery.";
	  if (FR_INT) {
	    msg  = "D\xE9sol\xE9, vous avez atteint la limite de "
	    msg += max_check;
	    msg += " documents pour transmission.";
    }
	  if (!no_more) alert(msg);    
	  no_more = true;
	  return;
  }
    
  var dn = t.name;
  if (t.checked)
    check_on(dn);
  else
    check_off(dn);  

  var rt = parent.top.RIGHT;
  if (window.self == rt) {
	  var e = get_dl_element('LEFT',dn);
  }
	else {
	  var e = get_dl_element('RIGHT',dn)
  }
	if (e)
	 e.checked = t.checked;    
} // rclick

function clickall(t) {
  var f = document.forms['DL'];
  no_more = false;
  if (f) { 
    for (var i=0; i<f.length;i++) {
	    if (no_more) {
		    t.checked = false;
		    i = f.length;
	      break;
      }
	    var e = f.elements[i];
	    if (e.type == "checkbox") {
		    if (e.checked != t.checked) e.click();
	    } // if
    } // for
  } // if f
} // clickall  

