var countdown = new Array();
var countdown2 = new Array();
var secs = new Array();
var secs2 = new Array();
var adotimer;
var saveOff=false, slideOff=false, slideID=0, slideTimer, slideState, slideNext = null;
var modified = false;
var checkids = new Array();
var checkTimer;
if (url === undefined) {
  var url = window.location.href.split("/");
  url[2] = url[2].replace("/admin\//", "");
  url = "http://"+url[2];
}

function kapcsmasol(id) {
  if (id == "sz") {
    $("[name='nev']").val($("#sznev").val());
    if ($("[name=orszag]").val() != $("#szorszag").val()) {
      $("[name=orszag]").val($("#szorszag").val());
      varoslista("orszag", "varos", $("#szvaros").val());
    }
    else 
      $("[name=varos]").val($("#szvaros").val());
    $("[name=irszam]").val($("[name=szirszam]").val());
    $("[name=uhaz]").val($("[name=szuhaz]").val());
  }
  else {
    $("[name='szall["+id+"][nev]']").val($("[name=sznev]").val());
    if ($("[name='szall["+id+"][orszag]']").val() != $("#szorszag").val()) {
      $("[name='szall["+id+"][orszag]']").val($("#szorszag").val());

      varoslista("szall"+id+"orszag", "szall"+id+"varos", $("#szvaros").val());
      document.getElementsByName("szall["+id+"][varos]")[0].innerHTML  = "";
    }
    else {
      $("[name='szall["+id+"][varos]']").val($("#szvaros").val());
    }
    $("[name='szall["+id+"][irszam]']").val($("[name=szirszam]").val());
    $("[name='szall["+id+"][uhaz]']").val($("[name=szuhaz]").val());
  }
  check(true);
}

function varoslista(name, id, sid) {
  var i = document.getElementById(name).selectedIndex;
  var orszag = document.getElementById(name)[i].innerHTML;

  document.getElementById(id).innerHTML = "";
  var el; el = document.createElement("option"); el.innerHTML = "Betöltés.."; document.getElementById(id).appendChild(el);
  $.get(url+'/varoslista/'+orszag, '', function(data){
    document.getElementById(id).innerHTML = ""; data = data.split("\r\n");
    for(var i=0; i<data.length-1; i++) {var el = document.createElement("option");el.innerHTML=data[i];el.value=data[i];document.getElementById(id).appendChild(el);}
    if (sid) 
      $("#"+id).val(sid);
    check(true);
  });
}

function pszcim() {
  document.forms.registration.pszcim.value = 1;
  document.forms.registration.action = document.forms.registration.action+"#szallcimek";
  document.forms.registration.submit();
}

function dszcim(i) {
  document.forms.registration.dszcim.value = i;
  document.forms.registration.action = document.forms.registration.action+"#szallcimek";
  document.forms.registration.submit();
}

function szallcim() {
  if ($("#szall :selected").val() == 0) {
    if ($("#fmod :selected").val() == 1) {
      $("#fmod :selected").attr("selected","");
      $("#fmod option[value='2']").attr("selected", "selected");
    }
    $("#fmod option[value='1']").attr("disabled","disabled");
    $("#fmod option[value='2']").attr("disabled","");
  }
  else {
    if ($("#fmod :selected").val() == 2) {
      $("#fmod :selected").attr("selected","");
      $("#fmod option[value='1']").attr("selected", "selected");
    }
    $("#fmod option[value='2']").attr("disabled","disabled");
    $("#fmod option[value='1']").attr("disabled","");
  }
  $("#szalltd div").hide();
  $("#szinfo"+$("#szall :selected").val()).show();
}

function nextstep(a) {
  if (check(true)) 
    return true;
  else {
    if (typeof a == "undefined")
      alert("Kérjük a piros X-el jelölt mezőket javítsa!");
    return false;
  }
}

function rendelesCimCheck() {
  if (check(true)) {
    document.forms.order.megrendel.value=0;
    document.forms.order.submit();
  }
  else
    alert("Kérjük a piros X-el jelölt mezőket javítsa!");
}

function adoorszag() {
  var i = document.getElementById('ado2orszag').selectedIndex;
  var orszag = document.getElementById('ado2orszag')[i].getAttribute('title');
  
  for(i=1; document.getElementById('szorszag')[i].length != 0; i++) {
    if (document.getElementById('szorszag')[i].innerHTML == orszag) {
      document.getElementById('szorszag').selectedIndex = i;
      break;
    }
  }
  varoslista('szorszag', 'szvaros');
}

function szammasol() {
  if ($("input[name='smsigeny']").attr("checked") == true && $("input[name=smskor[]]:first").val() == "" && $("input[name=sms1[]]:first").val() == "") {
    $("select[name='smsorszag[]']:first :selected").removeAttr("selected");
    $("select[name='smsorszag[]']:first option[value='"+$("input[name=mobilorszag]").val()+"']").attr("selected", "selected");
    $("input[name='smskor[]']:first").val($("input[name='mobilkor']").val());
    $("input[name='sms1[]']:first").val($("input[name='mobil1']").val());
  }
}

function emailmasol() {
  if ($("input[name='hirlevel']").attr("checked") == true && $("input[name=hirlevelemail[]]:first").val() == "") {
    $("input[name='hirlevelemail[]']:first").val($("input[name='email']").val());
  }
}

function tevcheck(id) {
  document.getElementsByName("statch["+id.value+"]")[0].checked = true;
}

function addtev() {
  document.getElementById("addtev").value = 1;
  document.forms.registration.action = document.forms.registration.action+"#tev";
  document.forms.registration.submit();
}
function deltev(id) {
  document.getElementById("deltev").value = id;
  document.forms.registration.action = document.forms.registration.action+"#tev";
  document.forms.registration.submit();
}

function clone(container, cl, x) {
  if (x == null)
    x = "X";
  $("#"+container).append($("#"+container).find("."+cl+":first").clone());
  $("#"+container).find("."+cl+":last").find("input").val("");
  $("#"+container).find("."+cl+":last").find("select").attr("selected", "");
  $("#"+container).find("."+cl+":last").append(" <a href=\"javascript:void(0);\" onclick=\"javascript:$(this).parent().remove()\">"+x+"</a>");
}

function pipa(id) {
  var text = '<img src="'+url+'/img/ok.png" alt="" />';
  if (checkids[id] != 1) {
    $("#"+id).html(text);
  }
  checkids[id] = 1;
}

function hiba(id) {
  var text = '<img src="'+url+'/img/error.png" alt="" />';
  if (checkids[id] != 0)
    $("#"+id).html(text);
  checkids[id] = 0;
}

function adoszamcheck(type, id) {
  check(true);
  var ado = $("#ado1a").val()+$("#ado1b").val()+$("#ado1c").val();
  if ((ado.length < 11) && ($("#ado2").val().length == 0)) {
    for(i=0; document.registration.elements[i]; i++)
      if (document.registration.elements[i].name.substr(0,3) != "ado")
        document.registration.elements[i].disabled=true;
    return false;
  }

  if (id == 1) {
    $("#registration_check").css("left", $("#adotd").offset().left+"px");
    $("#registration_check").css("top", $("#adotd").offset().top+"px");
    $("#registration_check").css("width", $("#adotd").css("width"));
    $("#registration_check").css("display", "block");
    $.get(url+'/adoszam/'+$("#ado1a").val()+'/'+$("#ado2orszag").val()+$("#ado2").val(),
    '', function(data){
      $("#registration_check").css("display", "none");
      check(true);
      if (data == "ok") {
        $("#adofoglalt").val(0);
        pipa("szado_check");
        for(i=0; document.registration.elements[i]; i++)
          document.registration.elements[i].disabled=false;
      }
      else {
        // belepos layer
        if (type == null) {
          $("#loginszam").val(data);
          login();
        }
        else {
          alert("Ez az adószám már foglalt!");
          $("#adofoglalt").val(1);
          hiba("szado_check");
        }
        for(i=0; document.registration.elements[i]; i++)
          if (document.registration.elements[i].name.substr(0,3) != "ado")
            document.registration.elements[i].disabled=true;
      }
    });
  }
  else {
    clearTimeout(adotimer);
    adotimer = setTimeout("adoszamcheck("+type+", 1)", 1000);
  }
}

function go(id) {
  document.forms.registration.nextpage.value = id;
  if (id < checkid || check(true))
    document.forms.registration.submit();
  else if (id == checkid+1)
    alert("Kérjük a piros X-el jelölt mezőket javítsa!");
}

function go2(id) {
  if (modified)
    var check = confirm("Biztos elhagyja az oldalt? A nem mentett módosítások elvesznek!");
  if (check || !modified) {
    document.forms.registration.nextpage.value= id;
    document.forms.registration.submit();
  }
}

function login() {
  if ($("#fade").length == 0) {
    var id = document.createElement("a");
    id.setAttribute("id", "fade");
    id.setAttribute("href", "javascript:clear()");
    document.body.appendChild(id);
  }
  document.body.style.overflow = "hidden";
  $("#fade").css("display", "block");
  $("#fade").css("width", "6000px");
  $("#fade").css("height", "6000px");
  $("#layer").css("display", "block");
}

function clear() {
  $("#fade").css("display", "none");
  $("#fade").css("width", "0px");
  $("#fade").css("height", "0px");
  $("#layer").css("display", "none");
  document.body.style.overflow = "auto";
}

function spam() {
  var kf = document.getElementById("messageform");
  var s = document.createElement("input");
  s.setAttribute("type","hidden");
  s.setAttribute("name","javascr");
  s.setAttribute("value","van");
  kf.appendChild(s);
}

function getcountdowns() {
  var ids = document.getElementsByTagName("span");
  for(var i=0; ids[i] != undefined; i++) {
    if (ids[i].className == "countdown") {
      countdown[countdown.length] = i;
      secs[secs.length] = ids[i].innerHTML;
    }
    else if (ids[i].className == "countdown2") {
      countdown2[countdown2.length] = i;
      secs2[secs2.length] = ids[i].innerHTML;
    }
  }
}

function countdowns(type) {
  var ids = document.getElementsByTagName("span");
  var id, hour, min, sec;
  if (typeof type == undefined)
    var type = 1;
  if (type == 1)
    var ar = countdown;
  else
    var ar = countdown2;
  for(var i=0; ar[i] != null; i++) {
    if (type == 1) {
      id = countdown[i];
      sec = secs[i];
    }
    else {
      id = countdown2[i];
      sec = secs2[i];
    }
    if (sec == -1)
      continue;
    if (type == 1) secs[i]--;
    else secs2[i]--;
    hour = Math.floor(sec / 3600);
    sec -= hour * 3600;
    min = Math.floor(sec / 60);
    sec -= min * 60;
    if (hour < 10)
      hour = "0"+hour;
    if (min < 10)
      min = "0"+min;
    if (sec < 10)
      sec = "0"+sec;
    if (type == 1) {
      day = Math.floor(hour / 24);
      hour -= day*24;
      if (hour < 10)
        hour = "0"+hour;
      if (day != 0)
        ids[id].innerHTML = day+" nap, "+hour+":"+min+":"+sec;
      else 
        ids[id].innerHTML = hour+":"+min+":"+sec;
      ids[id].style.visibility = "visible";
    }
    else {
      day = Math.floor(hour / 24);
      if (day > 0) {
        if (day >= 10)
          $("#c"+(i+1)+"hm").css("font-size", "30px");
        $("#c"+(i+1)+"hm").html(day+" nap");
        $("#c"+(i+1)+"s").html(null);
      }
      else if (hour == 0 && min == 0 && sec == 0) {
        $("#c"+(i+1)+"hm").css("font-size", "32px");
        $("#c"+(i+1)+"hm").html("lejárt!");
        $("#c"+(i+1)+"s").html(null);
      }
      else {
        $("#c"+(i+1)+"hm").html(hour+":"+min);
        $("#c"+(i+1)+"s").html(sec);  
      }
    }
  }
  setTimeout("countdowns("+type+");", 1000);
}

function savecd(pid) {
  if (saveOff === true) {
    setTimeout("savecd("+pid+")", 5000);
    return;
  }
  id = $("#save"+(pid+2));
  if (id.length == 0) {
    $("#saves-inner").animate({ 
      marginLeft: "0px"
    }, 1000, null, function callback() { setTimeout("savecd("+pid+");", 5000); });
    pid = 0;
  }
  else {
    pid++;
    $("#saves-inner").animate({ 
      marginLeft: "-=221px"
    }, 1000, null, function callback() { savetimer=setTimeout("savecd("+pid+");", 5000); });
  }
}
function savestop() {
  saveOff=true;
}
function savestart() {
  saveOff=false;
}

function slidebox() {
  if (slideOff === true) {
    setTimeout("slidebox()", 5000);
    return;
  }
  var next;
  if (slideNext === null || slideNext == slideID+1)
    id = $("#sd"+(slideID+1));
  else {
    id = $("#sd"+slideNext);
  }
  if (id.length == 0)
    next = 1;
  else {
    if (slideNext === null)
      next = slideID+1;
    else
      next = slideNext;
  }
  if (next == slideID)
    return;
  slideNext=null;

  slideState = 2;
  $("#sd"+(slideID)+"t").animate({ marginTop: "226px" }, 600,
    function callback() {
      $("#sd"+(slideID)).fadeOut(1000, function callback() { slideboxhide(next); });
    }
  );
}

function slideboxhide(next) {
  slideState = 2;
  if (slideNext === null)
    slideID=next; 
  else
    slideID=slideNext;
  slideboxshow(); 
}

function slideboxshowed() {
  slideState = 1;
  if (slideNext === null || slideNext==slideID) {
    slideTimer = setTimeout("slidebox()", 5000); 
    slideNext=null;
  }
  else
    slidebox();
}

function slideboxshow() {
  $("#sd"+(slideID)).fadeIn(1000,
    function callback() {
      $("#sd"+(slideID)+"t").animate({
       marginTop: "145px"
      }, 600, function callback() { slideboxshowed(); });
    }
  );
}

function slidestart() {
  slideOff=false;
}

function slidestop() {
  slideOff=true;
}

function slidenext(id) {
  clearTimeout(slideTimer);
  slideOff = false;
  if (slideState == 1) {
    slideNext=id;
    slidebox();
  }
  else if (slideState == 2) {
    slideNext = id;
  }
  else if (slideState == 3) {
    slideNext = id;
  }
}

function showmsg() {
  var val = $("#msg").html();
  if (val != null)
    alert(val);
}

function gallery(t) {
  window.open(url+"/galeria/"+t,'artvirag','width=700,height=600,resizable=no,toolbar=no,menubar=no,scrollbars=no');
}

function help(t) {
  window.open(url+"/"+t,'artvirag','width=700,height=600,resizable=no,toolbar=no,menubar=no,scrollbars=no');
}

function runCufon() {
  Cufon.replace('#nosave h2,#nosave h3', {
    textShadow: '2px 2px #000'
  });
  Cufon.replace('#saves h2', {
    textShadow: '1.5px 1.5px #444'
  });
  Cufon.replace("#saves .buttons a", {lineHeight: '0.1'});
  $("#nosave h2,#nosave h3,#save h2,#saves .buttons a").css("visibility", "visible");
}

window.onload = function() {
  if (typeof check == "function") {
    var re = check(true, 1);
    $('input[type=text]').mouseleave(function() {
      var re = check(true, 1);
    });
    $('input[type=text]').mouseover(function() {
      var re = check(true, 1);
    });
  }

  getcountdowns();
  setTimeout("countdowns(1);", 1000);
  setTimeout("countdowns(2);", 1000);
  setTimeout("savecd(0);", 5000);
  slideID = 1;
  slideState = 3;
  slideboxshow();
  showmsg();
  
  if (document.getElementsByTagName) {
    var i, a;
    a = document.getElementsByTagName('a');
    for(i=0; a[i] != undefined; i++) {
      if (a[i].href && a[i].rel == "external") 
        a[i].target = "_blank";
    }
  }
}

function number_format (number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}
var sms=0;
function pluszsms() {
  if (sms==0)
    sms = $("[name='smsorszag[]']").length;
  sms++;
  $("#smsmobilok").append("<div id=\"smsmobil"+sms+"\">+<select name=\"smsorszag[]\"></select> <input type=\"text\" name=\"smskor[]\" onkeyup=\"check();\" class=\"small\" /> <input type=\"text\" name=\"sms1[]\" onkeyup=\"check();\" /> (<a href=\"javascript:minuszsms("+sms+");\">x</a>)</div>");
  $("[name='smsorszag[]']:first > option").clone().appendTo("[name='smsorszag[]']:last");
}
function minuszsms(id) {
  $("#smsmobil"+id).remove();
  check();
}
var hirlevel=0;
function pluszhirlevel() {
  if (hirlevel==0) 
    hirlevel = $("[name='hirlevelemail[]']").length;
  hirlevel++;
  $("#hirlevelemailek").append("<div id=\"hirlevel"+hirlevel+"\"><input type=\"text\" name=\"hirlevelemail[]\" onkeyup=\"check();\" /> (<a href=\"javascript:minuszhirlevel("+hirlevel+")\">x</a>)</div>");
}
function minuszhirlevel(id) {
  $("#hirlevel"+id).remove();
  check();
}
function favorite(a, id) {
  $.get("/kedvenc/"+id, '', function(data) {
    data = data.split("\r\n");
    $(a).attr("title", data[0]);
    $(a).find("img").attr("src", data[1]);
  });
}
function sendForm(f) {
  $("form[name="+f+"]").submit();
}
