function mask(obj,e,mask )
{
   if ((obj.value.length >= obj.maxLength) || (obj.value.length >= mask.length)) return false;

   var strCheck = '#ADMYHMS';
   var numbers = '0123456789';
   var whichCode = (window.Event) ? e.which : e.keyCode;
   var key = String.fromCharCode(whichCode);
   var cChar = mask.charAt(obj.value.length);

   while ((obj.value.length < obj.maxLength) && (obj.value.length < mask.length))
   {
      if (strCheck.indexOf(cChar) < 0)
      {
         obj.value += cChar;
      }
      else
      {
         if (('#DMYHMS'.indexOf(cChar) >= 0) && (numbers.indexOf(key) < 0)) return false;
         break;
      }
      cChar = mask.charAt(obj.value.length);
   }
   return true;
}

/*******************************************************************/

function Valida_CPF (CPF) {return isCPF (CPF)}

function Valida_CNPJ(CNPJ)	{return isCNPF (CNPJ)}

function Valida_Data(Obj, FieldName, FieldFocus){return isDate(Obj, FieldName, FieldFocus)}

function Valida_Email (strString, FieldName, FieldFocus){return isEmail(strString, FieldName, FieldFocus)}

function Valida_String(FieldValue, FieldName, FieldFocus) {return isString(FieldValue, FieldName, FieldFocus)}

function Count_Char(FieldValue, FieldName, FieldLen, FieldFocus) {return CountChar(FieldValue, FieldName, FieldLen, FieldFocus)}

function Verifica_Char(str) {return isValidChar(str)}

function Valida_Combo(Field, FieldName) {return isSelected(Field, FieldName)}

/*******************************************************************/

function isCPF (CPF) {
		if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
			CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
			CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
			CPF == "88888888888" || CPF == "99999999999")
			return false;
		soma = 0;
		for (i=0; i < 9; i ++)
			soma += parseInt(CPF.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)
			resto = 0;
		if (resto != parseInt(CPF.charAt(9))) {
			return false;
		}
		soma = 0;
		for (i = 0; i < 10; i ++)
			soma += parseInt(CPF.charAt(i)) * (11 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)
			resto = 0;
		if (resto != parseInt(CPF.charAt(10))) {
			return false;
		}
		return true;
	}

function isCNPJ( CNPJ )	{
		var d1 = 0;
		var d2 = 0;
		var fator = 14;

		if ((CNPJ == 11111111111111)||(CNPJ == 22222222222222)||(CNPJ == 33333333333333)||(CNPJ == 44444444444444)|| (CNPJ == 55555555555555)||(CNPJ == 66666666666666)||(CNPJ == 77777777777777)||(CNPJ == 88888888888888)|| (CNPJ == 99999999999999)||(CNPJ == 00000000000000))
		{
			return false;
		}

		for (i=12; i>0; i--)
		{
			if (i==4) 
			{
				fator=6;
			}
			d1 = d1 + (CNPJ.charAt(i-1) * (fator-i));
		} 

		d1 = d1 % 11;
		if (d1 == 0 || d1 == 1)
		{
			d1 = 0;
		}
		else
		{
			d1 = 11 - d1;
		}

		if (CNPJ.charAt(12) != d1)
		{
			return false;
		}

		fator = 15;

		for (i=13; i>0; i--)
		{
			if (i==5) 
			{
				fator=7;
			}
			d2 = d2 + (CNPJ.charAt(i-1) * (fator-i));
		} 

		d2 = d2 % 11;
		if (d2 == 0 || d2 == 1)
		{
			d2 = 0;
		}
		else
		{
			d2 = 11 - d2;
		}

		if (CNPJ.charAt(13) != d2)
		{
			return false;
		}

		return true;
	}

function isDate(Obj, FieldName, FieldFocus){
	var dn1 = Obj.substr(0,2);
	var dn2 = Obj.substr(3,2);
	var dn3 = Obj.substr(6,4);

	if((Obj.indexOf("/",0) != 2) || (Obj.indexOf("/",3) != 5) || (Obj.charAt(7) == "")) {
		alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		FieldFocus.focus();
		return false;
	}

	if((isNaN(dn1))||(isNaN(dn2))||(isNaN(dn3))) {
		alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		FieldFocus.focus();
		return false;
	}

	if ((dn1 > 31) || (dn2 > 12)) {
		alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		FieldFocus.focus();
		return false;
	}	
	
	return true;
}

function isEmail (strString, FieldName, FieldFocus){
	if (strString.length == 0 || strString.value == '') {
		alert('O campo ' + FieldName + ' não está preenchido corretamente.');
		FieldFocus.focus();
		return false;
	}
	else {
		if (strString.indexOf("@") == -1 || strString.indexOf(".") == -1) {
			alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			FieldFocus.focus();
			return false;
		}
		if (strString.length < 10) {
			alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			FieldFocus.focus();
			return false;
		}
	}
	return true;
}

function isString(FieldValue, FieldName, FieldFocus) {
  if (FieldValue == '') {
    alert('O campo ' + FieldName + ' não está preenchido corretamente.');
    FieldFocus.focus();
    return false;
  }
  return true;
}

function CountChar(FieldValue, FieldName, FieldLen, FieldFocus)
{
	if (FieldValue.length > parseInt(FieldLen))
  {
    alert('O campo ' + FieldName + ' aceita um máximo de ' + FieldLen + ' caracteres.');
	 FieldFocus.focus();
    return false;
  }
  return true;
}

function isValidChar(str) {
  var strTemp = "/\'ÃãÕõÇç][!@#$%^&*()+|<>?-=[];: ";
  var strTemp2 = '"';
  var strTemp3;
  if (str.indexOf(strTemp2) != -1)
    return false;
  for (var i = 0; i<strTemp.length; i++) {
    strTemp3 = strTemp.substr(i,1);
    if (str.indexOf(strTemp3) != -1) return false;
  }
  return true;
}

function openPopUp(theURL,winName,features)
{
	try
	{
		var WIN;
		WIN = window.open(theURL,winName,features);
		WIN.focus();
	}
	catch(e)
	{
		alert('Uma nova janela não pode ser aberta, tente novamente.\nVerifique se utiliza anti-popup ou utilize a tecla CTRL.')
	}
}

function AbrePg(theURL,winName,features){openPopUp(theURL,winName,features)}

function isSelected(Field, FieldName)
{
	if (Field.type.toLowerCase() == ("select-multiple"))
	{
		if (Field.selectedIndex == -1)
		{
			alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			Field.focus();
			return false;
		}
	}
	else
		if (Field.selectedIndex == 0)
		{
			alert('O campo ' + FieldName + ' não está preenchido corretamente.');
			Field.focus();
			return false;
		}
	return true;
}

///
/// © 2006, Fabio Tezedor
///
/// makes a key-value object record
///
/// parameters: val -> a string containing the key and its value in format key=value.
///             sep -> a character that specify the key-value separator. 
///                    it´s optional. by default the separator is '=' character.
///
///    returns: nothing
///
///      usage: var x = new KeyValue('name=Jennifer');
///
function KeyValue( val, sep )
{
	if ( typeof(sep) != "string" ) sep = "=";
	var p = val.indexOf(sep);
	this.name = val;
	this.value = "";

	if ( p > 0 )
	{
		this.name = val.substr(0,p);
		this.value = val.substr(p+1);
	}
}
///
/// © 2006, Fabio Tezedor
///
/// seek for the specified object in the whole document
///
/// parameters: object -> the oject name/id to seek.
///             type   -> the object type to seek for.
///                       it´s optional. by default any kind of objects will be considered.
///
///    returns: the object or null if not found
///
///      usage: var o = findObject( "MyObj" );
///                     findObject( "MyObj", "div" );
///
function findObject( object, type )
{
	try
	{
		var array;
		if (typeof(type)!="string")
		{
			array = document.all || document.getElementsByTagName('*');
		}
		else
		{
			array = document.getElementsByTagName(type);
		}
		for (var i=0;i<array.length;i++)
		{
			if ( array[i].id == object || array[i].name == object ) return array[i];
		}
		return null;
	}
	catch ( ex )
	{
		return null;
	}
}

///
///  © 2006, Fabio Tezedor
///
///  builds a form and its text´s fields dinamically and submits them
///
///  parameters: values -> the key-value pairs in the format key1=value1&key2=value2&key3=value3...
///                 url -> the internet address or just a page name to post the form data.
///                        it´s optional. by default the data will be sent to the sender´s page.
///              method -> the submit form method (get or post).
///                        it´s optional. by default the method is get.
///
///     returns: nothing
///
///       usage: <a href="javascript:submitIt('name=Jennifer&age=25&genre=F',null,'post')">post</a>
///
function submitIt(values,url,method)
{
	var f, e, v;
	
	if ( typeof(url) != "string" ) url = window.location.href;
	if ( typeof(method) != "string" ) method = "get";
	if ( "post|get".indexOf(method.toLowerCase()) < 0 ) method = "get";
	
	var avalues = "pi=3.14";
	if ( typeof(values)=="string" ) avalues = values.split("&");
	
	try
	{
		f = findObject("post","form");
		//if ( typeof(document.forms["post"]) != "object" )
		if ( f == null )
		{
			var o = findObject("dvFrm","div");
			if(o==null)
			{
				o = document.createElement("div");
				o.name="dvFrm";
				o.id="dvFrm";
				o.style.position="absolute";
				o.style.top="0px";
				o.style.left="0px";
				//o.style.height="50px";
				//o.style.border="1px solid";
				o.style.display="none";
				document.body.appendChild(o);
			}
			f = document.createElement("form");
			f.name = "post";
			f.id = "post";
			o.appendChild(f);
		}
		else
		{
			f = document.forms["post"];
		}

		f.action = url;
		f.method = method;

		for ( var i=0;i<avalues.length;i++ )
		{
			kv = new KeyValue( avalues[i] );
			if ( typeof(f.elements[kv.name]) != "object" )
			{
				e = document.createElement("input");
				e.name = kv.name;
				e.id = kv.name;
				e.type = "hidden";
				f.appendChild(e);
			}
			else
			{
				e = f.elements[kv.name];
			}
			e.value = kv.value;
		}
		f.submit();
	}
	catch ( ex )
	{
		window.alert( ex.message );
	}
}

function MostraNoticia(param)
{
	if(param.style.display=='')
		param.style.display='none';
	else
		param.style.display='';
}

function MostraRelease(param)
{
	if(param.style.display=='')
		param.style.display='none';
	else
		param.style.display='';
}

function MostraTR(param)
{
	if(param.style.display=='')
		param.style.display='none';
	else
		param.style.display='';
}

function Fechar(obj){obj.style.visibility = 'hidden';}

function exibeFlash(swf, width, height, flashvar, wmode, cache)
{
	noCache  = cache    || cache    == undefined ? "" : "?" + new Date();
	wmode    = wmode    || wmode    == undefined ? "transparent" : "opaque";
	flashvar =             flashvar == undefined ? "" : flashvar;
	
	monta_swf  = "";
	monta_swf += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\""+ width +"\" height=\""+ height +"\" title=\"\">";
	monta_swf += "<param name=\"movie\" value=\""+ swf + noCache +"\" />";
	monta_swf += "<param name=\"quality\" value=\"high\" />";
	monta_swf += "<param name=\"wmode\" value=\""+ wmode +"\" />";
	monta_swf += "<param name=\"flashvars\" value=\""+ flashvar +"\" />";
	monta_swf += "<embed src=\""+ swf + noCache +"\" flashvars= \""+ flashvar +"\"quality=\"high\" wmode=\""+ wmode +"\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+ width +"\" height=\""+ height +"\"></embed>";
	monta_swf += "</object>";
	
	document.write(monta_swf);
}
