/******************************************************************************/

var ua = navigator.userAgent.toLowerCase();
var isOpera = ua.indexOf("opera")>-1;
var isIE = ua.indexOf("msie")>-1 && !isOpera;


function IsLowRes() {
  return screen.availWidth < 1024;
}

function Focus(control) {
  control.focus();
  control.select();
}

function IsEmailCorrect(email) {
  return email.match(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/)!=null;
}

/******************************************************************************/

function chcls(sender, className) {
  /*
  if (className=="") {
    if (sender.saveClass) className = sender.saveClass;
  } else {
    sender.saveClass = sender.className;
  }
  */
  sender.className = className;
}

function GetRealLink(link) {
  if (!document._realLink) document._realLink = document.createElement("div");
  document._realLink.innerHTML = '<a href="'+link+'"></a>';
  return document._realLink.firstChild.href;
}

function AddClass(sender, className) {
  if (sender.className.indexOf(className) == -1)
    sender.className += ' '+className;
//  alert("ADD "+sender.className);
}
function RemoveClass(sender, className) {
  if (sender.className.indexOf(className) > -1) {
    var exp = new RegExp(className);
    sender.className = sender.className.replace(exp, '');
  }
//  alert("REMOVE "+sender.className);
}

function mover(sender) {
  //chcls(sender,'over');
  AddClass(sender, 'over')
//  if (sender.className.indexOf('over') == -1)
//    sender.className += ' over';
//  alert(sender.className);
  //window.status = GetRealLink( sender.getAttribute("_href") );
}
function mout(sender) {
  //chcls(sender,'');
  RemoveClass(sender, 'over')
//  if (sender.className.indexOf('over') > -1)
//    sender.className = sender.className.replace(/over/, '');
//  alert(sender.className);
//  window.status = "";
}
function moverp(sender) {
  chcls(sender.parentNode,'over');
  window.status = GetRealLink( sender.getAttribute("_href") );
}
function moutp(sender) {
  chcls(sender.parentNode,'');
  window.status = "";
}

/******************************************************************************/

function chloc(sender) {
  var href = sender.getAttribute("_href");
  var target = sender.getAttribute("_target");
  
  if (target) {
    if (target=="_NEXT_CHILD") {
      target = "javascript:parent.frames[1]";
    }

    if (target.substring(0,11)=="javascript:") {
      target = eval( target.substring(11) );
      target.location.href = href;
    } else
      alert('"target" not supported');
  } else {
    if (href.substring(0,5)=="http:")
      window.open(href);
    else
      location.href = href;
  }
}

function chlocnc(sender) {
  sender.setAttribute("_target", "javascript:parent.frames[1]");
  chloc(sender);
}

/******************************************************************************/

/*
var popupImageHandle = null;
function popupImage(sender,event,src,title,w,h) {
//alert(1);
  //if (event && document.all?event.shiftKey:event.ctrlKey) { sender.target='_blank'; sender.href=src; return }
  //if (title=='') title = '  ';
  var win = window.top;
  if (document.all) {
    // window no border
    var xOffset = win.screenLeft + (win.document.body.offsetWidth-w)/2;
    var yOffset = win.screenTop + (win.document.documentElement.offsetHeight-h-80)/2;
  } else {
    // window border, same as parent
    var xOffset = win.screenX + (win.outerWidth-w)/2;
    var yOffset = win.screenY + (win.outerHeight-h-50)/2;
  }

  var opera = navigator.userAgent.indexOf("Opera")>-1;
  if (!opera && popupImageHandle && !popupImageHandle.closed) popupImageHandle.close();
  var s = 'toolbar=no,menubar=no,location=no,personalbar=no,scrollbars=no,directories=no,status=no,resizable=no,width='+w+',height='+h+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset;
  var img = '<img title="Zamknij" onclick="window.close()" style="cursor:pointer" src="'+src+'" border=0 width='+w+' height='+h+' />';
  var win = window.open('about:blank','popupImage',s);
  win.document.open();
//alert( escape(title) );
  
  win.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
  win.document.write('<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1250" /><meta http-equiv="imagetoolbar" content="no" /><title>'+unescape(title)+'</title></head><body style="margin:0;padding:0;overflow:hidden" onkeypress="if (event &amp;&amp; event.keyCode==27) self.close()">'+img+'</body></html>');
  win.document.close();
  win.focus();
  popupImageHandle = win;
}
*/

var popupHandle = null;
function __popupCenterWindow(w,h,title,src,content,style,resizable) {
  var win = window.top;
  var xOffset = 0;
  var yOffset = 0;
  //alert(w+" "+screen.availWidth);
  if (w>screen.availWidth || h>screen.availHeight) {
    w = screen.availWidth-20;
    h = screen.availHeight-60;
    resizable = true;
  } else if (isIE) {
    xOffset = win.screenLeft + (win.document.documentElement.offsetWidth-w-10)/2;
    yOffset = win.screenTop + (win.document.documentElement.offsetHeight-h-104)/2;
    xOffset = Math.min(xOffset, screen.availWidth-w-10);
    yOffset = Math.min(yOffset, screen.availHeight-h-30);
  } else {
    xOffset = win.screenX + (win.outerWidth-w-6)/2;
    yOffset = win.screenY + (win.outerHeight-h-44)/2;
    xOffset = Math.min(xOffset, screen.availWidth-w-6);
    yOffset = Math.min(yOffset, screen.availHeight-h-44); 
  }
  xOffset = Number( Math.max(xOffset, 0) );
  yOffset = Number( Math.max(yOffset, 0) );

  if (!isOpera && popupHandle && !popupHandle.closed) popupHandle.close();

  var attr = 'toolbar=no,menubar=no,location=no,personalbar=no,directories=no';
  if (resizable) attr += ',status=yes,resizable=yes,scrollbars=yes';
  else attr += ',status=no,resizable=no,scrollbars=no';
  attr += ',width='+w+',height='+h+',screenX='+xOffset+',screenY='+yOffset+',left='+xOffset+',top='+yOffset;
  
  if (src) {
  	var popup = win.open(src,'inventor_popup',attr);
	} else if (content) {
    var s = '';
    s += '<style type="text/css">'
    s += 'body, tbody {font-size:11px;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif}';
    s += 'body {margin:0px;padding:0px}';
    if (!resizable) s += 'body {overflow:hidden}';
    if (style) s += style;
    s += '</style>';
    
    var c = '';
    c += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    c += '<html xmlns="http://www.w3.org/1999/xhtml"><head>';
    c += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
    c += '<meta http-equiv="imagetoolbar" content="no" />';
    c += '<title>'+title+'</title>'+s+'</head>';
    c += '<body onkeypress="if (event &amp;&amp; event.keyCode==27) self.close()">'+content+'</body>';
    c += '</html>';
    
    var popup = win.open('','inventor_popup',attr); // on ssl open('/', or open('.', 
    popup.document.open();
    popup.document.write(c);
    popup.document.close();
	}
	//setTimeout('__popupOnKeyPress()', 1);
/*
  popup._onkeypress = 123;
    //setTimeout('window.document.body.onkeypress = new Function("self.close()");', 100);
    //alert(popup.document);
    
    //popup._onkeypress = 123;
    f = '';
    f += 'var w=popupHandle;';
    f += 'if (w && !w.closed) {'
      f += 'if (w.document && w.document.body) {'
      f += '} else {';
      f += '}';
    f += '}';
    popup.setTimeout('alert(popupHandle+" "+popupHandle._onkeypress)', 1500);
    //alert(popup.document);
    /*
    if (isIE) {
      popup.document.onkeypress = new Function("popupHandle.close()");
      if (popup.document) {
      } else {
      }
      //win.setTimeout('popupHandle.document.onkeypress = new Function("alert(event)")',500);
    } else {
        var func = new Function( 'event', 'popupHandle.close()' );
        popup.document.addEventListener("keypress", func, true);
    }
    */
    //popup.onload = new Function("alert(1)");
  
  
  
  if (popup && popup.focus) popup.focus();
  popupHandle = popup;

  //popupHandle._onkeypress = 123;
}
/*
function __popupOnKeyPress() {
  if (popupHandle && !popupHandle.closed && popupHandle.document) {
    //alert(popupHandle.document.body);
    popupHandle.document.onkeypress = new Function("popupHandle.close()");
  }
}
*/

function popupImage(sender,event,src,title,w,h) {
  var content = '<img title="Zamknij" onclick="window.close()" style="cursor:pointer;_cursor:hand" src="'+src+'" border="0" width="'+w+'" height="'+h+'" alt="" />';
  var  title = '';
  var style = '';
  __popupCenterWindow(w,h,title,null,content,style,false);
}
function popupFlash(sender,event,src,title,w,h) {
  var content = '';
  content += '<object type="application/x-shockwave-flash" data="'+src+'" width="'+w+'" height="'+h+'">';
  content += '<param name="movie" value="'+src+'" />';
  content += '<param name="menu" value="false" />';
  content += '<param name="quality" value="high" />';
  content += '<param name="wmode" value="opaque" />';
  content += '</object>';

  //var content = '<img title="Zamknij" onclick="window.close()" style="cursor:pointer;_cursor:hand" src="'+src+'" border="0" width="'+w+'" height="'+h+'" alt="" />';
  var  title = '';
  var style = '';
  __popupCenterWindow(w,h,title,null,content,style,false);
}

//function popupImage2(sender,event,src,title,w,h) {
function popupImage2(sender,src,w,h,Astyle,Atext) {
  winW = w+20;
  winH = h+(Atext?80:20);
//  alert(winW);
  var content = '<img title="Zamknij" onclick="window.close()" style="cursor:pointer;_cursor:hand" src="'+src+'" border="0" width="'+w+'" height="'+h+'" alt="" />';
if (Atext) content += '<div style="margin-top:10px;height:48px;overflow:auto">'+unescape(Atext)+'</div>';
var  title = '';
    style = 'body {margin:10px}';

  __popupCenterWindow(winW,winH,title,null,content,style,false);
}

function popupCenterWindow(src,w,h,resizable) {
  //if (typeof(resizable)=="undefined") resizable = true;
  //resizable = true;
  var title = '';
  __popupCenterWindow(w,h,title,src,null,null,resizable);
}

/******************************************************************************/

function ScanNodes(node, onNode) {
  // onNode = function(node, result) { result.result = node; result.doBreak = true; }
  var stack = new Array();
  var result = {doBreak:false,result:null};
  do {
    onNode(node, result);
    if (result.doBreak) return result.result;
    stack.push(node);
    if (node.firstChild)
      node = node.firstChild;
    else do {
      node = stack.pop();
      node = node.nextSibling;
    } while(stack.length && node==null);
  } while (stack.length)
}

function FindParentByTagName(startNode, tagName) {
  tagName = tagName.toUpperCase();
  node = startNode;
  while (node.tagName != tagName)
    node = node.parentNode;
  return node;
}

function FindChildControlByType(node, type) {
  node = node.firstChild;
  while (node) {
    if (node.type && node.type==type) return node;
    if (node.firstChild) result = FindChildControlByType(node, type);
    if (result) return result;
    node = node.nextSibling;
  }
  return null;
}

function GetChildrenByAttr(node, attrName, attrValue, result) {
  node = node.firstChild;
  while (node) {
    if (node.getAttribute && node.getAttribute(attrName) == attrValue) result.push(node);
    if (node.firstChild) GetChildrenByAttr(node, attrName, attrValue, result);
    node = node.nextSibling;
  }
}

function GetChildrenByType(node, type, result) {
  node = node.firstChild;
  while (node) {
    //alert( node.type );
    if (node.type && node.type==type) result.push(node);
    if (node.firstChild) GetChildrenByType(node, type, result);
    node = node.nextSibling;
  }
}

function GetChildrenByTagName(node, tagName, result) {
  node = node.firstChild;
  while (node) {
    if (node.tagName==tagName.toUpperCase()) result.push(node);
    if (node.firstChild) GetChildrenByTagName(node, tagName, result);
    node = node.nextSibling;
  }
}


/******************************************************************************/

function ShowImage(sender, ix) {
  //var checked = sender.getAttribute("_checked");
  //sender.setAttribute("_checked", 1-checked);
  checked = ix;

  var imgs = new Array();
  
  GetChildrenByTagName(sender, 'img', imgs);
//  alert(checked+"#"+imgs.length);
//  alert(imgs);

  for (var i=0; i<imgs.length; i++) imgs[i].style.display = 'none';
  imgs[checked].style.display = '';
}


function SetContentById(id, value) {
  var obj = document.getElementById(id);
  obj.innerHtml = value;
}

function xSetImgSrcById(id, src) {
  var obj = document.getElementById(id);
obj = obj.firstChild;
  obj.src = src;
}

/******************************************************************************/


function extractQueryParam(sender, paramName) {
  var s = sender.location.search;
  var arr = s.split('&');
  var tmp;
  for (var i=0; i<arr.length; i++) {
    tmp = arr[i].split('=');
    if (tmp.length==2 && tmp[0]==paramName)
      return tmp[1];
  }
  return null;
}

/******************************************************************************/

function cancel(event) {
//  event.cancelBubble=true;
}


function SetCheckBoxes(sender, value, prefix) {
  var form = FindParentByTagName(sender, 'FORM');
  var result = new Array();
  var checkboxes = GetChildrenByType(form, 'checkbox', result);
  var len = result.length;
  var prefixlen = prefix ? prefix.length : 0;
  for (var i=0; i<len; i++) {
    if (!prefix || result[i].name.substring(0,prefixlen)==prefix)
      result[i].checked = value;
  }
  
}

