/*
 * 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();
    applicationDate();
    //fixHeight();
	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 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;
}

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');
    }
}

/* Modified to support Opera */
function bookmarksite(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

/*
 * Enables the keypress of Enter as
 * a means to submit a form.
 */
function enableEnter() {
	// TODO: Attach Enter keypress event to all input/text.
}

function applicationDate() {
    var elem = document.getElementById('application_date');
    if (elem) {
	    var currentDate = new Date().toLocaleDateString();
	    currentDate = currentDate.substr(0,1).toUpperCase() + currentDate.substr(1);
        elem.innerHTML = currentDate;
    }
}

function wprinter(section, option) {
    var params = "toolbar=no,location=no, status=no,menubar=no,scrollbars=yes, resizable=no, titlebar=no, left=1,top=1";
    printerpage = window.open('?do=' + section + '&option=' + option + '&mode=wvpr', 'Printer', params);
}


function openColegioMap(Code, Title) {
    var wWin = (screen.width - 500) / 2;
    var hWin = (screen.height - 565) / 2;
    var url = "http://www.viamichelin.com/b2b2c/esp/dyn/controller/dlPoiAccess?productId=82081&name=" + Title + "&id=" + Code + "&typeZoom=400";
    var params = "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,titlebar=no,width=500,height=565,left="+wWin+",top="+hWin;
    window.open(url,"mapaBilbao",params);
}



function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

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 addEvent(obj, evType, fn){
	if(typeof(Event) != "undefined") { // con prototype.js
		Event.observe(obj, evType, fn);
	} else { // sin prototype.js
		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 checkUrl() {
	if (document.forms[0].option_type.selectedIndex == 1) {
		document.getElementById('urldiv').style.display = 'inline';
		document.getElementById('contentdiv').style.display = 'none';
	} else {
		document.getElementById('urldiv').style.display = 'none';
		document.getElementById('contentdiv').style.display = 'inline';
	}
}

/*
 * Unless url all the arguments are optionals
 */
function openframe(url, name, width, height, scrollbars, resizable) {

	if (!width) width = parseInt((screen.width*97)/100);
	if (!height) height = parseInt((screen.height*97)/100);

	if (!scrollbars) scrollbars = 1;
	if (!resizable) resizable = 1;

	var w = width;
	var h = height;
	var posx = (screen.width-w)/2;
	var posy = (screen.height-(h))/2;

	// alert(screen.width + ',' + screen.height);
	// alert(w + ',' + h + ':' + posx + ',' + posy);

  	var settings  ='height='+(h-150)+',';
    	settings +='width='+w+',';
      	settings +='top='+posy+',';
      	settings +='left='+posx+',';
      	settings +='location=0,';
      	settings +='menubar=0,';
      	settings +='directories=0,';
      	settings +='scrollbars='+scrollbars+',';
      	settings +='status=1,';
      	settings +='resizable='+resizable+'';

	var backoffice = window.open(url, name, settings);

	if (parseInt(navigator.appVersion) >= 4) {
		backoffice.window.focus();
	}

	// Make parent window not to load url
	return backoffice;
}

function fixHeight() {
	h=document.getElementById('contenedor').offsetHeight;
	document.getElementById('contenedor').style.height=h;

	in_h = document.body.clientHeight;
	in_e = parseInt(document.getElementById('contenedor').style.height);

	if (in_h <= in_e) {
		return;
	} else {
		document.getElementById('content_layout').style.height = (document.documentElement.clientHeight - 226) + 'px';
	}
}

