/*
 * 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() {
	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;
}

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;
		}
	}
	var contentWrapper = document.getElementById("assign");
	if(totalCheckbox > 0){
		if(contentWrapper)
			contentWrapper.style.display = 'block';
	}else{
		if(contentWrapper)
			contentWrapper.style.display = 'none';
	}
}
function compareDates2(){
	var today = new Date();
	var todayDay = today.getDate();
	var todayMonth = today.getMonth()+1;
	var todayYear = today.getYear();
	/** Lapsing = Fecha Caducidad **/
	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 (dayPublish != '' && monthPublish != '' && yearPublish != '') {
		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;
			}
	} else {
			alert('La fecha de publicación no es correcta');
			return false;
	}
}
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;

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
    obj.addEventListener(evType, fn, true); 
    return true; 
 } else if (obj.attachEvent){ 
    var r = obj.attachEvent("on"+evType, fn); 
    return r; 
 } else { 
    return false; 
 } 
}
function openWindow(strUrl, strWindowName, strWindowFeatures){
    windowName = strWindowName;
    if (windowName == null)
       windowName = '';

    if (strWindowFeatures != null){
       window.window.open(strUrl, windowName, strWindowFeatures);

    } else {
      window.window.open(strUrl, windowName, 'location=0,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1,fullscreen=0,width=600,height=450');
    }
} 
function checkUrl() {
	if (document.forms[0].option_type.selectedIndex == 1) {
		document.getElementById('urldiv').style.display = 'block';
		document.getElementById('contentdiv').style.display = 'none';
	} else {
		document.getElementById('urldiv').style.display = 'none';
		document.getElementById('contentdiv').style.display = 'block';
	}
}
function browse(order){
	document.forms[0].order.value = order;
	document.forms[0].submit();
}