﻿// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{     
    var i;
    // Is s empty?
    if (isEmpty(s)) return true;
    var whitespace = " \t\n\r";

    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 to add dropdown items
function AddItem(dropdownField,Text,Value)
{
    // Create an Option object        

    var opt = document.createElement("option");

    // Add an Option object to Drop Down/List Box
    dropdownField.options.add(opt);

    // Assign text and value to Option object
    opt.text = Text;
    opt.value = Value;

}

function isEmail (s)
{     
    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { 
        i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) 
        return false;
    else
        i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { 
        i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) 
        return false;
    else
        return true;
}

function trim(myString) 
{
    while (myString.substring(0,1) == ' ')
    {
        myString = myString.substring(1, myString.length);
    }
    
    while (myString.substring(myString.length-1, myString.length) == ' ')
    {
        myString = myString.substring(0,myString.length-1);
    }
    return myString;
}

//Used in the case of Contact Us page phone validation
function isPhoneNumber(PhoneField, Country)
{
    if(Country == "1" || Country == "3")
    {
        return checkUSPhone(PhoneField, false);
    }
    /*else if(Country == "2")
    {
        return checkUKPhone(PhoneField, false);
    }
    else if(Country == "4")
    {
        return checkIndiaPhone(PhoneField, false);
    }*/
    else
    {
        return true;
    }
}

function checkUSPhone (theField, emptyOK)
{ 
    if (checkUSPhone.arguments.length == 1) 
        emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) 
        return true;
    else
    { 
        var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
        if (!isUSPhoneNumber(normalizedPhone, false))
            return warnInvalid (theField, iUSPhone);
        else
        { // if you don't want to reformat as (123) 456-789, comment next line out
            theField.value = reformatUSPhone(normalizedPhone)
            return true;
        }
    }
}

//Check for UK phone number validity
function checkUKPhone (theField, isBlankOk) 
{
    telephoneNumber = theField.value;
    // Convert into a string and check that we were provided with something
    var telnum = telephoneNumber + " ";
    if (telnum.length == 1 && !isBlankOk) 
    {
        theField.focus();
        return false
    }
    telnum.length = telnum.length - 1;

    // Don't allow country codes to be included (assumes a leading "+")
    var exp = /^(\+)[\s]*(.*)$/;
    if (exp.test(telnum) == true) 
    {
        warnInvalid(theField, iUKPhone);
        return false;
    }

    // Remove spaces from the telephone number to help validation
    while (telnum.indexOf(" ")!= -1) 
    {
        telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
    }

    // Remove hyphens from the telephone number to help validation
    while (telnum.indexOf("-")!= -1) 
    {
        telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
    }

    // Now check that all the characters are digits
    exp = /^[0-9]{10,11}$/
    if (exp.test(telnum) != true) 
    {
        warnInvalid(theField, iUKPhone);
        return false;
    }

    // Now check that the first digit is 0
    exp = /^0[0-9]{9,10}$/
    if (exp.test(telnum) != true) 
    {
        warnInvalid(theField, iUKPhone);
        return false;
    }

    // Finally check that the telephone number is appropriate.
    exp = /^(01|02|03|05|070|077|07624|078|079)[0-9]+$/;
    if (exp.test(telnum) != true) 
    {
        warnInvalid(theField, iUKPhone);
        return false;
    }

    // Telephone number seems to be valid - return the stripped telehone number
    return true;
}

function checkIndiaPhone(theField, isBlankOk)
{
    telephoneNumber = theField.value;
    // Convert into a string and check that we were provided with something
    var telnum = telephoneNumber + " ";
    if (telnum.length == 1 && !isBlankOk)
    {
        theField.focus();
        return false
    }
    telnum = telnum.substring(0, telnum.length - 1);

    if(isInteger(telnum) && (telnum.length == 10 || (telnum.length >= 9 && telnum.length <= 13)))
        return true;
    else    
        warnInvalid(theField, iIndiaPhone);
}

function isUSPhoneNumber (s)
{ 
    if (isEmpty(s))
        if (isUSPhoneNumber.arguments.length == 1) 
            return defaultEmptyOK;
        else
            return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function stripCharsInBag (s, bag)
{ 
    var i;
    var 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 isInteger (s)
{ 
    var i;

    if (isEmpty(s))
        if (isInteger.arguments.length == 1) 
            return defaultEmptyOK;
        else 
            return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) 
            return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit
// (0 .. 9).

function isDigit (c)
{ 
    return ((c >= "0") && (c <= "9"))
}
// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.
function warnInvalid (theField, s)
{ 
    theField.focus()
    theField.select()
    alert(s)
    return false
}

