/** Vera API de YUI para informacion de metodos */

GALILEO.cylog.widget.CyLoGTree = function (id, oData) {
	if (id && oData) {
		if (typeof id == "string") {
			id = YAHOO.util.Dom.get(id);
		}
	
		GALILEO.cylog.widget.CyLoGTree.superclass.constructor.call(this, id);
		
		this.createRootNode(oData);
		this.draw();
		
		this._calendar = new GALILEO.cylog.widget.CyLoGCalendar();
	}
};
YAHOO.extend(GALILEO.cylog.widget.CyLoGTree, YAHOO.widget.TreeView);

GALILEO.cylog.widget.CyLoGTree.prototype._calendar = null;

GALILEO.cylog.widget.CyLoGTree.prototype.createNodes = function(node, childrens) {
	
	if (!childrens || childrens.length <= 0)
		return;
	
	var nodoHijo = null;	
	for (var i = 0; i < childrens.length; i++) {
		if (!childrens[i].leaf) {
			nodoHijo = new GALILEO.cylog.widget.CyLoGNode(childrens[i].label, node, childrens[i].nodeName, false, childrens[i].hasIcon)
			nodoHijo.contentStyle = childrens[i].contentStyle;
			this.createNodes(nodoHijo, childrens[i].childrens);
		} else {
			new GALILEO.cylog.widget.CyLoGLeafNode(childrens[i].label, node, childrens[i].nodeName, false);
		}
	}
};

GALILEO.cylog.widget.CyLoGTree.prototype.createRootNode = function(oData) {
	if (oData) {
		var rootNode = new GALILEO.cylog.widget.CyLoGRootNode(oData.rootNode.label, this.getRoot(), oData.rootNode.nodeName, true, true, true);
		if (oData.rootNode.childrens && oData.rootNode.childrens.length > 0) {
			this.createNodes(rootNode, oData.rootNode.childrens);
		}
	}
};

GALILEO.cylog.widget.CyLoGTree.prototype.showCalendar = function(node, inputEl, hrefEl) {
	if(!node._unselect){
	this._calendar.setElements(node, inputEl, hrefEl);
	this._calendar.show();
	}else{
		var dlg = new GALILEO.cylog.widget.GSimpleDialog('dlgError', '22em', true, false, null, null, "Error", "Para introducir Fechas Debe seleccionar una opci&oacute;n SI/NO de aplicaci&oacute;n.");
		dlg.show();
	}
};

/** 
 * Definicion del nodo root para allfunds
 **/
GALILEO.cylog.widget.CyLoGRootNode = function (oData, oParent, nodeName, expanded, hasIcon) {
    GALILEO.cylog.widget.CyLoGRootNode.superclass.constructor.call(this, oData, oParent, expanded, hasIcon);
    this._checkSi = false;
    this._checkNo = false;
    this._unselect = true;
    this._nodeName = (!nodeName) ? null : nodeName;
    /* Nueva linea con la propiedad, esto es lo que hay que usar en vez
de expandir y contraer */
    this.renderHidden = true;
};

YAHOO.extend(GALILEO.cylog.widget.CyLoGRootNode, YAHOO.widget.HTMLNode);

GALILEO.cylog.widget.CyLoGRootNode.prototype._checkSi = null;
GALILEO.cylog.widget.CyLoGRootNode.prototype._checkNo = null;
GALILEO.cylog.widget.CyLoGRootNode.prototype._unselect = null;
GALILEO.cylog.widget.CyLoGRootNode.prototype._nodeName = null;

GALILEO.cylog.widget.CyLoGRootNode.prototype.chequearSi = function() {
	this._checkNo = false;
	this._checkSi = (this._checkSi) ? false : true;
	this._unselect = (!this._checkNo && !this._checkSi) ? true : false;	
	this.updateCheckChildren();
	(this.parent) ? this.parent.refresh() : this.refresh();
};

GALILEO.cylog.widget.CyLoGRootNode.prototype.chequearNo = function() {
	this._checkSi = false;
	this._checkNo = (this._checkNo) ? false : true;
	this._unselect = (!this._checkNo && !this._checkSi) ? true : false;
	this.updateCheckChildren();
	(this.parent) ? this.parent.refresh() : this.refresh();
};

GALILEO.cylog.widget.CyLoGRootNode.prototype.updateCheckChildren = function() {
	if (!this.children || this.children.length <= 0) {
	   	return;
	}
	   
    for (var i=0;i< this.children.length;++i) {
    	this.children[i]._checkSi = this._checkSi;
       	this.children[i]._checkNo = this._checkNo;
       	this.children[i]._unselect = (!this.children[i]._checkSi && !this.children[i]._checkNo) ? true : false;
       	this.children[i].updateCheckChildren();
    }
};
GALILEO.cylog.widget.CyLoGRootNode.prototype.updateChildrenDate = function(className, date) {

	if (!this.children || this.children.length <= 0)
		return;
		 
	for (var i = 0; i < this.children.length; i++) {
		var nodeEl = this.children[i].getEl();
		if (nodeEl) {
			var inputs = GALILEO.util.Dom.getElementsByTagName('input', this.children[i].getEl());
			for (var j = 0; j < inputs.length; j++) {
				if (YAHOO.util.Dom.hasClass(inputs[j], className)) {
					inputs[j].value = date;
				}
			}
		}
		if (className == this._INPUT_FECHA_DESDE_CLASSNAME) {
			this.children[i]._fechaDesde = date;
		} else if (className == this._INPUT_FECHA_HASTA_CLASSNAME) {
			this.children[i]._fechaHasta = date;
		}
		this.children[i].updateChildrenDate(className, date);
	}
};

GALILEO.cylog.widget.CyLoGRootNode.prototype.getNodeHtml = function () {

	var sb = [];
	sb[sb.length] = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
	sb[sb.length] = "<tr>";
	for (var i = 0; i < this.depth; ++i) {
		sb[sb.length] = "<td class=\"" + this.getDepthStyle(i) + "\"><div class=\"ygtvspacer\"></div></td>";
	}
	if (this.hasIcon) {
		sb[sb.length] = "<td";
		sb[sb.length] = " id=\"" + this.getToggleElId() + "\"";
		sb[sb.length] = " class=\"" + this.getStyle() + "\"";
		sb[sb.length] = " onclick=\"javascript:" + this.getToggleLink() + "\"";
		if (this.hasChildren(true)) {
			sb[sb.length] = " onmouseover=\"this.className=";
			sb[sb.length] = "YAHOO.widget.TreeView.getNode('";
			sb[sb.length] = this.tree.id + "'," + this.index + ").getHoverStyle()\"";
			sb[sb.length] = " onmouseout=\"this.className=";
			sb[sb.length] = "YAHOO.widget.TreeView.getNode('";
			sb[sb.length] = this.tree.id + "'," + this.index + ").getStyle()\"";
		}
		sb[sb.length] = "><div class=\"ygtvspacer\"></div></td>";
	}
	sb[sb.length] = "<td";
	sb[sb.length] = " id=\"" + this.contentElId + "\"";
	sb[sb.length] = " class=\"" + this.contentStyle + "\"";
	sb[sb.length] = (this.nowrap) ? " nowrap=\"nowrap\" " : "";
	sb[sb.length] = " >";
	sb[sb.length] = this.html;
	sb[sb.length] = "</td>";
	sb[sb.length] = "<td class=\"formTreeFirstNode\">";
	sb[sb.length] = "<div>";
	sb[sb.length] = " <span class=\"labelSi\">SI</span>";
	//sb[sb.length] = "<input type=\"checkbox\" class=\"checkSi\" onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").chequearSi()\"";
	//if (this._checkSi) {
	//	sb[sb.length] = " checked=\"checked\"";
	//}
	//sb[sb.length] =	" />";
	sb[sb.length] = " <span class=\"labelNo\">NO</span>";
	//sb[sb.length] = "<input type=\"checkbox\" class=\"checkNo\" onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").chequearNo()\"";
	//if (this._checkNo) {
	//	sb[sb.length] = " checked=\"checked\"";
	//} 
	//sb[sb.length] = " />";
	sb[sb.length] = " <span class=\"labelFechaDesde\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fecha Desde Repercusion</span> <span class=\"labelFechaHasta\">Fecha Hasta Repercusion</span>";
	sb[sb.length] = "</div>";
	sb[sb.length] = "</td>";
	sb[sb.length] = "</tr>";
	sb[sb.length] = "</table>";
	return sb.join("");
};



/** Nodo generico para cylog */
GALILEO.cylog.widget.CyLoGNode = function (oData, oParent, nodeName, expanded, hasIcon) {

	GALILEO.cylog.widget.CyLoGNode.superclass.constructor.call(this, oData, oParent, nodeName, expanded, hasIcon);
	var nodoPadre = this.tree.getRoot().children[0];
	if (nodoPadre._checkNo) {
		nodoPadre._checkNo = false;
		this.tree.getRoot().refresh();
	}
};
YAHOO.extend(GALILEO.cylog.widget.CyLoGNode, GALILEO.cylog.widget.CyLoGRootNode);

GALILEO.cylog.widget.CyLoGNode.prototype._fechaDesde = null;
GALILEO.cylog.widget.CyLoGNode.prototype._fechaHasta = null;

GALILEO.cylog.widget.CyLoGNode.prototype._INPUT_FECHA_DESDE_CLASSNAME = 'cylog-treeview-fecha-desde';
GALILEO.cylog.widget.CyLoGNode.prototype._INPUT_FECHA_HASTA_CLASSNAME = 'cylog-treeview-fecha-hasta';

GALILEO.cylog.widget.CyLoGNode.prototype.unCheckParentSi = function (node) {
	
	var p = node.parent;
	if (!p) {
		return;
	}
	
	if (p._checkNo) {
		p._checkNo = false;
	}
	
	var todosSi = true;
	for (var i = 0; i < p.children.length; i++) {
		if (p.children[i]._checkNo || p.children[i]._unselect) {
			todosSi = false;
			break;
		}
	}
	p._checkSi = todosSi;
	
	
	p._unselect = (!p._checkSi && !p._checkNo) ? true : false;
	
	if (p.unCheckParentSi) {
		p.unCheckParentSi(p);
	}
	else {
		return;
	}
};

GALILEO.cylog.widget.CyLoGNode.prototype.unCheckParentNo = function (node) {
	
	var p = node.parent;
	
	if (!p) {
		return;
	}
	
	if (p._checkSi) {
		p._checkSi = false;
	}
	
	var todosNo = true;
	for (var i = 0; i < p.children.length; i++) {
		if (p.children[i]._checkSi || p.children[i]._unselect) {
			todosNo = false;
			break;
		}
	}
	p._checkNo = todosNo;
	
	
	p._unselect = (!p._checkNo && !p._checkSi) ? true : false;
	
	if (p.unCheckParentNo) {
		p.unCheckParentNo(p);
	}
	else {
		return;
	}
};

GALILEO.cylog.widget.CyLoGNode.prototype.chequearSi = function() {
	
	GALILEO.cylog.widget.CyLoGNode.superclass.chequearSi.call(this);
	this.unCheckParentSi(this);
	
	//Actualizamos las fechas de forma recursiva
	if(!this._unselect){
		var form = YAHOO.util.Dom.get('repercusionForm');
		this._fechaDesde = form.primerDiaSiguienteTrimestre.value;;
		this._fechaHasta = "31/12/9999";
	}else{
		this._fechaDesde = "";
	    this._fechaHasta = "";
	}
	this.updateChildrenDate(this._INPUT_FECHA_DESDE_CLASSNAME,this._fechaDesde);
	this.updateChildrenDate(this._INPUT_FECHA_HASTA_CLASSNAME,this._fechaHasta);
	
	this.tree.getRoot().refresh();
};

GALILEO.cylog.widget.CyLoGNode.prototype.chequearNo = function() {
	
	GALILEO.cylog.widget.CyLoGNode.superclass.chequearNo.call(this);
	this.unCheckParentNo(this);
	
	//Actualizamos las fechas de forma recursiva
	if(!this._unselect){
		var form = YAHOO.util.Dom.get('repercusionForm');
		this._fechaDesde = form.primerDiaSiguienteTrimestre.value;;
		this._fechaHasta = "31/12/9999";
	}else{
		this._fechaDesde = "";
	    this._fechaHasta = "";
	}
	this.updateChildrenDate(this._INPUT_FECHA_DESDE_CLASSNAME,this._fechaDesde);
	this.updateChildrenDate(this._INPUT_FECHA_HASTA_CLASSNAME,this._fechaHasta);
	
	this.tree.getRoot().refresh();
	
};

GALILEO.cylog.widget.CyLoGNode.prototype.getNodeHtml = function () {

	var sb = [];
	sb[sb.length] = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
	sb[sb.length] = "<tr>";
	for (var i = 0; i < this.depth; ++i) {
		sb[sb.length] = "<td class=\"" + this.getDepthStyle(i) + "\"><div class=\"ygtvspacer\"></div></td>";
	}
	if (this.hasIcon) {
		sb[sb.length] = "<td";
		sb[sb.length] = " id=\"" + this.getToggleElId() + "\"";
		sb[sb.length] = " class=\"" + this.getStyle() + "\"";
		sb[sb.length] = " onclick=\"javascript:" + this.getToggleLink() + "\"";
		if (this.hasChildren(true)) {
			sb[sb.length] = " onmouseover=\"this.className=";
			sb[sb.length] = "YAHOO.widget.TreeView.getNode('";
			sb[sb.length] = this.tree.id + "'," + this.index + ").getHoverStyle()\"";
			sb[sb.length] = " onmouseout=\"this.className=";
			sb[sb.length] = "YAHOO.widget.TreeView.getNode('";
			sb[sb.length] = this.tree.id + "'," + this.index + ").getStyle()\"";
		}
		sb[sb.length] = "><div class=\"ygtvspacer\"></div></td>";
	}
	sb[sb.length] = "<td";
	sb[sb.length] = " id=\"" + this.contentElId + "\"";
	sb[sb.length] = " class=\"" + this.contentStyle + "\"";
	sb[sb.length] = (this.nowrap) ? " nowrap=\"nowrap\" " : "";
	sb[sb.length] = " >";
	sb[sb.length] = this.html;
	sb[sb.length] = "</td>";
	sb[sb.length] = "<td class=\"formTreeNode\">";
	sb[sb.length] = "<div>";
	sb[sb.length] = "<input type=\"checkbox\" name=\"trm-si-" + this._nodeName + "\"class=\"radio\" onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").chequearSi()\"";
	if (this._checkSi) {
		sb[sb.length] = " checked=\"checked\"";
	}
	sb[sb.length] =	" /> ";
	sb[sb.length] = "<input type=\"checkbox\" name=\"trm-no-" + this._nodeName + "\"class=\"textNo radio\" onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").chequearNo()\"";
	if (this._checkNo) {
		sb[sb.length] = " checked=\"checked\"";
	} 
	sb[sb.length] = " />";	
	sb[sb.length] = " <span><input type=\"text\" readonly=\"true\" class=\"textFecha cylog-treeview-fecha-desde\" name=\"trm-fDesde-" + this._nodeName + "\"";
	if (this._fechaDesde) {
		sb[sb.length] = " value=\"" + this._fechaDesde + "\"";
	}
	sb[sb.length] = " onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").showTreeCalendar(this)\" /></span>";
	sb[sb.length] = "   <span><input type=\"text\" readonly=\"true\" class=\"textFecha cylog-treeview-fecha-hasta\" name=\"trm-fHasta-" + this._nodeName + "\"";
	if (this._fechaHasta) {
		sb[sb.length] = " value=\"" + this._fechaHasta + "\"";	
	}
	sb[sb.length] = " onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").showTreeCalendar(this)\" /></span>";
	sb[sb.length] = "</div>";
	sb[sb.length] = "</td>";
	sb[sb.length] = "</tr>";
	sb[sb.length] = "</table>";
	return sb.join("");
};

GALILEO.cylog.widget.CyLoGNode.prototype.updateChildrenDate = function(className, date) {

	if (!this.children || this.children.length <= 0)
		return;
		 
	for (var i = 0; i < this.children.length; i++) {
		var nodeEl = this.children[i].getEl();
		if (nodeEl) {
			var inputs = GALILEO.util.Dom.getElementsByTagName('input', this.children[i].getEl());
			for (var j = 0; j < inputs.length; j++) {
				if (YAHOO.util.Dom.hasClass(inputs[j], className)) {
					inputs[j].value = date;
				}
			}
		}
		if (className == this._INPUT_FECHA_DESDE_CLASSNAME) {
			this.children[i]._fechaDesde = date;
		} else if (className == this._INPUT_FECHA_HASTA_CLASSNAME) {
			this.children[i]._fechaHasta = date;
		}
		this.children[i].updateChildrenDate(className, date);
	}
};

GALILEO.cylog.widget.CyLoGNode.prototype._selectedDate = function(inputEl) {
	
	if (YAHOO.util.Dom.hasClass(inputEl, this._INPUT_FECHA_DESDE_CLASSNAME)) {
		className = this._INPUT_FECHA_DESDE_CLASSNAME;
		this._fechaDesde = inputEl.value;
		this.parent._fechaDesde = "";
	}else if (YAHOO.util.Dom.hasClass(inputEl, this._INPUT_FECHA_HASTA_CLASSNAME)) {
		className = this._INPUT_FECHA_HASTA_CLASSNAME;
		this._fechaHasta = inputEl.value;
		//TODO Quitamos la fecha al parent para ello this.parent._fechaHasta y hacer un updateParentDate y que sea recursivo.
		//Tambien podria llamarse a this.unCheckParentNo(this); ??
		
	}
	this.updateChildrenDate(className, inputEl.value);
	/*obj.tree.getRoot().refresh();*/
};

GALILEO.cylog.widget.CyLoGNode.prototype._clearCalendar = function(inputEl) {

	if (YAHOO.util.Dom.hasClass(inputEl, this._INPUT_FECHA_DESDE_CLASSNAME)) {
		this._fechaDesde = null;
	}else if (YAHOO.util.Dom.hasClass(inputEl, this._INPUT_FECHA_HASTA_CLASSNAME)) {
		this._fechaHasta = null;
	}
	/*obj.tree.getRoot().refresh();*/
};

GALILEO.cylog.widget.CyLoGNode.prototype.showTreeCalendar = function(el) {
	this.tree.showCalendar(this, el, el);
};

/** Nodo hoja */
GALILEO.cylog.widget.CyLoGLeafNode = function (oData, oParent, nodeName, expanded, hasIcon) {
	GALILEO.cylog.widget.CyLoGLeafNode.superclass.constructor.call(this, oData, oParent, nodeName, expanded, hasIcon);
};

YAHOO.extend(GALILEO.cylog.widget.CyLoGLeafNode, GALILEO.cylog.widget.CyLoGNode);

GALILEO.cylog.widget.CyLoGLeafNode.prototype.chequearSi = function() {
	this._checkNo = false;
	this._checkSi = (this._checkSi) ? false : true;
	this._unselect = (!this._checkNo && !this._checkSi) ? true : false;
	
	//Actualizamos el valor de la fecha
	if(!this._unselect){
		var form = YAHOO.util.Dom.get('repercusionForm');
		this._fechaDesde = form.primerDiaSiguienteTrimestre.value;;
		this._fechaHasta = "31/12/9999";
	}else{
		this._fechaDesde = "";
		this._fechaHasta = "";
	}
	
	this.unCheckParentSi(this);
	this.tree.getRoot().refresh();
};

GALILEO.cylog.widget.CyLoGLeafNode.prototype.chequearNo = function() {
	this._checkSi = false;
	this._checkNo = (this._checkNo) ? false : true;
	this._unselect = (!this._checkNo && !this._checkSi) ? true : false;
	
	//Actualizamos el valor de la fecha
	if(!this._unselect){
		var form = YAHOO.util.Dom.get('repercusionForm');
		this._fechaDesde = form.primerDiaSiguienteTrimestre.value;;
		this._fechaHasta = "31/12/9999";
	}else{
		this._fechaDesde = "";
		this._fechaHasta = "";
	}
	
	this.unCheckParentNo(this);
	this.tree.getRoot().refresh();
};

GALILEO.cylog.widget.CyLoGLeafNode.prototype.getNodeHtml = function () {

	var sb = [];
	sb[sb.length] = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
	sb[sb.length] = "<tr>";
	for (var i = 0; i < this.depth; ++i) {
		sb[sb.length] = "<td class=\"" + this.getDepthStyle(i) + "\"><div class=\"ygtvspacer\"></div></td>";
	}
	if (this.hasIcon) {
		sb[sb.length] = "<td";
		sb[sb.length] = " id=\"" + this.getToggleElId() + "\"";
		sb[sb.length] = " class=\"" + this.getStyle() + "\"";
		sb[sb.length] = " onclick=\"javascript:" + this.getToggleLink() + "\"";
		if (this.hasChildren(true)) {
			sb[sb.length] = " onmouseover=\"this.className=";
			sb[sb.length] = "YAHOO.widget.TreeView.getNode('";
			sb[sb.length] = this.tree.id + "'," + this.index + ").getHoverStyle()\"";
			sb[sb.length] = " onmouseout=\"this.className=";
			sb[sb.length] = "YAHOO.widget.TreeView.getNode('";
			sb[sb.length] = this.tree.id + "'," + this.index + ").getStyle()\"";
		}
		sb[sb.length] = "><div class=\"ygtvspacer\"></div></td>";
	}
	sb[sb.length] = "<td";
	sb[sb.length] = " id=\"" + this.contentElId + "\"";
	sb[sb.length] = " class=\"" + this.contentStyle + "\"";
	sb[sb.length] = (this.nowrap) ? " nowrap=\"nowrap\" " : "";
	sb[sb.length] = " >";
	sb[sb.length] = this.html;
	sb[sb.length] = "</td>";
	sb[sb.length] = "<td class=\"formTreeNode\">";
	sb[sb.length] = "<div>";
	sb[sb.length] = "<input type=\"checkbox\" name=\"trm-si-" + this._nodeName + "\"class=\"radio\" onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").chequearSi()\"";
	if (this._checkSi) {
		sb[sb.length] = " checked=\"checked\"";
	}
	sb[sb.length] =	" /> ";
	sb[sb.length] = "<input type=\"checkbox\" name=\"trm-no-" + this._nodeName + "\"class=\"textNo radio\" onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").chequearNo()\"";
	if (this._checkNo) {
		sb[sb.length] = " checked=\"checked\"";
	} 
	sb[sb.length] = " />";	
	sb[sb.length] = " <span><input type=\"text\" readonly=\"true\" class=\"textFecha cylog-treeview-fecha-desde\" name=\"trm-fDesde-" + this._nodeName + "\"";
	if (this._fechaDesde) {
		sb[sb.length] = " value=\"" + this._fechaDesde + "\"";
	}
	sb[sb.length] = " onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").showTreeCalendar(this)\" /></span>";
	sb[sb.length] = "   <span><input type=\"text\" readonly=\"true\" class=\"textFecha cylog-treeview-fecha-hasta\" name=\"trm-fHasta-" + this._nodeName + "\"";
	if (this._fechaHasta) {
		sb[sb.length] = " value=\"" + this._fechaHasta + "\"";	
	}
	sb[sb.length] = " onclick=\"javascript:YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + this.index + ").showTreeCalendar(this)\" /></span>";
	sb[sb.length] = "</div>";
	sb[sb.length] = "</td>";
	sb[sb.length] = "</tr>";
	sb[sb.length] = "</table>";
	return sb.join("");
};

