/*
 * Common routines.
 * Eix
 * net.noihe.Max <max@noihe.net> 20040517
 */

/*
 * The starting routine is attached to the page including the script.
 */

var blankReplacement = "/resources/media/images/res.gif";

window.onload = startUp;

/*
 * this function is called at body.onLoad, so if there is a function that needs
 * to be executed everytime a page is loaded, it should be called here.
 */
function startUp() {
	correctExternalLinks();
	correctFormElementsNames();
	fixPNGs();
	setFocus();
	enableEnter();	return true;
}

/*
 * forces the external links to open in a new window, in a manner that validates
 * against XHTML Strict. This last one does not allow 'target="_blank"' anymore,
 * but the DOM does.
 */
function correctExternalLinks() {
	// run through every a tag in the document
	var serverName = window.location.hostname;
	for (var index = 0; index <= (document.links.length - 1); index++) {
		var URL = document.links[index].href;

		// any links not containing the current server name are outbound.
		if ((URL.indexOf(serverName) < 0) && (URL.indexOf('javascript') != 0)) {
			document.links[index].target = "_blank";
		} else {
			document.links[index].target = "";
		}
	}

	return true;
}

/*
 * Ensures that each element in a form has a name, copying it from the id, if
 * none is found. This yields future compatibility with XHTML1.1.
 */
function correctFormElementsNames() {
	for (var index = 0; index < document.forms.length; index++) {
		var currentForm = document.forms[index];
		for (var elementIndex = 0; index < currentForm.elements.length; elementIndex++) {
			var formElement = currentForm.elements[elementIndex];
			if (formElement) {
				if ((formElement.name == "") && (formElement.id != "")) {
					formElement.name = formElement.id;
				}
			} else {
				// If formElement is undefined, somehow the loop
				// restarts and enters an infinite loop.
				break;
			}
		}
	}

	return true;
}

/*
 * sets the focus at the first form's first field.
 */
function setFocus() {
	if (document.forms[0]) {
		var editableTypes = {checkbox:"", "select-one":"", text:"", textarea:"", radio:""};
		var formElements = document.forms[0].elements;
		for (var index = 0; index < formElements.length; index++) {
			if ((formElements[index].type in editableTypes) && (!formElements[index].disabled)) {
				formElements[index].focus();
				break;
			}
		}
	}

	return true;
}

/*
 * Enables the keypress of Enter as
 * a means to submit a form.
 */
function enableEnter() {
	// TODO: Attach Enter keypress event to all input/text.
}

/*
 * Prints the current page.
 */
function printPage() {
	if (window.print) {
		window.print();
	} else {
		alert("Su navegador no permite la impresión esta página.");
	}

	return true;
}

/*
 * Shows contextual help.
 */
function showHelp(context) {
	alert('No hay ayuda disponible para esta página.');
}

/*
 * Routine to correct IE's PNGs transparency.
 */
function fixPNGs() {
	if (navigator.userAgent.match(/MSIE/) && navigator.platform == "Win32") {
		for (var index = 0; index < document.images.length; index++) {
			element = document.images[index];
			
			// get src
			var src = element.src;
			
			// If the image is still unprocessed...
			if (src != blankReplacement) {
				// ... check if it is a PNG...
				if ( /\.png$/.test(src.toLowerCase())) {
					// ... take note of the size...
					var width = element.width;
					var height = element.height;
					// ... replace image with a blank one...
					element.src = blankReplacement;
					// ... set its size...
					element.width = width;
					element.height = height;
					// ... and, finally, show the filtered image.
					element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
						src + "',sizingMethod='none')";
				}
			}
			
		}
	}
}

function bookmark(){
	var title = window.name;
	var url = window.location;
	
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else {
		if (window.opera && window.print) {
			var mbm = document.createElement('a');
			mbm.setAttribute('rel', 'sidebar');
			mbm.setAttribute('href', url);
			mbm.setAttribute('title', title);
			mbm.click();
		} else {
			if (document.all) {
				window.external.AddFavorite(url, title);
			}
		}
	}
}