var isDOM = (document.getElementById);
var isIE4 = (document.all && !isDOM);
var isNS4 = (document.layers);
var isDyn = (isDOM || isIE4 || isNS4);

function getRef(id) {
if (isDOM) return document.getElementById(id);
if (isIE4) return document.all[id];
if (isNS4) return document.layers[id];
}

function getVP(x) {
if (typeof x == 'undefined') x = 'vp';
return document.forms[x];
}

function r2(x, desMist) {
x = parseFloat(x);
return Math.round(x * Math.pow(10, desMist)) / Math.pow(10, desMist);
}

function nf(x) {
x = parseFloat(x.replace(/,/g, ".").replace(/ /g, ''));
return isNaN(x) ? 0 : x;
} 

function enAb(theObject) {
theObject.disabled = false;
if (theObject.nodeName == 'INPUT' && theObject.type == 'text' || theObject.nodeName == 'SELECT') theObject.style.backgroundColor = '#fff';
}

function disAb(theObject) {
theObject.disabled = true;
if (theObject.nodeName == 'INPUT' && theObject.type == 'text' || theObject.nodeName == 'SELECT') theObject.style.backgroundColor = '#ddd';
}

function itemIndex(r) {
for (var i = 0; i < r.length; i++) if (r[i].checked == true) return i;
return -1;
} 

function mysubmit2(name) {
  getVP(name).elements['pass2'].value = getVP(name).elements['pass1'].value;
  getVP(name).elements['pass1'].value = '';
}

/* administrace eshopu */
function showAdminPhoto(theObj) {
  if (!upozorneni_zobrazeno) {
    getRef('admin_photo').style.background = 'blue';
    recountPhotoSize(theObj.form.elements['img_file']);
    var x = nf(getRef('fid_img_small_x').value);
    var y = nf(getRef('fid_img_small_y').value);
    getRef('admin_photo').style.width = x + 'px';
    getRef('admin_photo').style.height = y + 'px';
  } else {
    getRef('admin_photo').style.width = '0px';
    getRef('admin_photo').style.height = '0px';
  }
}

var upozorneni_zobrazeno = 0;

function recountPhotoSize(theObj) {
  var parts = theObj.value.split('.');
  if (parts && parts[parts.length - 1]) {
    var pripona = parts[parts.length - 1].toLowerCase();
    switch (pripona) {
      case 'gif':
      case 'jpg':
      case 'jpeg':
      case 'png':
        var pic = new Image();
        pic.src = theObj.value;
        if (pic.width && pic.height) {
          getRef('admin_photo_osize').innerHTML = '(<small>sirka: ' + pic.width + 'px; vyska: ' + pic.height + 'px</small>)';
          var ox_oy = pic.width / pic.height;
          var oy_ox = pic.height / pic.width;

          // small photo
          if (!itemIndex(theObj.form.elements['rdio_img_small'])) {
            x = nf(getRef('fid_img_small_x').value);
            y = Math.round(oy_ox * x);
            getRef('fid_img_small_y').value = y;
          } else if (itemIndex(theObj.form.elements['rdio_img_small'])) {
            y = nf(getRef('fid_img_small_y').value);
            x = Math.round(ox_oy * y);
            getRef('fid_img_small_x').value = x;
          }
          getRef('admin_photo').style.width = x + 'px';
          getRef('admin_photo').style.height = y + 'px';

          // large photo
          var x = nf(getRef('fid_img_large_x').value);
          var y = nf(getRef('fid_img_large_y').value);
          if (x) getRef('fid_img_large_y').value = Math.round(oy_ox * x);
          else if (y) getRef('fid_img_large_x').value = Math.round(ox_oy * y);

        } else if (!upozorneni_zobrazeno) {
          enAb(getRef('fid_img_small_x'));
//          enAb(getRef('fid_img_small_y'));
          enAb(getRef('fid_img_large_x'));
//          enAb(getRef('fid_img_large_y'));
          upozorneni_zobrazeno = 1;
        }
        break;
      default:
        alert('Nepodporovany format obrazku - podporovane formaty jsou JPG, GIF, PNG.');
        if (getRef('admin_photo_osize').innerHTML) getRef('admin_photo_osize').innerHTML = '';
        theObj.value = '';
        return;
    }
  }
}

function disAbSizes(theObj) {
  var parent = theObj.form.elements[theObj.name];
  var sI = itemIndex(parent);
  var name = 'img' + theObj.name.substr(theObj.name.lastIndexOf('_'), 6) + '_';
  if (sI) {
    disAb(theObj.form.elements[name + 'x']);
    enAb(theObj.form.elements[name + 'y']);
    if (upozorneni_zobrazeno) theObj.form.elements[name + 'x'].value = '';
  } else {
    enAb(theObj.form.elements[name + 'x']);
    disAb(theObj.form.elements[name + 'y']);
    if (upozorneni_zobrazeno) theObj.form.elements[name + 'y'].value = '';
  }
}
/* /administrace eshopu */

/* vystaveni faktury */
function openBillWin(url) {
	var sizeX = 720;
	var sizeY = 500;
	var leftpos = (screen.availWidth - sizeX) / 2;
	var toppos = (screen.availHeight - sizeY - 30) / 2;

  url+='&amp;show=invoice&amp;tisk=window';
	var billWin = window.open(url, "faktura", "width="+sizeX+", height="+sizeY+", top=" + toppos + ", left=" + leftpos + ", directories=0, location=0, menubar=1, scrollbars=1, status=0, titlebar=0, toolbar=0, resizable=1");
	billWin.focus();
  return billWin;
}

function dom_modify(theObj) {
  value = prompt('', theObj.innerHTML);
  if (value != null) theObj.innerHTML = value;
}
/*/vystaveni faktury */
