function IsNumeric(sText) {
	return !isNaN(sText);
}

function GetCardType(cCardNumber)
{
	if ( cCardNumber.length == 0 ) 
		return "";
		 
	var cType = cCardNumber.substring(0,1);
	
	if ( cType == "3" )
		return "AmEx"
	else if ( cType == "4" )
		return "Visa"
	else if ( cType == "5" )
		return "Mastercard"
	else if ( cType == "6" )
		return "Discover";
	else
		return "";
}


function IsCreditCard(cCardNumber, cCardType)
{
	var nResult = 0
	var nNumberLen = cCardNumber.length
	var nMult = 2 - (nNumberLen % 2)
	var nLoop;
	var nVal;
	
	
	if( cCardType == 'Visa' ) 
	{
		if( !((cCardNumber.charAt(0) == '4') && (nNumberLen == 16)))
		{
			return false;
		}
	}
	else if( cCardType == 'MasterCard')
	{
		if( !((cCardNumber.charAt(0) == '5') && (nNumberLen == 16)))
		{
			return false;
		}
	}
	else if( cCardType == 'American Express' )
	{
		if( !((cCardNumber.charAt(0) == '3') && (nNumberLen == 15)))
		{
			return false;
		}
	}
	else if	( cCardType == 'Discover' ) 
	{
		if( !((cCardNumber.charAt(0) == '6') && (nNumberLen == 16)))
		{
			return false;
		}
	}
	else
	{
		return false;
	}
	
	for (nLoop = 0; nLoop < nNumberLen; nLoop++)
	{
		nVal = parseFloat(cCardNumber.substring(nLoop, nLoop+1)) * nMult;

		if (nVal >= 10) 
		{
			nVal = nVal - 9 ;
		}
		
		nResult = nResult + nVal;
		nMult = 3 - nMult;
	}

	return ((nResult % 10) == 0)

}

// BEGIN JavaScript Functions Written by:
//    Scott Mitchell
//    mitchell@4guysfromrolla.com
//    http://www.4GuysFromRolla.com
// 
// Modified by dbb 7/11/06.

function FormatNumber(num,decimalNum) {
	if (isNaN(parseInt(num))) {
		return "";
	}
	
	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum));
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	return tmpNumStr;		// Return our formatted string!
}

function LTrim(str) {
	var whitespace = new String(" \t\n\r");
	
	var s = new String(str);
	var j, i;
	
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		// We have a string with leading blank(s)...
		
		j = 0;
		i = s.length;
		
		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
			j++;
		}
		
		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}
	
	return s;
}

function RTrim(str) {
	// We don't want to trip JUST spaces, but also tabs,
	// line feeds, etc.  Add anything else you want to
	// "trim" here in Whitespace
	var whitespace = new String(" \t\n\r");
	
	var s = new String(str);
	var i;
	
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		// We have a string with trailing blank(s)...
		
		i = s.length - 1;       // Get length of string
		
		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
			i--;
		}
		
		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}
	
	return s;
}

function Trim(str) {
	return RTrim(LTrim(str));
}

function Em(str) {
	if (str === null) {
		return true;
	} else {
		return (String(Trim(str)).length === 0);
	}
}

function Len(str) {
	return String(str).length;
}

function Left(str, n) {
	if (n <= 0) {     // Invalid bound, return blank string
		return "";
	} else if (n > String(str).length) {   // Invalid bound, return
		return str;                // entire string
	} else { // Valid bound, return appropriate substring
		return String(str).substring(0,n);
	}
}

function Right(str, n) {
	var iLen;
	
	if (n <= 0) {     // Invalid bound, return blank string
		return "";
	} else if (n > String(str).length) {   // Invalid bound, return
		return str;                     // entire string
	} else { // Valid bound, return appropriate substring
		iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

function Mid(str, start, len) {
	// Make sure start and len are within proper bounds
	if (start < 0 || len < 0) {
		return "";
	}
	
	var iEnd, iLen = String(str).length;
	if (start + len > iLen) {
		iEnd = iLen;
	} else {
		iEnd = start + len;
	}
	
	return String(str).substring(start,iEnd);
}

// END JavaScript Functions Written by:
//    Scott Mitchell

function InStr(strSearch, strSearchFor) {
	return strSearch.indexOf(strSearchFor);
}

function Replace(strSearch, strSearchFor, strReplaceWith) {
	var cNewString = strSearch;
	//window.alert(InStr(cNewString, strSearchFor));
	while (InStr(cNewString, strSearchFor) != -1) {
		cNewString = cNewString.replace(strSearchFor, strReplaceWith);
	}
	
	return cNewString;
}

function setFocus(strField) {
	var oObj;
	
	try {
		oObj = document.getElementById(strField);
		if (oObj) {
			oObj.focus();
		}
	} catch (err) {
		//do nothing
	}
}

function HideCard(cardNum) {
	try 
	{
		if ( cardNum.length <= 4 )
		{
			return "************" + cardNum;
		}
		else
		{
			var lastFour = cardNum.substring(cardNum.length-4, cardNum.length);
			var hidden = "****************" + lastFour;
			if ( hidden.length > cardNum.length)
			{
				hidden = hidden.substring(hidden.length - cardNum.length, hidden.length)
				return hidden;
			}
		}
	}
	catch (err)
	{
		return "****************";
	}
}

function getRadioButtonSelectedValue(cName)
{
	var oRadio = document.getElementsByName(cName);
	
	if (oRadio == null )
		return "";
		
	for (i=0; i<oRadio.length; i++)
	{
		if (oRadio[i].checked)
			return oRadio[i].value;
	}
	
	return "";
}

function isDate(cMonth, cDay, cYear) {
	var aMonths = new Array();
	aMonths[1] = 31;
	aMonths[2] = 28;
	aMonths[3] = 31;
	aMonths[4] = 30;
	aMonths[5] = 31;
	aMonths[6] = 30;
	aMonths[7] = 31;
	aMonths[8] = 31;
	aMonths[9] = 30;
	aMonths[10] = 31;
	aMonths[11] = 30;
	aMonths[12] = 31;
	
	nMonth = parseInt(stripZero(cMonth),10);
	nDay   = parseInt(stripZero(cDay),10);
	nYear  = parseInt(stripZero(cYear),10);
	
	// verify all are valid #s
	if (isNaN(nMonth) || isNaN(nDay) || isNaN(nYear))
		return false;
	
	// verify month in range
	if (nMonth < 1 || nMonth > 12)
		return false;
	
	// verify year in range	
	if (nYear < 1000 || nYear > 9999)
		return false;
	
	// verify days in range for month	
	if (nMonth == 2) {
		if (nDay < 1 || nDay > FebDays(nYear))
			return false;
	} else {
		if (nDay < 1 || nDay > aMonths[nMonth])
			return false;
	}
	
	return true;
}

function stripZero(cString) {
	//while (cString.substr(0,1) == "0")
	while (jsleft(cString,1) == "0")
		cString = cString.substr(1);
	return cString;
}

function jsleft(string, len) {
	if (typeof(string) == "undefined")
		return "";
	else
		return string.substr(0,len);
}

function jsright(string, len) {
	pos = string.length - len;
	return string.substr(pos);
}

function empty(string) {
	return (trim(string).length == 0);
}

function FebDays(nYear) {
	if (nYear % 4 != 0)
		return 28;
	if (nYear % 400 == 0)
		return 29;
	if (nYear % 100 == 0)
		return 28;
	else
		return 29;
}

function formatCurrency(num) {
	var cFormattedNum = formatNumber(num, 2);
	
	if (cFormattedNum === '')
		return '';
	else
		return '$' + cFormattedNum;
}

function setCssClassAttribute(nStyleSheet, cRule, cAttribute, cValue) {
	var styleSheet = document.styleSheets[nStyleSheet];
	
	var rules = styleSheet.cssRules ? styleSheet.cssRules : styleSheet.rules;
	
	var output = "";
	for (i = 0; i < rules.length; i++) {
		output += rules[i].selectorText;
		if (rules[i].selectorText.toLowerCase() === cRule.toLowerCase()) {
			rules[i].style.setAttribute(cAttribute, cValue);
		}
	}
}

function updateLog(cActivityCode, cActivityString) {
    try {
        var oXMLHTTP = getXmlHttpObject();
        oXMLHTTP.open("POST", "/xml/updateLog.asp", false);
        oXMLHTTP.send("<xml>" +
                      "<ActivityCode>" + cActivityCode + "</ActivityCode>" +
                      "<ActivityString>" + cActivityString + "</ActivityString>" +
                      "</xml>");
	} catch (e) {
	    //don't do anything if a log update fails
	}
}

function getXmlHttpObject() {
    var xmlhttp = false;

    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); /* for IE < 5 */
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); /* for IE >= 5 */
        } catch (e) {
            xmlhttp = false;
        }
    }

    /* mozilla & opera */
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }

    return xmlhttp;
}

//XML functions
function selectNodes(xmlDoc, elementPath) {
    if (window.ActiveXObject) {
        return xmlDoc.selectNodes(elementPath)
    } else {
        //x.selectNodes() does not work in FireFox
        var xpe = new XPathEvaluator();
        var nsResolver = xpe.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
        var results = xpe.evaluate(elementPath, xmlDoc, nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

        var aNodes = new Array;

        if (results != null) {
            var oElement = results.iterateNext();
            while (oElement) {
                aNodes.push(oElement);
                oElement = results.iterateNext();
            }
        }

        return aNodes;
    }
}

function selectSingleNode(xmlDoc, elementPath) {
    if (window.ActiveXObject) {
        return xmlDoc.selectSingleNode(elementPath);
    } else {
        //x.selectSingleNode() does not work in FireFox
        var xpe = new XPathEvaluator();
        var nsResolver = xpe.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
        var results = xpe.evaluate(elementPath, xmlDoc, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        return results.singleNodeValue;
    }
}

function selectSingleNodeValue(xmlDoc, elementPath) {
    var oNode = selectSingleNode(xmlDoc, elementPath);

    if (oNode == null) {
        return "";
    } else if (window.ActiveXObject) {
        return oNode.text;
    } else {
        //x.text does not work in FireFox
        return oNode.textContent;
    }
}

//browser detection functions (whenever possible, use object detection instead)
function isChrome() {
    return (InStr(navigator.userAgent, "Chrome") >= 0);
}

function isFirefox() {
    return (InStr(navigator.userAgent, "Firefox") >= 0);
}

function isIE() {
    return (InStr(navigator.userAgent, "MSIE") >= 0);
}

function isSafari() {
    return (InStr(navigator.userAgent, "Safari") >= 0) && (InStr(navigator.userAgent, "Chrome") == -1);
}

