//            Title:	Form Functions
//      Description:	various functions for validating forms

function form_validate()
{
    this.textbox_ary = new Array();
    this.textarea_ary = new Array();
    this.email_ary = new Array();
    this.phone_ary = new Array();
    this.error_ary = new Array();
    
    // --- call this once you have included your fields
    this.check_form = function()
    {
        // --- Check the Textboxes
        if(this.textbox_ary.length >= 1)
        {
            for(var i = 0; this.textbox_ary.length > i; i++)
            {
                var textbox_obj = this.textbox_ary[i];
                var check_result = check_textbox(textbox_obj.value);
                if(check_result == false)
                {
                    this.error_ary.push(textbox_obj.label_name);
                }
            }
        }
        
        // --- Check the Textareas
        if(this.textarea_ary.length >= 1)
        {
            for(var i = 0; this.textarea_ary.length > i; i++)
            {
                var textarea_obj = this.textarea_ary[i];
                var check_result = check_textarea(textarea_obj.value);
                if(check_result == false)
                {
                    this.error_ary.push(textarea_obj.label_name);
                }
            }
        }
        
        // --- Check emails
        if(this.email_ary.length >= 1)
        {
            for(var i = 0; this.email_ary.length > i; i++)
            {
                var email_obj = this.email_ary[i];
                var check_result = check_email(email_obj.value);
                if(check_result == false)
                {
                    this.error_ary.push(email_obj.label_name);
                }
            }
        }
        
        // --- Check the Phone Numbers
        if(this.phone_ary.length >= 1)
        {
            for(var i = 0; this.phone_ary.length > i; i++)
            {
                var phone_obj = this.phone_ary[i];
                var check_result = check_phone(phone_obj.value);
                if(check_result == false)
                {
                    this.error_ary.push(phone_obj.label_name);
                }
            }
        }
        
        if(this.error_ary.length >= 1)
        {
            return this.error_ary;
        }
        else
        {
            return false;
        }
    }
    
    // --- Checker Functions
    function check_textbox(textbox_str)
    {
        if(textbox_str.length < 1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    function check_textarea(textarea_str)
    {
        if(textarea_str.length < 1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    function check_email(email_obj)
    {
        if(email_obj.match(/^[a-z0-9+-.%]+@[a-z0-9-.]+\.[a-z]+$/i))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    function check_phone(phone_obj)
    {
        if(phone_obj.match(/^(?:[+]{1}|[0-9]+)[0-9-#()\s]+$/))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    // --- Setters
    this.addtextbox = function($string)
    {
        this.textbox_ary.push($string);
    }
    
    this.addtextarea = function($string)
    {
        this.textarea_ary.push($string);
    }
    
    this.addemail = function($string)
    {
        this.email_ary.push($string);
    }
    
    this.addphone = function($string)
    {
        this.phone_ary.push($string);
    }
}