String.prototype.trim = function() { return this.replace(/^[\s\u3000]+|[\s\u3000]+$/g, ''); }
var script_url = 'http://playkey.ru';

function checkValue(element, hint, valrule)
{
  if (element.value.trim() == '') {element.value = hint; element.style.color=''; element.className = 'form';}
  else
    if (valrule != null)
    {
      if (validate(valrule, element.value))
        element.className = 'form';
      else
        element.className = 'form_invalid';
    }
}

function hideHint(element, hint)
{
  if (element.value.trim() == hint) {element.value = ''; element.style.color = '#000'}
}

function validate(rule, str) {
  var rules = new Array();
  
  rules['integer']       = /^\d{1,}$/;
  rules['something']     = /^.{1,}$/;
  rules['anything']      = /^.{0,}$/;
  rules['email']         = /^.*@.*\.\w{2,4}$/;
  rules['string']        = /^\w{1,}/;
  rules['sql_date']      = /^\d{4}-\d{2}-\d{2}$/;
  rules['empty']         = /^$/;
  rules['url']           = /^\w+:\/\//;
  rules['ip']            = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
  rules['host']          = /^[\w-]+(?:\.[\w-]+)+$/;
  rules['integer_list']  = /^\d+(?:\s*?,\s*?\d+)?$/;
  rules['phone']         = /^\+\d{1,2}.\d+$/;
  rules['float']         = /^\d{1,}(\.\d{1,})?$/;
  if (str.match(rules[rule])) return true
  else return false;
}

function showhidden(elementid)
{
  var element = document.getElementById(elementid);
  element.style.display = '';
}

$(document).ready(function(){

  // Games & goods blocks highlight
	$('.gameitem').hover(
	  function () {
	    $(this).css({'background-image': 'url('+script_url+'/css/images/gamebutton_green2.gif)', 'cursor':'pointer'});
	  }, 
	  function () {
	    $(this).css({'background-image': 'url('+script_url+'/css/images/gamebutton2.gif)', 'cursor':'default'});
	  }
	);
	
  $('#board tr').hover(
    function () {
      $(this).css({'background-color' : '#DAEEBD', 'cursor':'pointer'});
    }, 
    function () {
      $(this).css({'background-color': '', 'cursor':'default'});
    }
  );	
	
});

function showmenu(id)
{
  if (id == 1) 
  {
    $('#gamelist').toggle();
    $('#goodlist').hide();
  } 
  else if (id == 2)
  {
    $('#gamelist').hide();
    $('#goodlist').show();
  }
  else if (id == 3)
  {
    $('#gamelist').hide();
    $('#goodlist').hide();
  }
}

function choosegame(game)
{
  $('#gamelist').hide();
  $('#gameselect').html(games[game].menutitle);
  $('#goodlist li').hide();
  $('#goodlist li[@id^=g'+game+'-id]').show();
  firstgood_id = $('#goodlist li[@id^=g'+game+'-id]:first').attr('id');
  firstgood = firstgood_id.substr(firstgood_id.indexOf('-id')+3,firstgood_id.length);
  $('#goodselect').attr('href', 'javascript:showmenu(2);');
  $('#goodselect, #buybutton').addClass('active');
  if (firstgood) choosegood(firstgood);
}

function choosegood(good) 
{
  $('#goodlist').hide();
  $('#goodselect').html(goods[good].menutitle);
  $('#buybutton').attr('href', goods[good].url);
}

function choose_cur(currency, value, id, good_id)
{  
  $('#price').html(value);
  $('#buylink').attr('href', 'http://playkey.ru/payment/?good_id='+good_id+'&id_d='+id+'&currency='+currency.toUpperCase()+'&failpage='+location.href);
  $('#currencies img').css('border','2px solid #fff');
  $('#cur_'+currency).css('border','2px solid #6FAC13');
}

function filtergoods(region)
{
  borderStyle = '2px solid #0083b9';
  $('.gameitem').hide();
  $('.countryselect span[@id^=zone]').css('border-bottom', '0px');
  if (region == 3)
  {
    $('.gameitem[@id^=zone3-good]').show();
    $('#zone3title').css('border-bottom', borderStyle);
    $('#regiontitle').text('Русские версии');
    
  }
  else if (region == 2)
  {
    $('.gameitem[@id^=zone2-good]').show();
    $('#zone2title').css('border-bottom', borderStyle);
    $('#regiontitle').text('Американские версии');
  }
  else if (region == 1)
  {
    $('.gameitem[@id^=zone1-good]').show();
    $('#zone1title').css('border-bottom', borderStyle);
    $('#regiontitle').text('Европейские версии');
  }
}

function checkRegion() 
{
  var selectedGame = $('#userad-game-select').val();
  if (!geogames[selectedGame]) 
  {
    $('#userad-region-select').val(0);
    $('#userad-region-select').attr("disabled","disabled");
  }
  else $('#userad-region-select').removeAttr("disabled");
}

