// Validateform.js - version 1.0 - Olivarubi Version 4.1 website
//
// Copyright (c) 2010. ProductosRubí SLNE
// All rights reserved.
//
// Functions herein are for input form validations in various webpages
// of the Olivarubi Version 4.1 website (written in XHTML 1.0 Transitional
// with CSS 2.1).

// Master form validation function which uses all other functions below.

formValidator = function()
{
    // Setup references to our fields
    var opt1nm = document.getElementById('opt1nm');
    var req1cy = document.getElementById('req1cy');
    var req2em = document.getElementById('req2em');
    var opt2ph = document.getElementById('opt2ph');
    var opt3cm = document.getElementById('opt3cm');

    // Check that input was done on required fields
    if(notEmpty(req1cy, "Please enter your Company Name")===false)
    {
        return false;
    }
    if(notEmpty(req2em, "Please enter your Email Address")===false)
    {
        return false;
    }

    // Check each input in the order that it appears in the form!
    if(isvalName(opt1nm, "Please enter a valid alphabetic Name"))
    {
        if(isvalCmpny(req1cy, "Please enter a valid alphanumeric Company Name"))
        {
            if(isvalEmail(req2em, "Please enter a valid Email Address"))
            {
                if(isvalPhone(opt2ph, "Please enter a valid numeric Phone Number"))
                {
                    if(isvalComm(opt3cm, "Internet URL not allowed in Comments"))
                    {
                        return true;
                    }
                }
            }
        }
    }
    return false;
};

// The following function strips leading and trailing spaces
// from an input string, and returns the substring suitable for other
// validations on its content. Call is 'var valfld = stripBlanks(fld);'

// function for required field check

notEmpty = function(elem, helperMsg)
{
    if(elem.value.length === 0)
    {
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return false;
    }
    return true;
};

// Validate Optional 'Name' Alphabetic, space separated words and initials allowed,
// if input maximum length is 40 characters.

isvalName = function(elem, helperMsg)
{
    // If nothing entered it is OK, else check maximum input length.
    if (elem.value.length === 0)
    {
        return true;
    }
    else
    {
        if(lengthRestriction(elem, 40)===false)
        {
            return false;
        }
    }

    var nameExp = /^[a-zA-Z\.]+/;
    if(elem.value.match(nameExp))
    {
        return true;
    }
    else
    {
        alert(helperMsg);
        elem.focus();
        return false;
    }
};

// Validate Required 'Company' Alphanumeric, space separated words and '.' and ',' and '&'
// allowed, input maximum length is 40 characters.

isvalCmpny = function(elem, helperMsg)
{
    // We have already checked that something was entered,
    // so check its maximum length.
    if(lengthRestriction(elem, 40)===false)
    {
        return false;
    }

    var cmpnyExp = /^[a-zA-Z0-9,&\.]+/;
    if(elem.value.match(cmpnyExp))
    {
        return true;
    }
    else
    {
        alert(helperMsg);
        elem.focus();
        return false;
    }
};

// Validate Required 'Email' Special Format, input maximum length is 40 characters.

isvalEmail = function(elem, helperMsg)
{
    // We have already checked that something was entered,
    // so check its maximum length.
    if(lengthRestriction(elem, 40)===false)
    {
        return false;
    }

    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(elem.value.match(emailExp))
    {
        return true;
    }
    else
    {
        alert(helperMsg);
        elem.focus();
        return false;
    }
};

// Validate Optional 'Phone' Numeric, '+' allowed first character,
// if input maximum length is 25 characters.

isvalPhone = function(elem, helperMsg)
{
    // If nothing entered it is OK, else check maximum input length.
    if (elem.value.length === 0)
    {
        return true;
    }
    else
    {
        if(lengthRestriction(elem, 25)===false)
        {
            return false;
        }
    }

    var numericExpression = /^\+?[0-9]+$/;
    if(elem.value.match(numericExpression))
    {
        return true;
    }
    else
    {
        alert(helperMsg);
        elem.focus();
        return false;
    }
};

// Validate Optional 'Comments' Freeform textarea, 'http:' not allowed in text,
// No need to check for maximum length.

isvalComm = function(elem, helperMsg)
{
    // If nothing entered it is OK, else proceed to pattern check.
    if (elem.value.length === 0)
    {
        return true;
    }

    var commExp = /(http:)+/;
    if(!elem.value.match(commExp))
    {
        return true;
    }
    else
    {
        alert(helperMsg);
        elem.focus();
        return false;
    }
};

// function for maximum input length check

lengthRestriction = function(elem, max)
{
    var uInput = elem.value;
    if(uInput.length <= max)
    {
        return true;
    }
    else
    {
        alert("Please enter no more than " +max+ " characters");
        elem.focus();
        return false;
    }
};
