    // thank you Simon Willison -- http://simon.incutio.com/archive/2004/05/26/addLoadEvent
    if (typeof(addLoadEvent) != 'function') {
        function addLoadEvent(func) {
            var oldonload = window.onload;

            if (typeof window.onload != 'function') {
                window.onload = func;
            } else {
                window.onload = function() {
                    if (oldonload) {
                        oldonload();
                    }

                    func();
                }
            }
        }
    }


    function checkTrialUsernames(obj) {
        form = obj.form;
        if (typeof form.username_confirm == 'undefined') {
            return true;
        }
        if (form.username.value != form.username_confirm.value) {
            alert('Sorry.  Your Usernames do not match');
            form.username.select();
            return false;
        } else {
            return true;
        }
    }

    function checkTrialEmails(obj) {
        form = obj.form;
        if (typeof form.email_confirm == 'undefined') {
            return true;
        }
        if (form.email.value != form.email_confirm.value) {
           alert('Sorry.  Your Email addresses do not match');
            form.email.select();
            return false;
        } else {
            return true;
        }
    }

    function checkDetails(e) {
      	if(!e.srcElement) {
    		obj = e.currentTarget;
    	} else {
    		obj = e.srcElement;
    	}

        if(checkTrialEmails(obj) && checkTrialUsernames(obj)) {
            return true;
        } else {
            return false;
        }
    }



    if (!_e_r) {
        var _e_r = new RegExp("^(?:[\\#\\$\\&'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\}\\|\\~a-zA-Z0-9-]+(?:\\.[\\#\\$\\&'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\}\\|\\~a-zA-Z0-9-]+)*@[a-zA-Z0-9-]{1,63}(?:\\.[a-zA-Z0-9-]{1,63})*\\.[a-zA-Z][a-zA-Z0-9-]{1,62})?$");
    }

    if (!validateEmail) {
        var validateEmail = function(addr) {
            if(_e_r.test(addr) && (addr == '' || addr.indexOf('@') < 65)) {
                return true;
            } else {
                return false;
            }
        }
    }

//
// The following are for auto-suggesting usernames based on company name,
//   and checking username availability via XHR

var d = document, sug = '', sug2 = '', unc_timer = false, unc_check = '', unc_checked = '';
var cno = false, emo = false, emoc = false, uno = false, unoc = false, unwo = false, unw_short = false, unw_default = false;
var last_check_success = true;

function suggestUserName(evt, obj) {
    if (uno && unoc && String.prototype.split && Array.prototype.join && Array.prototype.unshift) {
        obj = obj || (typeof(evt.target) != 'undefined' ? evt.target : d.createElement('input'));
        var raw_names = trimString(obj.value.replace(/[^a-zA-Z0-9\']+/g, ' ').replace(/[\']/g, '').toLowerCase()).split(' ');
        var name_presplit = raw_names.join('').substring(0, split_len);
        var unsug_list = [], min_len = 3, split_len = 12, single_word_len = parseInt(split_len * 4 / 3, 10);
        var skip_idx = 0;
        var skip_words = ',a,an,and,at,in,is,it,ltd,of,on,pty,the,';
        var abbr_words = {
            'australia': 'aus',
            'australian': 'aus',
            'corporation': 'corp',
            'limited': 'ltd',
            'promotions': 'promo',
            'promotional': 'promo',
            'department': 'dept',
            'technology': 'tech',
            'communications': 'comms',
            'interactive': 'int',
            'one': '1',
            'two': '2',
            'three': '3',
            'university': 'uni',
            'queensland': 'qld',
            'international': 'intl'
        };

        // Process company name as per DEV-277

        // drop "skip words"
        if (raw_names.length > 0) {
            do {
                if (skip_words.indexOf(',' + raw_names[skip_idx] + ',') > -1) {
                    raw_names.splice(skip_idx, 1);
                } else {
                    skip_idx++;
                }
            } while (skip_idx < raw_names.length);
        }

        var raw_names_len = raw_names.length;

        if (raw_names_len > 0) {
            // abbreviate "abbr words"
            for (skip_idx = 0; skip_idx < raw_names_len; skip_idx++) {
                if (typeof(abbr_words[raw_names[skip_idx]]) != 'undefined') {
                    raw_names[skip_idx] = abbr_words[raw_names[skip_idx]];
                }
            }

            // determine all, first and second words
            var name0 = raw_names.join('').substring(0, split_len + 1);
            var name1 = raw_names[0].substring(0, single_word_len + 1);
            var name2 = (raw_names_len > 1 ? raw_names[1].substring(0, single_word_len + 1) : '');

            // start processing
            if (name0.length >= min_len) {
                // if entire name is long, check first 2 words individually
                if (name0.length > split_len) {
                    if (name1.length > split_len && name2.length > min_len - 1) {
                        // long 1st word and 2nd word present, try 1st letter of 1st word + first part of 2nd
                        unsug_list.push(name1.substring(0, 1) + name2.substring(0, split_len - 1));
                    } else if (name2.length >= split_len) {
                        // long 2nd word, try 1st letter of 1st word + first part of 2nd
                        unsug_list.push(name1.substring(0, split_len - 1) + name2.substring(0, 1));
                    }
                }

                // try entire name, trimmed at split point
                if ((',' + unsug_list.join(',') + ',').indexOf(',' + name0.substring(0, split_len) + ',') == -1) {
                    unsug_list.push(name0.substring(0, split_len));
                }

                // if < 2 suggestions but 2 words, try:
                if (unsug_list.length < 2 && name2 != '') {
                    var nametmp = name1.substring(0, parseInt(split_len / 2, 10));
                    nametmp += name2.substring(0, split_len - nametmp.length);

                    if (nametmp.length >= min_len && (',' + unsug_list.join(',') + ',').indexOf(',' + nametmp + ',') == -1) {
                        // even split of 1st and 2nd
                        unsug_list.push(nametmp);
                    } else if (name1.length >= min_len && (',' + unsug_list.join(',') + ',').indexOf(',' + name1.substring(0, split_len) + ',') == -1) {
                        // just the 1st - but use this as first suggestion
                        unsug_list.unshift(name1.substring(0, split_len));
                    }
                }
            }
        }

        // if still no suggestions, try entire name including "skip words", trimmed at split point
        if (unsug_list.length == 0 && name_presplit.length >= min_len) {
            unsug_list.push(name_presplit);
        }

        if (unsug_list.length > 0) {
            var tmp_sug = unsug_list[0];

            if (sug != tmp_sug) {
                var cur_un = uno.value;

//                if (cur_un == '' || cur_un == sug || cur_un == tmp_sug) {
                if (cur_un != tmp_sug) {
                    sug = tmp_sug;
                    sug2 = (unsug_list.length > 1 ? unsug_list[1] : '');

                    if (uno.value != sug) {
                        uno.value = unoc.value = sug;
                        checkNameEvent(false, uno);
                    }
                }
            }
        }
    }
}

function checkNameOnLoad() {
    if (trimString(uno.value) != '') {
        checkNameEvent(false, uno);
    }
}

function checkNameEvent(evt, obj) {
    obj = obj || evt.target;
    var unv = obj.value, minlen = 3;

    if (!username_regex_string.test(unv)) {
        if (unc_timer != false) {
            window.clearTimeout(unc_timer);
            unc_timer = false;
        }

        if (unv.length == 0) {
            unwo.style.color = 'gray';
            unwo.innerHTML = unw_default;
        } else {
            unwo.style.color = 'red';
            unwo.innerHTML = (unv.length < minlen ? unw_short : unw_default);
        }

        unc_check = '';
    } else if (unc_check != unv) {
        if (unc_timer != false) {
            window.clearTimeout(unc_timer);
            unc_timer = false;
        }

        unc_check = unv;
        unc_timer = window.setTimeout(usernameStartCheck, 400);
    }
}

function usernameStartCheck() {
    if (typeof xmlhttpCall != 'undefined') {
        unwo.style.color = 'gray';
        unwo.innerHTML = 'Checking for &quot;' + unc_check + '&quot&#8230;';
        unc_checked = unc_check;
        xmlhttpCall('POST', usernameCallback, 'usercheck.php?u=' + escape(unc_check));
    }
}

function usernameCallback(response) {
    var response_code = response.substr(0, 1), un_color = 'red', un_txt = unw_default;
    last_check_success = true;

    if (unc_checked != '') {
        switch (response_code) {
            case '0':
                un_color = 'red';
                un_txt = '&quot;' + unc_checked + '&quot; is already taken';
                last_check_success = false;

                if (sug2 != '' && uno.value != sug2) {
                    uno.value = unoc.value = sug2;
                    checkNameEvent(false, uno);
                }

                break;
            case '1':
                un_color = 'green';
                un_txt = '&quot;' + unc_checked + '&quot; is available!';
                break;
            default:
                un_color = '#543';
                un_txt = 'Could not reach the server';
        }
    }

    unwo.style.color = un_color;
    unwo.innerHTML = un_txt;
}

function submitFormCustom() {
    // check email address match, username availability, username match
    if (emo.value != emoc.value) {
        return { err: ' - Your email addresses do not match', to_focus: emo };
    }

    if (!last_check_success) {
        return { err: ' - The username you have chosen is already taken; please try another', to_focus: uno };
    }

    if (uno.value != unoc.value) {
        return { err: ' - Your usernames do not match', to_focus: uno };
    }

    return { err: '', to_focus: '' };
}

if (typeof(trimString) == 'undefined') {
    function trimString(str, c) {
        c = c || '\\s';
        var r1 = new RegExp('^' + c + '*');
        var r2 = new RegExp(c + '*$');
        return str.replace(r1, '').replace(r2, '');
    }
}

function trialAccountSignupInit() {
    if (d.getElementById && d.createElement) {
        cno = d.getElementById('signup_company_name');
        emo = d.getElementById('signup_email');
        emoc = d.getElementById('signup_email_confirm');
        uno = d.getElementById('signup_username');
        unoc = d.getElementById('signup_username_confirm');
        unwo = d.getElementById('signup_username_warning');
        unw_short = 'At least 3 alphanumeric characters';
        unw_default = '3-18 alphanumeric characters';

        // :NOTE: Using inline event handlers in HTML page, for IE's sake
//        cno.onkeyup  = suggestUserName;
//        cno.onchange = suggestUserName;
//        uno.onkeyup  = checkNameEvent;
//        uno.onchange = checkNameEvent;
        addLoadEvent(checkNameOnLoad);
    }
}

/**
 * openTermsAndConditions
 * @param object e (event)
 * @return void
 */
function openTermsAndConditions(e) {
    // IE compatibility
    e = e || event;
    if (!e.target && e.srcElement) {
        e.target = e.srcElement;
    }
    // Initialize vars
    var url         = e.target.href;
    var account_id  = false, key = false;
    var width       = 600,   height     = 450;
    var ah          = screen.availHeight - 30;
    var aw          = screen.availWidth - 10;
    var xc          = (aw - width) / 2;
    var yc          = (ah - height) / 2;
    var window_name = url.replace(/[^a-z]*/gi,'');
    // open popup window
    win = window.open(url, window_name, 'status=yes,location=no, resizable=yes, scrollbars=yes, width='+width+',height='+height+',left='+xc+',top='+yc);
    win.focus();
}
