//potwierdzenie
function confirmSubmit(infotext){
  var agree=confirm(infotext);
  if (agree){
    return true ;
  } else {
    return false ;
  }
}

function $(id)
{
	return document.getElementById(id);
}
function $$(id)
{
	return document.getElementById(id).value;
}
function submitForm(_formName, _actionName, _actionValue)
{
  	var actionName = document.getElementById(_actionName) ;
  	alert(_formName);
  	
  	if(actionName == null)
  	{
  		alert("Error: you have to define hidden action variable") ;
  		return ;
	}
  	actionName.value = _actionValue ;
  	document.forms[_formName].submit() ;
}
var flaga=true;
function parserecord(result){
alert(result);
}
function loginremember()
{
var formlog=$('loginremember');
if(flaga)
{
	formlog.innerHTML='';
	flaga = false;
	formlog.innerHTML='<div id="login"><div class="loginHaslo"><div class="prawy" ><div style="float:left; padding-top: 6px" class="email">email</div><div style="float:right;" class="inputColor"><input class="styl1" id="loginnameremember" name="loginnameremember" type="text" value="" style="width:127px" /></div></div></div></div><div  class="zaloguj" style="text-align:right"><a href="javascript:dorequest(\'POST\',\'changepassword.php\',\'tekst\',\'parserecord\',\'action=passwordremember&email=\'+$$(\'loginnameremember\'))" class="zaloguj"><img style="margin-right:5px; margin-top:4px" src="images/wyslij.jpg" width="88" height="24" alt="" /><div style="clear:both"></div></a></div><a class="przypomnijHaslo2" href="javascript:loginremember()">powrót do logowania</a>';
}
else
{
	formlog.innerHTML='';
	flaga = true;
	formlog.innerHTML='<div id="login"><div class="loginHaslo"><div class="prawy" ><div style="float:left; padding-top: 6px" class="email">email</div><div style="float:right;" class="inputColor"><input class="styl1" name="loginName" type="text" value="" style="width:127px" /></div><div style="float:left; padding-top: 6px" class="email">hasło</div><div style="float:right;" class="inputColor1"><input class="styl1" name="loginPasswd" type="password" value="" style="width:127px" /></div></div></div></div><div  class="zaloguj" style="text-align:right"><a href="javascript: submitForm(\'loginForm\', \'actionId\', \'login\')" class="zaloguj"><img style="margin-right:5px; margin-top:4px" src="images/eDelikatesy_29.jpg" width="88" height="24" alt="" /><div style="clear:both"></div></a></div><a class="zalozKonto" href="zalozKonto.php">załóż konto</a><a class="przypomnijHaslo" href="javascript:loginremember()">przypomnij hasło</a>';
}

}


// function changes images on product site
function changeImg(imgObj, imgHref, imgToChange,width,height)
{
  	var mediumPath = "images/products/medium/" ;
  	var normalnPath = "images/products/" ;
  	var img = document.getElementById(imgObj) ;
  	img.src = mediumPath + imgToChange ;
  	// link passed in 'alt' field for popup javascript
  	img.alt = imgToChange ;
  	img.onclick=new Function("autoResizeWindow(this,'" + width + "+20','" + height + "+20')");
  	var hrefObj = document.getElementById(imgHref) ;
  	//hrefObj.href = normalnPath + imgToChange ;
}


function autoResizeWindow(imageObj,imgWidth,imgHeight)
{
  	var imagePath = "images/products/" + imageObj.alt ;
//	alert(imageObj.alt);
   	var tempImg = new Image() ;
//	alert(imagePath);
	tempImg.src = imagePath ;
  //	alert(tempImg.src);
  	

  	var openedWindow = window.open(imagePath, "" ,"width="+imgWidth+",height="+imgHeight+", menubar=no, toolbar=no, location=no, scrollbars=no, resizable=yes, status=no'") ;
  	
  	// get image size

 // 	openedWindow.resizeTo(imgWidth, imgHeight) ;
  /*	
  	openedWindow.width = imgWidth ;
 	openedWindow.height = imgHeight ;*/
}

// ***************** Rozwijana lista do pliku... gdzieś do zaplecza po prostu :E - POCZATEK

function tree(id)
{
  this.id = id;

  this.click = function ()
  {
    for (var i = 0, el_node; i < this.parentNode.childNodes.length; i++)
    {
      el_node = this.parentNode.childNodes.item(i)
      if (el_node.nodeName.toLowerCase() == 'ul')
      {
        el_node.style.display = el_node.style.display == 'none' ? 'block' : 'none';
        this.parentNode.style.backgroundImage = 'url("' + (el_node.style.display == 'none' ? 'closed' : 'opened') + '.gif")';
        return;
      }
    }
  }

  this.start = function (el)
  {
  	var node_href = null;
	var location_href = null;
    for (var i = 0, el_node; i < el.childNodes.length; i++)
    {
      el_node = el.childNodes.item(i);
      if (el_node.nodeName.toLowerCase() == 'a')
      {
        el_node.onclick = this.click;
        for (var j = 0; j < el_node.parentNode.childNodes.length; j++)
        {
          if (el_node.parentNode.childNodes.item(j).nodeName.toLowerCase() == 'ul')
          {
            el_node.parentNode.style.backgroundImage = 'url("closed.gif")';
            el_node.className = (el_node.className ? el_node.className + ' ' : '') + 'folder';
            break;
          }
          if (el_node.parentNode.childNodes.item(j).nodeName.toLowerCase() == 'li') break;
        }
		node_href = unescape(el_node.href.match(/catId=\d+/));
		location_href = unescape(window.location.href.match(/catId=\d+/));
		
        if (el_node.href && location_href!=null && location_href!='null' && node_href==location_href)
        {
          el_node.className = 'menuTreeNodeActive';
          var el_parentNode = el_node;
          do
          {
            el_parentNode = el_parentNode.parentNode;
            if (el_parentNode.nodeName.toLowerCase() == 'ul')
            {
              el_parentNode.style.display = 'block';
              if (document.getElementById(this.id) != el_parentNode) el_parentNode.parentNode.style.backgroundImage = 'url("opened.gif")';
            }
          }
          while (document.getElementById(this.id) != el_parentNode)
        }
      }
      else if (el_node.nodeName.toLowerCase() == 'ul') el_node.style.display = 'none';
      this.start(el_node);
    }
  }

  if (document.getElementById && document.childNodes)
  {
    if (document.images)
    {
      new Image().src = 'pik.gif';
      new Image().src = 'pik.gif';
    }
    this.start(document.getElementById(this.id));
  }
}

////////////////////////////////////////////////////////////////////////////////

// general submit form function
function submitForm(_formName, _actionName, _actionValue)
{ 	
  	var actionName = document.getElementById(_actionName) ;
  	
  	if(actionName == null)
  	{
  		alert("Error: you have to define hidden action variable") ;
  		return ;
	}
  	actionName.value = _actionValue ;
  	document.forms[_formName].submit();

}

// show/hide div coresponding to checkBox
function showHideCheckBox(divId, checkBoxId)
{
  	
  	var hideDiv = document.getElementById(divId) ;
  	var checkBox = document.getElementById(checkBoxId) ;
  	
  	if(checkBox == null)
  		alert('checkBoxId incorrect') ;
	if(hideDiv == null)
  		alert('divId incorrect') ;
  	
  	if(checkBox.checked)
     		hideDiv.style.display = "block" ;
   	else
   		hideDiv.style.display = "none" ;
}
function ClickShowHideMenu(id) {
    this.box1Hover = true;
    this.box2Hover = true;

    this.init = function() {
        if (!document.getElementById(this.id)) {
            alert("Element '"+this.id+"' does not exist in this document. ClickShowHideMenu cannot be initialized");
            return;
        }
        this.parse(document.getElementById(this.id).childNodes, this.tree, this.id);
        this.load();
        if (window.attachEvent) {
            window.attachEvent("onunload", function(e) { self.save(); });
        } else if (window.addEventListener) {
            window.addEventListener("unload", function(e) { self.save(); }, false);
        }
    }

    this.parse = function(nodes, tree, id) {
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].nodeType != 1) {
                continue;
            }
            if (nodes[i].className) {
                if ("box1" == nodes[i].className.substr(0, 4)) {
                    nodes[i].id = id + "-" + tree.length;
                    tree[tree.length] = new Array();
                    eval('nodes[i].onmouseover = function() { self.box1over("'+nodes[i].id+'"); }');
                    eval('nodes[i].onmouseout = function() { self.box1out("'+nodes[i].id+'"); }');
                    eval('nodes[i].onclick = function() { self.box1click("'+nodes[i].id+'"); }');
                }
                if ("section" == nodes[i].className) {
                    id = id + "-" + (tree.length - 1);
                    nodes[i].id = id + "-section";
                    tree = tree[tree.length - 1];
                }
                if ("box2" == nodes[i].className.substr(0, 4)) {
                    nodes[i].id = id + "-" + tree.length;
                    tree[tree.length] = new Array();
                    eval('nodes[i].onmouseover = function() { self.box2over("'+nodes[i].id+'", "'+nodes[i].className+'"); }');
                    eval('nodes[i].onmouseout = function() { self.box2out("'+nodes[i].id+'", "'+nodes[i].className+'"); }');
                }
            }
            if (nodes[i].childNodes) {
                this.parse(nodes[i].childNodes, tree, id);
            }
        }
    }

    this.box1over = function(id) {
        if (!this.box1Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = (this.id_openbox == id ? "box1-open-hover" : "box1-hover");
    }

    this.box1out = function(id) {
        if (!this.box1Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = (this.id_openbox == id ? "box1-open" : "box1");
    }

    this.box1click = function(id) {
        if (!document.getElementById(id)) {
            return;
        }
        var id_openbox = this.id_openbox;
        if (this.id_openbox) {
            if (!document.getElementById(id + "-section")) {
                return;
            }
            this.hide();
            if (id_openbox == id) {
                if (this.box1hover) {
                    document.getElementById(id_openbox).className = "box1-hover";
                } else {
                    document.getElementById(id_openbox).className = "box1";
                }
            } else {
                document.getElementById(id_openbox).className = "box1";
            }
        }
        if (id_openbox != id) {
            this.show(id);
            var className = document.getElementById(id).className;
            if ("box1-hover" == className) {
                document.getElementById(id).className = "box1-open-hover";
            }
            if ("box1" == className) {
                document.getElementById(id).className = "box1-open";
            }
        }
    }

    this.box2over = function(id, className) {
        if (!this.box2Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = className + "-hover";
    }

    this.box2out = function(id, className) {
        if (!this.box2Hover) return;
        if (!document.getElementById(id)) return;
        document.getElementById(id).className = className;
    }

    this.show = function(id) {
        if (document.getElementById(id + "-section")) {
            document.getElementById(id + "-section").style.display = "block";
            this.id_openbox = id;
        }
    }

    this.hide = function() {
        document.getElementById(this.id_openbox + "-section").style.display = "none";
        this.id_openbox = "";
    }

    this.save = function() {
        if (this.id_openbox) {
            this.cookie.set(this.id, this.id_openbox);
        } else {
            this.cookie.del(this.id);
        }
    }

    this.load = function() {
        var id_openbox = this.cookie.get(this.id);
        if (id_openbox) {
            this.show(id_openbox);
            document.getElementById(id_openbox).className = "box1-open";
        }
    }

    function Cookie() {
        this.get = function(name) {
            var cookies = document.cookie.split(";");
            for (var i = 0; i < cookies.length; i++) {
                var a = cookies[i].split("=");
                if (a.length == 2) {
                    a[0] = a[0].trim();
                    a[1] = a[1].trim();
                    if (a[0] == name) {
                        return unescape(a[1]);
                    }
                }
            }
            return "";
        }
        this.set = function(name, value) {
            document.cookie = name + "=" + escape(value);
        }
        this.del = function(name) {
            document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }

    var self = this;
    this.id = id;
    this.tree = new Array();
    this.cookie = new Cookie();
    this.id_openbox = "";
    this.init();
}
if (typeof String.prototype.trim == "undefined") {
    String.prototype.trim = function() {
    var s = this.replace(/^\s*/, "");
	    return s.replace(/\s*$/, "");
    }
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}
