// JavaScript Document
///////////////////////////////////
function removeElement(divNum) {
	
  var d = document.getElementById('myDiv');
  var olddiv = document.getElementById(divNum);
 	  d.removeChild(olddiv);
  }

function add() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById("theValue").value -1)+ 2;
  numi.value = num;
  var divIdName = "my"+num+"Div";
  var newdiv = document.createElement('div');
  newdiv.setAttribute("id",divIdName);
  //pego o valor entre a tag <option></option> do select com o di 'area__0'
  var opts = document.getElementById('div__0');
  var label = opts.options[opts.selectedIndex].text;
  //////////////////////////////////////////////////////	
  newdiv.innerHTML = "<table border='0' cellpadding='0' cellspacing='0' ><tr><td><b>Tipo:</b></td><td>&nbsp;<label>"+label+"</label><input type=\"hidden\" name=\"div__" + num + "\" value="+document.getElementById('div__0').value+" /></td><td>&nbsp;<a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\"> <span class=\"txt_td\"> remover </span> </a></td></tr></table>"; 
  ni.appendChild(newdiv); 
}

function removeElement2(divNum) {
	
  var d = document.getElementById('myDiv2');
  var olddiv = document.getElementById(divNum);
 	  d.removeChild(olddiv);
  }


function add2() {
  var ni = document.getElementById('myDiv2');
  var numi = document.getElementById('theValue2');
  var num = (document.getElementById("theValue2").value -1)+ 2;
  numi.value = num;
  var divIdName = "my"+num+"Div2";
  var newdiv = document.createElement('div');
  newdiv.setAttribute("id",divIdName);
 
  var opts = document.getElementById('div2__0').value;
   
  //////////////////////////////////////////////////////	
  newdiv.innerHTML = "<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border-top:#999 1px solid;border-bottom:#999 1px solid;margin:5px 0 0 0;'><tr><td align='left'><b>Resposta:</b></td><td align='right'>&nbsp;<a href=\"javascript:;\" onclick=\"removeElement2(\'"+divIdName+"\')\"> <span class=\"txt_td\"> remover </span> </a></td></tr><tr><td colspan='2'>&nbsp;</td></tr><tr><td colspan='2' align='left'>&nbsp;<label>"+opts+"</label><input type=\"hidden\" name=\"div2__" + num + "\" value=\""+opts+"\" /></td><tr></table>";   
  ni.appendChild(newdiv);   
}
/**Script em Javascript para formatar o valor com virgula nas casas decimais no formulário**/
function FormataValor(id,tammax,teclapres) 
{
	if(window.event)  // Internet Explorer
	{
		var tecla = teclapres.keyCode; 
	}
    else if(teclapres.which) // Nestcape / firefox
	{
       	var tecla = teclapres.which;
    }
    
	vr = document.getElementById(id).value;
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( "/", "" );
	vr = vr.toString().replace( ",", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	vr = vr.toString().replace( ".", "" );
	
	tam = vr.length;

	if (tam < tammax && tecla != 8)
	{ 
		tam = vr.length + 1; 
	}
	if (tecla == 8 )
	{ 
		tam = tam - 1; 
	}
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if ( tam <= 2 )
		{
			document.getElementById(id).value = vr; 
		}
		if ( (tam > 2) && (tam <= 5) )
		{
			document.getElementById(id).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 6) && (tam <= 8) )
		{
			document.getElementById(id).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 9) && (tam <= 11) )
		{
			document.getElementById(id).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 12) && (tam <= 14) )
		{
			document.getElementById(id).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); 
		}
		if ( (tam >= 15) && (tam <= 17) )
		{
			document.getElementById(id).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
		}
	}
}


function valida_horas(edit){
      if(event.keyCode<48 || event.keyCode>57){
        event.returnValue=false;
      }
      /*if(edit.value.length==2 || edit.value.length==5){
        edit.value+=":";}*/
		if(edit.value.length==2){
        edit.value+=":";}
}


function mascaraData(campoData){
	var data = campoData.value;
    if (data.length == 2){
    	data = data + '/';
        document.trabalhe.data.value = data;
      	return true;              
    }
    if (data.length == 5){
    	data = data + '/';
        document.trabalhe.data.value = data;
        return true;
    }
}
//--->Função para a formatação dos campos...<---
function Mascara(tipo, campo, teclaPress) {
    if (window.event)
    {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var s = new String(campo.value);
    // Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
    s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

    tam = s.length + 1;

    if ( tecla != 9 && tecla != 8 ) {
        switch (tipo)
        {
        case 'CPF' :
            if (tam > 3 && tam < 7)
                campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
            if (tam >= 7 && tam < 10)
                campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
            if (tam >= 10 && tam < 12)
                campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
        break;

        case 'CNPJ' :

            if (tam > 2 && tam < 6)
                campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
            if (tam >= 6 && tam < 9)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
            if (tam >= 9 && tam < 13)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
            if (tam >= 13 && tam < 15)
                campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
        break;

        case 'TEL' :
            if (tam > 2 && tam < 4)
                campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
            if (tam >= 7 && tam < 11)
                campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
        break;

        case 'DATA' :
            if (tam > 2 && tam < 4)
                campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
            if (tam > 4 && tam < 11)
                campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
        break;
        
        case 'CEP' :
			if (tam > 2 && tam < 4)
                campo.value = s.substr(0,2) + ' ' + s.substr(2, tam);
            if (tam > 5 && tam < 7)
                campo.value = s.substr(0,5) + '-' + s.substr(5, tam);
        break;
	
	case 'HORA' :
            if (tam > 2 && tam < 4)
                campo.value = s.substr(0,2) + ':' + s.substr(2, tam);
            if (tam > 4 && tam < 11)
                campo.value = s.substr(0,2) + ':' + s.substr(2,2) + ':' + s.substr(4,tam-4);
	break;

        }
    }
	
}

//--->Função para verificar se o valor digitado é número...<---
function digitos(event){
    if (window.event) {
        // IE
        key = event.keyCode;
    } else if ( event.which ) {
        // netscape
        key = event.which;
    }
    if ( key != 8 || key != 13 || key < 48 || key > 57 )
        return ( ( ( key > 47 ) && ( key < 58 ) ) || ( key == 8 ) || ( key == 13 ) );
    return true;
}

//Bloqueio de caracteres especiais e números
function VerificaCaracteres(caracter) {

	if(window.event) { // Internet Explorer
  	var tecla = event.keyCode;
 	}
 	else { // Firefox
  	var tecla = caracter.which;
 	}

 	if((tecla == 33) || (tecla == 34) || (tecla == 35) || (tecla == 36) || (tecla == 37) || (tecla == 38) || (tecla == 39) || (tecla == 40) || (tecla == 41) || (tecla == 42) || (tecla == 43) || (tecla == 44) || (tecla == 45) || (tecla == 46) || (tecla == 47) || (tecla == 48) || (tecla == 49) || (tecla == 50) || (tecla == 51) || (tecla == 52) || (tecla == 53) || (tecla == 54) || (tecla == 55) || (tecla == 56) || (tecla == 57) || (tecla == 58) || (tecla == 59) || (tecla == 60) || (tecla == 61) || (tecla == 63) || (tecla == 64) || (tecla == 91) || (tecla == 92) || (tecla == 93) || (tecla == 123) || (tecla == 124) || (tecla == 125)) { 
  	alert("Favor digite o seu nome corretamente!");
  	return false;
 	}
 	else 
 	{ 
 		return true; 
 	}
}
////////confirmação para acessar url//////////
function confirma(msg,url)
{
	if (confirm(msg)){
		window.location = url;
	}
	else{
		return false;
	}
}

function validaUsuario()
{
	
	if(document.forms[0].name == "cadastro")
	{
		var caminho = document.cadastro;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome do usuário!");
			caminho.nome.focus();
			return false;
		}
		
		if(caminho.sobrenome.value == ""){
			alert("Digite o sobrenome do usuário!");
			caminho.sobrenome.focus();
			return false;
		}
		
		if(caminho.email.value != ""){
			if(caminho.email.value.indexOf("@") == -1 || caminho.email.value.indexOf(".") == -1){
				alert("Digite um E-mail v\u00e1lido!");
				caminho.email.focus();
				return false;
			}
		}
		
		if(caminho.telefone.value == ""){
			alert("Digite o telefone do usuario!");
			caminho.telefone.focus();
			return false;
		}
		
		if(caminho.login.value == ""){
			alert("Digite seu login de acesso!");
			caminho.login.focus();
			return false;
		}
		
		if(caminho.senha.value == ""){
			alert("Digite a senha do usu\u00e1rio!");
			caminho.senha.focus();
			return false;
		}	
		
		if(caminho.confirma.value == ""){
			alert("Confirme a senha do usu\u00e1rio!");
			caminho.confirma.focus();
			return false;
		}
		
		if(caminho.senha.value != caminho.confirma.value){
			alert("As senhas n\u00e3o conferem!");
			caminho.confirma.focus();
			return false;	
		}
	}
	else if(document.forms[0].name == "edita")
	{
		var caminho = document.edita;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome do usuário!");
			caminho.nome.focus();
			return false;
		}
		
		if(caminho.sobrenome.value == ""){
			alert("Digite o sobrenome do usuário!");
			caminho.sobrenome.focus();
			return false;
		}
		
		if(caminho.email.value != ""){
			if(caminho.email.value.indexOf("@") == -1 || caminho.email.value.indexOf(".") == -1){
				alert("Digite um E-mail v\u00e1lido!");
				caminho.email.focus();
				return false;
			}
		}
		
		if(caminho.telefone.value == ""){
			alert("Digite o telefone do usuario!");
			caminho.telefone.focus();
			return false;
		}
		
		if(caminho.login.value == ""){
			alert("Digite seu login de acesso!");
			caminho.login.focus();
			return false;
		}	
		
		if(caminho.senha.value != "")
		{
			if(caminho.confirma.value == ""){
				alert("Confirme a senha do usu\u00e1rio!");
				caminho.confirma.focus();
				return false; 
			}
		
			if(caminho.senha.value != caminho.confirma.value){
				alert("As senhas n\u00e3o conferem!");
				caminho.confirma.focus();
				return false;	
			} 
		}
	}
}

function validaCategoria()
{
	if(document.forms[0].name == "cadastro")
	{
		var caminho = document.cadastro;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da categoria!");
			caminho.nome.focus();
			return false;
		}
	}
	else if(document.forms[0].name == "edita")
	{
		var caminho = document.edita;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da categoria!");
			caminho.nome.focus();
			return false;
		}
	}
}

function validaSubcategoria()
{
	if(document.forms[0].name == "cadastro")
	{
		var caminho = document.cadastro;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da subcategoria!");
			caminho.nome.focus();
			return false;
		}
	}
	else if(document.forms[0].name == "edita")
	{
		var caminho = document.edita;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da subcategoria!");
			caminho.nome.focus();
			return false;
		}
	}
}

function appendOptionLast(id_obj,text,value)
{
  var i;
  var elSel = document.getElementById(id_obj);
  for (i = elSel.length - 1; i>=0; i--) {  
    if (elSel.options[i].text==text) {	
	alert('Item já selecionado!');
	return false;
    }
  }    
  var elOptNew = document.createElement('option');
  elOptNew.text = text;
  elOptNew.value = value;
  var elSel = document.getElementById(id_obj);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function getOptionName(id_obj){
	var x=document.getElementById(id_obj);
	return x.options[x.selectedIndex].text;
}


function selectAllIndexes(id_obj){ //Retorna o numero de options que o select tem ou false caso não tenha nenhum;
	var x=document.getElementById(id_obj);
	if (x.options.length <= 0) {return false;}
	for(var i=0; i<x.options.length; i++)
	{
		x.options[i].selected = true;
	}	
	/*alert( x.options.length);*/
}

function removeOptionSelected(id_obj)
{
  var elSel = document.getElementById(id_obj);
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
    if (elSel.options[i].selected) {
      elSel.remove(i);
    }
  }
}

function validaProduto(){
	
	if(document.forms[0].name == "cadastro")
	{
		var campo = document.cadastro;
		
		selectAllIndexes('selecionados');
		
		if(campo.nome.value == ""){
			alert("Digite o nome do produto");
		 	 campo.nome.focus();
		  	return false;
		}
		
		if(campo.foto.value == ""){
			alert("Importe a foto do produto");
		 	 campo.foto.focus();
		  	return false;
		}
		
		if(campo.categorias.value == 0){
			alert("O campo categorias não foi selecionado");
		 	campo.categorias.focus();
		  	return false;
		}
		if(campo.subcategorias.value == 0){
			alert("O campo subcategorias não foi selecionado");
		  	campo.subcategorias.focus();
		  	return false;
		}
		if(campo.selecionados.length == 0){
			alert("Voce deve adicionar ao menos uma subcategoria.");
			return false;
		}
		
	}
	else if(document.forms[0].name == "edita")
	{
		var campo = document.edita;
		
		selectAllIndexes('selecionados');
	
		if(campo.nome.value == ""){
			alert("O campo nome não foi preenchido");
		  campo.nome.focus();
		  return false;
		}
		
		if(campo.selecionados.length == 0){
		
			if(campo.categorias.value == 0){
				alert("O campo categorias não foi selecionado");
				campo.categorias.focus();
				return false;
			}
			if(campo.subcategorias.value == 0){ 
				alert("O campo subcategorias não foi selecionado");
				campo.subcategorias.focus();
				return false;
			}
			if(campo.selecionados.length == 0){
				alert("Voce deve adicionar ao menos uma subcategoria.");
				return false;
			}
		}
		
		
	}
	
}

function validaVitrine()
{

	if(document.forms[0].name == "cadastro")
	{
		var caminho = document.cadastro;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da vitrine!");
			caminho.nome.focus();
			return false;
		}
		
		if(caminho.imagem.value == ""){
			alert("Importe uma imagem para a vitrine!");
			caminho.imagem.focus();
			return false;
		}
		
		if(caminho.posicao.checked == false){
			alert("Escolha a posição da vitrine");
			return false;
		}
		
	}
	else if(document.forms[0].name == "edita")
	{
		var caminho = document.edita;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da vitrine!");
			caminho.nome.focus();
			return false;
		}
	}

}

function validaNovidade()
{
	if(document.forms[0].name == "cadastro")
	{
		var caminho = document.cadastro;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da novidade!");
			caminho.nome.focus();
			return false;
		}
		
		if(caminho.titulo.value == ""){
			alert("Digite o título da novidade!");
			caminho.titulo.focus();
			return false;
		}
		
		
	}
	else if(document.forms[0].name == "edita")
	{
		var caminho = document.edita;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome da novidade!");
			caminho.nome.focus();
			return false;
		}
		
		if(caminho.titulo.value == ""){
			alert("Digite o título da novidade!");
			caminho.titulo.focus();
			return false;
		}
	}		
}

function validaTipo(){
	
	if(document.forms[0].name == "cadastro")
	{
		var caminho = document.cadastro;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome do tipo!");
			caminho.nome.focus();
			return false;
		}	
	}
	else if(document.forms[0].name == "edita")
	{
		var caminho = document.edita;
		
		if(caminho.nome.value == ""){
			alert("Digite o nome do tipo!");
			caminho.nome.focus();
			return false;
		}	
	}
	
}

function validaFaq(){
	
	if(document.forms[0].name == "cadastro")
	{
		var caminho = document.cadastro;
		
		if(caminho.pergunta.value == ""){
			alert("Digite a pergunta do faq!");
			caminho.pergunta.focus();
			return false;
		}	
		if(caminho.tipo.value == ""){
			alert("Selecione o tipo para pergunta do faq!");
			caminho.tipo.focus();
			return false;
		}
	} 
	else if(document.forms[0].name == "edita")
	{
		var caminho = document.edita;
		 
		if(caminho.pergunta.value == ""){
			alert("Digite a pergunta do faq!");
			caminho.pergunta.focus();
			return false;
		}	
	} 
}
