function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return (encoded);
};



function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return (plaintext);
};




	function jsCreateWindow(cIdPanel, cTemplate, aW, cCaption, x, y, w, h, nZOrder, cContent, nModal) {
	  if ($("#"+cIdPanel).length>0) {
	    $('#'+cIdPanel).remove();
	    if ($('#'+cIdPanel+'_back').length>0)  $('#'+cIdPanel+'_back').remove();
	  }

      if (typeof(lGlobalOldIE)=='undefined') lGlobalOldIE = 0;
      var cImgExt = ((lGlobalOldIE==1)?"gif":"png");
      
      var cHTML = "";
	  cHTML += "<div style='display:block;float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"tl."+cImgExt+");width:"+aW[0]+"px;height:"+aW[1]+"px;'></div>";
	  cHTML += "<div style='float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"tm."+cImgExt+");width:"+(w-aW[0]-aW[2])+"px;height:"+aW[1]+"px;'></div>";
	  cHTML += "<div style='display:block;float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"tr."+cImgExt+");width:"+aW[2]+"px;height:"+aW[1]+"px;'></div>";
	  
      cHTML += "<div style='float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"ml."+cImgExt+");width:"+aW[0]+"px;height:"+(h-aW[1]-aW[3])+"px;clear:both;'></div>";
	  cHTML += "<div id='"+cIdPanel+"Util' style='float:left;background-image:url("+cTemplate+"mm."+cImgExt+");width:"+(w-aW[0]-aW[2])+"px;height:"+(h-aW[1]-aW[3])+"px;'>";
	  cHTML += "<div style='width:"+(w-aW[0]-aW[2]-2)+"px; height:"+(aW[5])+"px;cursor:move;' class='cWinCaption'>";
	  cHTML += "<div id='"+cIdPanel+"Title' style='float:left;width:"+(w-aW[0]-aW[2]-4-aW[4])+"px; height:"+(aW[5])+"px;cursor:move;'>"+cCaption+"</div>";
	  var cAux = 'jsRemoveWindow("'+cIdPanel+'")';
	  cHTML += "<div style='float:left;width:"+(aW[4])+"px;height:"+(aW[5])+"px;background-image:url("+cTemplate+"close."+cImgExt+");cursor:pointer;' onclick='"+cAux+"');'></div>";
	  cHTML += "</div>";
	  cHTML += "<div id='"+cIdPanel+"_content' style='clear:both;'>"+cContent+"</div>";
	  cHTML += "</div>";
	  cHTML += "<div style='float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"mr."+cImgExt+");width:"+aW[2]+"px;height:"+(h-aW[1]-aW[3])+"px;'></div>";	  
      cHTML += "<div style='float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"bl."+cImgExt+");width:"+aW[0]+"px;height:"+aW[3]+"px;clear:both;'></div>";
	  cHTML += "<div style='float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"bm."+cImgExt+");width:"+(w-aW[0]-aW[2])+"px;height:"+aW[3]+"px;'></div>";
	  cHTML += "<div style='float:left;font-size:0px;line-spacing:0px;background-image:url("+cTemplate+"br."+cImgExt+");width:"+aW[2]+"px;height:"+aW[3]+"px;'></div>";
	  
	  if (nModal!=1) {
	    var bw = $('body').width();
	    var bh = $('body').height();
        
		// Coloca uma barreira para impossibiltar o acesso aos items que ficarão em segundo plano, criando um efeito de janela modal
        $('body').append("<div id='"+cIdPanel+"_back' style='position: absolute; z-index:"+(nZOrder-1)+"; left:"+0+"px; top:"+0+"px; width:"+bw+"px; height:"+bh+"px;background-image:url(/img/blackshadow."+cImgExt+");'></div>");
	  }
		
	  // Adiciona a janela à estrutura da página
	  $('body').append("<div id='"+cIdPanel+"' style='position: absolute; z-index:"+nZOrder+"; left:"+x+"px; top:"+y+"px; width:"+w+"px; height:"+h+"px;'>"+cHTML+"</div>");
	  
	  // Torna a janela arrastável
	  $('#'+cIdPanel).draggable({handle:('#'+cIdPanel+'Title')});

	}    
    
    function jsRemoveWindow(cIdPanel) {
	  $('#'+cIdPanel).remove();
	  if ($('#'+cIdPanel+'_back').length>0)  $('#'+cIdPanel+'_back').remove();
	}
    
    
   function jsFormKeyDown(e,cNext,cSubmitFunc,cWindow) {
     var nAction = 0;
     if (window.event) { // IE
       if (e.keyCode==13) nAction = 13;
	   if (e.keyCode==27) nAction = 27;
     }
     else if(e.which) {// Netscape/Firefox/Opera
       if (e.which==13) nAction = 13;
	   if (e.which==27) nAction = 27;
     }
     if (nAction==13) {
       if (cNext=="") {
	     eval(cSubmitFunc);
	   }
	   else {
	    $("#"+cNext).focus();
	   }
     }
     else if (nAction==27) {
       if (cWindow!="") jsRemoveWindow(cWindow);
     }
   }
   
   
function jsForceSubmit(cFormName) {
  $('#'+cFormName).submit();
}

function jsGlobalAlert(nX, nY, nW, nH, cText) {
  var cContent = '<p class="cDlgText" style="margin-bottom:10px;">'+cText+'</p>';
  var cAux = 'jsRemoveWindow("idAlert");';
  cContent += "<button style='margin-left:"+Math.round((nW-100-32)/2)+"px;' class='cWinButton' onclick='"+cAux+"'>Fechar</button>";
  
  jsCreateWindow('idAlert', '/img/bluewin', new Array(16, 16, 26, 26, 20, 20), 'Atenção!', nX, nY, nW, nH, 1100, cContent);  
}

function jsGlobalToUpper(cFieldName) {
  var cVal = $("#"+cFieldName).val();
  cVal = cVal.toUpperCase();  
  $("#"+cFieldName).val(cVal);
}

function jsGlobalCheckEMail(cEmail) {
  var reValidEmail = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
  return(reValidEmail.test(cEmail));
}

 $(document).ready(function() {
   // do stuff when DOM is ready
   // fazer pre-load (cache) das imagens usadas nas janelas
   
   imgBL = new Image(16,26);
   imgBL.src = '/img/bluewinbl.png';
   
   imgBM = new Image(16,26);
   imgBM.src = '/img/bluewinbm.png';
   
   imgBR = new Image(26,26);
   imgBR.src = '/img/bluewinbr.png';
   
   imgML = new Image(16,17);
   imgML.src = '/img/bluewinml.png';
   
   imgMM = new Image(15,15);
   imgMM.src = '/img/bluewinmm.png';
   
   imgMR = new Image(26,14);
   imgMR.src = '/img/bluewinmr.png';
   
   imgTL = new Image(16,16);
   imgTL.src = '/img/bluewintl.png';
   
   imgTM = new Image(17,16);
   imgTM.src = '/img/bluewintm.png';
   
   imgTR = new Image(26,16);
   imgTR.src = '/img/bluewintr.png';
   
   imgCL = new Image(20,20);
   imgCL.src = '/img/bluewinclose.png';
   
   imgBS = new Image(100,100);
   imgBS.src = '/img/backshadow.png';
   
}
)