// JavaScript Document
//<input name="cep" type="text" id="cep" onkeypress="return formataCampo(event,this,'####-###');" size="10" maxlength="9">

function formataCampo(e,src,mask) {

	var _TXT = (e.which) ? e.which : e.keyCode;
	
    if(_TXT > 47 && _TXT < 58) {
	var i = src.value.length; var saida = mask.substring(0,1); var texto = mask.substring(i)
	if (texto.substring(0,1) != saida) { src.value += texto.substring(0,1); }
		return true;
	} else {
		if ((_TXT != 8) && (_TXT != 0)) { return false; }
		else { return true; }
    }
}

function verificaDDD(componente){
	if(componente.value.length == 2){
		document.cadastro.telefone.focus();
	}
}

function mostraDiv(id){
	if (document.getElementById(id).style.display == 'none'){
		document.getElementById(id).style.display = 'block';
	} else {		
		document.getElementById(id).style.display = 'none';
	}
	return true;
}

function remover_espacos(str){
	r = "";
	for(i = 0; i < str.length; i++){
		if(str.charAt(i) != ' '){
			r += str.charAt(i);
		}
	}
	return r;
}
function ajaxInit() {
	var xmlHttp; 
	try { 
	  // Firefox, Opera 8.0+, Safari 
	  xmlHttp=new XMLHttpRequest(); 
	} 
	catch (e) { 
	  // Internet Explorer 
	  try { 
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	  } 
	  catch (e) { 
		try { 
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch (e) { 
		  alert("Your browser does not support ajax!"); 
		  return false; 
		} 
	  } 
	}
	return xmlHttp;
}
function ordena(coluna,table,tipo,categoria){
	if (categoria != null){
		var ordem = document.getElementById(coluna+"_"+categoria);
		var seta = document.getElementById("seta_"+coluna+"_"+categoria);
	} else {
		var ordem = document.getElementById(coluna);
		var seta = document.getElementById("seta_"+coluna);
	}
	if (ordem.value == 'asc'){
		ordem.value = 'desc';
		seta.src = 'http://www.macromove.com.br/images/setaOrdena.gif';
	} else {
		ordem.value = 'asc';
		seta.src = "http://www.macromove.com.br/images/setaOrdena2.gif";
	}
	
	var url = "http://www.macromove.com.br/scriptAjax.php?ordena=" + coluna + "&ordem=" + ordem.value+"&tipo="+tipo;
	if (categoria != null){
		url += "&categoria="+categoria;
	}

	var div = document.getElementById("loading");
	ajax = ajaxInit();
	if(ajax) {
		ajax.open("GET", url, true);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 1){
				div.style.display = "block";
				div.innerHTML = "<img src='http://www.macromove.com.br/images/ajax-loader.gif' alt='loading...'  />"; 
			}
			if(ajax.readyState == 4) {
				document.getElementById(table).innerHTML = '<table width="100%" cellspacing="0" cellpadding="5">'+ajax.responseText+'</table>';
				div.style.display = "none";
				// Ordena
			}
		}
		ajax.send(null);
	}
}
function mostraDesc(cat,id){
	var fundo = "3";
	if (id%2 > 0){
		fundo = "4";
	}
	var seta = document.getElementById("seta"+cat+"_"+id);
	var botao = document.getElementById("botao"+cat+"_"+id);
	var status = document.getElementById("status"+cat+"_"+id);
	
	if (status.value == 'fechado'){
		seta.src = "http://www.macromove.com.br/images/setaFecha.gif";
		seta.style.width = "13px";
		seta.style.height = "13px";
		botao.src = "http://www.macromove.com.br/images/btMenos"+fundo+".jpg";
		status.value = 'aberto';
		
	} else {
		seta.src = "http://www.macromove.com.br/images/setaAbre.gif";
		seta.style.width = "9px";
		seta.style.height = "9px";
		botao.src = "http://www.macromove.com.br/images/btMais"+fundo+".jpg";
		status.value = 'fechado';
	}
}

function mostraDescOrdenado(div,cat,id){
	var fundo = "3";
	if (id%2 > 0){
		fundo = "4";
	}
	var seta = document.getElementById("seta"+cat+"_"+id);
	var botao = document.getElementById("botao"+cat+"_"+id);
	var status = document.getElementById("status"+cat+"_"+id);
	
	if (status.value == 'fechado'){
		seta.src = "http://www.macromove.com.br/images/setaFecha.gif";
		seta.style.width = "13px";
		seta.style.height = "13px";
		botao.src = "http://www.macromove.com.br/images/btMenos"+fundo+".jpg";
		status.value = 'aberto';
		document.getElementById(div).style.display = "block";
		
	} else {
		seta.src = "http://www.macromove.com.br/images/setaAbre.gif";
		seta.style.width = "9px";
		seta.style.height = "9px";
		botao.src = "http://www.macromove.com.br/images/btMais"+fundo+".jpg";
		status.value = 'fechado';		
		document.getElementById(div).style.display = "none";
	}
}


function mostraCat(cat,id){
	var fundo = "";
	if (id%2 > 0){
		fundo = "2";
	}
	if (document.getElementById("status"+cat).value == 'fechado'){
		document.getElementById("seta"+cat).src = "http://www.macromove.com.br/images/setaFecha.gif";
		document.getElementById("seta"+cat).style.width = "13px";
		document.getElementById("seta"+cat).style.height = "13px";
		document.getElementById("botao"+cat).src = "http://www.macromove.com.br/images/btMenos"+fundo+".jpg";
		document.getElementById("status"+cat).value = 'aberto';
	} else {
		document.getElementById("seta"+cat).src = "http://www.macromove.com.br/images/setaAbre.gif";
		document.getElementById("seta"+cat).style.width = "9px";
		document.getElementById("seta"+cat).style.height = "9px";
		document.getElementById("botao"+cat).src = "http://www.macromove.com.br/images/btMais"+fundo+".jpg";
		document.getElementById("status"+cat).value = 'fechado';
	}
	
}


function adicionarArq(index){
	var listaArq = document.getElementById("listaArq");
	listaArq.value += index+";";
	var table = document.getElementById("tabArq");
	
	var col0 = document.createElement("TD");
	col0.align = 'center';
	var col1 = document.createElement("TD");
	var col2 = document.createElement("TD");
	col2.align = "right";
	var col3 = document.createElement("TD");
	col3.align = "right";	
	var col4 = document.createElement("TD");
	col4.align = "right";
	
	var radio = document.createElement("input");
	radio.setAttribute("onClick","verificaPrincipal('numArquivos','principal',this)");
	radio.type = 'radio';
	radio.name = "principal_"+index;
	radio.id = "principal_"+index;
	radio.value = 'S';
	col0.appendChild(radio);
	
	var hidden = document.createElement("input");
	hidden.type = 'hidden';
	hidden.name = "desc"+index;
	hidden.id = "desc"+index;
	col1.appendChild(hidden);
	
	var input = document.createElement("input");
	input.type = 'file';
	input.name = "arq"+index;
	input.id = "arq"+index;
	col1.appendChild(input);
	
	var btDel = document.createElement("input");
	btDel.type = 'button';
	btDel.value = "Remover";
	btDel.setAttribute("onClick","removerLinha("+(index-1)+",this.parentNode.parentNode,'add');");
	col2.appendChild(btDel);
	
	var btAdd = document.createElement("input");
	btAdd.type = 'button';
	btAdd.id = "add"+index;
	btAdd.value = "Adicionar";
	btAdd.setAttribute("onClick","this.style.display='none';adicionarArq("+(index+1)+",this);");
	col3.appendChild(btAdd);
		
	var linha = table.insertRow(-1);
	linha.id = "tr"+index;
	
	linha.appendChild(col0);
	linha.appendChild(col1);
	linha.appendChild(col2);
	linha.appendChild(col3);
	linha.appendChild(col4);
	var hidden = document.getElementById("numArquivos");
	hidden.value = Number(hidden.value)+1;
}

function adicionarLink(index){
	var table = document.getElementById("tabArq");
	
	var col0 = document.createElement("TD");
	col0.align = 'center';
	var col1 = document.createElement("TD");
	var col2 = document.createElement("TD");
	var col3 = document.createElement("TD");
	
	var radio = document.createElement("input");
	radio.setAttribute("onClick","verificaPrincipal('numArquivos','principal',this)");
	radio.type = 'radio';
	radio.name = "principal_"+index;
	radio.id = "principal_"+index;
	radio.value = 'S';
	col0.appendChild(radio);
	
	var input = document.createElement("input");
	input.type = 'text';
	input.name = "arq"+index;
	input.id = "arq"+index;
	input.setAttribute("style","width:350px");
	col1.appendChild(input);
	
	var btDel = document.createElement("input");
	btDel.type = 'button';
	btDel.value = "Remover";
	btDel.setAttribute("onclick","removerLinha("+(index-1)+",this.parentNode.parentNode,'add');");
	col2.appendChild(btDel);
	
	var btAdd = document.createElement("input");
	btAdd.type = 'button';
	btAdd.id = "add"+index;
	btAdd.value = "Adicionar";
	btAdd.setAttribute("style","width:75px;");
	btAdd.setAttribute("onclick","this.style.display='none';adicionarLink("+(index+1)+",this);");
	col3.appendChild(btAdd);
		
	var linha = table.insertRow(-1);
	linha.id = "tr"+index;
	
	linha.appendChild(col0);
	linha.appendChild(col1);
	linha.appendChild(col2);
	linha.appendChild(col3);
	var hidden = document.getElementById("numArquivos");
	hidden.value = Number(hidden.value)+1;
}
function removerLinha(index, linha, tipo){
	remover = (index+1);
	aux = (remover+"");
	var hidden = document.getElementById("listaArq");
	i = hidden.value.indexOf(remover+";");
	inicio = hidden.value.substring(0,i);
	fim = hidden.value.substring(i + aux.length+1);
	hidden.value = inicio+fim;
	
	// Antes de remover a linha, exibe o botão de adicionar anterior se este for o último da lista
	var i;
	for (i = index; i > 0; i--){
		if (document.getElementById(tipo+i) != null){
			break;
		}
	}
	var linhas = document.getElementById("tabArq").getElementsByTagName("TR");
	var numArq = document.getElementById("numArquivos");
	numArq.value = Number(numArq.value) - 1;
	if (linhas.item(linhas.length-2) == document.getElementById(tipo+i).parentNode.parentNode){
		document.getElementById(tipo+i).style.display = 'block';
	}
	linha.parentNode.removeChild(linha);	
}
function verificaPrincipal(hidden,principal,componente){
	var valor = document.getElementById(hidden).value;
	var j = 0;
	for (var i = 0; i < valor; i++){
		while ((document.getElementById(principal+"_"+j) == null) && (j < 100)){
			j++;
		}
		var radio = document.getElementById(principal+"_"+j);
		if (radio != componente){
			if (radio.checked){
				radio.checked = false;
			}
		}
		j++;
	}
}
function validaArquivos(){
	Form = document.getElementById("galeria");
	
	var conteudo = remover_espacos(Form.arq0.value);
		
	// Verifica Imagem
	if (conteudo.length == 0){
		if ((Form.desc0 == null) || (Form.desc0.value == "")){
			alert("Insira um arquivo válido!");
			Form.arq0.focus();
			return false;
		}
	}
	j = 0;
	principal = false;
	aux = null;
	var numArqs = Number(Form.numArquivos.value);
	// Procura pelos campos existentes para poder verificar
	// i é o contador de inputs lidos
	// j é o verificador do id do input
	for (i = 0; i < numArqs; i++){
		while ((document.getElementById("arq"+j) == null) && (j < 100)){
			j++;
		}
		var principalA = document.getElementById("principal_"+j);
		if (principalA.checked){
			principal = true;
			aux = j;
		}
		j++;
	}
	// Se principal == true, pelo menos um campo foi selecionado como principal
	if (principal == false){
		alert("Selecione pelo menos um arquivo como principal!");
		Form.principal_0.focus();
		return false;
	} else { // Verifica se o campo selecionado é valido
		if ((document.getElementById("arq"+aux).value.length == 0) && (document.getElementById("desc"+aux).value == "")){
			alert("Selecione um arquivo válido como principal!");
			Form.principal_0.focus();
			return false;
		}
	}
	
	Form.submit();
	return true;
}
function validaLinks(){
	Form = document.getElementById("galeria");
	
	var conteudo = remover_espacos(Form.arq0.value);
		
	// Verifica Imagem
	if (conteudo.length == 0){
		alert("Insira um arquivo válido!");
		Form.arq0.focus();
		return false;
	}
	j = 0;
	principal = false;
	aux = null;
	var numArqs = Number(Form.numArquivos.value);
	// Procura pelos campos existentes para poder verificar
	// i é o contador de inputs lidos
	// j é o verificador do id do input
	for (i = 0; i < numArqs; i++){
		while ((document.getElementById("arq"+j) == null) && (j < 100)){
			j++;
		}
		var principalA = document.getElementById("principal_"+j);
		if (principalA.checked){
			principal = true;
			aux = j;
		}
		j++;
	}
	// Se principal == true, pelo menos um campo foi selecionado como principal
	if (principal == false){
		alert("Selecione pelo menos um arquivo como principal!");
		Form.principal_0.focus();
		return false;
	} else { // Verifica se o campo selecionado é valido
		if ((document.getElementById("arq"+aux).value.length == 0) && (document.getElementById("desc"+aux).value == "")){
			alert("Selecione um arquivo válido como principal!");
			Form.principal_0.focus();
			return false;
		}
	}
	
	Form.submit();
	return true;
}



//desabilita menu de opcoes ao clicar no botao direito
function desabilitaMenu(e)
{
if (window.Event)
{
if (e.which == 2 || e.which == 3)
return false;
}
else
{
event.cancelBubble = true
event.returnValue = false;
return false;
}
}

//desabilita botao direito
function desabilitaBotaoDireito(e)
{
if (window.Event)
{
if (e.which == 2 || e.which == 3)
return false;
}
else
if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnValue = false;
return false;
}
}

//desabilita botao direito do mouse
if ( window.Event )
document.captureEvents(Event.MOUSEUP);
if ( document.layers )
document.captureEvents(Event.MOUSEDOWN);

document.oncontextmenu = desabilitaMenu;
document.onmousedown = desabilitaBotaoDireito;
document.onmouseup = desabilitaBotaoDireito;