/*
  $Id: commonFunctions.js,v 1.17 2008/10/17 13:25:07 Tomas Exp $
  
  *************************************************
  *** common javascript functions               ***
  *************************************************
*/


// The funcion opens a new window. URL, name and windowFeatures are arguments.
// The function is used in <A HREF="javascript:openNewWindow(...)"> tag, where it is not possible to use the standard window.open() function
function openNewWindow(aUrl, aWindowName, aWindowFeatures) {
  window.open(aUrl, aWindowName,aWindowFeatures);
}

// vString is cut. The last char is the one char before the first one of specified chars next to vMinLength position.
// Specified break chars: '!', '?', ',', ';', '.', '\s' - i.e. white space, '\t' - i.e. tab, '\n' - i.e. new line
function cutString(vString, vMinPositionOfLastChar) {
  if(!vMinPositionOfLastChar) var vMinPositionOfLastChar = 200;
  if (vString) {
    var breakChars = /[!?,;.\s\t\n]/;
    if (vString.length > vMinPositionOfLastChar) {
      var vArrayOfMatchedChars = vString.substring(vMinPositionOfLastChar).match(breakChars);
      if(vArrayOfMatchedChars) var vPosition = vArrayOfMatchedChars.index + vMinPositionOfLastChar;
      else var vPosition = 0;
      if (vPosition>=vMinPositionOfLastChar) {
        if (vString.substring(vPosition-1,vPosition).match(breakChars)) vPosition--;
        vString=vString.substring(0,vPosition);
        vString += "...";
      }
    }
    return vString;
  }
  return "";
}

// The text in specified node should be cut and the html formating shoul be kept.
// The function is available for the browsers, which support DOM only.
function cutFormatedHtmlText(node, vMinPositionOfLastChar) {
  if(!vMinPositionOfLastChar) var vMinPositionOfLastChar = 200;

  // A special recursive function is used for cutting:
  function cutText(n) {

    if (n.nodeType == 1) {// Node.ELEMENT_NODE ... the node is element - not text ... go through the children
      var kids = n.childNodes;
      for (var iii = 0; iii < kids.length; iii++) cutText(kids[iii]);

      if (!isBeforeCutNode && n.tagName=="BR") { // if the node is after cut text node, replace all br tags by span tags
        var nSpan = document.createElement("span");
        var parent = n.parentNode;
        parent.replaceChild(nSpan, n);
      }

    } else if (n.nodeType == 3) { // Node.TEXT_NODE
      vTextLength += n.data.length; // The total text length is sumarised
      if (vTextLength > vMinPositionOfLastChar) {
        if (isBeforeCutNode) { //critical node was fond, the text will be cut with the cutString function 
          var vCurrentString = cutString(n.data, n.data.length - (vTextLength-vMinPositionOfLastChar));
          isBeforeCutNode=false;
        } else { // the nodes next to the critical node are set to empty.
          var vCurrentString = "";
        }

        // replace node
        var newNode = document.createTextNode(vCurrentString);
        var parent = n.parentNode;
        parent.replaceChild(newNode, n);

      }
    }
  }

  // if DOM is supported, cut the text by the cutText function
  if (document.getElementById) {
    var vTextLength=0;
    var isBeforeCutNode = true;
    cutText(node);
  }

}

// set the size of the image to max and keep the proportion of the original image
function setImageSize(docImg,notResizedImg,heightMax,widthMax) {
  if (!docImg || !heightMax || !widthMax) return; //the arguments are necessary

  if (notResizedImg.height!=0 && notResizedImg.width!=0) {
    if (notResizedImg.height/heightMax > notResizedImg.width/widthMax) {
        docImg.height = heightMax;
        docImg.width = Math.round( notResizedImg.width * (heightMax / notResizedImg.height) );
    } else {
        docImg.width = widthMax;
        docImg.height = Math.round( notResizedImg.height * (widthMax / notResizedImg.width) );
    }
  }
}

// The function disables all links in the tree of the appropriate node.
// Href is set to "javascript:;" and className is set to the class, which can be the second argument.
function disableLinksInNode(n, aClass) {
  if (document.getElementById) {
    if (n.nodeType == 1) {// Node.ELEMENT_NODE ... the node is element - not text
      if (n.tagName=="A" && n.href) {
        n.href="javascript:;";
        if (aClass) n.className=aClass;
      }
      else { // go through children
        var kids = n.childNodes;
        for (var iii = 0; iii < kids.length; iii++) disableLinksInNode(kids[iii], aClass);
      }
    }
  }
}

// The function disables all links in the document.
// ClassName is set to the class, which can be the second argument.
function disableAllLinks(aClass) {
  for (var iii1=0; iii1<document.links.length; iii1++) {
    document.links[iii1].href="javascript:;";
    if (aClass) document.links[iii1].className=aClass;
  }
}

// The funcion disable all menu items, navigbar and footer items.
// The function is used in for example in the Team Ordering section
function disableAllMenu() {
  disableAllLinks('submenuLinkPassive');
  if (document.getElementById && document.getElementById("menuLogoLink")) document.getElementById("menuLogoLink").onclick="";
}

// The function sets focus on the appropriate field of the form
function setFocusOnTheForm(form, field) {
  if (arguments.length >= 1) {
    if(arguments.length == 1) var vField = 0;
    else var vField = field;

    if (document.getElementsByName) document.getElementsByName(form)[0].elements[vField].focus();
    else if (document.all) document.all[form].elements[vField].select();
  }
}

// The function scrolls the window top.
// The functionality prepared for the eShop in the iFrame.
function scrollTop() {
	window.scrollTo(0,0);
}

// The function resizes the included iframe (object).
// The functionality prepared for the eShop in the iFrame.
function resizeIframe(pNewW, pNewH, pIfr) {
	var vIfr = (pIfr ? pIfr : document.getElementById("eshopIfr"));
	if (!pNewH) return;
	vIfr.width = pNewW;
	vIfr.height = pNewH;
	// adjust the basic layout
	var vBasketTab = document.getElementById("basketTable");
	var vGreyLineTab = document.getElementById("greyLineTab");
	var vVertLine = document.getElementById("imgRightLine");
	var vGreenLWide = document.getElementById("greenLWide");
	var vConsHeadlineTab = document.getElementById("consHeadlineTab");
	var vConsBoxRightLine = document.getElementById("consBoxRightLine");
	var vVertLineSh = document.getElementById("imgRightLineSh");
	
	if (vBasketTab && vGreyLineTab) {
		var basicIfrW = 693;
		var largeIfrW = 780;
		var basicBasketTW = vConsHeadlineTab ? 758 : 736;
		var basicGLTW = vConsHeadlineTab ? 739 : 717;
		var basHdlTab = 716;
		var basicIfrH = 700;

		if (pNewW < basicIfrW) { // common layout
			basketTable.width = basicBasketTW;
			vGreyLineTab.width = basicGLTW;
			if (vGreenLWide) vGreenLWide.bgColor = "#BEDA03";
			if (vVertLine) vVertLine.style.visibility = "visible";
			if (vConsHeadlineTab) vConsHeadlineTab.width = "100%";
			if (vConsBoxRightLine) vConsBoxRightLine.bgColor = "#DEDEDE";
			if (vVertLineSh) vVertLineSh.style.display = "none";
		} else if (pNewW < largeIfrW) { // slightly wider
			basketTable.width = basicBasketTW + pNewW - basicIfrW;
			vGreyLineTab.width = basicGLTW + pNewW - basicIfrW;
			if (vGreenLWide) vGreenLWide.bgColor = "#BEDA03";
			if (vVertLine) vVertLine.style.visibility = "visible";
			if (vConsHeadlineTab) vConsHeadlineTab.width = "100%";
			if (vConsBoxRightLine) vConsBoxRightLine.bgColor = "#DEDEDE";
			if (vVertLineSh) vVertLineSh.style.display = "none";
		} else { // extremely wider
			basketTable.width = basicBasketTW;
			vGreyLineTab.width = basicGLTW;
			if (vGreenLWide) vGreenLWide.bgColor = "#FFFFFF";
			if (vVertLine) vVertLine.style.visibility = "hidden";
			if (vConsHeadlineTab) vConsHeadlineTab.width = 716;
			if (vConsBoxRightLine) vConsBoxRightLine.bgColor = "#FFFFFF";
			if (vVertLineSh) {vVertLineSh.style.display = "inline";}
			else {
				vVertLineSh = new Image();
				vVertLineSh.id = "imgRightLineSh";
				vVertLineSh.src = vVertLine.src.substring(0, vVertLine.src.indexOf(".jpg"))+"_sh.jpg";
				vVertLineSh.style.position = "absolute";
				vVertLineSh.style.left = "760px";
				vVertLineSh.style.top = "0px";
				vVertLineSh.style.zIndex = "10";
				if(document.getElementById("menuRepresentation") && document.getElementById("menuRepresentation").appendChild) document.getElementById("menuRepresentation").appendChild(vVertLineSh);
			}
		}
	}
	
	if (pNewH > basicIfrH) { // long page without footer
			hideFooterFcs();
	} else {
			showFooterFcs();
	}

}

function hideFooterFcs() {
	var vFooterTab = document.getElementById("footerTab");
	if(vFooterTab) vFooterTab.style.display = "none";
}

function showFooterFcs() {
	var vFooterTab = document.getElementById("footerTab");
	if(vFooterTab) vFooterTab.style.display = "block";
}

