// Include this line in the cfm page: 
// <cfoutput><SCRIPT LANGUAGE="JAVASCRIPT" SRC="#Variables.JSDIR#/Common.js"> </SCRIPT></cfoutput>

//***
// openP: open a popup window with default attributes
// ARGUMENTS:
//  1- link: WEB page to be displayed in the popup window
//  2- opt_list(optional): Window attributes.
// 
// RETURN VALUES: none
//
//  By Yannick Haineault
// November 30, 2001
//***
function openP(link, opt_list) 
{
	!opt_list ? opt_list =  'toolbar=1,location=0,directories=0,status=yes,menubar=yes,scrollbars=1,resizable=1,width=750,height=550,left=0,top=0': opt_list

//	win_pop = window.open(link,'_popup', opt_list)
//	win_pop.focus()

	winID = window.open(link,'_blank', opt_list)
	winID.focus()
	//return(winID)
}

//***
// openP_screen: open a popup window with default attributes.  The window's size will be proportionnal to the screen size
// ARGUMENTS:
//  1- link: WEB page to be displayed in the popup window
//  2- opt_list(optional): Window attributes.
//  3- size_ratio: size of the window.  Default: .85 which is 85% of the current screen size.
//	4- width_ratio: Width of the window.  This value overwritte the size_ratio for the width.  Use 'null' if you don't want to use it.
//	5- height_ratio: Height of the window.  This value overwritte the size_ratio for the height.  Use 'null' if you don't want to use it.
//  6- location ratio: Location of the window.  Default .075 which is 7.5% of the current screen size.
//	7- x_ratio: Horizontal position of the window.  This value overwritte the location_ratio for the 'x' coordinate.
//	8- y_ratio: Vertical position of the window.  This value overwritte the location_ratio for the 'y' coordinate.
// 
// RETURN VALUES: none
//
//  By Yannick Haineault
// May 21, 2002
//***

function openP_screen(link, opt_list, size_ratio, width_ratio, height_ratio, location_ratio, x_ratio, y_ratio)
{
	size_ratio = !size_ratio ? .85 : size_ratio;
	size_ratio = size_ratio > 1 ? 1 : size_ratio;
		
	location_ratio = !location_ratio ? (1 - size_ratio)/2 : location_ratio;
	location_ratio = location_ratio > size_ratio ? 0 : location_ratio;

	x_ratio = !x_ratio ? location_ratio : x_ratio;
	y_ratio = !y_ratio ? location_ratio : y_ratio;

	width_ratio = !width_ratio ? size_ratio : width_ratio;
	height_ratio = !height_ratio ? size_ratio : height_ratio;

	var win_width=screen.availWidth * width_ratio;
	var win_height=screen.availHeight * height_ratio;
	var win_x=screen.availWidth * x_ratio;
	var win_y=screen.availHeight * y_ratio;
	
	var winsize=width='width='+(win_width)+', height=1, left='+(win_x)+', top='+(win_y);

	!opt_list ? opt_list = 'toolbar=1, location=0, directories=0, status=0, menubar=0, scrollbars=1,resizable=1' + winsize : opt_list + winsize;

	var winID = window.open(link,'_blank', opt_list);

	winID.moveTo((win_x), (win_y));

/*alert(screen.availWidth);
alert(screen.availHeight);
alert(win_height);
alert(width_ratio);
alert(height_ratio);*/

	for (var a = 1; a <= (win_height); a += 15 )
	{
		winID.resizeTo((win_width), a);
	}

	winID.resizeTo((win_width),(win_height));
	
}


//***
// back_or_close: Go back in history, or close window if in a popup
// ARGUMENTS:
//  1- back_num: Number of pages to go back in history
// 
// RETURN VALUES: none
//
//  By Yannick Haineault
// November 30, 2001
//***
function back_or_close(back_num)
{
	!back_num ? back_num = -1 : back_num
	back_num > 0 ? back_num * -1 : back_num
	
//	hist_len = history.length
		
	if (self.opener)// && ((hist_len == 0) || (hist_len == 2)))
	{
		window.close()
	}
	else 
	{
		history.go(back_num)
	}
}

//***
// cancel_input: Ask user confirmation before cancelling a form input
// ARGUMENTS:
//  1- cancel_msg: Confirmation message
//
// RETURN VALUES: None
//
//  By Yannick Haineault
//  November 30, 2001
//***
function cancel_input(cancel_msg)
{
	if (cancel_msg == null)
	{
		back_or_close()	
	}
	else if (confirm(cancel_msg))
	{
		back_or_close()
	}
}

//***
// back_msg: Ask user confirmation before load the previous page
// ARGUMENTS:
//  1- cancel_msg: Message
//
// RETURN VALUES: None
//
//  By Yannick Haineault
//  February 14, 2002
//***
function back_msg(back_msg)
{
	if (back_msg == null)
	{
		history.back()	
	}
	else if (confirm(back_msg))
	{
		history.back()
	}
}


//***
// validate_passwd - Compare values of passwd1 and passwd2.  
// ARGUMENTS:
//  1- passwd1: password field
//  2- passwd2: password confimration field
//  3- msg1(optional): message displayed when passwd1 or passwd2 is empty
//  4- msg2(optional): message displayed when passwd1 and passwd2 don't match
//
// RETURN VALUES:
//  true: password confirmation ok
//  false: password confirmation not ok
//
//  By Yannick Haineault
//  December 10, 2001
//***
function validate_passwd(passwd1, passwd2, msg1, msg2)
{
	!msg1 ? msg1 = "Please confirm your password." : msg1
	!msg2 ? msg2 = "Please reenter your password.\nConfirmation has failed." : msg2

	if (!passwd1.value || !passwd2.value)
	{
		alert(msg1);
		passwd1.focus()
		return false;
	}
	else if (passwd1.value != passwd2.value)
	{
		alert(msg2);
		passwd1.focus();
		return false;
	}
	else return true
}

//***
// validate_mandatory: Check if a value has been entered in a form field
// ARGUMENTS:
//  1- fieldname: Field to check
//  2- msg(optional): Message to display. If null, no message is displayed.
//  3- null_val(optional): Specify a customized value for "null".
//
// RETURN VALUES:
//  1- true: value entered, validation ok.
//  2- false: value not entered, validation error.
//
//  By Yannick Haineault
//  December 11, 2001
//***
function validate_mandatory(fieldname, msg, null_val)
{
	if (fieldname.value == null_val || fieldname.value == "")
	{
		return_val = false
		
		if (msg)
		{
			alert(msg)
		}
		
		fieldname.focus()
	}
	else 
	{
		return_val = true
	}

	return(return_val)
}

//***
// aevalidate_mandatory: Check if a value has been entered in an active edit field
// ARGUMENTS:
//  1- fieldname: Field to check
//  2- msg(optional): Message to display. If null, no message is displayed.
//  3- null_val(optional): Specify a customized value for "null".
//
// RETURN VALUES:
//  1- true: value entered, validation ok.
//  2- false: value not entered, validation error.
//
//  By Yannick Haineault
//  February 11, 2002
//***
function aevalidate_mandatory(fieldname, msg, null_val)
{
	if (aeObjects[fieldname].DOM.body.innerText == null_val || aeObjects[fieldname].DOM.body.innerText == "")
	{
		return_val = false
		
		if (msg)
		{
			alert(msg)
		}
		
		aeObjects[fieldname].focus()
	}
	else 
	{
		return_val = true
	}

	return(return_val)
}

//***
// validate_email: Validate an email field value (user@server.ext)
// ARGUMENTS:
//  1- fieldname: name of the email field in the form.
//  2- msg(optional): Message to be displayed if the format is wrong.
//  3- strPattern(optional): Customized email format
//
// RETURN VALUES:
//  1- true: Format ok.
//  2- false: format not ok.
//
//  By Yannick Haineault
//  December 11, 2001
//***
function validate_email(fieldname, msg, strPattern) 
{
	var addressPattern = /^[\w+\.]+\@[\w+\.]+\.[A-Za-z0-9]+$/;

	!strPattern ? strPattern = addressPattern : strPattern
	
	if (!addressPattern.test(fieldname.value))
	{
		return_val = false
		
		if (msg)
		{
			alert(msg)
		}
		
		fieldname.select()
	}
	else
	{
		return_val = true
	}

	return(return_val)
}
