// ************************************************************************************
// Competir.Web.UI
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.Web.UI");

// static methods
Competir.Web.UI.setInnerHTML = function(obj, html)
{
	if (obj)
	{
		obj.innerHTML = html;
		if (navigator.userAgent.indexOf("MSIE") == -1)
		{
			Competir.Web.UI.fixMozillaDisplay(obj);
		}
	}
};
Competir.Web.UI.toggleImage = function(obj)
{
	if (obj)
	{
		// obj
		if (String.isInstanceOfType(obj))
		{
			obj = $get(obj);
		}

		// value
		var objSrc = obj.getAttribute("src").toLowerCase();
		var altSrc = obj.getAttribute("altsrc").toLowerCase();

		// apply
		if (obj)
		{
			obj.setAttribute("altsrc", objSrc);
			obj.setAttribute("src", altSrc);
		}
	}
};
Competir.Web.UI.toggleDisplay = function(obj, value)
{
	if (obj)
	{
		// obj
		if (String.isInstanceOfType(obj))
		{
			var objRef = $find(obj);
			if (!objRef)
			{
				obj = $get(obj);
			}
			else
			{
				obj = objRef;
			}
		}

		// value
		if (value == null | value == undefined)
		{
			if (Sys.UI.Control.isInstanceOfType(obj))
			{
				value = !obj.get_visible();
			}
			else
			{
				value = (obj.style.display == "none");
			}
		}

		// apply
		if (obj)
		{
			if (Sys.UI.Control.isInstanceOfType(obj))
			{
				obj.set_visibilityMode(Sys.UI.VisibilityMode.collapse);
				obj.set_visible(value);
				if (value)
				{
					obj.raiseEvent("onShow", Sys.EventArgs.Empty);
				}
				else
				{
					obj.raiseEvent("onHide", Sys.EventArgs.Empty);
				}
			}
			else
			{
				obj.style.display = (value) ? "block": "none";
			}
		}

		// fix
		if (obj)
		{
			Competir.Web.UI.fixMozillaDisplay(obj);
		}
	}
};
Competir.Web.UI.show = function(obj)
{
	Competir.Web.UI.toggleDisplay(obj, true);
};
Competir.Web.UI.hide = function(obj)
{
	Competir.Web.UI.toggleDisplay(obj, false);
};
Competir.Web.UI.fixMozillaDisplay = function(obj)
{
	if (obj)
	{
		if (navigator.userAgent.indexOf("MSIE") == -1)
		{
			var refNode = obj.parentNode;
			while (refNode && refNode != document.body)
			{
				if (refNode.tagName.toLowerCase() == "table" && refNode.getAttribute("refresh") == "true")
				{
					var r = refNode.insertRow(-1);
					var t = r.insertCell(-1);
					refNode.deleteRow(refNode.rows.length - 1);
					break;
				}
				else
				{
					refNode = refNode.parentNode;
				}
			}
		}
	}
};
Competir.Web.UI.validateFormElement = function(control, id, controlType, validationType, blink)
{
	var rv = false;
	switch (validationType)
	{
		case "hasValue":
			switch (controlType)
			{
				case "text":
				case "combo":
					var obj = control.getChild(id);
					if (obj)
					{
						if (obj.value && obj.value != "")
						{
							rv = true;
						}
						else if (blink)
						{
							control.callFromBehavior("blink", [obj]);
						}
					}
					break;
				case "radio":
					var group = document.getElementsByName(control.get_ClientID() + id);
					if (group.length)
					{
						for (var i = 0; i < group.length; i++)
						{
							if (group[i].checked)
							{
								rv = true;
								break;
							}
						}
					}
					if (!rv && blink)
					{
						for (var i = 0; i < group.length; i++)
						{
							control.callFromBehavior("blink", [group[i].parentNode]);
						}
					}
					break;
			}
			break;
		case "insensitiveEquality":
			switch (controlType)
			{
				case "text":
				case "combo":
					rv = true;
					var value = "";
					var arrObj = new Array();
					var arrId = id.split(",");
					for (var i = 0; i < arrId.length; i++)
					{
						var obj = control.getChild(arrId[i]);
						if (obj)
						{
							arrObj.push(obj);
						}
					}
					for (var i = 0; i < arrObj.length; i++)
					{
						if (value == "")
						{
							value = arrObj[i].value.toLowerCase();
						}
						else
						{
							if (arrObj[i].value.toLowerCase() != value)
							{
								rv = false;
								break;
							}
						}
					}
					if (!rv && blink)
					{
						for (var i = 0; i < arrObj.length; i++)
						{
							control.callFromBehavior("blink", [arrObj[i]]);
						}
					}
					break;
			}
			break;
		case "email":
			switch (controlType)
			{
				case "text":
				case "combo":
					var obj = control.getChild(id);
					if (obj)
					{
						if (obj.value && obj.value != "")
						{
							var re = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
							rv = re.test(obj.value);
						}
						if (!rv && blink)
						{
							control.callFromBehavior("blink", [obj]);
						}
					}
					break;
			}
			break;
		case "numeric":
			switch (controlType)
			{
				case "text":
				case "combo":
					var obj = control.getChild(id);
					if (obj)
					{
						if (obj.value && obj.value != "")
						{
							rv = !obj.value.match(/\D/);
						}
						if (!rv && blink)
						{
							control.callFromBehavior("blink", [obj]);
						}
					}
					break;
			}
			break;
		case "date":
			switch (controlType)
			{
				case "text":
				case "combo":
					var arrObj = new Array();
					var arrId = id.split(",");
					for (var i = 0; i < arrId.length; i++)
					{
						var obj = control.getChild(arrId[i]);
						if (obj)
						{
							arrObj.push(obj);
						}
					}
					if (arrId.length == 3)
					{
						var dd = parseInt(arrObj[0].value, 10);
						var MM = parseInt(arrObj[1].value, 10) - 1;
						var yyyy = parseInt(arrObj[2].value, 10);
						var date = new Date(yyyy, MM, dd);
						if (date.getFullYear() == yyyy)
						{
							if (date.getMonth() == MM)
							{
								if (date.getDate() == dd)
								{
									rv = true;
								}
							}
						}
					}
					if (!rv && blink)
					{
						for (var i = 0; i < arrObj.length; i++)
						{
							control.callFromBehavior("blink", [arrObj[i]]);
						}
					}
					break;
			}
			break;
	}
	return rv;
};
Competir.Web.UI.getFormElementValue = function(control, id, type)
{
	var rv = "";
	switch (type)
	{
		case "text":
		case "combo":
			var obj = control.getChild(id);
			if (obj)
			{
				rv = obj.value;
			}
			break;
		case "radio":
			var group = document.getElementsByName(control.get_ClientID() + id);
			if (group.length)
			{
				for (var i = 0; i < group.length; i++)
				{
					if (group[i].checked)
					{
						rv = group[i].value;
						break;
					}
				}
			}
			break;
	}
	return rv;
};
Competir.Web.UI.getKeyCode = function(e)
{
	var rv = 0;
	if (!e)
	{
		if (window.event)
		{
			e = window.event;
		}
	}
	if (e)
	{
		if (e.which)
		{
			rv = e.which;
		}
		else if (e.keyCode)
		{
			rv = e.keyCode;
		}
	}
	return rv;
};
Competir.Web.UI.cancelEvent = function(e)
{
	if (!e)
	{
		if (window.event)
		{
			e = window.event;
		}
	}
	if (e.preventDefault && e.stopPropagation)
	{
		e.preventDefault();
		e.stopPropagation();
	}
	else
	{
		e.returnValue = false;
		e.cancelBubble = true;
	}
	return false;
};
Competir.Web.UI.DomObjectMethodExecutor = function(id, methodName)
{
	this.exec = function()
	{
		var s = "document.getElementById(\"" + id + "\")." + methodName + "();";
		eval(s);
	};
};



// ************************************************************************************
// SWFObject
// ************************************************************************************
// namespace
Type.registerNamespace("Competir.Web.UI");

// constructor
Competir.Web.UI.SWFObject = function(movie, id, width, height, scale, salign, menu, bgcolor, quality)
{
	this.variables = new Object();
	this.attributes = new Object();
	this.elementAttributes = new Array();
	if (id)
	{
		this.setAttribute("id", id);
		this.setAttribute("name", id);
	}
	if (width)
	{
		this.setAttribute("width", width);
	}
	if (height)
	{
		this.setAttribute("height", height);
	}
	if (navigator.userAgent.indexOf("IE") != -1)
	{
		this.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
		this.setAttribute("codebase", "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0");
		if (movie)
		{
			this.addParam("movie", movie);
		}
		if (scale)
		{
			this.addParam("scale", scale);
		}
		if (salign)
		{
			if (salign.toLowerCase() != "cc")
			{
				this.addParam("salign", salign);
			}
		}
		if (menu)
		{
			this.addParam("menu", menu);
		}
		if (bgcolor)
		{
			if (bgcolor.toLowerCase() == "transparent")
			{
				this.addParam("wmode", "transparent");
			}
			else
			{
				this.addParam("bgcolor", bgcolor);
				this.addParam("wmode", "opaque");
			}
		}
		else
		{
			this.addParam("wmode", "transparent");
		}
		this.addParam("quality", quality ? quality: "high");
		this.addParam("allowscriptaccess", "always");
	}
	else
	{
		if (movie)
		{
			this.setAttribute("src", movie);
		}
		if (scale)
		{
			this.setAttribute("scale", scale);
		}
		if (salign)
		{
			if (salign.toLowerCase() != "cc")
			{
				this.setAttribute("salign", salign);
			}
		}
		if (menu)
		{
			this.setAttribute("menu", menu);
		}
		if (bgcolor)
		{
			if (bgcolor.toLowerCase() == "transparent")
			{
				this.setAttribute("wmode", "transparent");
			}
			else
			{
				this.setAttribute("bgcolor", bgcolor);
				this.setAttribute("wmode", "opaque");
			}
		}
		else
		{
			this.setAttribute("wmode", "transparent");
		}
		this.setAttribute("quality", quality ? quality: "high");
		this.setAttribute("allowScriptAccess", "always");
		this.setAttribute("type", "application/x-shockwave-flash");
	}
};
Competir.Web.UI.SWFObject.prototype =
{
	addParam: function(key, value)
	{
		if (!this.params)
		{
			this.params = new Object();
		}
		this.params[key] = value;
	},
	getParam: function(key)
	{
		return this.params[key];
	},
	setAttribute: function(key, value)
	{
		this.attributes[key] = value;
	},
	getAttribute: function(key)
	{
		return this.attributes[key];
	},
	setElementAttribute: function(key, value)
	{
		this.elementAttributes[key] = value;
	},
	getElementAttribute: function(key)
	{
		return this.elementAttributes[key];
	},
	addVariable: function(key, value)
	{
		this.variables[key] = value;
	},
	getVariable: function()
	{
		return this.variables[key];
	},
	getVariablePairs: function()
	{
		var rv = new Array();
		for (var key in this.variables)
		{
			rv.push(key + "=" + this.variables[key]);
		}
		return rv;
	},
	applyVariables: function()
	{
		var value = this.getVariablePairs().join("&");
		if (navigator.userAgent.indexOf("IE") != -1)
		{
			this.addParam("flashvars", value);
		}
		else
		{
			this.setAttribute("flashvars", value);
		}
	},
	getSWFHTML: function()
	{
		var rv = "";
		var key;
		var tagName = "";
		if (navigator.userAgent.indexOf("IE") != -1)
		{
			tagName += "OBJECT";
		}
		else
		{
			tagName += "EMBED";
		}
		rv += "<" + tagName;
		this.applyVariables();
		for (key in this.attributes)
		{
			rv += " " + key + "=\"" + this.attributes[key] + "\"";
		}
		for (key in this.elementAttributes)
		{
			rv += " " + key + "=\"" + this.elementAttributes[key] + "\"";
		}
		if (this.params)
		{
			rv += ">";
			for (key in this.params)
			{
				rv += "<param name=\"" + key + "\" value=\"" + this.params[key] + "\"/>";
			}
			rv += "</" + tagName + ">";
		}
		else
		{
			rv += "/>";
		}
		return rv;
	},
	write: function(id)
	{
		var n = (typeof id == "string") ? document.getElementById(id): id;
		if (n.tagName.toLowerCase() == "textarea")
		{
			n.value = this.getSWFHTML();
		}
		else
		{
			if (navigator.userAgent.indexOf("IE") != -1)
			{
				if (this.getParam("movie"))
				{
					var objProxy = new Object();
					objProxy.id = this.getAttribute("id");
					objProxy.CallFunction = function(request)
					{
						document.getElementById(this.id).CallFunction(request);
					};
					objProxy.SetReturnValue = function(value)
					{
						document.getElementById(this.id).SetReturnValue(value);
					};
					window[this.getAttribute("id")] = objProxy;

					this.applyVariables();
					Competir.Web.UI.setInnerHTML(n, this.getSWFHTML());
				}
			}
			else
			{
				if (this.getAttribute("src"))
				{
					Competir.Web.UI.setInnerHTML(n, this.getSWFHTML());
				}
			}
		}
	}
};

// registration
Competir.Web.UI.SWFObject.registerClass("Competir.Web.UI.SWFObject", Sys.Component);



// ************************************************************************************
// Misc
// ************************************************************************************
// exceptions
function ShowHideExceptionInfo(id)
{
	var objLink = document.getElementById("div_ex_link_" + id);
	var objDiv = document.getElementById("div_ex_info_" + id);
	if (objDiv)
	{
		if (objDiv.style.display == "none")
		{
			objDiv.style.display = "block";
			if (objLink)
			{
				Competir.Web.UI.setInnerHTML(objLink, "Ocultar detalles");
			}
		}
		else
		{
			objDiv.style.display = "none";
			if (objLink)
			{
				Competir.Web.UI.setInnerHTML(objLink, "Mostrar detalles");
			}
		}
	}
}