/****************************************************************
*	Copyright 2004 - E-Guru - Todos os direitos reservados.		*
*	Última Modificação:	11/02/2004								*
****************************************************************/


/*******************************************************************************************
* function verCampoOriginal
* Verificação básica de um campo de formulário por caracteres inválidos: " & < > | \ / '
* Parâmetro: campo a ser verificado
*******************************************************************************************/

function verCampoOriginal(campo) {
	if ((campo.indexOf("'") >= 0) || (campo.indexOf("<") >= 0) || (campo.indexOf("&") >= 0) || (campo.indexOf(">") >= 0) || 
		(campo.indexOf("|") >= 0) || (campo.indexOf("/") >= 0) || (campo.indexOf("=") >= 0) || (campo.indexOf("!") >= 0) || 
		(campo.indexOf("*") >= 0)){
		return false;
		}
	return true;
	}

/******************************************************************************************/



/*******************************************************************************************
* function verCampo
* Verificação básica de um campo de formulário por caracteres inválidos: " & < > | \ / '
* Parâmetro: campo a ser verificado
*******************************************************************************************/

function verCampo(campo) {
	if ((campo.indexOf("'") >= 0) || (campo.indexOf('"') >= 0)){
		return false;
		}
	return true;
	}

/******************************************************************************************/



/*******************************************************************************************
* function verData
* Verificação se o valor digitado em um campo é uma data válida.
* Parâmetro: campo a ser verificado
*******************************************************************************************/

function verData(campo) {
	var dia = 0;
	var mes = 0;

	if (campo.charAt(1) == '8') dia = 8;
	else if (campo.charAt(1) == '9') dia = 9;
	else dia = parseInt(campo.substring(0, 2));

	if (campo.charAt(4) == '8') mes = 8;
	else if (campo.charAt(4) == '9') mes = 9;
	else mes = parseInt(campo.substring(3, 5));

	var ano = parseInt(campo.substring(6, 10));
	
	if ((dia < 1) || (dia > 31) || (mes < 1) || (mes > 12) || (ano < 1000)) return false;

	if ((campo.charAt(2) != "/") || (campo.charAt(5) != "/")) return false;

	return true;
	}

/******************************************************************************************/



/*******************************************************************************************
* function IE
* Verificação da Inscrição Estadual.
* Parâmetro: campo a ser verificado
*******************************************************************************************/

function IE(campo){
	if (!numero(campo.substring(0, 3))) return false;

	if (campo.charAt(3) != '.') return false;

	if (!numero(campo.substring(4, 7))) return false;

	if (campo.charAt(7) != '.') return false;

	if (!numero(campo.substring(8, 11))) return false;

	if (campo.charAt(11) != '.') return false;

	if (!numero(campo.substring(12, 15))) return false;

	return true;
	}

/******************************************************************************************/



/*******************************************************************************************
* function verDataLimite
* Verificação se o valor digitado é maior que a data mínima final de conclusão da turma.
* Parâmetro: campo a ser verificado e data mínima final
*******************************************************************************************/

function verDataLimite(campo, data) {
	var data_campo = new Date(campo);
	var data_minima = new Date(data);

	if (data_campo < data_minima) return false;

	return true;
	}

/******************************************************************************************/




/*******************************************************************************************
* function verTamanho
* Verifica se o tamanho está dentro do esperado
* Parâmetro: campo a ser medida e tamanho máximo permitido
*******************************************************************************************/

function verTamanho(campo, tamanho) {
	if (campo.length > tamanho) return false;
	return true;
	}

/******************************************************************************************/



/*******************************************************************************************
* function alerta										
* Mostra um alert para o usuário e volta o foco para
* o campo que está com problema
* Parâmetro(s): campo - campo do formulário com problema
*        	  	texto - texto a ser mostrado no alert
*******************************************************************************************/

function alerta(campo, texto){  
	campo.focus();
	alert(texto);
	return false;
	}

/******************************************************************************************/



/*******************************************************************************************
* function vazio
* Verifica se um campo está vazio
* Parâmetro(s): campo a ser verificado
*******************************************************************************************/

function vazio(campo) {
	return ((campo == null) || (campo.length == 0));
	}

/******************************************************************************************/



/*******************************************************************************************
* function digito
* Verifica se o caracter é um dígito de 0 a 9
* Parâmetro: caracter a ser verificado
*******************************************************************************************/

function digito (caracter){
	return ((caracter >= "0") && (caracter <= "9"));
	}

/******************************************************************************************/



/*******************************************************************************************
* function numero
* Verifica se um campo é numérico, ou seja, se contém apenas dígitos de 0 a 9
* Parâmetro: campo a ser verificado
*******************************************************************************************/

function numero(campo){
	var i;
	if (vazio(campo)) return false;
	for (i = 0; i < campo.length; i++){
		var caracter = campo.charAt(i);
		if (!digito(caracter)) return false;
		}
	return true;
	}

/******************************************************************************************/




/*******************************************************************************************
* function vogalAcentuada(s)
* Verifica se o email possui vogais acentuadas
* Parâmetro: string contendo o email
*******************************************************************************************/

function vogalAcentuada(s) {
	var ls = s.toLowerCase();
	if ((ls.indexOf("á") >= 0) || (ls.indexOf("à") >= 0) || (ls.indexOf("ã") >= 0) || (ls.indexOf("â") >= 0) ||
		(ls.indexOf("é") >= 0) || (ls.indexOf("í") >= 0) || (ls.indexOf("ó") >= 0) || (ls.indexOf("õ") >= 0) || 
		(ls.indexOf("ô") >= 0) || (ls.indexOf("ú") >= 0) || (ls.indexOf("ü") >= 0)){
		return true;
		}
	return false;
	}

/******************************************************************************************/




/*******************************************************************************************
* function geraStringAsc
* Gera uma string com os caracteres básicos na sequência de códigos ASC
*******************************************************************************************/

function geraStringAsc(){
	var astr;
	astr = ' !"#$%&\'()*+,-./0123456789:;<=>?@';
	astr+= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	astr+= '[\]^_`abcdefghijklmnopqrstuvwxyz';
	astr+= '{|}~';
	return astr;
	}

/******************************************************************************************/



/*******************************************************************************************
* function asc
* Retorna o código ASC de um caracter
* Parâmetro: caracter a ser convertido
*******************************************************************************************/

function asc(achar){
	var n = 0;
	var ascstr = geraStringAsc();
	for(i = 0; i < ascstr.length; i++){
		if(achar == ascstr.substring(i, i + 1)){
			n = i;
			break;
			}
		}
	return n + 32;
	}

/******************************************************************************************/



/*******************************************************************************************
* function trim
* Remove todos os caracteres excetos 0-9
* Parâmetro: string a ser verificada
*******************************************************************************************/

function trim(tstring){
	s = ""; 
	ts = new String(tstring);
	for (x=0; x < ts.length; x++){
		ch = ts.charAt(x);
		if (asc(ch) >= 48 && asc(ch) <= 57){
			s = s + ch;
			}
		}
	return s;
	}

/******************************************************************************************/



/*******************************************************************************************
* function verificaEmail
* Verifica se um email é válido
* Parâmetro: email a ser verificado
*******************************************************************************************/

function verificaEmail(email) {
	var s = new String(email);

	if ((email != "") && (email != null)){

		// { } ( ) < > [ ] | \ /
		if ((s.indexOf("{") >= 0) || (s.indexOf("}") >= 0) || (s.indexOf("(") >= 0) || (s.indexOf(")") >= 0) || (s.indexOf("<") >= 0) || 
			(s.indexOf(">") >= 0) || (s.indexOf("[") >= 0) || (s.indexOf("]") >= 0) || (s.indexOf("|") >= 0) || (s.indexOf("\"") >= 0) || 
			(s.indexOf("/") >= 0))
			return false;

		if (vogalAcentuada(email)) return false;

		// & * $ % ? ! ^ ~ ` ' "
		if ((s.indexOf("&") >= 0) || (s.indexOf("*") >= 0) || (s.indexOf("$") >= 0) || (s.indexOf("%") >= 0) || (s.indexOf("?") >= 0) || 
			(s.indexOf("!") >= 0) || (s.indexOf("^") >= 0) || (s.indexOf("~") >= 0) || (s.indexOf("`") >= 0) || (s.indexOf("'") >= 0))
			return false;

		// , ; : = #
		if ((s.indexOf(",") >= 0) || (s.indexOf(";") >= 0) || (s.indexOf(":") >= 0) || (s.indexOf("=") >= 0) || (s.indexOf("#") >= 0))
			return false;

		// procura se existe apenas um @
		if ((s.indexOf("@") < 0) || (s.indexOf("@") != s.lastIndexOf("@")))
			return false;

		// verifica se tem pelo menos um ponto após o @
		if (s.lastIndexOf(".") < s.indexOf("@"))
			return false;
		}

	return true;
	}

/******************************************************************************************/



/*******************************************************************************************
* function TESTA
* Verifica se um CPF ou CNPJ é válido
* Parâmetros: Número e tipo a serem verificados.
*******************************************************************************************/

function TESTA(CAMPO, CNUMB, CTYPE){
	if(Verify(CNUMB, CTYPE)){
		if (CTYPE == "CPF"){
			//CAMPO.value = "Mário";
			}
		else if(CTYPE == "CNPJ"){
			}
		return true;
		}
	else{
		return false;
		}
	return true;
	}

function ClearStr(str, char){
	while((cx = str.indexOf(char)) != -1){		
		str = str.substring(0, cx) + str.substring(cx + 1);
		}
	return(str);
	}

function ParseNumb(c){
	c = ClearStr(c, '-');
	c = ClearStr(c, '/');
	c = ClearStr(c, ',');
	c = ClearStr(c, '.');
	c = ClearStr(c, '(');
	c = ClearStr(c, ')');
	c = ClearStr(c, ' ');
	if((parseFloat(c) / c != 1)){
		if(parseFloat(c) * c == 0){
			return(c);
			}
		else{
			return(0);
			}
		}
	else{
		return(c);
		}
	}

function Verify(CNUMB, CTYPE){
	CNUMB = ParseNumb(CNUMB)
	if(CNUMB == 0){
		return(false);
		}
	else{
		g = CNUMB.length - 2;
		if(TestDigit(CNUMB, CTYPE, g)){
			g = CNUMB.length - 1;
			if(TestDigit(CNUMB, CTYPE, g)){	
				return(true);
				}
			else{
				return(false);
				}
			}
		else{
			return(false);
			}
		}
	}

function TestDigit(CNUMB, CTYPE, g){
	var dig = 0;
	var ind = 2;
	for(f = g; f > 0; f--){
		dig += parseInt(CNUMB.charAt(f - 1)) * ind;
		if (CTYPE == 'CNPJ'){
			if(ind > 8){
				ind = 2;
				}
			else{
				ind++;
				}
			}
		else{
			ind++;
			}
		}
	dig %= 11;
	if(dig < 2){
		dig = 0;
		}
	else{
		dig = 11 - dig;
		}
	if(dig != parseInt(CNUMB.charAt(g))){
		return(false);
		}
	else{
		return(true);
		}
	}


/******************************************************************************************/


function setTelaCheia(){
	var largura = screen.width;
	var altura = screen.height - 30;


	try{
		this.resizeTo(largura, altura);
		this.moveTo(0, 0);
		}
	catch(e){}
	}


function set800600(){
	var largura = 800;
	var altura = 580;


	try{
		this.resizeTo(largura, altura);
		this.moveTo((screen.width - largura) / 2, (screen.height - (altura + 30)) / 2);
		}
	catch(e){}
	}


function set400180(){
	var largura = 400;
	var altura = 180;


	try{
		this.resizeTo(largura, altura);
		this.moveTo((screen.width - largura) / 2, (screen.height - (altura + 30)) / 2);
		}
	catch(e){}
	}


function set400500(){
	var largura = 400;
	var altura = 500;


	try{
		this.resizeTo(largura, altura);
		this.moveTo((screen.width - largura) / 2, (screen.height - (altura + 30)) / 2);
		}
	catch(e){}
	}
	
	
function set350480(){
	var largura = 350;
	var altura = 480;


	try{
		this.resizeTo(largura, altura);
		this.moveTo((screen.width - largura) / 2, (screen.height - (altura + 30)) / 2);
		}
	catch(e){}
	}



/******************************************************************************************************/
function delineate(str){
	theleft = str.indexOf('=') + 1;
	theright = str.lastIndexOf('&');
	return(str.substring(theleft, theright));
	}


function printFlash(var_swf, var_largura, var_altura, var_teste, var_transparente){
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id='" + var_swf + "' width='" + var_largura + "' height='" + var_altura + "'");
	document.write("codebase='http://active.macromedia.com/flash4/cabs/swflash.cab#version=5,0,0,0'>");
	document.write("<param name='SRC' value='" + var_swf + ".swf" + "'");
	document.write("<param name='menu' value='false'>");
	if (var_transparente) document.write("<param name='wmode' value='transparent'>");


	if (var_transparente) document.write("<param name='bgcolor' VALUE='FFFFFF'>");
	else document.write("<param name='bgcolor' VALUE='#F0F0F0'>");
	document.write("<param name=scale value='exactfit'> ");


	document.write("<param name='QUALITY' value='high'>");
	document.write("<param name='flashvars' value='teste=" + var_teste + "'>");

	document.write("   <embed menu='false' ");
	document.write("src='" + var_swf + ".swf" + "' ");
	if (var_transparente) document.write("id='" + var_swf + "' name='" + var_swf + "' width='" + var_largura + "' height='" + var_altura + "' quality='high' loop='false' bgcolor='FFFFFF' ");
	else document.write("id='" + var_swf + "' name='" + var_swf + "' width='" + var_largura + "' height='" + var_altura + "' quality='high' loop='false' bgcolor='#F0F0F0' ");
	document.write("pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' ");
	document.write("embedFlashVar = 'teste=" + var_teste + "'");
	document.write("scale='exactfit' ");
	if (var_transparente) document.write("wmode='transparent'");
	document.write(">    </embed> </object>");
	}


function pop(url, Loc, tool, res, stat, w, h, l, t, s){
	var aw = screen.availWidth;
	var windowX = (aw / 2) - (w / 2);
	var cwPar =  'location=' +Loc+ ', toolbar=' +tool+ ', resizable=' +res+ ', status=' +stat+ ', width=' +w+ ', height=' +h+ ', left=' +windowX+ ', top=' +t+ ', scrollbars=' +s;
	window.open( url , 'remote2', cwPar ); 
	}


function bookmarkUs(who){ 
	var url = window.location; 
	var ver = navigator.appName; 
	var num = parseInt(navigator.appVersion); 
	if ((ver == 'Microsoft Internet Explorer')&&(num >= 4)) {
		window.external.AddFavorite(url,who); 
		}
	else{
		alert('Netscape press Ctrl+D keys.');
		}
	}
	

function rfPrint(){
	window.open('text.htm');
	}
/*******************************************************************************************************/

String.PAD_LEFT  = 0;
String.PAD_RIGHT = 1;
String.PAD_BOTH  = 2;

String.prototype.pad = function(size, pad, side) {
	var str = this, append = "", size = (size - str.length);
	var pad = ((pad != null) ? pad : " ");

	if ((typeof size != "number") || ((typeof pad != "string") || (pad == ""))) {
		throw new Error("Wrong parameters for String.pad() method.");
		}

	if (side == String.PAD_BOTH) {
		str = str.pad((Math.floor(size / 2) + str.length), pad, String.PAD_LEFT);
		return str.pad((Math.ceil(size / 2) + str.length), pad, String.PAD_RIGHT);
		}

	while ((size -= pad.length) > 0) {
		append += pad;
		}

	append += pad.substr(0, (size + pad.length));
	return ((side == String.PAD_LEFT) ? append.concat(str) : str.concat(append));
	}

Number.prototype.format = function(d_len, d_pt, t_pt) {
	var d_len = d_len || 0;
	var d_pt = d_pt || ".";
	var t_pt = t_pt || ",";

	if ((typeof d_len != "number") || (typeof d_pt != "string") || (typeof t_pt != "string")) {
		throw new Error("wrong parameters for method 'String.pad()'.");
		}

	var integer = "", decimal = "";
	var n = new String(this).split(/\./), i_len = n[0].length, i = 0;

	if (d_len > 0) {
		n[1] = (typeof n[1] != "undefined") ? n[1].substr(0, d_len) : "";
		decimal = d_pt.concat(n[1].pad(d_len, "0", String.PAD_RIGHT));
		}

	while (i_len > 0) {
		if ((++i % 3 == 1) && (i_len != n[0].length)) {
			integer = t_pt.concat(integer);
			}
		integer = n[0].substr(--i_len, 1).concat(integer);
		}

	return (integer + decimal);
	}
/*******************************************************************************************************************************/
