﻿// ************************************************************************************
// Popups
// ************************************************************************************
function openPopup(url, width, height)
{
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	var w = window.open(url, null, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=" + width + ",height=" + height + ",left=" + x + ",top=" + y, "");
	if (w)
	{
		w.focus();
	}
}
function openNamedPopup(name, url, width, height)
{
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	var w = window.open(url, name, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=" + width + ",height=" + height + ",left=" + x + ",top=" + y, "");
	if (w)
	{
		w.focus();
	}
}
function openPopup2(url, width, height, name)
{
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	var w = window.open(url, name, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=" + width + ",height=" + height + ",left=" + x + ",top=" + y, "");
	if (w)
	{
		w.focus();
	}
}
function openPopupScroll(url, width, height)
{
	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	var w = window.open(url, null, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=" + width + ",height=" + height + ",left=" + x + ",top=" + y, "");
	if (w)
	{
		w.focus();
	}
}



// **********************************************************************
// Global
// **********************************************************************
function RegisterNamespace(ns)
{
	var spaces = ns.split(".");
	if (spaces.length > 0)
	{
		var root = window[spaces[0]];
		if (!root)
		{
			root = new Object();
			window[spaces[0]] = root;
		}
		if (spaces.length > 1)
		{
			var parentSpace = root;
			for (var i = 1; i < spaces.length; i++)
			{
				var name = spaces[i];
				if (!parentSpace[name])
				{
					parentSpace[name] = new Object();
				}
				parentSpace = parentSpace[name];
			}
		}
	}
}
function CreateDelegate(instance, method)
{
	var rv = null;
	rv = function()
	{
		method.apply(instance);
	};
	return rv;
}



// **********************************************************************
// Competir.Web
// **********************************************************************
RegisterNamespace("Competir.Web");
Competir.Web.GetQueryStringVariable = function(name)
{
	var rv = "";
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i ++)
	{
		var pair = vars[i].split("=");
		if (pair[0].toLowerCase() == name.toLowerCase())
		{
			rv = pair[1];
		}
	}
	return rv;
};



// **********************************************************************
// Competir.Web.UI
// **********************************************************************
RegisterNamespace("Competir.Web.UI");
Competir.Web.UI.Over = function(obj)
{
	if (obj.tagName.toLowerCase() == "img")
	{
		obj.src = obj.src.replace(/\.(?=gif|jpg|png)/, "_over.");
	}
};
Competir.Web.UI.Out = function(obj)
{
	if (obj.tagName.toLowerCase() == "img")
	{
		obj.src = obj.src.replace("_over", "");
	}
};
Competir.Web.UI.FixMozillaDisplay = function(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.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.ShowHideExceptionInfo = function(id)
{
	var objLink = document.getElementById("lnk_info_" + id);
	var objDiv = document.getElementById("div_info_" + id);
	if (objDiv)
	{
		if (objDiv.style.display == "none")
		{
			objDiv.style.display = "block";
			objLink.innerText = "Ocultar detalles";
		}
		else
		{
			objDiv.style.display = "none";
			objLink.innerText = "Mostrar detalles";
		}
	}
};
Competir.Web.UI.SWFObject = function(movie, id, width, height, scale, salign, menu, bgcolor, quality)
{
	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 =
{
	variables: new Object(),
	attributes: new Object(),
	elementAttributes: new Array(),
	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;
					*/
					n.innerHTML = this.getSWFHTML();
				}
			}
			else
			{
				if (this.getAttribute("src"))
				{
					n.innerHTML = this.getSWFHTML();
				}
			}
		}
	}
};



// **********************************************************************
// Competir.Utils
// **********************************************************************
RegisterNamespace("Competir.Utils");
Competir.Utils.DecodeBase64 = function(input)
{
	var rv = "";
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var chr1;
	var chr2;
	var chr3;
	var enc1;
	var enc2;
	var enc3;
	var enc4;
	var i = 0;

	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	do
	{
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		rv += String.fromCharCode(chr1);

		if (enc3 != 64)
		{
			rv += String.fromCharCode(chr2);
		}
		if (enc4 != 64)
		{
			rv += String.fromCharCode(chr3);
		}
	}
	while (i < input.length);
	
	return rv;
};
Competir.Utils.TrimStart = function(input, prefix)
{
	var rv = input;
	prefix = prefix.replace("?", "\\?");
	prefix = prefix.replace("^", "\\^");
	prefix = prefix.replace("$", "\\$");
	var re = new RegExp("^" + prefix, "i");
	rv = rv.replace(re, "");
	return rv;
};
Competir.Utils.TrimEnd = function(input, suffix)
{
	var rv = input;
	suffix = suffix.replace("?", "\\?");
	suffix = suffix.replace("^", "\\^");
	suffix = suffix.replace("$", "\\$");
	var re = new RegExp(suffix + "$", "i");
	rv = rv.replace(re, "");
	return rv;
};