// CVS ID $Id: jdw_formhelper.js,v 1.2.4.1 2004/05/10 15:42:02 mikelittle Exp $
// CVS ID $Name:  $

function illegalChars(inValue, strLegalChars) {
    // allow Carriage returns
    var i = 0;
    var j = 0;
    var legal = true;
    var legalChars = 0;

    while ((i < inValue.length) && (legal == true)) {
        j = 0;
        legalChars = 0;
        while ((j < strLegalChars.length) && (legal == true)) {
            if (inValue.charAt(i) == strLegalChars.charAt(j)) {
                legalChars++;
            }
            j++;
        }
        if (legalChars < 1) {
            legal = false;
        }
        i++;
    }
    return !legal;
}

function indexOfIllegalChars(inValue, strLegalChars) {
    // allow Carriage returns
    var i = 0;
    var j = 0;
    var legal = -1;
    var legalChars = 0;

    while ((i < inValue.length) && (legal == -1)) {
        j = 0;
        legalChars = 0;
        while ((j < strLegalChars.length) && (legal == -1)) {
            if (inValue.charAt(i) == strLegalChars.charAt(j)) {
                legalChars++;
            }
            j++;
        }
        if (legalChars < 1) {
            legal = i;
        }
        i++;
    }
    return legal;
}

// handy function remove trailing and leading whitespaces
function trimWhiteSpace(strText) {
    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

    return strText;
}

function checkPhone(phoneValue)
{
    if (isEmpty(phoneValue))
        return true;
    var i = 0;
    for (j=0; j<phoneValue.length; j++)
    {
        x = phoneValue.charAt(j);
        if (!charInString(x, validWorldPhoneChars))
        {
            return false;
        }
        else if(charInString(x, digits))
        {
            i++;
        }
    }

    return true;
}

var daysInMonth = [12];
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function isYear (s)
{
    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isMonth (s)
{
    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }
    return isIntegerInRange (s, 1, 12);
}

function isDay (s)
{
    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }
    return isIntegerInRange (s, 1, 31);
}

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! isYear(year, false))
        return false;

    if (! isMonth(month, false))
        return false;

    if (! isDay(day, false))
        return false;

    var intYear = parseInt(year,10);
    var intMonth = parseInt(month,10);
    var intDay = parseInt(day,10);

    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var whitespace = " \t\n\r";
var decimalPointDelimiter = ".";

var validPasswordChars = digits + lowercaseLetters + uppercaseLetters;
var validAcctRefChars = digits + lowercaseLetters + uppercaseLetters;

var phoneNumberDelimiters = "()- ";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";

var defaultEmptyOK = false;

function isEmpty(s)
{
    return ((s == null) || (s.length == 0))
}

function isAnyValue(s)
{
    if (isEmpty(s))
        return defaultEmptyOK;
    return !isEmpty(s)
}

function isWhitespace (s)
{
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i=0; i<s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function stripCharsInBag (s, bag)
{
    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i=0; i<s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function stripCharsNotInBag (s, bag)
{
    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i=0; i<s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function stripWhitespace (s)
{
    return stripCharsInBag (s, whitespace)
}

function charInString (c, s)
{
    for (i=0; i<s.length; i++)
        {   if (s.charAt(i) == c) return true;
        }
    return false
}

function stripInitialWhitespace (s)
{
    var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
        i++;

    return s.substring (i, s.length);
}

function isLetter (c)
{
    return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{
    return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{
    return (isLetter(c) || isDigit(c))
}

function isInteger (s)
{
    var i;

    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }
    for (i=0; i<s.length; i++)
    {
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }
    return true;
}

function isIntegerInRange (s, a, b)
{
    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }
    if (!isInteger(s, false)) return false;

    var num = parseInt(s,10);
    return ((num >= a) && (num <= b));
}

function MFValidate(formField, aBool, prompt)
{
    if (aBool == false)
    {
        formField.focus();
        formField.select();
        alert(prompt);
        formField.focus();
    }
    return aBool;
}

function MFPrompt(prompt)
{
    window.status=prompt;
}

function toLowerCase(s)
{
    return s.toLowerCase;
}

function toUpperCase(s)
{
    return s.toUpperCase;
}

function isSignedInteger (s)
{
    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }
    else
    {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
            startPos = 1;
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isPositiveInteger (s)
{
    var secondArg = defaultEmptyOK;

    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt(s,10) > 0) ) );
}

function isNonnegativeInteger (s)
{
    var secondArg = defaultEmptyOK;

    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt(s,10) >= 0) ) );
}

function isNegativeInteger (s)
{
    var secondArg = defaultEmptyOK;

    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt(s,10) < 0) ) );
}

function isNonpositiveInteger (s)
{
    var secondArg = defaultEmptyOK;

    return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt(s,10) <= 0) ) );
}

function isFloat (s)
{
    var i;
    var seenDecimalPoint = false;

    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }

    if (s == decimalPointDelimiter) return false;

    for (i=0; i<s.length; i++)
    {
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    return true;
}

function isSignedFloat (s)
{
    if (isEmpty(s))
    {
        return defaultEmptyOK;
    }
    else
    {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
            startPos = 1;
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}

function isAlphabetic (s)
{
    var i;

    if (isEmpty(s))
        return defaultEmptyOK;

    for (i=0; i<s.length; i++)
    {
        var c = s.charAt(i);

        if (!isLetter(c))
            return false;
    }

    return true;
}

function isAlphanumeric (s)
{
    var i;

    if (isEmpty(s))
        return defaultEmptyOK;

    for (i=0; i<s.length; i++)
    {
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
            return false;
    }

    return true;
}

function validCharsForDeliveryname (s){

	var validChars = "-' " ;
	var validDeliveryNameChars = digits + lowercaseLetters + uppercaseLetters + validChars ;
	
	if(illegalChars(s,validDeliveryNameChars)) {
		return false;
	}
	 
	return true;

}
