//#############################################
// Pop Up
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
    popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
//#############################################

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//#############################################
// WORD COUNT
function CountWords (this_field, show_word_count, show_char_count, formName, fieldName) {
if (show_word_count == null) {
show_word_count = true;
}
if (show_char_count == null) {
show_char_count = false;
}
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;
if (fullStr.length <2) {
word_count = 0;
}
if (word_count == 1) {
wordOrWords = " word";
}
else {
wordOrWords = " words";
}
if (char_count == 1) {
charOrChars = " character";
} else {
charOrChars = " characters";
}
if (show_char_count) {
eval('document.' + formName + '.' + fieldName + '.value = char_count');	
//alert ("Char Count:\n\n" + "    " + char_count + charOrChars);
}
else if (show_word_count) {
eval('document.' + formName + '.' + fieldName + '.value = word_count');
//alert ("Word Count:\n\n" + "    " + word_count + wordOrWords);
}
return word_count;
}
//#############################################

//#############################################
// TEXT COUNTER BACKWARDS
function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}
//#############################################

//#############################################
// CONVERTS CARRIAGE RETURNS INTO SPACES
function ConvertBR(input)
{
	var output = "";
	for (var i = 0; i < input.length; i++)
		{
			if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10))
				{
					i++;
					output += " ";
				}
			else
				{
					output += input.charAt(i);
				}
		}
	return output;
}
//#############################################

//#############################################
// CONVERTS CARRIAGE RETURNS INTO <BR>
function ConvertBR2(input)
{
	var output = "";
	for (var i = 0; i < input.length; i++)
		{
			if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10))
				{
					i++;
					output += "<br>";
				}
			else
				{
					output += input.charAt(i);
				}
		}
	return output;
}
//#############################################

//#############################################
// VALIDATES EMAIL ADDRESS
function checkEmail(obj)
{
	if (obj.value == null || obj.value == "")
		{ 
			return true; 
		} 
	var em = obj.value
	var re = new RegExp();
	re = /.+@.+\..+/
	if (em != "") 
		{
			if (!(re.test(em))) 
				{
					return false;
				}
		return true;
		}
}
//#############################################

//#############################################
// VALIDATES PHONE NUMBER
// Pattern matches 9999999999, 999-999-9999, (999)-999-9999, (999) 999-9999, (999)999-9999
function isPhone(strPhone)
{
	var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|(\((\d{3})\)|(\d{3}))[- ]?)?(\d{3})[- ]?(\d{4})$/;
	if (regexp.exec(strPhone))
		{
			return true;
		}
	return false;
}
//#############################################

//#############################################
// ONLY VALID CHARACTERS
function validChars(formName,fieldName,validChars)
{
	var ok = 'yes';
	var strName = eval('document.' + formName + '.' + fieldName + '.value');
	var temp;
	for (var i=0; i<strName.length; i++)
		{
			temp = eval('document.' + formName + '.' + fieldName + '.value.substring(i, i+1);')
			if (validChars.indexOf(temp) == "-1")
				{
					return false
				}
		}
	return true;
}
//#############################################

//#############################################
// FORMAT CURRENCY
function moneyFormat(textObj)
{ 
            var newValue = textObj.value 
            var decAmount = "" 
            var dolAmount = "" 
            var decFlag = false 
            var aChar = "" 

		// ignore all but digits and decimal points 
            for (i=0; i < newValue.length; i++) { 
                        aChar = newValue.substring(i,i+1) 
                        if(aChar >= "0" && aChar <= "9") { 
                                    if(decFlag) { 
                                                decAmount = "" + decAmount + aChar 
                                    } 
                                    else { 
                                                dolAmount = "" + dolAmount + aChar 
                                    } 
                        } 
                        if(aChar == ".") { 
                                    if(decFlag) { 
                                                dolAmount = "" 
                                                break 
                                    } 
                                    decFlag=true 
                        } 
            } 
            // Ensure that at least a zero appears for the dollar amount. 
            if(dolAmount == "") { 
                        dolAmount = "0" 
            } 
            // Strip leading zeros. 
            if(dolAmount.length > 1) { 
                        while(dolAmount.length > 1 && dolAmount.substring(0,1) == "0") { 
                                    dolAmount = dolAmount.substring(1,dolAmount.length) 
                        } 
            } 
            // Round the decimal amount. 
            if(decAmount.length > 2) { 
                        if(decAmount.substring(2,3) > "4") { 
                                    decAmount = parseInt(decAmount.substring(0,2)) + 1 
                                                if(decAmount < 10) { 
                                                            decAmount = "0" + decAmount 
                                                } 
                                                else { 
                                                           decAmount = "" + decAmount 
                                                } 
                                    } 
                                    else { 
                                                decAmount = decAmount.substring(0,2) 
                                    } 
                                    if (decAmount == 100) { 
                                                decAmount = "00" 
                                                dolAmount = parseInt(dolAmount) + 1 
                                    } 
                        } 
                        // Pad right side of decAmount 
                        if(decAmount.length == 1) { 
                                    decAmount = decAmount + "0" 
                        } 
                        if(decAmount.length == 0) { 
                                    decAmount = decAmount + "00" 
                        } 
            // Check for negative values and reset textObj 
            if(newValue.substring(0,1) != '-' || (dolAmount == "0" && decAmount == "00")) { 
                        textObj.value = dolAmount + "." + decAmount 
            } 
            else{ 
                        textObj.value = '-' + dolAmount + "." + decAmount 
            } 
}
//#############################################