// Make a pop-up window function.
function open_window (width, height, url, name, scrollbars, left, top) {
  // Add some pixels to the width and height.
  //width = width + 25;
  //height = height + 50;

  // If the window is already open, resize it to the new dimensions.
  if (window.popup_window && !window.popup_window.closed) {
    window.popup_window.resizeTo (width, height);
  }

  // Set the window properties.
  var window_specs = "location=no, scrollbars=" + scrollbars + ", menubars=no, toolbars=no, resizable=yes, width=" + width + ", height=" + height + ", left=" + left + ", top=" + top;

  // Set the URL.
  //var url = "reg_window.php";

  // Create the pop-up window.
  popup_window = window.open(url, name, window_specs);
  popup_window.focus();
} // End of function.


//otevre okno s detaily o telocvicne
function open_popup_gym_detail(url) {
  open_window((screen.width / 2), (screen.height / 2), url, '_blank', 'yes', 0, 0);
  return false;
}

//otevre male okno s kalendarikem
function open_popup_calendar(url) {
  var calwidth=230;
  var calheight=180;
  open_window(calwidth, calheight, url, '_blank', 'no', ((screen.width/2)-(calwidth/2)), ((screen.height/2)-(calheight/2)));
  return false;
}

function open_popup_terms_and_conditions() {
  open_window(800, 600, 'popup_terms_and_conditions.php', '_blank', 'yes', ((screen.width/2)-(800/2)), ((screen.height/2)-(600/2)));
  return false;
}

//funkce ktera zapise do inputboxu pro vyber data narozeni v registraci
//datum vybrany v popup okne s kalendarem
function select_date(day,month,year,dayinputboxname,monthinputboxname,yearinputboxname) {
  var inputbox1=top.opener.window.document.getElementById(dayinputboxname);
  if (inputbox1==null) alert('I can\'t find box named \''+dayinputboxname+'\'');
  inputbox1.value=day;
  var inputbox2=top.opener.window.document.getElementById(monthinputboxname);
  if (inputbox2==null) alert('I can\'t find box named \''+monthinputboxname+'\'');
  inputbox2.value=month;
  var inputbox3=top.opener.window.document.getElementById(yearinputboxname);
  if (inputbox3==null) alert('I can\'t find box named \''+yearinputboxname+'\'');
  inputbox3.value=year;
  parent.window.close();
  return false;
}

//funkce, ktera se prirazuje na inputy pri stisknuti
//tlacitka a tato funkce nedovoli do editu napsat jine znaky nez cisla
function AllowOnlyNumbers(inputbox) {
  var i=0;
  var result='';
  if (inputbox.value.length>0) {
    while (!isNaN(inputbox.value.charAt(i))&&(i<inputbox.value.length)) {
      result=result+inputbox.value.charAt(i);
      i++;
    }
  }
  inputbox.value=result;
}

function OnFullLostFocus(inputbox,maxcapacity,nextboxid) {
  if (inputbox.value.length==maxcapacity)  {
    var nextbox=document.getElementById(nextboxid);
    if (nextbox==null) {
      alert('I can\'t find box \''+nextboxid+'\'');
    } else {
      nextbox.focus();
    }
  }
}


//nastavi viditelnost objektu objectname na visibility
function set_visibility(objectname,visibility) {
  var box=document.getElementById(objectname);
  if (visibility) {
    box.style.display='inline';
  } else {
    box.style.display='none';
  }
  return false;
}

//funkce, ktera je pouzita v registracnim formulari a plni tu funkci,
//ze zobrazi nebo nezobrazi uzivateli selectbox s vyberem telocvicen kde afm operates
//podle toho jestli vybral v hlavnim selectboxu ze chce cvicit tam kde afm operates
//nebo zobrazi adresu pro vypneni edity pokud zvolil in my local gym
function select_gym_place(selectbox,afmgymid,inmylocgymid,inpark) {
  if (selectbox.value==afmgymid) {
    refillselect('gymselectbox',get_gyms());
    ShowTag("gymstable");
  } else {
    set_visibility("gymstable",false);
  }
  if (selectbox.value==inmylocgymid) {
    ShowTag("tablelocalgym");
  } else {
    set_visibility("tablelocalgym",false);
  }
  //pokud uzivatel zvolil ze chce cvicit v nejakem parku
  if (selectbox.value==inpark) {
    refillselect('gymselectbox',get_parks());
    //pak najdu select, ktery obsauje telocvicny
    ShowTag("gymstable");
  }
  return false;
}

function refillselect(selectid,options) {
  var box=document.getElementById(selectid);
  if (box==null) alert('I can\'t find box '+selectid);
  //vymazu obsah selectu s telocvicnama
  for (i=(box.length-1);i>=0;i--) {
    box.remove(i);
  }
  //pridam do selectu jen optiony ktere maji nastaven typ=park
  for (i=0;i<options.length;i++) {
    box.options[i]=new Option(options[i][1],options[i][0]);
  }
}

//funkce, ktera presune oprion z jednoho select boxu do druheho
function MoveOption(from,to) {
  //najdu zdrojovy selectbox
  SelFrom=document.getElementById(from);
  if (SelFrom==null) {
    Alert("I can't find selectbox "+from);
    exit;
  }
  //najdu cilovy selectbox
  SelTo=document.getElementById(to);
  if (SelTo==null) {
    Alert("I can't find selectbox "+to);
    exit;
  }
  //pokud je ve zdrojovem boxu neco vybrano a je tam taky vice nez 0 polozek
  if ((SelFrom.length>0)&&(SelFrom.selectedIndex>-1)) {
    //vytvorim novy option v cilovem selectboxu
    SelTo.options[SelTo.length]= new Option(SelFrom.options[SelFrom.selectedIndex].text, SelFrom.options[SelFrom.selectedIndex].value);
    //smazu option ve zdrojovem boxu
    var ActSelected=SelFrom.selectedIndex;
    SelFrom.options[ActSelected] = null;
    if ((ActSelected-1)>-1) {
      SelFrom.selectedIndex=ActSelected-1;
    } else if (ActSelected<SelFrom.length) {
      SelFrom.selectedIndex=ActSelected;
    }
  }
  return false;
}

//funkce, ktera oznaci vsechny optiony v selectboxu
function SelectAll(SelectBoxId) {
  var SelectBox=document.getElementById(SelectBoxId);
  if (SelectBox==null) {
    Alert('I can\'t find select box '+SelectBoxId);
    exit;
  }
  var i;
  for (i=0;i<SelectBox.length;i++) {
    SelectBox.options[i].selected=true;
  }
}

//funkce, ktera zmeni barvu borderu boxu box
function ChangeBorderColor(box,border) {
  box.style.border=border;
}

//funkce, ktera zmeni barvu fontu v boxu box
function ChangeFontColor(box,color) {
  box.style.color=color;
}

//funkce, ktera mi smaze obsah editu za urcitych podminek
function EraseEdit(box) {
  if (isNaN(box.value)) {
    box.value='';
  }
}

//funkce ktera zobrazi podmenu podle predaneho parametru
function show_submenu(y,x,menuitem) {
//  WriteLog("show_submenu("+y+","+menuitem+")");
    //pak se pokusim nastavit vysku oblasti se submenu
    area=document.getElementById("submenuarea");
    if (area==null) alert('I can\'t find box submenuarea.');
    var table=null;
    //pokud chceme zobrazit podmenu k personal training
    if (menuitem=="pt") {
      table=document.getElementById("submenutablept");
    } else if (menuitem=="stm") {
      table=document.getElementById("submenutablestm");
    }
    if (table==null) alert('I can\'t find table for this item menu.');
    //pokud se jedna o IE tak musime menu posunout trochu niz
    if (isIE()) y=parseInt(y)-157;
    if (isIE()) x=parseInt(x)-65;
    area.style.top=y+'px';
    area.style.left=x+'px';
    area.style.display='block';
    HideTag("submenutablept");
    HideTag("submenutablestm");
    table.style.display='block';
}

function isIE() {
  if (navigator.appName=='Microsoft Internet Explorer') return true;
  else return false;
}

//funkce, ktera nastavi jeden z indikatoru slouzicich pro indikaci toho
//kde je ted uzivatel s mysi
function SetMouseIndicator(indicator,value) {
  box=document.getElementById(indicator);
  if (box==null) alert('I can\'t find box '+indicator);
  if (value) {
    box.value="1";
  } else {
    box.value="0";
  }
}

//funkce, ktera skryje submenu, pokud jsou oba indikarory nastaveny na 0
//a neni nastven parametr force
function HideSubmenu(force) {
  box1=document.getElementById('mouseindicator1');
  if (box1==null) alert('I can\'t find box mouseindicator1');
  box2=document.getElementById('mouseindicator2');
  if (box2==null) alert('I can\'t find box mouseindicator2');
  if (force) {
    //pak nastavim indikatory na nulu
    box1.value="0";
    box2.value="0";
  }
  //poud neni uzivatel ani na jedne z definovanych oblasti
  if ((box1.value=="0")&&(box2.value=="0")) {
    //a schovam menu
    HideTag('submenuarea');
  }
}

//funkce, ktera skryje tag, jehoz id je ji predana pomoci parametru
function HideTag(id) {
  box=document.getElementById(id);
  if (box==null) alert('I can\'t find box '+id);
  box.style.display='none';
}

//funkce, ktera zobrazi tag, jehoz id je ji predana pomoci parametru jako block
//pokud nekdo chce zobrazit jako inline tak at pouzije set_visibility
function ShowTag(id) {
  box=document.getElementById(id);
  if (box==null) alert('I can\'t find box '+id);
  box.style.display='block';
}

function WriteLog(ln) {
  box=document.getElementById('log');
  if (box==null) alert('I can\'t find box log');
  box.value=ln+"\n"+box.value;
}

//funkce, ktera mi nastavi zindex prvku na hodnotu index
function ChangeZindex(box,index) {
  tag=document.getElementById(box);
  if (tag==null) alert('I can\'t find box '+box);
  tag.style.zIndex=index;
}

function Move(box,x,y) {
  tag=document.getElementById(box);
  if (tag==null) alert('I can\'t find box '+box);
  tag.style.top=(parseInt(tag.style.top)+y)+'px';
  tag.style.left=(parseInt(tag.style.left)+x)+'px';
}

function SetXY(box,x,y,setx,sety) {
  tag=document.getElementById(box);
  if (tag==null) alert('I can\'t find box '+box);
  if (sety) {
    tag.style.top=y+'px';
  }
  if (setx) {
    tag.style.left=x+'px';
  }
}

function SetBoxValue(boxid,value) {
  box=document.getElementById(boxid);
  if (box==null) alert('I can\'t find box '+boxid);
  box.value=value;
}

