//--------------------------------------------------------------------//

function search_for_tag(){

	var loc=$("#txt_location_search_url").val();	

	$.ajax({				

		type: "POST",

		url: "/query/search_for_tag",

		data: "tag_name="+$("#txt_tag_search").val(),

		success: function(msg){

			myres=msg.split("|");

			if ((myres[0]!='false') && (myres[0]!='') && (loc!='')){

				window.parent.location='/' + myres[0] + '_' + loc;

			}else if ((myres[0]!='false') && myres[0]!=''){

				window.parent.location='/' + myres[0];

			}else{
				$("#looking_for_err").show();
				$("#txt_tag_search").select();
			}

			return false;

		}

	})

}

		

function formatItem(row) {

	//return row[0];	

	if (row[2]=='member'){

		var oRep = $('#txt_tag_search').val();					

		var re = new RegExp(oRep, 'gi');

		var oString = row[0];

		var nString = oString.replace(re, '<strong>' + $('#txt_tag_search').val() + '</strong>');		

		return "<div style=\"border-top: 1px dotted red; height: 24px; margin-top: -2px; padding-top: 6px;\"><table style=\"margin-top:-4px; width: 400px; height: 24px;\"><tr><td>"+ nString + "</td><td style=\"background-image:url(http://www.whereismydoctor.com/images/doctor.png); background-position: right top; padding-top: 4px; text-align: right;background-repeat: no-repeat; padding-right: 25px; margin-top: 2px; font-size: 11px; font-weight: bold; \">DOCTORS</td></tr></table></div>";}

	else if (row[2]=='branch'){

		var oRep = $('#txt_tag_search').val();					

		var re = new RegExp(oRep, 'gi');

		var oString = row[0];

		var nString = oString.replace(re, '<strong>' + $('#txt_tag_search').val() + '</strong>');		

		return "<div style=\"border-top: 1px dotted red; height: 24px; margin-top: -2px; padding-top: 6px;\"><table style=\"margin-top:-4px; width: 400px; height: 24px;\"><tr><td>"+ nString + "</td><td style=\"background-image:url(http://www.whereismydoctor.com/images/branches.png); background-position: right top; padding-top: 4px; text-align: right;background-repeat: no-repeat; padding-right: 25px; margin-top: 2px; font-size: 11px; font-weight: bold; \">BRANCH</td></tr></table></div>";}

	else if (row[2]=='treatment'){		

		var oRep = $('#txt_tag_search').val();					

		var re = new RegExp(oRep, 'gi');

		var oString = row[0];

		var nString = oString.replace(re, '<strong>' + $('#txt_tag_search').val() + '</strong>');		

		return "<div style=\"border-top: 1px dotted red; height: 24px; margin-top: -2px; padding-top: 6px;\"><table style=\"margin-top:-4px; width: 400px; height: 24px;\"><tr><td>"+ nString + "</td><td style=\"background-image:url(http://www.whereismydoctor.com/images/treatment.png); background-position: right top; padding-top: 4px; text-align: right;background-repeat: no-repeat; padding-right: 25px; margin-top: 2px; font-size: 11px; font-weight: bold; \">TREATMENTS</td></tr></table></div>";}

	else{

		return row[0];

	}		

}



function formatResult(row) {

	return row[0].replace(/(<.+?>)/gi, '');

}



$(document).ready(function() {

	

	$("#txt_tag_search").autocomplete("/query/get_tag_list", 

		{

			delay:100,

			matchContains:true,				

			formatItem:formatItem,

			formatResult:formatResult,

			minChars:3,

			max: 20,

			autoFill:false			

		}

	).result(function(event, data, formatted) {

			if (data){

				if (data[1].charAt(0)=='/'){

					window.parent.location=data[1];

				}else{
					$("#txt_location_search").focus();
					$("#looking_for_err").hide();

				}

			}

	});				

				

	function formatItemLocation(row) {			        		        

		return "<img src='/images/flags/"+row[1]+".gif'/>&nbsp;" + row[0];

	}

			

	$("#txt_location_search").autocomplete("/query/get_location_list", 

		{

			delay:100,

			matchContains:true,				

			formatItem:formatItemLocation,

			formatResult:formatResult,

			minChars:2,

			autoFill:false

		}

	).result(function(event, data, formatted) {

			if (data){

				$("#txt_location_search_url").val(data[2]);
				search_for_tag();

			}		

	});

	

});

//--------------------------------------------------------------------//
