// Uuden ikkunan avaaminen
function UusiIkkuna(URL, leveys, korkeus, otsake){
  var w=600;
  var h=700;
  var Otsikko="BASIC";

  if (arguments.length >= 3){
    w = leveys;
    h = korkeus;
  }
  if (arguments.length >= 4){
    Otsikko = otsake;
  }
  var scroll="yes";
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable,alwaysRaised'; 
  //if (win.closed || !win){ 
  win = window.open(URL,Otsikko,settings);
  if(win.window.focus){win.window.focus();}
}

// Uuden ikkunan avaaminen
function UusiIkkunaPaneelein(URL, leveys, korkeus, otsake){
  var w=600;
  var h=700;
  var Otsikko="BASICPaneelein";
  if (arguments.length >= 3){
    w = leveys;
    h = korkeus;
  }
  if (arguments.length >= 4){
    Otsikko = otsake;
  }
  var scroll="yes";
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable,alwaysRaised,location=yes,toolbar=yes'; 
  //if (win.closed || !win){ 
  win = window.open(URL,Otsikko,settings);
  if(win.window.focus){win.window.focus();}
}


// tähän voi tehdä linkin, jolla esim. pikkuikkunan saa kiinni
function SuljeIkkuna() { self.close(); }


// cookien asettaminen javascriptillä
function Set_Cookie(name,value,expires,path,domain,secure) {
  var today = new Date();
  today.setTime( today.getTime() );

  if (expires){
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" ) + 
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

// tekee linkkivalikon
// links = linkit osoitteisiin
// names = nimet linkeille
// alalinkit = alalinkkivalikko
// now = millä alasivulla nyt ollaan (ala muuttujan kenttä)
function linkkivalikko(links,names,alalinkit,now){
  if (now == ''){
    now = 'eivarmaanole';
  }
  // KOKONAISKORKEUS OLTAVA 53 KUVAN KOHDALLA
  document.write('<tr>');
  //document.write(vali);
  for (k=0;k<links.length;k++){
    if (k){
      document.write('<td width="1" align="center" valign="top">');
      document.write('<img src="fig/style2/linkkipalkki_ala_vali.png"></td>');
    }
    pois = 'alalinkkiOFF';
    if (links[k].search('ala=' + now)>0){
      pois = 'alalinkkiOFFNOW';
    }
    document.write('<td class="' + pois + '" height="28"');
    document.write('onmouseover="');
    document.write('this.className=\'alalinkkiON\'; ');
    document.write('POIS[' + k + ']=0; ');
    if (alalinkit[k] != ''){
      document.write('document.getElementById(\'linkkitaso_' + k + '\').className=\'linkkitasoON\';');
    }
    document.write('" onmouseout="tasopois(' + k + ',\'' + pois + '\');" ');
    document.write('onclick="window.location.href=\'' + links[k] + '\';" ');
    document.write('id="paalinkki_' + k + '"');
    document.write('nowrap>');
    document.write('<span style="margin-left:10px; margin-right:10px">');
    document.write(names[k]);
    document.write('</span>');
    document.write('</td>');
    //document.write(vali);
  }
  document.write('</tr><tr>');
  //document.write('<td height="0"></td>');
  for (k=0;k<links.length;k++){
    pois = 'alalinkkiOFF';
    if (links[k].search('ala=' + now)>0){
      pois = 'alalinkkiOFFNOW';
    }
    document.write('<td height="1" onmouseover="POIS[' + k + ']=0;" valign="top" align="left" ');
    document.write('onmouseout="tasopois(' + k + ',\'' + pois + '\');" nowrap>');
    document.write('<div class="linkkitasoOFF" id="linkkitaso_' + k + '">');
    if (alalinkit[k] != ''){
      // ALALINKKIENKÄSITTELY OMANA LAYERINA    
      document.write('<table border="0" cellspacing="0" cellpadding="0">');
      //document.write('<tr><td class="tummabg" height="1"></td></tr><tr><td>');
      document.write('<tr><td nowrap><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td>');
      document.write(alalinkit[k]);
      document.write('</td></tr></table>');
      document.write('</td></tr></table>');
    }
    document.write('</div></td><td></td>');	
  }
  document.write('</tr>');
  //document.write('<td height="53" width="2" valign="top"><img src="fig/style/palkki2_oikea.png"></td>');
  //document.write('</td></tr></table></div>');
}

// SEURAAVAT KAKSI FUNKTIOTA LAITTAVAT OIKEASTI TASON POIS NÄKYVISTÄ JA SIIHEN LIITTYVÄN MAHDOLLISEN ALATASON
function tasopois(nro,offclass){
  POIS[nro] = 1;
  setTimeout('piilotaTaso(' + nro + ',\'' + offclass + '\');',100);
}

function piilotaTaso(nro,offclass){
  if (POIS[nro]){
    document.getElementById('paalinkki_' + nro).className=offclass;
    //document.getElementById('paalinkki_' + nro).className='alalinkkiOFF';
    document.getElementById('linkkitaso_' + nro).className='linkkitasoOFF';	
  }
}


// TEKEE PÄÄOTSIKON, 
function ulkoasu_otsikko(teksti,kuva){

  kuvat = new Array('huuto',
		    'kalsin',
		    'kalora',
		    'muistio',
		    'stop');
  polut = new Array('huutomerkki',
		    'kalenteri_sin',
		    'kalenteri_ora',
		    'muistilehtio',
		    'stop');
  kuvanow = 'kalenteri_sin';
  for (k=0;k<kuvat.length;k++){
    if (kuva == kuvat[k]){
      kuvanow = polut[k];
      break;
    }
  }

  document.write('<table border="0" cellspacing="0" cellpadding="0" width="100%">' +
		 '<tr><td width="11" height="55" align="right" valign="top">' +
		 '<div style="margin-top: 17px;">' +
		 '<img src="fig/style2/ots_taustapalkki_vasen.png"></div></td>' +
		 '<td style="background-image: url(fig/style2/ots_taustapalkki_keski.png);' +
		 'background-repeat: repeat-x;' +
		 'background-position: 0 17">' +
		 '<table border="0" cellspacing="0" cellpadding="0">' +
		 '<tr><td width="20" height="55" width="40"></td>' +
		 '<td style="background-image: url(fig/style2/ots_taustapallo.png); ' +
		 'background-repeat: no-repeat;' +
		 'background-position: 0 9;" valign="middle" align="left" width="50">' +
		 '<div style="margin-left: 7px;">' + 
		 '<img src="fig/style2/ots_' + kuvanow +  '.png">' +
		 '</div></td>' +
		 '<td width="20"></td><td valign="middle">' +
		 '<font color="white">' + teksti + '</font>' +
		 '</td><td width="20"></td></tr></table>' +
		 '</td>' +
		 '<td width="11" height="55" align="left" valign="top">' +
		 '<div style="margin-top: 17px;">' +
		 '<img src="fig/style2/ots_taustapalkki_oikea.png"></div></td></tr></table>');

}


// YLEINEN KLIKKAUKSESTA TULEVA TASOJEN VAIHTO
// pois = naytettavan id
// paalle = naytettavan id
function TasoVaihto(pois,paalle){
  document.getElementById(pois).style.visibility='hidden';
  document.getElementById(pois).style.display='none';
  document.getElementById(paalle).style.visibility='visible';
  document.getElementById(paalle).style.display='block';
}

// NÄYTTÄÄ TASON
function TasoShow(id,pos){
  document.getElementById(id).style.visibility='visible';
  if (arguments.length == 1){
    document.getElementById(id).style.display='block';
  }
  else {
    document.getElementById(id).style.position='absolute';
  }
}

// PIILOTTAA TASON
function TasoHide(id){
  document.getElementById(id).style.visibility='hidden';
  document.getElementById(id).style.display='none';
}

function TasoHideShow(id){
  nyt = document.getElementById(id).style.visibility;
  if (nyt == 'visible'){
    TasoHide(id);
  }
  else {
    TasoShow(id);
  }
}


// HOITAA ILMOITTAUTUMISLOMAKKEEN JAVASCRIPTIN LÄHETYKSEN
function IlmoittauduTapahtumaan(tapid,kerhoid,status,color,arvo,LayerName,joukkueryhmanaytto){
  var myfield = 'viesti' + tapid;
  viesti = document.getElementById(myfield).value;
  document.getElementById('ilmo' + tapid).innerHTML = 
    '<font color="' + color + '">' + arvo + '<font>';
  URL = 'IlmoittauduTapahtumaanBlankko.php';
  params = '?tuleeko=' + status + '&kerhoid=' + kerhoid + '&tapid=' + tapid + '&viesti=' + viesti;
  document.images['BlankkoImage'].src = URL + params;
  TasoHide('ilmolomake' + tapid);  
  //Sleep(200);
  // KOMMENTOI VAAN TÄMÄ RIVI POIS, JOS TULEE ONGELMIA TOIMINNASSA
  URL2 = "XML/XML_TapahtumaIlmomaaraId.php?tapahtumaid=" + tapid + 
    "&kerhoid=" + kerhoid + "&LayerName=" + LayerName;
  if (arguments.length > 6){
    URL2 = URL2 + '&joukkueryhmanaytto=' + joukkueryhmanaytto;
  }
  setTimeout(function () {clientSideInclude("Tulossa" + tapid,URL2)},1000);
  if (LayerName != ''){
    setTimeout(function() {document.getElementById(LayerName).style.left = -120 + "px";},3000);
  }
}

function Sleep(ms){
  var dt = new Date();
  dt.setTime(dt.getTime() + ms);
  while (new Date().getTime() < dt.getTime());
}


// LATAA JONKUN SIVUN SISÄLTÖ JOHONKIN KOHTAAN SIVUA
function clientSideInclude(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } 
  else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
  if (id != 0){
    var element = document.getElementById(id);
  }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    text = req.responseText;
    if (id == 0){
      return text;
    }
    else {
      element.innerHTML = text;
    }
  } 
  else {
    // Ei toimi
    alert('Virhe. Selaimesei ei tule XMLHTTPRequest objekteja. Tämä toimii ainakin selaimissa IE 5 ja uudemmat, Firefox ja Safari.');
    element.innerHTML = 'Virhe';
    /*
      element.innerHTML =
      "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
    */
  }
}


function LahetaSahkoposti(otsikko,viesti,to,from){
  url = 'LahetaSahkoposti.php?otsikko=' + htmlEncode(otsikko) + 
    '&viesti=' + htmlEncode(viesti) + 
    '&to=' + to + '&from=' + from;
  //document.write(url);
  //return;
  vastaus = clientSideInclude(0, url);
  if (vastaus == 1){
    alert('Viesti lähetetty onnistuneesti.');
    return 1;
  }
  else {
    alert('Virhe viestin lähetyksessä.');
    return 0;
  }
}


function htmlEncode(s){
  s = encodeURI(s);
  return s.replace(/&/g,'ET');

  s.replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");

  return s.replace(/&(?!\w+([;\s]|$))/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

