
//------------------------------------------------------------------------------
// kontrola tvaru emailove adresy
function wrong_mail_address(adresa){
  var re = /^[_a-zA-Z0-9\.\-]+@[_a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,4}$/;
  return re.test(adresa) == 0;
}

//------------------------------------------------------------------------------
// funkce vraci element s danym ID
function getObj(id){
   if(Boolean(document.getElementById))
      return document.getElementById(id);
   else if(Boolean(document.all))
      return eval('document.all.'+id);
   else
      return eval('document.'+id);
}


//------------------------------------------------------------------------------
// prepinani obrazku
function switchPhoto(link){
    var main = getObj('main-pic');
    main.src = link.href;
    return false;
}


//------------------------------------------------------------------------------
// zobrazeni objednavkoveho fomrulare
function showForm(click){
    if(click) var vis = true;
    else try{ vis = show } catch(e){}

    if(vis){
        getObj('order-form').style.display = 'block';
        getObj('order').style.display = 'none';
        getObj('book-price').style.display = 'none';
        getObj('book-img').style.display = 'none';
    }
}

function showSub(id){
    var subs = getElementsByClassName('uroven2', 'ul');
    for(var i=0; i < subs.length; i++)
        subs[i].style.display = 'none';
    if(id) getObj('sub'+id).style.display = 'block';
}


function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}


function insertSmile(code){
  clearField(getObj('textarea'), 'text příspěvku');
  getObj('textarea').value += " " + code + " ";
}


function addFileField(){
    //$('file_fields').innerHTML += '<p>&nbsp; <input type="file" name="attachment_' + uid() + '" /></p>';
    var newP = document.createElement('p');
    var newInp = document.createElement('input');
    newInp.setAttribute('type', 'file');
    newInp.setAttribute('name', 'attachment[]');
    newP.innerHTML = '&nbsp;';
    newP.appendChild(newInp);
    $('file_fields').appendChild(newP);
}

function showLoginForm(href){
    var location = href;
    buildModalWindow('Přihlášení',
                     '<span id="msg"></span>' + 
                     '<p>Tato akce vyžaduje autorizaci. Přihlašte se prosím!</p>' +
                     '<form id="login-form" method="post" action="" id="login-form" >' +
                     '<p>E-mail: <input id="login" name="login" /></p>' +
                     '<p>Heslo:<input id="password" name="password" type="password"/></p>' +
                     '<p><input name="login_submit" type="submit" value="Přihlásit se" onclick="authorize(); return false;" /></p>' +
                     '<input type="hidden" id="location" name="location" value="'+location+'"' +
                     '</form>',
                     300);
}

function authorize(){
    var pswd = $('password').value;
    var login = $('login').value;
    var location = $('location').value;

    showLoader();
    send_xmlhttprequest(resolveAuth, 'GET', '/ajax/authorize.php?login='+login+'&pswd_hash='+SHA1(pswd)+'&location='+location);
}

function resolveAuth(xmlhttp){
    if(xmlhttp.readyState == 4) {
        hideLoader();
        eval(xmlhttp.responseText);

        if(result == 'OK') location.href=newLocation;
        else $('msg').innerHTML = '<p id="error">'+result+'</p>';
    }
}


function centerWindow(element) {
     if(element != null) {
          if(typeof window.innerHeight != 'undefined') {
              element.style.top =
                    Math.round(document.viewport.getScrollOffsets().top +
                    ((window.innerHeight - element.getHeight()))/2)+'px';
               element.style.left =
                    Math.round(document.viewport.getScrollOffsets().left +
                    ((window.innerWidth - element.getWidth()))/2)+'px';
          } else {
               element.style.top =
                    Math.round(document.body.scrollTop +
                    (($$('body')[0].clientHeight -element.getHeight()))/2)+'px';
               element.style.left =
                    Math.round(document.body.scrollLeft +
                    (($$('body')[0].clientWidth - element.getWidth()))/2)+'px';
          }
     }
}


function showLoader(){
    loader = document.createElement('div');
    loader.setAttribute('id', 'loader');
    document.body.appendChild(loader);
}

function hideLoader(){
    $('loader').remove();
}


function buildModalWindow(head, text, width, height){
    modal = Builder.node('div', {id: 'modal'});
    modal.style.display = 'none';
    if(width != null) modal.style.width = width + 'px';
    if(height != null) modal.style.height = height + 'px';

    modal.closer = Builder.node('a', {id: 'modalClose'});
    modal.appendChild(modal.closer);
    modal.closer.onclick = closeModalWindow;

    modal.header = Builder.node('div', {id: 'modalHeader'});
    modal.appendChild(modal.header);
    modal.header.innerHTML = head;

    modal.content = Builder.node('div', {id: 'modalContent'});
    modal.appendChild(modal.content);
    modal.content.innerHTML = text;

    document.body.appendChild(modal);
    centerWindow(modal);

    overlay = Builder.node('div', {id: 'ov'});
    overlay.style.display = 'none';
    document.body.appendChild(overlay);

    overlay.appear({duration: 0.5, from: 0, to: 0.3});
    modal.appear({duration: 0.5, from: 0, to: 1});

    return modal;
}

function closeModalWindow(){
    $('modal').remove();
    $('ov').remove();
}


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 openAttachmentBox(a){
    Effect.SlideDown('attachments',{duration: 0.3});
    a.innerHTML = 'skrýt přílohy...';
    a.onclick = function(){closeAttachmentBox(this); return false;};
}

function closeAttachmentBox(a){
    Effect.SlideUp('attachments',{duration: 0.3});
    a.innerHTML = 'přidat přílohu k příspěvku...';
    a.onclick = function(){openAttachmentBox(this); return false;};
}

function showRegionInfo(id){
    $('region-'+id).style.display = 'block';
}

//====================== ovladani FAQ ==============================

function showAnswer(link, answerID){
  var answer = getObj(answerID);
  if(answer.style.display == 'block'){
    answer.style.display = 'none';
    link.innerHTML = 'zobrazit odpověď';
  }
  else{
    answer.style.display = 'block';
    link.innerHTML = 'skrýt odpověď';
  }
  return false;
}

function showAnswerSearch(link, answerID){
  var answer = getObj(answerID);
  if(answer.style.display == 'none'){
    answer.style.display = 'block';
    link.innerHTML = 'skrýt odpověď';
  }
  else{
    answer.style.display = 'none';
    link.innerHTML = 'zobrazit odpověď';
  }
  return false;
}

function clearField(obj, str){
  if(obj.value == str) obj.value='';
}

 function openBlank(url){
    wasOpen  = false;
    win = window.open(url);
    return (typeof(win)=='object')?true:false;
  }

