//*************************************************************************
//	UTILIDADES VARIAS PARA JavaScript
//*************************************************************************

//-------------------------------------------------------------------------
// Sistema de herencia para Javascript
// (buscar en google Javascript inheriance de Harry Fiecks (sitepoint.com)
//
// El sistema es simple. Una rutina copia los prototipos del padre en el
// hijo. Para ello, simplemente, tras el constructor del hijo, se añade
// una llamada a copyPrototype con el hijo y el padre como parámetros.
// IMPORTANTE: Los objetos padres no pueden sobrecargar "toString()".
//   Para poder hacerlo, sobrecargar el prototipo del padre
//
// Ejemplo de herencia
// function Animal() { this.species = "animal"; };
// Animal.prototype.category = function() { alert(this.species); };
// function Dog() { this.Animal(); }
// copyPrototype(Dog,Animal);
//
// Sobrecargando métodos del padre
// function Animal() { this.species = "animal"; };
// Animal.prototype.category = function() { alert(this.species); };
// function Dog() { this.Animal(); }
// copyPrototype(Dog,Animal);
// Doc.prototype.category = function() { alert(this.species.toUpperCase()); };
//
// Llamando a métodos del padre desde un método sobrecargado en el hijo
// (mismo código que en el anterior)
// Doc.protorype.category = function(a,b,c) {
//   Animal.prototype.category.apply(this, new Array(a,b,c));
// o bien, de manera más moderna (el antiguo está marcado como deprecated)
//   Animal.prototype.category.call(this,a,b,c);
// aquí el resto del código del método
// }
//
//-------------------------------------------------------------------------

function copyPrototype(descendant, parent) {
	// Obtengo el "código fuente del objeto"
	var sConstructor = parent.toString();
	// Busco y copio el prototipo
	var aMatch = sConstructor.match( /\s*function (.*)\(/ );
	if (aMatch != null) { descendant.prototype[aMatch[1]] = parent; }
	// Copio las variables del prototipo
	for (var m in parent.prototype)
		{
			descendant.prototype[m] = parent.prototype[m];
		}
}

//-------------------------------------------------------------------------
// Objeto para detección de Plataformas y navegadores
// Tres propiedades: browser, version, OS
//-------------------------------------------------------------------------
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//-------------------------------------------------------------------------
// Rutinas para formularios
//-------------------------------------------------------------------------

function fieldEmpty(sFieldName) {
	if (document.getElementById(sFieldName).value == "")
			return true;
	else
			return false;
}

function fieldElement(sFieldName) {
	return document.getElementById(sFieldName);
}

function fieldValue(sFieldName) {
	return document.getElementById(sFieldName).value;
}

function fieldSetValue(sFieldName, sValue) {
	document.getElementById(sFieldName).value = sValue;
}

function fieldIsNumber(sFieldName, iMin, iMax) {
	var value = Number(document.getElementById(sFieldName).value);
	if (isNaN(value))
			return false;
	else
		{
			if (iMin != null && value < iMin)
					return false;
			if (iMax != null && value > iMax)
					return false;
			return true;
		}
}

function padZeros(sValue, iNumDigits)
{
	var res = "";
	var numZeros = iNumDigits - sValue.length;
	for (var i=0;i<numZeros;i++)
			res += "0";
	res += sValue;
	return res;
}

function fieldDateToNumber(sFieldName)
{
	var value = fieldValue(sFieldName);
	if (value == "")
			return 0;
	var dateParts = value.split("/");
	var date = padZeros(dateParts[2],4) + padZeros(dateParts[1],2) + padZeros(dateParts[0],2);
	return Number(date);
}

function fieldFocus(sFieldName) {
	document.getElementById(sFieldName).focus();
}

function fieldClear(sFieldName) {
	var field = document.getElementById(sFieldName);
	if (field)
			field.value = "";
}

// RUTINA PARA CAPAR EL ENTER EN SAFARI
function fieldIgnoreEnter(sFieldName) {
	var field = document.getElementById(sFieldName);
	if (field != null)
			field.onkeydown = function (oEvent) { oEvent = oEvent || window.event; if (oEvent.keyCode == 13) { return false; } };
}
		


function formClear(sFormName) {
	var form = document.getElementById(sFormName);
	if (form)
			form.reset();
}

function formIsEmpty(sFormName) {
	var form = document.getElementById(sFormName);
	var inputs = form.getElementsByTagName('input');
	if (inputs)
		{
			for (var i=0;i<inputs.length;i++)
				{
					var input = inputs.item(i);
					if (input.value != "")
							return false;
				}
		}
	return true;
}

//-------------------------------------------------------------------------
// Rutinas de ampliación del tipo de datos String
//-------------------------------------------------------------------------

// Añadimos a la String el prototipo para Trim
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }

