String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function checkForm(frm){
  var reqInputs = $('form input:text.req');
  var reqText = $('form textarea.req');
  if(reqInputs.length > 0){
    for(var i=0;i<reqInputs.length;i++){
      if((reqInputs[i].value).trim() == ""){
        alert("Vyplňte prosím povinná pole.");
        return false;
      }
    }
  }
  if(reqText.length > 0){
    for(var i=0;i<reqText.length;i++){
      if((reqText[i].value).trim() == ""){
        alert("Vyplňte prosím povinná pole.");
        return false;
      }
    }
  }
  return true;
}

function addCommentCheck(obj){
  return checkForm(obj);
}

function countDown(id, obj){
  var ret = $.ajax({
	url: "/rdajax.php?what=countdown&id="+id,
	  async: false
	  }).responseText;
  $('#'+obj).html(ret);
  setTimeout('countDown("'+id+'","'+obj+'")',1000);
}

$(document).ready(function (){
  $('a.backlink').click(function(){
    var ahref = $(this)[0].href;
    var ret = $.ajax({
	url: "/rdajax.php?what=backlink&href="+ahref,
	  async: false
	  }).responseText;
    window.open(ahref);
    return false;
  });
  
  if($('#registracni-form')){
    $('#registracni-form').submit(function(){
      var msg = "";
      var pass = true;
      $('.error').remove();
      if($('#login').val() == ""){
	msg = "Vyplňte prosím přihlašovací jméno.<br/>";
	$('#login').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
      if($('#email').val() == ""){
	msg = "Vyplňte prosím email.<br/>";
	$('#email').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
      if($('#password').val() == ""){
	msg = "Vyplňte prosím heslo.<br/>";
	$('#password').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
      if($('#password').val().length < 5){
	msg = "Heslo musí mít alespoň 5 znaků.<br/>";
	$('#password').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
      if($('#password').val() != $('#password_again').val() ){
	msg = "Hesla se neshodují.<br/>";
	$('#password_again').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
/*      if($('#phone').val() == ""){
	msg = "Vyplňte prosím telefon.<br/>";
	$('#phone').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
*/
      if($('#name').val() == ""){
	msg = "Vyplňte prosím Jméno a příjmení.<br/>";
	$('#name').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
      if($('#agreement').attr('checked') != true){
	msg = "Zaškrtněte prosím souhlas s pravidly.<br/>";
	$('#agreement').after("<div class='error'>"+msg+"</div>");
	pass = false;
      }
      if(pass == false){
	return false;
      }
      return true;
    });
  }
  $('a[rel=external]').click(function(){window.open(this.href);return false});
});

function vote(aid,id){
	var ret = $.ajax({
	      url: "/rdajax.php?what=vote&aid="+aid+"&vid="+id,
		async: false
		}).responseText;
	if(ret == "1"){
		alert("Děkujeme za Váš hlas.");
	//	location.reload();
	}else{
		alert("Nelze hlasovat vícekrát.");
	}
	return true;
}


function kvizCheck(){
  var arr = $("input[name^='odpoved']");
  return true;
}
