UK = 1;
US = 3;


function toggleSearchType(country)
{


   if (country == UK || country == US)
   {
      document.getElementById('spanPostcode').style.display = 'block';
      document.getElementById('spanDistance').style.display = 'block';
      document.getElementById('spanRegion').style.display = 'none';
      document.getElementById('spanCity').style.display = 'none';
   }
   else
   {
      document.getElementById('spanPostcode').style.display = 'none';
      document.getElementById('spanDistance').style.display = 'none';
      document.getElementById('spanRegion').style.display = 'block';
      document.getElementById('spanCity').style.display = 'block';

     getOptionList('country', country, 'region');
   }

}


function  getOptionList(param, val, selFld)
 {
      selectObj = document.getElementById(selFld);
      $.ajax({url : baseUrl + 'search.php?ajax=true&action=getoptionlist',
               'type' : 'POST',
               'data' :'param='+param+'&val='+val,
               success: function(data){
               	  removeAllSelectOptions(selectObj);

                  arrList = data.split('##');
                  if (data != '')
                  {
                      $.each(arrList, function(){
                        selectObj.options[selectObj.options.length] = new Option(this.split(':')[1], this.split(':')[0]);
                        })
                     selectObj.disabled = false;
                     $(selectObj).parent('.span_box').show();
                  }
                  else{
                  	 $(selectObj).parent('.span_box').hide();
	                 selectObj.disabled = true;

                 }

                  if (selFld == 'region')
                    getOptionList('region', '', 'city');
               }
               });
    }

function removeAllSelectOptions(selObj)
{
      for(i = selObj.options.length; i > 0; i--)
        selObj.remove(i);
}

function validateFormSearch(formObj)
{
   if (formObj.country.value == '')
   {
        errAlert(formObj.country, 'select', 'Country is required field');
        return false;
   }

   if (formObj.country.value == UK || formObj.country.value == US)
   {
	    if (formObj.postcode.value == '')
        {   errAlert(formObj.postcode, 'text', 'Postcode is required field');
            return false;
        }
        else if(!isValidPostcode(formObj.country.value, formObj.postcode.value))
        {
           errAlert(formObj.postcode, 'text', 'Postcode is invalid');
           return false;
        }
   }
   else
   {
    /*
        if (!formObj.region.disabled && formObj.region.value == '')
        {
            errAlert(formObj.region, 'select', 'Region is required field');
            return false;
        }

        if (!formObj.city.disabled && formObj.city.value == '')
        {
            errAlert(formObj.city, 'select', 'City is required field');
            return false;
        }

        */
   }

   if(formObj.restaurant_name.value == txtRestaurantName)
     formObj.restaurant_name.value = '';

   if(formObj.postcode.value == txtPostcode)
     formObj.postcode.value = '';

   return true;
}

function isValidPostcode(country, postcode)
{   postcodeValue = removeSpaces(postcode.toUpperCase());

    if (country == UK)
    {  regexp = /^((GIR0AA)|((([A-PR-UWYZ][0-9][0-9]?)|([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))[0-9][ABD-HJLNP-UW-Z]{2}))$/;
       if (regexp.test(postcodeValue))
        return true;
       else
        return false;
    }
    else if (country == US)
    { regexp = /^[0-9]{5}((-| )[0-9]{4})?$/;

      if (regexp.test(postcodeValue))
        return true;
       else
        return false;
    }

    return true;
}

function removeSpaces(string) {
 return string.split(' ').join('');
}

function onFocusHint(txtFld)
{
    if (txtFld.name == 'restaurant_name')
       txtHint = txtRestaurantName;
    else
       txtHint = txtPostcode;

     if (txtFld.value == txtHint)
     {   txtFld.value = '';
         //txtFld.style.color = '#000000';
         $(txtFld).addClass('black-text').removeClass('grey-text');
     }

}

function onBlurHint(txtFld)
{
    if (txtFld.name == 'restaurant_name')
       txtHint = txtRestaurantName;
    else
       txtHint = txtPostcode;

    if(!emptyCheck(txtFld.value))
    {   txtFld.value = txtHint;
        //txtFld.style.color = '#7F7F7F';
        $(txtFld).addClass('grey-text').removeClass('black-text');
    }
}

function getCuisineList(countryVal)
{
   arraySelCuisines = (typeof(selCuisines) != "undefined" && selCuisines != '') ? selCuisines.split(',') : false;

   $.ajax({ url : baseUrl + 'index_utils.php?action=get_cuisine_list&country_val='+countryVal,
            type : 'GET',
            success : function(data) {
          	  htmlstr = '';
                if (data != '')
                {
                    json_data = eval('(' +  data + ')');
                    for(var d in json_data)
                    {
                        if (arraySelCuisines.constructor == Array && inArray(d, arraySelCuisines))
                            checkVal = 'checked';
                        else
                            checkVal = '';

                        htmlstr += '<input type="checkbox" name="cuisines[]" value="'+d+'" '+checkVal+' checked="checked" />'+json_data[d]+'<br />';

                    }
					$("#cuisine_list_option").css("display","block");
                }else{
					$("#cuisine_list_option").css("display","none");
				}
                $('#cuisine_list').html(htmlstr);				
            }

            })
}

function toggleCheckAllCuisines( state ){
	$("#cuisine_list :checkbox").attr("checked",state);
}

function checkRestoListing(keyword)
{

   if(!emptyCheck(keyword))
   {
      alert('Please enter keyword');
      return false;
   }

   document.location.href = baseUrl + 'search_results.php?keyword='+keyword+'&l=kw';

}

function SearchServicesFilter()
{
   arrayValues = new Array();

   $('input[name*=services]:checked').each(function(){
        arrayValues.push(this.value);
    })

   $('input[name=svc]').val(arrayValues.join());

   ajaxSearchResults();
}

function SearchFacilitiesFilter()
{
   arrayValues = new Array();

   $('input[name*=facilities]:checked').each(function(){
        arrayValues.push(this.value);
    })

   $('input[name=fac]').val(arrayValues.join());

   ajaxSearchResults();
}

function ajaxSearchResults()
{
	formObj = document.Search;

    params = '';
    for(i=0; i<formObj.length; i++)
    {
       fieldName = formObj[i].name;
       fieldValue = formObj[i].value;

       if(fieldName == 'restaurant_name' && fieldValue == txtRestaurantName)
         fieldValue = '';

       if(fieldName.indexOf("cuisines") > -1 && !formObj[i].checked)
         fieldValue = '';

       if(i>0 && fieldValue != '') params+= '&';

      if(fieldValue != '')
       params += fieldName + '=' + fieldValue;
    }

    $.ajax({ url : baseUrl + 'search_results.php',
             type: "GET",
             data: "search_result_ajax=true&" + params,
             success : function(serverData) {

                $("#search-result-listings").html(serverData);

             }
           });
}