/*============================ ======================================
	   			Funcoes JavaScript Gerais do Sistema
 
 * @author $Author: cleber $
 * @version $Revision: 1890 $ - $Date: 2009-12-15 19:04:50 -0200 (Tue, 15 Dec 2009) $
 * @copyright Interage Integradora © <cleber@interage.com.br>
 
 ============================= ======================================*/

/**
	Direciona a pagina com JS
*/
var redirect = function (link) {
	window.location.href = link;
}

/**
	Efetua submit formulario
*/
var submit = function(id) {
	document.getElementById(id).submit();
}

/**
	Retira espacos em branco
*/
var trim = function (field) {
	return field.replace(/^\s+|\s+$/g,"");
}

/**
	Verifica campos em branco
*/
var empty = function (field) {
	return (trim(field) == '') ? true : false;
}


/**
	Plugin para validade : validacao de telefone
*/
jQuery.validator.addMethod("phonevalidator", function(phone_number, element) {
	phone_number = phone_number.split("-");
	var ddd = phone_number[0];
	var phone = phone_number[1];
	return (isNaN(ddd) || isNaN(phone) || ddd.length != 2 || (phone.length < 7 || phone.length > 8)) ? false : true;
}, "Telefone informado &eacute;  inv&aacute;lido.");


/**
	Plugin para validade : validacao de CEP
*/
jQuery.validator.addMethod("cepvalidator", function(cep_number, element) {
	cep_number = cep_number.replace("-","");
	cep_number = cep_number.replace("_","");
	return cep_number.length == 8 ? true : false;
}, "CEP  informado &eacute; inv&aacute;lido.");


/**
	Plugin para validade : validacao de cnpj
*/
jQuery.validator.addMethod("cnpjvalidator", function(cnpj,element){
	if (cnpj=='00.000.000/0000-00')	{ return false;	 }
	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 ("/","");   
	   cnpj = cnpj. replace ("_","");   
	}
	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])){
		return false;
	}  
	return true;
},"CNPJ informado &eacute; inv&aacute;lido");


/**
	Plugin para validade : validacao de CPFs
*/
jQuery.validator.addMethod("cpfvalidator", function(cpf,element){
	var igual=0;
	cpf = cpf.replace(".","");
	cpf = cpf.replace(".","");
	cpf = cpf.replace("-","");
	cpf = cpf.replace("_","");
	for(x=0; x <=8; x++){
		var pos1=cpf.substr( x, 1);
		var pos2=cpf.substr( x+1, 1);
		if(pos1==pos2){
			igual++;
		}
	}
	if(igual==9){ 		return false;	}
	var POSICAO, I, SOMA, DV, DV_INFORMADO;
	var DIGITO = new Array(10);
	DV_INFORMADO = cpf.substr(9, 2);
	for (I=0; I<=8; I++) {
	  DIGITO[I] = cpf.substr( I, 1);
	}
	POSICAO = 10;
	SOMA = 0;
   	for (I=0; I<=8; I++) {
      SOMA = SOMA + DIGITO[I] * POSICAO;
      POSICAO = POSICAO - 1;
   	}
	DIGITO[9] = SOMA % 11;
	if (DIGITO[9] < 2) {
        DIGITO[9] = 0;
	}else{
	   DIGITO[9] = 11 - DIGITO[9];
	}
	POSICAO = 11;
	SOMA = 0;
	for (I=0; I<=9; I++) {
	  SOMA = SOMA + DIGITO[I] * POSICAO;
	  POSICAO = POSICAO - 1;
	}
	DIGITO[10] = SOMA % 11;
	if (DIGITO[10] < 2) {
	    DIGITO[10] = 0;
	}else {
	    DIGITO[10] = 11 - DIGITO[10];
	}
	DV = DIGITO[9] * 10 + DIGITO[10];
	if (DV != DV_INFORMADO) {
	  return false;	
	} 
	return true;
},"CPF informado &eacute; inv&aacute;lido");
