function FindObject(Objeto, d) 
{ 
	var p, i, x, d; 
	if (!d) 
	{ 
		d = document; 
	}	
	if ((p = Objeto.indexOf("?")) > 0 && parent.frames.length) 
	{ 
		d = parent.frames[Objeto.substring(p + 1)].document; 
		Objeto = Objeto.substring(0, p); 
	} 
	if (!(x = d[Objeto]) && d.all) 
	{ 
		x = d.all[Objeto]; 
	} 
	for (i = 0; !x && i < d.forms.length; i++) 
	{ 
		x = d.forms[i][Objeto]; 
	} 
	for (i = 0; !x && d.layers && i < d.layers.length; i++) 
	{ 
		x = FindObject(Objeto, d.layers[i].document); 
	} 
	if (!x && d.getElementById) 
	{ 
		x = d.getElementById(Objeto); 
	} 
	return x; 
}

function PreloadImages() 
{ 
	if (document.images) 
	{ 
		if (!document.MM_p) 
		{ 
			document.MM_p = new Array(); 
		} 
		var i, j = document.MM_p.length , Parameters = PreloadImages.arguments; 
		for (i = 0; i < Parameters.length; i++) 
		{ 
			if (Parameters[i].indexOf("#") != 0) 
			{ 
				document.MM_p[j] = new Image; 
				document.MM_p[j++].src = Parameters[i]; 
			} 
		} 
	} 
}

function SubmitForm(Form)
{
	if (typeof(Form) == "string") Form = document.getElementById(Form);
	if (Form.onsubmit()) Form.submit();
}

var fAjax = {
	requestAsync:function(url, ready)
	{
		var ajax = NuevoAjax();
		ajax.onreadystatechange=function()
		{
			if(ajax.readyState == 4)
			{
				if(ajax.status == 200)
				{
					if (ready != undefined && ready != null)
						ready(ajax);
				}
			}
		};
		ajax.open("GET", url, true);
		ajax.send(null);
	},
	
	requestSync:function(url, ready)
	{
		var ajax = NuevoAjax();
		ajax.open("GET", url, false);
		ajax.send(null);
		if (ready != undefined && ready != null)
			ready(ajax);
	}
}

function NuevoAjax()
{
	var xmlhttp = false;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			xmlhttp = false;
		}
	}

	if(!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

Number.prototype.FormatNumber = function(Decimals)
{
	var CompleteNumber = this;
	var Index = CompleteNumber.toString().indexOf(".");
	if (Index == -1)
	{
		CompleteNumber += ".00";	
	}
	else
	{
		CompleteNumber = CompleteNumber.toString().substr(0, Index + Decimals + 1);
		var Count = CompleteNumber.length;
		for (var i = 2; i > (Count - 1 - Index); i--)
		{
			CompleteNumber += "0";
		}
	}
	return CompleteNumber;
}

String.prototype.NLToBR = function()
{
	var Pat = new RegExp(String.fromCharCode(13),"g");
	var Pat2 = new RegExp(String.fromCharCode(10),"g");
	var Result = this.replace(Pat, "");
	Result = Result.replace(Pat2, "<br />");
	return Result;
}

String.prototype.ToJSON = function()
{
	try
	{
		return eval("(" + this + ")");
	}
	catch (e)
	{
		alert(this);
	}
}

String.prototype.BRToNL = function()
{
	var Result = this.replace(/<br \/>/g, String.fromCharCode(13));
	Result = Result.replace(/< br>/g, String.fromCharCode(13));
	Result = Result.replace(/<br >/g, String.fromCharCode(13));
	Result = Result.replace(/<br \/>/g, String.fromCharCode(13));
	Result = Result.replace(/<br \/>/g, String.fromCharCode(13));
	Result = Result.replace(/<br >/g, String.fromCharCode(13));
	Result = Result.replace(/< br>/g, String.fromCharCode(13));
	return Result;
}

String.prototype.trim = function()
{
	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

String.prototype.startsWith = function(str) 
{
	return (this.match("^"+str)==str);
}

String.prototype.endsWith = function(str) 
{
	return (this.match(str+"$")==str);
}

String.prototype.highlight = function(str) 
{
	var returnString = this;
	if (str != undefined && str != "")
	{
		str = str.replace(/<span style="background-color:#ddd;">/g, "").replace(/<\/span>/g, "");
		returnString = "";
		for (var i = 0; i < this.length; i++)
		{
			if (this.substr(i,str.length).toLowerCase() == str.toLowerCase())
			{
				returnString += '<span style="background-color:#ddd;">' + this.substr(i,str.length) + '</span>';
				i += str.length - 1;
			}
			else
			{
				returnString += this.substr(i,1);
			}
		}
	}
	return returnString;
}

function GetWindowSize(Index)
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	if (Index != null && Index != undefined)
	{
		return arrayPageSize[Index];
	}
	else
	{
		return arrayPageSize;
	}
}

function ChangeOpacity(Div, Value) 
{ 
	Div.style.opacity = Value; 
	Div.style.MozOpacity = Value; 
	Div.style.KhtmlOpacity = Value; 
	Div.style.filter = 'alpha(opacity=' + Value*100 + ')'; 
	Div.style.zoom=1;
	if (Value >= 1)
	{
		Div.style.filter = '';
	}
}

function GetScrollValue()
{
	return (window.scrollY != undefined) ? window.scrollY : document.documentElement.scrollTop;
}

function ScrollPage(Amount)
{
	var ActualScroll = GetScrollValue();
	window.scrollBy(0, (ActualScroll * -1));
	window.scrollBy(0, Amount);
}

function ScrollPageById(Id)
{
	var ActualScroll = GetScrollValue();
	var Amount = document.getElementById(Id).offsetTop + 270;
	window.scrollBy(0, (ActualScroll * -1));
	window.scrollBy(0, Amount);
}

function RenderSelectMultiple(Select, UseSearchBox)
{
	if (UseSearchBox == undefined) UseSearchBox = true;
	var ContainerDiv = document.createElement("div");
	ContainerDiv.style.overflowY = "scroll";
	ContainerDiv.style.border = "1px solid #000";
	if (Select.style.width != "")
	{
		ContainerDiv.style.width = Select.style.width;
	}
	if (Select.style.height != "")
	{
		ContainerDiv.style.height = Select.style.height;
	}
	var ContainerTable = document.createElement("div");
	ContainerTable.id = "container-ms-" + Select.name;
	var ContainerBox = document.createElement("div");
	var SearchBox = "";
	if (UseSearchBox == true)
	{
		SearchBox = '<div style="padding-top:2px; padding-left:2px"><input type="checkbox" onClick="SelectAllMultipleSelect(this.checked, \'' + Select.name + '\', document.getElementById(\'txtSearch-' + Select.name + '\').value);" /> <input type="text" name="txtSearch-' + Select.name + '" id="txtSearch-' + Select.name + '" onKeyUp="document.getElementById(\'container-ms-' + Select.name + '\').innerHTML = RenderSelectMultipleTable(\'' + Select.id + '\', this.value);" style="width:80%;" /></div>';
	}
	ContainerBox.innerHTML = SearchBox;
	ContainerTable.innerHTML = RenderSelectMultipleTable(Select);
	ContainerDiv.appendChild(ContainerBox);
	ContainerDiv.appendChild(ContainerTable);
	Select.parentNode.appendChild(ContainerDiv);
	Select.style.display = "none";
}

function RenderSelectMultipleTable(Select, Search)
{
	//Si me manda el id, lo convierto a objeto
	if (Select.options == undefined || Select.options == null) Select = document.getElementById(Select);
	
	if (Search == undefined) Search = "";
	var TableString = '<table cellspacing="0" cellpadding="0" border="0" width="100%">';
	for (var i = 0; i < Select.options.length; i++)
	{
		if (Select.options[i].innerHTML.toLowerCase().startsWith(Search.toLowerCase()))
		{
			TableString += '<tr><td style="border-bottom:1px solid #ddd; width:10px"><input style="width:auto; height:auto;" type="checkbox" onClick="document.getElementById(\'' + Select.id + '\').options[' + i + '].selected = this.checked;" name="s' + Select.name + "-" + i + '" id="s' + Select.name + "-" + i + '"';
			if (Select.options[i].selected)
			{
				TableString += ' checked';
			}
			TableString += '/></td><label for="s' + Select.name + "-" + i + '"><td align="left" style="border-bottom:1px solid #ddd"><label for="s' + Select.name + "-" + i + '">' + Select.options[i].innerHTML.highlight(Search) + '</label></td></label></tr>';
		}
	}
	TableString += '</table>';
	return TableString;
}

function SelectAllMultipleSelect(Operation, Select, Search)
{
	//Si me manda el id, lo convierto a objeto
	if (Select.options == undefined || Select.options == null) Select = document.getElementById(Select);
	if (Search == undefined) Search = "";
	for (var i = 0; i < Select.options.length; i++)
	{
		if (Select.options[i].innerHTML.toLowerCase().startsWith(Search.toLowerCase()))
		{
			Select.options[i].selected = Operation;
		}
	}
	document.getElementById('container-ms-' + Select.name).innerHTML = RenderSelectMultipleTable(Select, Search);
}

function MakePageHeightAll(TableId)
{
	var Table = document.getElementById(TableId);
	if (Table.offsetHeight < GetWindowSize(3))
	{
		Table.style.height = GetWindowSize(3) + "px";
	}
}

function SelectAllCheckboxs(State, Checkboxs)
{
	for (var i = 0; i < Checkboxs.length; i++)
	{
		Checkboxs[i].checked = State;
	}
}

var Intervals = new Array();
function AnimateDiv(Div, Args)
{
	if (Args != undefined)
	{
		if (Args.w != undefined)
		{
			var ChangeValue = "+" + Args.v;
			var Direction = ">";
			if (parseInt(document.getElementById(Div).style.width) > Args.w)
			{
				ChangeValue = "-" + Args.v;
				Direction = "<";
			}
			var Position = Intervals.length;
			Intervals[Position] = setInterval(GetAnimationFunction("width", Div, ChangeValue, Args.w, Position, Direction), 10);
		}
		if (Args.h != undefined)
		{
			var ChangeValue = "+" + Args.v;
			var Direction = ">";
			if (parseInt(document.getElementById(Div).style.height) > Args.h)
			{
				ChangeValue = "-" + Args.v;
				Direction = "<";
			}
			var Position = Intervals.length;
			Intervals[Position] = setInterval(GetAnimationFunction("height", Div, ChangeValue, Args.h, Position, Direction), 10);
		}
		if (Args.l != undefined)
		{
			var ChangeValue = "+" + Args.v;
			var Direction = ">";
			if (parseInt(document.getElementById(Div).style.left) > Args.l)
			{
				ChangeValue = "-" + Args.v;
				Direction = "<";
			}
			var Position = Intervals.length;
			Intervals[Position] = setInterval(GetAnimationFunction("left", Div, ChangeValue, Args.l, Position, Direction), 10);
		}
		if (Args.t != undefined)
		{
			var ChangeValue = "+" + Args.v;
			var Direction = ">";
			if (parseInt(document.getElementById(Div).style.top) > Args.t)
			{
				ChangeValue = "-" + Args.v;
				Direction = "<";
			}
			var Position = Intervals.length;
			Intervals[Position] = setInterval(GetAnimationFunction("top", Div, ChangeValue, Args.t, Position, Direction), 10);
		}
	}
}

function GetAnimationFunction(Property, Div, ChangeValue, EndValue, Position, Direction)
{
	var Return = "document.getElementById('" + Div + "').style." + Property + " = (parseInt(document.getElementById('" + Div + "').style." + Property + ")" + ChangeValue + ") + 'px';";
	Return += "if (parseInt(document.getElementById('" + Div + "').style." + Property + ") " + ChangeValue + " " + Direction + " " + EndValue + ") document.getElementById('" + Div + "').style." + Property + " = '" + EndValue + "px';"
	Return += "if (parseInt(document.getElementById('" + Div + "').style." + Property + ") " + Direction + "= " + EndValue + ") clearInterval(Intervals[" + Position + "]);";
	return Return;
}