/**
 * --------------------------------------------------------------------
 * Scripts Javascritp básicos
 * Version: 1, 28.07.2008
 * Por Jorge Vergara, jvergara@trabajando.com
 *
 * [FOR] Respositorio de todos los script de trabajando pre 2008 que antes estaban en cualquier parte del documento.
 * La idea es que estén 1) todos juntos, 2) documentados, así podremos saber si son o no necesarios.
 * MODIFICACIONES:
 *	- 13/05/2009 JGR Se añade funciones para Brasil
 * --------------------------------------------------------------------
 */
function ventana(archivo,nombre,ancho,alto){
	LeftPosition=(screen.width)?(screen.width-ancho)/90:100;
	TopPosition=(screen.height)?(screen.height-alto)/90:100;
	trabaja = window.open(archivo,nombre,"left="+LeftPosition+",top="+TopPosition+",height="+alto+",width="+ancho+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,maximized");
}

function ventana2(archivo,nombre,ancho,alto) {
/**
 * Abre una ventana nueva, en el vínculo se sugiere usar:
 * href="javascript:ventana('verpostulanteb.cfm','verpostulante',600,500)
 **/
	LeftPosition=(screen.width)?(screen.width-ancho)/2:100;
	TopPosition=(screen.height)?(screen.height-alto)/2:100;
	anexosayuda = window.open(archivo,nombre,"left="+LeftPosition+",top="+TopPosition+",height="+alto+",width="+ancho+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,maximized");
}
function ventana3(archivo,nombre,ancho,alto){
	LeftPosition=(screen.width)?(screen.width-ancho)/2:100;
	TopPosition=(screen.height)?(screen.height-alto)/2:100;
	anexosayuda = window.open(archivo,nombre,"left="+LeftPosition+",top="+TopPosition+",height="+alto+",width="+ancho+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,maximized");
}

function mOvr(src,clrOver) {
	if (typeof event != "undefined" ) { // Se hace esta validación para evitar errores en FireFox
		if (!src.contains(event.fromElement)) {
			src.style.cursor = 'hand';
			src.bgColor = clrOver;
		}
	}
}
					  
function mOut(src,clrIn) {
	if (typeof event != "undefined") { // Se hace esta validación para evitar errores en FireFox
		if (!src.contains(event.toElement)) {
			src.style.cursor = 'default';
			src.bgColor = clrIn;
		}
	}
}

// FUNCIONES COPIADAS DE LA PAGINA "" PARA HACER LA VALIDACION DEL CPF
function isDigit (c){
	return ((c >= "0") && (c <= "9"))
} 

function TestaCPF(strCPF) {
    var Soma;
    var Resto;
	var strCPFtemp = '';
    Soma = 0;
	//Primero quitamos los caracteres que no sean numericos (como '.' y '-')
    for (i = 1; i <= strCPF.length; i++) {
		if ( isDigit( strCPF.substring(i - 1, i) ) ) { 
        	strCPFtemp = strCPFtemp + strCPF.substring(i - 1, i);
	   }
    }
	strCPF = strCPFtemp;
    if (strCPF == "00000000000") {
        return false;
    }
    for (i = 1; i <= 9; i++) {
        Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (11 - i);
    }
    Resto = Soma * 10 % 11;
    if (Resto == 10 || Resto == 11) {
        Resto = 0;
    }
    if (Resto != parseInt(strCPF.substring(9, 10))) {
        return false;
    }
    Soma = 0;
    for (i = 1; i <= 10; i++) {
        Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (12 - i);
    }
    Resto = Soma * 10 % 11;
    if (Resto == 10 || Resto == 11) {
        Resto = 0;
    }
    if (Resto != parseInt(strCPF.substring(10, 11))) {
        return false;
    }
    return true;
}


function validaCNPJ(CNPJ) {
	//CNPJ = document.validacao.CNPJID.value;
	// \u00C9 = É; \u00FA = ú
	
	erro = new String;
	if (CNPJ.length < 18) erro += "\u00C9 necessarios preencher corretamente o n\u00FAmero do CNPJ! \n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "E' necessarios preencher corretamente o n\u00FAmero do CNPJ! \n\n";
	}
	//substituir os caracteres que nao sao numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
		x = CNPJ.substring(0,2);
		x += CNPJ.substring(3,6);
		x += CNPJ.substring(7,10);
		x += CNPJ.substring(11,15);
		x += CNPJ.substring(16,18);
		CNPJ = x;	
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas n\u00FAmeros! \n\n";	
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]); 
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		erro +="Digito verificador com problema!";
	}
	if (erro.length > 0){
		alert(erro);
		return false;
	} else {
		//alert("CNPJ valido!");
	}
	return true;
}


function IsNumeric(valor) 
{ 
var log=valor.length; var sw="S"; 
for (x=0; x<log; x++) 
{ v1=valor.substr(x,1); 
v2 = parseInt(v1); 
//Compruebo si es un valor numérico 
if (isNaN(v2)) { sw= "N";} 
} 
if (sw=="S") {return true;} else {return false; } 
} 

var primerslap=false; 
var segundoslap=false; 

function formatocep(cep)
{
		var	largo=cep.length;
		var cep; 

	if(largo<=5){
if ( IsNumeric(cep) ) {
			if(largo==5)
				return cep +'-';
			else 
				return cep ;
		} else 
			return ;
	}
	else {
		tempCep = substr(6, cep.length-6);
		if ( IsNumeric(tempCep) ) {
			return cep;
		} else
			cep.substr(0, cep.length-1);
			}
}

function formatocnpj(cnpj)
{
	var	largo = cnpj.length;
	var cnpj; 
	
	// Se evalua 12.xxx.xxx/xxxx-xx
	if(largo<=2){
		if ( IsNumeric(cnpj) ) {
			if(largo==2) {
				return cnpj +'.';
			}
			else {
				return cnpj ;
			}
		} else 
			return cnpj.substr(0, cnpj.length-1);
	}
	// Se evalua xx.456.xxx/xxxx-xx
	if ((largo>=4) && (largo<=6) ){
		tempCep = cnpj.substr(4, cnpj.length-3);
		if ( IsNumeric(tempCep) ) {
			if(largo==6){
				return cnpj +'.';
			} else 
				return cnpj ;
		} else 
			return cnpj.substr(0, cnpj.length-1);
	}
	// Se evalua xx.xxx.89(10)/xxxx-xx
	if ((largo>=8) && (largo<=10) ){
		tempCep = cnpj.substr(8, cnpj.length-7);
		if ( IsNumeric(tempCep) ) {
			if(largo==10) {
				return cnpj +'/';
			} else 
				return cnpj ;
		} else 
			return cnpj.substr(0, cnpj.length-1);
	}
	// Se evalua xx.xxx.xxx/(12)(13)(14)(15)-xx
	if ((largo>=12) && (largo<=15) ){
		tempCep = cnpj.substr(12, cnpj.length-11);
		if ( IsNumeric(tempCep) ) {
			if(largo==15) {
				return cnpj +'-';
			} else 
				return cnpj ;
		} else 
			return cnpj.substr(0, cnpj.length-1);
	}
	// Se evalua xx.xxx.xxx/xxxx-(17)(18)
	if ((largo>=17) && (largo<=18) ){
		tempCep = cnpj.substr(17, cnpj.length-16);
		if ( IsNumeric(tempCep) ) {
			return cnpj ;
		} else 
			return cnpj.substr(0, cnpj.length-1);
	}
return cnpj ;
}


function formatofono(fono)
{

		var	largo=fono.length;
		var fono;
	
		// Primer caracter
		if (largo<=1){ 
			if (IsNumeric(fono) ) {
				if(largo==1)
					return '(' + fono ;
				else 
					return fono ;
			} 
		}
		
		// Ingresa segundo caracter, añadimos el cierre de parentesis
		if (largo==3) { 
			return fono + ')' ;
		} 

		
		// Validar los caracteres del 5 al 8 que sean numeros
		if (largo==8){ 
			 return fono + '-' ;
		} 
		else {
				return fono;
			}
		
		
		return fono;
}

function formateafecha(fecha) 
{ 
var long = fecha.length; 
var dia; 
var mes; 
var ano; 

if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
else { fecha=""; primerslap=false;} 
} 
else 
{ dia=fecha.substr(0,1); 
if (IsNumeric(dia)==false) 
{fecha="";} 
if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
} 
if ((long>=5) && (segundoslap==false)) 
{ mes=fecha.substr(3,2); 
if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
else { fecha=fecha.substr(0,3);; segundoslap=false;} 
} 
else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
if (long>=7) 
{ ano=fecha.substr(6,4); 
if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } } } 
} 

if (long>=10) 
{ 
fecha=fecha.substr(0,10); 
dia=fecha.substr(0,2); 
mes=fecha.substr(3,2); 
ano=fecha.substr(6,4); 
// Año no viciesto y es febrero y el dia es mayor a 28 
if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
} 
return (fecha); 
}

//VALIDACION DE FECHA 
var formato = 1; 

/*
Indicar el nombre del formulario, nombre del combobox, el valor a ser asignado (1-marcados; 0-desmardados)
*/
function fg_SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

function formato(obj,ver)
	{

	 if (ver == 1){//chile
		
		largoTotal = 12;
	 }
	 else{//brasil
		
		largoTotal = 14;
		 
	 }
	 cadena=obj.value;
	 cuenta=0;
	 var tmpstr = "";
	  for ( i=0; i < largoTotal ; i++ )
		if ( cadena.charAt(i) != ' ' && cadena.charAt(i) != '.' && cadena.charAt(i) != '-' )
	 	{
		  	tmpstr = tmpstr + cadena.charAt(i);
	   		character=cadena.charAt(i);
	   		codi=character.charCodeAt(0);
	   	if (codi>=48 && codi<=57)
			cuenta++;
	   	}
	   		largo=tmpstr.length;
	   	if (largo>ver)
	   	{	 
	   		if ((ver==2 && cuenta!=largo) || (ver==1 && cuenta+1<largo))
	   		{
	  			alert('Formato erroneo');
	  			obj.focus();
	   		}
	   		else
	   		{	
				sw=largo-ver; 
				cambiado="";
	   			for (i=0;i<sw;i++){
					cambiado=cambiado + tmpstr.charAt(i);
					if (i+1!=sw && ((sw-i)%3==1))
						cambiado=cambiado + '.';
	   			}
	   		if (ver==1)
			
				cambiado=cambiado+'-'+tmpstr.charAt(largo-1);
				
			
	   		else
				cambiado=cambiado+'-'+tmpstr.charAt(largo-2)+tmpstr.charAt(largo-1);
	   			obj.value=cambiado; 
	   		}
	  	}
	 }

	function isDigit (c){
	return ((c >= "0") && (c <= "9"))
	}
