
// --------------------------------------------------------------------------------------------
// Funciones para operar con Layers (capas) en un documento
// --------------------------------------------------------------------------------------------

	// Inside a group of layers with the same name numbered, hide all and show the one indexed by num
	function showNumLayer(name, num){
	    for (i = 0; i < 15; i++){
	        if (document.getElementById(name + i) != null)
				showLayer(name + i, false);
	        else
				break;
		}
	    if (num > -1) showLayer(name + num, true);
	}

	// Show or hide the layer (name)
	function showLayer(name, yes){
	    var layer = document.getElementById(name);
	    if (layer != null) layer.style.display = (yes)?'block':'none';
	}

	// Hide all the layers of the array 'docLayers' (if exist) and load the layer (name)
	function loadLayer(name){
		if (docLayers != null){
		    for (i = 0; i < docLayers.length; i++){
		        showLayer(docLayers[i], false);
			}
		}

		showLayer(name, true);
	}

	// Center an element on the window screen
	function showCentered(name){
		if (parseInt(navigator.appVersion, 10) > 3) {
			
			var centerX = 0;
			var centerY = 0;
			
			if (navigator.appName=="Netscape" || navigator.appName=="Mozilla") {
				centerX = (window.innerWidth / 2);
				centerY = (window.innerHeight / 2);
			}
			if (navigator.appName.indexOf("Microsoft") != -1) {
				centerX = (document.body.offsetWidth / 2);
				centerY = (document.body.offsetHeight / 2);
			}
			
			var layer = document.getElementById(name);
			if (layer != null){
				posT = centerX - (layer.style.width / 2);
				posL = centerY - (layer.style.height / 2);
				
				layer.style.top = posT;
				layer.style.left = posL;
				
				showLayer(name, true);
			}
		}
	}
// --------------------------------------------------------------------------------------------


// ********************************************************************************************
// Funciones para operar con los estilos de los elementos
// ********************************************************************************************

	// Changes the attribute class of the element (element) by the class (cName)
	function classChange(element, cName){
	    if (element != null) element.className = cName;
	}

// ********************************************************************************************


// --------------------------------------------------------------------------------------------
// Funciones para realizar operaciones con tablas
// --------------------------------------------------------------------------------------------

	// Erase the row (index) of the table (tableId);
	function delRow(tableId, index){
		var tbl = document.getElementById(tableId);
		if (tbl != null) tbl.deleteRow(index);
	}

	// Adds a row to the table (tableId) and return it
	function addRow(tableId){
	    var tbl = document.getElementById(tableId);
	    if (tbl != null){
			var newRow = tbl.insertRow(tbl.rows.length);
			return newRow;
		}

		return null;
	}

	// Return the last row of a table
	function getLastRow(tableId){
	    var tbl = document.getElementById(tableId);
	    if (tbl != null){
	        if (tbl.rows.length > 0){
				return tbl.rows[tbl.rows.length - 1];
			}
		}

		return null;
	}

	// Erase all rows of the table (tableName)
	function clearTable(tableId){
	    var tbl = document.getElementById(tableId);
	    if (tbl != null){
	        var numRows = tbl.rows.length - 1;
			for (i = numRows; i > 0; i--){
			    tbl.deleteRow(i);
			}
		}
	}

// --------------------------------------------------------------------------------------------


// ********************************************************************************************
// Funciones para realizar operaciones con forms
// ********************************************************************************************

// Remove first and last spaces of a string value
function trim(str){ return str.replace(/^\s*|\s*$/g,""); }

// Check all mandatory form fields
function checkMandatory(){
	if (mandatoryFields != null){
    	for (i = 0; i < mandatoryFields.length; i++){
	        field = document.getElementById(mandatoryFields[i]);
	        if (field != null){
	        	if (field.type.toLowerCase() == 'text' || 
		        	field.type.toLowerCase() == 'password' ||
	        		field.tagName.toLowerCase() == 'textarea'){
			        if (trim(field.value).length == 0){ field.focus(); return false; }
			        
				} else if (field.tagName.toLowerCase() == 'select'){
			        if (field.selectedIndex == 0){ field.focus(); return false; }				
				}
	        }
		}
	}
	
	return true;
}

// Check the max length form fields
function checkMaxLength(){
	if (maxLengthFields != null && maxLengthValue != null){
    	for (i = 0; i < maxLengthFields.length; i++){
	        field = document.getElementById(maxLengthFields[i]);
	        if (field != null){
		        if (field.value.length > maxLengthValue[i]){ field.focus(); return false; }
		    }
		}
	}
	
	return true;
}

// Check if a field is numeric
function checkIsNumeric(){
	if (isNumericFields != null){
    	for (i = 0; i < isNumericFields.length; i++){
	        field = document.getElementById(isNumericFields[i]);
	        if (field != null){
		        if (isNaN(field.value)){ field.focus(); return false; }
		    }
		}
	}
	
	return true;
}

// Check if a field is a date
function checkIsDate(){
	if (isDateFields != null){
    	for (i = 0; i < isDateFields.length; i++){
	        field = document.getElementById(isDateFields[i]);
	        if (field != null){
		        if(!checkDate(field)) return false;
		    }
		}
	}
	
	return true;
}

// Check if a field is an hour
function checkIsHour(){
	if (isHourFields != null){
    	for (i = 0; i < isHourFields.length; i++){
	        field = document.getElementById(isHourFields[i]);
	        if (field != null){
		        if(!checkHour(field)) return false;
		    }
		}
	}
	
	return true;
}

// Check de the content of a date field
//		- checkToday == 0: do nothing
//		- checkToday == -1: check date before current day
//		- checkToday == 1: check date after current day
function checkDate(field, checkToday){

	var aDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	try{
		if(field.value != ""){
			var s = field.value.split("/");
			var today = new Date();
			var day, month, year;
			
			if (s.length == 3){
				day = s[0];
				month = s[1] - 1;
				year = s[2];
				
			} else if (s.length == 1){			
				day = field.value.substr(0,2);
				monthStr = field.value.substr(2,2);
				month = parseInt(field.value.substr(2,2), 10) - 1;
				year = field.value.substr(4);
				if (field.value.length == 6)	year = "20" + year;	
				field.value = day + "/" + monthStr + "/" + year;
						
			} else {
				throw 0;
			}

			aDays[1] = (((parseInt(year, 10) % 4 == 0) && ( (!(parseInt(year, 10) % 100 == 0)) || (parseInt(year, 10) % 400 == 0))) ? 29 : 28 );

			if (parseInt(month, 10) < 0 || parseInt(month, 10) > 11) throw 0;
			if (parseInt(day, 10) < 1 || parseInt(day, 10) > parseInt(aDays[month], 10)) throw 0;

			if (isNaN(day) || isNaN(month) || isNaN(year)) throw 0;
			
			var date = new Date();
			date.setDate(day);
			date.setMonth(month);
			date.setFullYear(year);
			
			if (checkToday > 0 && today > date) throw 1;
			if (checkToday < 0 && today < date) throw 2;  
		}
		
		return true;
		
	} catch (ex){
		if(ex == 1) alert("La data ha d'èsser posterior a la data d'avui");
		else if(ex == 2) alert("La data ha d'èsser anterior a la data d'avui");
		else alert("Format de data incorrecte. \n El format correcte és: DD/MM/AAAA");
		field.value = "";
		field.focus();
		
		return false;
	}
}

// Check de format of an hour field
function checkHour(field){
	try{
		
		var value = field.value;
		if (value.length == 0) return true;
		var aBase = value.split(":");
		if(aBase.length != 2) throw "error";
		var hour = aBase[0];
		var minuts = aBase[1];
		if (isNaN(hour)) throw "error";
		if (isNaN(minuts)) throw "error";
		hour = parseInt(hour, 10);
		minuts = parseInt(minuts, 10);
		if(hour < 0 || hour > 23) throw "error";
		if(minuts < 0 || minuts > 59) throw "error";
		
		return true;
		
	} catch (ex){
		alert("Format d'hora incorrecte");
		field.focus();
		field.select();
		return false;
	}
}

// Set the option of a select (id) with de value passed
function setSelectIndex(id, value){
	var select = document.getElementById(id);
	if (select != null){
		var selectedIndex = 0;
		for (i == 0; i < select.length; i++){
			if (value == select.options[i].value) selectedIndex = i;
		} 
		select.selectedIndex = selectedIndex;
	}
}

// ********************************************************************************************


// --------------------------------------------------------------------------------------------
// Funciones para operar con un calendario
// --------------------------------------------------------------------------------------------

function showCalendar(idField){
	var field = document.getElementById(idField);
	var iframe = parent.document.getElementById("ifcalendar");
	iframe.contentWindow.VCALshowCalendar(field);
	iframe.style.display = "block";
}

function hideCalendar(){
	var iframe = parent.document.getElementById("ifcalendar").style.display = "none";
}

// --------------------------------------------------------------------------------------------


// ********************************************************************************************
// Funciones para operar con un calendario
// ********************************************************************************************

function popup(url){
	var settings = "channelmode=yes";		// whether to display the window in theater mode
	settings += ",directories=no";			// whether to add directory buttons
	settings += ",fullscreen=no";			// whether to display the browser in full-screen mode (in theater mode)
	settings += ",location=no";				// whether to display the address field
	settings += ",menubar=no";				// whether to display the menu bar
	settings += ",resizable=yes";			// whether the window is resizable
	settings += ",scrollbars=yes";			// whether to display scroll bars
	settings += ",status=no";				// whether to add a status bar
	settings += ",titlebar=no"				// whether to display the title bar
	settings += ",toolbar=no";				// whether to display the browser toolbar
	settings += ",top=0,left=0";			// the left and top position, in pixels
	settings += ",width=720,height=480"; 	// the width and height of the window, in pixels. Min. value is 100
	var pop = window.open(url, "popup", settings, true)
}

function maxPopup(url){
	var settings = "channelmode=yes";		 	// whether to display the window in theater mode
	settings += ",directories=no";			 	// whether to add directory buttons
	settings += ",fullscreen=no";			 	// whether to display the browser in full-screen mode (in theater mode)
	settings += ",location=yes";			 	// whether to display the address field
	settings += ",menubar=yes";				 	// whether to display the menu bar
	settings += ",resizable=yes";			 	// whether the window is resizable
	settings += ",scrollbars=yes";			 	// whether to display scroll bars
	settings += ",status=no";				 	// whether to add a status bar
	settings += ",titlebar=no"				 	// whether to display the title bar
	settings += ",toolbar=yes";				 	// whether to display the browser toolbar
	settings += ",top=0,left=0";			 	// the left and top position, in pixels
	settings += ",width="+screen.availWidth; 	// the width of the window, in pixels. Min. value is 100
	settings += ",height="+screen.availHeight; 	// the height of the window, in pixels. Min. value is 100
	var pop = window.open(url, "popup", settings, true)
}

// ********************************************************************************************
