// JavaScript Document


//CONSTANTES
//SERVER_URL = "HTTP://";

MODULO_1 = "fonte";
MODULO_2 = "estoque";
MODULO_4 = "financeiro";
SERVER_URL = "http://www.crechecrescer.com.br/";
SERVER_CORE = "http://www.crechecrescer.com.br/";

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";
}

var lastElemId = "insertForm_1";
function showHideCloseAll(elemId){
	
	if($GEBId(elemId).className == "allinserts off"){
		$GEBId(elemId).className = "allinserts on";
		$GEBId(lastElemId).className = "allinserts off";
		lastElemId = elemId;
		
	}
	//else  $GEBId(elemId).className = "off";
}

//////////////////////////ESQUCEU SENHA////////////////////////////////////////


function showhidepass(elemId){

	$GEBId("answerretpass").innerHTML = "";	
	if($GEBId(elemId).className == "off") $GEBId(elemId).className = "on";
	else  $GEBId(elemId).className = "off";
}


function retPassMail(oRet){
	//alert(oRet);		
		if(oRet == 1) var resp ="Nova senha enviada por e-mail";
		else if(oRet == 2) var resp ="Entre com seu e-mail";
		else  var resp ="E-mail não cadastrado";
		$GEBId('answerretpass').innerHTML = resp;
		$GEBId('answerretpass').style.visibility = "visible";
		$GEBId('answerretpass').className = "on";
}

//////////////////////////ALTERAR SENHA////////////////////////////////////////
function retChgPass(oRet){
	//alert(oRet);
	$GEBId('senha').value = "";
	$GEBId('newsenha1').value = "";
	$GEBId('newsenha2').value = "";
	//$GEBId('chgsenha').className = "off";
	if(oRet == 1) var resp ="<span style='color: #FF0000;'>Senha alterada com sucesso</span>";
	else if(oRet == 2) var resp ="<span style='color: #FF0000;'>Todos os campos s&atilde;o obrigat&oacute;rios</span>";
	else if(oRet == 5) var resp ="<span style='color: #FF0000;'>As senhas novas n&atilde;o conferem</span>";
	else  var resp ="Dados incorretos";
	$GEBId('answerchgpass').innerHTML = resp;
	$GEBId('answerchgpass').className = "on";
}


//cria a lista de parametros do form e poe no hidden params 
function createparams(form){ 
 
var str = ""; 
var tags = $GEBTn('input',form); 
var slect = $GEBTn('select',form); 
 
	for(d=0;d < tags.length;d++){ 
	if(tags[d].type == 'text' || tags[d].type == 'password') str+=tags[d].id+"="+tags[d].value+"&";  
	} 
	 
	for(d=0;d < slect.length;d++){						 
		str+=slect[d].id+"="+slect[d].value+"&";  
		 
	} 
	 
	str = str.replace(/\&$/,"");		 
	 
form.params.value = str;
 
} 
 
 
function foclog(){ 
	document.frlogin.login.focus(); 
} 


function getKey(event) {	
	
	if(document.all){ 
			var e = window.event;
			var code = e.keyCode;
		} else{ 
			e = event;	 
			var code = e.keyCode; 
		}
		
		return code;
} 
 
function onKeyDown(event) {
	
		if(getKey(event) == 13)	$GEBId("enviar").click();
} 



function moneyBR(oStot){
	//alert(oStot);
		var temp = oStot.split(".");
		var fnum = "";
		if(oStot.length > 6){
		//r = 0;	
			
			if((temp[0].length) == 4){
				
				for(d=0; d < (temp[0].length); d++){
				
					if(d < 1) {
						fnum += temp[0].charAt(d)+".";
						//r++;
					}else{
						fnum += temp[0].charAt(d);
						
					}
				
				}	
				
			}else if((temp[0].length) == 5){
				
				for(d=0; d < (temp[0].length); d++){
				
					if(d == 1) {
						fnum += temp[0].charAt(d)+".";
						//r++;
					}else{
						fnum += temp[0].charAt(d);
						
					}
				
				}	
			
			}else if((temp[0].length) == 6){
				
				for(d=0; d < (temp[0].length); d++){
				
					if(d == 2) {
						fnum += temp[0].charAt(d)+".";
						//r++;
					}else{
						fnum += temp[0].charAt(d);
						
					}
				
				}	
			
			}else if((temp[0].length) == 7){
				
				for(d=0; d < (temp[0].length); d++){
				
					if(d == 0 || d == 3) {
						fnum += temp[0].charAt(d)+".";
						//r++;
					}else{
						fnum += temp[0].charAt(d);
						
					}
				
				}	
			
			}else if((temp[0].length) == 8){
				
				for(d=0; d < (temp[0].length); d++){
				
					if(d == 1 || d == 4) {
						fnum += temp[0].charAt(d)+".";
						//r++;
					}else{
						fnum += temp[0].charAt(d);
						
					}
				
				}	
			
			}else if((temp[0].length) == 9){
				
				for(d=0; d < (temp[0].length); d++){
				
					if(d == 2 || d == 5) {
						fnum += temp[0].charAt(d)+".";
						//r++;
					}else{
						fnum += temp[0].charAt(d);
						
					}
				
				}	
			
			}
			
			oFtot = fnum+","+temp[1];
			
		}else{
			
			oFtot = temp[0]+","+temp[1];
		}
		
		return oFtot;
}


function moneyMath(oNum){	
	mathNum = oNum.replace(/\./, "").replace(/\,/, ".");
	return mathNum * 1;
}


function walk(elem, signal){ // mantem os divs absolutes corretos - sites centralizados	
	
	if(IE)	var iw=document.body.clientWidth;
	else var iw=window.innerWidth;		
		
		iw2 = iw / 2;
		var factor = IE ? eval("iw2 "+signal+" 68") :  eval("iw2 "+signal+"60");
		var lpos = factor+"px";		
		$GEBId(elem).style.left = lpos;	
		$GEBId(elem).style.display = "";
}



// BISEXTO
function Leap(Year)	{
	if ((Year % 4) == 0){		
		if ((Year % 100) == 0)	Result = ((Year % 400) == 0);
		else Result = 1;		
	}else{		
	Result = 0;
	}
return (Result);
}	
// BISEXTO FIM


//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


rnum1 = "";
rnum2 = "";

function random_number(min,max) {

    var num = (Math.round((max-min) * Math.random() + min));
    if(rnum1 != "" ){    
    	if(num != rnum1) rnum2 = num;
    	else random_number(min,max);    
    }else{
    	rnum1 = num;
    	random_number(min,max);  
    }
    
   
    
    $GEBId("pri").src = "img/representadas/imgsmallrepresentadas_"+arrIds[rnum1]+".jpg";
	$GEBId("seg").src = "img/representadas/imgsmallrepresentadas_"+arrIds[rnum2]+".jpg";
    
    
}


function retRandomImgs(oRet){
	
	arrIds = oRet.split("-");	
	
	random_number(0,(arrIds.length - 1));
		
}


function setRandomImgs(){
	ajax("supportAjax/setRandomImgs.php", retRandomImgs, "", "POST");
}

////////////////////////////////////////////////////////

function intoView(id){     
	var el = $GEBId(id);
    	el.scrollIntoView();
}

function disableCtrlV(e, 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 {
                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;
}



