function ManageSearch( args ) {
	this.pathMainPictures = "images/portada/";
	this.strImgMore = 'images/more.jpg';
	this.strDay = "days";
	this.strNight = "nights";
	this.strMore = 'more';
	this.strSeparatorDuration = "/";
	this.strAnd = "&";
	this.strSeparator = ",";
	this.strExt = ".php";
	this.strPriceSymbol = "USD$";
	this.strPriceDecimal = ".00";
	this.strTwoPoints = ':';
	this.strSpace = '&nbsp;';
	this.vtIdComboCountry = "vtIdComboCountry";
	this.vtIdComboStyle = "vtIdComboStyle";
	this.vtHPqsMessage = 'qsMessage';
	this.vtHPTourIdeas = 'vtTourIdeas';
	this.vtHPPanelTableItem = "vtHPPanelTableItem";
	this.msplace = args.msplace;		// List
	this.mscountry = args.mscountry;	// Hash
	this.mscategory = args.mscategory;	// List
	this.mstour = args.mstour;			// Hash
	this.msdefault = args.msdefault;	// List
	this.className = "qsComboField";
	this.headerTextCode = 'Code';
	this.headerTextDuration = 'Duration';
	this.headerTextPriceFrom = 'Price From';
	this.headerTextPlaces = 'Highlights';
	this.errorMsgEmptySearch = "You have to fill some field in the form.";
	this.errorMsgOnlyNumbers = "Values have to be numbers.";
	this.errorMsgNoTourMatch = "Sorry, no tours match with the information you gave us. Please, try another tour.";
	this.params = new Array( 'class', 'colspan', 'rowspan', 'href', 'cellspacing', 'cellpadding' );

	this.comboCountryContent = this.buildComboCountry();
	this.comboCategoryContent = this.buildComboCategory();
}

ManageSearch.prototype.buildComboBody = function( content, id ) {
	var str = '<select id="' + id + '" class="' + this.className + '">';
	str += content;
	str += '</select>';
	return str;
}

ManageSearch.prototype.buildComboItem = function( value, label ) {
	var str = '<option value="';
	str += value + '">';
	str += label;
	str += '</option>';
	return str;
}

ManageSearch.prototype.showComboCountry = function() {
	var divComboCountry = document.getElementById( "vtSearchByCountry" );
	divComboCountry.innerHTML = this.comboCountryContent;
}

ManageSearch.prototype.buildComboCountry = function() {
	var str = '';
	
	str += this.buildComboItem( "", "--- Select a Country ---" );

	for ( var c = 0; c < this.mscountry.name.length; c++ ) {
		str += this.buildComboItem( this.mscountry.id_country[c], this.mscountry.name[c] );
	}
	
	return this.buildComboBody( str, this.vtIdComboCountry );
}

ManageSearch.prototype.showComboCategory = function() {
	var divComboCountry = document.getElementById( "vtSearchByStyle" );
	divComboCountry.innerHTML = this.comboCategoryContent;
}

ManageSearch.prototype.buildComboCategory = function() {
	var str = '';
	
	str += this.buildComboItem( "", "--- Select a Style ---" );

	for ( var c = 0; c < this.mscategory.length; c++ ) {
		str += this.buildComboItem( c, this.mscategory[c] );
	}
	
	return this.buildComboBody( str, this.vtIdComboStyle );
}

ManageSearch.prototype.makeDefault = function() {
	var divSearchResultPanel = document.getElementById( "vtSearchResultPanel" );
	var divMessage = document.getElementById( this.vtHPqsMessage );
	var divTourIdeas = document.getElementById( this.vtHPTourIdeas );
	divMessage.style.display = 'none';
	divTourIdeas.style.display = 'block';
	divSearchResultPanel.innerHTML = this.buildToursPage( this.msdefault, false );
}

ManageSearch.prototype.makeSearch = function() {
	var divSearchResultPanel = document.getElementById( "vtSearchResultPanel" );
	if( this.validateQuickSearchForm() )
		divSearchResultPanel.innerHTML = this.buildToursPage( this.engineSearch(), true );
}

ManageSearch.prototype.validateQuickSearchForm = function() {
	var valueFieldCountry = document.getElementById( this.vtIdComboCountry ).value;
	var valueFieldStyle = document.getElementById( this.vtIdComboStyle ).value;
	var valueFieldKeyword = document.getElementById( "vtSearchByKeywords" ).value;
	var valueSearchByPriceMin = document.getElementById( "vtSearchByPriceMin" ).value;
	var valueSearchByPriceMax = document.getElementById( "vtSearchByPriceMax" ).value;

	valueFieldKeyword = valueFieldKeyword.replace( /\s/g, '' );
	valueSearchByPriceMin = valueSearchByPriceMin.replace( /\s/g, '' );
	valueSearchByPriceMax = valueSearchByPriceMax.replace( /\s/g, '' );

	if( valueFieldCountry == "" && valueFieldStyle == "" && valueFieldKeyword == "" && valueSearchByPriceMin == "" && valueSearchByPriceMax == "" ) {
		alert( this.errorMsgEmptySearch );
		return false;
	}
	if( ( valueSearchByPriceMin != "" || valueSearchByPriceMax != "" ) && ( !this.isInteger( valueSearchByPriceMin ) || !this.isInteger( valueSearchByPriceMax ) ) ) {
		alert( this.errorMsgOnlyNumbers );
		return false;
	}

	return true;
}

ManageSearch.prototype.engineSearch = function() {
	var arrayResult = this.GetPositionsFromRegister();

	arrayResult = this.SearchByCountry( arrayResult );
	arrayResult = this.SearchByStyle( arrayResult );
	arrayResult = this.SearchByKeyword( arrayResult );
	arrayResult = this.SearchByPrice( arrayResult );

	return arrayResult;
}

ManageSearch.prototype.GetPositionsFromRegister = function() {
	var arrayTemp = new Array();

	for( var a = 0; a < this.mstour.id_category.length ; a++ ) {
		arrayTemp.push( a );
	}

	return arrayTemp;
}

ManageSearch.prototype.SearchByCountry = function( arrayResult ) {
	var arrayTmp = new Array();
	var valueFieldCountry = document.getElementById( this.vtIdComboCountry ).value;

	if( valueFieldCountry != "" ) {
		for( var a = 0; a < arrayResult.length ; a++ ) {
			if( this.mstour.id_country[ arrayResult[a] ] == valueFieldCountry ) {
				arrayTmp.push( arrayResult[a] );
			}
		}
	}
	else {
		arrayTmp = arrayResult;
	}
	
	return arrayTmp;
}

ManageSearch.prototype.SearchByStyle = function( arrayResult ) {
	var arrayTmp = new Array();
	var valueFieldStyle = document.getElementById( this.vtIdComboStyle ).value;

	if( valueFieldStyle != "" ) {
		for( var a = 0; a < arrayResult.length ; a++ ) {
			if( this.mstour.id_category[ arrayResult[a] ] == valueFieldStyle ) {
				arrayTmp.push( arrayResult[a] );
			}
		}
	}
	else {
		arrayTmp = arrayResult;
	}
	
	return arrayTmp;
}

ManageSearch.prototype.SearchByKeyword = function( arrayResult ) {
	var arrayTmp = new Array();
	var valueFieldKeyword = document.getElementById( "vtSearchByKeywords" ).value;
	var valueFieldKeyword_tmp = valueFieldKeyword;
	valueFieldKeyword_tmp = valueFieldKeyword_tmp.replace( /\s/g, '' );

	if( valueFieldKeyword_tmp != "" ) {
		for( var a = 0; a < arrayResult.length ; a++ ) {
			if( this.StringSearchEngine( arrayResult[a], valueFieldKeyword ) ) {
				arrayTmp.push( arrayResult[a] );
			}
		}
	}
	else {
		arrayTmp = arrayResult;
	}
	
	return arrayTmp;
}

ManageSearch.prototype.StringSearchEngine = function( pos, keyword ) {
	var keyword_curr = keyword.replace( /\s+/g, ',' );
	var arrayKeyword = this.GetArrayFromString( keyword_curr );
	var arrayStatus = new Array( arrayKeyword.length );
	var status = false;

	for( var k = 0; k < arrayKeyword.length; k++ ) {
		if( this.SearchByAnyStringField( this.mstour.name[ pos ], arrayKeyword[k] ) )
			status = true;
		if( this.SearchByAnyStringField( this.mstour.code[ pos ], arrayKeyword[k] ) )
			status = true;
		if( this.SearchByAnyStringField( this.mstour.description[ pos ], arrayKeyword[k] ) )
			status = true;
		if( this.SearchByPlace( this.mstour.places[ pos ], arrayKeyword[k] ) )
			status = true;
		arrayStatus[ k ] = status;
		status = false;
	}

	for( var s = 0; s < arrayStatus.length; s++ )
		if( arrayStatus[s] == false )
			return false;

	return true;
}

ManageSearch.prototype.SearchByPlace = function( str, keyword ) {
	var arrayPlaces = this.GetArrayFromString( str );

	for( var s = 0; s < arrayPlaces.length; s++ )
		if( this.SearchByAnyStringField( this.msplace[ arrayPlaces[s] ], keyword ) )
			return true;

	return false;
}

ManageSearch.prototype.SearchByAnyStringField = function( str, regexp ) {
	var strTemp = new String( str );
	var reTemp = new RegExp( regexp, 'i' );
	
	if( strTemp.search( reTemp ) != -1 )
		return true;

	return false;
}

ManageSearch.prototype.SearchByPrice = function( arrayResult ) {
	var arrayTmp = new Array();
	var arrayPriceInput = new Array();
	var valueSearchByPriceMin = document.getElementById( "vtSearchByPriceMin" ).value;
	var valueSearchByPriceMax = document.getElementById( "vtSearchByPriceMax" ).value;

	valueSearchByPriceMin = valueSearchByPriceMin.replace( /\s/g, '' );
	valueSearchByPriceMax = valueSearchByPriceMax.replace( /\s/g, '' );

	arrayPriceInput = this.ValidateInputPrice( valueSearchByPriceMin, valueSearchByPriceMax );
	valueSearchByPriceMin = arrayPriceInput[0];
	valueSearchByPriceMax = arrayPriceInput[1];

	if( valueSearchByPriceMin != "" && valueSearchByPriceMax != "" ) {
		for( var a = 0; a < arrayResult.length ; a++ ) {
			if( valueSearchByPriceMin <= this.mstour.pricefrom[ arrayResult[a] ] && valueSearchByPriceMax >= this.mstour.pricefrom[ arrayResult[a] ] ) {
				arrayTmp.push( arrayResult[a] );
			}
		}
	}
	else {
		arrayTmp = arrayResult;
	}
	
	return arrayTmp;
}

ManageSearch.prototype.ValidateInputPrice = function( priceMin, priceMax ) {
	var integerstatus = false;
	var arrayTemp = new Array();

	if( priceMin != '' && priceMax != '' ) {
		if( this.isInteger( priceMin ) && this.isInteger( priceMax ) ) {
			integerstatus = true;
		}
	}
	else if( priceMin != '' && priceMax == '' ) {
		if( this.isInteger( priceMin ) ) {
			priceMax = priceMin;
			integerstatus = true;
		}
	}
	else if( priceMin == '' && priceMax != '' ) {
		if( this.isInteger( priceMax ) ) {
			priceMin = priceMax;
			integerstatus = true;
		}
	}

	if( !integerstatus ) {
		priceMin = priceMax = "";
		this.clearPriceFields();
	}

	arrayTemp[0] = priceMin;
	arrayTemp[1] = priceMax;

	return arrayTemp;
}

ManageSearch.prototype.clearPriceFields = function( ) {
	document.getElementById( "vtSearchByPriceMin" ).value = '';
	document.getElementById( "vtSearchByPriceMax" ).value = '';
}

ManageSearch.prototype.isInteger = function( val ) {
	if( val.match( /^\d+$/ ) )
		return true;

	return false;
}

ManageSearch.prototype.buildToursPage = function( arrayResult, showMsg ) {
	var str = '';
	var textResutMessage = '';
	var textResutPanel = '';
	var divSearchResultMessage = '';
	var divMessage = '';
	var divTourIdeas = '';
	var divColumnHPContainer = '';

	if( !arrayResult.length ) {
		textResutMessage = this.errorMsgNoTourMatch;
	}
	else {
		textResutMessage = "We have " + arrayResult.length + " tour" + ( arrayResult.length > 1 ? "s" : "" ) + " available for you";
		for ( var t = 0; t < arrayResult.length; t++ ) {
			str += this.CreateRegister( arrayResult[t] );
		}
		
		textResutPanel = this.CreateTable( str, ['vtTravelListMain', 0, 0], [0, 4, 5] );
	}

	if( showMsg ) {
		divSearchResultMessage = document.getElementById( "vtSearchResultMessage" );
		divMessage = document.getElementById( this.vtHPqsMessage );
		divTourIdeas = document.getElementById( this.vtHPTourIdeas );
		divMessage.style.display = 'block';
		divTourIdeas.style.display = 'none';
		divSearchResultMessage.innerHTML = textResutMessage;
	}

	return textResutPanel;
}

ManageSearch.prototype.CreateRegister = function( numregister ) {
	var str = '';
	var strHeader = '';
	var strDetails = '';

	strHeader += this.CreateHeaderColumnTourName( this.mstour.name[ numregister ] );
	strHeader += this.CreateHeaderColumnLink( this.mstour.code[ numregister ] );
	strDetails += this.CreateContentCellDescription( this.mstour.picture[ numregister ], this.mstour.description[ numregister ] );
//	strDetails += this.CreateContentCellCode( this.mstour.code[ numregister ] );
	if( this.mstour.id_category[ numregister ] != 3) {
		strDetails += this.CreateContentCellDuration( this.mstour.duration[ numregister ] );
		strDetails += this.CreateContentCellPriceFrom( this.mstour.pricefrom[ numregister ] );
		strDetails += this.CreateContentCellPlaces( this.mstour.places[ numregister ] );
	}
	str += this.CreateSingleField( strHeader );
	str += this.CreateContentField( this.CreateTable( strDetails, ['vtTravelListTableContent'], [0] ) );
	return this.CreateCell( str );
}

ManageSearch.prototype.CreateSingleField = function( data ) {
	var str = this.CreateCell( this.CreateColumn( this.strSpace, [ 'vtTravelListTableHeaderSpace' ], [ 0 ] ) + data );
	str = this.CreateCell( this.CreateColumn( this.CreateTable( str, [ 'vtTravelListTableHeader' ], [ 0 ] ), [ 'vtTravelListTableItemHeader' ], [ 0 ] ) );
	return str;
}

ManageSearch.prototype.CreateContentField = function( data ) {
	return this.CreateCell( this.CreateColumn( data, [ 'vtTravelListTableItemContent' ], [0] ) );
}

ManageSearch.prototype.CreateContentPicture = function( picture ) {
	return this.CreateImage( this.pathMainPictures + picture, [ 'vtTravelListMainPictures' ], [ 0 ] );
}

ManageSearch.prototype.CreateHeaderColumnTourName = function( tourname ) {
	return this.CreateColumn( this.CreateSpan( tourname, [ 'vtTravelListItemHeader' ], [ 0 ] ), [ 'vtTravelListTableHeaderTitle' ], [ 0 ] );
}

ManageSearch.prototype.CreateHeaderColumnLink = function( code ) {
	var str = this.CreateImage( this.strImgMore, {} ) + ' ' + this.CreateSpan( this.strMore, [ 'vtTravelListItemDetailActionText' ], [ 0 ] );
	str = this.CreateLink( str, [ 'vtTravelListLink', this.CodeToUrl( code ) ], [ 0, 3 ] );
	return this.CreateColumn( str, [ 'vtTravelListTableHeaderAction' ], [ 0 ] );
}

ManageSearch.prototype.CreateContentCellDescription = function( picture, description ) {
	var str = this.CreateColumn( this.CreateContentPicture( picture ), [ 'vtTravelListTableContentPicture', 5 ], [ 0, 2 ] );
	str += this.CreateColumn( description, [ 'vtTravelListItemDetailContent', 3 ], [ 0, 1 ] );
	str = this.CreateCell( str );
	return str;
}

ManageSearch.prototype.GetPlacesString = function( places ) {
	var arrayString = this.GetArrayFromString( places );
	var str = '';
	var comma = '';

	for( var p = 0; p < ( arrayString.length - 1 ); p++ ) {
		str += comma + " " + this.msplace[arrayString[p]];
		comma = this.strSeparator;
	}

	str += " " + this.strAnd + " " + this.msplace[arrayString[ arrayString.length - 1 ]];

	return str;
}

ManageSearch.prototype.CreateContentCellCode = function( code ) {
	return this.CreateContentCellDetails( this.headerTextCode, code );
}

ManageSearch.prototype.CreateContentCellDuration = function( duration ) {
	return this.CreateContentCellDetails( this.headerTextDuration, this.GetDurationString( duration ) );
}

ManageSearch.prototype.GetDurationString = function( duration ) {
	var arrayString = this.GetArrayFromString( duration );
	return arrayString[0] + ' ' + this.strDay + this.strSeparatorDuration + arrayString[1] + ' ' + this.strNight;
}

ManageSearch.prototype.CreateContentCellPriceFrom = function( pricefrom ) {
	return this.CreateContentCellDetails( this.headerTextPriceFrom, this.strPriceSymbol + pricefrom + this.strPriceDecimal );
}

ManageSearch.prototype.CreateContentCellPlaces = function( places ) {
	return this.CreateContentCellDetails( this.headerTextPlaces, this.GetPlacesString( places ) );
}

ManageSearch.prototype.GetArrayFromString = function( str ) {
	var strObject = new String( str );
	var arrayString = strObject.split( this.strSeparator );
	return arrayString;
}

ManageSearch.prototype.CreateContentCellDetails = function( key, value ) {
	var str = this.CreateColumn( key, [ 'vtTravelListItemDetailHeader' ], [ 0 ] );
	str += this.CreateColumn( this.strTwoPoints, {} );
	str += this.CreateColumn( value, [ 'vtTravelListItemDetailContent' ], [ 0 ] );
	str = this.CreateCell( str );
	return str;
}

ManageSearch.prototype.CodeToUrl = function( code ) {
	var url = code.replace( /-/g, '_' );
	url = url.toLowerCase();
	url += this.strExt;
	return url;
}

ManageSearch.prototype.makeReset = function() {
	var divTourIdeas = document.getElementById( this.vtHPTourIdeas );
	divTourIdeas.style.display = 'block';
	document.forms[1].reset();
	document.getElementById( "vtSearchResultPanel" ).innerHTML = "";
	document.getElementById( "vtSearchResultMessage" ).innerHTML = "";
	this.makeDefault();
}

ManageSearch.prototype.BuildArgs = function( values, labels ) {
	var str = '';

	for( var i = 0; i < values.length; i++ ) {
		str += ' ' + this.params[labels[i]] + '="' + values[i] + '"';
	}

	return str;
}

ManageSearch.prototype.CreateTable = function( data, vals, lbls ) {
	return '<table' + this.BuildArgs( vals, lbls ) + '>' + data + '</table>';
}

ManageSearch.prototype.CreateCell = function( data ) {
	return '<tr>' + data + '</tr>';
}

ManageSearch.prototype.CreateColumn = function( data, vals, lbls ) {
	return '<td' + this.BuildArgs( vals, lbls ) + '>' + data + '</td>';
}

ManageSearch.prototype.CreateSpan = function( data, vals, lbls ) {
	return '<span' + this.BuildArgs( vals, lbls ) + '>' + data + '</span>';
}

ManageSearch.prototype.CreateLink = function( data, vals, lbls ) {
	return '<a' + this.BuildArgs( vals, lbls ) + '>' + data + '</a>';
}

ManageSearch.prototype.CreateImage = function( data, vals, lbls ) {
	return '<img' + this.BuildArgs( vals, lbls ) + ' src="' + data + '" />';
}

app.ManageSearch = ManageSearch;