var firstDate = true;

Event.observe(window,"load",initExternal,false);

function initExternal() {
  var x = document.getElementsByClassName('external');
  for (var i=0;i<x.length;i++)
  {
    x[i].onclick = function () {
      return !OpenMyWin(this, 'ext');
    };
  }
}

function posel(narod, jmeno, domena) {
  var securitycode = new Array(109, 97, 105, 108, 116, 111, 58);
  var gfx = '';
  if ((narod!='') && (jmeno!='') && (domena!=''))
  {
    for(i=0;i<securitycode.length;i++) gfx+=String.fromCharCode(securitycode[i]);
    gfx+=(jmeno+String.fromCharCode(64)+domena+String.fromCharCode(46)+narod);
    document.location=gfx;
  }
}
	
function init(narod, jmeno, domena, text, titul) {
  if ((narod!='') && (jmeno!='') && (domena!='') && (text!='')) {
    if (typeof(titul)=="undefined") titul=""; else titul='title:'+titul;
    document.write("<a href=\"javascript:posel('"+narod+"', '"+jmeno+"', '"+domena+"')\""+(titul.length>0?" ":"")+titul+">"+text+"</a>");
  }
}

function e(name, msg, show) {
  if (show == null) {
    show = true;
  }
  if (show) {
    alert(msg);
  }
  $(name).select();
  return false;
}

function ch_length(item,length,msg) {
  if ($(item).value.length<length) {
    alert(msg);
    $(item).select();
    return false;
  }
  return true;
}

function ch_date(show, p) {
  if (p == null) {
    p = '';
  }
  var re = /^(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[0-2])\.((20)?[0-9]{2})$/;
  if (!re.test($(p+'od').value)) {
    return e(p+'od', msg1, show);
  }
  if(!re.test($(p+'do').value))	{
    return e(p+'do', msg1, show);
  }
  dateod=toDate(p+'od');
  datedo=toDate(p+'do');
  if (datedo.getTime()-7200000>=maxdatum.getTime()) {
    return e(p+'od', msg4+' (max: '+maxdatum.getDate()+'.'+(maxdatum.getMonth()+1)+'.'+maxdatum.getFullYear()+')', show);
  }
  if (datedo.getTime()-dateod.getTime()<=0) {
    return e(p+'od', msg2, show);
  }
  if (datedo.getTime()-dateod.getTime()>limit*86400000) {
    return e(p+'od', msg3, show);
  }
  if (dateod.getTime()<now-86400000) {
    return e(p+'od', msg18, show);
  }
  return true;
}

function validate_date_form(show_msg) {
  if (!ch_date(show_msg)) {
    return false;
  }
  if (($('osob').value < 1) || ($('osob').value > maxosob)) {
    return e('osob', msg16, show_msg);
  }
  return true;
}

function validatecheck() {
  if (!ch_date(true, 't')) {
    return false;
  }
  if (($('tosob').value < 1) || ($('tosob').value > maxosob)) {
    return e('tosob', msg16);
  }
  $('overit_btn').value=msgzprac;
  return !send_xmlhttprequest(termin_obsluha, 'GET', '/termin_rpc.php?tod='+$('tod').value+'&tdo='+$('tdo').value+'&tosob='+$('tosob').value);
}

function termin_obsluha(xmlhttp) {
  if ((xmlhttp.readyState == 4) && (xmlhttp.status==200))
  { // aktualizace
    var odpoved = null;
    var str = xmlhttp.responseText;
    try{//next try JSON eval();
      odpoved = parseJSON(str);
    }catch(e){//then try Classic eval();
      odpoved = parseJS(str);
    }
    $('termin').innerHTML = odpoved['content'];
     if (odpoved['alert'] != null && odpoved['alert'].length > 0) {
      alert(odpoved['alert']);
    }
  }
}

function send_xmlhttprequest(obsluha, method, url, content, headers) {
  var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
  if (!xmlhttp) {
    return false;
  }
  xmlhttp.open(method, url);
  xmlhttp.onreadystatechange = function() {
    obsluha(xmlhttp);
  }
  if (headers) {
    for (var key in headers) {
      xmlhttp.setRequestHeader(key, headers[key]);
    }
  }
  xmlhttp.send(content);
  return true;
}

function Clear1() {
  $('od').value='';
  $('do').value='';
  $('osob').value='';
  $('od').focus();
}

var myTWin = window.myTWin;

function OpenMyWin(link,winName) {
  if (winName=='') {
    winName='default';
  }
  var retValue=true;
  if (myTWin!=null && !myTWin.closed) {
    myTWin.focus();
    myTWin.location.href=link.href;
  } else {
    myTWin=window.open(link.href,winName, 'menu=1,navigate=1,width=800,height=600,left=20,top=20,resizable=yes,scrollbars=yes');
    if (myTWin==null || typeof(myTWin)=="undefined")
      retValue=false;
    else {
      link.target=winName;
      myTWin.focus();
    }
  }
  return retValue;
}

function mainWatcher(show_msg) {
  if (validate_date_form(show_msg)) {
    $('indicator').style.visibility = "visible";
    $('overit_btn').className = "button";
    $('overit_btn').value=msgzprac;
    //send AJAX request
    if (!send_xmlhttprequest(handle_response, 'GET', '/termin_rpc.php?form=1&od='+
      $('od').value+'&do='+$('do').value+'&osob='+$('osob').value)) {
      //$('indicator').style.visibility = "hidden";
      return true;
    }
  } else {
    $('overit_btn').value = overit;
    $('overit_btn').className = "button";
  }
  return false;
}

function handle_response(xmlhttp) {
  if ((xmlhttp.readyState == 4) && (xmlhttp.status==200))
  { // aktualizace
    var str=xmlhttp.responseText;
    try{//next try JSON eval();
      odpoved = parseJSON(str);
    }catch(e){//then try Classic eval();
      odpoved = parseJS(str);
    }
    $('overit_btn').value = odpoved['value'];
    $('overit_btn').className = odpoved['class'];
    $('price').innerHTML = odpoved['price'];
    $('count').innerHTML = odpoved['count'];
    $$('.price-info').each(
      function(e) {
        e.style.visibility = 'visible';
      }
      );
    if (odpoved['alert'] != null) {
      alert(odpoved['alert']);
    }
  }
  $('indicator').style.visibility = "hidden";
}

function initMain() {
  $('od').select();
}

function prepareEnd() {
  if (firstDate) {
    $('do').value=$('od').value;
    firstDate = false;
  }
}

function parseJS(str){ 
  eval(str);
  var r = eval(str.split('=')[0].replace(/\s/g,''));
  return r;
}

function parseJSON(str){
  return  eval('('+str+')');
}

function toDate(fieldName) {
  datum=$(fieldName).value.split('.');
  return new Date(datum[2], datum[1]-1, datum[0]);
}

function isemail(t) {
  return !(t.indexOf("@") < 1 || t.indexOf("@") != t.lastIndexOf("@") || t.lastIndexOf(".") < t.lastIndexOf("@")+2  || t.lastIndexOf(".") > (t.length-2) || t.lastIndexOf(".") < (t.length-4));
}