function strcmp ( str1, str2 ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Waldo Malqui Silva
    // +      input by: Steve Hilder
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: gorthaur
    // *     example 1: strcmp( 'waldo', 'owald' );
    // *     returns 1: 1
    // *     example 2: strcmp( 'owald', 'waldo' );
    // *     returns 2: -1

    return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
}

function mudaImagem(){
	var tipo = document.getElementById('tipo_index');
	
	if (tipo.value == 1){ 
		document.getElementById('imgPreview').src = 'img/modelo1.gif';
	} else if (tipo.value == 2){
		document.getElementById('imgPreview').src = 'img/logo.png';
	} else if (tipo.value == 3){
		document.getElementById('imgPreview').src = 'img/logo.png';
	}
}

function mostraPreview(){
	
	var acao = document.getElementById('txtPreview');
	
	if (strcmp(acao.innerHTML, 'Pré-visualizar' ) > 0){
		mudaImagem();
		document.getElementById('preview').style.display = 'inline';
		document.getElementById('txtPreview').innerHTML = 'Ocultar vistualiza&ccedil;&atilde;o';
		
	} else {
		document.getElementById('preview').style.display = 'none';
		document.getElementById('txtPreview').innerHTML = 'Pr&eacute;-visualizar';
	}
}


function enviaAlteracao(idProduto,idPedido,coluna,url,retorno){
	
	var linha  = 'item_quant_'+coluna;
	var nrQuantidade = document.getElementById(linha).value;
	var valor  = 'item_valor_'+coluna;
	var nrValor = document.getElementById(valor).value;
	location.href=url+'controller/orcamentoController.php?id='+idProduto+'&pedido='+idPedido+'&nrValor='+nrValor+'&nrQuantidade='+nrQuantidade+'&retorno='+retorno+'&acao=Atualizar';
}

function enviaAlteracaoExterno(idProduto,idPedido,coluna,url,retorno){
	var linha  = 'item_quant_'+coluna;
	var nrQuantidade = document.getElementById(linha).value;
	location.href=url+'controller/orcamentoController.php?id='+idProduto+'&pedido='+idPedido+'&nrQuantidade='+nrQuantidade+'&retorno='+retorno+'&acao=Atualizar';
}

/***********************************
FUNÇÃO GLOBAL QUE VALIDA SE UM CAMPO ESTÁ VAZIO
-- Funciona para textarea, input e select
***********************************/
function validaCampoVazio(objCampo,caminho)
{
	var idCampo 		=   objCampo.id;
	//var NomeMsgCampo 	=  'msg_'+ idCampo;
	//var imgErro 		=   document.getElementById('imgErro').value;
	
	var campo 		=  document.getElementById(idCampo);
	//var msgCampo 	=  document.getElementById(NomeMsgCampo);
	
	
	
	if (campo.value.length == 0){ //Campo vazio
		//var img = '<img src="'+imgErro+'"/> Preenchimento obrigatório.';
		//msgCampo.innerHTML = img;	
		alert('Campo obrigatorio.');
		campo.focus();
		return false;
		
	} else if (objCampo.name.search("mail") > 0){
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(campo.value))){
			alert("E-mail invalido!\nInforme corretamente seu endereco de e-mail!"); 
			campo.value = '';
			campo.focus();
			return false;	
		} else {
			return true;	
		}
		
	} else { //Campo com valor
		//var img = '<img src="http://'+document.location.host+'/portal/img/icones/ok.gif"/>';
		//msgCampo.innerHTML = img;	
		return true;
	}
}

/***********************************
FUNÇÃO GLOBAL QUE VALIDA OS CAMPOS VAZIOS
DE UM FORMULÁRIO
-- Funciona para textarea, input e select
***********************************/
function validaFormularioVazio(objFormulario){
	
	var i = 0;
	for (i=0; i<objFormulario.length; i++){
		
			if (objFormulario.elements[i].style.top == 'auto'){
				
				if (objFormulario.elements[i].type == 'text' || objFormulario.elements[i].type == 'file' || objFormulario.elements[i].type == 'password' || objFormulario.elements[i].type == 'textarea' || objFormulario.elements[i].type == 'select-one'){
					if (!validaCampoVazio(objFormulario.elements[i])){
						return false;
						break;
					} 
				} else {
					
				}
			} 
			
	}
	
				
	return true;
}

/**********************************************
 *	FUNCAO COM TODAS A MAKARAS
 *  OnKeyPress="formatar(this, '#####-###')"
 *  OnKeyPress="formatar(this, '###.###.###-##')"
 *  OnKeyPress="formatar(this, '##/##/####')"
 **********************************************/
function mascara(src, mask)
{
	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);
	
	}
} 

/*
	LIMITA A QUANTIDADE MÁXIMA DE CARACTERES QUE SE PODE DIGITAR EM UM TEXTAREA
	Eder Franco
	14/06/2011
*/

function limitadorCampo(obj, MaxLength, Display, campoMsg) {

	if (MaxLength !=0) {
		if (obj.value.length > MaxLength)  {
			obj.value = obj.value.substring(0, MaxLength);
			alert ("Por favor, limite seu comentario ao maximo de "+ MaxLength +" caracteres.");  
		} 
	}
		
	document.getElementById(Display).innerHTML = obj.value.length + '/' + MaxLength;
	
}

//Excluir um conteúdo
function excluirConteudo(ID){
	if(confirm("Deseja realmente EXCLUIR este item?")){
		location.href='actConteudo.php?acao=Excluir&idConteudo='+ID;
	}
}

//Excluir um conteúdo
function excluirCategoria(ID){
	if(confirm("Deseja realmente EXCLUIR este item?")){
		location.href='actCategoria.php?acao=Excluir&idCategoria='+ID;
	}
}

//Excluir um conteúdo
function excluirMenu(ID){
	if(confirm("Deseja realmente EXCLUIR este item?")){
		location.href='actMenu.php?acao=Excluir&idMenu='+ID;
	}
}

//Excluir um conteúdo
function excluirItem(ID,url,acao,idLabel){
	if(confirm("Deseja realmente EXCLUIR este item?")){
		location.href=''+url+'acao='+acao+'&'+idLabel+'='+ID;
	}
}

//Excluir um conteúdo
function excluirOrcamento(URL){
	if(confirm("Deseja realmente EXCLUIR este item?")){
		location.href=URL;
	}
}

function adicionarOrcamento(URL,idPedido){
	var ID = document.getElementById("idNovoProduto");
	if(ID.value == ""){
		alert('Selecione um produto.');
		ID.focus();
		return false;
	}
	
	if(confirm("Deseja realmente ADICIONAR este item?")){
		
		location.href=URL+'?idProduto='+ID.value+'&idPedido='+idPedido+'&acao=AdicionarAdmin';
	}
}

function limpaCampo(){
	document.getElementById('campoBusca').value = '';
}

function preencheCampo(){
	if (document.getElementById('campoBusca').value == ''){
		document.getElementById('campoBusca').value = 'Buscar Produtos';
	}
}

function tiraCaracteres(original){
	exp = /\-|\.|\/|\(|\)| /g
	novaString = original.replace(exp, "");
	return novaString;
}

function limpaStringNumerica(objCampo,campoDest){
	var original = objCampo.value;
	stringLimpa = tiraCaracteres(original);
	document.getElementById(campoDest).value = stringLimpa;
}

function trataTelefone(objCampo,campoDD,campoTel){
	var original = objCampo.value;
	stringLimpa = tiraCaracteres(original);
	var tamanho = stringLimpa.length;
	var DDD = stringLimpa.substr(0,2);
	var Telefone = stringLimpa.substr(2,tamanho);
	document.getElementById(campoDD).value = DDD;
	document.getElementById(campoTel).value = Telefone;
}

function concatenaCampos(campo1,campo2){
	var textoCampo1 = campo1.value;
	var textoCampo2 = document.getElementById(campo2).value;
	document.getElementById(campo2).value = '- Cliente: '+textoCampo2+'<br/>- Loja: '+textoCampo1;
}


function mudaTituloPagina(textoAtual,nomeSecao,textoAdicional){
	
	var novoNomeSite = textoAtual + ' - ' + nomeSecao +': ' + textoAdicional;
	document.getElementById('tituloSite').innerHTML = novoNomeSite;
	
}


function FormataCPFCampo(campo, teclapres)
{
	var tecla = teclapres.keyCode;
	var vr = new String(campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;
	if (tecla != 14)
	{
		if (tam == 4)
			campo.value = vr.substr(0, 3) + '.';
		if (tam == 7)
			campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 6) + '.';
		if (tam == 11)
			campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(7, 3) + '-' + vr.substr(11, 2);
	}
}

function validarCPF(cpf,nomeCampo){
	
	var cpf = cpf.value;
	
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     alert("CPF invalido. Tente novamente.");
	 document.getElementById(nomeCampo).focus();
	 document.getElementById(nomeCampo).innerHTML = '';
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  alert("CPF invalido. Tente novamente.");
	  document.getElementById(nomeCampo).focus();
	  document.getElementById(nomeCampo).innerHTML = '';
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 alert("CPF invalido. Tente novamente.");
	 document.getElementById(nomeCampo).focus();
	 document.getElementById(nomeCampo).innerHTML = '';
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     alert("CPF invalido. Tente novamente.");
	 document.getElementById(nomeCampo).focus();
	 document.getElementById(nomeCampo).innerHTML = '';
	 return false;
   }
   return true;
 }
 
 function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }
 
 function flickr(user){
	$.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?id='+user+'&format=json&jsoncallback=?', function(data){
		for(i=0; i<12; i++){
			$("<img />").attr("src", data.items[i].media.m).appendTo("#flickr").wrap("<a href='" + data.items[i].link + "' title='" + data.items[i].title + "' target='_blank'><\/a>");
		}
	});
}




