// JavaScript Document

//CONSTANTES


MODULO_1 = "utr";
SERVER_URL = "http://localhost/utr/";
SERVER_CORE = "http://localhost/utr/CORE/";


IE = (document.all);

function $GEBId(id,doc) { return (doc || document).getElementById(id); }
function $GEBTn(nome,doc) { return (doc || document).getElementsByTagName(nome); }
function $DCElem(el) { return document.createElement(el); }
function $DCTNode(el) { return document.createTextNode(el); }


//FUNCOES MANIPULACAO STRING

function trimStr (str, mode) {
  if(mode == "L") return str.replace(/^\s*/, ''); // trim left
  else if(mode == "R") return str.replace(/\s*$/, ''); // trim right
  else  return str.replace(/^\s*/, '').replace(/\s*$/, ''); // trim both
}


function tiracentos(name) {
	var fordel = "������������������������������������������";
	var forsub = "aaaaaAAAAAcCeeeEEEiiiIIIoooooOOOOOuuuuUUUU";
	var newname = "";
	  for(i = 0; i < name.length; i++){
	  var nc = false;
	  var oc = name.charAt(i);
		for(j = 0; j < fordel.length; j++){
		var fd = fordel.charAt(j);
		  if(oc == fd) nc = forsub.charAt(j);
		}
	  newname += nc ? nc : oc;
	  }	 
	return newname;
}


//FUNCOES MOUSE

function getCoords(event){ //pega as coordenadas do mouse
	x=event.clientX;
	y=event.clientY;
	return x+"-"+y;
}



//mostra um loading antes do retorno	
function showLoad (element) { 
		$GEBId(element).innerHTML = "<span style='color:#ff6600;'><img src='images/ajax-loader.gif' /></span>";	
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
	}
	
	
//calcula percents
function stripBad(string) {
    for (var i=0, output='', valid="eE+/*-0123456789.()"; i<string.length; i++)
       if (valid.indexOf(string.charAt(i)) != -1)
          output += string.charAt(i)
    return output;
} 

function calc1() {
	a = stripBad(form.a.value) / 100;
	b = a * stripBad(form.b.value);
	form.total1.value = b;
}

function calc2(num, tot) {
	a = stripBad(num);
	b = stripBad(tot);
	c = a/b;
	d = c;
	return d;
}

function showhide(elemId){
	if($GEBId(elemId).className == "off") $GEBId(elemId).className = "on";
	else  $GEBId(elemId).className = "off";
}


//VALIDA EMAIL

function valEmail(mail){
	
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
//var filter2=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var ok = filter.test(mail);
	if(!ok) return false;
	else return true;
}

//VALIDA EMAIL FIM


//VALIDA CPF
function criticarCIC (numcpf){
	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	numcpf1="";
	len = numcpf.length; x = len -1;
	// var numcpf = "12345678909";
	for (var i=0; i <= len - 3; i++) {
		y = numcpf.substring(i,i+1);
		soma = soma + ( y * x);
		x = x - 1;
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ;
	if (dig1 == 11) dig1=0 ;
	numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (numcpf1.substring(i,i+1) * x);
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0;
	if (dig2 == 11) dig2=0;
	//alert ("Digito Verificador : " + dig1 + "" + dig2);
	if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
		return true;
	}
	//alert ("Numero do CPF invalido !!!");
	return false;
}

// VALIDA CPF FIM


// VALIDA CNPJ

function modulo(str) {
   	soma=0;
   	ind=2;
   	for(pos=str.length-1;pos>-1;pos=pos-1) {
   		soma = soma + (parseInt(str.charAt(pos)) * ind);
   		ind++;
   		if(str.length>11) {
   			if(ind>9) ind=2;
   		}
	}
   	resto = soma - (Math.floor(soma / 11) * 11);
   	if(resto < 2) {
    	return 0
   	}
   	else {
   		return 11 - resto
   	}
}

function VerificaCNPJ(valor) {

	primeiro=valor.substr(1,1);
	falso=true;
	size=valor.length;
	if (size!=14){
		return false;
	}
	size--;
	for (i=2; i<size-1; ++i){
		proximo=(valor.substr(i,1));
		if (primeiro!=proximo) {
			falso=false
		}
	}
	
	if (falso){
		return;
	}
	
   	if(modulo(valor.substring(0,valor.length - 2)) + "" + modulo(valor.substring(0,valor.length - 1)) !=valor.substring(valor.length - 2,valor.length)) {
   		return false;
   	}
   	return true;
}

// VALIDA CNPJ FIM


////////////////////////////////////////////////////////

function disableCtrlV(event, campo)
{
        
        
        //list all CTRL + key combinations you want to disable
        var forbiddenKeys = 'v';
        var key;
        var isCtrl;

        if(window.event) {
                key = window.event.keyCode;     //IE
                if(window.event.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        } else {
        e = event;
                key = e.which;     //firefox
                if(e.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }

        //if ctrl is pressed check if other key is in forbidenKeys array
        if(isCtrl) {    //case-insensitive comparation
				if(forbiddenKeys.toLowerCase() == String.fromCharCode(key).toLowerCase()) {
					alert('� necess�rio redigitar sua senha');
					$GEBId(campo).value = "";
					return false;
				}
        }
        return true;
}


