var xmlHttp;
var sent = false;
var t;
var actualContent = '';
var loadingMsg = "Enviando...";
var goodMsg = "&iexcl; Gracias por comunicarse !";
var badMsg = "Verifique los campos marcados en rojo";
var NOPOSTS = '<b style="font-size: 200%;">No se han encontrado entradas.</b>';
var inner = '';
var _SPEED_FADEOUT = 500;
var _SPEED_FADEIN = 500;	
var lastSearch = '';

function popup(msg,fade,color){
	Mcolor = color || "#FFF";
	jQuery.blockUI({ css: { 
		border: 'none', 
		padding: '20px', 
		backgroundColor: '#000', 
		fontSize: '16px', 
		'-webkit-border-radius': '10px', 
		'-moz-border-radius': '10px', 
		opacity: '.9', 
		color: Mcolor
 },message: msg });
 
 if(fade==true) fade = 1500;
 if(fade>0) setTimeout(jQuery.unblockUI, fade); 
}

function validate(status,id){
	if(status == 0){
		document.getElementById(id).style.borderColor = '#f00';
		document.getElementById(id).style.color= '#f00';
	}else{
		document.getElementById(id).style.borderColor = '#666';
		document.getElementById(id).style.color= '#333';
	}
}

function sendForm(){
	if(sent) return;
	popup(loadingMsg);
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ('Tu explorador no soporta AJAX.');
		return;
	} 
	var params = "nombre="+escape(document.getElementById('nombre').value)+"&email="+escape(document.getElementById('email').value)+"&tel="+escape(document.getElementById('tel').value)+"&empresa="+escape(document.getElementById('empresa').value)+"&mensaje="+escape(document.getElementById('mensaje').value);
	xmlHttp.onreadystatechange = doSendForm
	xmlHttp.open("POST","validate.php",true)
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params)
} 

function doSendForm(){ 
	if (xmlHttp.readyState==4){ 
		var r = xmlHttp.responseText;
		var f1 = r.substr(0,1);
		var f2 = r.substr(1,1);
		var f3 = r.substr(2,1);
		var f4 = r.substr(3,1);
		var f5 = r.substr(4,1);
		
		validate(f1,"nombre");
		validate(f2,"email");
		validate(f3,"tel");
		validate(f4,"empresa");
		validate(f5,"mensaje");
		
		if(f1 == 1 && f2 == 1 && f3 == 1 && f4 == 1 && f5 == 1){
			sent = true;
			popup(goodMsg,true);
			document.getElementById('messageSent').style.display = 'block';
		}else{
			popup(badMsg,true,"#f00");
		}
	}
}

function GetXmlHttpObject(){var xmlHttp=null;try{xmlHttp=new XMLHttpRequest();}catch(e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}return xmlHttp;}

function cargar(){
	if(document.getElementById('inputTerm') != null){
		inner = document.getElementById('col-blog-center').innerHTML;
		if(inner.trim() == ''){
			document.getElementById('col-blog-center').innerHTML = NOPOSTS;
		}
		hide('close');
		hide('loading');
		show('unclose');
		document.getElementById('inputTerm').value = '';
		inner = document.getElementById('col-blog-center').innerHTML;
		actualContent = inner;
	}
}

function cerrar(){
	hide('close');
	hide('loading');
	show('unclose');
	lastSearch = '';
	document.getElementById('inputTerm').value = '';
	jQuery('#col-blog-center').slideUp(_SPEED_FADEOUT,function(){
		document.getElementById('col-blog-center').innerHTML = actualContent;
		jQuery('#col-blog-center').slideDown(_SPEED_FADEIN);
	});
}

function show(id){
	document.getElementById(id).style.display = 'block';
}
function hide(id){
	document.getElementById(id).style.display = 'none';
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function sendAjax(){
	clearTimeout(t);
	t = setTimeout('sendAjax2();',500);
}

function sendAjax2(){
	var text = document.getElementById('inputTerm').value;
	if(lastSearch.trim() == text.trim()) return;
	lastSearch = text;
	hide('unclose');
	hide('close');
	show('loading');

	if(text.trim() == ''){
			cerrar();
			return;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ('Tu explorador no soporta AJAX !');
		return;
	} 
	var params = "term="+text;
	xmlHttp.onreadystatechange = doSearch;
	xmlHttp.open("POST","buscar.php",true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
} 

function doSearch(){ 
	if (xmlHttp.readyState==4){ 
		var r = xmlHttp.responseText;
		if(r.trim() == ''){
			r = NOPOSTS;
		}
		inner = document.getElementById('col-blog-center').innerHTML;
		hide('loading');
		hide('unclose');
		show('close');
		
		if(r.trim() == inner.trim()) return;
		jQuery('#col-blog-center').slideUp(_SPEED_FADEOUT,function(){
			document.getElementById('col-blog-center').innerHTML = r;
			jQuery('#col-blog-center').slideDown(_SPEED_FADEIN);
		});
	}
}

// Blog functions
var pid;

function comentar(redirID){
	pid = document.getElementById('postid').value;
	if(sent) return;
	popup(loadingMsg);
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ('Tu explorador no soporta AJAX !');
		return;
	}
	var params = "name="+escape(document.getElementById('name').value)+"&email="+escape(document.getElementById('mail').value)+"&url="+escape(document.getElementById('url').value)+"&comment="+escape(document.getElementById('comment').value)+"&postid="+pid;
	xmlHttp.onreadystatechange = doComentar;
	xmlHttp.open("POST","validate.php",true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function doComentar(){ 
	if (xmlHttp.readyState==4){
		var r = xmlHttp.responseText;
		
		var f1 = r.substr(0,1);
		var f2 = r.substr(1,1);
		var f3 = r.substr(2,1);
		
		validate(f1,"name");
		validate(f2,"mail");
		validate(f3,"comment");
		
		if(f1 == 1 && f2 == 1 && f3 == 1){
			sent = true;
			popup("Comentario enviado, lo revisaremos para ponerlo online.",4000,'#00dd00');
			redir('post.php?id='+pid,4500);
		}else{
			popup(badMsg,true,"#f00");
		}
	}
}

function redir(url,timeout){
	setTimeout('doRedir(\''+url+'\')',timeout);
}
function doRedir(url){
	window.location = url;
}

// MY CYCLE

var actual = 1;
var maxSlides;

function myCycle(){
	maxSlides = document.getElementById('max_slides').value;
	jQuery('#s1>img').hide().css({position:'absolute'});
	jQuery('#s1>a').hide().css({position:'absolute'});
	jQuery('#s1 img:first').show();
	setTimeout('myCycle_ShowLabel()',1500);
}

function myCycle_ShowLabel(){
	jQuery('#i_'+actual+'_2').fadeIn();
	setTimeout('myCycle_HideSlide()',3000);
}

function myCycle_HideSlide(){
	jQuery('#i_'+actual+'_1').fadeOut();
	jQuery('#i_'+actual+'_2').fadeOut();
	
	actual++;
	if(actual == maxSlides) actual = 1;
	
	jQuery('#i_'+actual+'_1').fadeIn();
	setTimeout('myCycle_ShowLabel()',1500);
}
