function get_select_photo(inc_hidden){

	if (all_empty()){
		window.alert("Please, for searching fill at least one field ");
		return "";
	}else{
		//--- CLAUSULE SELECT FROM
		if (any_subject()){
			sql = "Select photos.*, country.co_name, photo_subject.su_id";
			sql = sql + " from photos, country, photo_subject where";
			sql = sql + " photos.ph_id = photo_subject.ph_id"; 
			sql = sql + " and photos.ph_country_id = country.co_id"; 
		}else{
			sql = "Select photos.*, country.co_name from photos, country where";
			sql = sql + " photos.ph_country_id = country.co_id"; 
		}
		
		if(!inc_hidden){
			//--- CLAUSULE WHERE NOT HIDDEN
			sql = sql + " and photos.ph_hidden=\"FALSE\"" ;
		}

		//--- CLAUSULE WHERE SUBJECTS
		if (any_subject()) sql = sql + select_subjects() ;

		//--- CLAUSULE WHERE COUNTRY
		if (document.fSearch.country.value != "0"){
			sql = sql + " and photos.ph_country_id = " + document.fSearch.country.value ; 		
		};

		//--- CLAUSULE WHERE FILM FORMAT
		if (document.fSearch.film_format.value != "0"){
			sql = sql + " and photos.ph_film_format_id = " + document.fSearch.film_format.value ; 		
		};
		//--- CLAUSULE WHERE FILM MEDIUM
		if (document.fSearch.film_medium.value != "0"){
			sql = sql + " and photos.ph_film_medium_id = " + document.fSearch.film_medium.value ; 		
		};
		//--- CLAUSULE WHERE FILM TYPE
		if (document.fSearch.film_type.value != "0"){
			sql = sql + " and photos.ph_film_type_id = " + document.fSearch.film_type.value ; 		
		};

		//--- CLAUSULE WHERE CAMERA
		if (document.fSearch.camera.value != "0"){
			sql = sql + " and photos.ph_camera_id = " + document.fSearch.camera.value ; 		
		};

		//--- CLAUSULE WHERE SCANNER
		if (document.fSearch.scanner.value != "0"){
			sql = sql + " and photos.ph_scanner_id = " + document.fSearch.scanner.value ; 		
		};

		
		//--- CLAUSULE WHERE LOCATION
		if (document.fSearch.location.value != ""){
			sql = sql + " and photos.ph_location = \"" + document.fSearch.location.value  + "\"";
		};
		

		//--- CLAUSULE WHERE DATE PHOTO
		date_from = document.fSearch.date_from.value;
		date_to= document.fSearch.date_to.value;
		if (date_from != ""){
			sql = sql + " and photos.ph_date_photo >= \"" + date_from  + "\"";
		};
		if (date_to != ""){
			sql = sql + " and photos.ph_date_photo <= \"" + date_to  + "\"";
		};

		//--- CLAUSULE WHERE DATE ENTRY
		date_from = document.fSearch.date_entry_from.value;
		date_to= document.fSearch.date_entry_to.value;
		if (date_from != ""){
			sql = sql + " and photos.ph_date_entry >= \"" + date_from  + "\"";
		};
		if (date_to != ""){
			sql = sql + " and photos.ph_date_entry <= \"" + date_to  + "\"";
		};

		//--- CLAUSULE KEYWORDS
		keywords = document.fSearch.keywords.value;
	
		hay_comillas = keywords.indexOf("\"",0);
		while(hay_comillas != -1){
			keywords = keywords.replace("\"", "");
			hay_comillas = keywords.indexOf("\"",0)
		}
		
		hay_comodines = keywords.indexOf("*",0);
		while(hay_comodines != -1){			
			keywords = keywords.substring(0, hay_comodines) + ".*" + keywords.substring(hay_comodines+1, keywords.length)
			hay_comodines = keywords.indexOf("*", hay_comodines + 2)
		}		
		
	
		if (keywords != ""){
			keywords_array = keywords.split(" ");
			for (i=0; i<keywords_array.length; i++){
			
				patron = "[ ,.()?:;!]"
				
				sql = sql + " and (" ; 
				
				//start by keword
				sql = sql + " photos.ph_title REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_descrip REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_archive_code REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_location REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_photo_url REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " country.co_name REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_creator REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_publisher REGEXP \"^" + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_rights REGEXP \"^" + keywords_array[i] + patron + "\" or "; 

				//finish by keword
				sql = sql + " photos.ph_title REGEXP \"" + patron + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_descrip REGEXP \"" + patron + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_archive_code REGEXP \"" + patron + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_location REGEXP \"" + patron + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_photo_url REGEXP \"" + patron + keywords_array[i] + "$\" or "; 
				sql = sql + " country.co_name REGEXP \"" + patron + keywords_array[i] + "$\" or "; 				
				sql = sql + " photos.ph_creator REGEXP \"" + patron + keywords_array[i] + "$\" or "; 				
				sql = sql + " photos.ph_publisher REGEXP \"" + patron + keywords_array[i] + "$\" or "; 				
				sql = sql + " photos.ph_rights REGEXP \"" + patron + keywords_array[i] + "$\" or "; 				

				// only keyword
				sql = sql + " photos.ph_title REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_descrip REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_archive_code REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_location REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_photo_url REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " country.co_name REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_creator REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_publisher REGEXP \"^" + keywords_array[i] + "$\" or "; 
				sql = sql + " photos.ph_rights REGEXP \"^" + keywords_array[i] + "$\" or "; 
				
				//has keyword
				sql = sql + " photos.ph_title REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_descrip REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_archive_code REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_location REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 
				sql = sql + " photos.ph_photo_url REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 
				sql = sql + " country.co_name REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 				
				sql = sql + " photos.ph_creator REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 				
				sql = sql + " photos.ph_publisher REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 				
				sql = sql + " photos.ph_rights REGEXP \"" + patron + keywords_array[i] + patron + "\" or "; 				
				
				//search year
				sql = sql + " year(photos.ph_date_photo) = \"" + keywords_array[i]  + "\" or "; 				

				sql = sql.substring(0,sql.length - 3)+ ")";
				
			};
			
		};
		//--- CLAUSULE ORDER BY
		sql = sql + get_order() ;
		//sql = sql + get_order_dir();
		
		return sql;			
	};

}
//---------------------------------------------------------------------------
function any_subject(){
    for (i=0;i<document.fSearch.subject.length;i++){ 
       if (document.fSearch.subject[i].checked) 
         return true; 
    } ;
	
	return false;

}

//--------------------------------------------------------------------------- 
function select_subjects(){
	return_value = " and (";
    for (i=0;i<document.fSearch.subject.length;i++){ 
       if (document.fSearch.subject[i].checked) 
          return_value = return_value +  " photo_subject.su_id = " +document.fSearch.subject[i].value + " or"; 
    } 
	return_value = return_value.substring(0,return_value.length - 3)+ ")";
    return return_value;
} 
//---------------------------------------------------------------------------
function get_order(){
    var i 
    for (i=0;i<document.fSearch.order_by.length;i++){ 
       if (document.fSearch.order_by[i].checked) 
          break; 
    } 
    return " order by " + document.fSearch.order_by[i].value;
} 
//---------------------------------------------------------------------------
function get_order_dir(){
    var i 
    for (i=0;i<document.fSearch.order_dir.length;i++){ 
       if (document.fSearch.order_dir[i].checked) 
          break; 
    } 
    return " " + document.fSearch.order_dir[i].value ;
} 

//---------------------------------------------------------------------------
function all_empty(){
	if (document.fSearch.keywords.value != "" ) return false;
	if (document.fSearch.location.value != "") return false;
	if (document.fSearch.date_from.value != "") return false;
	if (document.fSearch.date_to.value != "") return false;
	if (document.fSearch.date_entry_from.value != "") return false;
	if (document.fSearch.date_entry_to.value != "") return false;
	if (document.fSearch.country.value != "0") return false;
	if (document.fSearch.film_format.value != "0") return false;
	if (document.fSearch.film_type.value != "0") return false;
	if (document.fSearch.film_medium.value != "0") return false;
	if (document.fSearch.camera.value != "0") return false;
	if (document.fSearch.scanner.value != "0") return false;
	if (any_subject()) return false;

	return true;	
}


