// Diversas funções em JavaScript

// Abre janela Popup centralizada na tela
function abre_win(nome, url, largura, altura)
{
	//window.open(url, name, 'left='+Math.round((screen.availWidth-largura)/2)+',top='+Math.round((screen.availHeight-altura)/2)+',width='+largura+',height='+altura+',toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no');
	
	newWindow = window.open("","newWindow","width="+(largura+0)+",height="+altura+",left="+Math.round((screen.availWidth-largura)/2)+",top="+Math.round((screen.availHeight-altura)/2)+", scrollbars=no");
	newWindow.document.open();
	newWindow.document.write('<html><title>:: Portal Via Forum ::</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0"">'); 
	newWindow.document.write('<div align=center><iframe align=top allowtransparency=true frameborder=0 height='+altura+' name=Frame scrolling=no src='+url+' width='+largura+' ></iframe><div>'); 
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
	
}
// Idem, mas com Barra de Scroll
function abre_win2(nome, url, largura, altura)
{
	window.open(url, name, 'left='+Math.round((screen.availWidth-largura)/2)+',top='+Math.round((screen.availHeight-altura)/2)+',width='+largura+',height='+altura+',toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=no');
}
// Abre Janela em FULL-SIZE
function abre_win3(nome, url, largura, altura)
{
	window.open(url, name, 'left=0 ,top=0,width='+(screen.availWidth-8)+',height='+(screen.availHeight-140)+',toolbar=yes,menubar=yes,status=yes,scrollbars=yes,resizable=yes');
}
//Abre Janela com Codigo HTML dentro. Usado apenas para IMAGENS!
function abre_win4(nome, url, largura, altura) {
	newWindow = window.open("","newWindow","width="+(largura+19)+",height="+altura+",left="+Math.round((screen.availWidth-largura)/2)+",top="+Math.round((screen.availHeight-altura)/2)+", scrollbars=yes");
	newWindow.document.open();
	newWindow.document.write('<html><title>:: Portal Via Forum ::</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">'); 
	newWindow.document.write('<div align=center><img src='+url+'><div>'); 
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}
//
function abre_win5(nome, url, largura, altura)
{
	//window.open(url, name, 'left='+Math.round((screen.availWidth-largura)/2)+',top='+Math.round((screen.availHeight-altura)/2)+',width='+largura+',height='+altura+',toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no');
	
	newWindow = window.open("","newWindow","width="+(largura+0)+",height="+altura+",left="+Math.round((screen.availWidth-largura)/2)+",top="+Math.round((screen.availHeight-altura)/2)+", resizable=no status=no toolbar=no");
	newWindow.document.open();
	newWindow.document.write('<html><title>:: Portal SecMaster ::</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0"">'); 
	newWindow.document.write('<div align=center><iframe align=top allowtransparency=true frameborder=0 height='+altura+' name=Frame scrolling=yes src='+url+' width='+largura+' ></iframe><div>'); 
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
	
}


// Corrige o fundo transparente para imagens PNG no IE
var strGif = "Imagens/Espaco.gif"
var strFilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader"

function correctPNG() 
{
   for(var i=0; i<document.images.length; i++)
   {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	  {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
         if (img.useMap)
		 {		  
	      	 strAddMap = "<img style=\"position:relative; left:-" + img.width + "px;"
	         + "height:" + img.height + "px;width:" + img.width +"\" "
			 + "src=\"" + strGif + "\" usemap=\"" + img.useMap 
			 + "\" border=\"" + img.border + "\">"
		 }		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:" + strFilter
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 if (img.useMap) strNewHTML += strAddMap
		 img.outerHTML = strNewHTML
		 i = i-1
	  }
   }
   for(i=0; i < document.forms.length; i++)
   {
      findImgInputs(document.forms(i))
   }
}

function findImgInputs(oParent)
{
	var oChildren = oParent.children
    if (oChildren)
	{	
		for (var i=0; i < oChildren.length; i++ )
		{
		   var oChild = oChildren(i)
           if ((oChild.type == 'image') && (oChild.src))
		   {
		       var origSrc = oChild.src
		       oChild.src = strGif
		       oChild.style.filter = strFilter + "(src='" + origSrc + "')"
		   }
		   findImgInputs(oChild)	
	    }
	}
}
window.attachEvent("onload", correctPNG);
////

// Preenche o campo Telefone, Data, CEP, CPF e CNPJ com Zeros
function Retira_Zeros_TEL(campo)
	{
	if (document.getElementById(campo).value=='(00) 0000.0000')
		{
		document.getElementById(campo).value='';
		}
	}
function Retira_Zeros_Data(campo)
	{
	if (document.getElementById(campo).value=='00/00/0000')
		{
		document.getElementById(campo).value='';
		}
	}
function Retira_Zeros_CEP(campo)
	{
	if (document.getElementById(campo).value=='00000-000')
		{
		document.getElementById(campo).value='';
		}
	}
function Retira_Zeros_CPF(campo)
	{
	if (document.getElementById(campo).value=='000.000.000-00')
		{
		document.getElementById(campo).value='';
		}
	}
function Retira_Zeros_CNPJ(campo)
    {
    if (document.getElementById(campo).value=='00.000.000/0000-00')
	    {
	    document.getElementById(campo).value='';
	    }
    }
//

// Retira os Zeros do campo Telefone, CEP, CPF e CNPJ quando o usuário clica sobre ele
function Coloca_Zeros_TEL(campo)
	{
	if (document.getElementById(campo).value=='(' | document.getElementById(campo).value=='')
		{
		document.getElementById(campo).value='(00) 0000.0000';
		}
	}	
function Coloca_Zeros_Data(campo)
	{
	if (document.getElementById(campo).value=='')
		{
		document.getElementById(campo).value='00/00/0000';
		}
	}	
function Coloca_Zeros_CEP(campo)
	{
	if (document.getElementById(campo).value=='')
		{
		document.getElementById(campo).value='00000-000';
		}
	}	
function Coloca_Zeros_CPF(campo)
	{
	if (document.getElementById(campo).value=='')
		{
		document.getElementById(campo).value='000.000.000-00';
		}
	}	
function Coloca_Zeros_CNPJ(campo)
	{
	if (document.getElementById(campo).value=='')
		{
		document.getElementById(campo).value='00.000.000/0000-00';
		}
	}	
//

// Funciona = ao Request.QueryString do ASP.NET
function PageQuery(q) 
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}

function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}	
//

// Troca letras minúsculas para maiúsculas em tempo de digitação
function changeKey (textControl, evt, keyChecker) {
	var keyCode = evt.keyCode ? evt.keyCode :
					evt.charCode ? evt.charCode :
			evt.which ? evt.which : void 0;
	var key;
	if (keyCode) {
		key = String.fromCharCode(keyCode);
	}
	var keyCheck = keyChecker(keyCode, key);
	if (keyCode && window.event && !window.opera) {
		if (keyCheck.cancelKey) {
		return false;
		}
		else if (keyCheck.replaceKey) {
		window.event.keyCode = keyCheck.newKeyCode;
		if (window.event.preventDefault) {
			window.event.preventDefault();
		}
		return true;
		}
		else {
		return true;
		}
	}
	else if (typeof textControl.setSelectionRange != 'undefined') {
		if (keyCheck.cancelKey) {
		if (evt.preventDefault) {

			evt.preventDefault();
		}
		return false;
		}
		else if (keyCheck.replaceKey) {
		// cancel the key event and insert the newKey for the current
		// selection
		if (evt.preventDefault) {
		evt.preventDefault();
		}
		var oldSelectionStart = textControl.selectionStart;
		var oldSelectionEnd = textControl.selectionEnd;
		var selectedText = textControl.value.substring(oldSelectionStart,
														oldSelectionEnd);
		var newText = typeof keyCheck.newKey != 'undefined'
						? keyCheck.newKey
						: String.fromCharCode(keyCheck.newKeyCode);
		textControl.value = 
			textControl.value.substring(0, oldSelectionStart) +
			newText +
			textControl.value.substring(oldSelectionEnd);
		textControl.setSelectionRange(oldSelectionStart + newText.length,
										oldSelectionStart + newText.length);
		return false;
		}
		else {
		return true;
		} 
	}
	else if (keyCheck.cancelKey) {
		if (evt.preventDefault) {
		evt.preventDefault();
		}
		return false;
	}
	else {
		return true;
	}
}

function lettersToUpperCase (keyCode, key) {
	var newKey = key.toUpperCase();
	if (newKey != key) {
		return { replaceKey: true,
				newKeyCode: newKey.charCodeAt(),
				newKey: newKey };
	}
	else {
		return { cancelKey: false };
	}
}


// Função TRIM
function Trim(texto)
{
	while(texto.slice(0, 1) == ' ')
	texto = texto.slice(1);
	while(texto.slice(texto.length - 1) == ' ')
	texto = texto.slice(0, texto.length - 1);
	return texto;
}

// Função para desligar a tecla ENTER e não dar o submit automaticamente
var nav = window.Event ? true : false; 
if (nav) { 
window.captureEvents(Event.KEYDOWN); 
window.onkeydown = NetscapeEventHandler_KeyDown; 
} else { 
document.onkeydown = MicrosoftEventHandler_KeyDown; 
} 

function NetscapeEventHandler_KeyDown(e) { 
if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false; } 
return true; 
} 

function MicrosoftEventHandler_KeyDown() { 
if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit') 
return false; 
return true; 
}

// Função para manter o Scroll na mesma posição após o Postback. 
/*function SetScrollPosition()
{ 
	var intY = document.body.scrollTop; 
	document.cookie = "yPos=!~" + intY + "~!"; 
}
window.onscroll = SetScrollPosition;
*/

// Funções do Dreamweaver para fazer mouse over nos botões
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Recupera o endereço completo (URL) de uma página
function getURL(uri) {
    uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
    uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
    uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
    uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
    pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
    pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
    uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
    uri.file = uri.page;
    if (uri.ext != '') uri.file += '.' + uri.ext;
    if (uri.file == '') uri.page = 'index';
    uri.args = location.search.substr(1).split("?");
    return uri;
}

//Address of this page : "http://javascript.about.com/library/blurl1.htm"

//domain (uri.dom) : "javascript.about.com"
//path (uri.path) : "library"
//page (uri.page) : "blurl1"
//extension (uri.ext) : "htm"
//filename (uri.file) : "blurl1.htm"
//query string (uri.arg) : ""

//Como Usar:
//var uri = new Object();
//getURL(uri);

