﻿var $j = jQuery.noConflict();

function incScript(f) { document.write('<scr'+'ipt type="text/javascript" src="'+f+'"></scr'+'ipt>'); }
incScript('/scripts/jquery.options.js');
incScript('/scripts/jquery.url.js');
incScript('/scripts/validate.js');

function getQuery(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
};


$j(document).ready(function($) {

  //newsletter sign up form validation
  $('#signupform').validate([
    'req,email,Please enter your email address.',
    'email,email,Please enter a valid email address.'
  ]);


  //tabs
  $('#finder-tabs .tab a').click(function() {
    $('#finder-tabs .tab').removeClass('activetab');
    $('#finder-body .finder').hide();
    $(this).parent('.tab').addClass('activetab');
    $('#finder_' + $(this).parent('.tab').attr('id').substring(4)).show();
  });
  //finder ajax forms
  var opts_disabled = { 'disabled': 'disabled', 'class': 'disabled', 'value': '' };
  $('.finder').each(function() {
    var searchbox = $(this);
    var str_table = searchbox.attr('id').substring(7);
    var provsel = $('.provsel', searchbox);
    var monthsel = $('.monthsel', searchbox);
    var divsel = $('.divsel', searchbox);
    var citysel = $('.citysel', searchbox);
    provsel[0].selectedIndex = 0;
    
    $('.monthsel').each(function() {
      //monthsel.html('').removeAttr('disabled');
      monthsel.addOpt('Choose a month...', opts_disabled).addOpt('--------------------', opts_disabled).addOpt('All', { 'value': '' });
      monthsel.addOpt('January', { 'value': '1' }).addOpt('February', { 'value': '2' }).addOpt('March', { 'value': '3' });
      monthsel.addOpt('April', { 'value': '4' }).addOpt('May', { 'value': '5' }).addOpt('June', { 'value': '6' });
      monthsel.addOpt('July', { 'value': '7' }).addOpt('August', { 'value': '8' }).addOpt('September', { 'value': '9' });
      monthsel.addOpt('October', { 'value': '10' }).addOpt('November', { 'value': '11' }).addOpt('December', { 'value': '12' });
      monthsel[0].selectedIndex = 0;
    });
    $('.divsel').each(function() {
      //divsel.html('').removeAttr('disabled');
      divsel.addOpt('Choose a division...', opts_disabled).addOpt('--------------------', opts_disabled);
      divsel.addOpt('All', { 'value': '' }).addOpt('Men', { 'value': 'men' }).addOpt('Women', { 'value': 'women' });
      divsel[0].selectedIndex = 0;
    });

    provsel.change(function(event) {
      citysel.disableOpt('Loading available cities...', opts_disabled);
      $.ajax({
        type: 'GET',
        url: '/scripts/get_cities.ashx?table=' + str_table + '&prov=' + provsel.val(),
        dataType: 'json',
        error: function() {
          citysel.disableOpt('Unable to find any cities for this province', opts_disabled);
        },
        success: function(json) {
          citysel.html('').removeAttr('disabled');
          if (json.cities && (json.cities.length > 0)) {
            citysel.addOpt('Choose a city...', opts_disabled).addOpt('--------------------', opts_disabled).addOpt('All', { 'value': '' });
            citysel[0].selectedIndex = 0;
            var opts = null;
            $j(json.cities).each(function() {
              opts = { value: this.value };
              if (($.args.length > 2) && (this.value == $.args[2])) opts['selected'] = 'selected';
              citysel.addOpt(this.name, opts);
            });
          } else {
            citysel.disableOpt('No cities available...', opts_disabled);
          }
        }
      });

      //var ip = '<!--#echo var="REMOTE_ADDR"-->'
      //if (ip == '82.10.138.78') { alert("STOP! You are viewing this page from an IP address that is not allowed!") };

    });
    $('input:submit', searchbox).click(function () {

      var url = '/' + str_table;
      var prov = provsel.selectedOpt().val();
      if (prov) {
        if (prov == 'us') {
          url += '/us-tournaments';
        } else {
          url += '/' + prov;
          var city = citysel.val();
          if (city) url += '/' + city;
        }
      } else {
        url += '/find';
      }
    url += '.aspx';
      if ($('.search', searchbox).val() != 'finder') {
        var month = monthsel.selectedOpt().val();
        var div = divsel.selectedOpt().val();
        if (prov != 'us') {
          var query = ""
          if (month) { query += 'month=' + month };
          if (div) {
            if (month) { query += '&div=' + div } else { query += 'div=' + div };
          }
          if (query) { url += '?' + query };
        }
      }
      //alert(url);
      document.location.href = url;
      return false;
    });

    //load checks
    if (($.args.length > 1) && (str_table == $.args[0])) {
      $('option', provsel).each(function(i) {
        if ($(this).val() == $.args[1]) {
          provsel[0].selectedIndex = i;
          provsel.change();
          return true;
        }
      });
    }
    if (getQuery('month')!='') {
      $('option', monthsel).each(function(i) {
        if ($(this).val() == getQuery('month')) {
          monthsel[0].selectedIndex = i;
          monthsel.change();
          return true;
        }
      });
    }
    if (getQuery('div')!='') {
      $('option', divsel).each(function(i) {
        if ($(this).val() == getQuery('div')) {
          divsel[0].selectedIndex = i;
          divsel.change();
          return true;
        }
      });
    }
  });
  var str_table = 'tournaments';
  if (($.args.length > 0) && ($.args[0] == 'arenas')) str_table = 'arenas';
  $('#tab_' + str_table + ' a').click();
});