$(document).ready(
	function() {
		trace('main-com');
	}
);
function getCategorias(success){
	try{
		var valores ="'accion':'sel',";
		valores+="'id':'0'";		

		valores='_p='+encode(escape('{'+valores+'}'));
				
		url='./src/controller/menu.php';

		$.ajax({
		    url: url,
		    data: valores,
		    success: function(response){
				success(parsearJSON(decode(response)));
			}
		});
	}catch(err){
		errorMsg(err,arguments);
		return null;
	}
}


function enviarMensaje(){
	try{
		$('#bt-send').attr("disabled", "true");
		var ok=true;
		jsonDatos=eval('({"campos":['+
		'{"nombre":"Name","campo":"contact_name","validacion":"B"},'+
		'{"nombre":"E-mail","campo":"contact_mail","validacion":"BE"},'+		
		'{"nombre":"Phone","campo":"contact_phone","validacion":"B"},'+
		'{"nombre":"Message","campo":"contact_message","validacion":"B"},'+
		']})');
		
		
		for(var i=0;jsonDatos.campos.length>i;i++){
			var mensaje='';
			if(jsonDatos.campos[i]==null){break;}//para IE falla el for al procesar javascript	
			$('#'+jsonDatos.campos[i].campo).attr("class","field");
			if(jsonDatos.campos[i].validacion.indexOf('B')>-1){ 
				if(esVacio ($('#'+jsonDatos.campos[i].campo).val())){
					$('#'+jsonDatos.campos[i].campo).attr("class","field-error");
					mensaje+='*';
					ok=false;
				}
			}
			if(jsonDatos.campos[i].validacion.indexOf('E')>-1){ 
				if(!validarEmail ($('#'+jsonDatos.campos[i].campo).val())){
					$('#'+jsonDatos.campos[i].campo).attr("class","field-error");
					if(mensaje=='')
					mensaje+='*';
					else
					mensaje+=' | invalid email';
					ok=false;
				}
			}
			/*if(mensaje!=''){ 
				
				$('<label style="float:right; position:inherit; font-family:verdana; font-size:9px; color:#666;" id="err'+jsonDatos.campos[i].campo+'">'+mensaje+'</label>')
				.insertAfter('#'+jsonDatos.campos[i].campo)
				.hide()
				.fadeIn(500)
				.delay(2500)
				.fadeOut(500,
				function(){
					$(this).remove();
					$('#bt-send').removeAttr("disabled");
				});
				
			}*/
		}

		if(!ok){
			return;
		}

	}catch(e){
		alert(e);
	}
	
	var mensaje ='<html>';
	mensaje +='<head>';
	mensaje +='<meta http-equiv=Content-Type content="text/html; charset=UTF-8">';
	mensaje +='</head>';
	mensaje +='<body>';
	mensaje += '<div style="background-color: #333; border-bottom:1px solid rgb(50, 50, 50);width:100%;height:50px"><img src="http://www.ceramicaformas.com/css/images/header_mail.jpg" /></div><br/>';
	mensaje += '<b>Nombre:</b> '+$('#contact_name').val()+'<br/><br/>';
	mensaje += '<b>Email:</b> '+$('#contact_mail').val()+'<br/><br/>';//+' - '+$('#contacto_localidad').val()+' ('+$('#contacto_CP').val()+')'
	mensaje += '<b>Telefono:</b> '+$('#contact_phone').val()+'<br/><br/>';
	mensaje += '<b>Mensaje:</b> '+$('#contact_message').val();
	mensaje += '<br/><br/>www.ceramicaformas.com';
	mensaje +='</body>';
	mensaje +='</html>';
	
	var valores ="'accion':'enviarMail',";
	valores+="'asunto':'"+$('#contact_name').val()+"',";
	valores+="'mensaje':'"+escape(mensaje)+"',";
	valores+="'email':'no-reply@ceramicaformas.com',";
	valores+="'name':'Ceramica Formas'";

	valores='_p='+encode(escape('{'+valores+'}'));

	url='./src/controller/mail.php';

	$.ajax({
	    url: url,
	    data: valores,
	    success: function(response){
			//alert(response)
			//alert(decode(response))
			var json=parsearJSON(decode(response))
	    	if(json.resultado=='OK') 
				msg('Mensaje Enviado, muchas gracias.');
	    	else 
				msg('No se pudo enviar el mensaje, disculpe las molestias ocasionadas.');
				
	    	$('#bt-send').removeAttr("disabled");
		}
	});
}

function msg(txt){
	var h=50
	var w=200

	if($('#alertBox').attr('id')!='alertBox'){
		var s='<div id="alertBox" style="'
		s+='background:#dddddd;'
		s+='border:solid 1px #555555;'
		s+='font-family:trebuchet ms,arial,sans-serif;'
		s+='font-size:10pt;'
		s+='color:#222222;'
		s+='padding:20px;'
		s+='text-align:center;'
		s+='position:absolute;'
		s+='z-index:9999;'				
		s+='margin-top:'+($('body').height()-h-40)/2+'px;'
		s+='margin-left:'+($('body').width()-w-40)/2+'px;'
		s+='height:'+h+'px;'
		s+='width:'+w+'px;'
		s+='"></div>'
		$('body').prepend(s);
		$('#alertBox').hide();
	}else{
		$('#alertBox').hide();
	}
	$('#alertBox').html(txt);
	$('#alertBox').fadeIn(600).delay(1500).fadeOut(600); 
}

function getFilesID(carpeta, id, success){
	var valores ="'accion':'selFiles',";
	valores+="'carpeta':'"+carpeta+"'";

	valores='_p='+encode(escape('{'+valores+'}'));
	
	
	var url='./src/controller/util.php';	
	
	$.ajax({
		url: './src/controller/util.php',
		data: valores,
		success: function(response){
			success(id, parsearJSON(decode(response)));
		}
	});
}

function validarEmail(str) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str))
	{
		return (true);
  	} 
  	else 
  	{
     	return (false);
  	}
}


function getSeccion(id,success){
	try{
		var valores ="'accion':'sel',";
		valores+="'id':'"+id+"'";

		valores='_p='+encode(escape('{'+valores+'}'));
				
		url='./src/controller/seccion.php';

		$.ajax({
		    url: url,
		    data: valores,
		    success: function(response){
				success(parsearJSON(decode(response)));
			}
		});
	}catch(err){
		errorMsg(err,arguments);
		return null;
	}
}

function getHTML(url,convert,success){
	try{
		var valores ="'accion':'getHTML',";
		valores+="'url':'"+url+"',";
		valores+="'convert':'"+convert+"'";
	
		valores='_p='+encode(escape('{'+valores+'}'));
		
		url='./src/controller/util.php';
		$.ajax({
		    url: url,
		    data: valores,
			//type: 'GET',
		    success: function(response){
				success(parsearJSON(decode(response)));
			}
		});
	}catch(err){
		errorMsg(err,arguments);
		return null;
	}
}

function getLinksGrupo(success){
	try{
		var valores ="'accion':'sel',";
		valores+="'id':'0'";		

		valores='_p='+encode(escape('{'+valores+'}'));
				
		url='./src/controller/link_grupo.php';

		$.ajax({
		    url: url,
		    data: valores,
		    success: function(response){
				success(parsearJSON(decode(response)));
			}
		});
	}catch(err){
		errorMsg(err,arguments);
		return null;
	}
}

function getLinks(grupoId, success){
	try{
		var valores ="'accion':'selxGrupo',";
		valores+="'grupoId':'"+grupoId+"'";		

		valores='_p='+encode(escape('{'+valores+'}'));
				
		url='./src/controller/link.php';

		$.ajax({
		    url: url,
		    data: valores,
		    success: function(response){
				success(parsearJSON(decode(response)));
			}
		});
	}catch(err){
		errorMsg(err,arguments);
		return null;
	}
}

function getCatalogo(success){
	try{
		var valores ="'accion':'sel',";
		valores+="'id':'0'";		

		valores='_p='+encode(escape('{'+valores+'}'));
				
		url='./src/controller/productos.php';

		$.ajax({
		    url: url,
		    data: valores,
		    success: function(response){
				success(parsearJSON(decode(response)));
			}
		});
	}catch(err){
		errorMsg(err,arguments);
		return null;
	}
}

