var advancedSearch = function(){

	this.timer = null;

	this.formName = 'submitAdvanced';
	this.path = 'asdasdas';
	this.platePath = '/graphics/plate_preview/medium/';
	this.anyLetter = '<img src="/graphics/plate_preview/medium/any_letter.gif" class="imagePreview">';
	this.anyNumber = '<img src="/graphics/plate_preview/medium/any_number.gif" class="imagePreview">';
	this.anyAll = '<img src="/graphics/plate_preview/medium/any.gif" class="imagePreview">';
	this.blankImage = '<img src="/graphics/blank.gif" width="12" class="imagePreview">';
	
	this.prefix1 = '';
	this.prefix2 = '';
	this.numbers = '';
	this.initials1 = '';
	this.initials2 = '';
	this.initials3 = '';
	this.selection = '';
	
	this.presetType = '';
	
	this.submitForm = function(){
		$('#'+this.formName).attr({'action' : this.path});
		window.location = this.path;
		return false;
	}

	this.showErrorMessage = function( id , message, direction){
		$('#'+id).removeClass('tinytext').addClass('tinytextError');
		this.displayError($('#'+id) , id , message, direction);
	}
	this.displayError = function(obj, name,  message, direction){
	

		var classname = direction =='above' ? 'errorAbove ' : '';
	
		html  = '<div id="error-'+name+'" class="'+classname+'advancedSearchError"><p>';
		html += message;
		html += '</p></div>';
		var position = obj.offset();
		$(document.body).prepend(html);
		var top = 0;
		if (direction =='above'){
			top = position.top -75;
		} else {
			top = position.top +20;
		}
		
		$('#error-'+name).hide().css({'top':(top)+'px', 'left':position.left-6 }).fadeIn(150);

		$(document.body).bind('mouseup' , this.clearErrors);
		$(document.body).bind('keyup' , this.clearErrors);
	}

	this.hideError = function(name){
		$('#error-'+name).fadeOut(150);
		setTimeout(function(){$('#error'+name).remove() , 150});
	}
	this.clearErrors = function(e){

		if(e.originalTarget) {
			targetNode = e.originalTarget;
		} else if(e.srcElement) {
			targetNode = e.srcElement;
		}
		if (!$(targetNode).parents('div').is('.advancedSearchError') && !$(targetNode).is('.advancedSearchError') ) {

			$(document.body).unbind('mouseup' , this.clearErrors);
			$(document.body).unbind('keyup' , this.clearErrors);

			$('.advancedSearchError').fadeOut(150);
			setTimeout(function(){
				$('.advancedSearchError').remove();
			} , 150);
		}
	}
	this.compareString = function(string , compareArr){

		var stringLength = string.length;
		var validCharsCount = 0;

		for (var t=0;t< stringLength; t++){
			for (var i=0;i< compareArr.length; i++){
				if (string.charAt(t) == compareArr[i]){
					validCharsCount++;
				}
			}
		}
		if (validCharsCount != stringLength){
			return false;
		} else {
			return true;
		}
	}

	this.displayPreviewChar = function(string , count , anyVar){
		var html = '';
		if (string == ''){
			for(var i=0;i < count; i++){
				html += anyVar;
			}
		} else {
			for(var i=0;i < string.length; i++){
				if (string.charAt(i) == '*' || string.charAt(i) == '_' || string.charAt(i) == '-'){
					html += anyVar;
				} else {
					html += '<img src="/graphics/plate_preview/medium/'+string.charAt(i).toUpperCase()+'.gif" class="imagePreview">';
				}
			}
		}
		return html;
	}
	
	this.buildSelect = function (obj , array , selected){
		document.getElementById(obj).options.length = 0;
		for(i=0; i<array.length; i++){
		    document.getElementById(obj).options[i] = new Option(array[i] , array[i]);
		    if (array[i] == selected){
			document.getElementById(obj).selectedIndex = i;
		    }
		}
	}

	
}
