
// Copyright (C) 1999-2006 IT Dimensions Inc. All rights reserved.
// Redistribution and use in source and binary forms, with or without modification
// are permitted provided that the following conditions are met:
// www.jreps.com/legal/license.txt
// tools v 1.30


var activeo = null;

var mbox,pos ;
var oldind = 0;
var oldcolor = '';
//alert ('1.2');
var isnn = (navigator.appName == "Netscape" && navigator.appVersion.charAt(0) >= 4) ? true : false;
var isns = document.getElementById&&!document.all;

var ismac = navigator.userAgent.indexOf("Mac")>-1;
var popw,popw2  // need this for the focus

// control functions
function setval (nme,val) {
	if (isnn)
		document.forms[0].elements[nme].value=val;
	else {
		var t=document.forms[0];
		t[nme].value=val;
	}
}

function setfocus (nme) {
	if (isnn)
		document.forms[0].elements[nme].focus();
	else {
		var t=document.forms[0];
		t[nme].focus();
	}
}



function getval (ctl,nme) {
	var ret;
	if (!nme) {
		nme=ctl;
		if (isnn)
			ret = document.forms[0].elements[nme].value;
		else {
			var t=document.forms[0];
			ret = t[nme].value;
		}
		return ret;
	}

	var parentForm = ctl.form;
	if (isnn)
		ret = parentForm.elements[nme].value;
	else {
		var t=parentForm;
		ret = t[nme].value;
	}
	return ret;
}


function getctl (nme,ctl) {
	if (!ctl) {
		if (isnn) return document.forms[0].elements[nme];
		else { var t=document.forms[0]; return t[nme]; }
	}
	else {
		var parentForm = ctl.form;
		if (isnn) return parentForm.elements[nme];
		else {var t=parentForm;return t[nme];}
	}
}



function getindex(nme) {
	var x=nme;
	x= x.substring(x.lastIndexOf("_")+1);
	return x;
}



function jrsuba (ctl,act ) {
	//alert('d1='+ctl +":" + act);
	var notdef=false;
	if (!act) notdef = true;
	if (activeo) {
		var nme= activeo.name;
		act +="&dd=" + nme;
	}

	var w=getw();h=geth();
	var parentForm ;
	if (typeof(ctl)!="object") {
		parentForm = document.forms[ctl];
	}
	else
		parentForm = ctl.form;// this doesn't work on <a stuff
	if (parentForm) {
		parentForm.action+=act + '&h=' + h + '&w=' +w;
		parentForm.submit();
	}
	else {// navigation might not be an element thus cannot refer to a form
		if (notdef) act=ctl; //if only one par is passed the act value actually comes via ctl this step insures that the first argument is passed to act
		document.forms[0].action+=act + '&h=' + h + '&w=' +w;
		document.forms[0].submit();
	}
}

function getw () { 	return window.innerWidth  || document.body.clientWidth; }
function geth () { 	return window.innerHeight || document.body.clientHeight; }


function jrlink ( link) {
	if (link.charAt(0)=='?') {
		link = getpath()+link.substring(1);
	}
	else if (link.charAt(0)=='&') {
		strURL=document.location.href;
		pathp=strURL.indexOf("&");
		link==strURL.substring(0,pathp) + link;

	}
	document.location.href=link;
}

function jrsub     (act )		 { 	setval ('ac',act); 	jsubmitcheck (); }
function jrsubcf   (act,cf ) { 	setval ('ac',act); 	setval ('cf',cf); 	jsubmitcheck (); }
function jrsubcfnb (act,cf ) { 	setval ('ac',act); 	setval ('cf',cf); 	jsubmit (); }
function jrcf (cf ) { 	setval ('ac',''); 	setval ('cf',cf); 	jsubmitcheck (); }
function jsubmitcheck () { 	scanCheckBox(); 	jsubmit(); }
function jsubmit () { 	document.forms[0].submit(); }



function alter (e,tt ) {
	var kk = getkey (e);
	var tpe = 0;
	if (tt=='upper')
		tpe = ((kk > 96 && kk < 123)?kk-=32:kk) ;
	else if (tt=='lower')
		tpe = ((kk > 64 && kk < 91)?kk=kk+32:kk);
	else
		tpe = kk;

	if (isnn) 	return tpe;
	else		e.keyCode=tpe;
}

function getkey (e) {
	if (isnn) return e.which;
	else return e.keyCode;
}

function highlight (objx) {
	//alert (':');
	nme = objx.name;
	//alert ('nme=' + nme);
	ind = nme.substring(nme.lastIndexOf('_')+1,nme.length+1);
	
	obo = 	getmaintable(oldind);
	if (obo) {
		obo.bgColor=oldcolor;
	}	
	obo = 	getmaintable(nme);
	if (obo) {
		oldcolor = obo.bgColor;
		alert ('oldcol=' + oldcolor);
		obo.bgColor="343311";
	}
	activeo = objx;
	oldind = nme;
}

// popup windows suport
function jrpop (link ) {
	jrpops (link,'450','600');
}

// make sure that we come forward
function jrpops (link,width,height,opts ) {
	if (!opts) opts='';
	else opts = ','+opts;
	if (link.charAt(0)=='?') {
		link = getpath()+link.substring(1);
	}
	//alert('d='+opts);
	if(!popw || popw.closed)
    // g_win = window.open(url, 'glossaryWin', '...');
		popw=window.open(link, 'popupWin',"width=" +width+ ",height="+height+",toolbar=0,location=0,directories=0,status=no,menubar=0,scrollbars=0,resizable=yes "+ opts);
   else
     popw.location.replace(link);

   popw.focus();
}


function jrpops2 (link,width,height,opts ) {
	if (!opts) opts='';
	else opts = ','+opts;
	if (link.charAt(0)=='?') {
		link = getpath()+link.substring(1);
	}
	if(!popw2 || popw2.closed )
		popw2=window.open(link, 'popupWin2',"width=" +width+ ",height="+height+",toolbar=0,location=0,directories=0,status=no,menubar=0,scrollbars=0,resizable=yes "+ opts);
   else
     popw2.location.replace(link);

   popw2.focus();
}


function pop (name,link,width,height,opts ) {
	if (link.charAt(0)=='?') {
		link = getpath()+link.substring(1);
	}
	//alert('d='+opts);
	if(!popw || popw.closed)
		popw=window.open(link, name,"width=" +width+ ",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0 " + opts);
	else
    popw.location.replace( link);
  popw.focus();
}


function jrpopx (tmpl,pars ) {
	temp=tmpl.split(":");
	var x=window.open(temp[0]+'&'+pars, 'popupWinx',"width=" +temp[1]+ ",height="+temp[2]+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1");
}


mi = function(f, m){ 
	function ma(e){	
		var pa = {"1": /[A-Z]/i, "2": /[0-9]/,"4": /[À-ÿ]/i,"8": /./ }, rs= {"a": 3, "A": 7,"9": 2,"C":5, "c": 1, "*": 8};
		function ac(c, rule){	for(var i = 1, r = rs[rule] || 0; i <= r; i<<=1)	if(r & i && pa[i].test(c))  break;	return i <= r || c == rule;	}
		var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;	(!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?	r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : ac(c, r[1]) || r[0]: (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ? r.index : l)).length) < m.length && ac(c, m.charAt(l)) ) || e.preventDefault();
	}	for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1}) {ae(f, i, ma);}
};



function checkEnter(event) {
	var code=0;
	if (isnn) code=event.which;
	else 	    code=event.keyCode;
	if (code==13) this.document.forms[0].submit();
}



function getcode(event,shiftd,ctrld) {
	var key=0;
	
	if (isnn) {
		key = event.which;
		ctrdown = event.modifiers && 2;
		shiftdown = event.modifiers && 4;
	}
	
	else {
		key = event.keyCode;
		ctrdown = event.ctrlKey;
		shiftdown = event.shiftKey;
	}

	if (shiftdown && shiftd) 		return key;
	else if (ctrdown && ctrld) 	return key;
	else 		return 0;
}

ae = function(o, e, f){
	var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c,d;
	r[r.length] = [f, o];
	o[e] = function(e){	try{
			(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
			e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});e.target || (e.target = e.srcElement || null);e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
		}catch(f){}
		for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
		return e = null, !!d;
	}
};


 
// used in jreps
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
	}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
			}
		}
	}
}

function getNextElement(iElement) {
 var parentForm = iElement.form;

 for (i=0; i<parentForm.length; i++) {
  if (parentForm.elements[i]==iElement)
   if (parentForm.elements[i+1])
    return parentForm.elements[i+1];
 } return null;
}


// autotab stuff used in jreps

function autotab(event,iCtl, iMax) {
	if (getcode(event,true,false)) return; // ingore shift
	var kk = getkey(event);
	if (kk==8 || kk==46 || kk==9) return;
	//alert ('here');	
	var nextElement, textValue = iCtl.value;
	//setval('yo',textValue.length);
	if (textValue.length>=iMax) {
		nextElement = getNextElement(iCtl);

		if (nextElement) {
			nextElement.select();
			nextElement.focus(); // this sometimes gives an error message when enter pressed
		}
	}
}


function getpath(inp){
	strURL=document.location.href;
	if (!inp) inp='?';
	pathp=strURL.lastIndexOf(inp);
	if(pathp>0)
		return strURL.substring(0,pathp+1);
	else
		return "";
}


// frame related stuff
function setframe(frm,path){
	top.frames[frm].location.href=getpath()+path;
}

function sendsearch (frm,parst) {
	//alert('parst='+ parst);
	pars=parst.split("&");
	newurl = pars[0];
	//alert('pc='+ pars.length);
	for (i=1; i<pars.length; i++) {
		//alert ('p'+i+':'+ix[i]+'='+getval(ix[i]);
		ix=pars[i].split("=");
		newurl+='&'+ix[0]+'='+getval(ix[1]);
		//alert('nu='+ newurl);
	}
	//alert('newurl='+newurl);
	setframe (frm,newurl);
}


function gettime(kk,vl,inter) {
	var min=0;hour=0;
	var ptr = vl.indexOf(":");
	if (ptr>0) {
		hour = 1*vl.slice(0,ptr);
		min = 1*vl.slice(ptr+1);
	}
	else {
		hour=vl;
		min=0;
	}
	//alert ('kk='+kk);	
	if (kk==38) min -=inter;
	if (kk==40) min +=inter;
	var omin = min;

	if (min <0)  {hour--;min=60-inter;}
	if (min>=60) {hour++;min=0;}
	if (hour<0)  {hour=23;}
	if (hour>23) {hour=0;}
	var mins = ""+min;
	if (kk==40 || kk==38) {
		if (hour.length==0) {hours = new Date().getUTChour()};
		if (mins.length==1) mins=mins="0"+mins;
		//alert('h='+hour + ' min='+ omin + '->' +mins+ ' v60=' + (min>=60)+ ' v0=' + (min<=0));
		return hour+':' + mins;
	}
	else return vl 
}
 

 function getstatus(driver) {
	//alert('d='+getctr('cat3'));
	var url = getpath() + driver  ;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	//alert('req='+req);
	req.onreadystatechange = processRequest;
	req.open("GET", url, true);
	req.send(null);
}


 function getreq(driver,fnx) {
	var url;
	if (driver.charAt(0)=='?') {
		url = getpath() + driver.substring(1)  ;
	}
	else if (driver.charAt(0)=='/') {
		url = getpath('/') + driver.substring(1)  ;
	}
	else 
		url =  driver  ;
	//alert('url='+url);
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.onreadystatechange = fnx;
	req.open("GET", url);
	req.send(null);
	return req;
}



function processRequest() {
	if (req.readyState == 4) {
		if (req.status == 200) 	  parseMessages();
		else 		  alert ( "Not able to retrieve description" );
	}
}

function parseMessages() {
	res = req.responseText;
	var xx= res.split('\n');
	var px = xx[0].split('|');
	var src = px[0];
	var qry = px[1];
	alert('d='+res);
	// trim
	//qry = qry.replace (/\s*$/,'');
	return res;
}



function sb (msg){
	pos=-20;
	mbox.display='';
	document.getElementById("bubble").innerHTML='<div class="msgdiv">'+msg+'</div>';
}


function hb(){
	pos = -1000;
	mbox.display="none";
}

