/**********Javascript de validação de campos de formulário********/

function isMail(campo){
	var mail = campo.value;
	if( 6 > mail.length || 0 > mail.search(new RegExp('^([a-z0-9_]|\-|\\\.)+@(([a-z0-9_]|\-)+\\\.)+[a-z]{2,4}$','gi')) ){
		return false;
	}
return true;
}
function cadastraNewsletter(){
	if(confirm('Voce deseja participar do nosso newsletter informativo?')){
		if(isMail(document.newsletter.e_mail)){
			document.newsletter.submit();
		}else{
			alert('Formato de e-mail invalido!');
		}
	}
}
function validacpf(s){
    var i;	var c = s . substr(0, 9); var dv = s . substr(9, 2);	var d1 = 0;
    for (i = 0; i < 9; i++){	d1 += c . charAt(i) * (10 - i);	}
    if (d1 == 0){	alert("CPF Invalido"); return false;	}
    d1 = 11 - (d1 % 11);
    if (d1 > 9)	d1 = 0;
    if (dv . charAt(0) != d1){	alert("CPF Invalido"); return false;	}
    d1 *= 2;
    for (i = 0; i < 9; i++){	d1 += c . charAt(i) * (11 - i);	}
    d1 = 11 - (d1 % 11);
    if (d1 > 9)	d1 = 0;
    if (dv . charAt(1) != d1){	alert("CPF Invalido"); return false;	}
    return true;
}
function val_registro(){
 valida = new Array(false, false, false, false, false, false, false, false, false, false, false, false, false, false); //numero de campos a ser verificado
 var i;
 var d_nasc = document.getElementById('data_nascimento').value.match(/^\d{2}\/\d{2}\/\d{4}$/);
 var e_mail = document.getElementById('email').value.match(/^[\w-]+(\.[\w-]+)*@(([\w-]{1,63}\.)+[A-Za-z]{1,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/);
 var numero_1 = document.getElementById('numero').value.match(/^\d{1,6}$/);
 var cep_1 = document.getElementById('cep').value.match(/^\d{8}$/);
 var cpf = validacpf(document.getElementById('cpf').value );

 if (document.getElementById('nome').value!="") {
  valida[0]=true;
  document.getElementById('form_nome').style.color = '#000000';
  }else document.getElementById('form_nome').style.color = '#FF0000';

 if (document.getElementById('data_nascimento').value!="" && d_nasc!=null) {
  valida[1]=true;
  document.getElementById('form_dtnasc').style.color = '#000000';
  }else document.getElementById('form_dtnasc').style.color = '#FF0000';
//alert( document.getElementById('email').value);
 if (document.getElementById('email').value != "" && e_mail != null) {
  valida[2]=true;
  document.getElementById('form_email').style.color = '#000000';
  }	else document.getElementById('form_email').style.color = '#FF0000';

 if (document.getElementById('endereco').value!="") {
  valida[3]=true;
  document.getElementById('form_endereco').style.color = '#000000';
  }	else document.getElementById('form_endereco').style.color = '#FF0000';

  if (document.getElementById('numero').value!="" && numero_1!=null) {
  valida[4]=true;
  document.getElementById('form_numero').style.color = '#000000';
  }	else document.getElementById('form_numero').style.color = '#FF0000';

  if (document.getElementById('bairro').value!="") {
  valida[5]=true;
  document.getElementById('form_bairro').style.color = '#000000';
  }	else document.getElementById('form_bairro').style.color = '#FF0000';

  if (document.getElementById('cidade').value!="") {
  valida[6]=true;
  document.getElementById('form_cidade').style.color = '#000000';
  }	else document.getElementById('form_cidade').style.color = '#FF0000';

  if (document.getElementById('estado').value!="") {
  valida[7]=true;
  document.getElementById('form_estado').style.color = '#000000';
  }	else document.getElementById('form_estado').style.color = '#FF0000';

  if (document.getElementById('cep').value!="" && cep_1!=null) {
  valida[8]=true;
  document.getElementById('form_cep').style.color = '#000000';
  }	else document.getElementById('form_cep').style.color = '#FF0000';

  if (document.getElementById('telefone1').value!="") {
  valida[9]=true;
  document.getElementById('form_telefone').style.color = '#000000';
  }	else document.getElementById('form_telefone').style.color = '#FF0000';

  if (document.getElementById('login').value!="") {
  valida[10]=true;
  document.getElementById('form_login').style.color = '#000000';
  }	else document.getElementById('form_login').style.color = '#FF0000';

  if (document.getElementById('senha1').value!="") {
  valida[11]=true;
  document.getElementById('form_senha1').style.color = '#000000';
  }	else document.getElementById('form_senha1').style.color = '#FF0000';

  if (document.getElementById('senha2').value!="" && document.getElementById('senha2').value==document.getElementById('senha1').value) {
  valida[12]=true;
  document.getElementById('form_senha2').style.color = '#000000';
  }	else document.getElementById('form_senha2').style.color = '#FF0000';

  if(cpf){
    valida[13] = true;
    document.getElementById('form_cpf').style.color = '#000000';
  }else	document.getElementById('form_cpf').style.color = '#FF0000';

	var tama = valida.length;
 	var ver=0;
 	var sub=0;
 	for (x=0;x<tama;x++){
		if (valida[x]==false || valida[x]=="") {
		ver++;
		}else {
			sub++;
		}

	}
	ver_final = eval(tama-sub);
	//alert(ver_final);
	if (ver_final==0) return true
		else {
		 alert('Corrija o(s) ('+ver_final+') campo(s) em vermelho!');
		 return false;
		 }


}


function val_perfil(){
 valida = new Array(false,false,false,false, false); //numero de campos a ser verificado
 var i;
 var d_nasc = document.getElementById('data_nascimento').value.match(/^\d{2}\/\d{2}\/\d{4}$/);
 var e_mail = document.getElementById('email').value.match(/^[\w-]+(\.[\w-]+)*@(([\w-]{1,63}\.)+[A-Za-z]{1,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/);
 var cpf = validacpf(document.getElementById('cpf').value );

 if (document.getElementById('nome').value!="") {
  valida[0]=true;
  document.getElementById('form_nome').style.color = '#000000';
  }else document.getElementById('form_nome').style.color = '#FF0000';

 if (document.getElementById('data_nascimento').value!="" && d_nasc!=null) {
  valida[1]=true;
  document.getElementById('form_dtnasc').style.color = '#000000';
  }else document.getElementById('form_dtnasc').style.color = '#FF0000';

 if (document.getElementById('email').value!="" && e_mail!=null) {
  valida[2]=true;
  document.getElementById('form_email').style.color = '#000000';
  }	else document.getElementById('form_email').style.color = '#FF0000';

  if (document.getElementById('telefone1').value!="") {
  valida[3]=true;
  document.getElementById('form_telefone').style.color = '#000000';
  }	else document.getElementById('form_telefone').style.color = '#FF0000';

  if(cpf){
    valida[4] = true;
    document.getElementById('form_cpf').style.color = '#000000';
  }else	document.getElementById('form_cpf').style.color = '#FF0000';


var tama = valida.length;
 var ver=0;
 var sub=0;
 	for (x=0;x<tama;x++){
		if (valida[x]==false || valida[x]=="") {
		ver++;
		}else {
			sub++;
		}

	}
	ver_final = eval(tama-sub);
	//alert(ver_final);
	if (ver_final==0) return true
		else {
		 alert('Corrija o(s) ('+ver_final+') campo(s) em vermelho!');
		 return false;
		 }


}

function ver_val(){
 //alert (valida);
 var tama = valida.length;
 var ver=0;
 var sub=0;
 	for (x=0;x<tama;x++){
		if (valida[x]==false || valida[x]=="") {
		ver++;
		}else {
			sub++;
		}

	}
	ver_final = eval(tama-sub);

	if (ver_final==0) return true
		else {
		 alert('Corrija o(s) ('+ver_final+') campo(s) em vermelho!');
		 return false;
		 }

}

function ver_val_end(){
 valida2 = new Array(false,false, false, false, false,false,false);
 var tam = valida2.length;
 if (document.getElementById('rotulo').value!="") valida2[0]=true;
 if (document.getElementById('endereco').value!="") valida2[1]=true;
 if (document.getElementById('numero').value!="") valida2[2]=true;
 if (document.getElementById('cidade').value!="") valida2[3]=true;
 if (document.getElementById('estado').value!="") valida2[4]=true;
 if (document.getElementById('cep').value!="") valida2[5]=true;
 if (document.getElementById('bairro').value!="") valida2[6]=true;

 var ver=0;
 var sub=0;
 	for (i=0;i<tam;i++){
		if (valida2[i]==false || valida2[i]=="") {
		ver++;
		}else {
			sub++;
		}

	}
	ver_final = eval(tam-sub);

	if (ver_final==0) return true
		else {
		 alert('Corrija o(s) ('+ver_final+') campo(s) em vermelho!');
		 return false;
		 }

}


function ver_val_prod(){
 var j;
 valida3 = new Array(false,false, false, false);
 xpto = valida3.length;

 var peso = document.getElementById('prod_peso').value.match(/^\d{1,8}$/);
 var valor = document.getElementById('prod_valor').value.match(/^\d{1,6}\.\d{2}$/);
 //alert(c);
 if (document.getElementById('prod_nome').value!="") {
  valida3[0]=true;
  document.getElementById('prod_nome_val').style.color = '#000000';
  }else document.getElementById('prod_nome_val').style.color = '#FF0000';

 if (document.getElementById('ID_CATALOGO').value!="") {
  valida3[1]=true;
  document.getElementById('catalogo_val').style.color = '#000000';
  }
	 else document.getElementById('catalogo_val').style.color = '#FF0000';
 if (document.getElementById('prod_peso').value!="" && peso!= null && peso >= 1) {
  valida3[2]=true;
  document.getElementById('prod_peso_val').style.color = '#000000';
  }
 	else document.getElementById('prod_peso_val').style.color = '#FF0000';
 if (document.getElementById('prod_valor').value != "" && valor != null) {
  valida3[3]=true;
  document.getElementById('prod_valor_val').style.color = '#000000';
  }
 	else document.getElementById('prod_valor_val').style.color = '#FF0000';


 var ver1=0;
 var sub1=0;
 	for (j=0;j<xpto;j++){
		if (valida3[j]==false) {
		ver1++;
		}else {
			sub1++;
		}

	}
	ver_final = eval(xpto-sub1);
	//alert(valida3+ver1+'--'+sub1+'--'+ver_final);
	if (ver_final==0) return true
		else {
		 alert('Corrija o(s) ('+ver_final+') campo(s) em vermelho!');
		 return false;
		 }

}

function val_valor (valor){
 	//verifica valores sem ponto e decimais como 0 - 90
 	var sp = valor.match(/^\d{1,6}$/);

 	//verifica valores incompletos como 0.0 - .0 - .00
 	var pt = valor.match(/^\d{1,6}\.\d{1}$/);
 	var pt2 = valor.match(/^\.\d{1}$/);
 	var pt3 = valor.match(/^\.\d{2}$/);

 	//verifica valores incompletos e com vírgula como 0,00 - 0,0 - ,0 - ,00
 	var vir = valor.match(/^\d{1,6}\,\d{1}$/);
 	var vir2 = valor.match(/^\,\d{1}$/);
 	var vir3 = valor.match(/^\,\d{2}$/);
 	var vir4 = valor.match(/^\d{1,6}\,\d{2}$/);

	//alert (pt+vir);
	if (sp!=null) document.getElementById('prod_valor').value = sp+'.00';

	if (pt!=null) document.getElementById('prod_valor').value = pt+'0';
	if (pt2!=null) document.getElementById('prod_valor').value = '0'+pt2+'0';
	if (pt3!=null) document.getElementById('prod_valor').value = '0'+pt3;

	if (vir!=null) {
	    var str	= String(vir);
	 	var item = str.replace(/,/,".");
	 	document.getElementById('prod_valor').value = item+'0';
	 }

	if (vir2!=null) {
	    var str2	= String(vir2);
	 	var item = str2.replace(/,/,".");
	 	document.getElementById('prod_valor').value = '0'+item+'0';
	 }

	if (vir3!=null) {
	    var str3	= String(vir3);
	 	var item = str3.replace(/,/,".");
	 	document.getElementById('prod_valor').value = '0'+item;
	 }

	if (vir4!=null) {
	    var str4	= String(vir4);
	 	var item = str4.replace(/,/,".");
	 	document.getElementById('prod_valor').value = item;
	 }
}

function val_cep (valor) {
	var traco = valor.match(/^\d{5}\-\d{3}$/);

	if (traco!=null) document.getElementById('cep').value = valor.replace("-", "");
}

function val_banner(){
 valida2 = new Array(false,false);
 var tam = valida2.length;
 var veraction=0;
 if (document.getElementById('action').value=='redit') veraction = 1;

 if (document.getElementById('banner_link').value!="") {
  	valida2[0]=true;
  	document.getElementById('banner_link_label').style.color = '#000000';
  }	else document.getElementById('banner_link_label').style.color = '#FF0000';
 if (document.getElementById('img').value!="" || veraction==1) {
  	 valida2[1]=true;
  	 document.getElementById('banner_img_label').style.color = '#000000';
  	 }
 	else document.getElementById('banner_img_label').style.color = '#FF0000';


 var ver=0;
 var sub=0;
 	for (i=0;i<tam;i++){
		if (valida2[i]==false || valida2[i]=="") {
		ver++;
		}else {
			sub++;
		}

	}
	ver_final = eval(tam-sub);

	if (ver_final==0) return true
		else {
		 alert('Preencha o(s) ('+ver_final+') campo(s) em vermelho!');
		 return false;
		 }

}

function val_numban(value) {
	if (value>20) document.getElementById('numban').value=20;
	if (value<1) document.getElementById('numban').value=1;
}
// AREA JS DENIS... INCLUSAO DE JS PARA A PARTE DE MODELOS.. SELECT
function implode( glue, pieces ) {
    return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
}

/*function incluiModelo(valor){
	modelos = document.getElementById('prod_modelos');
	modelos = modelos.value + ';' + valor;

	a_modelos = new Array();
	a_modelos = modelos.split(';');
	modelos = '<ul>';
	a_2 = new Array();
	x = 0;
	for(i = 0; i < a_modelos.length; i++){
		if(a_modelos[i] != ''){
			modelos += '<li>' + a_modelos[i]+' <a href="javascript: excluiModelo('+i+');" style="color:#000000;">Apagar</a></li>';
			a_2[x] = a_modelos[i];
			x++;
		}
	}
	modelos += '</ul>';
	//alert(modelos);
	document.getElementById('prod_modelos').value = implode(';', a_2);
	document.getElementById('txt_modelos').innerHTML = modelos;
}
//ALTER TABLE `loja2_produtos` ADD `prod_modelos` MEDIUMTEXT NOT NULL AFTER `prod_data` ;
function excluiModelo(id){
	if(confirm('Deseja excluir o item selecionado?')){
		modelos = document.getElementById('prod_modelos');
		a_modelos = new Array();
		a_modelos = modelos.value.split(';');
		a_2 = new Array();
		x = 0;
		for(i = 0; i < a_modelos.length; i++){
			if(i != id){
				a_2[x] = a_modelos[i];
				x++;
			}
		}
		modelos.value = implode(';', a_2);
		incluiModelo('');
	}
}*/
function excluiModelo(id){
	if(confirm("Deseja realmente excluir o item desejado?")){
		document.getElementById("tr["+id+"]").innerHTML = "";
	}
}
function incluiModelo(){
	nome = document.getElementById('nome').value;
	id = document.getElementById('id').value;
	qtd = document.getElementById('qtd').value;
	if(qtd != "" && nome != ""){
		novos = document.getElementById('novos').value;
		models = document.getElementById('modelos').innerHTML;
		document.getElementById('modelos').innerHTML = models+"<div id=\"tr[n"+novos+"]\"><table  width=\"100%\" border=\"0\">\n\t\t\t\t<tr >"+
		"<td><input type=\"text\" name=\"modelon["+novos+"]\" maxlength=\"10\" size=\"3\" value=\""+id+"\"/></td>\n\t\t\t\t"+
		"<td><input type=\"text\" name=\"nomen["+novos+"]\" maxlength=\"60\" value=\""+nome+"\"/></td>\n\t\t\t\t"+
		"<td><input type=\"text\" name=\"estoquen["+novos+"]\" maxlength=\"3\" size=\"2\" value=\""+qtd+"\"/></td>\n\t\t\t\t"+
		"<td style=\"width: 47px;\" align=\"center\"><a href=\"javascript: excluiModelo(\'n"+novos+"\')\"><img src=\"images/fechar_xis.gif\" border=\"0\"/></a></td></tr>\n</table></div>\n";
		document.getElementById('novos').value = parseInt(novos) + 1;
	}
}
function mudaimg(img){
	//alert(id);
	//if(img != '-')
		document.getElementById('img_news').src = 'images/newsletter/'+img;
}


