var loading_img = new Image();
loading_img.src = 'images/loading.gif';

var key_pressed = 0;
//ajax
/* The following function creates an XMLHttpRequest object... */

function private_show_hide_other(input){
	try{
		if (input == 'cityzip'){
			var ctrl = document.getElementById("divResultscityzip");
			if (ctrl != null){
				ctrl.style.display = "none";
			}
		}
		if (input == 'txt_categ'){
			var ctrl = document.getElementById("divResultstxt_categ");
			if (ctrl != null){
				ctrl.style.display = "none";
			}
		}
	}catch(err){

	}

}

function show_hide_other(input){
	try{
		if (input == 'cityzip'){
			setTimeout("private_show_hide_other('"+input+"')", 500);
		}
		if (input == 'txt_categ'){
			setTimeout("private_show_hide_other('"+input+"')", 500);
		}
	}catch(err){

	}
}

function createRequestObject(){
        var request_o; //declare the variable to hold the object.
        var browser = navigator.appName; //find the browser name
        if(browser == "Microsoft Internet Explorer"){
                /* Create the object using MSIE's method */
                request_o = new ActiveXObject("Microsoft.XMLHTTP");
        }else{
                /* Create the object using other browser's method */
                request_o = new XMLHttpRequest();
        }
        return request_o; //return the object
}

function dataSearch(str, dataType, inputId,srchType)
{
	if( document.getElementById('hdn'+inputId)!=null)
 		document.getElementById('hdn'+inputId).value=-1;
    var divId = "divResults"+inputId;
    if(str.length > 2)
    {
       var url = "data_selector_ctrl/internal_request.php";
       var postData = "search="+encodeURIComponent(str)+"&data_type="+dataType+"&input_id="+inputId+"&srchtp="+srchType;

       var sqlSearch = document.getElementById("hdn_sql_search_"+inputId).value;
       postData += "&sql_search="+encodeURIComponent(sqlSearch);

		var div = document.getElementById(divId);

		/*	show loading picture
		div.innerHTML = '<img src="'+loading_img.src+'" />';

		div.style.display 		= "block"	;
		div.style.visibility 	= "visible"	;
*/

       var http_request = createRequestObject();
       http_request.open("post", url);
       http_request.onreadystatechange = function()
       {
            if(http_request.readyState == 4)
            {
                var response = http_request.responseText;

                div.style.display = "block";
                div.style.visibility = 'visible';
                div.innerHTML = response;
            }
       }
       http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
       http_request.send(postData);

    }
    else
    {
        var div = document.getElementById(divId)
        div.innerHTML = "";
        div.style.display = "none";
    }
}

function getTarget(e){
	if (e.target)
		targ = e.target;
	else
		if (e.srcElement)
	 		targ = e.srcElement;
	 return targ;
}


function dataSearchForAbsolutePosRequest(str, dataType, inputId, srchType, event){

		var divId = "divResults"+inputId;
		var div = document.getElementById(divId);
		var url = "data_selector_ctrl/internal_request.php?search="+encodeURIComponent(str)+"&data_type="+dataType+"&input_id="+inputId+"&srchtp="+srchType+"&header=1&time="+escape(currentTimestamp());

		var http_request = createRequestObject();

		http_request.open("get", url);
		http_request.onreadystatechange = function()
		{
			if(http_request.readyState == 4)
			{
				var response = http_request.responseText;
				div.style.display = "block";
				div.style.visibility = 'visible';
				obj = document.getElementById(inputId);

				div.style.left=getposOffset(obj, "left") + "px";//-clearbrowseredge(obj, "rightedge") + "px"
				div.style.top=getposOffset(obj, "top") + 15 + "px";//-clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + "px"

				div.innerHTML = response;
			}
		}
		http_request.send(null);

};

function dataSearchForAbsolutePos(str, dataType, inputId, srchType, event, show_loading)
{
  str = ltrim(str);
	if(!event)
		event = window.event;

	if( (event.which || event.keyCode) && ( (event.which == 13 || event.keyCode == 13) )   )
	{
		return;
	}

	if( document.getElementById('hdn'+inputId)!=null)
 		document.getElementById('hdn'+inputId).value=-1;

	var divId = "divResults"+inputId;
	var div = document.getElementById(divId);
	var obj = document.getElementById(inputId);

	if(str.length > 2)
    {
		var sPath = window.location.pathname;
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

		if (show_loading == 1){
			div.style.left=getposOffset(obj, "left") + "px";//-clearbrowseredge(obj, "rightedge") + "px"
			div.style.top=getposOffset(obj, "top") + 15 + "px";//-clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + "px"
			div.innerHTML = '<img style="position: relative; left: 0;" src="'+loading_img.src+'" />';

			div.style.display 		= "block"	;
			div.style.visibility 	= "visible"	;
		}

		var url = "data_selector_ctrl/internal_request.php?search="+encodeURIComponent(str)+"&data_type="+dataType+"&input_id="+inputId+"&srchtp="+srchType+"&header=1&time="+escape(currentTimestamp());

		var http_request = createRequestObject();

		http_request.open("get", url);
		http_request.onreadystatechange = function()
		{
			if(http_request.readyState == 4)
			{
				var response = http_request.responseText;

				if (show_loading == 0){
					div.style.display = "block";
					div.style.visibility = 'visible';

					div.style.left=getposOffset(obj, "left") + "px";//-clearbrowseredge(obj, "rightedge") + "px"
					div.style.top=getposOffset(obj, "top") + 15 + "px";//-clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + "px"
				}
				if(http_request.getResponseHeader("search")==encodeURIComponent(ltrim(document.getElementById(inputId).value)))
				div.innerHTML = response;
			}
		}
		http_request.send(null);

/*
		if (inputId == "txt_categ" && (sPage=="index.php" || sPage=="")){
			div.innerHTML='<img  src="'+loading_img.src+'" />';
			div.style.left=getposOffset(obj, "left") + "px";//-clearbrowseredge(obj, "rightedge") + "px"
			div.style.top=getposOffset(obj, "top") + 15 + "px";//-clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + "px"

			div.style.display = "block";
			div.style.visibility = 'visible';

			setTimeout("dataSearchForAbsolutePosRequest('"+str+"', '"+dataType+"', '"+inputId+"', '"+srchType+"', '"+event+"');", 3000);
		}else{
			if ((sPage=="index.php" || sPage=="")){
				div.innerHTML='<img  src="'+loading_img.src+'" />';
				div.style.left=getposOffset(obj, "left") + "px";//-clearbrowseredge(obj, "rightedge") + "px"
				div.style.top=getposOffset(obj, "top") + 15 + "px";//-clearbrowseredge(obj, "bottomedge") + obj.offsetHeight + "px"

				div.style.display = "block";
				div.style.visibility = 'visible';
			}
			dataSearchForAbsolutePosRequest(str, dataType, inputId, srchType, event);
		}
*/

    }
    else
    {
        var div = document.getElementById(divId)
        div.innerHTML = "";
        div.style.display = "none";
    }
}


function onResultSelected(str, inputId, hiddenParam)
{
    document.getElementById(inputId).value = str;
    if(document.getElementById('hdn_txt_'+inputId) != null)
    	document.getElementById('hdn_txt_'+inputId).value = str;
    var divId = "divResults"+inputId;
    var div = document.getElementById(divId);
    div.innerHTML = "";
	div.style.visibility = 'hidden';
   	if(hiddenParam!=null)
	   document.getElementById('hdn'+inputId).value=hiddenParam;
}

function onClickDropDownSelector(inputId){
	try{
		if (inputId == "flt_vendor_or_ad"){
			var input = document.getElementById("flt_category");
			if (input!=null){
				onResultSelected("", "flt_category", "");
			}
		}
		if (inputId == "flt_category"){
			var input = document.getElementById("flt_vendor_or_ad");
			if (input!=null){
				onResultSelected("", "flt_vendor_or_ad", "");
			}
		}
	}catch(err){
	}
}

function doDropDownSearch(param){
	try{
		if (param.length!=0){
			var auxArray = param.split(', ');

			if (auxArray.length >= 5){
				var inputId 	= ("" + auxArray[auxArray.length-3]).substring(1, ("" + auxArray[auxArray.length-3]).length-1);
				var hiddenParam = ("" + auxArray[auxArray.length-2]).substring(1, ("" + auxArray[auxArray.length-2]).length-1);

				var str = "";

				for (var i=1; i< (auxArray.length-3); i++){
					str += auxArray[i];
				}

				str 			= str.substring(1, ("" + auxArray[1]).length-1);

				onResultSelected(str.replace(/\\'/, "'"), inputId, hiddenParam);

				var hiddenValue = document.getElementById('filterResults');
				hiddenValue.value = "1";
			if (document.getElementById("form1") != null){
					document.getElementById("form1").submit();
				}
			}
				/*
			if (auxArray.length==5){
				var str 		= ("" + auxArray[1]).substring(1, ("" + auxArray[1]).length-1);
				var inputId 	= ("" + auxArray[2]).substring(1, ("" + auxArray[2]).length-1);
				var hiddenParam = ("" + auxArray[3]).substring(1, ("" + auxArray[3]).length-1);

				onResultSelected(str, inputId, hiddenParam);

				var hiddenValue = document.getElementById('filterResults');
				hiddenValue.value = "1";
				document.getElementById("form1").submit();
			}else{
				if (auxArray.length > 5){
					var inputId 	= ("" + auxArray[auxArray.length-3]).substring(1, ("" + auxArray[auxArray.length-3]).length-1);
					var hiddenParam = ("" + auxArray[auxArray.length-2]).substring(1, ("" + auxArray[auxArray.length-2]).length-1);

					var str = "";

					for (var i==0; i< (auxArray.length-3); i++){
						str += 	"" + auxArray[i];
					}

					str 			= str.substring(1, ("" + auxArray[1]).length-1);
				}
			}
			*/
		}
	}catch(err){
		alert(err);
	}
}

function doDropDownSearchKey(param, event){

	if(event.keyCode)
	{
		key_pressed = event.keyCode;
	}
	else if(event.which)
	{
		key_pressed = event.which;
	}


	if (key_pressed == 13){
		doDropDownSearch(param);
	}else{
		if (key_pressed == 38){
			var v_dd_l = document.getElementById("vendor_drop_down_list");
			if (v_dd_l != null){
				if (v_dd_l.style.display != "none" && v_dd_l.options.length != 0 && v_dd_l.options[0].selected){
					var input_vendor = document.getElementById("flt_vendor_or_ad");
					if (input_vendor != null){
						input_vendor.focus();
					}
				}
			}

			var k_dd_l = document.getElementById("keyword_drop_down_list");
			if (k_dd_l != null){
				if (k_dd_l.style.display != "none" && k_dd_l.options.length != 0 && k_dd_l.options[0].selected){
					var input_keyword = document.getElementById("flt_category");
					if (input_keyword != null){
						input_keyword.focus();
					}
				}
			}

		}else{
			if (key_pressed != 37 && key_pressed != 39 && key_pressed != 40){
				var v_dd_l = document.getElementById("vendor_drop_down_list");
				if (v_dd_l != null && v_dd_l.options.length != 0 ){
					var input_vendor = document.getElementById("flt_vendor_or_ad");
					if (input_vendor != null){
						v_dd_l.options[0].selected = true;
						input_vendor.focus();
						input_vendor.value += String.fromCharCode( key_pressed );
					}
				}

				var k_dd_l = document.getElementById("keyword_drop_down_list");
				if (k_dd_l != null && k_dd_l.options.length != 0 ){
					var input_keyword = document.getElementById("flt_category");
					if (input_keyword != null){
						k_dd_l.options[0].selected = true;
						input_keyword.focus();
						input_keyword.value += String.fromCharCode( key_pressed );
					}
				}
			}
		}
	}
}

function hidePopUpMenu(inputId){
	var div = document.getElementById("divResults"+inputId);
	div.style.visibility = 'hidden';
}

function syncDataSelectorText(inputId)
{
   var txtVal = document.getElementById(inputId).value;
   document.getElementById('hdn_txt_'+inputId).value = txtVal;

}

function OnEnterSearch(){
	try{

		var show_message = false;

		var v_dd_l = document.getElementById("vendor_drop_down_list");
		var k_dd_l = document.getElementById("keyword_drop_down_list");

		if (v_dd_l != null){
			if (v_dd_l.options.length != 0 && v_dd_l.options[v_dd_l.selectedIndex].value != ""){
				doDropDownSearch(v_dd_l.options[v_dd_l.selectedIndex].value);
			}else{
				show_message=true;
			}
		}

		if (k_dd_l != null){
			if (k_dd_l.options.length != 0 && k_dd_l.options[k_dd_l.selectedIndex].value != ""){
				doDropDownSearch(k_dd_l.options[k_dd_l.selectedIndex].value);
			}else{
				show_message=true;
			}
		}

		if (show_message){
			var button_go = document.getElementById("filterResults_Button");

			if (button_go != null){
				button_go.focus();
			}


			var elem_error = document.getElementById("error_paragraph");
			elem_error.innerHTML = "The keywords or venue that you entered does not have listings.Please select a valid or different keyword or venue!";

			var elem_error2 = document.getElementById("error_paragraph_2");

			if (elem_error2 != null){
				elem_error2.style.display = "none";
			}

		}

	}catch(err){

	}
}

function dataSearchDropDownList(str, dataType, inputId, srchType, event, show_loading)
{
	if(event.keyCode)
	{
		key_pressed = event.keyCode;
	}
	else if(event.which)
	{
		key_pressed = event.which;
	}

	if (key_pressed == 13){
		OnEnterSearch();
	}

	if (key_pressed == 40){
		if (inputId == "flt_vendor_or_ad"){
			var v_dd_l = document.getElementById("vendor_drop_down_list");
			if (v_dd_l != null){
				if (v_dd_l.options.length != 0 && v_dd_l.options[0].value != ""){
					v_dd_l.focus();
					if (v_dd_l.options.length > 1){
						v_dd_l.options[0].selected = false;
						v_dd_l.options[1].selected = true;
					}
				}
			}
		}
		if (inputId == "flt_category"){
			var k_dd_l = document.getElementById("keyword_drop_down_list");

			if (k_dd_l != null){
				if (k_dd_l.options.length != 0 && k_dd_l.options[0].value != ""){
					k_dd_l.focus();
					if (k_dd_l.options.length > 1){
						k_dd_l.options[0].selected = false;
						k_dd_l.options[1].selected = true;
					}
				}
			}
		}

		return;
	}

	var dataType = dataType + "_dropdown";

	if( document.getElementById('hdn'+inputId)!=null)
 		document.getElementById('hdn'+inputId).value=-1;
    var divId = "divResults"+inputId;

    if(str.length > 2)
    {
       var url = "data_selector_ctrl/internal_request.php";
       var postData = "search="+encodeURIComponent(str)+"&data_type="+dataType+"&input_id="+inputId+"&srchtp="+srchType;

       var sqlSearch = document.getElementById("hdn_sql_search_"+inputId).value;
       postData += "&sql_search="+encodeURIComponent(sqlSearch);

		var div = document.getElementById(divId);


		if (show_loading == 1){
			div.innerHTML = '<img src="'+loading_img.src+'" />';

			div.style.display 		= "block"	;
			div.style.visibility 	= "visible"	;
		}

       var http_request = createRequestObject();
       http_request.open("post", url);
       http_request.onreadystatechange = function()
       {
            if(http_request.readyState == 4)
            {
                var response = http_request.responseText;

				if (show_loading == 0){
					div.style.display = "block";
					div.style.visibility = 'visible';
				}
                div.innerHTML = response;

				if (inputId == "flt_vendor_or_ad"){
					var v_dd_l = document.getElementById("vendor_drop_down_list");
					if (v_dd_l != null){
						if (v_dd_l.options.length != 0 && v_dd_l.options[0].value != ""){
							v_dd_l.options[0].selected = true;
						}
					}
				}
				if (inputId == "flt_category"){
					var k_dd_l = document.getElementById("keyword_drop_down_list");

					if (k_dd_l != null){
						if (k_dd_l.options.length != 0 && k_dd_l.options[0].value != ""){
							k_dd_l.options[0].selected = true;
						}
					}
				}

            }
       }
       http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
       http_request.send(postData);
    }
    else
    {
        var div = document.getElementById(divId)
        div.innerHTML = "";
        div.style.display = "none";
    }
}
