/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

var tooltipObj;
var im = 1;

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}

/**
	* Funções Fluxnet & eKosnet

** trimToRect: trim text to a rectangle 15:24 01.05.2005 ******************** 
** width, height: dimensions of target rect (must be pixel value for height); 
** str: string to trim 
** strApp: string to append (e.g., "..." or "... (show more)") 
** strClass: css class to use (leave empty for body style) 
** bTrimToWords: prevents in-word trimming 
** bAlwaysAppend: strApp is always appended, even when text fits in rect. 
*****************************************************************************

EXAMPLE USAGE: 
... 
function refresh() { 
	elM = document.getElementById('fillme'); 
	elM.innerHTML = trimToRect(elM.style.width, elM.clientHeight, "replayce with very long text", "... <B>(show more)</B>", "smallText", false, false); 
} 
... 
<BODY onload="refresh()" onresize="refresh()"> 
<div id=ruler style="position:absolute; left:-5000px;"></div> 
<div id=fillme class=smallText style="position:absolute; top:20%; left:50%; width:40%; height:55%; overflow:hidden; background-color:#aaaaaa;"></DIV> 
</BODY> 
*/

function trimToRect(w, h, str, strApp, strClass, bTrimToWords, bAlwaysAppend) { 
	// element with id "ruler" must exist in the document, e.g.: 
	// <div id=ruler style="position:absolute; left:-5000px;"><!-- --></div> 
	elR = document.getElementById("ruler"); 
	elR.className = strClass; 
	elR.style.width = w; 

	if(bAlwaysAppend) 
		strResult = str + strApp; 
	else 
		strResult = str; 

	// fits already? opt out early 
	elR.innerHTML = strResult; 
	if(elR.clientHeight <= h) 
		return strResult; 

	with(Math) position = nextOffset = pow(2,floor(log(str.length) / LN2)); 

	do { 
		nextOffset = nextOffset / 2; 
		elR.innerHTML = str.substr(0, position) + strApp; 
		position = position + (elR.clientHeight > h? -nextOffset:nextOffset); 
	} while(nextOffset!= 1); 

	elR.innerHTML = str.substr(0, position) + strApp; 
	if(elR.clientHeight > h) 
		position--; 

	if(bTrimToWords) 
		while(str.substr(--position, 1)!= " "); 

	return str.substr(0, position) + strApp; 
}

function DelHTMLTags(str) {
	return str.replace(/<[^>]*>/g, "");
}

/* ***************************************************
By FluxNet
*************************************************** */
function PselectOptionValue(Id,campo)
{
	var selObj = parent.document.getElementById(Id);
	for (i=0; i<selObj.options.length; i++) {
		if(selObj.options[i].value == campo) {
			var num = i;
		}
	}
	selObj.selectedIndex = num;
}

function selectOptionValue(Id,campo)
{
	var selObj = document.getElementById(Id);
	for (i=0; i<selObj.options.length; i++) {
		if(selObj.options[i].value == campo) {
			var num = i;
		}
	}
	selObj.selectedIndex = num;
}

function selectOptionText(Id,campo)
{
	var selObj = document.getElementById(Id);
	for (i=0; i<selObj.options.length; i++) {
		if(selObj.options[i].text == campo) {
			var num = i;
		}
	}
	selObj.selectedIndex = num;
}

function addRowMov(id,t1,t2,t3,t4,t5,t6,t7,t8,cor){
   	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
   	var row = document.createElement("TR");
   	row.setAttribute("style","color:"+cor);
   	var td1 = document.createElement("TD");
   	td1.appendChild (document.createTextNode(t1));
   	var td2 = document.createElement("TD");
   	td2.appendChild (document.createTextNode(t2));
   	var td3 = document.createElement("TD");
   	td3.appendChild (document.createTextNode(t3));
   	var td4 = document.createElement("TD");
   	td4.appendChild (document.createTextNode(t4));
   	var td5 = document.createElement("TD");
   	td5.appendChild (document.createTextNode(t5));
   	var td6 = document.createElement("TD");
   	td6.appendChild (document.createTextNode(t6));
   	var td7 = document.createElement("TD");
   	td7.setAttribute("align","center");
   	td7.appendChild (document.createTextNode(t7));
   	var td8 = document.createElement("TD");
   	td8.setAttribute("align","right");
   	td8.appendChild (document.createTextNode(t8));
   	
   	row.appendChild(td1);
   	row.appendChild(td2);
   	row.appendChild(td3);
   	row.appendChild(td4);
   	row.appendChild(td5);
   	row.appendChild(td6);
   	row.appendChild(td7);
   	row.appendChild(td8);
	tbody.appendChild(row);
}


function addimage2_ajax(id,t2) {
   	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
    
   	if(t2 != "") {
   		var row = document.createElement("TR");

   		var td3 = document.createElement("TD");
   		var cellimg=document.createElement("img");
	    	cellimg.src="/sm/imgphp/phpThumb.php?src="+t2+"&w=72";
		    cellimg.border=0;
		    cellimg.name="img"+im;
	    	cellimg.id=im;
   		td3.appendChild (cellimg);

   		var td2 = document.createElement("TD");
   		td2.appendChild (document.createTextNode(t2));

   		/*var td4 = document.createElement("TD");
   		td4.appendChild (document.createTextNode(t2));
   		var cellhd=document.createElement("input");
		    cellhd.type="hidden";
		    cellhd.value=t2;
	    	cellhd.name="img_tmp"+im;
	    	cellhd.id="img_tmp"+im;
		td4.appendChild (cellhd);

   		addEvent(cellimg, "mouseover", showimgtmp);
   		addEvent(cellimg, "mouseout", hideimgtmp);*/
	
   		row.appendChild(td3);
	   	row.appendChild(td2);
	   	/*row.appendChild(td4);*/
		tbody.appendChild(row);
		im = im + 1;
   	}
}


function addimagem_ajax(id,t2) {
   	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
   	/*var t2    = document.getElementById("imgtmp").value;*/
    
   	if(t2 != "") {
   		var row = document.createElement("TR");

   		var td1 = document.createElement("TD");
   		var cellText=document.createElement("input");
	    	cellText.type="checkbox";
	    	cellText.checked=false;
	    	cellText.value=1;
	    	cellText.name="chkbox"+im;
	    	cellText.id="chkbox"+im;
   		td1.appendChild (cellText);

   		var td2 = document.createElement("TD");
   		td2.appendChild (document.createTextNode(t2));
   		var cellhd=document.createElement("input");
		    cellhd.type="hidden";
		    cellhd.value=t2;
	    	cellhd.name="img_tmp"+im;
	    	cellhd.id="img_tmp"+im;
		td2.appendChild (cellhd);

   		var td3 = document.createElement("TD");
   		var cellimg=document.createElement("img");
	    	cellimg.src="/sm/imgphp/phpThumb.php?src="+t2+"&w=72";
		    cellimg.border=0;
		    cellimg.name="img"+im;
	    	cellimg.id=im;
   		td3.appendChild (cellimg);
   	
   		addEvent(cellimg, "mouseover", showimgtmp);
   		addEvent(cellimg, "mouseout", hideimgtmp);
	
   		row.appendChild(td1);
	   	row.appendChild(td2);
   		row.appendChild(td3);
		tbody.appendChild(row);
		im = im + 1;
		document.getElementById("imgtmp").value = "";
		document.getElementById("totimage").value = im;
   	}
}

function addimagem(id) {
   	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
   	var t2    = document.getElementById("imgtmp").value;
    
   	if(t2 != "") {
   		var row = document.createElement("TR");

   		var td1 = document.createElement("TD");
   		var cellText=document.createElement("input");
	    	cellText.type="checkbox";
	    	cellText.checked=false;
	    	cellText.value=1;
	    	cellText.name="chkbox"+im;
	    	cellText.id="chkbox"+im;
   		td1.appendChild (cellText);

   		var td2 = document.createElement("TD");
   		td2.appendChild (document.createTextNode(t2));
   		var cellhd=document.createElement("input");
		    cellhd.type="hidden";
		    cellhd.value=t2;
	    	cellhd.name="img_tmp"+im;
	    	cellhd.id="img_tmp"+im;
		td2.appendChild (cellhd);

   		var td3 = document.createElement("TD");
   		var cellimg=document.createElement("img");
	    	cellimg.src="/sm/imgphp/phpThumb.php?src="+t2+"&w=72";
		    cellimg.border=0;
		    cellimg.name="img"+im;
	    	cellimg.id=im;
   		td3.appendChild (cellimg);
   	
   		addEvent(cellimg, "mouseover", showimgtmp);
   		addEvent(cellimg, "mouseout", hideimgtmp);
	
   		row.appendChild(td1);
	   	row.appendChild(td2);
   		row.appendChild(td3);
		tbody.appendChild(row);
		im = im + 1;
		document.getElementById("imgtmp").value = "";
		document.getElementById("totimage").value = im;
   	}
}
function showimgtmp() {
	var strElementId = this.getAttribute("id");
	img00 = "img_tmp"+strElementId;
	tooltipObj = new DHTMLSuite.dynamicTooltip();	// Create ONE tooltip object.
	
/*	document.getElementById("prev").src = '/sm/imgphp/phpThumb.php?src='+document.getElementById(img00).value+'&w=345';
	document.getElementById("imgprev").style.left = '50%';
	document.getElementById("imgprev").style.top = '50%';
	document.getElementById("imgprev").style.visibility = 'visible';
*/
	tooltipObj.displayTooltip('ajax-img.php?w=180&i='+document.getElementById(img00).value,document.getElementById(strElementId));
	return false;
}
function hideimgtmp() {
	/*document.getElementById("imgprev").style.visibility = 'hidden';*/
	tooltipObj.hideTooltip();
	return false;
}

function delimage(tblId) {
	// Apagar Linha
	var tbl = document.getElementById(tblId);
	for (var i=tbl.tBodies[0].rows.length-1; i>=1; i--) {
		x = tbl.tBodies[0].rows[i].getElementsByTagName("input").item(0);
		//alert(x.type + " : " + x.checked);
		if(x.checked) {
			tbl.tBodies[0].deleteRow(i);
		}
	}
}

function removeRowimage(tblId)
{
	var tbl = document.getElementById(tblId);
	for (var i=tbl.tBodies[0].rows.length-1; i>=1; i--) {
		tbl.tBodies[0].deleteRow(i);
	}
}

function removeRowNumber(tblId,line)
{
	var tbl = document.getElementById(tblId);
	tbl.tBodies[0].deleteRow(line);
}

function removeRowFromTable(tblId)
{
	var tbl = document.getElementById(tblId);
	for (var i=tbl.tBodies[0].rows.length-1; i>=0; i--) {
		tbl.tBodies[0].deleteRow(i);
	}
}

function changeTabCell(tblID,rrow,ccell,content,p)
{
	if(p==1){
		var tbl = parent.document.getElementById(tblID).rows[rrow].cells;	
	} else {
		var tbl = document.getElementById(tblID).rows[rrow].cells;
	}
	tbl[ccell].innerHTML=content;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
// getCheckedValue(document.forms['radioExampleForm'].elements['number']);

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
// setCheckedValue(document.forms['radioExampleForm'].elements['number'], '2');

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

/**
*
* UTF-8 data encode / decode
* http://www.webtoolkit.info/
*
**/

var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

// Newsletters Users
function adduserlst_ajax(id,t2,act,iduser,site,actu) {

   	var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
   	if(t2 != "") {
   		var row = document.createElement("TR");
   			row.id = 'us' + im;
   		var td2 = document.createElement("TD");
   		td2.appendChild (document.createTextNode(t2));
   		//var cellhd=document.createElement("input");
		//    cellhd.type="hidden";
		//    cellhd.value=t2;
	   // 	cellhd.name="img_tmp"+im;
	   // 	cellhd.id="img_tmp"+im;
		//td2.appendChild (cellhd);

   		var td3 = document.createElement("TD");
   		    td3.align="center";
  		var na = document.createElement('a');
  			//na.innerHTML = "href='#' onclick='deluser('"+im+"','"+site+"','"+iduser+"','"+t2+"',this);'><img src='/mg3/images/news/remove.gif' id='us0"+im+"' name='us0"+im+"' border=0 width=18 height=18>"
			na.setAttribute('href', "javascript:deluser('"+im+"','"+site+"','"+iduser+"','"+t2+"');");
			na.setAttribute('id',"U"+im);
			//na.attachEvent("onclick",function () {deluser(im,site,iduser,t2,this);});
  			//na.onclick = function () { deluser(im,site,iduser,t2,'');}
   		var cellimg=document.createElement("img");
	    	cellimg.src="/mg3/images/news/remove.gif";
		    cellimg.border=0;
		    cellimg.width=18;
		    cellimg.height=18;
		    cellimg.name="us0"+im;
	    	cellimg.id="us0"+im;
   		na.appendChild (cellimg);
   		
  		var na1 = document.createElement('a');
  			na1.setAttribute('href', "javascript:edituser("+im+",'"+site+"',"+iduser+",'"+t2+"');");
  			//na1.id = "U1"+im;
  			na1.setAttribute('id',"U1"+im);
   		var cellimg1=document.createElement("img");
	    	cellimg1.src="/mg3/images/news/edit.gif";
		    cellimg1.border=0;
		    cellimg1.width=18;
		    cellimg1.height=18;
		    cellimg1.name="us1"+im;
	    	cellimg1.id="us1"+im;
   		na1.appendChild (cellimg1);
   		
  		var na2 = document.createElement('a');
  			na2.setAttribute('href', "javascript:onoffuser("+im+",'"+site+"',"+iduser+","+actu+",'"+t2+"');");
  			na2.setAttribute('id',"U2"+im);
  			//na2.id = "U2"+im;
  			//na2.setAttribute('href', 'javascript:onoffuser(im=us2'+im+',site='+site+',id='+iduser+',actu='+actu+',usr='+t2+');');
   		var cellimg2=document.createElement("img");
	    	cellimg2.src=act;
		    cellimg2.border=0;
		    cellimg2.width=18;
		    cellimg2.height=18;
		    cellimg2.name="us2"+im;
	    	cellimg2.id="us2"+im;
   		na2.appendChild (cellimg2);

   		td3.appendChild (na);
   		td3.appendChild (document.createTextNode('  '));
   		td3.appendChild (na1);
   		td3.appendChild (document.createTextNode('  '));
   		td3.appendChild (na2);
   		
	   	row.appendChild(td2);
   		row.appendChild(td3);
		tbody.appendChild(row);
		im = im + 1;
   	}
}

// Apartamentos
function addapart_ajax(tab,id,img,tipol,qtd,estado,p) {

	if(p==1){
   		var tbody = parent.document.getElementById(tab).getElementsByTagName("TBODY")[0];
	} else {
		var tbody = document.getElementById(tab).getElementsByTagName("TBODY")[0];
	}
   	if(tipol != "") {
   		var row = document.createElement("TR");
   			row.id = 'us' + im;
   		var td2 = document.createElement("TD");
   		td2.appendChild (document.createTextNode(tipol));
   		var td21 = document.createElement("TD");
   		td21.appendChild (document.createTextNode(qtd));

   		var td3 = document.createElement("TD");
   		    td3.align="center";

  		var na = document.createElement('a');
			na.setAttribute('href', "javascript:delapart('"+im+"','"+id+"');");
			na.setAttribute('id',"U"+im);
   		var cellimg=document.createElement("img");
	    	cellimg.src="/mg3/images/news/remove.gif";
		    cellimg.border=0;
		    cellimg.width=18;
		    cellimg.height=18;
		    cellimg.name="us0"+im;
	    	cellimg.id="us0"+im;
   		na.appendChild (cellimg);
   		
  		var na1 = document.createElement('a');
  			na1.setAttribute('href', "javascript:editapart("+im+","+id+");");
  			na1.setAttribute('id',"U1"+im);
   		var cellimg1=document.createElement("img");
	    	cellimg1.src="/mg3/images/news/edit.gif";
		    cellimg1.border=0;
		    cellimg1.width=18;
		    cellimg1.height=18;
		    cellimg1.name="us1"+im;
	    	cellimg1.id="us1"+im;
   		na1.appendChild (cellimg1);
   		
  		var na2 = document.createElement('a');
  			na2.setAttribute('href', "javascript:onoffapart("+im+","+id+");");
  			na2.setAttribute('id',"U2"+im);
   		var cellimg2=document.createElement("img");
	    	cellimg2.src=img;
		    cellimg2.border=0;
		    cellimg2.width=18;
		    cellimg2.height=18;
		    cellimg2.name="us2"+im;
	    	cellimg2.id="us2"+im;
   		na2.appendChild (cellimg2);

   		td3.appendChild (na);
   		td3.appendChild (document.createTextNode('  '));
   		td3.appendChild (na1);
   		td3.appendChild (document.createTextNode('  '));
   		td3.appendChild (na2);
   		
	   	row.appendChild(td2);
	   	row.appendChild(td21);
   		row.appendChild(td3);
		tbody.appendChild(row);
		im = im + 1;
   	}
}

// H4U Facilidades
function addh4ufacil_ajax(tab,id,descp) {
   	var tbody = document.getElementById(tab).getElementsByTagName("TBODY")[0];
    
	var row = document.createElement("TR");

   	var td1 = document.createElement("TD");
   	td1.appendChild (document.createTextNode(id));
   	var td2 = document.createElement("TD");
   	td2.appendChild (document.createTextNode(descp));

   	row.appendChild(td1);
	row.appendChild(td2);
	tbody.appendChild(row);
}

// H4U Rooms
function addh4uroom_ajax(tab,id,descp,min,max) {
   	var tbody = document.getElementById(tab).getElementsByTagName("TBODY")[0];
    
	var row = document.createElement("TR");

   	var td1 = document.createElement("TD");
   	td1.appendChild (document.createTextNode(id));
   	var td2 = document.createElement("TD");
   	td2.appendChild (document.createTextNode(descp));
   	var td3 = document.createElement("TD");
   	td3.appendChild (document.createTextNode(min));
   	var td4 = document.createElement("TD");
   	td4.appendChild (document.createTextNode(max));

   	row.appendChild(td1);
	row.appendChild(td2);
	row.appendChild(td3);
	row.appendChild(td4);
	tbody.appendChild(row);
}

// Tabelas

// F. Permadi 2005.
// Highlights table row
// Copyright (C) F. Permadi
// This code is provided "as is" and without warranty of any kind.  Use at your own risk.

// These variables are for saving the original background colors
var savedStates=new Array();
var savedStateCount=0;

/////////////////////////////////////////////////////
// This function takes an element as a parameter and 
//   returns an object which contain the saved state
//   of the element's background color.
/////////////////////////////////////////////////////
function saveBackgroundStyle(myElement)
{
  saved=new Object();
  saved.element=myElement;
  saved.className=myElement.className;
  saved.backgroundColor=myElement.style["backgroundColor"];
  return saved;   
}

/////////////////////////////////////////////////////
// This function takes an element as a parameter and 
//   returns an object which contain the saved state
//   of the element's background color.
/////////////////////////////////////////////////////
function restoreBackgroundStyle(savedState)
{
  savedState.element.style["backgroundColor"]=savedState.backgroundColor;
  if (savedState.className)
  {
    savedState.element.className=savedState.className;    
  }
}

/////////////////////////////////////////////////////
// This function is used by highlightTableRow() to find table cells (TD) node
/////////////////////////////////////////////////////
function findNode(startingNode, tagName)
{
  // on Firefox, the TD node might not be the firstChild node of the TR node
  myElement=startingNode;
  var i=0;
  while (myElement && (!myElement.tagName || (myElement.tagName && myElement.tagName!=tagName)))
  {
    myElement=startingNode.childNodes[i];
    i++;
  }  
  if (myElement && myElement.tagName && myElement.tagName==tagName)
  {
    return myElement;
  }
  // on IE, the TD node might be the firstChild node of the TR node  
  else if (startingNode.firstChild)
    return findNode(startingNode.firstChild, tagName);
  return 0;
}

/////////////////////////////////////////////////////
// Highlight table row.
// newElement could be any element nested inside the table
// highlightColor is the color of the highlight
/////////////////////////////////////////////////////
function highlightTableRow(myElement, highlightColor)
{
  var i=0;
  // Restore color of the previously highlighted row
  for (i; i<savedStateCount; i++)
  {
    restoreBackgroundStyle(savedStates[i]);          
  }
  savedStateCount=0;

  // To get the node to the row (ie: the <TR> element), 
  // we need to traverse the parent nodes until we get a row element (TR)
  // Netscape has a weird node (if the mouse is over a text object, then there's no tagName
  while (myElement && ((myElement.tagName && myElement.tagName!="TR") || !myElement.tagName))
  {
    myElement=myElement.parentNode;
  }

  // If you don't want a particular row to be highlighted, set it's id to "header"
  // If you don't want a particular row to be highlighted, set it's id to "header"
  if (!myElement || (myElement && myElement.id && myElement.id=="header") )
    return;
		  
  // Highlight every cell on the row
  if (myElement)
  {
    var tableRow=myElement;
    
    // Save the backgroundColor style OR the style class of the row (if defined)
    if (tableRow)
    {
	  savedStates[savedStateCount]=saveBackgroundStyle(tableRow);
      savedStateCount++;
    }

    // myElement is a <TR>, then find the first TD
    var tableCell=findNode(myElement, "TD");    

    var i=0;
    // Loop through every sibling (a sibling of a cell should be a cell)
    // We then highlight every siblings
    while (tableCell)
    {
      // Make sure it's actually a cell (a TD)
      if (tableCell.tagName=="TD")
      {
        // If no style has been assigned, assign it, otherwise Netscape will 
        // behave weird.
        if (!tableCell.style)
        {
          tableCell.style={};
        }
        else
        {
          savedStates[savedStateCount]=saveBackgroundStyle(tableCell);        
          savedStateCount++;
        }
        // Assign the highlight color
        tableCell.style["backgroundColor"]=highlightColor;

        // Optional: alter cursor
        tableCell.style.cursor='default';
        i++;
      }
      // Go to the next cell in the row
      tableCell=tableCell.nextSibling;
    }
  }
}

/////////////////////////////////////////////////////
// This function is to be assigned to a <table> mouse event handler.
// If the element that fired the event is within a table row,
//   this function will highlight the row.
/////////////////////////////////////////////////////
function trackTableHighlight(mEvent, highlightColor)
{
  if (!mEvent)
    mEvent=window.event;
		
  // Internet Explorer
  if (mEvent.srcElement)
  {
    highlightTableRow( mEvent.srcElement, highlightColor);
  }
  // Netscape and Firefox
  else if (mEvent.target)
  {
    highlightTableRow( mEvent.target, highlightColor);		
  }
}

/////////////////////////////////////////////////////
// Highlight table row.
// newElement could be any element nested inside the table
// highlightColor is the color of the highlight
/////////////////////////////////////////////////////
function highlightTableRowVersionA(myElement, highlightColor)
{
  var i=0;
  // Restore color of the previously highlighted row
  for (i; i<savedStateCount; i++)
  {
    restoreBackgroundStyle(savedStates[i]);          
  }
  savedStateCount=0;

  // If you don't want a particular row to be highlighted, set it's id to "header"
  if (!myElement || (myElement && myElement.id && myElement.id=="header") )
    return;
		  
  // Highlight every cell on the row
  if (myElement)
  {
    var tableRow=myElement;
    
    // Save the backgroundColor style OR the style class of the row (if defined)
    if (tableRow)
    {
	  savedStates[savedStateCount]=saveBackgroundStyle(tableRow);
      savedStateCount++;
    }

    // myElement is a <TR>, then find the first TD
    var tableCell=findNode(myElement, "TD");    

    var i=0;
    // Loop through every sibling (a sibling of a cell should be a cell)
    // We then highlight every siblings
    while (tableCell)
    {
      // Make sure it's actually a cell (a TD)
      if (tableCell.tagName=="TD")
      {
        // If no style has been assigned, assign it, otherwise Netscape will 
        // behave weird.
        if (!tableCell.style)
        {
          tableCell.style={};
        }
        else
        {
          savedStates[savedStateCount]=saveBackgroundStyle(tableCell);        
          savedStateCount++;
        }
        // Assign the highlight color
        tableCell.style["backgroundColor"]=highlightColor;

        // Optional: alter cursor
        tableCell.style.cursor='default';
        i++;
      }
      // Go to the next cell in the row
      tableCell=tableCell.nextSibling;
    }
  }
}

// SelectBox
// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// HISTORY
// ------------------------------------------------------------------
// April 20, 2005: Fixed the removeSelectedOptions() function to 
//                 correctly handle single selects
// June 12, 2003: Modified up and down functions to support more than
//                one selected option
/*
DESCRIPTION: These are general functions to deal with and manipulate
select boxes. Also see the OptionTransfer library to more easily 
handle transferring options between two lists

COMPATABILITY: These are fairly basic functions - they should work on
all browsers that support Javascript.
*/


// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}

// -------------------------------------------------------------------
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
//  This is a general function used by the select functions below, to
//  avoid code duplication
// -------------------------------------------------------------------
function selectUnselectMatchingOptions(obj,regex,which,only) {
	if (window.RegExp) {
		if (which == "select") {
			var selected1=true;
			var selected2=false;
			}
		else if (which == "unselect") {
			var selected1=false;
			var selected2=true;
			}
		else {
			return;
			}
		var re = new RegExp(regex);
		if (!hasOptions(obj)) { return; }
		for (var i=0; i<obj.options.length; i++) {
			if (re.test(obj.options[i].text)) {
				obj.options[i].selected = selected1;
				}
			else {
				if (only == true) {
					obj.options[i].selected = selected2;
					}
				}
			}
		}
	}
		
// -------------------------------------------------------------------
// selectMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Currently-selected options will not be changed.
// -------------------------------------------------------------------
function selectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",false);
	}
// -------------------------------------------------------------------
// selectOnlyMatchingOptions(select_object,regex)
//  This function selects all options that match the regular expression
//  passed in. Selected options that don't match will be un-selected.
// -------------------------------------------------------------------
function selectOnlyMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"select",true);
	}
// -------------------------------------------------------------------
// unSelectMatchingOptions(select_object,regex)
//  This function Unselects all options that match the regular expression
//  passed in. 
// -------------------------------------------------------------------
function unSelectMatchingOptions(obj,regex) {
	selectUnselectMatchingOptions(obj,regex,"unselect",false);
	}
	
// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
	
// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
			to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
//  This function copies options between select boxes instead of 
//  moving items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copySelectedOptions(from,to) {
	var options = new Object();
	if (hasOptions(to)) {
		for (var i=0; i<to.options.length; i++) {
			options[to.options[i].value] = to.options[i].text;
			}
		}
	if (!hasOptions(from)) { return; }
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
				if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				to.options[index] = new Option( o.text, o.value, false, false);
				}
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}

// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		moveSelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		moveSelectedOptions(from,to,arguments[2]);
		}
	else if (arguments.length==4) {
		moveSelectedOptions(from,to,arguments[2],arguments[3]);
		}
	}

// -------------------------------------------------------------------
// copyAllOptions(select_object,select_object[,autosort(true/false)])
//  Copy all options from one select box to another, instead of
//  removing items. Duplicates in the target list are not allowed.
// -------------------------------------------------------------------
function copyAllOptions(from,to) {
	selectAllOptions(from);
	if (arguments.length==2) {
		copySelectedOptions(from,to);
		}
	else if (arguments.length==3) {
		copySelectedOptions(from,to,arguments[2]);
		}
	}

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}
	
// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
				}
			}
		}
	}

// -------------------------------------------------------------------
// removeSelectedOptions(select_object)
//  Remove all selected options from a list
//  (Thanks to Gene Ninestein)
// -------------------------------------------------------------------
function removeSelectedOptions(from) { 
	if (!hasOptions(from)) { return; }
	if (from.type=="select-one") {
		from.options[from.selectedIndex] = null;
		}
	else {
		for (var i=(from.options.length-1); i>=0; i--) { 
			var o=from.options[i]; 
			if (o.selected) { 
				from.options[i] = null; 
				} 
			}
		}
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// removeAllOptions(select_object)
//  Remove all options from a list
// -------------------------------------------------------------------
function removeAllOptions(from) { 
	if (!hasOptions(from)) { return; }
	for (var i=(from.options.length-1); i>=0; i--) { 
		from.options[i] = null; 
		} 
	from.selectedIndex = -1; 
	} 

// -------------------------------------------------------------------
// addOption(select_object,display_text,value,selected)
//  Add an option to a list
// -------------------------------------------------------------------
function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
}

// -------------------------------------------------------------------
// Send info to other URL via POST
// -------------------------------------------------------------------
function post(URL, PARAMS) {
  var temp=document.createElement("form");
  temp.action=URL;
  temp.method="POST";
  temp.style.display="none";
	for(var x in PARAMS) {
	   var opt=document.createElement("textarea");
	   opt.name=x;
	   opt.value=PARAMS[x];
	   temp.appendChild(opt);
	}
	document.body.appendChild(temp);
	temp.submit();
	return temp;
}

// -------------------------------------------------------------------
// Verify  is Numeric
// -------------------------------------------------------------------
function IsValidNum(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }

   return IsNumber;   
}
