AddressSearch = function(searchText){
	var searchStr = searchText;
	
	var _name = "";
	var _address = "";
	var _filter = "";	
	var _section = "";
	
	var _tempSection = null;
	var _tempFilter = null;
	
	var textoPersona = "persona:";
	var textoEmpresa = "empresa:";
	var textoDireccion = "en:";

	var textoRegion = "region:";
	var textoRegion2 = "region";	
	 
	var parse = function(){		
		var positionPersona = searchStr.indexOf(textoPersona);
		var positionEmpresa = searchStr.indexOf(textoEmpresa);
		var positionDireccion = searchStr.indexOf(textoDireccion);
		var textoEn;			
		
		if (positionEmpresa != -1){
			_section = textoEmpresa;
		} else if (positionPersona != -1){
			_section = textoPersona;
		} else {
			_section = "";
		}
		
		if (positionEmpresa != -1 && positionDireccion != -1){			
			if (positionEmpresa < positionDireccion){
				//empresa:flores en:santiago	
				_name = searchStr.substring(textoEmpresa.length, positionDireccion-1);
				_address = searchStr.substring(positionDireccion + textoDireccion.length);							
			} else {
				//en:santiago empresa:flores
				_address = searchStr.substring(textoDireccion.length, positionEmpresa-1);
				_name = searchStr.substring(positionEmpresa + textoEmpresa.length);				
			}				
		} else if (positionPersona != -1 && positionDireccion != -1){
			if (positionPersona < positionDireccion){
				//persona:flores en:santiago	
				_name = searchStr.substring(textoPersona.length, positionDireccion-1);
				_address = searchStr.substring(positionDireccion + textoDireccion.length);
			} else {
				//en:santiago persona:flores
				_address = searchStr.substring(textoDireccion.length, positionPersona-1);
				_name = searchStr.substring(positionPersona + textoPersona.length);
			}
		} else if (positionDireccion != -1 && positionEmpresa == -1 && positionPersona == -1 && positionDireccion == 0) {
			//en:santiago
			_address = searchStr.substring(textoDireccion.length);
			_name = "";
			
		} else if ((positionDireccion == -1 && positionEmpresa == -1 && positionPersona == -1) || (positionPersona != -1 && positionEmpresa == -1 && positionDireccion == -1) || (positionEmpresa != -1 && positionPersona == -1 && positionDireccion == -1)){
			//flores
			//persona:flores
			//empresa:flores
			if (positionDireccion == -1 && positionPersona == -1 && positionEmpresa == -1){
				_name = searchStr;
				_address = "";
			} else if (positionPersona != -1){
				_name = searchStr.substring(textoPersona.length);
				_address = "";
			} else if (positionEmpresa != -1){
				_name = searchStr.substring(textoEmpresa.length);
				_address = "";
			}
			
		} else if (positionDireccion != -1 && positionEmpresa == -1 && positionPersona == -1 && positionDireccion != 0) {								
			if (positionDireccion != 0){
				//flores en:santiago
				_name = searchStr.substring(0, positionDireccion-1);
				_address = searchStr.substring(positionDireccion + textoDireccion.length);
			}
		}
		
		var position;
				
		if (_address.length > 0){		
			if (_address.indexOf(textoRegion) != -1){
				position = _address.indexOf(textoRegion);			
				if (position != -1){
					_filter = _address.substring(position);
					_address = _address.substring(0, position-1);					
				}
			} else if (_address.indexOf(textoRegion2) != -1) {
				position = _address.indexOf(textoRegion2);			
				if (position != -1){
					_filter = _address.substring(position);
					_address = _address.substring(0, position-1);					
				}
			}					
		}
	}	
	parse();
	
	this.doParse = function(searchText){
		_name = "";
		_address = "";
		_filter = "";	
		_section = "";
		
		searchStr = searchText;
		
		parse();
	};
	this.getTextSearch = function(){
		var text = "";
		
		if (_tempSection != null){
			text+= _tempSection;
		} else {
			text+= _section;
		}		
		text+= _name;
		
		var address = "";
		
		if (_address.length > 0){
			address+= _address;
			if (_tempFilter != null || _filter.length > 0){
				address+= ",";
			}
		}
		if (_tempFilter != null){
			address+= _tempFilter;
		} else {
			if (_filter.length > 0){
				address+= _filter;
			}
		}
		address = address.trim();
		if (address.length > 0){
			text+= " en:" + address;
		}
		text = text.trim();
		
		return text;
	};
	
	this.generateTextSearch = function(section, filter){			
		switch (section){
			case 1:
				_tempSection = "";
				break;
			case 2:
				_tempSection = textoPersona;
				break;
			case 3:
				_tempSection = textoEmpresa;
				break;
		}
		if (filter != null && filter != undefined && filter.length>0){
			_tempFilter = filter;
		}
	};
	
	this.getSection = function(){
		var section = "";
		
		if (_tempSection != null){
			section = _tempSection;
		} else {
			section = _section;
		}
		
		if (section.length === 0){
			return 1;
		} else if (section == textoPersona){
			return 2;
		} else if (section == textoEmpresa){
			return 3;
		}
	};
	
	this.resetTempValues = function(){
		_tempSection = null;
		_tempFilter = null;
	};
	
	this.getName = function(){
		return _name;
	};
	
	this.getAddress = function(){
		var address = "";
		
		if (_address.length > 0){
			address+= _address;
		}		
		if (_filter.length > 0){
			address+= " " + _filter;
		}		
		address = address.trim();		
			
		return address;
		
	};
	
	
	
	this.hasDirection = function(){
		if (_address.length > 0 || _filter > 0){
			return true;
		} else {
			return false;
		}
	};
	
	this.setFilterRegion = function(descRegion){
		var position;
		
		position = _filter.indexOf(textoRegion);			
		if (position != -1){
            if (position == 0){
            	_filter = textoRegion + " " + descRegion;
            } else {
                _filter = _filter.substring(0, position) + " " + textoRegion + " " + descRegion;                    
            }            
		} else {
			position = _filter.indexOf(textoRegion2);			
			if (position != -1){
                if (position == 0){
                	_filter = textoRegion2 + " " + descRegion;
                } else {
                    _filter = _filter.substring(0, position) + " " + textoRegion2 + " " + descRegion;                    
                }
			} else {
				_filter = _filter + " " + textoRegion2 + " " + descRegion;
			}
		}
		_filter = _filter.trim();
				
	};
	
	
	this.getRegion = function(){
		
		var texto = "";
		var position = _filter.indexOf(textoRegion);
		if (position == -1){
			position = _filter.indexOf(textoRegion2);
			texto = textoRegion;
		}
		var nameRegion = '';
		if (position != -1){
			texto = textoRegion2;
			nameRegion = _filter.substring(texto.length + position);
		}
		nameRegion = nameRegion.trim();
		var nroRegion = -1;
		
		switch(nameRegion){
			case "RM":
				nroRegion = 0;
				break;
			case "I":
				nroRegion = 1;
				break;
			case "II":
				nroRegion = 2;
				break;
			case "III":
				nroRegion = 3;
				break;
			case "IV":
				nroRegion = 4;
				break;
			case "V":
				nroRegion = 5;
				break;
			case "VI":
				nroRegion = 6;
				break;
			case "VII":
				nroRegion = 7;
				break;
			case "VIII":
				nroRegion = 8;
				break;
			case "IX":
				nroRegion = 9;
				break;
			case "X":
				nroRegion = 10;
				break;
			case "XI":
				nroRegion = 11;
				break;
			case "XII":
				nroRegion = 12;
				break;			
			case "XIV":
				nroRegion = 14;
				break;
			case "XV":
				nroRegion = 15;
				break;
			
				
		}
		
		return nroRegion;
	}
	
}