// JavaScript Document
function insc_est()
{
 	var	cnpj= document.getElementById('cpf');
	if (cnpjfield_validate(cnpj))
	{
		return document.getElementById('insc_est').value.length!=0;
	}
	else return true;
}
function allDigits(str){
   return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset){
	var result = true;
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0) {
			result = false;
			break;
		}
	
	return result;
}

function datefield_validate(campo) {
	var result = true;
	if(campo.value.length!=10)return false;
   	if (result){
		var elems = campo.value.split("/");
 		result = (elems.length == 3); // should be three components
 		if (result){
 			var month = parseInt(elems[1],10);
  			var day = parseInt(elems[0],10);
 			var year = parseInt(elems[2],10);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
					 allDigits(elems[1]) && (day > 0) && (day < 32) &&
					 allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
 		}
	} 
	return result;
}

function cepfield_validate(campo){
	if(campo.value.length!=9)return false;
	if(campo.value.charAt(5)!='-')return false;
//	if(isNaN(parseInt(campo.value.substring(0, 8))))return false;
	return true;
}

function textfield_validate(campo) {
	return campo.value.length!=0;
}

function phonefield_validate(campo) {
	if(campo.value.length==0)
		return false;
	var regex = /^\d{10,11}$/;
	return regex.test(campo.value);
}

function emailfield_validate(campo){
	if(campo.value.length==0)return false;
	var regex = /^[a-z0-9._-]+\@[a-z0-9._-]+\.[a-z]{2,4}$/;
	regex.ignoreCase = true;
	return regex.test(campo.value);
}

function cpffield_validate(campo) {
	if(campo.value.length==0)return false;
	var strcpf = campo.value;
	var str_aux = "";
	for (i = 0; i <= strcpf.length - 1; i++)
		if ((strcpf.charAt(i)).match(/\d/))
			str_aux += strcpf.charAt(i);
		else if (!(strcpf.charAt(i)).match(/[\.\-]/))
			return false;
	if (str_aux.length != 11)
		return false;
	var soma1 = 0;
	var soma2 = 0;
	for (i = 0; i <= 8; i++) {
		soma1 += str_aux.charAt(i) * (10-i);
		soma2 += str_aux.charAt(i) * (11-i);
	}
	var d1 = ((soma1 * 10) % 11) % 10;
	var d2 = (((soma2 + (d1 * 2)) * 10) % 11) % 10;
	if ((d1 != str_aux.charAt(9)) || (d2 != str_aux.charAt(10)))
		return false;
	return true;
}

function cnpjfield_validate(campo){
	if(campo.value.length==0)return false;
	var CNPJ = campo.value;
	if (CNPJ.length < 18)return false;
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-"))
		return false;
	//substituir os caracteres que não são números
	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))return false;
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	var i;
	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;
}

function cpf(strcpf)
{
	var str_aux = "";
	for (i = 0; i <= strcpf.length - 1; i++)
		if ((strcpf.charAt(i)).match(/\d/))
			str_aux += strcpf.charAt(i);
		else if (!(strcpf.charAt(i)).match(/[\.\-]/))
			return false;
	if (str_aux.length != 11)
		return false;
	var soma1 = 0;
	var soma2 = 0;
	for (i = 0; i <= 8; i++) {
		soma1 += str_aux.charAt(i) * (10-i);
		soma2 += str_aux.charAt(i) * (11-i);
	}
	var d1 = ((soma1 * 10) % 11) % 10;
	var d2 = (((soma2 + (d1 * 2)) * 10) % 11) % 10;
	if ((d1 != str_aux.charAt(9)) || (d2 != str_aux.charAt(10)))
		return false;
	return true;
}

function cnpj(CNPJ)
{
	if (CNPJ.length < 18)return false;
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-"))
		return false;
	//substituir os caracteres que não são números
	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))return false;
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	var i;
	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;
}

function cpf_cnpj_validate(campo)
{
	if(campo.value.length==0)return false;
	if(!cpf(campo.value))
		if(!cnpj(campo.value))
			return false;
	return true;
}

function pricefield_validate(campo)
{
	var texto = campo.value.replace('.', '');
	if(campo.value.length==0)return false;	
	var regex = /^\d+(,\d{1,2})?$/;
	regex.ignoreCase = true;
	return regex.test(texto);
}

function preciseNumberfield_validate(campo){
	if(campo.value.length==0)return false;
	var regex = /^\d+([.,]\d{1,4})?$/;
	regex.ignoreCase = true;
	return regex.test(campo.value);
}

function selectfield_validate(campo){
	return campo.selectedIndex!=0;
}

function integerfield_validate(campo){
	if(campo.value.length==0)return false;
	for(var i = 0; i < campo.value.length; i++)
		if(isNaN(parseInt(campo.value.charAt(i))))return false;
	return true;
}

function passwordfield_validate(campo){
	if(campo.value.length==0)return false;
	if(document.getElementById(campo.name+'_valid').value!=campo.value)return -1;
	return true;
}

function form_validate(form){
	for(var i = 0;i<form.elements.length;i++){
			var input = form.elements[i];
			if(!input.getAttribute('alt'))continue;
			var idx = input.getAttribute('alt').indexOf(':');
			if(idx<0)idx = input.getAttribute('alt').length;
			var validation = input.getAttribute('alt').substring(0, idx);
			var message = input.getAttribute('alt').substring(idx+1);
			var required = true;
			var option = '';
			if((idx = validation.indexOf('-'))>-1){
				option = validation.substring(idx+1);
				validation = validation.substring(0, idx);
				if(option=='norequired')
					required = false;
			}
			if((!required) && (input.value.length==0))
				continue;
			if(input.disabled)
				continue;
			var block = false;
			if(validation == 'date'){
				block = !datefield_validate(input);insc_est
			}else if(validation == 'text'){
				block = !textfield_validate(input);
			}else if(validation == 'insc_est'){
				block = !insc_est();
			}else if(validation == 'cep'){
				block = !cepfield_validate(input);
			}else if(validation == 'phone'){
				block = !phonefield_validate(input);
			}else if(validation == 'email'){
				block = !emailfield_validate(input);
				if(option.substring(0, 7)=='domain(')
				{
					option = option.substring(7, option.length-1);
					if(input.value.substr(input.value.indexOf('@')+1)!=option)
						block = true;
				}
			}else if(validation == 'cpf'){
				block = !cpffield_validate(input);
			}else if(validation == 'cnpj'){
				block = !cnpjfield_validate(input);
			}else if(validation == 'cpf_cnpj'){
				block = !cpf_cnpj_validate(input);
			}else if(validation == 'price'){
				block = !pricefield_validate(input);
			}else if(validation == 'preciseNumber'){
				block = !preciseNumberfield_validate(input);
			}else if(validation == 'select'){
				block = !selectfield_validate(input);
			}else if(validation == 'int'){
				block = !integerfield_validate(input);
			}else if(validation == 'password'){
				var ret = passwordfield_validate(input);
				block = !(ret==true);
				if(ret==-1){
					alert(option);
					input.focus();
					if(input.select)input.select();
					return false;
				}
			}
			if(block){
				alert(message);
				input.focus();
				if(input.select)input.select();
				return false;
			}
	}
	return true;
}

function date_cross(dataFirst, dataLast){
	var dateSumFirst = parseInt(dataFirst.substr(0, 2))+parseInt(dataFirst.substr(3, 2))*100+parseInt(dataFirst.substr(6, 4))*10000;
	var dateSumLast = parseInt(dataLast.substr(0, 2))+parseInt(dataLast.substr(3, 2))*100+parseInt(dataLast.substr(6, 4))*10000;
	return dateSumLast>=dateSumFirst;
}