//  Name  : ft_formValidate
//  Version 1.2 Original Author: Marion Dorsett - http://www.FigmentThinking.com
//  Modified: Mike Sharp - rdcpro@hotmail.com
//  Version: 1.2-modified
//  Changes to allow error message to show friendly field name, which is passed in the second argument.
//  A result of this change is the IsMatch doesn't work (which is ok for this particular instance).
//  IsMatch needs to be modified so the match field name is part of the IsMatch parameter, like the InRange.
function ft_formValidate(){
    ft_result=true;
    ft_errors='';
    c=ft_formValidate.arguments;

    for(i=0;i<=(c.length-1);i+=3)
    {
        o=document.getElementById(c[i]);
        if(o)
        {
            fieldValue=o.value;
            c[i+2]=c[i+2].toLowerCase();
            isRequired=c[i+2].substring(0,1);
            isEmail=c[i+2].indexOf('isemail');
            isNum=c[i+2].indexOf('isnum');
            inRange=c[i+2].indexOf('inrange');
            isMatch=c[i+2].indexOf('ismatch');
            if(isRequired=='r')
            {
                hasValue=false;
                switch(o.type)
                {
                    case 'radio':
                    case 'checkbox':
                        f=document.forms;
                        for(e=0;e<=(f.length-1);f++)
                        {
                            if(f[e].elements[o.name].length)
                            {
                                isChecked=false;
                                    for(s=0;s<=(f[e].elements[o.name].length-1);s++)
                                    {   
                                        if(f[e].elements[o.name][s].checked)
                                        {
                                            isChecked=true;
                                            break;
                                        }
                                    }
                                if(isChecked)
                                {
                                    hasValue=true;
                                }
                            }
                            else if(o.type=='checkbox')
                            {
                                isChecked = o.checked;
                                if(isChecked)
                                {
                                    hasValue=true;
                                }
                                else
                                {
                                    hasValue=false;
                                }
                            }
                            else
                            {
                                hasValue=true;
                            }
                        }
                        break;
                    default:
                        if(fieldValue!='')
                        {
                            hasValue=true;
                        }
                        break;
                }
                if(!hasValue)
                {
                    ft_errors+=c[i+1]+' is required.\n';
                }
            }
            if(isEmail>=0)
            {
                if(fieldValue!='')
                {
                    if(fieldValue.search(/.{1,}@{1}.{1,}\.{1}.{2,}/gi)==-1)
                    {
                        ft_errors+=c[i+1]+' must be an email address.\n';
                    }
                }
            }
            if(isNum>=0)
            {
                if(fieldValue!='')
                {
                    if(isNaN(parseFloat(fieldValue)))
                    {
                        ft_errors+=c[i+1]+' must be a number.\n';
                    }
                }
            }
            if(inRange>=0)
            {
                if(fieldValue!='')
                {
                    rTemp=c[i+2].split(':',2);
                    rStart=parseFloat(rTemp[0].substring(8,c[i+2].indexOf(':')));
                    if(isNaN(rStart))
                    {
                        rStart=0;
                    }
                    rStop=parseFloat(rTemp[1]);
                    fieldNumber=parseFloat(fieldValue);
                    if(isNaN(fieldNumber))
                    {
                        fieldNumber=0;
                    }
                    if(fieldNumber<rStart||fieldNumber>rStop)
                    {
                        ft_errors+=c[i+1]+' must be between '+rStart+' and '+rStop+'.\n';
                    }
                }
            }
            if(isMatch >= 0)
            {
                vField = document.getElementById(c[i]);
                mField = document.getElementById(c[i+1]);
                if(vField.value!=mField.value)
                {
                    ft_errors+=c[i+1]+' doesn\'t match '+c[i+1]+'.\n';
                }
            }
        }
        else
        {
            continue;
        }
    }

    if(ft_errors)
    {
        alert(ft_errors);
        ft_result = false;
    }
    else
    {
        document.forms[0].action = 'http://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
    }

    return ft_result;
}