function isEmptyString(stringToCheck, trimFirst)
{	
	var returnValue = true;
	if (stringToCheck != null)
	{
		var afterTrim = stringToCheck;
		if (trimFirst)
		{
			afterTrim = trimWhiteSpace(afterTrim);
		}
		returnValue = afterTrim.length == 0;
	}
	return returnValue;
}

function equalsIgnoreCase(string1, string2)
{
	var returnValue = true;
	if (string1 != null)
	{
		if(string2 != null)
		{
			returnValue = string1.toLowerCase() == string2.toLowerCase();
		}
		else
		{
			returnValue = false;
		}
	}
	else
	{
		returnValue = (string2 == null);
	}
	return returnValue;
}

function trimWhiteSpace(stringToTrim)
{
	var returnValue = stringToTrim;
	if (stringToTrim != null)
	{
		returnValue = stringToTrim.replace(/^\s*|\s*$/g, "");
	}
	return returnValue;
}

function getRadioButtonValue(elementName, defaultValue)
{
	var returnValue = defaultValue;
	var toCheck = document.getElementsByName('sendType');
	if (toCheck)
	{
		for (var i=0; i<toCheck.length; i++)
		{			
			if (toCheck[i].checked)
			{
				returnValue = toCheck[i].value;
				break;
			}
		}
	}
	return returnValue;
}

function validateEmailElement(element, showMessage)
{
	var returnValue = false;
	if (element)
	{
		if (element.value != "")
		{
			returnValue = validateEmail(element.value, showMessage);
			if (!returnValue)
			{
				var fieldName = getValidationName(element);
				showValidationMessage(element, 'The value you have entered for the ' + fieldName + ' is not a valid email address\nPlease edit the ' + fieldName + ' and correct this.');
			}
		}
		else
		{
			returnValue = true;
		}
	}
	return returnValue;
}

function validateEmail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);				
}

function validateDecimalFormat(decimalString, scale, precision)
{
	var validated = false;	
	if (!isEmptyString(decimalString))
	{		
		var stringExp = "(^[0-9]{0," + scale + "}\\.[0-9]{0," + precision + "}$)|(^[0-9]{0," + (scale) + "}$)";		
		var decRegExp = new RegExp(stringExp, "g");
		var decResult = decRegExp.exec(decimalString);					
		validated = (decResult != null);
	}
	return validated;
}
function validateDecimalElement(element, showMessage)
{
	var returnValue = false;
	if (element != null)
	{		
		if (element.value != "")
		{
			var scale = getNumericValidationAttribute(element, 'asScale', 10);	
			var precision = getNumericValidationAttribute(element, 'asPrecision', 2);					
			returnValue = validateDecimalFormat(element.value, scale-precision, precision);
			if (!returnValue)
			{
				var fieldName = getValidationName(element);
				var errorMessage = "";
				var parsedValue = parseFloat(element.value);
				if (isNaN(parsedValue))
				{
					errorMessage = 'is not a valid decimal value.  It contains characters that cannot be converted to a number.'
				}
				else
				{
					errorMessage = 'is not a valid decimal value.  You may only specify ' + (scale-precision) + ' numbers to the right of the decimal and ' + precision + ' numbers to the left.'; 
				}
							
				showValidationMessage(element, 'The value you have entered for the ' + fieldName + ' ' + errorMessage + '\nPlease edit the ' + fieldName + ' and correct this.');
			}		
		}
		else
		{
			returnValue = true; //don't validate empty strings, that is for the required validation routine.
		}
	}	
	return returnValue;
}

function isNumber(fieldValue)
{
	var returnValue = true;
	if (!isEmptyString(fieldValue, true))
	{	
		var testValue = trimWhiteSpace(fieldValue);
		var i=0;
		for (i=0; i< testValue.length;i++)
		{
			var currentCharCode = testValue.charCodeAt(i);
			if (currentCharCode < 48 || currentCharCode > 57)
			{
				returnValue = false;
				break;
			}
		}
	}	
	return returnValue;
}

function validateForm(form, showMessage)
{
	var returnValue = true;
	if (form != null)
	{
		var elementArray = form.elements;
		var i=0;
		for (i=0; i<form.elements.length;i++)
		{
			var element = form.elements[i];			
			var validated = validateElement(element, showMessage);				
			if (!validated)
			{
				returnValue = false;
				break;
			}
		}
	}	
	return returnValue;
}

function getValidationAttribute(element, attributeName)
{
	var returnValue = "";
	if (element != null)
	{
		var attributes = element.attributes;
		var j=0;
		for (j=0;j<attributes.length;j++)
		{
			if(equalsIgnoreCase(attributes[j].nodeName, attributeName))
			{
				returnValue = attributes[j].value;
				break;
			}
		}		
	}
	//alert('getValidationAttribute(' + element.nodeName + ',' + attributeName + ') returns ' + returnValue);
	return returnValue;
}

function getBooleanValidationAttribute(element, attributeName)
{
	var returnValue = false;
	var attribute = getValidationAttribute(element, attributeName);
	returnValue = equalsIgnoreCase(attribute, 'true');	
	return returnValue;
}

function getNumericValidationAttribute(element, attributeName, defaultValue)
{
	var returnValue = defaultValue;
	var stringValue = getValidationAttribute(element, attributeName);
	if (stringValue != "")
	{
		var parsedValue = parseInt(stringValue);
		if (!isNaN(parsedValue))
		{
			returnValue = parsedValue;
		}
	}
	return returnValue;
}

function getValidationName(element)
{
	return getValidationAttribute(element, 'asname');
}

function isRequiredElement(element)
{
	return getBooleanValidationAttribute(element, 'asrequired');
}

function disallowHighBitCharacters(element)
{
	return getBooleanValidationAttribute(element, 'ascheckhbc');
}

function isTextElement(element)
{
	var returnValue = false;
	var typeAttribute = getValidationAttribute(element, 'astype');
	returnValue = equalsIgnoreCase(typeAttribute, 'text');
	return returnValue;
}

function isEmailElement(element)
{
	var returnValue = false;
	var typeAttribute = getValidationAttribute(element, 'astype');
	returnValue = equalsIgnoreCase(typeAttribute, 'email');
	return returnValue;
}

function isDecimalElement(element)
{
	var returnValue = false;
	var typeAttribute = getValidationAttribute(element, 'astype');
	returnValue = equalsIgnoreCase(typeAttribute, 'decimal');
	return returnValue;
}

function isNumericElement(element)
{
	var returnValue = false;
	var typeAttribute = getValidationAttribute(element, 'astype');
	returnValue = equalsIgnoreCase(typeAttribute, 'number');	
	return returnValue;
}

function validateTextElement(element, showMessage)
{
	var returnValue = true;
	if (element != null)
	{
		if (disallowHighBitCharacters(element))
		{						
			returnValue = validateNoHbcElement(element, showMessage);			
		}			
	}
	return returnValue;
}

function getHighBitChars(stringToTest)
{
	var returnValue = "";
	if (stringToTest != null && stringToTest != "")
	{		
		var i=0;
		for (i=0;i<stringToTest.length;i++)
		{
			if (stringToTest.charCodeAt(i) > 127)
			{
				returnValue = returnValue + stringToTest.charAt(i);						
			}
		}						
	}
	return returnValue;
}

function validateNoHbcElement(element, showMessage)
{
	var returnValue = true;
	if (element != null)
	{
		var highBitChars = getHighBitChars(element.value);
		if (!isEmptyString(highBitChars, true))
		{
			returnValue = false;
			var fieldName = getValidationName(element);
			showValidationMessage(element, 'The value you have entered for the ' + fieldName + ' contains the following invalid characters:\n' + highBitChars + '\nPlease edit the ' + fieldName + ' and correct this.');
		}			
	}
	return returnValue;
}

function validateNumericElement(element, showMessage)
{
	var returnValue = true;
	if (element != null)
	{		
		var hbcValidated = true;
		if (disallowHighBitCharacters(element))
		{			
			hbcValidated = validateNoHbcElement(element, showMessage);										
		}					
		if (hbcValidated)
		{
			var isValidNumber = isNumber(element.value);	
			if (showMessage && !isValidNumber)
			{
				var fieldName = getValidationName(element);
				returnValue = false;				
				showValidationMessage(element, 'The value for ' + fieldName + ' must be a number.');
			}					
		}
		else
		{
			returnValue = false;
		}
	}
	return returnValue;
}

function showValidationMessage(element, messageToShow)
{
	var returnValue = false;	
	if (element != null)
	{
		returnValue = true;
		try
		{
			element.focus();		
		}
		catch (err)
		{
			
		}
	}
	alert(messageToShow);
	return returnValue;
}

function validateRequiredElement(element, showMessage)
{
	var returnValue = true;
	if (element != null)
	{		
		returnValue = !isEmptyString(element.value, true);
	}		
	if (showMessage && !returnValue)
	{		
		var fieldName = getValidationName(element);
		showValidationMessage(element, 'You must fill out a value for ' + fieldName + '.');
	}	
	return returnValue;
}

function validateElement(element, showMessage)
{
	var returnValue = true;
	if (isRequiredElement(element))
	{
		returnValue = validateRequiredElement(element, showMessage);
		//alert('validateRequiredElement='+ returnValue);
	}
	
	if (returnValue)
	{
		if (isTextElement(element))
		{
			returnValue = validateTextElement(element, showMessage);
			//alert('validateTextElement='+ returnValue);
		}
		else if (isNumericElement(element))
		{
			returnValue = validateNumericElement(element, showMessage);
				//alert('validateNumericElement='+ returnValue);
		}
		else if (isEmailElement(element))
		{
			returnValue = validateEmailElement(element, showMessage);
		}
		else if (isDecimalElement(element))
		{
			returnValue = validateDecimalElement(element, showMessage);
		}
	}
	//alert('validateElement(' + element + ',' + showMessage + ')');
	return returnValue;
}

function refreshPageByUrl(originalUrl)
{
	changeCurrentUrl(getUniqueUrl(originalUrl));
}

function getDateMillis()
{
	var currentDate = new Date();
	return currentDate.getMilliseconds();
}

function getUniqueUrl(originalUrl)
{
	var returnValue = originalUrl;
	if (returnValue != null)
	{
		var unique = getDateMillis();
		if (returnValue.indexOf("?") > -1)
		{
			returnValue += "&d=" + unique;
		}
		else
		{
			returnValue += "?d=" + unique;
		}
	}
	return returnValue;
	
}

function changeCurrentUrl(newUrl)
{
	window.location.href = newUrl;
}

function returnToEmailList()
{
	changeCurrentUrl('int_email_draft_list.asp');
}

function getSafeWindowName(windowName)
{
	var returnValue = windowName;
	if (!isEmptyString(windowName, false))
	{
		returnValue = returnValue.replace(/-/g, ''); 
	}
	return returnValue;
}

var defaultDialogOptions = 'fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,channelmode=no,directories=no';

function openWindowAS(newWindowURL, newWindowName, newWindowFeatures, childArray, parentName)
{	
	var safeWindowName = getSafeWindowName(newWindowName);	
	var newWindow = window.open(newWindowURL, safeWindowName, newWindowFeatures);		
	newWindow.focus();
}

function hilightRow(rowElement, hilightOn)
{
	if (rowElement != null)
	{
		if (hilightOn)
		{
			rowElement.setAttribute('bgColor', '#EEEEEE');
		}
		else
		{
			rowElement.setAttribute('bgColor', '#FFFFFF');
		}
	}
}
function setParentRefresh(enabled)
{
	var refreshParentField = document.getElementById('asRefreshParent');
	if (refreshParentField)
	{
		//alert ('setParentRefresh(' + enabled + ' : refreshParentField.value = ' + refreshParentField.value);
		if (refreshParentField.value == "" && enabled)
		{			
			refreshParentField.value = "1";
			//alert ('setParentRefresh(' + enabled + ' : refreshParentField.value = ' + refreshParentField.value);
		}
	}
}
function triggerParentRefresh(docopener, millis)
{
	//alert ('docopener=' + docopener);
	if (docopener && docopener.document)
	{
		var trigger = docopener.document.getElementById('asRefresh');
		//alert('trigger = ' + trigger);
		if (trigger)
		{
			trigger.value = millis;
			//alert('trigger.value = ' + trigger.value + ' : trigger.onclick = ' + trigger.onclick);
			if (trigger.onclick)
			{				
				trigger.onclick();
			}
		}		
		else
		{
			//alert ('docopener.parent=' + docopener.parent);
			if (docopener.parent && docopener.parent != docopener)
			{				
				triggerParentRefresh(docopener.parent, millis);
			}
		}
	}
}
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
function unloadRefreshParent(delay)
{
	var isCloseField = document.getElementById('frm_IsWindowClose');
	//alert ('unloadRefreshParent(' + delay + ' : isCloseField.value = ' + isCloseField.value);
	if (isCloseField.value == "1")
	{
		refreshParent(10);
	}
}
function refreshParent(delay)
{	
	var refreshParentField = document.getElementById('asRefreshParent');
	//alert ('refreshParent(' + delay + ' : refreshParentField.value = ' + refreshParentField.value);
	if (refreshParentField && refreshParentField.value == '1')
	{
		triggerParentRefresh(window.opener, delay)
	}
}
function trackCharacterCount(field, maxlen)
{
	var teststring = field.value;
	var actlen = teststring.length;
	if( actlen > maxlen )
	{
		alert('The limit of ' + maxlen + ' characters has been reached.');
		teststring = teststring.substring(0,maxlen);
		var currentCharCode = teststring.charCodeAt(maxlen);
		if (currentCharCode ==10)
			teststring = teststring.substring(0,maxlen-1);
		actlen = teststring.length;
		field.value = teststring;
	}

	remlen = maxlen - actlen;
	msgField = field.name + '_cc';
	document.getElementById(msgField).innerHTML = remlen + '/' + maxlen + ' chars left';
}
function getWindowInnerDimensions()
{
	var returnValue = null;
	try
	{
		if (window.innerWidth)
		{
			returnValue = new Object();
			returnValue.Width = window.innerWidth;
			returnValue.Height = window.innerHeight;
		}
		else if (document.body.parentNode.clientWidth)
		{
			returnValue = new Object();
			returnValue.Width = document.body.parentNode.clientWidth;
			returnValue.Height = document.body.parentNode.clientHeight;			
		}
		/*else if (document.body.offsetWidth)
		{
			returnValue = new Object();
			returnValue.Width = document.body.offsetWidth;
			returnValue.Height =document.body.offsetHeight;
		}*/
	}
	catch (err)
	{
		
	}
	return returnValue;
}
function fitWindow(x, y, maxX, maxY)
{
	var dimensions = getWindowInnerDimensions();	
	if (dimensions)
	{
		var width = x - dimensions.Width;
		var height = y - dimensions.Height;
		window.resizeBy(width, height);
	}		
	else
	{
		window.resizeTo(maxX, maxY);
	}	
}