// JavaScript Document
function checaItens(theform) {
	var theform = document.getElementById(theform);
	var sfEls = theform.getElementsByTagName("INPUT");
	var erro = 0;
	
	for (var i=0; i<sfEls.length; i++) {
		if (sfEls[i].type != 'hidden') {
			if (!sfEls[i].value || sfEls[i].value == '0') erro++;
		}
	}
	
	if (erro > 0) {
		alert('Informe uma quantidade válida para cada item!');	
	} else if (document.getElementById('obs').value || erro == 0) { 
		document.getElementById('form').action = 'index.php?fecha=true&obs=' + document.getElementById('observacao').value;
		document.getElementById('form').submit();
		//window.location='index.php?fecha=true&obs=' + document.getElementById('obs').value;
	}

}

function acao(tipo,id) {
	switch (tipo) {
		case 'atualiza':
			document.getElementById('form').submit();
			break;
		case 'exclui':
			window.location='index.php?excId='+id;
			break;
	}	
}

function ampliaImg(id){
	window.open('showProduto.php?id='+id,'produto','height=10,width=10,status=no,toolbar=no')
}

function resize(img) {
	var largura = 480;
	var altura = img.height + 143;
	window.resizeTo(largura,altura);
}

function openServ(imagem){
	window.open('showServ.php?img='+imagem,'servico','height=10,width=10,status=no,toolbar=no')
}

function resizeServ(img) {
	var largura = img.width + 7;
	var altura = img.height + 53;
	window.resizeTo(largura,altura);
}

function checaCad() {
	form = document.getElementById('cad_user');	
}

function checaCad(form) {
		//theform = document.getElementById('forms');
		theform = form;
		if (document.all || document.getElementById) {
			
			var label = '';
			var errors = 0;
			
			for (i = 0; i < theform.length; i++) {
			var formElement = theform.elements[i];
				if (true) {
					if(theform.elements[i].type != 'button') {
						document.getElementById(formElement.id).style.backgroundColor='';
						
						if (formElement.alt == 'required' || formElement.title == 'required') {
							if (!formElement.value) {
								document.getElementById(formElement.id).style.backgroundColor='#FFFF00';	
								errors++;
							}
						}
					}
				}
			}
		}
		
		if (errors == 0) {
			return true;
		} else {
			alert('Os campos em destaque são obrigatórios');
			return false;
		}
}


function validaCNPJ() {
	CNPJ = document.getElementById('cnpj').value;
	erro = new String;
	if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! \n\n";
	if (CNPJ == '00.000.000/0000-00' ) erro += "Forneça um CNPJ válido! \n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
	}
	//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)) erro += "A verificação de CNPJ suporta apenas números! \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 +="Dígito verificador com problema!";
	}
	
	if (erro.length > 0){
		alert('CNPJ Inválido');
		document.getElementById('cnpj').value = "";
		document.getElementById('cnpj').focus();
		return false;
	} 
	
	return true;
}

function esqueci() {
	email = document.getElementById('mail_log2').value;
	if (!email) {
		alert('Preencha o campo e-mail!');	
		email.focus();
	} else window.location='esqueci.php?m='+email;	
}

function apagaCampo(campo) {
	if (campo.value == 'Seu e-mail' || campo.value == 'senha')
		campo.value = '';
}

function vValue(campo) {
	if (campo.value == '') {
		if (campo.id == 'mail_log') 
			campo.value = 'Seu e-mail';
		else campo.value = 'senha'	;
	}	
}
