//Collection of javascript included in every page header

//OnLoad hook-up mechanism

function jsQSOnLoad() {
  //Check if a custom onload function is defined
  if (window.jsCustomOnLoad) {
    jsCustomOnLoad();    
  }
}

function jsHookUpOnLoad(funCustomOnLoad) {
  // DOM2
  if ( typeof window.addEventListener != "undefined" )
	  window.addEventListener( "load", funCustomOnLoad, false );

  // IE 
  else if ( typeof window.attachEvent != "undefined" )
	  window.attachEvent( "onload", funCustomOnLoad );

  else {
	  if ( window.onload != null ) {
		  var oldOnload = window.onload;
		  window.onload = function ( e ) {
			  oldOnload( e );
			  funCustomOnLoad();
		  };
	  }
	  else 
		  window.onload = funCustomOnLoad;
  }
}

//open_window : no more used. Is here for compatibility reasons -> probably in swissmedia project
function open_window(url) { 
  var corpagri
  corpagri = window.open(url,"QuickSitePortal",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=640,height=480'); 
} 

function jsOpenWindow640480(pstrUrl) {
  var objWindow
  objWindow = window.open(pstrUrl, "QuickSite",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=640,height=480'); 
}

function jsOpenWindow(pstrUrl, pintWidth, pintHeight) {
  var objWindow
  objWindow = window.open(pstrUrl, "_blank",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + pintWidth + ',height=' + pintHeight); 
  //window.showModalDialog(pstrUrl, "", "dialogHeight:" + pintHeight + "px; dialogWidth:" + pintWidth + "px;")
}

function jsQuickLogin() {    
  if (gobjSessionVar.lngMemberID == 0) {
    if (typeof CMenuPopUp != "undefined") {  
      CMenuPopUp('QuickLoginMenu', window.event);
      if (window.event) {
        //QSP396d : cancels any other event handler
        window.event.returnValue = false;
      }
      return false;
    }
  } else {
    if (typeof CMenuPopUp != "undefined") {  
      CMenuPopUp('QuickLogoutMenu', window.event);
      if (window.event) {
        //QSP396d : cancels any other event handler
        window.event.returnValue = false;
      }
      return false;
    }  
  }
  return true;
}

var IFrameObj; // our IFrame object

function CallToServer(URL) {
  if (!document.createElement) {return true};
  var IFrameDoc;
  if (!IFrameObj && document.createElement) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // callToServer() is called
   try {
      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id','RSIFrame');
      tempIFrame.style.border='0px';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);
      
      if (document.frames) {
        // this is for IE5 Mac, because it will only
        // allow access to the document object
        // of the IFrame if we access it through
        // the document.frames array
        IFrameObj = document.frames['RSIFrame'];
      }
    } catch(exception) {
      // This is for IE5 PC, which does not allow dynamic creation
      // and manipulation of an iframe object. Instead, we'll fake
      // it up by creating our own objects.
      iframeHTML='\<iframe id="RSIFrame" style="';
      iframeHTML+='border:0px;';
      iframeHTML+='width:0px;';
      iframeHTML+='height:0px;';
      iframeHTML+='"><\/iframe>';
      document.body.innerHTML+=iframeHTML;
      IFrameObj = new Object();
      IFrameObj.document = new Object();
      IFrameObj.document.location = new Object();
      IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
      IFrameObj.document.location.replace = function(location) {
        this.iframe.src = location;
      }
    }
  }
  
  if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
    // we have to give NS6 a fraction of a second
    // to recognize the new IFrame
    setTimeout('callToServer()',10);
    return false;
  }
  
  if (IFrameObj.contentDocument) {
    // For NS6
    IFrameDoc = IFrameObj.contentDocument; 
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
    IFrameDoc = IFrameObj.contentWindow.document;
  } else if (IFrameObj.document) {
    // For IE5
    IFrameDoc = IFrameObj.document;
  } else {
    return true;
  }
  
  IFrameDoc.location.replace(URL);
  return false;
}

function buildQueryString(theFormName) {
  theForm = document.forms[theFormName];
  var qs = ''
  for (e=0;e<theForm.elements.length;e++) {
    if (theForm.elements[e].name!='') {
      qs+=(qs=='')?'?':'&'
      qs+=theForm.elements[e].name+'='+escape(theForm.elements[e].value)
      }
    }
  return qs
}

function jsSetCookie(pstrName, pstrValue, pintNbDays) {
	var expires = "";
	
	if (pintNbDays != 0) {
	  var d = new Date();
	  d.setTime( d.getTime() + pintNbDays * 24 * 60 * 60 * 1000 );
	  expires = "; expires=" + d.toGMTString();
	}

	document.cookie = pstrName + "=" + escape(pstrValue) + expires + "; path=/";
}

function jsGetCookie(pstrName) {

	var search;
	var offset;
	var end;

	search = pstrName + "="
	offset = document.cookie.indexOf(search) 
	if (offset != -1) {
		offset += search.length ;
		end = document.cookie.indexOf(";", offset) ;
		if (end == -1)
		  end = document.cookie.length;
		return unescape(document.cookie.substring(offset, end));
	}
	else
		return "";
}					

function jsRemoveCookie(pstrName) {
	jsSetCookie(pstrName, '', -1 );
}

function jsPlaySong(pstrContainer, pstrUrl) {
  var objContainer = document.getElementById(pstrContainer);
  if (objContainer) {
    objContainer.innerHTML = embedSound(pstrUrl); 
  }
  return false;
}

function embedSound(sound)
{
  return '<embed src="' + sound + '" autostart="true" ' +
    'width="320" height="32">';
}

function jsToggleVisibility(pstrDivID) {
	var e = document.getElementById(pstrDivID);
	if (e) {
		if (e.style.display == 'none') 
			e.style.display = 'block'
		else
			e.style.display = 'none'
			
	}
}

function jsOEA(b, c, a) {
  var e = a + "@" + b + "." + c;
  document.write("<a href='mai" + "lto:"+e+"'>");
  document.write(e);
  document.write("</a>"); 
};

function IsNumeric(sText)
{
   var ValidChars = "0123456789.-";
   var IsNumber=true && (sText.length > 0);
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function GetNumber(sText) {
	if (IsNumeric(sText)) {
		if (sText.indexOf('.') == -1)
			return parseInt(sText);	
		else
			return parseFloat(sText);
	}
	else {
	  return 0;
	}
}

//Add the trim function to the string object
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, "")};
