<!--

// Check whether string s is empty.

function isEmpty(s)
{
    return ((s == null) || (s.length == 0))
}

//
// Is a string empty? Note- gets rid of any trailing or leading spaces.
//

function isBlank(str)
{
    if(str == null || str == "")
        return true;
    if ( stripTrailingAndLeadingSpaces(str) == "")
        return true;
    else
        return false;
}//end function




//
//  Strip any spaces from the start or end of a string.
//
function stripTrailingAndLeadingSpaces(str)
{
    size = str.length;
    while (str.slice(0,1) == " ") {        //Strip leading spaces
        str = str.substr(1,size-1);
        size = str.length;
    }
    while(str.slice(size-1,size)== " ") {  //Strip trailing spaces
        str = str.substr(0,size-1);
        size = str.length;
    }
    return str;
}//function


function removeBlanks(s) {
    r = "";
    for (i=0; i < s.length; i++) {
        if (s.charAt(i) != '\n'
            && s.charAt(i) != '\r'
            && s.charAt(i) != '\t'
            && s.charAt(i) != ' '

    ) {
            r += s.charAt(i);
        }
    }
    return r;
}

//
function isEmailValidFormat(str)
{
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
        return false;
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
        return false;
    if (str.indexOf(at,(lat+1))!=-1)
        return false;
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
        return false;
    if (str.indexOf(dot,(lat+2))==-1)
        return false;
    if (str.indexOf(" ")!=-1)
        return false;
    return true;
}//end function


// Validate FORMAT of UK postcode. Returns error string or "POSTCODE OKAY".
//
function isPostCodeValidFormat(postcode)
{
    size = postcode.length;
    postcode = postcode.toUpperCase();          //Change to uppercase

    postcode = stripTrailingAndLeadingSpaces(postcode);

    if (size < 6 || size > 8)           //Code length rule
        return postcode + " is not a valid postcode - wrong length";

    if (!(isNaN(postcode.charAt(0))))       //leftmost character must be alpha character rule
        return postcode + " is not a valid postcode - cannot start with a number";

    if (isNaN(postcode.charAt(size-3)))     //first character of inward code must be numeric rule
        return postcode + " is not a valid postcode"; // - alpha character in wrong position"; --tk message simplified

    if (!(isNaN(postcode.charAt(size-2))))  //second character of inward code must be alpha rule
        return postcode + " is not a valid postcode - number in wrong position";

    if (!(isNaN(postcode.charAt(size-1))))  //third character of inward code must be alpha rule
        return postcode + " is not a valid postcode - number in wrong position";

    if (!(postcode.charAt(size-4) == " "))  //space in position length-3 rule
        return postcode + " is not a valid postcode - no space or space in wrong position";

    count1 = postcode.indexOf(" ");
    count2 = postcode.lastIndexOf(" ");

    if (count1 != count2)              //only one space rule
        return postcode + " is not a valid postcode - only one space allowed";

    return "POSTCODE OKAY";
}//end function


// Check phone number; allows international numbers, e.g. +44(0)20 8358 9234 (i.e. allows brackets, spaces and a plus sign).

function isPhoneNumberValid(phonenumb) {
	var i=0;
	var p=phonenumb.split('');	// split string into array, empty string param means split on every char.
	for (i=0; i<p.length; i++)
		if (p[i]!='0' && p[i]!='1'&& p[i]!='2' && p[i]!='3'&& p[i]!='4' && p[i]!='5' && p[i]!='6' && p[i]!='7'&&
			p[i]!='8' && p[i]!='9' && p[i]!='+' && p[i]!='(' && p[i]!=')' && p[i]!=' ')
			return false;
	return true;
}

// Returns true if character c is a digit
// (0 .. 9).
function isDigit (c)
{
    return ((c >= "0") && (c <= "9"))
}


function isInteger (str)
{
    var i;

    // 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 < str.length; i++)
    {
        // Check that current character is number.
        var c = str.charAt(i);

        if (!isDigit(c))
            return false;
    }

    return true;    // All characters are numbers.
}//end isInteger()

function isFloat (s)
{
    var i;
    var seenDecimalPoint = false;

    if (isEmpty(s))
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // 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 ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// find a value in an array, and return the index of that value (or -1 if not found)
function findArrayIndexOfValue(p_array, p_value)
{
    for (i=0; i<p_array.length; i++)
        if (p_array[i] == p_value)
            return i;
    return -1; // not found
}
///////
//  For a given form, find the index into the form's elements array of a property of name: str.
//  With JavaScript's associative array access, shouldn't really need this, but...
//
function getIdxOf(frm, str)
{
    for (var j=0; j<frm.elements.length; j++) {
        if (frm.elements[j].name == str)
            return j;
    }
    return -1;
}

function round(number,X) {
    // rounds number to X decimal places, defaults to 2
    X = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}


//   -->
