﻿function OpenWindow(theUrl, theWinName, theWidth, theHeight, theFeatures)
{
	this.theUrl       = theUrl;
	this.theWinName   = theWinName;
	this.theWidth     = theWidth;
	this.theHeight    = theHeight;
	this.theFeatures  = theFeatures;
	
	var theLeft       = (window.screen.width - this.theWidth ) / 2;
	var theTop        = (window.screen.height - this.theHeight) / 2;
	var Features      = 'left='+theLeft+',top='+theTop+',width='+this.theWidth+',height='+this.theHeight;	
	if (this.theFeatures.replace(/ /gi, "").length > 0) Features = Features + ',' + this.theFeatures;			

	hWnd = window.open (this.theUrl, this.theWinName, Features);
	hWnd.focus();
}

function OpenModalWindow(theUrl, theWinName, theArguments, theWidth, theHeight, theFeatures)
{
	this.theUrl       = theUrl;
	this.theWinName   = theWinName;
	this.theWidth     = theWidth;
	this.theHeight    = theHeight;
	this.theFeatures  = theFeatures;
	this.theArguments  = theArguments;
		
    if (!window.showModalDialog) 
    {
    	var Features      = 'dialogWidth:'+this.theWidth+'px,dialogHeight:'+this.theHeight+'px,center:yes';	
    	if (this.theFeatures.replace(/ /gi, "").length > 0) Features = Features + ',' + this.theFeatures;
	    hWnd = window.showModalDialog (this.theUrl, this.theArguments, Features);
    } 
    else 
    {
        OpenWindow(this.theUrl, this.theWinName, this.theWidth, this.theHeight, this.theFeatures + ',modal=yes');
    }
}

function CharacterCounter(theField, theLabel, Limit)
{
	if (theField.value.length < Limit)
	{
		theLabel.innerText = Limit - theField.value.length;
		return true;
	}
	else
	{
		theLabel.innerText = 0;
		theField.value = theField.value.substr(0,Limit);
		return false;			
	}
}

	function CopyListElements(SourceList, TargetList, AllowRepeat)
	{
		var newOption;
		var isRepeat;

		for (var i = 0; i < SourceList.options.length; i++)
		{
			isRepeat = false;
			if (SourceList.options[i].selected == true)
			{
				if (AllowRepeat == false)
				{
					for (var j = 0; j < TargetList.options.length; j++)
					{
						if (SourceList.options[i].value == TargetList.options[j].value)
						{
							isRepeat = true;
							break;
						}
					}
				}
				if (isRepeat == false)
				{
					newOption = new Option;
					newOption.value = SourceList.options[i].value;
					newOption.text	= SourceList.options[i].text;
					TargetList.options[TargetList.options.length] = newOption;					
				}
			}
		}
	}

	function RemoveListElements(TargetList, RemoveFirstElement, RemoveFirstElementError)
	{
		for (var i = TargetList.options.length - 1; i >= 0; i--) 
		{
			if (TargetList.options[i].selected == true)
			{
				if ((i == 0 && RemoveFirstElement == true) || i > 0)
				{
					TargetList.options[i] = null;
				}
				else
				{
					if(RemoveFirstElementError.length > 0)
					{
						alert(RemoveFirstElementError);
					}
				}
			}
		}
	}

	function RemoveListElement(theList, theErrorMessage) 
	{
		if (theList.length > 0) 
		{
			if (theList.options.selectedIndex != -1) 
			{ 
				theList.options[theList.options.selectedIndex] = null;
			}
			else
			{
				if (theErrorMessage.length > 0)
				{
					alert(theErrorMessage);
				}
				theControl.focus();
			}
		}
	}
	function AddListElement(theId, theItem, theList) 
	{
		var NewOption	= new Option(theItem, theId)
		var OptionIndex = theList.options.length;

		for (var i = 0; i < theList.options.length; i++)
		{
			if (theList.options[i].value == theId)
			{
				theId = null;
				break;
			}
		}
		if (theId != null) theList.options[OptionIndex] = NewOption;
	}

	function MoveListElement(sourceSelect, theDirection)
	{	

		if (sourceSelect.length > 1) 
		{
			var options = sourceSelect.options;

			// find which ones are selected
			var selectedIds = new Array ();
			var index = 0;
			var selId;

			
			if (theDirection == 'up')
			{
				for (var i = 1; i < sourceSelect.length; i++) 
				{
					if (options[i].selected) 
					{
						selectedIds[index] = i;
						index++;
					}
				}

				// move each selected option up
				for (var i = 0; i < selectedIds.length; i++) 
				{
					selId = selectedIds[i];
					privateMoveUp (options, selId);
					options[selId].selected = false;
					options[selId-1].selected = true;
				}
			}
			else
			{
				for (var i = sourceSelect.length-2; i >= 0; i--)
				{
					if (sourceSelect.options[i].selected) 
					{
						// add any remaining selected elements to our array of elements to move
						selectedIds[index] = i;
						index++;
					}
				}

				// move each selected element down
				for (var i = 0; i < selectedIds.length; i++) 
				{
					selId = selectedIds[i];
					privateMoveDown (options, selId);
					options[selId].selected = false;
					options[selId+1].selected = true;
				}
			}
			sourceSelect.focus ();
		}
	}
	
	/*
	 * Do not call this function directly.
	 * As it does NO bounds checking.
	 * Please use the moveUp or moveTop calls.
	 */
	function privateMoveUp (options, index) {
		var newOption = new Option (options[index-1].text, options[index-1].value);
		options[index-1].text = options[index].text;
		options[index-1].value = options[index].value;
		options[index].text = newOption.text;
		options[index].value = newOption.value;
	}

	/*
	 * Do not call this function directly.
	 * As it does NO bounds checking.
	 * Please use the moveDown or moveBottom calls.
	 */
	function privateMoveDown (options, index) {
		var newOption = new Option (options[index+1].text, options[index+1].value);
		options[index+1].text = options[index].text;
		options[index+1].value = options[index].value;
		options[index].text = newOption.text;
		options[index].value = newOption.value;
	}

	
	function SelectAllListElements(theList, theSelection)
	{
		this.theList = theList;
		this.theSelection = theSelection;

		for (var i = 0; i < this.theList.options.length; i++)
		{
			this.theList.options[i].selected = this.theSelection;
		}
	}
	
	
	function DependencyListObject(theID, theName, theParentID)
	{
		this.theID = theID;
		this.theName = theName;
		this.theParentID = theParentID;
	}
	
	function GetListSelectedOption(theList)
	{
		selectedOption = parseInt(-1);
		for (var i = 0; i < theList.options.length; i++)
				{
				if(theList.options[i].selected) 
						{
						selectedOption = parseInt(i);
						}
				}
		return selectedOption;
	}

	//FUNCION PARA REDIRECCIONAR SEGUN NAVEGADOR (si se entra con un mobil)
	function redireccion(pagina)	
	{
		var nav;
		
		nav = navigator.appName +" "+ navigator.appVersion
		//alert(nav);
		if (nav.indexOf("IEMobile") != -1)
		{
			document.location = "/mobil/" + pagina;
		}
		else if (nav.indexOf("Blackberry") != -1)
		{
			document.location = "/mobil/" + pagina;
		}
		else if (nav.indexOf("iPhone") != -1)
		{
			document.location = "/mobil/" + pagina;
		}
	}
	//FIN FUNCION PARA REDIRECCIONAR
	
	//FUNCION PARA REDIRECCIONAR SEGUN NAVEGADOR (si se entra con un PC)
	function redireccionNormal(pagina)	
	{
		var nav;
		
		nav = navigator.appName +" "+ navigator.appVersion
		//alert(nav);
		if (nav.indexOf("Explorer") != -1)
		{
			document.location = "/" + pagina;
		}
		else if (nav.indexOf("Netscape") != -1)
		{
			document.location = "/" + pagina;
		}
		else if (nav.indexOf("Opera") != -1)
		{
			document.location = "/" + pagina;
		}
		else if (nav.indexOf("Safari") != -1)
		{
			document.location = "/" + pagina;
		}
	}
	//FIN FUNCION PARA REDIRECCIONAR