//---------------------------------------------------------------------
// WAF - Web/Windows Application Framework
// Copyright © 2006 The Enticy Group LLC
//
// Notes: Miscellaneous WAF Javascript functions
//
// Revisions :
//	2003-07-08	Conrad	Added ScrollAndFocus()
//	2005-08-24	Conrad	Made "constants" of URLPARM_*
//	2005-09-05	Conrad	Added front-end adding/deleting of child table rows
//	2006-02-14	Conrad	Pulled out ERROR_MESSAGE[] and WINDOW_STYLE[] constants
//---------------------------------------------------------------------

// We want to use "const" but IE's version of JavaScript can't handle it
var URLPARM_ACTION = '_act';
var URLPARM_FKEYENTRY = '_fke';
var URLPARM_CHANGEDFIELDNAME = '_cfn';

var ERROR_MESSAGE = new Array();
ERROR_MESSAGE['NOMOREROWS'] = 'Cannot add any more rows.';

var WINDOW_STYLE = new Array();

//20060916 DBR Increased popup size for SEARCH 
//WINDOW_STYLE['POPUP'] = 'width=600,height=400,resizable=yes,scrollbars=yes,status=yes';
WINDOW_STYLE['POPUP'] = 'width=700,height=750,resizable=yes,scrollbars=yes,status=yes';

WINDOW_STYLE['AUTOCOMPLETE'] = 'height=150,width=200,status=no,resizable=yes,toolbar=no,menubar=no,directories=no,location=no,scrollbars=no';
WINDOW_STYLE['HELP'] = 'width=613,height=400,resizable=yes,scrollbars=yes';
WINDOW_STYLE['EMAIL'] = 'width=700,height=500,resizable=yes,scrollbars=yes,status=yes,menu=yes';
WINDOW_STYLE['PRINT'] = 'width=700,height=500,resizable=yes,scrollbars=yes,status=yes,menu=yes';

// This function is used by foreign keys to call the popup form
function FKPopup(href, field, displayfield) {
	if (field.type == 'text') {
		if (field.value != displayfield.value) href += '&' + URLPARM_FKEYENTRY + '=' + field.value;
	}
	EditPopup(href);
}

// This function is used to call the popup form
function EditPopup(href) {
	var hWnd = window.open(href, '', WINDOW_STYLE['POPUP']);
	if ((document.window != null) && (!hWnd.opener)) hWnd.opener = document.window; 
	if (window.focus) hWnd.window.focus();
}

// This function is called to put a value into a foreign key field
function FKUpdate(inputfield, displayfield, valuefield, keyvalue, displayvalue) {
	FieldUpdate(inputfield, displayvalue, displayvalue);
	FieldUpdate(displayfield, displayvalue, displayvalue);
	FieldUpdate(valuefield, keyvalue, displayvalue);
}

// This function is called to put a value into any field
function FieldUpdate(field, value, displayvalue) {
	if (field) {
		if (field.type == 'text') {
			field.value = value;
		} else if (field.type == 'hidden') {
			field.value = value;
		} else {
			// Loop through the existing options trying to find the given key
			var bOptionFound = false;
			for (var i = 0 ; i < field.length; i++) {
				if (field.options[i].value == value) {
					field.options[i].selected = true;
					bOptionFound = true;
				}
			}
			// If we didn't find it, then we need to add a new one.
			if (!bOptionFound) {
		 		var oOption = new Option(displayvalue, value, true, true);
				field.options[field.length] = oOption;
			}
		}
	}
}

// This function is called to put data into a valid value field
function VVSet(field, value) {
	if ((field.type == 'text') || (field.type == 'hidden')) {
		field.value = value;
	} else if (field.type == 'select-one') {
		// Loop through the existing options trying to find the given value
		var bOptionFound = false;
		for (var i = 0 ; i < field.length; i++) {
			if (field.options[i].value == value) {
				field.options[i].selected = true;
				bOptionFound = true;
			}
		}
		// If we didn't find it, then we need to add a new one.
		if (!bOptionFound) {
			var oOption = new Option(value, value, true, true);
			field.options[field.length] = oOption;
		}
	} else {
		// radio buttons
		for (var i = 0 ; i < field.length; i++) {
			if (field[i].value == value) {
				field[i].checked = true;
			}
		}
	}
}

// This function is called to put data into a valid value field
function VVCopy(tofield, fromfield) {
	if ((fromfield.type == 'text') || (fromfield.type == 'hidden') || (fromfield.type == 'select-one')) {
		VVSet(tofield, fromfield.value);
	} else {
		// radio buttons
		for (var i = 0 ; i < fromfield.length; i++) {
			if (fromfield[i].checked == true) {
				VVSet(tofield, fromfield[i].value);
			}
		}
	}
}

// This function is called to set the value of a Boolean field
function BoolSet(field, value) {
	if ((field.type == 'text') || (field.type == 'hidden')) {
		field.value = value;
	} else {
		// checkbox
		if (field.value == value) {
			field.checked = true;
		} else {
			field.checked = false;
		}
	}
}

// This function is called to copy data between Boolean fields
function BoolCopy(tofield, fromfield) {
	if ((fromfield.type == 'text') || (fromfield.type == 'hidden')) {
		BoolSet(tofield, fromfield.value);
	} else {
		// fromfield is a checkbox
		if (fromfield.checked) {
			BoolSet(tofield, fromfield.value);
		} else {
			BoolSet(tofield, 0);
		}
	}
}

// This function is called for each keydown in a foreign key
function FKKeydownHandler(event, fnAutocomplete, fnChoice, fnNew, fnNavigate) {
	// gets the code for NS or IE
	var keycode = event.keyCode ? event.keyCode : 
                event.which ? event.which : event.charCode;
	// We've chosen "Tab" as the autocomplete character, 
	// F2 for choice, F3 for navigate, and F4 for new
	if (keycode == 9 && fnAutocomplete) {
		fnAutocomplete();
		return false;
	} else if (event.keyCode == 113 && fnChoice) {
		fnChoice();
		cancelEvent(event);
		return false;
	} else if (event.keyCode == 114 && fnNavigate) {
		fnNavigate();
		cancelEvent(event);
		return false;
	} else if (event.keyCode == 115 && fnNew) {
		fnNew();
		cancelEvent(event);
		return false;
	} else {
		return true;
	}
}

function cancelEvent(event) {
	if (event.which) {
		// Mozilla 1.3 required to get rid of processing of F3 key
		needToCancelFunctionKey = true;
	} else {
		// IE 6.0 Required to get rid of processing of F3 key
		event.keyCode = 0;
		event.returnValue = false;
	}
}


// This function invokes the autocomplete function by popping up a
// window which will put the value back into the given field.
function FKAutocomplete(field, displayfield, parms) {
	if (field.type == 'text') {
		if (field.value != '' && field.value != displayfield.value) {
			var href = 'WAFPopup.aspx?' + URLPARM_ACTION + '=Autocomplete&' + parms + '&' + URLPARM_FKEYENTRY + '=' + field.value;
			var windowname = '_blank';
			window.open(href, windowname, WINDOW_STYLE['AUTOCOMPLETE']);
		}
	}
}

// Function invoked on dropdown option change to re-display a form.
// Submits the form with added values to the form action to specify 
// the action and identify the field
function ReDisplayForm(field) {

	var ActionURL = field.form.action
	var addUrl = URLPARM_ACTION + '=ReDisplay&' + URLPARM_CHANGEDFIELDNAME + '=' + field.name;
	
	// Make sure we didn't already add the ReDisplay action.
	// The change event can arrive fast, this will prevent re-entry.
	if (ActionURL.indexOf(URLPARM_ACTION + '=ReDisplay') > 0) return false;

	if (ActionURL.indexOf('?') > 0)
		ActionURL += '&' + addUrl;
	else
		ActionURL += '?' + addUrl;

	field.form.action = ActionURL;
	field.form.submit(); 
	return false;
}

// This utility function opens a help window
function ShowHelp(url) 
{ 
	var hWnd = window.open(url,'HelpWindow', WINDOW_STYLE['HELP']); 
	if (window.focus) hWnd.window.focus(); 
}

// This function is used to call the email popup window 
function EmailPopup(href) {
	var hWnd = window.open(href, '', WINDOW_STYLE['EMAIL']);
	if (window.focus) hWnd.window.focus();
}

// This function is used to call the print popup window 
function PrintPopup(href) {
	var hWnd = window.open(href, '', WINDOW_STYLE['PRINT']);
	if (window.focus) hWnd.window.focus();
}

// Trigger the print dialog 
function PrintPage() {
	if (window.print) {
		self.focus();
		self.print();
	}
}

// This function is used to scroll the page to the appropriate field,
// and then set focus to it.  Used after ReDisplay.
function ScrollAndFocus(field) {
	if (field) {
		if (field.scrollIntoView) {
			field.scrollIntoView(true);
		}
		if (field.type != 'hidden') {
			if (field.focus) {
				field.focus();
			}
		}
	}
}

// This function is called to visually mark a row as deleted.  The actual deleting is not done here.
function toggleRowDeleted(row) {
	toggleClassName(row, 'deleted');
};

// This function finds the first row in a table that is classed with "hidden" and changes it to normal.
function unhideRow(table) {
	toggleNextRow(table, 'hidden', ERROR_MESSAGE['NOMOREROWS']);
}

// This function will toggle the classname of an object (or add the second class if both are missing)
function toggleClassName(e, cn) {
	if (containsToken(e.className, cn, ' ')) {
		e.className = removeToken(e.className, cn, ' ');
	} else {
		e.className = addToken(e.className, cn, ' ');
	}
}

// This function finds the first row in a table that has a given class and changes it to another class.
// If it finds no such row, it can give the user an alert.
function toggleNextRow(table, cls, msg) {
	var rows = table.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
	for (var i = 0; rows[i] != null; i++) {
		if (containsToken(rows[i].className, cls, ' ')) {
			rows[i].className = removeToken(rows[i].className, cls, ' ');
			break;
		}
	}
	if (rows[i] == null) {
		if (msg != null) alert(msg);
	}
}

function containsToken(str, tok, delim) {
	return (delim + notNull(str) + delim).indexOf(delim + tok + delim) != -1
}

function addToken(str, tok, delim) {
	if (containsToken(str, tok, delim)) {
		return str;
	} else {
		if (notNull(str) == '') {
			return tok;
		} else {
			return str + delim + tok;
		}
	}
}

function removeToken(str, tok, delim) {
	var tempstr = delim + notNull(str) + delim;
	tempstr = tempstr.replace(delim + tok + delim, delim);
	return tempstr.substring(delim.length, tempstr.length - 2 * delim.length + 1);
}

function notNull(str) {
	if (str == null) {
		return '';
	} else {
		return str;
	}
}
