var baseText = null;
var color = 0;
var suma = 10;

/**********************************************/
/* MOSTRAR/OCULTAR ENVIAR AMIGO ***************/
/**********************************************/
var http = getHTTPObject();
	
function getHTTPObject() {
	var http = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try {http = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
			try {http = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (E) {http = false;}
		}
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try {http = new XMLHttpRequest();}
		catch (e) {http = false;}
	}
	return http;
}

function handler() {//Call a function when the state changes.
	if (http.readyState == 4 && http.status == 200) {
		
		if (http.responseText == "TRUE") { alert("El e-mail fue enviado con éxito."); }
		else { alert("Hubo un problema al enviar el correo."); }
	}
}

function enviarAmigo() {
	
	var eDest = true;
	var eOrig = true;
	var nombre = true;
	var ret = true;
	
	if (!validarEmail(document.getElementById("txtEmailDest").value)) { eDest = false; ret = false; }
	if (!validarEmail(document.getElementById("txtEmailOrig").value)) { eOrig = false; ret = false; }
	if (!validarString(document.getElementById("txtNombre").value)) { nombre = false; ret = false; }
	
	//Determina el color del texto teniendo en cuenta si hay error o no en el input.
	if (!eDest) { document.getElementById("txtEmailDest").style.color = "#FF0000"; }
	else { document.getElementById("txtEmailDest").style.color = "#666666"; }
	if (!eOrig) { document.getElementById("txtEmailOrig").style.color = "#FF0000"; }
	else { document.getElementById("txtEmailOrig").style.color = "#666666"; }
	if (!nombre) { document.getElementById("txtNombre").style.color = "#FF0000"; }
	else { document.getElementById("txtNombre").style.color = "#666666"; }
	
	//Si los valores de los campos son correctos establece los valores que se enviarán por e-mail.
	if (ret) {
		var url = document.getElementById("hidUrlNota");
		url.value = window.location.href;
		
		var titulo = document.getElementById("nota_titulo").innerHTML;
		document.getElementById("hidTxtTitulo").value = titulo;

		var bajada = document.getElementById("nota_bajada").innerHTML + "..";
		document.getElementById("hidTxtBajada").value = bajada;
		
		var action = "../../amigo_enviar.php";
		var params = "hidUrlNota=" + url.value + "&hidTxtTitulo=" + titulo + "&hidTxtBajada=" + bajada + "&txtEmailDest=" + document.getElementById("txtEmailDest").value + "&txtEmailOrig=" + document.getElementById("txtEmailOrig").value + "&txtNombre=" + document.getElementById("txtNombre").value;
		if (document.getElementById('chkEnviarCopia').checked == 1) { params = params + "&chkEnviarCopia=1"; }
	
		http.open("POST", action, true);
	
		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
	
		http.onreadystatechange = handler;
		http.send(params);
	}
	else {
		//Hace foco en el campo a corregir.
		if (!eDest) { document.getElementById("frmEnviarAmigo").txtEmailDest.focus(); }
		else if (!nombre) { document.getElementById("frmEnviarAmigo").txtNombre.focus(); }
		else if (!eOrig) { document.getElementById("frmEnviarAmigo").txtEmailOrig.focus(); }
	}
}

function validarEmail(email) {
	//Separa la direccion de email en dos y chequea que user y domain esten bien formados.
	if (email.indexOf("@") <= 0) { return false; }
	else {
		var user = email.substr(0, email.indexOf("@") + 1);
		var domain = email.substr(email.indexOf("@") + 1);
		
		userExp = /^[a-z_0-9\-]+(\.[a-z_0-9\-]+)*@/i;
		domainExp = /^[a-z_0-9\-]+(\.[a-z_0-9\-]+)*\.([a-z]{2,4})$/i;
		
		if (userExp.test(user) && domainExp.test(domain)) { return true; }
		else { return false; }		
	}
}

function validarString(str) {
	var strexp = /^[a-zñÑáÁéÉíÍóÓúÚ]+[a-zñÑáÁéÉíÍóÓúÚ\s]{0,}$/i;
	if (strexp.test(str)) { return true; }
	else { return false; }
}

function hideEnviarAmigo() {
	var popUp = document.getElementById("amigo_content");
	
	color -= suma;

	if (color >= 0) {
		popUp.style.filter = 'alpha(opacity='+color+')';
		popUp.style.opacity = color / 100;
		popUp.style.MozOpacity = color / 100;
		popUp.style.KHTMLOpacity = color / 100;
		window.setTimeout("hideEnviarAmigo();", 50);
	}
	else {
		popUp.style.display = "none";
		document.getElementById("disable_layer").style.display = "none";
	}
}

function showEnviarAmigo() {

	document.getElementById("disable_layer").style.display = "block";

	var popUp = document.getElementById("amigo_content");
	
	popUp.style.display = "block";

	color += suma;

	var scrolledX, scrolledY; 
	
	if( self.pageYoffset ) { 
		scrolledX = self.pageXoffset; 
		scrolledY = self.pageYoffset; 
	}
	else if( document.documentElement && document.documentElement.scrollTop ) { 
		scrolledX = document.documentElement.scrollLeft; 
		scrolledY = document.documentElement.scrollTop; 
	}
	else if( document.body ) { 
		scrolledX = document.body.scrollLeft; 
		scrolledY = document.body.scrollTop; 
	} 
	
	var point = window.center({width:400,height:230});
	var t = point.y;
	var l = point.x;
	
	popUp.style.top = t + "px";
	popUp.style.left = l + "px";
	
	if (!(color >= 110)) {
		popUp.style.filter = 'alpha(opacity='+color+')';
		popUp.style.opacity = color / 100;
		popUp.style.MozOpacity = color / 100;
		popUp.style.KHTMLOpacity = color / 100;
		window.setTimeout("showEnviarAmigo();", 50);
	}
}

/**********************************************/
/* MOSTRAR/OCULTAR CUADRO *********************/
/**********************************************/
var cuadro_activo = '';

function hideCuadro() {
	
	var popUp = document.getElementById("cuadro_content");
		
	color -= suma;

	if (color >= 0) {
		popUp.style.filter = 'alpha(opacity='+color+')';
		popUp.style.opacity = color / 100;
		popUp.style.MozOpacity = color / 100;
		popUp.style.KHTMLOpacity = color / 100;
		window.setTimeout("hideCuadro();", 50);
	}
	else {
		var txt = document.getElementById(cuadro_activo);
		txt.style.display = "none";
		popUp.style.display = "none";
		cuadro_activo = '';
		document.getElementById("disable_layer").style.display = "none";
	}
}

function showCuadro(cuadro) {
	
	document.getElementById("disable_layer").style.display = "block";
		
	var popUp = document.getElementById("cuadro_content");

	popUp.style.display = "block";

	var txt = document.getElementById(cuadro);
	txt.style.display = "block";
	
	color += suma;

	var scrolledX, scrolledY; 
	
	if( self.pageYoffset ) { 
		scrolledX = self.pageXoffset; 
		scrolledY = self.pageYoffset; 
	}
	else if( document.documentElement && document.documentElement.scrollTop ) { 
		scrolledX = document.documentElement.scrollLeft; 
		scrolledY = document.documentElement.scrollTop; 
	}
	else if( document.body ) { 
		scrolledX = document.body.scrollLeft; 
		scrolledY = document.body.scrollTop; 
	} 
	
	var point = window.center({width:500,height:450});

	popUp.style.top = point.y + "px";
	popUp.style.left = point.x + "px";
	
	if (!(color >= 110)) {
		popUp.style.filter = 'alpha(opacity='+color+')';
		popUp.style.opacity = color / 100;
		popUp.style.MozOpacity = color / 100;
		popUp.style.KHTMLOpacity = color / 100;
		window.setTimeout("showCuadro('" + cuadro + "');", 50);
	}
	
	cuadro_activo = cuadro;
}


/**********************************************/
/* PARA CENTRAR UN ELEMENTO EN LA PAGINA ******/
/**********************************************/
window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.center = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var _x = 0;
	var _y = 0;
	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	_x = ((this.size().width-hWnd.width)/2)+offsetX;
	_y = ((this.size().height-hWnd.height)/2)+offsetY;

	return{x:_x,y:_y};
}

/**************************************************/
/* Selecciona o deselecciona todos los Checkboxes */
/**************************************************/
function CheckAll(field) {
	var elems = field.elements;
	for( i = 0; i < elems.length; i++) { 
		var n = elems[i].name; 
		if (n.substr(0, 3) == "chk") { elems[i].checked = true ; }
	}
	return false;
}

function UncheckAll(field) {
	var elems = field.elements;
	for (i = 0; i < field.length; i++) {
		var n = elems[i].name; 
		if (n.substr(0, 3) == "chk") { elems[i].checked = false ; }
	}
	return false;
}


/***************************************************************************/
/* Funciones necesarias para cambiar las noticias que se muestran del blog */
/***************************************************************************/
var oldColor = 100;
var variacionColor = 10;
var notaActiva = 0;

var secs
var timerID = null;
		
function InitializeTimer() {
	secs = 6;
	StartTheTimer();
}

function StartTheTimer() {
	if (secs == 0) {
		AvanzarNoticiaBlog();
		secs = 6;
		timerID = self.setTimeout("StartTheTimer()", 1000);
	}
	else {
		secs = secs - 1;
		timerID = self.setTimeout("StartTheTimer()", 1000);
	}
}

function AvanzarNoticiaBlog() {
	var oldDivID = document.getElementById("blog_news_" + notaActiva);
	if (notaActiva == 5) { var newDivID = document.getElementById("blog_news_0"); }
	else { var newDivID = document.getElementById("blog_news_" + (notaActiva + 1)); }
	
	oldColor -= variacionColor;

	if (oldColor >= 0) {
		oldDivID.style.filter = 'alpha(opacity='+oldColor+')';
		oldDivID.style.opacity = oldColor / 100;
		oldDivID.style.MozOpacity = oldColor / 100;
		oldDivID.style.KHTMLOpacity = oldColor / 100;

		window.setTimeout("AvanzarNoticiaBlog();", 50);
	}
	else {
		oldDivID.style.display = "none";
		// Hace que el nuevo DIV sea siempre visible
		newDivID.style.display = "block";
		// Si esto no esta, en la segunda vuelta de noticias, son todas transparentes y no se ven
		newDivID.style.filter = 'alpha(opacity='+100+')';
		newDivID.style.opacity = 10;
		newDivID.style.MozOpacity = 10;
		newDivID.style.KHTMLOpacity = 10;

		oldColor = 100;
		if (notaActiva != 5) { notaActiva += 1; }
		else { notaActiva = 0; }
	}
	
	secs = 6; // Resetea el tiempo que falta para cambiar la noticia cada vez que se ejecuta esta funcion.
}

function RetrocederNoticiaBlog() {
	var oldDivID = document.getElementById("blog_news_" + notaActiva);
	if (notaActiva == 0) { var newDivID = document.getElementById("blog_news_5"); }
	else { var newDivID = document.getElementById("blog_news_" + (notaActiva - 1)); }
	
	oldColor -= variacionColor;

	if (oldColor >= 0) {
		oldDivID.style.filter = 'alpha(opacity='+oldColor+')';
		oldDivID.style.opacity = oldColor / 100;
		oldDivID.style.MozOpacity = oldColor / 100;
		oldDivID.style.KHTMLOpacity = oldColor / 100;

		window.setTimeout("RetrocederNoticiaBlog();", 50);
	}
	else {
		oldDivID.style.display = "none";
		// Hace que el nuevo DIV sea siempre visible
		newDivID.style.display = "block";
		// Si esto no esta, en la segunda vuelta de noticias, son todas transparentes y no se ven
		newDivID.style.filter = 'alpha(opacity='+100+')';
		newDivID.style.opacity = 10;
		newDivID.style.MozOpacity = 10;
		newDivID.style.KHTMLOpacity = 10;

		oldColor = 100;
		if (notaActiva != 0) { notaActiva -= 1; }
		else { notaActiva = 5; }
	}
	
	secs = 6; // Resetea el tiempo que falta para cambiar la noticia cada vez que se ejecuta esta funcion.
}

///////////////////////
function CambiarSrc() {
	var obj_img = document.getElementById('imgSec');
	var obj_file = document.getElementById('fileImgSec');

	var tmp1 = obj_file.value;
	var tmp2 = tmp1.substr(tmp1.indexOf(":") + 30);
	var tmp3 = tmp2.replace(/\\/g, "/");
	var url = "http://localhost/visionsustentable/" + tmp3;
	obj_img.src = url;
}

function GenerarLink() {
	var obj_link = document.getElementById('txtLink');
	var obj_titulo = document.getElementById('txtTitulo');
	
	obj_link.value = obj_titulo.value.toLowerCase().replace(/\s/g, '-');
	obj_link.value = QuitarAcentos(obj_link.value);
	obj_link.value = QuitarNoAlfanumericos(obj_link.value);
	obj_link.value =  obj_link.value + ".php";
}

function QuitarAcentos(str) {
	str = str.replace(/[\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6]/g,'a'); //A's y a's especiales
	str = str.replace(/[\u00E8\u00E9\u00EA\u00EB]/g,'e'); //E's y e's especiales
	str = str.replace(/[\u00EC\u00ED\u00EE\u00EF]/g,'i'); //I's y i's especiales
	str = str.replace(/[\u00F2\u00F3\u00F4\u00F5\u00F6]/g,'o'); //O's y o's especiales
	str = str.replace(/[\u00F9\u00FA\u00FB\u00FC]/g,'u'); //U's y u's especiales
	str = str.replace(/[\u00F1]/g,'n'); //Ñ y ñ
	return str;
}

function QuitarNoAlfanumericos(str) {
		str = str.replace(/[^\d\w\-]/g, "");
		return str;
}

function AddTextareaForParrafo() {
	var lastItem = document.getElementById("divFinal");
	var y = document.createElement('TEXTAREA');
	var z = lastItem.parentNode;
	z.insertBefore(y,lastItem);
//	z.removeChild(obj);
	y.className = "nota_texto";
	y.style.overflow = "hidden";
	y.style.width = "555px";
	y.style.border = "0px";
	y.style.fontSize = "12px";
	y.style.fontFamily = "Arial";
	y.onkeyup = function() { y.style.height = y.scrollHeight + 'px'; }
	y.focus();
}

function AddTextareaForSubtitulo() {
	var lastItem = document.getElementById("divFinal");
	var y = document.createElement('TEXTAREA');
	var z = lastItem.parentNode;
	z.insertBefore(y,lastItem);
//	z.removeChild(obj);
	y.className = "nota_subtitulo_ver_1";
	y.style.overflow = "hidden";
	y.style.width = "555px";
	y.style.border = "0px";
	y.style.fontSize = "18px";
	y.style.fontFamily = "Georgia, 'Times New Roman', Times, serif";
	y.style.color = "#98B928";
	y.style.padding = "0px 5px 0px 5px";
	
	y.onkeyup = function() { y.style.height = y.scrollHeight + 'px'; }
	
	d = document.createElement('DIV');
	z.insertBefore(d, lastItem);
	d.className = "padtop15px";

	y.focus();
}

function AddImg() {
	var lastItem = document.getElementById("divFinal");
	var y = document.createElement('DIV');
	var z = lastItem.parentNode;
	z.insertBefore(y,lastItem);
	y.className = "sucesos_div_foto";
	
	a = document.createElement('DIV');
	y.appendChild(a);
	
	b = document.createElement('IMG');
	a.appendChild(b);
	b.className = "sucesos_img_gr img_250x540";
	
	c = document.createElement('TEXTAREA');
	y.appendChild(c);
	c.className = "sucesos_epig epig_535";
	c.style.overflow = "hidden";
	c.style.height = c.scrollHeight + 'px';
	
	d = document.createElement('DIV');
	z.insertBefore(d, lastItem);
	d.className = "padtop15px";
}
