/* FieldSync */
var FieldSync = $.inherit(
{
	__constructor : function(oWidgetFrom, oWidgetTo)
	{
		
		var oWidgetFrom = oWidgetFrom;
		var oWidgetTo = oWidgetTo;
		var jFrom = $(oWidgetFrom.oElement);
		var jTo = $(oWidgetTo.oElement);
		
		jTo.bind("blur", function(){
			jTo.syncable = (jQuery.trim(jTo.val()).length == 0);
		});
		jTo.trigger("blur");
		
		oWidgetFrom.attachOuterObserver(
			ZForms.EVENT_TYPE_ON_CHANGE,
			function(sEventType, oWidget)
			{
				if (jTo.syncable)
				{
					oWidgetTo.setValue(oWidgetTo.createValue(oWidgetFrom.getValue().get()));
				}
			}
		);
	}
},{
});

/* $.maxLength plugin */
jQuery.fn.maxLength = function(max)
{  
	this.each(function()
	{
        if(this.tagName.toLowerCase() == 'textarea')
        {  
            this.onkeypress = function(e)
            {  
                var ob = e || event;  
                var keyCode = ob.keyCode;  
                var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;  
                return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);  
            };  
            this.onkeyup = this.onblur = function()
            {
                if(this.value.length > max){
                    this.value = this.value.substring(0,max);  
                }  
            };  
        }
    });  
};

