/*
 * $Header: /depot/Metropole_site/tools/source/tools/portal/portal-components/home/portal/std/scripts/dialogBox.js,v 1.3 2006/03/28 19:48:26 jtrepanier Exp $
 *
 * Copyright (c) 2004 - EXICA
 * 50 Queen Street, Montreal, Quebec, Canada, H3C2N5.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * EXICA, Inc.("Confidential Information").  You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with EXICA.
 *
 * @author Dliottier, 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 = FIND('popUpDO');
	var pi = FIND('popUI');
	var wHeight = 0;
	var wWidth = 0;	
	
	var sHeight = 0;
	var sWidth = 0;
	
	if (window.innerHeight){
	    wHeight = window.innerHeight;
	    wWidth = window.innerWidth;		
		sHeight = window.pageYOffset;
		sWidth = window.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;
		document.body.scrollLeft = 0;
		document.body.scrollTop = 0;
	} 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 + 2 ) + "px";
	pdo.style.height = ( pHeight + 2 ) + "px";
	pdo.style.top = ( sHeight + wHeight/2 - pHeight/2 - 1) + "px";
	pdo.style.left = ( sWidth + wWidth/2 - pWidth/2 - 1) + "px";
	
	pi.style.width = ( pdo.offsetWidth - 2) + "px";
    pi.style.height = ( pdo.offsetHeight - 2) + "px";
    pi.style.top = ( pdo.offsetTop + 1 ) + "px";
    pi.style.left = ( pdo.offsetLeft + 1 ) + "px";
    
    setTimeout("resetPopUpPosition();", 100);
}

function showPopUp(url, width, height, rtFct){
	popUpHeight = height;
	popUpWidth = width;

    if(rtFct==null) {
      popUpReturnFct = emptyReturnFunction;
    }
    else {
      popUpReturnFct = rtFct;
    }

    var pwo = FIND('popUpWO');
	var pdo = FIND('popUpDO');
	var pi = FIND('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";
				}
			}
		}
	}
	
	//************************
	//Only for Metropole site
	//************************
	for(i = 0; i < document.forms.length; i ++){
		for(j = 0; j < document.forms[i].elements.length; j ++) {
	       if(document.forms[i].name == "AccueilForm") {
	          document.forms[i].flash_accueil.style.visibility = "hidden";
	       }
	    }
	}
	
	// Block out content DIV
	pwo.style.left = "0px";
	pwo.style.top = "0px";
	if (document.documentElement && document.documentElement.scrollTop != null) {
		pwo.style.height = document.documentElement.scrollHeight + "px";
		pwo.style.width = document.documentElement.scrollWidth + "px";
	} else {
		pwo.style.height = document.body.scrollHeight + "px";
		pwo.style.width = document.body.scrollWidth + "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 (window.innerHeight){
	    wHeight = window.innerHeight;
	    wWidth = window.innerWidth;		
		sHeight = window.pageYOffset;
		sWidth = window.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;
		document.body.scrollLeft = 0;
		document.body.scrollTop = 0;
	} 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 + 2 ) + "px";
	pdo.style.height = ( pHeight + 2 ) + "px";
	pdo.style.top = ( sHeight + wHeight/2 - pHeight/2 - 1) + "px";
	pdo.style.left = ( sWidth + wWidth/2 - pWidth/2 - 1) + "px";
	pdo.style.visibility = 'visible';
	
	pi.style.width = ( pdo.offsetWidth - 2) + "px";
    pi.style.height = ( pdo.offsetHeight - 2) + "px";
    pi.style.top = ( pdo.offsetTop + 1 ) + "px";
    pi.style.left = ( pdo.offsetLeft + 1 ) + "px";
    pi.style.visibility = 'visible';
    pi.style.display = 'block';

	pi.src = url;

    setTimeout("resetPopUpPosition();", 100);
}

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";
				}
			}
		}
	}
	
	//************************
	//Only for Metropole site
	//************************
	for(i = 0; i < document.forms.length; i ++){
		for(j = 0; j < document.forms[i].elements.length; j ++) {
	       if(document.forms[i].name == "AccueilForm") {
	          document.forms[i].flash_accueil.style.visibility = "visible";
	       }
	    }
	}
}

function hidePopUp(){
    if(!popUpOpened){
	    return;
	}
	
	popUpOpened = false;
    popUpHeight = null;
    popUpWidth = null;
	popUpReturnFct = null;
	popUpObj = null;

    var pwo = FIND('popUpWO');
	var pdo = FIND('popUpDO');
	var pi = FIND('popUI');
	
	pwo.style.visibility = 'hidden';
	pdo.style.visibility = 'hidden';
	pi.style.visibility = 'hidden';
	pi.style.display = 'none';
}


// 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);
}

//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) {
}
