﻿// CONTROL
var Control = function( id )
{
	this.id      = id;
	this.OnFocus = new EventHandler();
	this.OnBlur  = new EventHandler();
	
	if( id )
		this.setElement( document.getElementById( id ) );
}

Control.prototype = new function()
{
	this.id             = null;
	this.element        = null;//|
    this.htmlRoot       = null;//| Los tres iguales. hmltRoot y renderOuput ya no los uso, son de versiones viejas.
    this.renderOutput   = null;//|
    
    this.postBackHidden = null;
    this.renderLocked   = false;
    
    this.parentFocus    = null;

	this.OnFocus        = null;
	this.OnBlur         = null;
	
	this.minValue       = null;
	this.maxValue       = null;
	
	this.getBrowser = function()
    {
		return browser;
    }
    
    this.setElement = function( element )
    {
		try
		{
	        this.htmlRoot        = element;
			this.renderOutput    = element;
	        this.element         = element;
			this.element.control = this;
		}
		catch( e )
		{
			throw( "No se pudo inicializar el Control.\r\n"+ e.message );
		}
    }
    
    this.serialize = serialize; function serialize( form )
    {
    
    }
    
    this.setPostBackValue = setPostBackValue; function setPostBackValue( form, value, name )
	{
		if( this.postBackHidden == null )
		{	
			this.postBackHidden      = document.createElement( "input" );
			this.postBackHidden.type = "hidden";
			
			if( name )
				this.postBackHidden.name = name;
			else
				this.postBackHidden.name = this.id;
			
			form.appendChild( this.postBackHidden );
		}
		
		this.postBackHidden.value = value;
	}
	
	this.IsEmpty = IsEmpty; function IsEmpty()
	{
		return this.element.value == "";
	}
    
    this.IsEnabled  = function(){ return !this.element.disabled; }
    this.SetEnabled = function( value ){ this.element.disabled = !value; }
    
    this.GetClientPosition = function(){ return browser.GetClientPosition( this.element ); }
    this.GetClientRectangle = function(){ return browser.GetClientRectangle( this.element ); }
    
    this.SetPosition = function( top, left )
    {
		this.element.style.top  = top;
		this.element.style.left = left;
    }
    
    this.GetWidth = function(){ return this.element.offsetWidth; }
    this.SetWidth = function SetWidth( value ){ this.element.style.width = value; }
    
	this.GetHeight = function(){ return this.element.offsetHeight; }
	this.SetHeight = function( value ){ this.element.style.height = value; }
    
	this.IsOnFocus = IsOnFocus; function IsOnFocus()
	{
		return ControlHandler.IsOnFocus( this );
	}
	
	this.Focus = Focus; function Focus()
	{
		if( !this.IsOnFocus() )
		{
			ControlHandler.setOnFocus( this );
			this.onFocus();
		}
	}
	
	this.onFocus = onFocus; function onFocus()
	{
		//browser.Trace( "Gana el foco:" + this.id );
		
		this.OnFocus.Raise( this );
	}
	
	this.Blur = Blur; function Blur( control )
	{
		if( this.IsOnFocus() )
		{
			ControlHandler.controlOnFocus = null;
			this.onBlur( control );
		}
	}
	
	this.onBlur = onBlur; function onBlur()
	{
		//browser.Trace( "Pierde el foco:" + this.id );
		
		this.OnBlur.Raise( this );
	}
	
	this.lockRender   = function(){ this.renderLocked = true; }
	this.unlockRender = function(){ this.renderLocked = false; }
	
	// Metodos que se utilizan para crear html del control.
	this.createElement = createElement; function createElement( tagName )
    {
		var element;
	
		element              = document.createElement( tagName );
		element.control      = this;
		element.AddElement   = addElement;
		element.RemoveChilds = RemoveChilds;

		return element;
    }
    
    function addElement( element )
    {
		this.appendChild( element );
    }
    
    function RemoveChilds()
    {
		var i;
		var childs;

		childs = this.childNodes.length;

		for( i = 0; i < childs; i++ )
			this.removeChild( this.childNodes[ 0 ] );
    }
    
    this.Dispose = function()
    {
		if( this.IsOnFocus() )
			this.Blur();
			
		this.element.parentNode.removeChild( this.element );
    }
}

Control.FromElement = function( element )
{
	var control = null;
	
	while( element != null && control == null )
	{
		if( element.control )
			control = element.control;
		
		element = element.parentNode;
	}
	
	return control;
}

Control.FromElementId = function( elementId )
{
	var element = browser.GetElementById( elementId );
	
	if( element != null && element.control )
		return element.control;
	else
		return null;
}

// Control basado en un tag html del tipo Input
var InputControl = function( id )
{
	Extends( Control, this, id );
	
	this.ValueChanged   = new EventHandler();
	this.EnabledChanged = new EventHandler();
}	

InputControl.prototype = new function()
{
	this.ValueChanged   = null;
	this.EnabledChanged = null;
	
	this.setElement = function( element )
	{
		this.base.setElement( element );
		
		browser.AttachEvent( this.element, "focus", new pox.Delegate( this, this.Focus ) );
		browser.AttachEvent( this.element, "blur", new pox.Delegate( this, this.Blur ) );
	}
	
	this.IsEnabled  = function(){ return !this.element.disabled; }
	this.SetEnabled = function( value )
	{
		if( this.element.disabled != !value )
		{
			this.element.disabled = !value;
			this.EnabledChanged.Raise( this );
		}
	}
}

// TEXTBOX
var TextBox = function( id )
{
	Extends( InputControl, this, id );
}

TextBox.prototype = new function()
{
	this.SetValue = SetValue; function SetValue( value )
	{
		this.element.value = value;
	}
	
	this.GetValue = GetValue; function GetValue()
	{
		if( !this.IsEmpty() )
			return this.element.value;
		else	
			return null;
	}
	
	//Override( this, "onBlur", onBlur );
	this.onBlur = onBlur; function onBlur()
	{
		this.element.blur();
		
		this.base.onBlur();
	}
	
	this.Render = Render; function Render( output )
	{
		this.element                = this.createElement( "input" );
		this.element.type           = "text";
		this.element.style.position = "absolute";
		
		output.appendChild( this.element );
		
		this.setElement( this.element );
	}
}

// DateTimeBox -- NO VA MAS --
var DateTimeBox = function( id )
{
	Extends( TextBox, this, id );
}

// Button
var Button = function( id )
{
	Extends( Control, this, id );
	
	this.OnClick   = new EventHandler();
	this.navegable = false;
}

Button.prototype = {
	
	CancelEventClick : true,
	confirmMessage   : null,
	clickDelegate    : null,
	enabled          : true,
	
	setElement : function( element )
	{
		this.base.setElement( element );
		
		this.onEnabledChanged();
	},
	
	IsEnabled : function()
	{
		return this.enabled;
	},
	
	SetEnabled : function( value )
	{
		if( value != this.enabled )
		{
			this.enabled = value;
			this.onEnabledChanged();
		}
		
		this.base.SetEnabled( value );
	},
	
	onEnabledChanged : function()
	{
		if( this.enabled )
		{
			if( this.clickDelegate == null )
				this.clickDelegate = new pox.Delegate( this, this.onElementClick );
				
				this.getBrowser().AttachEvent( this.element, "click", this.clickDelegate );
		}
		else
		{
			if( this.clickDelegate != null )
				this.getBrowser().DetachEvent( this.element, "click", this.clickDelegate );
		}
	},
	
	onElementClick : function( args )
	{
		this.CancelEventClick = false;
		this.OnClick.Raise( this );
		
		if( this.CancelEventClick || !this.navegable && this.isAnchorChild() )
			args.CancelEvent = true;
	},
	
	isAnchorChild : function()
	{
		var r;
		var element = this.element.parentNode;
		
		while( element && !r )
		{
			r = element.tagName == "A";
			
			element = element.parentNode;
		}
	
		return r;
	}
}

// ImageButton
var ImageButton = function( id )
{
	Extends( Button, this, id );
}

ImageButton.prototype = {

	onMouseOverDelegate : null,
	onMouseOutDelegate  : null,
	
	onEnabledChanged : function()
	{
		if( this.enabled )
		{
			this.element.src = this.imageIdle;
			
			if( this.onMouseOverDelegate == null )
				this.onMouseOverDelegate = new pox.Delegate( this, this.onMouseOver );
			
			if( this.onMouseOutDelegate == null )
				this.onMouseOutDelegate = new pox.Delegate( this, this.onMouseOut );
			
			Browser.GetCurrent().AttachEvent( this.element, "mouseover", this.onMouseOverDelegate );
			Browser.GetCurrent().AttachEvent( this.element, "mouseout", this.onMouseOutDelegate );
		}
		else
		{
			if( this.onMouseOverDelegate != null )
				Browser.GetCurrent().DetachEvent( this.element, "mouseover", this.onMouseOverDelegate );
			
			if( this.onMouseOutDelegate != null )
				Browser.GetCurrent().DetachEvent( this.element, "mouseout", this.onMouseOutDelegate );
			
			this.element.src = this.imageDisabled;
		}
		
		this.base.onEnabledChanged();
	},
	
	onMouseOver : function()
	{
		if( this.imageOver != null )
			this.element.src = this.imageOver;
	},
	
	onMouseOut : function()
	{
		if( this.imageOver != null )
			this.element.src = this.imageIdle;
	}
}

// Radio Button
var RadioButton = function( id )
{
	Extends( InputControl, this, id );
}
RadioButton.prototype = new function( id )
{
	this.group        = null;
	this.checked      = false;
	
	this.GetValue  = function(){ return this.checked; }
	this.IsChecked = this.GetValue;
	
	this.setElement = function( element )
	{
		this.group      = element.name;
		this.checked    = element.checked;
		
		browser.AttachEvent( element, "click", new pox.Delegate( this, this.onElementClick ) );
		
		this.base.setElement( element );
	}
	
	this.onElementClick = function( args )
	{
		this.SetValue( this.element.checked );
	}
	
	this.SetValue = function( value )
	{
		if( value != this.checked )
		{
			this.element.checked = value;
			this.checked         = value;
			
			if( value )
			{
				var rdns = this.element.form.elements[this.group];
				
				for( var i = 0; i < rdns.length; i++ )
				{
					if( rdns[i].control != this )
						rdns[i].control.SetValue( false );
				}
			}
			
			this.ValueChanged.Raise( this );
		}
	}
}

// ToolTip
var ToolTip = function( id, textContainerId )
{
	Extends( Control, this, id );
	
	if( textContainerId  )
		this.textContainer = document.getElementById( textContainerId );
	else
		this.textContainer = this.element;
}

ToolTip.prototype = new function()
{
	this.visibleFor = null;
	
	this.SetText = function( text )
	{
		browser.SetInnerText( this.textContainer, text );
	}
	
	this.Show = function( control, top, left )
	{
		this.visibleFor = control;
		
		browser.SetPosition( this.htmlRoot, top, left );
		browser.Show( this.htmlRoot );
	}
	
	this.Hide = function( control )
	{
		if( this.visibleFor == control )
		{
			browser.Hide( this.htmlRoot );
			this.visibleFor = null;
		}
	}
}

// File Upload
var FileUpload = function( id )
{
	Extends( Control, this, id );
}

FileUpload.prototype = new function( id )
{
	this.SetValue = function( value )
	{
		this.element.value = value;
	}
	
	this.GetValue = function()
	{
		if( !this.IsEmpty() )
			return this.element.value;
		else	
			return null;
	}
}

// Image Upload
var ImageUpload = function( id, imageId, autoPostBack )
{
	Extends( FileUpload, this, id );
	
	this.autoPostBack = autoPostBack;
	this.image        = document.getElementById( imageId );
}

ImageUpload.prototype = new function()
{
	this.autoPostBack = true;
	this.image        = null;
	
	this.setElement = function( element )
	{
		this.base.setElement( element );
		
		element.onchange = function()
		{
			if( this.control.autoPostBack )
				document.forms[0].submit();	
		}
	}
}

// DateTimePicker
var DateTimePicker = function( id )
{
	Extends( TextBox, this, id );
}

DateTimePicker.prototype = new function()
{
	this.Calendar       = null;
	this.value          = null;
	this.watingResponse = false;
	this.lockKeyBoard   = false;
	this.minValue       = null;
	this.maxValue       = null;
	
	this.setElement = function( element )
	{
		this.base.setElement( element );
		
		element.onclick   = function(){ this.control.onClick() };
		browser.AttachEvent( element, "keydown", new pox.Delegate( this, this.onKeyDown ) );
		//element.onkeydown = function(){ browser.Trace( !this.control.lockKeyBoard ); return !this.control.lockKeyBoard; };
	}
	
	this.onKeyDown = function( args )
	{
		if( args.KeyCode != pox.KeyBoard.Keys.Tab )
			args.CancelEvent = this.lockKeyBoard;
	}
	
	this.onCalendarDaySelect = onCalendarDaySelect; function onCalendarDaySelect( sender )
	{
	    if( this.watingResponse )
	    {
			this.SetValue( this.Calendar.value );
			
			this.focoManual     = true;
			this.Focus();
			this.recienEnfocado = false;
			this.focoManual     = false;
		}
	}
	
	this.onCalendarBlur = function( sender, control )
	{
		this.watingResponse = false;
	}
	
	this.showCalendar = function()
	{
		this.watingResponse = true;
			
		var rectangle = this.GetClientRectangle();
		
		if( !this.calendarSuscription )
		{
			this.Calendar.DaySelect.Suscribe( this, this.onCalendarDaySelect );
			this.Calendar.OnBlur.Suscribe( this, this.onCalendarBlur );
			
			this.calendarSuscription = true;
		}
		
		var left;
		
		switch( this.htmlRoot.style.textAlign )
		{
			case "right":
			this.Calendar.Show(
				this.value,
				rectangle.Bottom,
				rectangle.Left + pox.Drawing.Aligning.AlignRight( rectangle.Width, this.Calendar.GetWidth() ),
				this.minValue,
				this.maxValue );
			break;
			
			default:
				this.Calendar.Show(
					this.value,
					rectangle.Bottom,
					rectangle.Left,
					this.minValue,
					this.maxValue );
			break;
		}
	}
	
	this.hideCalendar = function( timer )
	{
		if( this.watingResponse )
		{
			this.Calendar.Hide();
			this.watingResponse = false;
		}
		
		if( this.hideTimer != null )
			this.hideTimer.Stop();
	}
	
	this.GetValue = GetValue; function GetValue()
	{
	    return this.value;
	}
	
	this.SetValue = SetValue; function SetValue( value )
	{
	    this.value          = value;
	    this.element.value = pox.String.PadLeft( value.getDate().toString(), "0", 2 ) +"/"+ pox.String.PadLeft( (value.getMonth() + 1).toString(), "0", 2 ) +"/"+ value.getFullYear().toString();
	}
	
	//Override( this, "onFocus", onFocus );
	this.onFocus = function()
	{
		if( this.focoManual )
		{
			this.element.focus();
		}
		else if( !this.watingResponse )
		{
			this.showCalendar();
			this.recienEnfocado = true;
		}
		
		this.base.onFocus();
	}
	
	//Override( this, "onBlur", onBlur );
	this.onBlur = function( control )
	{
		if( this.watingResponse && control != this.Calendar )
			this.hideCalendar();
		
		this.base.onBlur( control );
	}
	
	this.onClick = function()
	{
		if( this.IsOnFocus() && !this.recienEnfocado )
		{
			if( !this.watingResponse )
				this.showCalendar();
			else
			{
				this.hideCalendar();
			}
		}
		
		this.recienEnfocado = false;
	}
}

// ButtonResource
function ButtonResource( onUrl, disabledUrl, overUrl, downUrl )
{
	this.OnUrl       = onUrl;
	this.DisabledUrl = disabledUrl;
	this.OverUrl     = overUrl;
	this.DownUrl     = downUrl;
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();