// JavaScript source code
//
//
//

// ---------------------------------------------------------------------
// Open window functions
// ---------------------------------------------------------------------

/**
 * Opens a new window of size width x height with the name windowName and
 * loads the given URL into that window.
 */
function OpenWindow(URL, windowName, width, height) {

	windowFeatures = "dependent=yes, directories=no, location=no, menubar=no, personalbar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no";
	windowFeatures += ", height=" + height;
	windowFeatures += ", width=" + width;
	newwin = window.open(URL, windowName, windowFeatures);
	
	xpos = (screen.availWidth - width) / 2;
	ypos = (screen.availHeight - height) / 2;
	newwin.moveTo(xpos, ypos);
	newwin.focus();
}

/**
 * Opens a new window of size width x height with the name windowName and
 * loads the given URL into that window.
 */
function OpenWindowEx(URL, windowName, width, height, windowFeatures) {

	windowFeatures += ", height=" + height;
	windowFeatures += ", width=" + width;
	newwin = window.open(URL, windowName, windowFeatures);
	
	xpos = (screen.availWidth - width) / 2;
	ypos = (screen.availHeight - height) / 2;
	newwin.moveTo(xpos, ypos);
	newwin.focus();
}

/**
 * 
 */
function OpenDynamicWindowEx(controlId, URL, windowName, width, height, windowFeatures) {

	windowFeatures += ", height=" + height;
	windowFeatures += ", width=" + width;
	
	param = "&dynamicValue=" + controlId;
	
	newwin = window.open(URL + param, windowName, windowFeatures);
	
	xpos = (screen.availWidth - width) / 2;
	ypos = (screen.availHeight - height) / 2;
	newwin.moveTo(xpos, ypos);
	newwin.focus();
}

/**
 * Opens a new maximized window and loads the given URL into that window.
 */
function OpenFullscreenWindow(URL) {
	// Fix the URL so that the right parameters get sent.
	URL = UpdateURLParameter(URL, "height", screen.height - 50);
	URL = UpdateURLParameter(URL, "width", screen.width - 25);
	URL = UpdateURLParameter(URL, "scale", 0);
	URL = UpdateURLParameter(URL, "fullscreen", "true");
	
	// Setup window open features (Note! fullscreen=yes is an IE feature)
	windowFeatures = "dependent=yes, directories=no, fullscreen=yes, location=no, menubar=no, personalbar=no, resizable=yes, scrollbars=yes, status=no, toolbar=no";
	// hence set width and height as well
	windowFeatures += ", height=" + (screen.availHeight - 35);
	windowFeatures += ", width=" + (screen.availWidth - 10);
	
	// Open window and move it to upper Žleft corner of the screen.
	win = window.open(URL, "FullscreenWindow", windowFeatures);
	win.moveTo(0, 0);
	win.focus();
}

/**
 * Update and/or add the given name-value pair to the URL.
 */
function UpdateURLParameter(URL, name, value) {
	re = new RegExp(name + "=[^&]*", "i");
	str = name + "=" + value;
	if (URL.search(re) != -1) {
		URL = URL.replace(re, str);
	} else {
		if( URL.indexOf("?") >= 0){
			URL += "&" + str;
		}
		else{
			URL += "?" + str;
		}
	}
	return URL;
}

// ---------------------------------------------------------------------
// Helper funtions
// ---------------------------------------------------------------------

/**
 * Favorites/Bookmarks helper function.
 */
function AddToFavorite(URL, name) 
{
  if (window.sidebar)
  { // Firefox
    window.sidebar.addPanel(name, URL, "");
  }
  else if (window.external)
  { //MSIE
    window.external.AddFavorite(URL, name);
  }
  else
  {
    alert("Sorry, your web browser does not support this!\nYour address field will be updated so you may bookmark this page.");
    top.location.href = URL;
  }
}

/**
 * Function that checks if the Enter key has be pressed and in that case "clicks" a submit button. Can be called from example a
 * text box's keydown event
 */
function IfEnterClickButton(submitButtonName) {
	if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13))
    {
		document.getElementById(submitButtonName).click();
		return false;
    } 
    else
    {
		return true;
	}
}

function FocusOnFirstEnabledTextBox(){

	var controls = document.forms[0].elements
	
	for(i = 0; i < controls.length; i++)
	{
		var t = controls[i].type;
		
		if (t == 'text')
		{
			if(!controls[i].readOnly)
			{
				controls[i].focus();
				return;
			}
		}
	}
}

// ---------------------------------------------------------------------
// Checkbox list functions
// ---------------------------------------------------------------------

/**
 * Set all the boxes with the same name, with the same value as the 
 * checkAll box.
 * The method assumes that all the checkboxes is in the first form on the
 * page in question.
 *
 * @param checkAll		The checkbox whos value is to be set.
 * @param checkBoxName	The name of the checkboxes to set. Note that the
 *                      whole range of checkboxes must be given the same
 *                      name.
 *
 * @example				<input type="checkbox" value="0" name="checkAllName" onclick="Checkbox_SetBoxes(this, 'checkboxName')">
 */
function Checkbox_SetBoxes(checkAll, checkboxName) 
{
   for (i = 0; i < checkAll.form.length; i++) 
   {
      element = checkAll.form.elements[i];
      if (element.type == 'checkbox' && !element.disabled)
      { 
         element.checked = checkAll.checked; 
      }
   }
}

/**
 * Set the checkAll box as checked if all the checkboxes with the same 
 * name as the given box is checked, otherwise set as unchecked.
 * The method assumes that all the checkboxes is in the first form on the
 * page in question.
 *
 * @param checkbox		A checkbox with the name to verify.
 * @param checkBoxName	The name of the checkbox to set.
 *
 * @example				<input type="checkbox" value="0" name="checkboxName" onclick="Checkbox_SetCheckAll(this, 'checkAllName')">
 */
function Checkbox_SetCheckAll(checkbox, checkAllName)
{
	check = true;
	var checkAll = checkbox.form.elements[checkAllName];
	for (i = 0; i < checkbox.form.length && check == true; i++) 
	{
		element = checkbox.form.elements[i];
		if (element.type == 'checkbox')
		{ 
			if (!element.disabled && element != checkAll && element.checked == false)
			{
				check = false;
			}
		}
	}
	checkAll.checked = check;
}

// ---------------------------------------------------------------------
// JS Function to be called when page is finished loading
// ---------------------------------------------------------------------

function addLoadEvent(func) 
{
  var oldonload = window.onload;
  
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  }
  else
  {
    window.onload = function() 
    {
      oldonload();
      func();
    }
  }
}


// ---------------------------------------------------------------------
// Page dimension functions
// ---------------------------------------------------------------------

/**
 * Javscript object.
 * Container for the page dimensions; width and height.
 */
function PageDimension(width, height) {
	this.width = width;
	this.height = height;
}

/**
 * Retrieves the current pages dimensions (width and height)
 * as a PageDimension object.
 */
function GetPageDimensions() {
	myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if(document.documentElement &&
			  (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if(document.body &&
			  (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}	 
	return new PageDimension(myWidth, myHeight);
}

function ShowERESDialog(mode, objectName, submitButtonId)
{
  if ( this.document.getElementsByName('ERES')[0].value != '1')
  {
    url = 'ElectronicSignaturePage.aspx?' + 'mode=' + mode + '&objectName=' + objectName + '&submitButtonId=' + submitButtonId + '&eresFieldId=' + "ERES";
    OpenWindowEx(url, 'popup', 580,420,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=no');
    return false;
  }
  return true;
}

function ToggleVisibility(target)
{
	obj = this.document.getElementById(target);
	
	if ( obj != null )
	{
	  obj.style.display = ( (obj.style.display=='none' || obj.style.display=='') ? 'block' : 'none');
	}
	
	return;
}

function AddValueToList(selectObject, optionText, optionValue )
{
  if ( !recheck (selectObject, optionText, optionValue) )
  {
    var optionObject = new Option(optionText,optionValue);
    var optionRank = selectObject.options.length;
    selectObject.options[optionRank] = optionObject;
  }
}

function recheck (selectObject, optionText, optionValue){
  var sltbool = false;
  for (i=0; i<selectObject.options.length; i++){
     if (optionText == selectObject.options[i].text ||  optionValue == selectObject.options[i].value ){
        sltbool = true;
        break
     }
  }
  return sltbool;
}

/*******************************************************************
This JavaScript function is used to check/uncheck all checkboxes
in an ASP.NET CheckBoxList control. You cannot add onclick handlers
to individual checkboxes in such a control (known bug). This is a
workaround, call from a check box that is not a member of the
CheckBoxList.

Function:   checkListItems.

Inputs:     checkBoxListId - The id of the checkbox list.
            
            numOfItems - The number of checkboxes in the list.
            
            caller - The object (checkbox) that is calling this
                     function.
            
Purpose:    Checks/unchecks all checkboxes in a CheckBoxList
            depending on the value (.checked) of the caller.
********************************************************************/
function checkListItems(checkBoxListId, numOfItems, caller)
{
    // Get the checkboxlist object.
    objCtrl = document.getElementById(checkBoxListId);
    
    // Does the checkboxlist not exist?
    if(objCtrl == null)
    {
        return;
    }

    var i = 0;
    var objItem = null;
    
    // Get the checkbox to verify.
    var objItemChecked = caller;

    // Does the individual checkbox exist?
    if(objItemChecked == null)
    {
        return;
    }

    // Is the checkbox to verify checked?
    var isChecked = objItemChecked.checked;
    
    // Loop through the checkboxes in the list.
    for(i = 0; i < numOfItems; i++)
    {
        objItem = document.getElementById(checkBoxListId + '_' + i);

        if(objItem == null)
        {
            continue;
        }
        
        // Check/uncheck the checkbox.
        objItem.checked = isChecked;
    }
}

function HideLoading(toHideId)
{
	obj = this.document.getElementById(toHideId);
	
	if ( obj != null )
	{
	  obj.style.display = 'none';
	}
	
	return;
}

function Logout(logoutMessage, logoutPage)
{
    if (logoutMessage != "")
    {
        if (!confirm(logoutMessage))
        {
             return;
        }
    }
    top.location = logoutPage;
}

