/*
 * 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;
		}
	}

	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;

function addEvent(element, type, handler) {
  // assign each event handler a unique ID
  if (!handler.$$guid) handler.$$guid = addEvent.guid++;
  // create a hash table of event types for the element
  if (!element.events) element.events = {};
  // create a hash table of event handlers for each element/event pair
  var handlers = element.events[type];
  if (!handlers) {
    handlers = element.events[type] = {};
    // store the existing event handler (if there is one)
    if (element["on" + type]) {
      handlers[0] = element["on" + type];
    }
  }
  // store the event handler in the hash table
  handlers[handler.$$guid] = handler;
  // assign a global event handler to do all the work
  element["on" + type] = handleEvent;
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
  // delete the event handler from the hash table
  if (element.events && element.events[type]) {
    delete element.events[type][handler.$$guid];
  }
};

function handleEvent(event) {
  // grab the event object (IE uses a global event object)
  event = event || window.event;
  // get a reference to the hash table of event handlers
  var handlers = this.events[event.type];
  // execute each event handler
  for (var i in handlers) {
    this.$$handleEvent = handlers[i];
    this.$$handleEvent(event);
  }
};

function writeMail(part1, part2){
  document.write("<a href="+"mail"+"to:"+part1+"@"+part2+">"+part1+"@"+part2+"</a>")
}

function MM_goToURL() { //v3.0

  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;

  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");

}

function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function MM_openBrWindow2(theURL,winName,features) {
    window.open(theURL,winName,features);
}

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 openGMap(title, filter) {
	var url = '?do=search&option=map&title=' + title + '&' + filter;
	var widthWindow = 500;
	var heightWindow = 300;
	var leftWindow	= (screen.width - widthWindow) / 2;
	var topWindow	= (screen.height - heightWindow) / 2;
	
	// Explicaci�n que aparece en la p�gina de msdn.microsoft.com:
	// 	"The param titlebar specifies whether to display a title bar for the window.
	//   This parameter is ignored unless the calling application is an HTML Application or a trusted dialog box.
	//	 The default is yes."

	// var params = "toolbar=yes,location=yes, status=yes,menubar=yes,scrollbars=yes, resizable=yes, titlebar=no, width=" + widthWindow + ", height=" + heightWindow + ", left=" + leftWindow + ",top=" + topWindow;
	var params = "toolbar=no,location=no, status=no,menubar=no,scrollbars=no, resizable=no, titlebar=no, width=" + widthWindow + ", height=" + heightWindow + ", left=" + leftWindow + ",top=" + topWindow;
    openWindow(url, "gMap", params);
}

