// JavaScript Document
//
// Copyright 2006 Nelson Daza. nelson.daza@gmail.com. All rights reserved.
// DOM HTML 
// ----------------------------------------------------
//
// Functions.js
//
//	version 2.1.8.4 major.minor[.revision[.build]]
//

/* START Pop-up Windows Script */

function popup(url, name, width, height, posx, posy, features) {
	if (typeof (posx) == 'undefined')
		posx = 'center';
	if (typeof (posy) == 'undefined')
		posy = 'middle';
	if (typeof (features) == 'undefined')
		features = '';

	if (posx.toLowerCase() == 'center')
		posx = (screen.width - width) / 2;
	else if (posx.toLowerCase() == 'right')
		posx = (screen.width - width - 30);
	else if (posx < 0)
		posx = screen.width - width + posx;
	else
		posx = 0;

	if (posy.toLowerCase() == 'middle')
		posy = (screen.height - height) / 2;
	else if (posy.toLowerCase() == 'bottom')
		posy = (screen.height - height - 60);
	else if (posy < 0)
		posx = screen.height - height - posy - 30;
	else
		posy = 0;

	if (typeof (name) == 'undefined' || typeof (width) == 'undefined'
			|| typeof (height) == 'undefined')
		return open(url);
	else
		return open(url, name, 'width=' + width + ',height=' + height
				+ ',screenX=' + posx + ',left=' + posx + ',screenY=' + posy
				+ ',top=' + posy + ',' + features);
}

/* END Pop-up Windows Script */

function validateEmail(email) {
	var pos = 0;
	if (email.length > 7 && email.indexOf(' ', 0) == -1) {
		pos = email.indexOf('@', 0);
		if (pos > 2 && email.indexOf('@', pos)
				&& email.indexOf('.', pos) > (pos + 2)) {
			pos = email.indexOf('.', pos);
			if (pos > -1 && pos < email.length - 2)
				return true;
		}
	}
	return false;
}

var font_size = 9;
var MAX = 17;
var MIN = 11;

function changeFont(num) {
	font_size += num;
	if (font_size > MAX)
		font_size = MAX;
	if (font_size < MIN)
		font_size = MIN;
	document.getElementsByTagName('body')[0].style.fontSize = font_size + 'px';
}

function toObject(something) {
	if (typeof (something) == 'string')
		return document.getElementById(something);
	if (typeof (something.nodeName) != 'undefined')
		return something;
	return null;
}

function jumpTo(url) {
	document.location.href = url;
}

function hideElement(something) {
	something = toObject(something);
	if (something) {
		something.style.visibility = 'hidden';
		something.style.display = 'none';
	}
}

function showElement(something) {
	something = toObject(something);
	if (something) {
		something.style.visibility = 'visible';
		something.style.display = 'block';
	}
}

function changeVisibility(something) {
	something = toObject(something);
	if (something) {
		if (something.style.visibility == 'visible')
			hideElement(something);
		else
			showElement(something);
	}
}

function setElementPosition(something, posx, posy) {
	something = toObject(something);
	something.style.position = 'absolute';
	something.style.left = posx + 'px';
	something.style.top = posy + 'px';
}

function isDate(year, month, day) {
	if (year.toString().length == 0)
		return false;
	if (day == null) {
		month = (month == null) ? '-' : month;
		if (year.toString().indexOf(month) == -1)
			return false;

		var arr = year.toString().split(month);
		if (arr.length != 3)
			return false;
		year = arr[0];
		month = arr[1];
		day = arr[2];
	}
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year, month, day);
	var nyear = (tempDate.getYear() < 1000) ? tempDate.getYear() + 1900
			: tempDate.getYear()

	return ((nyear == year) && (month == tempDate.getMonth()) && (day == tempDate
			.getDate()));
}

function imagenChange(image, source) {
	image = toObject(image);
	if (image)
		image.src = source;
}

function checkMaxChars(something, maxChars) {
	something = toObject(something);
	if (something && something.value.length > maxChars)
		something.value = something.value.substring(0, maxChars);
}

function innerChange(something, child) {
	something = toObject(something);
	if (something) {
		while (something.hasChildNodes())
			something.removeChild(something.firstChild);
		if (typeof (text) == 'string')
			something.appendChild(document.createTextNode(child))
		else
			something.appendChild(child)
	}
}

function bookMark(value) {
	var bookData = new Array();
	bookData = value.split('|');
	if (document.all)
		window.external.AddFavorite(bookData[0], bookData[1]);
	else
		alert('Lo Sentimos, los usuarios de Netscape o Mozilla deben agregar a \nfavoritos manualmente desde el menu o haciendo uso de <Ctrl-D>');
}

function setElementOpacity(something, opacity) {
	var old = something;
	something = toObject(something);
	if (!something)
		return;

	if (opacity < 0)
		opacity = 0;
	else if (opacity > 1)
		opacity /= 100;

	if (document.all)
		something.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
	else
		something.style.MozOpacity = opacity;
	something.style.opacity = opacity;
}

function moveElement(elem, nX, nY, nXStepIn, nYStepIn, bPercent, nInterval,
		onCallBack) {
	elem = toObject(elem);
	var style = elem.style;
	var nDirX = 0;
	var nDirY = 0;
	var nXStep = nXStepIn;
	var nYStep = nYStepIn;

	if (nXStep <= 0)
		nXStep = 1;

	if (nYStep <= 0)
		nYStep = 1;

	var left = Number(style.left.replace('px', ''));
	var top = Number(style.top.replace('px', ''));

	if (Math.abs(nX - left) > 0) {
		if (bPercent)
			nXStep = Math.ceil((nXStepIn) * Math.abs(nX - left) / 100);

		if (Math.abs(nX - left) < Math.abs(nXStep) + 1)
			nXStep = Math.abs(nX - left);

		nDirX = (nX - left) >= 0 ? 1 : -1;

		left += nXStep * nDirX;
		style.left = left + 'px';
	}

	if (Math.abs(nY - top) > 0) {
		if (bPercent)
			nYStep = Math.ceil((nYStep) * Math.abs(nY - top) / 100);

		if (Math.abs(nY - top) < Math.abs(nYStep) + 1)
			nYStep = Math.abs(nY - top);

		nDirY = (nY - top) >= 0 ? 1 : -1;

		top += nYStep * nDirY;
		style.top = top + 'px';
	}

	if (nDirX == 0 && nDirY == 0) {
		if (onCallBack != undefined) {
			var func = new Function(onCallBack);
			func();
		}
	} else
		elem.timeout = setTimeout("moveElement ( '" + elem.id + "', " + nX
				+ ", " + nY + ", " + nXStepIn + ", " + nYStepIn + ", "
				+ bPercent + ", " + nInterval + ", \"" + onCallBack + "\" )",
				nInterval);
}

/* START SortTable Script */

function compare(obj1, obj2) {
	var str1 = obj1.toString().toLowerCase();
	var str2 = obj2.toString().toLowerCase();
	if (str1 == str2)
		return 0;
	if (isNaN(str1) && isNaN(str2)) {
		return (str1 > str2) ? 1 : -1;
	}

	return Number(str1) - Number(str2);
}

function sortTable(column) {
	column = toObject(column);
	if (column) {
		var table = column;
		while (table && table.parentNode
				&& table.parentNode.nodeName.toLowerCase() != 'table')
			table = table.parentNode;
		if (table) {
			var groups = table.getElementsByTagName('tbody');
			alert(groups.length)
			for ( var c = 0; c < groups.length; c++)
				sortTableRows(groups[c], column.parentNode.cellIndex);
		}
	}
}

function sortTableRows(group, col) {
	var oRows = new Array(); // set the rows to be removed as an array of
	// cloneNodes
	var iRows = new Array(); // set those rows' indexes as array

	for ( var c = 0; c < group.rows.length; c++) {
		oRows[c] = group.rows[c].cloneNode(true);
		iRows[c] = group.rows[c].sectionRowIndex;
	}
	var oCol = new Array(); // set the string content of column cells as array
	var vCol = new Array(); // set the "compare" array for a future sort/reverse

	for (c = 0; c < iRows.length; c++) {
		if (group.rows[c].cells[col])
			vCol[c] = oCol[c] = [
					group.rows[c].cells[col].firstChild.nodeValue, iRows[c] ];
		else
			vCol[c] = oCol[c] = [ '', iRows[c] ];
	}

	oCol.sort(compare); // sorts the content array

	if (vCol.toString() == oCol.toString())
		oCol.reverse(); // if the content was already sorted, reverse

	for (c = 0; c < group.rows.length; c++) {
		group.replaceChild(oRows[oCol[c][1]], group.rows[c]); // writes the
		// rows in a
		// sorted/reversed
		// order
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}

/* END SortTable Script */

/** ******************************* */
/* BEGIN General Functions Script */
/** ******************************* */

function inputFocus(obj, def) {
	if (obj && obj.value.trim() == def)
		obj.value = '';
}

function inputBlur(obj, def) {
	if (obj && obj.value.trim() == '')
		obj.value = def;
}

function validateSearchForm() {
	var sInput = toObject('searchKey');
	if (!sInput || sInput.value.trim() == 'Buscar'
			|| sInput.value.trim().length < 3) {
		sInput.focus();
		alert(unescape('La b%FAsqueda debe contener 3 caracteres como m%EDnimo'));
		return false;
	}
	return true;
}

function validateNewsForm() {
	var sInput = toObject('newsName');
	if (!sInput || sInput.value.trim() == 'Nombre'
			|| sInput.value.trim().length <= 3) {
		sInput.focus();
		alert(unescape('No ha ingresado un nombre correcto.'));
		return false;
	}
	sInput = toObject('newsEmail');
	if (!sInput || sInput.value.trim() == 'E-mail'
			|| sInput.value.trim().length <= 3 || !validateEmail(sInput.value)) {
		sInput.focus();
		alert(unescape('No ha ingresado un e-mail correcto.'));
		return false;
	}

	requestInto('newsForm', 'Scripts/newsletter.php', 'newsHolder', null, true);

	return false;
}

function validateLoginForm() {
	return true;
}

function setValueSelect(selectName, value) {
	eval('selectObject = document.getElementById("' + selectName + '");');
	for (index = 0; index < selectObject.length; index++) {
		if (selectObject[index].value == value)
			selectObject.selectedIndex = index;
	}
}

// Radio Button Validation
// copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005
// you may copy this function but please keep the copyright notice with it
function valButton(btn) {
	var cnt = -1;
	for ( var i = btn.length - 1; i > -1; i--) {
		if (btn[i].checked) {
			cnt = i;
			i = -1;
		}
	}
	if (cnt > -1)
		return btn[cnt].value;
	else
		return null;
}

var lastMenu = null;
var menuInterval = null;
function menuOver(id) {
	clearTimeout(menuInterval);
	if (lastMenu != id)
		hideLastMenu();
	lastMenu = id;
	var opt = toObject('menuOpt_' + lastMenu);
	if (opt.className != 'menuOptionSel')
		opt.className = 'menuOptionOver';
	var list = toObject('menu_' + lastMenu);
	if (list)
		showElement(list);
}

window.executeScript = function(text) {
	var startPos = text.indexOf('<script');
	if (startPos >= 0)
		startPos = text.indexOf('>', startPos) + 1;
	while (startPos >= 0) {
		var endPos = text.indexOf('<\/script>', startPos);
		var script = text.substring(startPos, endPos);
		if (window.execScript)
			window.execScript(script);
		else
			window.eval(script);
		if (endPos > startPos) {
			startPos = text.indexOf('<script', endPos);
			if (startPos >= 0)
				startPos = text.indexOf('>', startPos) + 1;
		}
	}
}

function sendForm(formObject, idContent, mode, fileUpload) {
	var idcont = document.getElementById(idContent);
	var idform = document.getElementById(formObject);

	if (idcont == undefined) {
		alert("Zona de Contenido no Existe");
		return;
	}

	if (idform == undefined) {
		alert("Formulario no Existe");
		return;
	}

	if (fileUpload == undefined) {
		var callback = {
			success : function(o) {
				idcont.innerHTML = o.responseText;
				executeScript(o.responseText);
			},
			failure : function(o) {
				idcont.innerHTML = (mode != 'silent' ? 'Error en la Comunicacion.'
						: '');
			}
		}
		YAHOO.util.Connect.setForm(idform);
		var cObj = YAHOO.util.Connect.asyncRequest('POST', idform.action,
				callback);
	} else {
		YAHOO.util.Connect.setForm(idform, true);

		var uploadHandler = {
			upload : function(o) {
				idcont.innerHTML = o.responseText;
				executeScript(o.responseText);
			}
		};

		cObj = YAHOO.util.Connect.asyncRequest('POST', idform.action,
				uploadHandler);
	}

	mode != 'silent' ? idcont.innerHTML = "<img src='/site/images/loader.gif' border='0'>"
			: idcont.innerHTML = ' '
}

function hideLastMenu() {
	if (lastMenu) {
		var opt = toObject('menuOpt_' + lastMenu);
		if (opt.className != 'menuOptionSel')
			opt.className = 'menuOption';
		var list = toObject('menu_' + lastMenu);
		if (list)
			hideElement(list);
	}
}

function menuOut(id) {
	menuInterval = setTimeout('menuOutTimeOut( \'' + id + '\' )', 500);
}

function menuOutTimeOut(id) {
	if (lastMenu == id)
		hideLastMenu();
}

function correctPNG() {
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		for ( var i = 0; i < document.images.length; i++) {

			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.indexOf(".PNG") != -1) {

				var imgStyle = "display:inline-block;" + img.style.cssText
				if (img.align == "left")
					imgStyle = "float:left;" + imgStyle
				if (img.align == "right")
					imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href)
					imgStyle = "cursor:pointer;" + imgStyle
				imgStyle = "width:"
						+ img.width
						+ "px; height:"
						+ img.height
						+ "px;"
						+ imgStyle
						+ ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
						+ "(src='" + img.src + "', sizingMethod='scale');"

				var span = document.createElement("span")
				span.id = img.id
				span.className = img.className
				span.title = (img.title) ? img.title : img.alt
				span.onclick = img.onclick
				span.style.cssText = imgStyle

				img.parentNode.replaceChild(span, img)

				i = i - 1
			}
		}
	}
}