﻿// Framework Javascript de IdasI

var jIdasI = function() {
	// OJO: Los métodos extendidos se caracterizan porque la primera letra es una x...
	var metodosExtendidos = {
		xText: function(newValue) {
			if(newValue==undefined) return $getText(this);
			return $setText(this, newValue);
		},
		xClearText: function() {
			$setText(this, '');
			return this;
		},
		xValue: function(newValue, numDecimals) {
			if(newValue==undefined) return $getValue(this);
			return $setValue(this, newValue, numDecimals);
		},
		xCSS: function(name, value) {
			if (value==undefined) return this.style[name];
			this.style[name] = value;
			return this;
		},
		xVisible: function(value) {
			if (value==undefined) return (this.style.display=='none') ? false : true;
			this.style.display = (!value) ? 'none' : '';
			return this;
		},
		xToggleVisible: function() {
			this.style.display = (this.style.display=='') ? 'none' : '';
			return this;
		},
		xRows: function(rowIndex) {
			return jIdasI.extend(this.rows.item(rowIndex), metodosExtendidos);
			//if(this.rows.item) return $(this.rows.item(rowIndex));
			//return $(this.rows(rowIndex));
		},
		xCells: function(rowIndex, colIndex) {
			if(colIndex==undefined) return jIdasI.extend(this.cells.item(rowIndex), metodosExtendidos); // Es un objeto row
			return jIdasI.extend(this.rows.item(rowIndex).cells.item(colIndex), metodosExtendidos); ;     // Es un objeto table
			//return this.xRows(rowIndex).xCells(colIndex);     // Es un objeto table
		},
		xChildren: function(childIndex) {
			return jIdasI.extend(this.childNodes.item(childIndex));
		}
//		xAttr: function(name, value) {
//			if (!value) return this.getAttribute(name);
//			this.setAttribute(name, value);
//			return this;
//		},
//		xAddEvent: function(type, fn ) {
//			if ( this.attachEvent ) {
//				this['e'+type+fn] = fn;
//				var oThis = this;
//				this[type+fn] = function(){oThis['e'+type+fn]( window.event );}
//				this.attachEvent( 'on'+type, this[type+fn] );
//			} else
//				this.addEventListener( type, fn, false );
//			return this;
//		},
//		xRemoveEvent: function(type, fn ) {
//			if ( this.detachEvent ) {
//				this.detachEvent( 'on'+type, this[type+fn] );
//				this[type+fn] = null;
//			} else
//				this.removeEventListener( type, fn, false );
//			return this;
//		}
	}

	return {
		prefix: 'ctl00_Contenido_', // Prefijo para construir el nombre del elemento en estilo .NET
		init: function(id) {
			var pos = id.lastIndexOf('_');
			prefix = id.substr(0,pos);
		},
		extend: function(elemento, opt) {
			for (var name in opt) elemento[name] = opt[name];
			return elemento;
		},
		getElement: function(obj) {
			if(typeof(obj)=='string') {
				var obj2 = document.all ? document.all[jIdasI.prefix+obj] : document.getElementById(jIdasI.prefix+obj);
				if(obj2==undefined) {
					obj2 = document.all ? document.all[obj] : document.getElementById(obj);
				}
				return jIdasI.extend(obj2, metodosExtendidos);
			} else {
				return jIdasI.extend(obj, metodosExtendidos);
			}
		}
	}
}();

// Alias
window.$ = jIdasI.getElement;
//if(!windows.$) {window.$ = jIdasI.getElement;}


// Funciones útiles

function $valorStr(S) {
	var Coma;

	try {
		if(S=='') return 0;
		if(S.indexOf(',')==-1) return parseFloat(S);

		S = S.replace('.', '');
		Coma = S.indexOf(',');
		if(Coma!=-1) S = S.substring(0, Coma)+'.'+S.substring(Coma+1, S.length);
		return parseFloat(S);
	}
	catch(e) {
		throw e;  // Paso el error al siguiente controlador de errores
	}
}

function $Text(obj, newText) {
	if (!newText) return $getText(obj);
	return $setText(obj, newText);
}

function $Value(obj, newValue) {
	if (!newValue) return $getValue(obj);
	return $setValue(obj, newValue);
}

function $getText(obj) {
	if(typeof(obj)=='string') obj = jIdasI.getElement(obj);

	if(obj.value) {
		return obj.value;
	} else {
		if(obj.textContent==undefined) {
			return obj.innerText;
		} else {
			return obj.textContent;
		}
	}
}

function $setText(obj, newText, bold) {
	if(typeof(obj)=='string') obj = jIdasI.getElement(obj);

	if(obj.value!=undefined) {
		obj.value = newText;
	} else {
		if(obj.innerText!=undefined) {
			obj.innerText = newText;
		} else {
			obj.textContent = newText;
		}
	}
	if(bold) obj.innerHTML = obj.innerHTML.bold();
	return obj;
}

function $getValue(obj) {
	var S;

	S = $getText(obj);
	return $valorStr(S);
}

function $setValue(obj, newValue, numDecimals, bold) {
	var S, Tam;

	if(numDecimals==undefined) {
		$setText(obj, newValue+'');
	} else {
		S = newValue.toFixed(numDecimals);
		S = S.replace(",", ".")   // Pongo el punto de miles adecuado
		if(numDecimals!=0) {
			Tam = S.length-numDecimals;
			S = S.substring(0, Tam-1)+","+S.substring(Tam, S.length); // Coma para los decimales
		}
		$setText(obj, S, bold);
	}
	return obj;
}

// Métodos extendidos


//HTMLElement.prototype.getText2 = function(){
//    return this.value;
//}


////Check radio buttons or checkbox
//     //Bol could be true or false;
//     checked:function(bol){
//            for(var i=0;i < this.elems.length;i++){
//                //Verify if the element is an input type chekbox or radio because they are the object that have that property.
//                if(this.elems[i].nodeName.toLowerCase()==='input' && (this.elems[i].getAttribute('type') == 'checkbox' || this.elems[i].getAttribute('type') == 'radio')){
//                    this.elems[i].checked = bol; //Asigns true or false;
//                }
//            }
//            return this;
//        },
