﻿function sleep(ms) {
    var dt = new Date();
    dt.setTime(dt.getTime() + ms);
    while (new Date().getTime() < dt.getTime());
}

function addOption (oListbox, text, value, isDefaultSelected, isSelected){
  var oOption = document.createElement('option');
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute('value', value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);
}

function CreateRequest(){
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
        }
      catch (e)
        {
        try
          {
          xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
          }
        catch (e)
          {
          alert('Your browser does not support AJAX!');
          return null;
          }
        }
      }
    return xmlHttp;
}

function fillCountries(country, state, city, only){
    var Request = CreateRequest();
    Request.onreadystatechange = function(){
        if (Request.readyState == 4){
            fillOptionsFromString(country, Request.responseText, ''); //Country
            if (!only){
                fillStatesFromCountry(country, state, city, false);
            }
            if (country.onFilled != null){
                country.onFilled();
            }
        }    
    }
    Request.open('GET','ajax_get.aspx?get=countries',true);
    Request.send(null);
}

function fillStatesFromCountry(country, state, city, only){
    var Request = CreateRequest();
    Request.onreadystatechange = function(){
        if (Request.readyState == 4){
            fillOptionsFromString(state, Request.responseText, ''); //Province/State
            if (!only){
                fillCities(country, state, city)
            }
            if (state.onFilled != null){
                state.onFilled();
            }
        }    
    }
    Request.open('GET','ajax_get.aspx?get=states&from=country&id=' + new String(country.options[country.selectedIndex].value),true);
    Request.send(null);
}

function fillCities(country, state, city){
    var Request = CreateRequest();
    Request.onreadystatechange = function(){
        if (Request.readyState == 4){
            fillOptionsFromString(city, Request.responseText, 'All');
            if (city.onFilled != null){
                city.onFilled();
            }
        }    
    }
    var strGet = new String();
    strGet = 'ajax_get.aspx?get=cities&from=state&id=' + new String(state.options[state.selectedIndex].value)
    Request.open('GET',strGet,true);
    Request.send(null);
}

function fillVenueTypes(venue, insertDefault){
    var Request = CreateRequest();
    Request.onreadystatechange = function() {
        if (Request.readyState == 4) {
            if (insertDefault == true) {
                fillOptionsFromString(venue, Request.responseText, 'All');
            }
            else {
                fillOptionsFromString(venue, Request.responseText, '');
            }
            if (venue.onFilled != null) {
                venue.onFilled();
            }
        }
    }    
    Request.open('GET','ajax_get.aspx?get=venue_types',true);
    Request.send(null);
}

function fillMusic(music) {
    var Request = CreateRequest();
    Request.onreadystatechange = function() {
        if (Request.readyState == 4) {
            fillOptionsFromString(music, Request.responseText, 'All');
            if (music.onFilled != null) {
                music.onFilled();
            }
        }
    }    
    Request.open('GET','ajax_get.aspx?get=music_types',true);
    Request.send(null);
}

function fillOptionsFromString(select, input, defaultName){
    var strResults = input.split(';');
    var i;
    var strName = new String();
    var strID = new String();
    select.options.length = 0;
    if (defaultName.length > 0){
        addOption(select, defaultName, '', true, false);
    }
    for (i = 0; i < strResults.length; i++) {
        strName = strResults[i].substring(0, strResults[i].lastIndexOf(' ', strResults[i].length));
        strID = strResults[i].substring(strResults[i].lastIndexOf(' ', strResults[i].length) + 1, strResults[i].length);
        addOption(select, strName, strID, false, false);
    }
}

function fillPrice(price){
    var i;
    price.options.length = 0;
    addOption(price, "All", '0', true, false);
    for (i=1; i<=5; i++){
        addOption(price, verbalizePrice(i), i, false, false);
    }
}

function verbalizePrice(price){
    switch (price){
        case 0:{
            return 'Free';
            break;
        }
        case 1:{
            return 'Very Economical';
            break;
        }
        case 2:{
            return 'Economical';
            break;
        }
        
        case 3:{
            return 'Moderate';
            break;
        }
        
        case 4:{
            return 'Moderately Posh';
            break;
        }
        
        case 5:{
            return 'Posh';
            break;
        }    
    }
}

function SearchByOptions(country, state, city, venue, price, age){
    var strRedirect = new String('clubs.aspx?');
    
    if (city.selectedIndex != 0){
        strRedirect += 'city=' + city.options[city.selectedIndex].value + '&';
    }
    else 
    {
        strRedirect += 'state=' + state.options[state.selectedIndex].value + '&';
    }
    
    if (venue.selectedIndex != 0){
        strRedirect += 'type=' + venue.options[venue.selectedIndex].value + '&';
    }
    
    if (price.selectedIndex != 0){
        strRedirect += 'price=' + price.options[price.selectedIndex].value + '&';
    }

    if (age.selectedIndex != 0) {
        strRedirect += 'age=' + age.options[age.selectedIndex].value + '&';
    }
    strRedirect = strRedirect.substring(0, strRedirect.length-1);
    document.location.href = strRedirect;
}

function SearchByName(name) {
    if (name.value.length > 0 && name.value != 'club name')
    {
        var strRedirect = new String('clubs.aspx?');
        strRedirect += 'name=' + name.value;
        document.location.href = strRedirect;
    }
}

function AdvUserSearchByOptions(gender, age_from, age_to, country, state, city, preference, going_out, places, alcohol, smoking, music, scale, dress){
    var strRedirect = new String('user_search.aspx?');

    if (gender.selectedIndex != 0){
        strRedirect += 'gender=' + gender.options[gender.selectedIndex].value + '&';
    }
    if (age_from.value.length > 0){
        if (IsIntegerInRange(age_from.value))
        {
            strRedirect += 'age_from=' + age_from.value + '&';
        }
        else
        {
            alert('Invalid Age1');
            return;
        }
    }
    if (age_to.value.length > 0){
        if (IsIntegerInRange(age_to.value))
        {
            strRedirect += 'age_to=' + age_to.value + '&';
        }
        else
        {
            alert('Invalid Age2');
            return;
        }
    }
    
    if (city.selectedIndex != 0){
        strRedirect += 'city=' + city.options[city.selectedIndex].value + '&';
    }
    else 
    {
        strRedirect += 'state=' + state.options[state.selectedIndex].value + '&';
    }

    if (preference.selectedIndex != 0){
        strRedirect += 'pref=' + preference.options[preference.selectedIndex].value + '&';
    }    
    if (going_out.selectedIndex != 0){
        strRedirect += 'going_out=' + going_out.options[going_out.selectedIndex].value + '&';
    }
    if (places.selectedIndex != 0){
        strRedirect += 'places=' + places.options[places.selectedIndex].value + '&';
    }    
    if (alcohol.selectedIndex != 0){
        strRedirect += 'alcohol=' + alcohol.options[alcohol.selectedIndex].value + '&';
    }    
    /*if (smoking.selectedIndex != 0){
        strRedirect += 'smoking=' + smoking.options[smoking.selectedIndex].value + '&';
    } */   
    /*if (music.selectedIndex != 0){
        strRedirect += 'music=' + music.options[music.selectedIndex].value + '&';
    } */   
    /*if (scale.selectedIndex != 0){
        strRedirect += 'scale=' + scale.options[scale.selectedIndex].value + '&';
    }    
    if (dress.selectedIndex != 0){
        strRedirect += 'dress=' + dress.options[dress.selectedIndex].value + '&';
    }*/

    strRedirect = strRedirect.substring(0, strRedirect.length-1);
    document.location.href = strRedirect;
}

function AdvClubSearchByOptions(country, state, city, type, price, age, music, dress, p_own, p_dist, p_price, w_walk, w_vip, w_book) {
    var strRedirect = new String('clubs.aspx?');
    if (city.selectedIndex != 0){
        strRedirect += 'city=' + city.options[city.selectedIndex].value + '&';
    }
    else 
    {
        strRedirect += 'state=' + state.options[state.selectedIndex].value + '&';
    }

    if (type.selectedIndex != 0) {
        strRedirect += 'type=' + type.options[type.selectedIndex].value + '&';
    }
    if (price.selectedIndex != 0) {
        strRedirect += 'price=' + price.options[price.selectedIndex].value + '&';
    }
    if (age.selectedIndex != 0) {
        strRedirect += 'age=' + age.options[age.selectedIndex].value + '&';
    }
    if (music.selectedIndex != 0) {
        strRedirect += 'music=' + music.options[music.selectedIndex].value + '&';
    }
    if (dress.selectedIndex != 0) {
        strRedirect += 'dress=' + dress.options[dress.selectedIndex].value + '&';
    }
    if (p_own.selectedIndex != 0) {
        strRedirect += 'p_own=' + p_own.options[p_own.selectedIndex].value + '&';
    }
    if (p_dist.selectedIndex != 0) {
        strRedirect += 'p_dist=' + p_dist.options[p_dist.selectedIndex].value + '&';
    }
    if (p_price.selectedIndex != 0) {
        strRedirect += 'p_price=' + p_price.options[p_price.selectedIndex].value + '&';
    }
    if (w_walk.selectedIndex != 0) {
        strRedirect += 'w_walk=' + w_walk.options[w_walk.selectedIndex].value + '&';
    }
    if (w_vip.selectedIndex != 0) {
        strRedirect += 'w_vip=' + w_vip.options[w_vip.selectedIndex].value + '&';
    }
    if (w_book.selectedIndex != 0) {
        strRedirect += 'w_book=' + w_book.options[w_book.selectedIndex].value + '&';
    }
            
    strRedirect = strRedirect.substring(0, strRedirect.length - 1);
    document.location.href = strRedirect;
}

function IsIntegerInRange(s) // 1 - 100
{

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!((c >= "0") && (c <= "9"))) return false;
      }

      var num = parseInt(s);
      if ((num < 1) || (num > 100))
      {
         return false;
      }
      
      return true;
}

function setToValue(select, value)
{
    var n = select.options.length;
    var i;
    for (i = 0; i < n; i++) {
        if (select.options[i].value == value) {
            select.selectedIndex = i;
            return;
        }
    }
}

function setLocationValues(selCountry, selState, selCity, nCountry, nState, nCity) {
    //Magic. Do not touch.
    //alert(navigator.userAgent);

    selCountry.onFilled = function() {

        selState.onFilled = function() {

            selCity.onFilled = function() {

                //We need to do nothing the first two times that we come here. That is,
                //after pageload, and after the country has been selected.
                //After the state has been selected, however..
                if (this.secondTime == null) {
                    this.secondTime = true;
                    if (navigator.userAgent.indexOf('MSIEsdf') < 0) 
                    {
                        return;
                    }
                }
                if (this.secondTime == true) {
                    this.secondTime = false;
                    if (navigator.userAgent.indexOf('MSIEsdf') < 0) 
                    {
                        return;
                    }
                }

                //...we kill secondTime and set the value for city.
                this.secondTime = null;

                this.onFilled = null;

                setToValue(this, nCity);
            }
            //If we come here right after page loaded, secondTime is null and is set to true
            //If we come here for the second time, after the country dropdown has been reloaded...
            if (this.secondTime == null) {
                this.secondTime = true;
                if (navigator.userAgent.indexOf('MSIEsdf') < 0) 
                {
                    return;
                }
            }
            //...secondTime is killed, as well as is onFilled (we never come back)
            this.secondTime = null;
            //and we set the value for state. And -  guess what? - reload the cities list.
            setToValue(this, nState);

            this.onchange();
            this.onFilled = null;
        }
        //List of countries is filled; we select the country we need and reload state/city list
        setToValue(this, nCountry);
        this.onchange();
        this.onFilled = null;
    }
}

function setToValueOnFilled(select, value){
    select.onFilled = function(){
        this.onFilled = null;
        setToValue(this, value);
    }
}
function RemoveSearchText() {
    var txtControl = 'txtClubSearch';
    if (document.getElementById(txtControl)) {
        var txtValue = document.getElementById(txtControl).value;
        if (txtValue == 'club name') {
            document.getElementById(txtControl).value = '';
            document.getElementById(txtControl).className = 'inputFonSearch login_text_black';
        }
    }
}
function AddSearchText() {
    var txtControl = 'txtClubSearch';
    if (document.getElementById(txtControl)) {
        var txtValue = document.getElementById(txtControl).value;
        if (txtValue == '' || txtValue == 'club name') {
            document.getElementById(txtControl).value = 'club name';
            document.getElementById(txtControl).className = 'inputFonSearch login_text_gray';
        }
    }
}
