function showAlternateImages() {
  var cvr = document.getElementById("cover");
  var dlg = document.getElementById("alternateImages");

  cvr.style.display = "block";
  dlg.style.display = "block";

  var iframe = document.getElementById("altImageIframe");
  
  var contentHeight = iframe.contentWindow.document.body.scrollHeight;

  iframe.style.height = (contentHeight + 10) + "px";
  
  var dlgHeight = contentHeight + 40;
  
  dlg.style.height = dlgHeight + "px";
  
  var height = parseInt(document.documentElement.clientHeight);
  var width = parseInt(document.documentElement.clientWidth);

  dlg.style.left = parseInt(( width / 2 ) - 150) + "px";
  dlg.style.top = parseInt(( height / 2 ) - (dlgHeight / 2) - 20) + "px";

  cvr.style.width = width + "px";
  cvr.style.height = height + "px";

  return false;
}

function hideAlternateImages() {
  var alternateImages = document.getElementById("alternateImages");
  var cvr = document.getElementById("cover");
  alternateImages.style.display = "none";
  cvr.style.display = "none"
  return false;
}

function treeToggleDisplay(portletId, sectionIndex) {
  links = document.getElementById("sectionLinks" + portletId + sectionIndex);
  bullet = document.getElementById("sectionBullet" + portletId + sectionIndex);
  
  if (links.style.display == "block")
  {
    bullet.src = SKIN_PATH + "/images/product/icon_plus.gif";
    links.style.display = "none";
  }
  else
  {
    bullet.src = SKIN_PATH + "/images/product/icon_minus.gif";
    links.style.display = "block";
  }
}

function performSearch(form) {
  var searchUrl = form.action;
  searchUrl += "?query=" + escape(form['query'].value);
  window.location = searchUrl;
  
  return false;
}

function showSessionTimeoutMessage() {
  var cover = document.getElementById("cover");
  var dlg = document.getElementById("sessionTimeoutMessage");

  cover.style.display = "block";
  dlg.style.display = "block";

  var dlgHeight = dlg.offsetHeight;
  var dlgWidth = dlg.offsetWidth;
  
  dlg.style.height = dlgHeight + "px";
  
  var height = parseInt(document.documentElement.clientHeight);
  var width = parseInt(document.documentElement.clientWidth);
  
  dlg.style.left = parseInt(( width / 2 ) - (dlgWidth  / 2)) + "px";
  dlg.style.top = parseInt(( height / 2 ) - (dlgHeight / 2) - 20) + "px";
  
  var coverHeight = document.documentElement.clientHeight;
  
  if (document.documentElement.scrollHeight > coverHeight)
    coverHeight = document.documentElement.scrollHeight;

  cover.style.width = width + "px";
  cover.style.height = coverHeight + "px";

  document.body.style.overflow = "hidden";

  return false;
}

function startSessionTimeoutTimer() {
  if (sessionTimeout > 0)
    setTimeout("showSessionTimeoutMessage();", sessionTimeout);
}

// Rewrite url to https if url is currently http.
// Note: port number is also removed if part of the url
function rewriteUrlForLoggedInUser() {
  try {
    var url = location.href;
			  			  
    if (url.search(/^http:.+$/i) != -1) {
	  var portExpr = new RegExp("^https://.+(:\\d+)/.+$");
			    
      url = url.replace('http:', 'https:');
      var matches = url.match(portExpr);
			    
      if (matches != null)
        url = url.replace(matches[1], "");
			      
        location.href = url;
      }
  } catch (err) {
    // do nothing
  }
}

// Limit the text area field to a max number of characters found in the class attr
function limitTextArea(event) {  
  var key;
  if (event.which) {
    key = event.which;
  } else {
    key = event.keyCode;
  }
  // catch most keys, including enter, but not delete or arrow keys
  if ((key >= 32 || key == 13) && key != 46 && key != 37 && key != 38 && key != 39 && key != 40) {
    var limit = $(this).attr("class");
    if ($(this).val().length >= limit) {
      event.preventDefault();
    }
  }
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/ 
var UrlEncoding = { 
  // public method for url encoding
  encode : function (string) {
    return escape(this._utf8_encode(string));
  },
 
  // public method for url decoding
  decode : function (string) {
    // replace + signs with %20 so unescape can convert to a space
    return this._utf8_decode(unescape(string.replace(/\+/g, "%20")));
  },
 
  // private method for UTF-8 encoding
  _utf8_encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
 
    for (var n = 0; n < string.length; n++) { 
      var c = string.charCodeAt(n);
 
      if (c < 128) {
        utftext += String.fromCharCode(c);
      }
      else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
    }
 
    return utftext;
  },
 
  // private method for UTF-8 decoding
  _utf8_decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
 
    while ( i < utftext.length ) { 
      c = utftext.charCodeAt(i);
 
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      }
      else if((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i+1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else {
        c2 = utftext.charCodeAt(i+1);
        c3 = utftext.charCodeAt(i+2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      } 
    }
 
    return string;
  }
}
