/////
// David Grudl Tools Collection
//
// The source code is protected by copyright laws and international
// copyright treaties, as well as other intellectual property laws
// and treaties. The source code is licensed, not sold.
//
// author David Grudl <dave@dgx.cz> {@link http://www.dgx.cz}
// copyright Copyright by David Grudl, 2005. All rights reserved!
// version 0.9
///


////////////////////// BROWSER SNIFFING

dSd=document;dSna=navigator;dSs=window.screen;
dSgebi=dSd.getElementById?1:0;
// Opera 5 and higher
dSopera=window.opera?1:0;
// Netscape v4
dSns4=dSd.layers?1:0;
// Gecko - Mozilla, FireFox
dSgecko=(dSna.product=='Gecko'?1:0);
// Internet Explorer
dSie=(dSd.all?1:0)&&!dSopera;
dSie5up=dSie&&dSgebi;
dSie55up=dSie5up&&(dSd.URLUnencoded?1:0);
dSie6up=dSie5up&&(dSd.compatMode?1:0);



////////////////////// DOM HELPERS

// copyright Simon Willison
function createElement(element) {
  if (typeof document.createElementNS != 'undefined') {
    return document.createElementNS('http://www.w3.org/1999/xhtml', element);
  }
  if (typeof document.createElement != 'undefined') {
    return document.createElement(element);
  }
  return false;
}


////////////////////// CSS


// add CSS rule
// ------------
// example: addCSSRule('body','background:blue');
// copyright (c) David Grudl, http://www.dgx.cz

function addCSSRule(selector, style) {
  if (document.styleSheets) {  
  
    if (!document.styleSheets.length) {   // at least one must exists
      var el = createElement('link'); 
      el.setAttribute('href','');
      el.setAttribute('rel','stylesheet');
      el.setAttribute('type','text/css');
      document.getElementsByTagName('head').item(0).appendChild(el); 
    }

    ss = document.styleSheets[0];
      
    if (ss.insertRule) {  // mozilla
      ss.insertRule(selector + '{' + style + '}', ss.cssRules.length); 
      return true; 
    }
    
    if (ss.addRule) {     // ie
      ss.addRule(selector, style);
      return true; 
    }
    
  }
     // opera ???
/*     
  var el = createElement('style');
  el.setAttribute('type','text/css');  
  el.appendChild(document.createTextNode(selector + '{' + style + '}'));
  document.getElementsByTagName('head').item(0).appendChild(el); 
*/
}




// definuje styl závislý od přítomnosti javascript
addCSSRule('.jshide','display: none');
addCSSRule('.jsshow','display: inline');
addCSSRule('.jsshowB','display: block');

// definuje styl závislý od přítomnosti IE a javascript
if (dSie) {
  addCSSRule('.iehide','display: none');
}





////////////////////// POPUP WINDOWS


//
function queryMark(url) {
  return (url.indexOf('?') == -1) ? '?' : '&';
}


// otevře vyskakovací okénko
function popup(url, title) {
  url = url + queryMark(url) + 'popup';
  var wnd = window.open(url, title,'status=0,toolbar=0,location=0,scrollbars=1,width=350,height=300,resizable=1,left=150');
  var opened = (typeof(wnd) == "object");
  return (opened);
}

// otevře vyskakovací okénko s fixní velikostí a bez scrollbarů
function popupFixed(url, title) {
  url = url + queryMark(url) + 'popup&fixed';
  var wnd = window.open(url, title,'status=0,toolbar=0,location=0,scrollbars=0,width=350,height=300,resizable=0,left=150');
  var opened = (typeof(wnd) == "object");
  return (opened);
}


function fotoopen(url) {
  return popup('foto.php?f=' + escape(url), 'foto'); 
}


// zjisti polohu objektu vzhledem k levemu hornimu rohu dokumentu
function getAbsolutePos(id) { 
  var result = { x: 0, y: 0 }

  if ((typeof id) != 'object') id = document.getElementById(id);
  
  while (id) {
    result.x += id.offsetLeft - id.scrollLeft;
    result.y += id.offsetTop - id.scrollTop;
    id = id.offsetParent;
  }
  return result;  
}


// změní rozměry okna tak, aby byl zobrazen celý jeho obsah
function autoSize() {
  var screenW = (dSs.width || 800); 
  var screenH = (dSs.height || 600) - 20;
  var recR = 0.75; 
  var maxR = 0.95;
  
  var body = document.body;
  var body_height = 0;
  if (typeof bottom == "undefined") {
    var div = createElement("div");
    body.appendChild(div);
    var pos = getAbsolutePos(div);
    body_height = pos.y;
  } else {
    var pos = getAbsolutePos(bottom);
    body_height = pos.y + bottom.offsetHeight;
  }
  if (!dSie) {
    window.sizeToContent();
    window.sizeToContent(); 
  } else {
    // width, height podle rozmeru obrazovky
    var w = (body.offsetWidth < recR * screenW) ? (body.offsetWidth) : ((body.offsetWidth < maxR * screenW) ? (body.offsetWidth) : (recR * screenW));
    var h = (body.offsetHeight < recR * screenH) ? (body.offsetHeight) : ((body.offsetHeight < maxR * screenH) ? (body.offsetHeight) : (recR * screenH));

    // margin
    var marginX=0;
    var marginY=0;
    if (body.currentStyle) {
      marginX = parseInt(body.currentStyle.marginLeft) + parseInt(body.currentStyle.marginRight);
      marginY = parseInt(body.currentStyle.marginTop) + parseInt(body.currentStyle.marginBottom);
    }

    // scrollbars
    var scrollbarV = body.offsetHeight > h; 
    var scrollbarH = body.offsetWidth > w;
    scrollbarV = 1;
    var scrollbarSize = 20;

    // window client area
    var winX = 12; var winY = 35;
    
    // resize
    w += marginX + winX + scrollbarV*scrollbarSize;
    h += marginY + winY + scrollbarH*scrollbarSize;
    window.resizeTo(w, h);
  }
}





////////////////////// IMAGES
// copyright (c) David Grudl, http://www.dgx.cz


// preloading obrázků
var preloadImg = new Array();

function newImage(src) {
  rslt = new Image();
  rslt.src = src;
  return rslt;
}


function preloadImages() {
  for (var i=0; i < arguments.length; i++) 
    preloadImg[preloadImg.length] = newImage(arguments[i]);
}

function preloadImagesArr(arr) {
  for (var i=0; i < arr.length; i++) 
    preloadImg[preloadImg.length] = newImage(arr[i]);
}


// zobrazování a zatmívání objektů
function fade(id, visibility, duration) {
  if ((typeof id) != 'object') id = document.getElementById(id);

  if (id.style.visibility == visibility) return;
  
  if (dSie) { 
    if (!duration) duration = 4;
    id.style.filter = 'progid:DXImageTransform.Microsoft.Fade(duration=' + (duration*1) + ');';
    if (id.filters[0]) id.filters[0].apply();
    id.style.visibility = visibility;
    if (id.filters[0]) id.filters[0].play();
  } else {
    id.style.visibility = visibility;
  }
}


function fadeIn(id, duration) {
  fade(id, 'visible', duration);
}


function fadeOut(id, duration) {
  fade(id, 'hidden', duration);
}


// fade z jednoho objektu do druhého
function swapEffect(id, destimg, duration) { 
  if ((typeof id) != 'object') id = document.getElementById(id);
  if (id.style.visibility == 'hidden') return;
  
  if (dSie) { 
    if (!duration) duration = 0.8;
    id.style.filter = 'progid:DXImageTransform.Microsoft.Pixelate(MaxSquare=30, Duration=' + (duration*1) + ', Enabled=false);';
    if (id.filters[0]) id.filters[0].apply();
    id.style.backgroundImage = 'url(' + destimg + ')';
    if (id.filters[0]) id.filters[0].play();
  } else {
    id.style.backgroundImage = 'url(' + destimg + ')';
  }
}







////////////////////// FORMS
// copyright (c) David Grudl, http://www.dgx.cz


function trim(s) {
  while (s.substr(0, 1) == ' ') s = s.substr(1);
  while (s.substr(s.length-1, 1) == ' ') s = s.substr(0, s.length-2);
  return s;
}


// vraci value pro formularovy prvek, vcetne radio buttonu
function getValue(e) {
  if (e.length) {
    for (var a=0; a<e.length; a++) {
     if (e[a].checked) return e[a].value; 
     if (e[a].selected) return e[a].value; 
    }
  } else {
    return parseInt(e.value);
  }
}


// ověřování formulářů
function validX(e, errormsg, re) {
  var ok;
  var value = trim(e.value);
  if (re) ok = (new RegExp(re)).test(value);
  else ok = value != '';

  if (!ok) {
    e.focus();
    if (errormsg) alert(errormsg);
  }
  
  return ok;
}


function validEmail(e, errormsg) {
  return validX(e, errormsg, '^$|^[^@]+@[^.]+\\..+$');
}


function validIC(e, errormsg) {
  var sum = 0;
  var value = trim(e.value);
  var len = value.length;
  var i;

  for (i=0; i<len; i++) sum += (len-i) * value.charAt(i);
  if (value.charAt(len-1)=="0") sum += 10;
  tojedivne = (value.charAt(len-1)=="1");
  if ((sum % 11 != 0) && ((sum - tojedivne) % 11 !=0)) {
    e.focus();
    if (errormsg) alert(errormsg);
    return false;
  }
  return true;
}



function validCC(cctype, ccnum, ccexpm, ccexpy, cccvv) {   
  if (cctype.value==0)   // VISA
    if (!validX(ccnum, 'Zadaná číslo není platným číslem karty VISA (zadávejte jen číslice, bez mezer).', "^4(([0-9]{12})|([0-9]{15}))$")) return false;

  if (cctype.value==1)   // EC/MC
    if (!validX(ccnum, 'Zadaná číslo není platným číslem karty Eurocard/MasterCard (zadávejte jen číslice, bez mezer).', "^5[0-9]{15}$")) return false;

  if (cctype.value==2) // AMEX
    if (!validX(ccnum, 'Zadaná číslo není platným číslem karty American Express (zadávejte jen číslice, bez mezer).', "^3[47][0-9]{13}$")) return false;

  if (cctype.value==3) // DINERS CLUB 
    if (!validX(ccnum, 'Zadaná číslo není platným číslem karty Diners Club (zadávejte jen číslice, bez mezer).', "^3[068][0-9]{12}$")) return false;

  if (cctype.value==4) // JCB
    if (!validX(ccnum, 'Zadaná číslo není platným číslem karty JCB (zadávejte jen číslice, bez mezer).', "^35[0-9]{14}$")) return false;

  if (cctype.value<2) 
    if (!validX(cccvv, 'Pro karty Eurocard/MasterCard a VISA musí být CVV kód třímístný.', "^[0-9]{3}$")) return false;

  if (cctype.value==2) 
    if (!validX(cccvv, 'Pro karty American Express musí být CVV kód (Batch Code) čtyřmístný.', "^[0-9]{4}$")) return false;

 return true;
}



////////////////////// MISC


// add bookmark / favorites
// example: <a href="#" title="Přidat stránku k oblíbeným odkazům" rel="sidebar" onclick="return AddFavorite(document.location.href,document.title,this);">Oblíbený</a>
function AddFavorite(uri, title, this_a)  { 
  if (document.all && !window.opera)  { 
    window.external.AddFavorite(uri,title); 
    return false; 
  } 
  else if (window.opera) { 
    this_a.title = title; 
    return true; 
  } 
  else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) { 
    if (window.confirm('Přidat oblíbenou stránku jako nový panel?'))  { 
      window.sidebar.addPanel(title, uri, ''); 
      return false; 
    } 
  } 
  window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k vašim oblíbeným odkazům.'); 
  return false; 
}
 
