﻿/************************************************************************************
 * File Name:                     fieldValidations.js                               *
 * Author:                        Sandra Mansour                                    *
 * Last Modified Date:            January 31 2008                                   *
 * Purpose:                       This file contains functions that will be used to *
 *                                validate and re-format form fields                *
 ************************************************************************************/
var my_string = new MyString();
 
// function FormatPostalCode() - validates and formats a postal code
function FormatPostalCode(sender, args)
{
    var obj    = document.getElementById(sender.controltovalidate);
    var myreg1 = new RegExp("^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]$") // allows xnxnxn
    var myreg2 = new RegExp("^[a-zA-Z][0-9][a-zA-Z][-]{1}[0-9][a-zA-Z][0-9]$") //allows xnx-nxn
    
    if (obj)
    {
        var data   = obj.value;
        var pcode1 = "";
        var pcode2 = "";
        
        if (data.match(myreg2))//valid
            args.IsValid = true;
        else
        {
            if (data.match(myreg1))//valid
            {
                args.IsValid = true;
                pcode1 = data.substring(0,3);
                pcode2 = data.substring(3);
                data   = pcode1 + "-" + pcode2;
            }            
            else //invalid
                args.IsValid = false;
        }
        obj.value = data; //update data
    }
}

// function FormatTelephone() - validates and formats a postal code
function FormatTelephone(sender, args)
{
    var obj     = document.getElementById(sender.controltovalidate);
        
    var myreg1  = new RegExp("^[0-9]{10}$");                        // allows 4166364598
    var myreg2  = new RegExp("^[(]{1}[0-9]{3}[)]{1}[0-9]{3}[-]{1}[0-9]{4}$");    // allows (416)636-4598
    var myreg3  = new RegExp("^[(]{1}[0-9]{3}[)]{1}[0-9]{7}$");             // allows (416)6364598
    var myreg4  = new RegExp("^[0-9]{6}[-]{1}[0-9]{4}$");                // allows 416636-4598
    
    if (obj)
    {        
        var data = obj.value;
    
        while (data.search(/-/) != -1)
            data = data.replace(/-/, ""); //if the user included any dashes, remove them
            
        if (data.match(myreg1))//valid
        {
            args.IsValid = true;
            
            part1 = data.substring(0,3);
            part2 = data.substring(3,6);
            part3 = data.substring(6);
            data  = "(" + part1 + ")" + part2 + "-" + part3;
        }
        else if (data.match(myreg2))//valid
        {
            args.IsValid = true;
        }
        else if (data.match(myreg3))//valid
        {
            args.IsValid = true;
            
            part1 = data.substring(0,8);
            part2 = data.substring(8);
            data  = part1 + "-" + part2;
        }
        else if (data.match(myreg4))//valid
        {
            args.IsValid = true;
            
            part1 = data.substring(0,3);
            part2 = data.substring(3);
            data  = "(" + part1 + ")" + part2;
        }
        else//invalid data
            args.IsValid = false;
        
        obj.value = data;
    }
}

// function FormatSINNumber() - validates and formats a Social Insurance Number
function FormatSINNumber(sender, args)
{
    var obj     = document.getElementById(sender.controltovalidate);
    var myreg1  = new RegExp("^[0-9]{9}$");                     // allows xxxxxxxxx
    var myreg2  = new RegExp("^[0-9]{3}-[0-9]{3}-[0-9]{3}$");   // allows xxx-xxx-xxx (correct format)
    
    if (obj)
    {
        var data = obj.value;
        
        while (data.search(/ /) != -1)
            data = data.replace(/ /, "");
        
        if (data.match(myreg2))//valid - perfect format
        {
            args.IsValid = true;
        }
        else
        {
            while (data.search(/-/) != -1)
                data = data.replace(/-/, ""); //if the user included any dashes, remove them
            
            if (data.match(myreg1))//valid
            {
                args.IsValid = true;
                
                part1 = data.substring(0,3);
                part2 = data.substring(3,6);
                part3 = data.substring(6);
                data  = part1 + "-" + part2 + "-" + part3;
            }
            else
                args.IsValid = false;
        }
        obj.value = data;
    }
}

// function FormatLicenseNumber() 
//  - validates and formats a Driver's License Number using 
//    the first letter of the provided last name
function FormatLicenseNumber(obj, lname, args)
{ 
    data = obj.value;
    
    while (data.search(/ /) != -1)
        data = data.replace(/ /, "");
        
    data = data.toUpperCase();
    lname_initial  = lname.substring(0,1).toUpperCase(); // get the first initial of the provided Last Name
    data_frst_lttr = data.substring(0,1).toUpperCase();  // get the first letter of the provided driver license
    var myreg1 = new RegExp("^[a-zA-Z]{1}[0-9]{14}$");   // allows Xnnnnnnnnnnnnnn
    var myreg2 = new RegExp("^[a-zA-Z]{1}[0-9]{4}-[0-9]{5}-[0-9]{5}$"); // allows Xnnnn-nnnnn-nnnnn
    var valid  = false;
    
    if (data_frst_lttr == lname_initial)//valid
    {
        if (data.match(myreg2))//valid - perfect format
            valid = true;
        else
        {
            while (data.search(/-/) != -1)
                data = data.replace(/-/, "");
            
            if (data.match(myreg1))//valid
            {
                part1 = data.substring(0,5);
                part2 = data.substring(5,10);
                part3 = data.substring(10);
                data  = part1 + "-" + part2 + "-" + part3;
                
                valid = true;
            }
            else//invalid
                valid = false;
        }
    }
    else//invalid
        valid = false;

    obj.value = data;
    return valid;
}

// function FormatCustomerLicense() - validates and formats the Customer's Driver License
function FormatCustomerLicense(sender, args)
{
    var obj   = document.getElementById(sender.controltovalidate);
    var lname = document.form1.txt_last_name;
    if (obj && lname)
    {
        args.IsValid = FormatLicenseNumber(obj, lname.value);
    }
}

// function FormatCustomerLicense() - validates and formats the Customer's Spouse's Driver License
function FormatSpouseLicense(sender, args)
{
    var obj   = document.getElementById(sender.controltovalidate);
    var lname = document.getElementById("txt_spouse_last_name");
    if (obj)
    {
        args.IsValid = FormatLicenseNumber(obj, lname.value);
    }
}

// function FormatCurrency() - validates and formats currency
function FormatCurrency(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    if (obj)
    {
        data = obj.value;
        
        while (data.search(/ /) != -1)//remove whitespace
            data = data.replace(/ /, "");
        while (data.search(/,/) != -1)//remove commas
            data = data.replace(/,/, "");
        
        var myreg1 = new RegExp("^[0-9]{1,7}[.]{1}[0-9]{1,2}$"); // matches nnnnnnn.nn
        var myreg2 = new RegExp("^[0-9]{1,7}$"); // matches nnnnnnn
        
        if (data.match(myreg1) || data.match(myreg2))
        {
            data = formatNumber(data, 2, ',', '.', '', '', '', '');
            args.IsValid = true;
        }
        else
            args.IsValid = false;
        
        obj.value = data;
    }
}

// function ResidentStatus() - ensures the user selected a resident status
function ResidentStatus(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    var other_txtbox = document.getElementById("div_resident_stat_other");
    var textbox = document.getElementById("txt_resident_stat_other");
    
    if (obj)
    {
        args.IsValid = true;
        
        if (other_txtbox && textbox)
        {
            // check if the user selected "other"
            if (obj.value == "OTHER")
                other_txtbox.style.display = "inline";
            else
            {
                textbox.value = "";
                other_txtbox.style.display = "none";
            }
        }
    }
}

function RelationToPrimary(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    var other_txtbox = document.getElementById("div_spouse_relation_other");
    var textbox = document.getElementById("txt_spouse_relation_other");
    
    if (obj)
    {
        args.IsValid = true;
        if (other_txtbox && textbox)
        {
            if (obj.value == "OTHER")
                other_txtbox.style.display = "inline";
            else
            {
                textbox.value = "";
                other_txtbox.style.display = "none";
            }
        }
    }
}

// function ResidentStatusOtherRequired() - ensures the user enters data and that it's valid data
function ResidentStatusOtherRequired(sender, args)
{
    var obj  = document.getElementById(sender.controltovalidate);
    var obj2 = document.getElementById("ddl_resident_stat");
    
    if (obj2 && obj)
    {
        if (obj2.value != "OTHER")//the user did not select "other", therefore this field is not required
            args.IsValid = true;
        else
        {
            if (obj.value.length == 0)
                args.IsValid = false;
            else
                args.IsValid = true;
        }
    }
}

// function ResidentStatusOtherValid() - ensures the user enters data and that it's valid data
function ResidentStatusOtherValid(sender, args)
{
    var obj  = document.getElementById(sender.controltovalidate);
    var obj2 = document.getElementById("ddl_resident_stat");
    
    if (obj2 && obj)
    {
        if (obj2.value != "OTHER")//the user did not select "other", therefore this field is not required
            args.IsValid = true;
        else
        {
            // ensure it does not contain invalid characters...
            if (my_string.IsAlphaExtra(obj.value))//valid
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    }
}

// function CallIsAlpha() - calls the String IsAlpha function
function CallIsAlpha(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    if (obj)
    {
        args.IsValid = my_string.IsAlpha(obj.value);
    }
}

// function CallIsAlpha() - calls the String IsAlpha function
function CallIsAlphaNumeric(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    if (obj)
    {
        args.IsValid = my_string.IsAlphaNumeric(obj.value);
    }
}

// function CallIsAlphaNumericExtra() - calls the String IsAlphaNumericExtra function
function CallIsAlphaNumericExtra(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    if (obj)
    {
        args.IsValid = my_string.IsAlphaNumericExtra(obj.value);
    }
}

// function CallIsAlphaExtra() - calls the String IsAlphaExtra function
function CallIsAlphaExtra(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    if (obj)
    {
        args.IsValid = my_string.IsAlphaExtra(obj.value);
    }
}

// function PreviousAddressRequired() 
// - function checks if the previous address information is required
// - it is required if the user has resided at his/her current address
//   for less than 2 years.
function PreviousAddressRequired(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    var ddl = document.getElementById("ddl_reside_less_than_two");
    if (obj && ddl)
    {
        if (ddl.value == "1")// field is required
        {
            if (obj.value.length == 0)
                args.IsValid = false;
            else
                args.IsValid = true;
        }
        else if (ddl.value == "0") // field is not required
            args.IsValid = true;
    }
}

// function PreviousEmployerRequired()
// - checks if the previous employer information is required.
// - it is required if the user has been employed at his/her current
//   employer for less than 3 years.
function PreviousEmployerRequired(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    var ddl = document.getElementById("ddl_curr_emp_less_than_two");
    if (obj && ddl)
    {
        if (ddl.value == "1")//field required
        {
            if (obj.value.length == 0)
                args.IsValid = false;
            else
                args.IsValid = true;
        }
        else if (ddl.value == "0")//field not required
            args.IsValid = true;
    }
}

// function ValidateMonths()
// - ensures the user does not enter a value less than 0 or greater
//   than or equal to 12
function ValidateMonths(sender, args)
{
    var obj = document.getElementById(sender.controltovalidate);
    if (obj)
    {
        var myreg = new RegExp("^[0-9]+$");
        if (obj.value.match(myreg))
        {
            if (parseInt(obj.value) >= 0 && parseInt(obj.value) <= 11)
                args.IsValid = true;
            else
                args.IsValid = false;
        }
        else
            args.IsValid = false;
    }
}

// function SpouseInfoRequired() 
// - checks if any of the spouse info is required.
// - this info is only required if the user provides either a last name, first name
//   or both for the spouse area.
function SpouseInfoRequired(sender, args)
{
    var this_obj     = document.getElementById(sender.controltovalidate);
    var spouse_lname = document.getElementById("txt_spouse_last_name");
    var spouse_fname = document.getElementById("txt_spouse_first_name");
    
    if (this_obj && spouse_lname && spouse_fname)
    {
        if (spouse_lname.value.length > 0 || spouse_fname.value.length > 0)//field is required
        {
            if (this_obj.value.length == 0)
                args.IsValid = false;
            else
                args.IsValid = true;   
        }
        else//field is not required
            args.IsValid = true;
    }
}

// function SpouseEmployerRequired()
// - checks if the spouse employer information is required
// - this info is required only if the user provides an employer
//   name and/or occupation
function SpouseEmployerRequired(sender, args)
{
    var this_obj   = document.getElementById(sender.controltovalidate);
    var emp_name   = document.getElementById("txt_spouse_emp_name");
    var occupation = document.getElementById("txt_spouse_emp_occ");
    
    if (this_obj && emp_name && occupation)
    {
        if (emp_name.value.length > 0 || occupation.value.length > 0)//fields are required
        {
            if (this_obj.value.length == 0)
                args.IsValid = false;
            else
                args.IsValid = true;
            
        }
        else//fields not required
            args.IsValid = true;   
    }
}
