	var BACK_SPACE_ASCII_CODE = 8;
	var ENTER_ASCII_CODE = 13;
	var TAB_ASCII_CODE = 9;

function DialogResult() {
	this.ok = "DialogResultOK";
	this.cancel = "DialogResultCancel";
}

function ShowWindow( dialogId ) {
	var dw = new DialogWorker(dialogId);
	dw.OpenWindow();
}
function HideWindow( dialogId ) {
	var dw = new DialogWorker(dialogId);
	dw.HideWindow();
}            

function ShowDialog( dialogId ) {
	var dw = new DialogWorker(dialogId);
	dw.Show();
}

function HideDialog( dialogId ) {
	var dw = new DialogWorker(dialogId );
	dw.Hide();
}

function HideDialogForLoading( dialogId ) {
	var dw = new DialogWorker(dialogId );
	dw.HideForLoading();
}

function SetDialogBusy( dialogId ) {
	var dw = new DialogWorker(dialogId );
	dw.SetBusy();
}

function SetDialogFree( dialogId ) {
	var dw = new DialogWorker(dialogId );
	dw.SetFree();
}

function EnableLoadingImage( enable, zindex ) {
	var size = BSGetWindowSize();
	PlaceLoadingImage(enable, 0, 0, size.x, size.y, zindex);
}

function PlaceLoadingImage( enable, x, y, width, height, zindex ) {
	var loadingImg = document.getElementById("loading_image");
	if ( loadingImg ) {
		if ( enable ) {
			loadingImg.style.zIndex = zindex;
			loadingImg.style.left = (x + width/2 - loadingImg.style.width/2) + "px"; 
			loadingImg.style.top =  (y + height/3 - loadingImg.style.height/2) +"px"; 
		}
		loadingImg.style.display=enable?"block":"none";
	}
}

function DialogWorker(dialogId ){
	var stackZIndex = 100;
	var bigImageLength = "4000px";
	var overflowProperty = "auto";
	var overflowXProperty = "auto";
	var overflowYProperty = "auto";

	var dialogElem = document.getElementById( dialogId );

	this.OpenWindow = function ( ) {
		SetVisibleStyle(dialogElem);
		MoveRightTop( dialogElem );
	}
	this.HideWindow = function ( ) {
		SetInvisibleStyle(dialogElem);
	}
	this.Show = function() {
		var scroller = BSGetScroll();
		DimmScreen();
		SetVisibleStyle(dialogElem);
		MoveLeftTop(dialogElem);
		BSSetDimmedSELECTsVisibility( dialogElem, false);		
		EnableLoadingImage( false );
		BSSetScroll(scroller);
	}
	this.Hide = function () {
		UndimmScreen();
		SetInvisibleStyle(dialogElem);
		BSSetDimmedSELECTsVisibility( dialogElem, true );		
		EnableLoadingImage( false );
	}

	this.HideForLoading = function () {
		SetInvisibleStyle(dialogElem);
		EnableLoadingImage( true, stackZIndex+10 );
	}

	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
    	    } 
			while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	}

	this.SetBusy = function () {
		var bodyElem = document.body;	
		var dimmer = document.createElement("div");
		bodyElem.insertBefore( dimmer, bodyElem.firstChild);
		dimmer.setAttribute("id", "dimmer");
		dimmer.style.display = "block";
		dimmer.style.position = "absolute";

		var pos = findPos(dialogElem);
		dimmer.style.left = pos[0] + "px";
		dimmer.style.top = pos[1] + "px";
		dimmer.style.width = dialogElem.offsetWidth + "px";
		dimmer.style.height = dialogElem.offsetHeight + "px";

		dimmer.style.zIndex = stackZIndex++;
		PlaceLoadingImage( true, pos[0], pos[1], dialogElem.offsetWidth, dialogElem.offsetHeight, stackZIndex+10 );
		
	}

	this.SetFree = function () {
		var bodyElem = document.body;	
		bodyElem.removeChild(bodyElem.firstChild);
		PlaceLoadingImage( false );
	}

	function SetVisibleStyle(dialogElem){
		dialogElem.style.display="block";
		dialogElem.style.position="absolute";
		dialogElem.style.zIndex=stackZIndex++;
	}                                  
	function MoveLeftTop(dialogElem){
		dialogElem.style.left = ((document.documentElement.offsetWidth - dialogElem.offsetWidth)/2) + "px";
		var pageSize = BSGetWindowSize();
		var scrollSize = BSGetScroll();
		var elemHeight = dialogElem.offsetHeight;
		var elemTop = Math.floor( elemHeight>pageSize.y?0:(pageSize.y-elemHeight)/2+scrollSize.y );
		dialogElem.style.top = elemTop+ "px";
	}
	function MoveRightTop(dialogElem){

		dialogElem.style.left =  document.documentElement.offsetWidth-dialogElem.offsetWidth + "px";
		dialogElem.style.top = document.documentElement.scrollTop + "px";
	}

	function SetInvisibleStyle(dialogElem){
		dialogElem.style.display="none";
	}                                  

	function ResetOverflow(element){
		overflowProperty = element.style.overflow;
		element.style.overflow = "hidden";
		if (element.style.overflowX){
			overflowXProperty = element.style.overflowX;
			element.style.overflowX = "hidden";
		}
		if (element.style.overflowY){
			overflowYProperty = element.style.overflowY;
			element.style.overflowY = "hidden";
		}
		document.body.scroll = "no";
		if (document.body.scroll){
			element.scroll = "no";
		}
	}

	function SetOverflow(element){
		element.style.overflow = overflowProperty;
		if (element.style.overflowX){
			element.style.overflowX = overflowXProperty;
		}
		if (element.style.overflowY){
			element.style.overflowY = overflowYProperty;
		}
	}	

	function UndimmScreen() {
		var bodyElem = document.body;	
		bodyElem.removeChild(bodyElem.firstChild);
	//	SetOverflow(bodyElem);                    
	}

	function DimmScreen() {
		var bodyElem = document.body;	
		var dimmer = document.createElement("div");
		bodyElem.insertBefore( dimmer, bodyElem.firstChild);
		ResetOverflow(bodyElem.firstChild);
		dimmer.setAttribute("id", "dimmer");
		dimmer.style.display = "block";
		dimmer.style.position = "absolute";
		dimmer.style.top = "0";
		dimmer.style.left = "0";
		dimmer.style.zIndex = stackZIndex++;
		dimmer.style.width = "100%";
		var size = BSGetPageSize();
		dimmer.style.height = size.ypx;
	}
}    

function GetSelectOptionsAsArray( element ) {
	var options = element.childNodes;
	var result = new Array();
	for( var i=0;i<options.length;++i ) {
		var opt = options[i];
		if ( 'OPTION' == opt.nodeName ) {
			result[ result.length ] = opt.value;
			result[ result.length ] = opt.firstChild.nodeValue;
		}
	}
	return result;
}

function ElementAccessorRadio( ) {
	this.GetElement = function () {
		return document.getElementById( this.id + '0' );
	}
	this.GetValue = function ( ) {
		var index = 0;
		while( true ) {
			var element = document.getElementById( this.id + index++ );
			if ( !element ) {
				break;
			}
			if ( element.checked ) {
				return element.value;
			}
		}

	}
	this.ResetValue = function ( ) {
		while( true ) {
			var element = document.getElementById( this.id + index++ );
			if ( !element ) {
				break;
			}
			if ( element.defaultChecked ) {
				element.checked = true;
			}
		}
	}
	this.SetValue = function ( value ) {
		var index = 0;
		while( true ) {
			var element = document.getElementById( this.id + index++ );
			if ( !element ) {
				break;
			}
			element.checked = element.value==value;
		}
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode.parentNode;
	}
}



function ElementAccessorCheckbox() {
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.ResetValue = function ( ) {
		var element = this.GetElement();
		element.checked = element.defaultChecked;
	}
	this.GetValue = function ( ) {
		return this.GetElement().checked?1:0;		
	}
	this.SetValue = function ( value ) {
		var element = this.GetElement();
		element.checked = value?1:0;
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode.parentNode;
	}
}

function ElementAccessorDefault() {
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.GetValue = function ( ) {
		return this.GetRawValue();
	}
	this.GetRawValue = function ( ) {
		return this.GetElement().value;
	}
	this.ResetValue = function ( ) {
		var element = this.GetElement();
		element.value = element.defaultValue;
	}
	this.SetValue = function ( value ) {
		var element = this.GetElement();
		if  (typeof(element.type)=='undefined') {
			element.innerHTML = value;
			return;
		}
		element.value = value;
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
}

function ElementAccessorNumber() {
	this.inheritFrom = ElementAccessorDefault;
	this.inheritFrom();
	this.GetValue = function ( ) {
		return Number(this.GetElement().value.replace(",", "."));
	}
}

function ElementAccessorImage() {
	this.inheritFrom = ElementAccessorDefault;
	this.inheritFrom();
	this.GetRawValue = function ( ) {
		return this.GetElement().src;
	}
	this.ResetValue = function ( ) {
	}
	this.SetValue = function ( value ) {
		var element = this.GetElement();
		element.src = value;
	}
}


function ElementAccessorSelect() {
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.GetValue = function ( ) {
		return this.GetElement().value;
	}
	this.ResetValue = function ( ) {
		var element = this.GetElement();
		for( var i=0;i<element.options.length;++i ) {
			var opt = element.options[i];
			if ( opt.defaultSelected ) {
				this.SetValue( opt.value );
				return;
			}
		}
		if ( element.options.length ) {
			this.SetValue( element.options[0].value );
		}
	}
	this.SetValue = function ( value ) {
		var element = this.GetElement();
		var textFound  = '';
		var valueFound = false;
		for( var i=0;i<element.options.length;++i ) {
			var opt = element.options[i];
			if ( opt.value == value) {
				valueFound = true;
				break;
			} else if ( opt.text == value ) {
				textFound = opt.value;
			}
		}
		if ( valueFound ) {
			element.value = value;
		} else if (textFound!='' ) {
			element.value = textFound;
		}
		
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
}

function ElementAccessorDate() {
	this.YearsGap = 100;
	this.CenturyGap = 25;
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.GetControls = function () {
		return { 
			"d" : document.getElementById( this.id + '_day' )
			,"m" : document.getElementById( this.id + '_month' )
			,"y" : document.getElementById( this.id + '_year' )
			,"c" : document.getElementById( this.id + '_century' )
			,"h" : document.getElementById( this.id + '_hour' )
			,"n" : document.getElementById( this.id + '_minute' )
		};
	}

	this.GetValue = function ( ) {
		var ctrls = this.GetControls();
		var year = ctrls.y? ctrls.y.value:ctrls.c?ctrls.c.value:0;
		var d = new Date( year, getControlValueIfExists( ctrls.m , 0 ), getControlValueIfExists( ctrls.d, 1 ), getControlValueIfExists( ctrls.h, 0 ), getControlValueIfExists( ctrls.n, 0 ) );
		if ( ctrls.m && d.getMonth() != ctrls.m.value ) {
			d.setMonth( ctrls.m.value );
		}
		d.setFullYear( year );
		return d;
	}
	this.ResetValue = function ( ) {
		this.SetValue( eval(this.id + '_init') );
	}
	function getControlValueIfExists( ctrl, value ) {
		if ( ctrl ) {
			return ctrl.value;
		}
		return value;
	}
	function setControlValueIfExists( ctrl, value ) {
		if ( ctrl ) {
			ctrl.value = value;
		}
	}

	this.SetValue = function ( value ) {
		var date = new Date( String2Date( value ) );
		var ctrls = this.GetControls();
		setControlValueIfExists( ctrls.m , date.getMonth() );
		setControlValueIfExists( ctrls.d , date.getDate() );
		setControlValueIfExists( ctrls.h , date.getHours() );
		setControlValueIfExists( ctrls.n , date.getMinutes() );
		this.UpdateY( date );
		this.UpdateD( date );
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
	function EmptySelectOptions( s ) {
		while( s.options.length ) { s.remove( 0 ); }
	}

	this.UpdateY = function ( d ) {
		var ctrls = this.GetControls();
		var bcElement = document.getElementById( 'b_c');
		var bcText = bcElement?(' ' + bcElement.innerHTML):'';
		if ( ctrls.y ) {
			EmptySelectOptions( ctrls.y );
			for( var i=-this.YearsGap;i<=this.YearsGap;++i ) {
				var key = d.getFullYear()+i;
				var value = key;
				if ( key<=0 ) {
					value = (1-key) + bcText;
				} 
				BSAddSelectOption( ctrls.y, key, value );
			}
			ctrls.y.value = d.getFullYear();
		}
		if ( ctrls.c ) {
			EmptySelectOptions( ctrls.c );
			var century = d.getFullYear();
			
			var centuryText = ' ' + document.getElementById( 'century').innerHTML;
			for( var i=-this.CenturyGap;i<=this.CenturyGap;++i ) {
				
				var key = century+i*100;
				var value = Math.floor((key-1)/100);
				if ( value>=0 ) {
					value= (value+1) + centuryText;
				} else {
					value = (-value) + centuryText + bcText;
				}
				BSAddSelectOption( ctrls.c, key, value );
			}
			ctrls.c.value = century;
		}
	}
	this.UpdateD = function ( d ) {
		var ctrls = this.GetControls();
		if ( ctrls.d ) {
			EmptySelectOptions( ctrls.d );
			for(var i =1;i<=31;++i ) {
				var d1 = new Date( d );
				d1.setDate( i );
				if ( d.getMonth() != d1.getMonth() ) {
					break;
				}
				BSAddSelectOption( ctrls.d, i, document.getElementById( 'dow_' + d1.getDay() ).innerHTML +', ' +i );	
			}
			ctrls.d.value = d.getDate();
		}
	}

	this.Initialize = function ( d ) {
		var ctrls = this.GetControls();
		if ( ctrls.h ) {
			EmptySelectOptions( ctrls.h );
			for( var i=0;i<24;++i ) {
				BSAddSelectOption( ctrls.h, i, i );
			}
			ctrls.h.value = d.getHours();
		}
		if ( ctrls.n ) {
			EmptySelectOptions( ctrls.n );
			for( var i=0;i<60;++i ) {
				BSAddSelectOption( ctrls.n, i, ((i<10)?'0':'')+i );
			}
			ctrls.n.value = d.getMinutes();
		}
		if ( ctrls.m ) {
			EmptySelectOptions( ctrls.m );
			for( var i=0;i<12;++i ) {
				BSAddSelectOption( ctrls.m, i, document.getElementById( 'mon_' + i).innerHTML );
			}
			ctrls.m.value = d.getMonth();
		}
		this.UpdateY( d );
		this.UpdateD( d );
	}
}


function ElementAccessorDynamicSelect( url, params, fieldid, defaultvalue, defaultvaluefieldid, handlerVarName, sort ) {
	if ( url ) {
		this.URL = 'http://' + BSGetDataSourceDomain() + url;
	}
	this.PatchDefaultValue = function ( value ) {
		if ( typeof(value)=='object' ) {
			return value;
		}
		var valueString = '({"' + this.pkFieldName + '" : ';
		var quote = typeof(value)=='string'?'"':'';
		valueString += quote + value + quote;
		valueString += '})';
		var evaluated = eval(valueString);
		return eval( valueString );
	}

	this.params = params;
	this.fieldid = fieldid;
	this.pkFieldName = "" == defaultvaluefieldid ? fieldid : defaultvaluefieldid ;
	this.currentvalue = this.PatchDefaultValue( defaultvalue ); 
	this.initialvalue = this.currentvalue;
	this.valuesarray = new Array();
	this.sort = sort;
	this.m_handlerVarName = handlerVarName;

	if ( this.URL ) {
		BSMakePOSTRequest(this.URL, this, this.params );						
	}

	this.SetParams = function(params) {
		this.params = params;
		if ( this.URL ) {
			BSMakePOSTRequest(this.URL, this, this.params );						
		}
	}
	this.Query = function( url, params ) {
		this.params = params;
		this.URL = 'http://' + BSGetDataSourceDomain() + url;
		BSMakePOSTRequest( this.URL, this, this.params );						
	}

	this.OnResponse = function( obj ) {
		if ( this.sort ) {
			var rst = new Recordset();
			rst.LoadFromArray( obj );
			rst.SortAsc( this.fieldid );
			this.valuesarray = rst.GetRecordArray();
		} else {
			this.valuesarray = obj;
		}
		var text = '';
		var selectedById = false;
		var selectedByName = false;
		for(var i=0; i<this.valuesarray.length; ++i ) {
			var item = this.valuesarray[i];
			var selected = '';
			if (this.currentvalue[this.pkFieldName] == item[this.pkFieldName]) {
				selected = 'selected="selected"';              
				selectedById = true;
			} else if (!selectedById && this.currentvalue[this.pkFieldName] == item[this.fieldid]) {
				selected = '%selectedbyname%';              
				selectedByName = true;
			}
			text += '<OPTION VALUE="' + i + '" ' + selected + '>' + item[this.fieldid] + '</OPTION>';
		}
		if ( selectedByName ) {
			text = text.replace( '%selectedbyname%', selectedById?'':'selected="selected"' );
		}
		if (text.length) {
			BSReplaceOptions( this.GetElement(), text );
			if ( eval(this.m_handlerVarName) ) { //if handler ready
				this.GetElement().onchange();
			}
		}
	}

	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.ResetValue = function ( ) {
		this.SetValue( this.initialvalue );
	}

	this.GetValue = function ( ) {
		var value = this.GetElement().value;
		if (undefined != this.valuesarray[value])
			return this.valuesarray[value];
		else
			return "";
	}

	this.SetValue = function ( value ) {
		var element = this.GetElement();
		if  (typeof(element.type)=='undefined') {
			return;
		}
		this.currentvalue = this.PatchDefaultValue( value );
		if (undefined != this.valuesarray[value]) {
			this.OnResponse(this.valuesarray);
		} else {
			if ( this.URL ) {
				BSMakePOSTRequest(this.URL, this, this.params );						
			}
		}
	}

	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
}

function ElementAccessorInputTable( handlerName ) {
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.GetRecordset = function () {
		return eval( handlerName +'.Recordset' );
	}
	this.GetValue = function ( ) {
		return this.GetRecordset().GetRecordArray();
	}
	this.ResetValue = function ( ) {
		return this.GetRecordset().Clear();
	}
	this.SetValue = function ( value ) {
		this.GetRecordset().LoadFromArray( value );
		eval( handlerName +'.Refresh();');
	}
}

function ElementAccessorFormula() {
	this.value = '';
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.ResetValue = function ( ) {
	}
	this.GetValue = function ( ) {
		return this.value;		
	}
	this.SetValue = function ( value ) {
		this.value = value;
		var v = ( typeof( value ) == 'object' )?PCM.getTeX(value):value;
		if ( v == '' ) {
			this.GetElement().innerHTML = '';
		} else {
			var alt = ( typeof( value ) == 'object' )?PCM.getMath(value):value;
			this.GetElement().innerHTML = '<img src="/cgi-bin/mimetex.cgi?' +  v + '" alt="' + alt + '"/>';
		}
		
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
}

function ElementAccessorPrecision() {
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.ResetValue = function ( ) {
	}
	this.GetValue = function ( ) {
		return PCF.getCalcPrecision();		
	}
	this.SetValue = function ( value ) {
		PCF.setCalcPrecision( value );
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
}


function ElementAccessorResource() {
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.ResetValue = function ( ) {
	}
	this.GetValue = function ( ) {
		return eval( '(' + this.id + '_data)' );		
	}
	this.SetValue = function ( value ) {
	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
}


function ElementAccessorDegrees() {
	this.GetElement = function () {
		return document.getElementById( this.id );
	}
	this.ResetValue = function ( ) {
		var elm = document.getElementById( this.id + '_value');
		this.SetValue( elm.defaultValue );
	}
	this.GetValue = function ( ) {
		var elm = document.getElementById( this.id + '_value');
		return Number(elm.value);		
	}
	this.SetValue = function ( value ) {
		this.SetDegreesValue( value );
	}
	this.SetDegreesValue = function ( value ) {
		var elm = document.getElementById( this.id + '_value');
		elm.value = value;		
		var nVal = Number(value);
		var sign = "";
		if ( nVal<0 ) {
			nVal = -nVal;
			sign = "-";
		}	
		document.getElementById( this.id + '_sec').value = Math.floor(nVal*3600)%60;		
		document.getElementById( this.id + '_min').value = Math.floor(nVal*60)%60;		
		document.getElementById( this.id + '_deg').value = sign + Math.floor(Number(nVal));

	}
	this.GetParent = function () {
		var thisElement = this.GetElement();
		return thisElement.parentNode;
	}
}


function ElementAccessorCoordinate() {
	this.inheritFrom = ElementAccessorDegrees;
	this.inheritFrom();
	this.SetValue = function ( value ) {
		var nVal = Number(value);
		document.getElementById( this.id + '_side1').checked = nVal>=0;		
		document.getElementById( this.id + '_side2').checked = nVal<0;	
		if ( nVal<0 ) {
			nVal = -nVal;
		}	
		this.SetDegreesValue( nVal );
		var elm = document.getElementById( this.id + '_value');
		elm.value = value;		
	}
}

function DialogInput( inputId, dialog, inputValidator, accessor ) {
	this.id = inputId;
	this.dialog = dialog;
	this.validator = inputValidator;
	this.formatter = null;
	this.elementAccessor = accessor;
	this.elementAccessor.id = dialog.GetElement().id + '_' + this.id;

	this.GetElement = function () {
		return this.elementAccessor.GetElement();;
	}

	this.GetElementAccessor = function() {
		return this.elementAccessor;
	}
	this.ResetValue = function( ) {
		this.elementAccessor.ResetValue();
	}
	this.SetValue = function( value ) {
		var formatted = this.formatter ? this.formatter.Format( value ) : value;
		this.elementAccessor.SetValue( formatted );
	}
	this.GetValue = function( value ) {
		return this.elementAccessor.GetValue( ); 
	}
	/// returns parent <li> element 
	this.GetElementToShow = function() {
		return this.elementAccessor.GetParent();
	}
	this.Show= function() {
		var element = this.GetElementToShow();
		return this.GetElementToShow().style.display="block";
	}
	this.Hide= function() {
		var element = this.GetElementToShow();
		return element.style.display="none";
	}
	this.ShowError = function( text ) {
		return this.dialog.ShowError( this.GetElement(), text );
	}
	this.ClearError = function() {
		return clearError( this.GetElement() );
	}
	this.ShowOrClearError = function ( errorText ) {
		if ( errorText=='' ) {
			clearError( this.GetElement() );
			return true;
		}
		this.ShowError( errorText );
		return false;
	}
	this.Validate = function ( value ) {
		if ( this.validator ) {
			var val = this.elementAccessor.GetRawValue?this.elementAccessor.GetRawValue():this.elementAccessor.GetValue( );
			return this.ShowOrClearError( this.validator.Validate( val ) );
		}
		return true;
	}
	this.ValidateCurrent = function ( ) {
		if ( this.validator ) {
			return this.Validate( this.GetValue() );
		}
		return true;
	}
	this.GetOptions = function () {
		return GetSelectOptionsAsArray( this.GetElement() );
	}
	this.Load = function ( httpParameters ) {
		var name = this.GetElement().name;
		if ( name ) {
			var value = httpParameters[name ];
			if ( typeof(value)=='undefined') {
				return;
			}
			this.SetValue( value );
		}
	}
}



function HTTPParameters( ) {
	this.Parameters = {};
	this.SetParameter = function( id, value ) {
		this.Parameters[ id ] = value;
	}
	var url = new String( document.URL );
	var baseAndParams= url.split('?');
	if ( baseAndParams.length>1 ) {
		var args= baseAndParams[1].split('&');
		for( var i=0;i<args.length;++i ) {
			var argAndName = args[i].split('=');
			var value="";
			if ( argAndName.length==2 ) {
				value=argAndName[1];
			}
			this.SetParameter( argAndName[0], decodeURIComponent( value ) );
		}
	}
}

function SaveObjectToURLParams( obj ) {
	var result = "";
	for( var id in obj ) {
		var value = obj[id];
		if ( typeof(value) == 'object' ) {
			value = Value2Json( value, true );
		}
		if (typeof(value)!='undefined') {
			result += ("&"+id + "=" + encodeURIComponent(value));	
		}
	}
	if ( result.length ){
		return result.substring( 1 );
	}
	return result;
}

function CookieParameters( ) {
	this.Parameters = new Object();
	this.SetParameter = function( id, value ) {
		this.Parameters[ id ] = value;
	}
	function trimString( s ) {
	    var result = s.replace(/^[\s\t]+/,"");
	    return  result.replace(/[\s\t]+$/,"");
	}
	var namePrefix = 'field_';
	var cookies = document.cookie.split(';');
	for( var i =0;i<cookies.length;++i ) {
		var cookie=cookies[i].split('=');
		var prefixedName = trimString(cookie[0]);
		var thisPrefix = prefixedName.substring(0,namePrefix.length);
		if ( namePrefix == thisPrefix ) {
			var name = prefixedName.substring(namePrefix.length);
			var value = "";
			if ( cookie.length>1 ) {
				value = trimString(cookie[1]);
			}
			this.SetParameter( name, value );
		}

	}

	var expirationPeriod = 30;

	var erpirationDate = new Date();
	erpirationDate.setTime(erpirationDate.getTime()+(expirationPeriod*24*60*60*1000));
	this.expires = "expires="+erpirationDate.toGMTString();
	this.Save = function() {
		for( var id in this.Parameters ) {
			document.cookie = namePrefix + id + "=" + this.Parameters[id] + ";" + this.expires;
		}
	}

}


function getControlValueAfterChange( pressedKey, element ) {
	var insertPosition = BSGetTextSelection( element );
	var prevValue = element.value;
	var resultValue = prevValue.substring( 0, insertPosition.selectionStart );
	if ( pressedKey.charCode==BACK_SPACE_ASCII_CODE ) {
		if ( resultValue.length ) {
			resultValue = resultValue.substring(0,resultValue.length-1);
		}
	} else if ( pressedKey.charCode ) {
		resultValue += String.fromCharCode( pressedKey.charCode );
	} 
	resultValue += prevValue.substring( insertPosition.selectionEnd, prevValue.length );
	return resultValue;
}

function handleOnKeyPressed( event, element, validator, msgbox ) {
	var pressedKey = BSGetPressedKey( event );
	if ( typeof(pressedKey)=='undefined' || pressedKey.charCode==BACK_SPACE_ASCII_CODE 
		|| pressedKey.charCode==ENTER_ASCII_CODE || pressedKey.charCode==TAB_ASCII_CODE 
		|| pressedKey.charCode == 0 || BSIsSystemKey(pressedKey) ) {
		return true;
	}
	var value =  getControlValueAfterChange( pressedKey, element );
	if ( showOrClearError( element, validator.Validate( value ), msgbox ) ) {
		return true;
	}
	return BSPreventDefaultAction( event );
}



