// JavaScript Document
function PassFail(){
	this.failID = '';
	this.passID = '';

if( typeof PassFail._initialized == 'undefined'){
		PassFail.prototype.show = function( text ){
			if( text == 'pass' ){
				document.getElementById( this.passID ).style.display = 'block';
				document.getElementById( this.failID ).style.display = 'none';
			}
			else{
				document.getElementById( this.passID ).style.display = 'none';
				document.getElementById( this.failID ).style.display = 'block';
			}
		};
		PassFail.prototype.setFailID = function( id ){
			this.failID = id;
		};
		PassFail.prototype.setPassID = function( id ){
			this.passID = id;
		};
	
	PassFail._initialized = true;
	}
}

function PassFail2(){
	this.failID = '';
	this.passID = '';

if( typeof PassFail2._initialized == 'undefined'){
		PassFail2.prototype.show = function( text ){
			if( text != 'fail' ){
				document.getElementById( this.passID ).style.display = 'block';
				document.getElementById( this.failID ).style.display = 'none';
				pass( text );
			}
			else{
				document.getElementById( this.passID ).style.display = 'none';
				document.getElementById( this.failID ).style.display = 'block';
				fail();
			}
		};
		PassFail2.prototype.setFailID = function( id ){
			this.failID = id;
		};
		PassFail2.prototype.setPassID = function( id ){
			this.passID = id;
		};
	
	PassFail2._initialized = true;
	}
}

function SelectList(){
	this.selectID = '';
	
	if( typeof SelectList._initialized == 'undefined'){
		SelectList.prototype.show = function( text ){ 
			//alert(text);
			var bl = text.split(",");
			this.createSelect( bl );
			//alert(bl);
		};
		SelectList.prototype.createSelect = function( bl ){
			//alert(bl);
			var sel = document.getElementById( this.selectID );
			sel.options.length = 0;
			var ct = 0;
			sel.options[ct] = new Option( '--Select--' , '-1' );
			for( i=0; i<bl.length ; i++){
				sel.options[++ct] = new Option( bl[i] , bl[i++] );
				
			}
		};
		SelectList.prototype.setSelectID = function( sid ){
			this.selectID = sid;
		};
	SelectList._initialized = true;
	}
}
function SuggestionProvider(){
	this.suggestions = new Array('test1','test2');
	
	if( typeof SuggestionProvider._initialized == 'undefined'){
		SuggestionProvider.prototype.requestSuggestions = function( _autosuggestcontrol ){
			
			_autoSuggestControl.autoSuggest( this.suggestions );
		};
	
	SuggestionProvider._initialized = true;
	}
}


function AutoSuggestControl( _textbox , _provider ){
	this.provider = _provider;
	this.textbox = _textbox;
	this.init();
	
	if( typeof AutoSuggestControl._initialized == 'undefined'){
		AutoSuggestControl.prototype.init = function(){
			var _this = this;
			this.textbox.onkeyup = function( _event ){
				if( !_event ){
					_event = window.event;
				}
				_this.handleKeyUp( _event );
			};
		};
		AutoSuggestControl.prototype.SelectRange = function ( _start , _length ){
			if( this.textbox.createTextRange ){
				var range = this.textbox.createTextRange();
				range.moveStart( 'character' , _start );
				range.moveEnd( 'character' , _length - this.textbox.value.length);
				range.select();
			}
			else if( this.textbox.setSelectionRange ){
				this.textbox.setSelectionRange( _start , _length );
			}
			this.textbox.focus();
		};
		AutoSuggestControl.prototype.typeAhead = function( _suggestion ){
			if( this.textbox.createTextRange || this.textbox.setSelectionRange ){
				var len = this.textbox.value.length;
				this.textbox.value = _suggestion; 
				this.selectRange( len , _suggestion.length );
			}
		};
		AutoSuggestControl.prototype.autoSuggest = function( _suggestions ){
			if( _suggestions.length > 0 ){
				this.typeAhead( _suggestions[0] );
			}
		};
		AutoSuggestControl.prototype.handleKeyUp = function( _event ){
			var keycode = _event.keyCode;
			if( keycode < 32 || ( keycode >= 33 && keycode <= 46 ) || ( keycode >= 112 && keycode <= 123 )){
				//ignore
			}
			else{
				this.provider.requestSuggestions( this );
			}
		};
		
	AutoSuggestControl._initialized = true;
	}
}



