<!--

//Created By:	 				Reid Guest
//Last Modified By:			Reid Guest
//Created On:					August 25, 2000
//Modified: 						August 25, 2000
//Purpose:						To provide standard functions essential to most OnTheFly modules


// VARIABLES

// This variable is the one-and-only variable to use for non-unique popup windows
var popup;

// This variable is used to revert the document's title (in title bar) back to original title when the hash of the location is altered
var strCurrentTitle = document.title;

// install the global error-handler
window.onerror = errorHandler;

//FUNCTIONS

//this script verifies a numeric entry in a quantity field
function isFieldAlphanumeric(inputText) {
	var flg = 0;
	var str='';
	//var spc='';
	var arw='';
	var bLowerCase = false;
	var strLowerCase = '';
	var strNameOfField;
	var cmp='0123456789abcdefghijklmnopqrstuvwxyz ';
	strNameOfField = (arguments[1].toString());
	if (typeof arguments[2] != 'undefined')
		bLowerCase = (arguments[2]);
	else
		bLowerCase = false;
	if (bLowerCase==true)
		strLowerCase = ' and lower-case';
	else
		cmp += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ';

	for (var i=0; i < inputText.length; i++) {
		tst=inputText.substring(i,i+1);

		if (cmp.indexOf(tst) < 0) {
			flg++;
			str += tst;
			//spc += tst;
			arw += '^';
		} else {
			arw += '_';
		}
	}

	if (flg != 0) {
		//if (spc.indexOf(' ') > -1) {
	//		str += ' and a space';
	//	}
		return strNameOfField + ' must be alphanumeric' + strLowerCase + '. You have entered ' + flg + ' unacceptable characters (' + str + ').';
	} else {
		return 'valid';
	}
}

function errorHandler()
{
 // handle the error here
//alert('There has been an error in this application.\nThis message should only be seen in testing/development phases.\n\nPlease comment out this line if this site is live (see templates).');
 // stop the event from bubbling up to the default window.onerror handler
   return true;
}

function checkDate(myMonthStr, myDayStr, myYearStr) {
	 
	//var myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;

	/* Using form values, create a new date object
	which looks like "Wed Jan 1 00:00:00 EST 1975". */
	var myDate = new Date( myYearStr, myMonthStr, myDayStr );

	// Convert the date to a string so we can parse it.
	var myDate_string = myDate.toGMTString();

	/* Split the string at every space and put the values into an array so,
	using the previous example, the first element in the array is "Wed", the
	second element is "Jan", the third element is "1", etc. */
	var myDate_array = myDate_string.split( ' ' );

	/* If we entered "Feb 31, 1975" in the form, the "new Date()" function
	converts the value to "Mar 3, 1975". Therefore, we compare the month
	in the array with the month we entered into the form. If they match,
	then the date is valid, otherwise, the date is NOT valid. */
	myMonthStr = getMonthAbbreviation(myMonthStr);

	if ( myDate_array[2] != myMonthStr ) {
	  return false;
	} else {
	  return true;
	}
}

//This function closes the popup window and restores focus to the opener window
function closePopup() {
	self.close();
	opener.focus();
	return true;
}

function getMonthAbbreviation(intMonth) {
	var strMonthAbbreviation = '';
	arrMonths = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	strMonthAbbreviation = arrMonths[intMonth];
	return strMonthAbbreviation;
}

//function to count number of text rows entered in a textarea
function countRows(strText) {
	var intLineCount;
	var intTempIndex = 0;

	// if there is text, then we have one line
	intLineCount = (strText.length > 0) ? 1: 0;

	// loop through text, removing  any double-carriage-returns until there are no more
	do
	{
		intTempIndex =  strText.indexOf('\r\n');
		if (intTempIndex > 0)
		{
			strText = strText.substring(intTempIndex + 1);
			intLineCount ++;
		}
	}
	while (intTempIndex > 0);
	return intLineCount;
}

function isEmpty(objInputField) {
	if (objInputField.value == '')
	{
		return true;
	} else {
		return false;
	}
}

// This function links to a given anchor (ie. <A NAME="strAnchorName"> ....) in the current document, then corrects the document's title
function linkToAnchor(strAnchorName) {
	document.location.hash = strAnchorName;
	document.title = strCurrentTitle;
}


//function to remove double-line-breaks (empty lines) from a textarea (while trimming external breaks)
function removeEmptyLines(strText) {
	var tempText = stripSpaces(getAcceptableText(String(strText))); 
	var intTempIndex = 0;

	do
	{
		intTempIndex =  tempText.indexOf('\r\n\r\n');
		if (intTempIndex > -1)
		{
			// remove double lines (from the text to be returned from the function
			tempText = tempText.replace('\r\n\r\n','\r\n');
		}
	}
	while (intTempIndex > 0);

	return tempText;
}


//this script puts an input number into currency format and returns it as a string
function formatCurrencywiggitywhack(numInput) {

	var intIntegerPart = Math.floor(numInput);					// eg. if numInput = 1234.236, intIntegerPart = 1234
	var intDecimalPart = Math.round((numInput % 1)*100);		// eg. if numInput = 1234.236, intMod = 24
	alert(intIntegerPart);
	var strDecimalPart = intDecimalPart.toString();

	if (intDecimalPart == 0) {					// ie. there was originally no decimal values in number
		strDecimalPart = '00';
	} else if (strDecimalPart.length == 1) {	// ie. there was originally one decimal place value, eg. 1234.1
		strDecimalPart = strDecimalPart + '0';	// ie. to make it 1234.10
	}

	return '$' + intIntegerPart.toString() + '.' + strDecimalPart;
}

function focusOnFirstField() {
	//if there is a form, focus on it's first editable field
	if (typeof frmCurrentForm != 'undefined')
	{
		//Focus and select the first element on the form that is a textual input field
		for (var x=0; x<frmCurrentForm.elements.length;x++) {
			if ( String('|hidden|button|checkbox|radio|textarea|reset|submit|select-one|').indexOf( '|' + frmCurrentForm.elements[x].type.toString() + '|' ) == -1 ) {
				if ((frmCurrentForm.elements[x].focus) && (frmCurrentForm.elements[x].name != 'WordCount')){ 
					frmCurrentForm.elements[x].focus();
					if ( String('text').indexOf( frmCurrentForm.elements[x].type.toString() ) > -1 )
						frmCurrentForm.elements[x].select();
					break;
				}
			}
		}
	}
	return true;
}


function formatCurrency(num) {
	num = Math.abs(num);
	if (isNaN(num)) {
		num = "0";
	}
	cents = Math.floor((num*100+0.5)%100); 
	num = Math.floor(num).toString();
	if (cents < 10) {
		cents = "0" + cents; 
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+num.substring(num.length-(4*i+3)); 
	}

	return (num + '.' + cents); 
}

// Function to include a style sheet based on the browser and version of the user
function includeStyleSheet( strAdminOrPublic , strRootPath ) {
	if ((browserIsIE4) || (browserIsIE3))
	   document.write('<LINK rel="stylesheet" type="text/css" href="' + strRootPath + 'Common/Styles/' + strAdminOrPublic + '_ie4.css">');
	else if (browserIsNN4)
	   document.write('<LINK rel="stylesheet" type="text/css" href="' + strRootPath + 'Common/Styles/' + strAdminOrPublic + '_nn.css">');
	return true;
}

// The variable through which a popup can be tested for/accessed from any page
var popup

function popupWindow(url, windowsWidth, windowsHeight, scrollBar)
{
	if (scrollBar == '')
	{
		scrollBar='No';
	}

	popup = window.open(url,'popupWindow','resizable=yes,scrollbars='+ scrollBar +',width=' + windowsWidth + ',height=' + windowsHeight + ',top=50,left=200');
	// delay to halt IE4 errors
	setTimeout('popup.focus();',250);

	return popup.name;
}

function popupWindowDetails(url, toolBar, windowsLocation, directories, status, menuBar, scrollBars, resizable, windowsWidth, windowsHeight, windowsTop, windowsLeft)
{
	popup = window.open(url,'popupWindow','toolbar=' + toolBar + ',location=' + windowsLocation + ',directories=' + directories + ',status=' + status + ',menubar=' + menuBar + ',scrollbars=' + scrollBars + ',resizable=' + resizable + ',width=' + windowsWidth + ',height=' + windowsHeight + ',top=' + windowsTop + ',left=' + windowsLeft + '');
	// delay to halt IE4 errors
	setTimeout('popup.focus();',250);

	return popup.name;
}



//Created By:	 				Reid Guest
//Last Modified By:			Reid Guest
//Created On:					July 18, 2000
//Modified: 						August 16, 2000
//Purpose:						To preload images on a page so they can be used with minimal load time OnTheFly
//									ie. to load images which are only shown on selecting a certain drop-down item or used to swap graphics onMouseOver
//Parameters:					A listing of 'virtualpath/filename' parameters


function preloadImages()
{
	for (i=0, j=arguments.length; i<j; i++)
	{
		preloadImage = new Image();
		preloadImage.src = arguments[i];	 
	}
}

// this function is used to change the text on a submit button for a bilingual back-end
function setSubmitButtonText(objButton, objCheckBox) {
		//backup original value of submit button to replace when equiv checkbox is cleared
		if (typeof strOriginalSubmitButtonValue == 'undefined') {
			strOriginalSubmitButtonValue = objButton.value.toString().replace(' EQUIVALENT','');;
		}

		// if the checkbox is checked, then insert 'EQUIVALENT' into the button text
		if (objCheckBox.checked) {		
			// Insert ' EQUIVALENT' into the button text
			objButton.value=strOriginalSubmitButtonValue.replace(' >>',' EQUIVALENT >>');
		} else {
			// Remove ' EQUIVALENT' from the button text
			objButton.value=strOriginalSubmitButtonValue;
		}
}



// Equivalent to vbscript's Trim() function	
function stripSpaces(strText) {
	x = strText;
	while (x.substring(0,1) == ' ') x = x.substring(1);
	while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	return x;
}

// This function removes all line-breaks and other characters not found in the acceptable characters string
function getAcceptableText(strText) {
	x = strText;
	strAcceptables = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
	while (strAcceptables.indexOf(x.substring(0,1)) == -1) x = x.substring(1);
	while (strAcceptables.indexOf(x.substring(x.length-1,x.length)) == -1) x = x.substring(0,x.length-1);
	return x;
}

// validates an email address
function isEmailAddress(objAddressField) {
	var emailAddressEntered = objAddressField.value;
	if ((emailAddressEntered.indexOf('@') < 1) || (emailAddressEntered.lastIndexOf('.') < (emailAddressEntered.indexOf('@') + 2)) || (emailAddressEntered.indexOf('\'') > -1) || (emailAddressEntered.indexOf('"') > -1) ) {
		alert('Please enter a valid email address');
		objAddressField.focus();
		objAddressField.select();
		return false;
	} else {
		return true;
	}
}




//this script verifies a numeric entry in the Quantity field on the shopping cart forms
function verifyNumeric(inputText) {
	var flg = 0;
	var str='';
	var spc='';
	var arw='';
	for (var i=0; i < inputText.length; i++) {
		cmp='0123456789';
		tst=inputText.substring(i,i+1);
		if (cmp.indexOf(tst) < 0) {
			flg++;
			str += tst;
			spc += tst;
			arw += '^';
		} else {
			arw += '_';
		}
	}
	if (flg != 0) {
		if (spc.indexOf(' ') > -1) {
			str += ' and a space';
		}
		alert('Sorry, only numbers are acceptable. You have entered ' + flg + ' unacceptable characters (' + str + ').');
		return false;
	} else {
		return true;
	}
}



//===============================================================================
// This function sets the two form variables which are critical to the functionality of all pages
//===============================================================================
function setFormVariables(strCurrentForm, isPopupWindow) {
	// if the page has a form, then setup the currentForm variable
	if (strCurrentForm !='')
	{
		frmCurrentForm = eval('lyrPageContents.layer.document.' + strCurrentForm);	
	}
	//alert(frmCurrentForm.name);
	// and there is always a toolbox in a main window, not in a popup though
	if (! isPopupWindow)
		frmToolBox = lyrToolBox.layer.document.toolbarLinks;
}




//===============================================================================
// This function assigns layer objects to the required layer variables (see template's BODY onLoad event for call)
// NOTE:
//	To get at the style for the layer, IE is lyrToolBoxControl.style.attributename whereas NN is lyrToolBoxControl.left;
//===============================================================================
function initializeLayers() {
	// Assign layers to objects
	// IMPORTANT: ALL LAYERS IN ANY MODULE IN THE SITE <<MUST>> BE DECLARED HERE

	// Menu for site
	lyrToolBox = new LayerObject('ToolBox');
	//lyrToolBox.alertStatus();


	// Open/Close Image layers for the site menu
	lyrToolBoxControl = new LayerObject('ToolBoxControl');
	lyrToolBoxControlClose = new LayerObject('ToolBoxControlClose');

	// Main layer where module-specific HTML is placed (all forms, etc...)
	lyrPageContents = new LayerObject('PageContents');
	//lyrPageContents.alertStatus();

	// Empty page for covering screen with nothing when necessary
	lyrEmptyPage = new LayerObject('EmptyPage');

	// Preview pane for CM pages
	lyrPreviewPage = new LayerObject('PreviewPage');
	
	// Newsletter status bar (also needed to be declared earlier in page load on SendNewsletter.asp)
	lyrStatusBar = new LayerObject('statusBar');

	// CM page-formatting buttons
	lyrFormattingTools = new LayerObject('FormattingTools');

	// Popup mousovers/clicks (descriptions, menus)
	lyrCurrentPopup = new LayerObject('overlayPopupStyle');
}

function CheckFileType(d){
	var msg = ['Please select a valid file to upload.',
		'Please select an iamge file (GIF,JPG, or JPEG) to upload.',
		'Please ensure that the file has no quotes ( \' or " )in the filename.']
	var file_name = d.value.toLowerCase();
	var start_position = file_name.lastIndexOf(".");
	if (start_position < 0) { alert( msg[0] );return false; }
	var end_position = file_name.length;
	var file_extension = file_name.substring(start_position + 1, end_position );
	if (String('gif,jpg,jpeg').indexOf(file_extension) < 0) { alert( msg[1] );return false; }
	if ( (file_name.indexOf('\'') > -1) || (file_name.indexOf('"') > -1) ) { alert( msg[2] );return false; }
	return true;
}



var strCMImagePath = '';
function setCMImagePath( strPath ){ strCMImagePath = strPath;return true;}
function getCMImagePath(){ return(strCMImagePath); }



//-->
