// Unescapes html tags in the str by using
// browser's internal html escape feature.
function unescapeHTML(str) {
   if(str == null || str == "")
      return "";
	
   var htmlNode = document.createElement("DIV");
   htmlNode.innerHTML = str;
   
   if(htmlNode.innerText)
      return htmlNode.innerText; // IE
   return htmlNode.textContent; // FF
}

// Show message box relative to passed object
//
// "unescapeStr" parameter should be set true, if this method is being used between javascript/script tags.
// By doing this, we guarantee that "sTitle" and "sMessage" will be unescaped for desired situations and 
// so that, they will be displayed properly.
function showmessage(obj,sTitle,sMessage, unescapeStr,bArrow,widthadj,heightadj){	
	if(unescapeStr != null && unescapeStr != "" && unescapeStr) {
		sTitle = unescapeHTML(sTitle);
		sMessage = unescapeHTML(sMessage);
	}
	
	var objmsg = document.getElementById('cntShowMessage');
	// create message div if it does not yet exist
	if (objmsg == null){

		objmsg = document.createElement('div');
		objmsg.setAttribute('id', 'cntShowMessage');
		objmsg.innerHTML = 
			'<div class="in">' + 
				'<div class="top">' + 
					'<a href="#" onclick="javascript:document.getElementById(\'cntShowMessage\').style.display=\'none\';return false;">x</a>' + 
				'</div>' + 
				'<div class="in2">' + 
					'<div class="body">' + 
						'<h1 id="cntShowMessageTitle"></h1>' + 
						'<div id="cntShowMessageText"></div>' + 
					'</div>' + 
				'</div>' + 
				'<div class="bot">&nbsp;</div>' + 
			'</div>';
		document.body.appendChild(objmsg);
		objmsg = document.getElementById('cntShowMessage');
	}

	// Show arrow if applicable
	objmsg.className = (bArrow)?objmsg.className = 'cntShowMessageArrow':'';

	// Set message content
	document.getElementById('cntShowMessageTitle').innerHTML = sTitle;
	document.getElementById('cntShowMessageText').innerHTML = sMessage;

	// position it
	var pos = ElementPosition.get(obj);
	if (bArrow)	ElementPosition.set(objmsg,pos.left+pos.width+widthadj, pos.top+heightadj);
	else ElementPosition.set(objmsg,pos.left+pos.width+10, pos.top-30);

	// show it
	objmsg.style.display='block';
}

/**
 * Copyright (c)2005-2007 Matt Kruse (javascripttoolbox.com)
 * 
 * Dual licensed under the MIT and GPL licenses. 
 * This basically means you can use this code however you want for
 * free, but don't claim to have written it yourself!
 * Donations always accepted: http://www.JavascriptToolbox.com/donate/
 * 
 * Please do not link to the .js files on javascripttoolbox.com from
 * your site. Copy the files locally to your server instead.
 * 
 */
var ElementPosition = (function() {
  // Resolve a string identifier to an object
  // ========================================
  function resolveObject(s) {
    if (document.getElementById && document.getElementById(s)!=null) {
      return document.getElementById(s);
    }
    else if (document.all && document.all[s]!=null) {
      return document.all[s];
    }
    else if (document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x) {
      for (var i=0; i<document.anchors.length; i++) {
        if (document.anchors[i].name==s) { 
          return document.anchors[i]
        }
      }
    }
  }
  
  var pos = {};
  pos.$VERSION = 1.0;
  
  // Set the position of an object
  // =============================
  pos.set = function(o,left,top) {
    if (typeof(o)=="string") {
      o = resolveObject(o);
    }
    if (o==null || !o.style) {
      return false;
    }
    
    // If the second parameter is an object, it is assumed to be the result of getPosition()
    if (typeof(left)=="object") {
      var pos = left;
      left = pos.left;
      top = pos.top;
    }
    
    o.style.left = left + "px";
    o.style.top = top + "px";
    return true;
  };
  
  // Retrieve the position and size of an object
  // ===========================================
  pos.get = function(o) {
    var fixBrowserQuirks = true;
      // If a string is passed in instead of an object ref, resolve it
    if (typeof(o)=="string") {
      o = resolveObject(o);
    }

    if (o==null) {
      return null;
    }

    var left = 0;
    var top = 0;
    var width = 0;
    var height = 0;
    var parentNode = null;
    var offsetParent = null;

    offsetParent = o.offsetParent;
    var originalObject = o;
    var el = o; // "el" will be nodes as we walk up, "o" will be saved for offsetParent references
    while (el.parentNode!=null) {
      el = el.parentNode;
      if (el.offsetParent==null) {
      }
      else {
        var considerScroll = true;
        /*
        In Opera, if parentNode of the first object is scrollable, then offsetLeft/offsetTop already 
        take its scroll position into account. If elements further up the chain are scrollable, their 
        scroll offsets still need to be added in. And for some reason, TR nodes have a scrolltop value
        which must be ignored.
        */
        if (fixBrowserQuirks && window.opera) {
          if (el==originalObject.parentNode || el.nodeName=="TR") {
            considerScroll = false;
          }
        }
        if (considerScroll) {
          if (el.scrollTop && el.scrollTop>0) {
            top -= el.scrollTop;
          }
          if (el.scrollLeft && el.scrollLeft>0) {
            left -= el.scrollLeft;
          }
        }
      }
      // If this node is also the offsetParent, add on the offsets and reset to the new offsetParent
      if (el == offsetParent) {
        left += o.offsetLeft;
        if (el.clientLeft && el.nodeName!="TABLE") { 
          left += el.clientLeft;
        }
        top += o.offsetTop;
        if (el.clientTop && el.nodeName!="TABLE") {
          top += el.clientTop;
        }
        o = el;
        if (o.offsetParent==null) {
          if (o.offsetLeft) {
            left += o.offsetLeft;
          }
          if (o.offsetTop) {
            top += o.offsetTop;
          }
        }
        offsetParent = o.offsetParent;
      }
    }

    if (originalObject.offsetWidth) {
      width = originalObject.offsetWidth;
    }
    if (originalObject.offsetHeight) {
      height = originalObject.offsetHeight;
    }

    return {'left':left, 'top':top, 'width':width, 'height':height
        };
  };
  
  // Retrieve the position of an object's center point
  // =================================================
  pos.getCenter = function(o) {
    var c = this.get(o);
    if (c==null) { return null; }
    c.left = c.left + (c.width/2);
    c.top = c.top + (c.height/2);
    return c;
  };
  
  return pos;
})();

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// Show message box relative to passed object
function showmodal(objhtmlsource,lclTextClose){
	var modaltransparency = document.getElementById('modaltransparency');

	// create modal transparency div if it does not yet exist
	if (modaltransparency == null){
		modaltransparency = document.createElement('div');
		modaltransparency.setAttribute('id', 'modaltransparency');
		document.body.appendChild(modaltransparency);
		modaltransparency = document.getElementById('modaltransparency');
	}
	var arrayPageSize = getPageSize();
	modaltransparency.style.height = (arrayPageSize[1] + 'px');

	// if ie6, hide all select elements
	toggleselect('none');

	modaltransparency.style.display = 'block';

	// show popup
	var modalbox = document.getElementById('modalbox');

	// create modal box div if it does not yet exist
	if (modalbox == null){
		modalbox = document.createElement('div');
		modalbox.setAttribute('id', 'modalbox');
		document.body.appendChild(modalbox);
		modalbox = document.getElementById('modalbox');
	}
	else{
		modalbox.style.display = 'block';
	}

	//center popup
	var arrayPageSize = getPageSize();
	var pos = ElementPosition.get(modalbox)
	var heightadjustie6 = (navigator.userAgent.indexOf('IE 6') > -1)?document.documentElement.scrollTop:0;
	ElementPosition.set(modalbox,(arrayPageSize[2]/2)-(pos.width/2),heightadjustie6+((arrayPageSize[3]/2)-(pos.height/2)));

	// set contents
	modalbox.innerHTML = '<p class=\'close\'><a href=\'#\' onclick=\'hidemodal();return false;\'>' + lclTextClose + '&nbsp;&nbsp;X</a></p>' + 
		'<div class=\'scroll\'>' + document.getElementById(objhtmlsource).innerHTML + '</div>';

}

function hidemodal(){
	document.getElementById('modaltransparency').style.display = 'none';
	toggleselect('');
	document.getElementById('modalbox').style.display = 'none';
}

// Hide or show all select elements on the page as they show through the modal background in ie6
function toggleselect(sDisplay){
	if (navigator.userAgent.indexOf('IE 6') > -1){
		var objselitems = document.getElementsByTagName('select');
		for (i=0;i<objselitems.length;i++) objselitems[i].style.display=sDisplay;
	}
}