/* Add InsertAdjacentHTML for non-IE browsers */
if(typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function
(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function
(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
/* End Add InsertAdjacentHTML for non-IE browsers */

var errorpopup = {};
errorpopup.container = "";
errorpopup.alert     = "";
errorpopup.message   = "";
errorpopup.parameters = {
  top:210,
  bottom:0
};

errorpopup.isCreated = false;
errorpopup.msgqueue  = new Array();
errorpopup.bHideSelect = false;

agt = navigator.userAgent.toLowerCase()
errorpopup.is_ie 	= ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
errorpopup.is_gecko 	= (agt.indexOf('gecko') != -1);
errorpopup.is_nav  	= ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

errorpopup.create = function() {
  var mainContainer = document.getElementById('contentContainer');
  if (mainContainer==null)
	mainContainer = document.getElementById('maincontainer');
  
  if (errorpopup.isCreated == false) {
    var alertContainer = "";
    alertContainer += '<div id="cpAlertcontainer" style="display:none;"></div>';
    alertContainer += '<div id="cpAlert" style="display:none;">';
    alertContainer += '<div class="cpAlertBox">';
    alertContainer += '<h4>'+ labels.lbl_alert_title +'<a href="#"  onClick="errorpopup.close()"><span>close</span></a></h4>';
    alertContainer += '<h2 id="cpAlertMessage"></h2>';
    alertContainer += '<div class="button"><a href="#" onClick="errorpopup.close()"><span>Ok</span></a></div>';
    alertContainer += '</div>';
    alertContainer += '</div>';

	//    if (errorpopup.is_ie) mainContainer.insertAdjacentHTML('beforeEnd', alertContainer);
	//    else mainContainer.innerHTML += alertContainer;// + mainContainer.innerHTML;
	mainContainer.insertAdjacentHTML('beforeEnd', alertContainer);

    errorpopup.container = document.getElementById("cpAlertcontainer");
    errorpopup.alert     = document.getElementById("cpAlert");
    errorpopup.message   = document.getElementById("cpAlertMessage");
    errorpopup.isCreated = true;
  } else {
    // @TODO: ADD check to really be sure that the div is already written.
  }
}

errorpopup.setHideSelect = function ( newBool ) {
  errorpopup.bHideSelect = newBool;
}

errorpopup.addLoadEvent = function(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function')  window.onload = func;
  else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

errorpopup.setAlert = function (string) {
  var iIndex = errorpopup.msgqueue.length;
  string = string.replace("<red>", "<span class='red'>").replace("</red>", "</span>");
  errorpopup.msgqueue[iIndex] = string;
}

errorpopup.getAlert = function() {
  var sReturn = "";
  if (errorpopup.msgqueue.length > 0) {
    sReturn = errorpopup.msgqueue.shift();
  }
  return sReturn;
}

errorpopup.setParameters = function ( params ) {
  if (params != null) {
    if (typeof params.top == 'number') errorpopup.parameters.top = params.top;
    if (typeof params.bottom == 'number') errorpopup.parameters.bottom = params.bottom;
  }
}
errorpopup.updateStyle = function() {
  errorpopup.alert.style['top']     = "";
  errorpopup.alert.style['bottom']  = "";

  if ( errorpopup.parameters.bottom > 0 ) errorpopup.alert.style['bottom']  = errorpopup.parameters.bottom+"px";
  else errorpopup.alert.style['top']     = errorpopup.parameters.top+"px";
}

errorpopup.close = function() {
  var dropdowns = document.getElementsByTagName("SELECT");
  if (dropdowns.length > 0 && errorpopup.is_ie && errorpopup.bHideSelect) { // && errorpopup.bHideSelect(SCD-1333)
    var i = 0;
    while (i < dropdowns.length){
      var element = document.getElementById(dropdowns[i++].id);
      element.style['display'] = 'block';
	}
  }

  errorpopup.container.style['display'] = "none";
  errorpopup.alert.style['display'] = "none";

  if (errorpopup.msgqueue.length > 0) {
   errorpopup.show();
  }
}

errorpopup.show = function() {
  if (errorpopup.isCreated && errorpopup.msgqueue.length > 0) {
    var dropdowns = document.getElementsByTagName("SELECT");
    if (dropdowns.length > 0 && errorpopup.is_ie && errorpopup.bHideSelect) {  // && errorpopup.bHideSelect  (SCD-1333)
    var i = 0;
	    while (i < dropdowns.length){
	      var element = document.getElementById(dropdowns[i++].id);
	      element.style['display'] = 'none';
		}
    }
    errorpopup.updateStyle();
    errorpopup.message.innerHTML = errorpopup.getAlert();
    errorpopup.container.style['display'] = "block";
    errorpopup.alert.style['display'] = "block";
  }
}

cpAlert = function(string, parameters) {
  var params = (typeof parameters == 'object') ? parameters : null;
  errorpopup.setAlert(string);
  errorpopup.setParameters(params);
  errorpopup.show();
}

errorpopup.initialize = function() {
  errorpopup.create();
  errorpopup.show();
}

errorpopup.addLoadEvent(errorpopup.initialize);


