/*
 * $Header: e:/cvs/cvsDepot/smart4_dev/tools/source/tools/portal/portal-components/home/portal/std/scripts/dialogBox.js,v 1.8 2007/01/31 22:33:54 mpereira Exp $
 *
 * Copyright (c) 2004 - EXICA
 * 50 Queen Street, Montreal, Quebec, Canada, H3C2N5.
 * All rights reserved.
 *
 * @author alord
*/

//Takes an object as parameter.
//The object MUST define ".message" - The pop-up message
//The object CAN define ".title" - The pop-up title - Default is ""
//The object CAN define ".lang" - The language the pop-up should display the buttons in - Default is ""
//The object CAN define ".height" - The height of the dialog box - Default is 200 px
//The object CAN define ".width" - the width of the dialog box - Default is 200 px

var popUpOpened = false;
var popUpHeight = null;
var popUpWidth = null;
var popUpReturnFct = null;
var popUpObj = null;

function resetPopUpPosition(){
	if(!popUpOpened){
		return;
	}

	var pdo = elementFinder('popUpDO');
	var pi = elementFinder('popUI');
	var wHeight = 0;
	var wWidth = 0;	
	
	var sHeight = 0;
	var sWidth = 0;
	
  if (self.innerHeight){
      wHeight = self.innerHeight;
      wWidth = self.innerWidth;   
      sHeight = self.pageYOffset;
      sWidth = self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop != null) {
      wHeight = document.documentElement.offsetHeight;
      wWidth = document.documentElement.offsetWidth;    
      sHeight = document.documentElement.scrollTop;
      sWidth = document.documentElement.scrollLeft;
  } else {
      wHeight = document.body.offsetHeight;
      wWidth = document.body.offsetWidth;   
      sHeight = document.body.scrollTop;
      sWidth = document.body.scrollLeft;  
  }
	
	// Get the popUp size information
	var pHeight = popUpHeight;
	var pWidth = popUpWidth;
	
	if(pHeight == null){
        pHeight = 500;
    } else if(pHeight > wHeight) {
        pHeight = wHeight;
    }
	
	if(pWidth == null){
        pWidth = 700;
    } else if(pWidth > wWidth) {
        pWidth = wWidth;
    }
	
	// Set the properties
    pdo.style.width = ( pWidth  ) + "px";
	pdo.style.height = ( pHeight ) + "px";
	pdo.style.top = ( sHeight + wHeight/2 - pHeight/2 ) + "px";
	pdo.style.left = ( sWidth + wWidth/2 - pWidth/2 ) + "px";
	
	pi.style.width = ( pdo.offsetWidth) + "px";
    pi.style.height = ( pdo.offsetHeight) + "px";
    pi.style.top = ( pdo.offsetTop ) + "px";
    pi.style.left = ( pdo.offsetLeft ) + "px";

    setTimeout("resetPopUpPosition();", 70);
}

function showPopUp(url, width, height, rtFct){
	popUpHeight = height;
	popUpWidth = width;

    if(rtFct==null) {
      popUpReturnFct = emptyReturnFunction;
    }
    else {
      popUpReturnFct = rtFct;
    }
	
    var pwo = elementFinder('popUpWO');
	var pdo = elementFinder('popUpDO');
	var pi = elementFinder('popUI');

	popUpOpened = true;
	
	// Loop to disable the input fields
	for(i = 0; i < document.forms.length; i ++){
		for(j = 0; j < document.forms[i].elements.length; j ++){
			if(document.forms[i].elements[j].disabled != null && !document.forms[i].elements[j].disabled){
				document.forms[i].elements[j].popUpdisabled = true;
				document.forms[i].elements[j].disabled = true;
				
				if(document.forms[i].elements[j].type == "select-one"){
					document.forms[i].elements[j].style.visibility = "hidden";
				}
			}
		}
	}
	
	// Block out content DIV
	pwo.style.left = "0px";
	pwo.style.top = "0px";
	
	pwo.style.MozOpacity = "0.5";
	pwo.style.opacity = "0.5";
	
  if (self.innerHeight)   {
      // all except Explorer
      pwo.style.width = "100%";
      pwo.style.height = self.innerHeigh+"px";
  }
  else if (document.documentElement && document.documentElement.scrollTop != null) {
      pwo.style.height = (document.documentElement.scrollHeight-5) + "px";
      pwo.style.width = "100%";
  } else {
      pwo.style.height = (document.body.scrollHeight-5) + "px";
      pwo.style.width = "100%"; 
  }

  // Compute height
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight
  if (test1 > test2) // all but Explorer Mac
  {
	pwo.style.height = document.body.scrollHeight+"px";
  }
  else // Explorer Mac;
      //would also work in Explorer 6 Strict, Mozilla and Safari
  {
	pwo.style.height = document.body.offsetHeight+"px";
  }

	pwo.style.visibility = "visible";
	
	// Get the size information used to display
	var wHeight = 0;
	var wWidth = 0;	
	
	var sHeight = 0;
	var sWidth = 0;
	
  if (self.innerHeight){
      wHeight = self.innerHeight;
      wWidth = self.innerWidth;   
      sHeight = self.pageYOffset;
      sWidth = self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop != null) {
      wHeight = document.documentElement.offsetHeight;
      wWidth = document.documentElement.offsetWidth;    
      sHeight = document.documentElement.scrollTop;
      sWidth = document.documentElement.scrollLeft;
  } else {
      wHeight = document.body.offsetHeight;
      wWidth = document.body.offsetWidth;   
      sHeight = document.body.scrollTop;
      sWidth = document.body.scrollLeft;
  }
	
	// Get the popUp size information
	var pHeight = height;
	var pWidth = width;
	
	if(pHeight == null){
        pHeight = 500;
    } else if(pHeight > wHeight) {
        pHeight = wHeight;
    }
	
	if(pWidth == null){
        pWidth = 700;
    } else if(pWidth > wWidth) {
        pWidth = wWidth;
    }
	
	// Set the properties
    pdo.style.width = ( pWidth  ) + "px";
	pdo.style.height = ( pHeight  ) + "px";
	pdo.style.top = ( sHeight + wHeight/2 - pHeight/2 ) + "px";
	pdo.style.left = ( sWidth + wWidth/2 - pWidth/2) + "px";
	pdo.style.visibility = 'visible';
	
	pi.style.width = ( pdo.offsetWidth  )+ "px";
    pi.style.height = ( pdo.offsetHeight) + "px";
    pi.style.top = ( pdo.offsetTop  ) + "px";
    pi.style.left = ( pdo.offsetLeft ) + "px";
    pi.style.visibility = 'visible';
    pi.style.display = 'block';

	pi.src = url;
	
	setTimeout("resetPopUpPosition();", 70);
}

function reenableParentPopUp(){
    if(!popUpOpened){
	    return;
	}

	// Loop to enable the input fields
	for(i = 0; i < document.forms.length; i ++){
		for(j = 0; j < document.forms[i].elements.length; j ++){
			if(document.forms[i].elements[j].popUpdisabled){
				document.forms[i].elements[j].popUpdisabled = null;
				document.forms[i].elements[j].disabled = false;
				
				if(document.forms[i].elements[j].type == "select-one"){
					document.forms[i].elements[j].style.visibility = "visible";
				}
			}
		}
	}
}

function hidePopUp(){
    if(!popUpOpened){
	    return;
	}
	
	popUpOpened = false;
    popUpHeight = null;
    popUpWidth = null;
	popUpReturnFct = null;
	popUpObj = null;

    var pwo = elementFinder('popUpWO');
	var pdo = elementFinder('popUpDO');
	var pi = elementFinder('popUI');
	
	pwo.style.visibility = 'hidden';

	pwo.style.MozOpacity = null;
	pwo.style.opacity = null;

	pdo.style.visibility = 'hidden';
	pi.style.visibility = 'hidden';
	pi.style.display = 'none';

	// BLN - Revert to the please wait source
    pi.src = "/blank.html";
}

// urlBox, displays the given url in a new window.
// This dialog is the exception as it doesn't take the regular object parameter of the other boxes
// Doesn't return anything
function urlBox(url,width,height){
	showPopUp(url, width, height, null);
}

// urlBox, displays the given url in a new window.
// This dialog is the exception as it doesn't take the regular object parameter of the other boxes
// The displayed window can call the specified return function
function urlBoxWithReturn(url,width,height, rtFct){
	showPopUp(url, width, height, rtFct);
}

//Alert dialog.
//Returns true if closed properly, null otherwise
function alertBox(obj, rtFct){
    popUpObj = obj;
	showPopUp(portalResourcesUrl + "scripts/dialog/alertBox.jsp", obj.width, obj.height, rtFct);
}

//Confirmation dialog
//Return true is "ok" is pressed, null otherwise
function confirmBox(obj, rtFct){
    popUpObj = obj;
	showPopUp(portalResourcesUrl + "scripts/dialog/confirmBox.jsp", obj.width, obj.height, rtFct);
}

//Yes no dialog
// Returns true if "yes", false is "no" and null otherwise
function yesNoBox(obj, rtFct){
    popUpObj = obj;
	showPopUp(portalResourcesUrl + "scripts/dialog/yesNoBox.jsp", obj.width, obj.height, rtFct);
}

//Yes no cancel dialog
//Returns true if "yes", false is "no", null if "cancel" and null otherwise
function yesNoCancelBox(obj, rtFct){
    popUpObj = obj;
	showPopUp(portalResourcesUrl + "scripts/dialog/yesNoCancelBox.jsp", obj.width, obj.height, rtFct);
}

//Edit box
//Returns the value if "ok", null otherwise
//It CAN take ".value" as an initial value for the field
function editBox(obj, rtFct){
    popUpObj = obj;
	showPopUp(portalResourcesUrl + "scripts/dialog/editBox.jsp", obj.width, obj.height, rtFct);
}

//printbox
//Returns null no matter what
function printBox(obj){
    popUpObj = obj;
	showPopUp(portalResourcesUrl + "scripts/dialog/print.jsp", obj.width, obj.height, null);
}

// Empty return function if null is given for return values
function emptyReturnFunction(returnValue) {
}

function refresh(){
	location.reload(true);
}

