function ValidaEmail(email) {
	var reEmail1 = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	var reEmail2 = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	var reEmail3 = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;	

	return reEmail3.test(email);
}

function ValidaData(dt) {
	// Valida data no formato dd/mm/yyyy
	
	var reData = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;	
	
	return reData.test(dt);
}

function addOption(selectId, txt, val, selected) {
	// Adiciona objetos options em objetos select
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		
		oOption 		= document.createElement("Option");
		oOption.text 	= txt;
		oOption.value 	= val;
		//	Setting selected before adding Option to select1 demonstrates problem.
		
		if (selected) {
			oOption.selected = selected;
		}	
		document.getElementById(selectId).add(oOption);
		
	} else {
		var objOption = new Option(txt, val, selected);
		document.getElementById(selectId).options.add(objOption);		
	}
}

function Mascara(objeto, evt, mask) {
	/*
	// Mascara Input´s 
	//
	//	Ex.: <input type="text" name="edData"  onkeypress="return Mascara(this, event, '##/##/####');">
	//		 <input type="text" name="edPlacaCarro"  onkeypress="return Mascara(this, event, 'ZZZ-####');">
	//
	*/
	
	var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
	var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	var Numeros = '0123456789';
	var Fixos  = '().-:/ ';
	var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

	evt = (evt) ? evt : (window.event) ? window.event : "";
	var value = objeto.value;
	if (evt) {
		var ntecla = (evt.which) ? evt.which : evt.keyCode;
 		tecla = Charset.substr(ntecla - 32, 1);
 		if (ntecla < 32) return true;

		var tamanho = value.length;
 		if (tamanho >= mask.length) return false;

 		var pos = mask.substr(tamanho,1);
 		while (Fixos.indexOf(pos) != -1) {
  			value += pos;
  			tamanho = value.length;
  			if (tamanho >= mask.length) return false;
  			pos = mask.substr(tamanho,1);
 		}

 		switch (pos) {
   			case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
   			case 'A' : if (LetrasU.indexOf(tecla) == -1) return false; break;
   			case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
   			case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
   			case '*' : objeto.value = value; return true; break;
   			default : return false; break;
 		}
	}
	objeto.value = value;
	return true;
}


/*
// Esta função faz parte da Funcao FormataMoeda
*/
function LimparCaracteresInvalidosMoeda(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

/*
//	Formata os valores do campo no formato moeda
//  Deve ser usado no evento onKeyDown
//
//	Ex.:
//		<input type="text" name="texto" size="20" onKeydown="Formata(this,20,event,2)">
*/
function FormataMoeda(campo,tammax,teclapres,decimal) {
	var tecla = teclapres.keyCode;
	vr = LimparCaracteresInvalidosMoeda(campo.value,"0123456789");
	
	tam = vr.length;
	dec=decimal

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

		if (tecla == 8 ) { tam = tam - 1 ; }

		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tam <= dec ) { campo.value = vr ; }
			if ( (tam > dec) && (tam <= 5) ){
				campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
			}
			if ( (tam >= 6) && (tam <= 8) ) {
				campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
			}
			if ( (tam >= 9) && (tam <= 11) ){
				campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
			}
			if ( (tam >= 12) && (tam <= 14) ){
				campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
			}
			if ( (tam >= 15) && (tam <= 17) ){
				campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;
			}
	} 
}

function ConfirmaExclusao(txt, url) {
	YAHOO.namespace("example.container");
	var handleYes = function() {
		location.href=url;
		this.hide();
	};
	var handleNo = function() {
		this.hide();
	};
	YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1", 
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: true,
																			   draggable: true,
																			   close: true,
																			   modal: true,
																			   text: txt,
																			   icon: YAHOO.widget.SimpleDialog.ICON_HELP,
																			   constraintoviewport: true,
																			   buttons: [ { text:"Sim", handler:handleYes, isDefault:true },
																						  { text:"Não",  handler:handleNo } ],
																			   effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}
																			 } );
	
	YAHOO.example.container.simpledialog1.setHeader("Excluir registro?");
	
	YAHOO.example.container.simpledialog1.render(document.body);
	YAHOO.example.container.simpledialog1.show();
}

function ShowMsg(titulo,txt) {
	YAHOO.namespace("example.container");
	var handleYes = function() {
		this.hide();
	};

	YAHOO.example.container.simpledialog2 = new YAHOO.widget.SimpleDialog("simpledialog2", 
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: true,
																			   draggable: true,
																			   close: true,
																			   modal: true,
																			   text: txt,
																			   icon: YAHOO.widget.SimpleDialog.ICON_HELP,
																			   constraintoviewport: true,
																			   buttons: [ { text:"Ok", handler:handleYes, isDefault:true }],
																			   effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}
																			 } );
	
	YAHOO.example.container.simpledialog2.setHeader(titulo);
	
	YAHOO.example.container.simpledialog2.render(document.body);
	YAHOO.example.container.simpledialog2.show();
}

function MostraTela(titulo) {
	YAHOO.namespace("example.container");
	// Initialize the temporary Panel to display while waiting for external content to load
	YAHOO.example.container.wait = 
			new YAHOO.widget.Panel("wait",  
											{ width:"240px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);
	YAHOO.example.container.wait.setHeader(titulo);
	YAHOO.example.container.wait.setBody('<img src="http://www.casamoveis.com/site/imagens/loading/loading.gif" />');
	YAHOO.example.container.wait.render(document.body);		
	YAHOO.example.container.wait.show();	
}

function EscondeTela() {
	YAHOO.example.container.wait.hide();
}

function isCPF(st) {
	if (st == "")
	return (false);
	l = st.length;

	//aleterado para se usuário não digitar os zeros na frente do CPF, completar sozinho
	if ((l == 9) || (l == 8)) {
		for (i = l ; i < 10; i++) {
			st = '0' + st
		}
	}
	l = st.length;
	st2 = "";
	for (i = 0; i < l; i++) {
		caracter = st.substring(i,i+1);
		if ((caracter >= '0') && (caracter <= '9'));
		st2 = st2 + caracter;
	}
	if ((st2.length > 11) || (st2.length < 10)) return (false);
	
	if (st2.length==10) st2 = '0' + st2;
	
	digito1 = st2.substring(9,10);
	digito2 = st2.substring(10,11);
	digito1 = parseInt(digito1,10);
	digito2 = parseInt(digito2,10);
	sum = 0; mul = 10;
	
	for (i = 0; i < 9 ; i++) {
		digit = st2.substring(i,i+1);
		tproduct = parseInt(digit ,10) * mul;
		sum += tproduct;
		mul--;
	}

	dig1 = ( sum % 11 );
	if ( dig1==0 || dig1==1 ) dig1=0;
	else dig1 = 11 - dig1;

	if (dig1!=digito1) return (false);
	
	sum = 0;
	mul = 11;
	
	for (i = 0; i < 10 ; i++) {
		digit = st2.substring(i,i+1);
		tproduct = parseInt(digit ,10)*mul;
		sum += tproduct;
		mul--;
	}

	dig2 = (sum % 11);
	if ( dig2==0 || dig2==1 ) dig2=0;
	else dig2 = 11 - dig2;

	if (dig2 != digito2) return (false);
	
	return (true);
}


function isCNPJ(s) {
	var i;

	var c = s.substr(0,12);
	
	var dv = s.substr(12,2);
	
	var d1 = 0;

	for (i = 0; i < 12; i++) {
		d1 += c.charAt(11-i)*(2+(i % 8));
	}

	if (d1 == 0) return false;

	d1 = 11 - (d1 % 11);

	if (d1 > 9) d1 = 0;

	if (dv.charAt(0) != d1) {
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 12; i++) {
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}

	d1 = 11 - (d1 % 11);

	if (d1 > 9) d1 = 0;

	if (dv.charAt(1) != d1) {
		return false;
	}

    return true;
}

function SoNumeros(tecla) {
	// alert(tecla.keyCode);
	
	if (tecla.keyCode == 8) return true;
	if (tecla.keyCode == 9) return true; 	// TAB
	if (tecla.keyCode == 13) return true;	// Enter
	if (tecla.keyCode == 46) return true;		
	if (tecla.keyCode == 16) return true;	// SHIFT
	if (tecla.keyCode == 17) return true;	// CTRL
	if (tecla.keyCode == 37) return true;	// Seta Esquerda
	if (tecla.keyCode == 38) return true;	// Seta Cima
	if (tecla.keyCode == 39) return true;	// Seta Direita
	if (tecla.keyCode == 40) return true;	// Seta Baixo
	if (tecla.keyCode == 35) return true;	// END
	if (tecla.keyCode == 36) return true;	// Home
			
	if ((tecla.keyCode >= 48) && (tecla.keyCode <= 57)) { return true; }
	
	if ((tecla.keyCode >= 96) && (tecla.keyCode <= 105)) { return true; }
	
	return false;
}

function abrir(pagina, largura, altura)
	{
	newWindow=window.open(pagina,"nova","width="+largura+",height="+altura)
	if(newWindow)
		{
		newWindow.focus();
		}
	else
		{
		alert("Verifique o seu bloqueador de popup´s.\nA foto não pode ser aberta.");
		}
	}
