if (typeof(v6js_init_quickform) == 'undefined') {
    var v6js_init_quickform = true;

    // :NOTE: Entire content of quickform.js is only loaded if not previously done

var oForm               = null;
var hasSteps            = false;
var wizard_last_step    = false;
var hasTabSteps         = false;


function focusField(field_name, has_steps, oForm){
    if (field_name) {
        if( document.getElementsByName(field_name) ){
            field = (!oForm) ? document.getElementsByName(field_name) : oForm[field_name];

            if(typeof(field) == 'object' && (typeof(field.tagName) != 'undefined') ) {
                // If its a step table
                if (has_steps == true) {
                    hasSteps = true;
                    hideStepTables();

                    var focus_tbl = getStepTable(field);
                    if (focus_tbl) {
                        focus_tbl.style.display = '';
                    }
                }

                if (!field.disabled && isVisible(field) && (typeof(field.tagName) != 'undefined')) {

                    field.focus();
                }
            }
        }
    }
}


function makePhoneNumber(e, field_name) {
    if(!e.srcElement) {
        obj = e.currentTarget;
    } else {
        obj = e.srcElement;
    }

    form = obj.form;

    eval('phone_field = form.' + field_name);
    eval('area_code = form.' + field_name + '_area');
    eval('ph_number = form.' + field_name + '_no');
    phone_field.value = area_code.value + ' ' + ph_number.value;
}



//
// Form Validation Functions
//

var err                     = ["Please make sure you have filled the form in correctly:\n\n"];
var to_focus                = null;
var sf_website_filter       = new RegExp('^(http:\/\/|https:\/\/|ftp:\/\/)+([a-z0-9\.\\-%])+(\/[a-z0-9\.\/\\-~%\+_]*)*(\/\?\\?[a-z0-9\.,\/\\-~%&=\+_]*)?(\/\?#[a-z0-9\.,\/\\-~%&=\+_]+)?$', 'i');
var sf_alpha_filter         = new RegExp('^[a-z\\s]+$', 'i');
var sf_alphanumeric_filter  = new RegExp('^[0-9a-z\\s]+$', 'i');
var sf_numeric_filter       = new RegExp('^[0-9\-\\s]+$', 'i');
var sf_decimal_filter       = /^(\-\$|\$\-|\-|\$)?([0-9\s,]*\.[0-9\s,]+|[0-9\s,]+\.[0-9\s,]*|[0-9\s,]+)$/;

var sf_unlimited_numeric_filter = new RegExp('^([0-9\-\\s]+)|(Unlimited)$', 'i');
var sf_float_filter         = new RegExp('^\\s*-?(\\d+(\\.\\d*)?|\\d*\\.\\d+)\\s*$');
var sf_date_filter          = new RegExp('^$', 'i');
var sf_user_filter          = new RegExp('^[_0-9a-z]+$', 'i');

var sf_phone_filter         = new RegExp('^[0-9 +\-\.]{7,30}$', 'i');
var sf_password_filter      = new RegExp('^\w.+$', 'i');
var sf_corp_email_filter    = new RegExp('(%%Email=Virtual_Account%%)+$', 'i');

var sf_prevent_multiple_checks = { };
var sf_checked_radios = { };
var sf_mandatory_checkbox_values = { };



function checkField(fld_obj, form_element_id) {
    var fld_name = fld_obj.getAttribute('sf_title');

    // this makes the new function sig compatible with old validation methods used in
    // other areas (eg campaign step, wizard)
    if (form_element_id == undefined) {
        form_element_id = fld_name;
    }

    var field_error = '';

    var is_checkbox = false;

    var validate    = (fld_obj.getAttribute('validation') ? fld_obj.getAttribute('validation') : false);
    var mandatory   = fld_obj.getAttribute('mandatory') == 'true';
    var mandatory_failed = false;

    var fld_val = null;

    if ((fld_obj.tagName.toLowerCase() == 'input') && (fld_obj.type == 'checkbox')) {
        var is_checkbox = true;
        // var name = fld_obj.name.slice(0, fld_obj.name.indexOf('['));
        if (mandatory && (typeof sf_mandatory_checkbox_values[form_element_id] == 'undefined')) {
            sf_mandatory_checkbox_values[form_element_id] = [ ];
        }
        fld_val = fld_obj.value;
        if (mandatory && fld_obj.checked) {
            sf_mandatory_checkbox_values[form_element_id].push(fld_val);
        }
    } else if (fld_obj.tagName.toLowerCase() != "select") {
        fld_val  = fld_obj.value.trim();
    } else if ((fld_obj['selectedIndex'] != -1) && (typeof fld_obj[fld_obj.selectedIndex] != 'undefined')) {
        fld_val = fld_obj[fld_obj.selectedIndex].value.trim();
    }

    var fld_val_len = fld_val ? fld_val.length : 0;


    if (mandatory && !fld_val) {
        mandatory_failed = true;
    } else if (mandatory && fld_obj.type == 'radio' && !fld_obj.checked && !sf_checked_radios[fld_name]) {
        sf_checked_radios[fld_name] = true;
        mandatory_failed = true;
        var radios = document.getElementsByTagName('input');
        for (var i = 0; i < radios.length; i ++) {
            if (radios[i].type == 'radio') {
                if ((radios[i].getAttribute("sf_title") == fld_name) && radios[i].checked) {
                    mandatory_failed = false;
                    break;
                }
            }
        }
    }


    if (!is_checkbox) {
        if ((mandatory && ! fld_val_len) || mandatory_failed) {
            field_error += " - '" + fld_name + "' is a Mandatory field\n";

            // Phone Number - Cant focus hidden field
            if (validate == "11") {
                ph_fld = eval('oForm.' + fld_obj.name + '_no');
                to_focus = (to_focus==null) ? ph_fld : to_focus;
            } else {
                to_focus = (to_focus==null) ? fld_obj : to_focus;
            }
        }
    }

    if ((validate != false) && fld_val.length) {

        switch( validate ){

            case "email":
            case "SF_EMAIL":
            case "1":
            case "SF_UNIQUE_EMAIL":
            case "22":
                if(!validateEmail(fld_val)) {
                    field_error += " - '" + fld_val + "' is not a valid Email Address\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            case "website":
            case "SF_WEBSITE":
            case "2":
                if(fld_val == 'http://') {
                    fld_obj.value = '';
                    fld_val       = '';
                } else {
                    if( ! sf_website_filter.test(fld_val)){

                        if(sf_website_filter.test('http://' + fld_val)){
                            fld_obj.value = 'http://' + fld_obj.value;
                        } else {
                            field_error += " - '" + fld_val + "' is not a valid Website Address\n";
                            to_focus = (to_focus==null)?fld_obj:to_focus;
                        }
                    }
                }
                break;

            case "alpha":
            case 3:
                if( ! sf_alpha_filter.test(fld_val) ){
                    field_error += " - '" + fld_name + "'  must only contain Alphabetic characters\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            case "SF_ALPHANUMERIC":
            case "4":
                if( ! sf_alphanumeric_filter.test(fld_val) ){
                    field_error += " - '" + fld_name + "'  must only contain Alphanumeric characters\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            case "SF_NUMERIC":
            case "5":
                if( ! sf_numeric_filter.test(fld_val) ){
                    field_error += " - '" + fld_name + "' must be a number\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;
            case "SF_UNLIMITED_NUMERIC":
            case "18":
                if( ! sf_unlimited_numeric_filter.test(fld_val) ){
                    field_error += " - '" + fld_name + "' must be a number\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            case "SF_DECIMAL":
            case "19":
                if( ! sf_decimal_filter.test(fld_val) ){
                    field_error += " - '" + fld_name + "' must be a number\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            case "8":
                emails = fld_val.split(',');
                error  = false;

                for(a=0; a<emails.length; a++ ) {
                    if(!validateEmail(emails[a])) {
                        error = true;
                    }
                }

                if (error == true) {
                    field_error += " - One of the Emails in the '" + fld_name + "' field is not Valid\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            // Sf Phone validation
            case "SF_PHONE_NUMBER":
            case "11":
                if (!sf_phone_filter.test(fld_val)) {
                    field_error += " - '" + fld_val + "' is not a valid Phone Number\n";
                    //Only works for phone fields.
                    //{
                     //ph_fld = eval('oForm.' + fld_obj.name + '_no');
                     //to_focus = (to_focus==null) ? ph_fld:to_focus;
                     to_focus = (to_focus==null)?fld_obj:to_focus;
                    // }
                }
                break;

            // Sf Phone validation
            case "SF_MULTIPLE_PHONE_NUMBER":
            case "26":

                phones_array = fld_val.split(',');

                for(i in phones_array) {
                    if (typeof(phones_array[i]) == 'function') continue;
                    if (phones_array[i].trim() && !sf_phone_filter.test(phones_array[i].trim())) {
                        field_error += " - '" + phones_array[i] + "' is not a valid Phone Number\n";
                        to_focus = (to_focus==null)?fld_obj:to_focus;
                        break;
                    }
                }

                break;

            case "SF_CORP_EMAIL":
            case "21":
                if(!validateEmail(fld_val) && !sf_corp_email_filter.test(fld_val)) {
                    field_error += " - '" + fld_val + "' is not a valid Email Address or Wildcard\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            // Sf Multiple Email/Phone validation
            case "SF_MULTIPLE_ECAF":
            case "20":

                var ecaf_array = fld_val.split(','), ecaf_errors = '';

                for (i in ecaf_array) {
                    if (typeof(ecaf_array[i]) == 'function') continue;
                    if (ecaf_array[i].trim() && !validateEmail(ecaf_array[i].trim()) && !sf_phone_filter.test(ecaf_array[i].trim())) {
                        ecaf_errors += " - > " + ecaf_array[i] + "\n";
                        to_focus = (to_focus==null)?fld_obj:to_focus;
                    }
                }

                if (ecaf_errors != '') {
                    field_error += " - The '" + fld_name + "' field contains the following invalid Recipients:\n" + ecaf_errors;
                }

                break;

            // Sf Float validation
            case "12":
                if( ! sf_float_filter.test(fld_val) ){
                    field_error += " - '" + fld_val + "' must be a number\n";
                    to_focus = (to_focus==null)?fld_obj:to_focus;
                }
                break;

            //Credit Card validation
            case "13":
                if (Sub_Package.SUB_PKG_BETA_DALLAS) {
                    var cc_num = document.getElementById(fld_obj.id + '_fld[number_1]').value;
                } else {
                    var cc_num = document.getElementById(fld_obj.id + '_fld[number_1]').value +
                                 document.getElementById(fld_obj.id + '_fld[number_2]').value +
                                 document.getElementById(fld_obj.id + '_fld[number_3]').value +
                                 document.getElementById(fld_obj.id + '_fld[number_4]').value;
                }

                var cc_type = document.getElementsByName(fld_obj.id + '_fld[cc_type]');
                var cc_type_value = 0;
                var cc_type_obj;
                var cc_type_error = false;
                for (var i=0; i < cc_type.length; i++) {
                    if(cc_type[i].checked) {
                        cc_type_obj   = cc_type[i];
                        cc_type_value = cc_type_obj.value;
                    }
                }
                // check 'Name on Card' field
                var cc_name = document.getElementById(fld_obj.id + '_fld[name]');
                if (cc_name.value.length == 0) {
                    error        = true;
                    field_error += " - Please enter the Name on the Card\n";
                    to_focus     = (to_focus==null)?cc_name:to_focus;
                }

                // check credit card type radio buttons
                if (cc_type_value == 0) {
                    error        = true;
                    field_error += " - Please enter a valid Credit Card number\n";
                    cc_type_error = true;
                }

                // check valid credit card number
                if (!creditCardIsValid(cc_num, cc_type)) {
                    error        = true;
                    if (!cc_type_error) {
                        field_error += " - Please enter a valid Credit Card number\n";
                    }
                    to_focus = (to_focus == null)?document.getElementById(fld_obj.id + '_fld[number_1]'):to_focus;
                } else if(getCreditCardType(cc_num) != cc_type_value) {
                    error        = true;
                    field_error += " - The Credit Card number does not match the selected Credit Card type\n";
                    to_focus     = (to_focus == null) ? cc_type_obj : to_focus;
                }

                var exp_month     = document.getElementById(fld_obj.id + '_fld[exp_month]');
                var exp_year      = document.getElementById(fld_obj.id + '_fld[exp_year]');
                var ccv           = document.getElementById(fld_obj.id + '_fld[ccv]');
                var date_obj      = new Date();
                var current_year  = parseInt(date_obj.getFullYear().toString().substr(2,2));
                var current_month = date_obj.getMonth() + 1;
                var month_error   = false;
                var year_error    = false;

                if(exp_month.value.charAt(0) == '0') {
                    exp_month.value = exp_month.value.substr(1,1);
                }
                if(exp_year.value.charAt(0) == '0') {
                    exp_year.value = exp_year.value.substr(1,1);
                }

                if(isNaN(parseInt(exp_month.value)) || parseInt(exp_month.value) < 1 || parseInt(exp_month.value) > 12) {
                    error        = true;
                    month_error  = true;
                    field_error += " - Please enter a valid Credit Card expiration month\n";
                    to_focus     = (to_focus == null) ? exp_month : to_focus;
                }

                // update to reflect current year
                if(isNaN(parseInt(exp_year.value)) || parseInt(exp_year.value) < parseInt(current_year) || parseInt(exp_year.value) > 50) {
                    error        = true;
                    year_error   = true;
                    field_error += " - Please enter a valid Credit Card expiration year\n";
                    to_focus     = (to_focus == null) ? exp_year : to_focus;
                }

                // ensure valid combination
                if (!month_error && !year_error &&
                    parseInt(exp_month.value) < current_month &&
                    parseInt(exp_year.value) <= current_year
                ) {
                    error        = true;
                    field_error += " - Please enter a valid Credit Card expiry month and year\n";
                    to_focus     = (to_focus == null) ? exp_month : to_focus;
                }

                if(ccv && isNaN(parseInt(ccv.value))) {
                    error        = true;
                    field_error += " - Please enter a valid Credit Card CCV number\n";
                    to_focus     = (to_focus == null) ? ccv : to_focus;
                }

                // DD Checkbox (T&C's)
                if (Sub_Package.SUB_PKG_BETA_DALLAS) {
                    var dd_checkbox = document.getElementById('dd_tnc');
                    if (dd_checkbox && typeof(dd_checkbox) && dd_checkbox.getAttribute('mandatory') == 'true' && !dd_checkbox.checked) {
                        field_error += " - You must agree to the Direct Debit Terms and Conditions\n";
                    }
                }

                break;

            // Bank Account Direct Debit validation
            // look values in constants.php
            case "24":
                if (Sub_Package.SUB_PKG_BETA_DALLAS) {
                    // Bank Name Validation
                    var bank_name = document.getElementById(fld_obj.id + '_fld[bank_name]');
                    if (typeof(bank_name) != 'undefined' && bank_name.value.length == 0) {
                        field_error += " - Please enter a valid Bank Name\n";
                    }

                    // Account Name Validation
                    var account_name = document.getElementById(fld_obj.id + '_fld[bank_acc_name]');
                    if (typeof(account_name) != 'undefined' && account_name.value.length == 0) {
                        field_error += " - Please enter a valid Account Name\n";
                    }

                    // BSB Validation
                    var bsb_bank_state  = document.getElementById(fld_obj.id + '_fld[bank_bsb_parent_state]').value;
                    var bsb_bank_branch = document.getElementById(fld_obj.id + '_fld[bank_bsb_branch]').value;
                    var bsb             = bsb_bank_state + bsb_bank_branch;

                    if (bsb.length == 0) {
                        field_error += " - Please enter a valid BSB Number\n";
                    } else {
                        if (bsb.length == 6) {
                            var valid_bsb = new RegExp('^[0-9]+$', 'g');
                            if (!valid_bsb.test(bsb)){
                                field_error += " - BSB should only contain digits\n";
                            }
                        } else {
                            field_error += " - Length of BSB should only be 6 digits\n";
                        }
                    }

                    // Account Number Validation
                    var bank_account_number = document.getElementById(fld_obj.id + '_fld[bank_acc_number]');
                    if (typeof(bank_account_number) != 'undefined') {
                        if (bank_account_number.value.length == 0) {
                            field_error += " - Please enter a valid Bank Account Number\n";
                        } else {
                            if (bank_account_number.value.length >= 16) {
                                field_error += " - Bank Account Number should only be 15 digits\n";
                            } else {
                                var valid_bank_number = new RegExp('^[0-9]+$', 'g');
                                if (!valid_bank_number.test(bank_account_number.value)){
                                    field_error += " - Bank Account Number should only contain digits\n";
                                }
                            }
                        }
                    }

                    // DD Checkbox (T&C's)
                    var dd_checkbox = document.getElementById('dd_tnc');
                    if (!dd_checkbox || (dd_checkbox && typeof(dd_checkbox) && !dd_checkbox.checked)) {
                        field_error += " - You must agree to the Direct Debit Terms and Conditions\n";
                    }
                }
                break;

            // CAF - Credit Authority Form
            // look values in constants.php
            case "25":
                // All fields on this page are mandatory

                // 1 more then actual (2)
                var total_suppliers = 3;
                var form_field_name_array = new Array('caf_supplier_name', 'caf_contact_number', 'caf_invoice_number', 'caf_invoice_date', 'caf_invoice_total');
                var caf_supplier_acknowledgement = document.getElementById('caf_supplier_acknowledgement');

                if (caf_supplier_acknowledgement && !caf_supplier_acknowledgement.checked) {
                    field_error += ' - ' + caf_supplier_acknowledgement.getAttribute('sf_title') + " is Mandatory \n";
                }

                for (var i = 1; i <= total_suppliers; ++i) {

                    for (var j = 0; j < form_field_name_array.length; ++j) {
                        var field_string = form_field_name_array[j] + '_' + i;
                        var field_obj    = document.getElementById(field_string);

                        if (field_obj && typeof(field_obj) != 'undefined') {
                            if (field_obj.value.length == 0) {
                                field_error += ' - ' + field_obj.getAttribute('sf_title') + " is Mandatory \n";
                            } else {
                                if (field_obj.value.length > 0) {
                                    // trim spaces from the value
                                    var caf_field_value = field_obj.value.trim();

                                    if (form_field_name_array[j] == 'caf_invoice_date') {
                                        var valid_date_format = '(dd/mm/yyyy)';
                                        var is_date_valid = isValidDate(caf_field_value);

                                        if (is_date_valid < 0) {
                                            switch (is_date_valid) {
                                                case -1:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " Invalid date format " + valid_date_format + " \n";
                                                    break;
                                                case -2:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " Invalid day format " + valid_date_format + " \n";
                                                    break;
                                                case -3:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " Invalid number of days check for leap year \n";
                                                    break;
                                                case -4:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " Invalid month format " + valid_date_format + " \n";
                                                    break;
                                                case -5:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " Invalid Year " + valid_date_format + " \n";
                                                    break;
                                                case -6:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " Year is not valid \n";
                                                    break;
                                                case -7:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " is greater than today\'s date\n";
                                                    break;
                                                default:
                                                    field_error += ' - ' + field_obj.getAttribute('sf_title') + " Invalid date format " + valid_date_format + " \n";
                                            }
                                        }
                                    }

                                    // Validate invoice totals
                                    if (form_field_name_array[j] == 'caf_invoice_total') {
                                        // make sure we don't have any html code or 2 "$" signs in the amount text
                                        var re = /[^0-9\.]+/g;
                                        caf_field_value = caf_field_value.replace(re, '');

                                        if (caf_field_value > 0) {
                                            if (caf_field_value.indexOf('$') == -1) {
                                                caf_field_value = '$' + caf_field_value;
                                            }
                                            if (!isValidCurrency(caf_field_value)) {
                                                field_error += ' - ' + field_obj.getAttribute('sf_title') + " Invalid currency format ($100.00) \n";
                                            } else {
                                                field_obj.value = caf_field_value;
                                            }
                                        } else {
                                            field_error += ' - ' + field_obj.getAttribute('sf_title') + " amount should be greater than $0.00 \n";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Supplier Name + Invoice Number + Invoice Date should be unique
                // Create a large string and store that in an array
                if (validateInvoiceDetails()) {
                    field_error += " - Supplier Name, Invoice Number and Invoice Date must be unique.\n";
                }

                break;
            case "27":
                /*
                // Validate ABN/ACN numbers
                var abn_acn_field = document.getElementById('abn_acn');

                if (typeof(abn_acn_field) && abn_acn_field) {
                    var abn_acn = abn_acn_field.value;
                    if (abn_acn.length > 0){
                        // Remove spaces from the number
                        abn_acn = abn_acn.replace(/[ \t]+/g, '');
                        var valid_business_number = new RegExp('^[0-9]+$', 'g');
                        if (valid_business_number.test(abn_acn)) {
                            switch (abn_acn.length){
                                case 11:
                                    if (!validateABNumber(abn_acn)) {
                                        field_error += " - Your ABN number is invalid \n"
                                    }
                                    break;
                                case 9:
                                    if (!validateACNumber(abn_acn)) {
                                        field_error += " - Your ACN number is invalid \n"
                                    }
                                    break;
                                default:
                                    field_error += " - Your ABN / ACN number is invalid \n"
                            }
                        } else {
                            field_error += " - Your ABN / ACN number should only contain digits \n"
                        }
                    } else {
                        field_error += " - Your ABN / ACN number is Mandatory \n"
                    }
                } else {
                    field_error += " - Your ABN / ACN number is Mandatory \n"
                }
                */
                break;
        }
    }


    return new qfErrorObj(fld_name, field_error, to_focus);

}

function checkDropdownDateField(fld_obj, form_element_id) {

    var fld_name = fld_obj.getAttribute('name');
    var field_error = '';

    var fld_label = document.getElementById(fld_name + '_label');
    if (fld_label) {
        if (fld_label.getAttribute('mandatory') == 1 && fld_obj.value == 'Never') {
            fld_name = fld_label.innerHTML;
            fld_name = fld_name.replace(/\<.*\>/g, ''); //remove HTML tags (usually mandatory constraint text)
            field_error = " - '" + fld_name + "' is a Mandatory field\n";
        }
    }

    return new qfErrorObj(fld_name, field_error, fld_obj);
}

    // :NOTE: bkuskopf - created function to loop through multiple values and replace a needle in a haystack
    function replaceStrings( str, from, to ) {
        var idx = str.indexOf( from );


        while ( idx > -1 ) {
            str = str.replace( from, to );
            idx = str.indexOf( from );
        }

        return str;
    }


function checkForm(form_obj) {

    //updateCheckboxes();
    oForm    = form_obj;
    to_focus = null;
    sf_checked_radios = { };
    sf_mandatory_checkbox_values = { };

    if(typeof(oForm) == 'undefined') {
        oForm = document.forms[0];
    }

    err = ["Please make sure you have completed the form correctly:\n\n"];

    //var sf_website_filter         = new RegExp('^[(http:\/\/|https:\/\/|ftp:\/\/)]+((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)([a-z0-9A-Z\.-\/])*$', 'i');
    var olen = err.length, check_result = false;

    var error_fields = { };

    for (i=0; i<oForm.length; i++){

        if (oForm[i].getAttribute('sf_title') == null) {
            if (oForm[i].getAttribute('sf_date') == 1) {
                check_result = checkDropdownDateField(oForm.elements[i], i);
            } else {
                continue;
            }
        } else {
            // Pass off to checkField
            check_result = checkField(oForm.elements[i], i);
        }

        if (check_result.err.length) {
            error_fields[check_result.field_name] = true;
            err[i] = check_result.err;
        }
        to_focus = check_result.to_focus;

    }

    // ====================== START CHECKBOX ERROR CHECKING ROUTINE ======================
    // find the group names of all mandatory checkbox fields that have been chosen
    var ok_checkboxes = [];
    for (var chkid in sf_mandatory_checkbox_values) {
        var chkname = oForm.elements[chkid].getAttribute('sf_title');
        if ((typeof sf_mandatory_checkbox_values[chkid] == 'function')
            || (typeof error_fields[chkid] != 'undefined')) {
            continue;
        }
        if (sf_mandatory_checkbox_values[chkid].length > 0) {
            ok_checkboxes.push(chkname);
        }
    }

    // now check every checkbox against the 'ok' ones
    var errored_checkboxes = [];
    for (var chkid in sf_mandatory_checkbox_values) {
        var chkname = oForm.elements[chkid].getAttribute('sf_title');
        skip_error = false;
        // if this checkbox group has already errored, skip the error message
        for (var i = 0; i < errored_checkboxes.length; ++i) {
            if (errored_checkboxes[i] == chkname) {
                skip_error = true;
            }
        }
        var this_check_ok = false;
        // if the checkbox group is ok, skip the error message
        for (var i = 0; i < ok_checkboxes.length; ++i) {
            if (ok_checkboxes[i] == chkname) {
                this_check_ok = true;
            }
        }
        if (!this_check_ok && !skip_error) {
            errored_checkboxes.push(chkname);
            err[chkid] = " - '" + chkname + "' is a Mandatory field\n";
        }
    }
    // ====================== END CHECKBOX ERROR CHECKING ROUTINE ======================

    if (typeof(submitFormCustom) != 'undefined') {
        custom_check_result = submitFormCustom();
        if (custom_check_result.err.length > 0) {
            err.push(custom_check_result.err);
        }
        to_focus = custom_check_result.to_focus;
    }

    //check file inputs, if the validation class has been loaded
    if (typeof File_Upload_Field != 'undefined') {
        var file_errors = File_Upload_Field.checkAll(form_obj, true, true);
        if (file_errors == false) {
            file_errors = '';       //just in case. will lengthen the error array and prevent the form from submitting
        }
        if (file_errors != true) {
            err.push(file_errors);
        }
    }

    if(err.length > olen) {

        if (hasSteps) {
            //Firstly hide all the step tables
            hideStepTables();

            // Focus to the First field with the Error
            var focus_tbl = getStepTable(to_focus);
                focus_tbl.style.display = '';
        }

        if (to_focus) {
            if (hasTabSteps) {
                focusTabField(to_focus.name, hasSteps, oForm);
            } else {
                focusField(to_focus.name, hasSteps, oForm);
            }
        }

        alert(err.join(''));
        return false;
    }

    if (typeof(oForm.submitted) != 'undefined') {
        // Disable the button only if its not a last step in wizard mode.
        if (wizard_last_step) {
            oForm.submitted.disabled = false;
        } else {
            oForm.submitted.disabled = true;
        }
    }

    if (wizard_last_step) {
        setCookie('wizard_step', wizard.next_step_id, null, '/');

    }

    if (typeof(quickform_asclient) != 'undefined') {
        var srch = asclient_current.searcharray();
        var input   = document.createElement('input');
        input.type  = 'hidden';
        input.name  = quickform_asclient;
        input.value = JSON.stringify(srch);
        oForm.appendChild(input);
    }

    if (typeof(quickform_simplebrowse) != 'undefined') {
        var rows = quickform_simplebrowse_object.getSelectedIds();
        var input   = document.createElement('input');
        input.type  = 'hidden';
        input.name  = quickform_simplebrowse;
        input.value = JSON.stringify(rows);
        oForm.appendChild(input);
    }

    return true;
}

function submitForm(form_obj) {
    if (checkForm(form_obj)) {

        form_obj.submit();
    }
}



function qfErrorObj(field_name, err, to_focus) {
    this.field_name = field_name;
    this.err        = err;
    this.to_focus   = to_focus;
}


function isVisible(element) {
    vis = true;

    if((typeof(element) == 'object') && (typeof(element.style) == 'object')) {

        if(element.style.display=='none') {
            vis = false;
        }

        if(element.type && element.type.toLowerCase() == 'hidden') {
            vis = false;
        } else if(element.type && element.type.toLowerCase() == 'radio') {
            vis = false;
        }

        if (vis) {
            el = element.parentElement;
            while(typeof(el) != 'undefined' && el != null){
                if((typeof(el) == 'object') && (typeof(el.style) == 'object')) {
                    if (el.style.display == 'none') {
                        vis = false;
                    }
                }
                el = el.parentElement;
            }
        }
    }

    return vis;
}

function updateTextCheckField(checkbox, textfield) {
    if(checkbox.checked) {
        textfield.value = "";
        textfield.focus();
    } else {
        textfield.value = "Unlimited";
        checkbox.focus();
    }
}

String.prototype.trim = function(c, n) {
    if (!c) {
        c = '\\s';
    }

    n = parseInt(n);

    var r1 = new RegExp('^' + c + '*');
    var r2 = new RegExp(c + '*$');

    var value = this.replace(r1, '').replace(r2, '');

    if ((n > 0) && (value.length > n)) {
        value = value.slice(0, n) + '...';
    }

    return value;
};

function isValidDate(date_string) {

    var pos1      = date_string.indexOf('/');
    var pos2      = date_string.indexOf('/', pos1 + 1);

    var str_day   = date_string.substring(0, pos1);
    var str_month = date_string.substring(pos1 + 1, pos2);
    var str_year  = date_string.substring(pos2 + 1);

    if (str_day.charAt(0) == "0" && str_day.length > 1) {
        str_day = str_day.substring(1);
    }
    if (str_month.charAt(0) == "0" && str_month.length > 1) {
        str_month = str_month.substring(1);
    }
    for (var i = 1; i <= 3; i++) {
        if (str_year.charAt(0) == "0" && str_year.length > 1) {
            str_year = str_year.substring(1);
        }
    }

    var day   = parseInt(str_day,   10);
    var month = parseInt(str_month, 10);
    var year  = parseInt(str_year,  10);

    // valid format is dd/mm/yyyy
    if (pos1 == -1 || pos2 == -1) {
        return -1;
    }

    if (date_string.indexOf('/', pos2 + 1) != -1 || isInteger(stripChars(date_string, '/')) == false) {
        return -1;
    }

    // Day validations
    if (str_day.length < 1 || day < 1 || day > 31) {
        return -2;
    } else {
        // check if its a leap year
        if (month == 2) {
            var number_of_days = (((year % 4) == 0 && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28) ;
            if (day > number_of_days) {
                return -3;
            }
        }
    }

    // Month validations
    if (str_month.length < 1 || month < 1 || month > 12) {
        return -4;
    }

    // Year validations
    if (str_year.length != 4 || year == 0) {
        return -5;
    } else {
        // Pretty old invoice we didnt exist !!
        if (str_year < 2000) {
            return -6;
        }
    }

    // compare whole date rather comparing each individual component
    var date_selected = new Date(str_year, str_month - 1, str_day);
    var current_day   = new Date();

    if (date_selected > current_day) {
        return -7;
    }

    return true;
}

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        //Check that current character is number.
        var c = s.charAt(i);
            if ((c < "0") || (c > "9")) {
            return false;
            }
    }
    // All characters are numbers.
    return true;
}

function stripChars(s, seperator){;
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (seperator.indexOf(c) == -1) {
            returnString += c;
        }
    }
    return returnString;
}

function isValidCurrency(amount) {
    if (amount.length > 0) {
        var valid_currency =  /^((\$\d+)|(\$\d+\.\d{2})|(\d+)|(\d+\.\d{2})|((\d{1,3}\,\d{3})(\.\d{2}))|((\$\d{1,3}\,\d{3})(\.\d{2}))|(\$\d{1,3}\,\d{3})|(\d{1,3}\,\d{3}))$/;
        if (valid_currency.test(amount)) {
            return true;
        }
    }
    return false;
}

// Supplier Name + Invoice Number + Invoice Date should be unique
// Create a large string and store that in an array
function validateInvoiceDetails() {
    var form_field_name_array = new Array('caf_supplier_name', 'caf_invoice_number', 'caf_invoice_date');
    var supplier_details      = new Array();
    var has_key               = false;

    for (var i = 1; i <= 3; i++) {
        var supplier_string = '';
        for (var j = 0; j < form_field_name_array.length; j++) {
            var field_string = form_field_name_array[j] + '_' + i;
            var field        = document.getElementById(field_string);
            if (typeof(field) && field) {
                if (field.value.length > 0) {
                    supplier_string += field.value.toLowerCase();
                }
            }
        }
        if (supplier_string.length > 0) {
            if (!in_array(supplier_string, supplier_details)) {
                supplier_details.push(supplier_string);
            } else {
                has_key = true;
            }
        }
    }
    return has_key;
}

function validateABNumber(abn_number) {
    var left_digit    = abn_number.charAt(0) - 1;
    var weight_factor = new Array(10,1,3,5,7,9,11,13,15,17,19);

    var sum           = 0;
    for (var i = 0; i < abn_number.length; i++) {
        if (i == 0) {
            sum += (left_digit * weight_factor[i]);
        } else {
            sum += (abn_number.charAt(i) * weight_factor[i]);
        }
    }

    var remainder = sum % 89;
    if (remainder == 0) {
        return true;
    }
    return false;
}

function validateACNumber(acn_number) {
    var weight_factor = new Array(8,7,6,5,4,3,2,1);

    var sum           = 0;
    for (var i = 0; i < acn_number.length - 1; i++) {
        sum += (acn_number.charAt(i) * weight_factor[i]);
    }

    var remainder = sum % 10;
    // complement the remainder to 10
    var check_digit = 10 - remainder;
    if (check_digit == 10) {
        check_digit = 0;
    }
    if (check_digit == acn_number.charAt(acn_number.length - 1)) {
        return true;
    }
    return false;
}



    // :NOTE: Entire content of quickform.js is now loaded, and won't be asked for again

}

