/*
 * common routines
 * org.notariado.inti.bcn.MaxNoe 20040517
 */

/*
 * The starting routine is attached to the
 * page including the script.
 */

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();
	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 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() {

	for(var level = 0; level < document.forms.length; level++){
		if(document.forms[level] != 'search'){
			//var editableTypes = {checkbox:"", "select-one":"", text:"", textarea:"", radio:""};
			var editableTypes = {checkbox:"", text:"", textarea:"", radio:""};
			var formElements = document.forms[level].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;
}

function ifAssign(form){
	var totalCheckbox = 0;
	for (var e = 0; e < form.elements.length; e++) {
		var elemento = form.elements[e];
		if (elemento.type == 'checkbox' && elemento.checked) {
			totalCheckbox = totalCheckbox + 1;
		}
	}

	if(totalCheckbox > 0){
		document.getElementById("assign").style.display = 'block';
	}else{
		document.getElementById("assign").style.display = 'none';
	}
}

function compareDates(){

	var today = new Date();
	var todayDay = today.getDate();
	var todayMonth = today.getMonth()+1;
	var todayYear = today.getYear();

	dayLapsing = document.getElementById('dayLapsing').value;
	monthLapsing = document.getElementById('monthLapsing').value;
	yearLapsing = document.getElementById('yearLapsing').value;
	dayPublish = document.getElementById('dayPublish').value;
	monthPublish = document.getElementById('monthPublish').value;
	yearPublish = document.getElementById('yearPublish').value;
	
	var lapsingDate=new Date(yearLapsing,(monthLapsing-1),dayLapsing);
	var publishDate=new Date(yearPublish,(monthPublish-1),dayPublish);

	if (lapsingDate < today ){
		alert('La fecha de caducidad debe ser superior a ' +todayDay+'/'+todayMonth+'/'+todayYear);
		return false;
	}else if (lapsingDate < publishDate) {
		alert('La fecha de caducidad debe ser mayor a la fecha de publicación');
		return false;
	}else{
		return true;
	}
}

function emphasize(em, newcolor) {
	if (newcolor) {
		lastcolor = em.style.backgroundColor;
		em.style.backgroundColor = newcolor;
	} else {
		em.style.backgroundColor = lastcolor;
	}
}
lastcolor = 0;

/**
 * Para la parte privada.
 * Cambia estilo para emitir error
 */
function launchError(){
	option = document.getElementById('currentOption');
	option.style.backgroundColor = '#FF0000';
	option.style.color = '#FFFFFF';
}