// Constants used by this file
var MAX_FLASH_VERSION = 6

// Create a global browser capabilities.
var browser = new BrowserCapabilities();
// array for storing references to onload functions
var onLoadFunctionsAcsys = [];


/********************************************************************
* The input argument must be a string type.
*/
function AddWindowOnload(f) {
    if (typeof (f) != "string") return;

    if (browser.IsIEmac && browser.IsIE4) {
        // IE 4.5 blows out on testing window.onload, I'm not sure I care
        window.onload = WindowOnload;
    }
    else if (window.onload && (window.onload != AcsysWindowLoad)) {
        onLoadFunctionsAcsys[onLoadFunctionsAcsys.length] = window.onload.toString();
        window.onload = AcsysWindowLoad;
    }
    else {
        window.onload = AcsysWindowLoad;
    }
    onLoadFunctionsAcsys[onLoadFunctionsAcsys.length] = f;
}

function AcsysWindowLoad() {
    for (var i = 0, len = onLoadFunctionsAcsys.length;
		i < len;
		eval(onLoadFunctionsAcsys[i++])
	);
}

/********************************************************************
* This class encapsulates the version of the current browser and also
* provides information javascript and flash capabilities.
*/
function BrowserCapabilities() {
    this.IsMac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
    this.IsWindows = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;
    this.IsNN4 = (document.layers) ? true : false;
    this.IsIE = (document.all) ? true : false;
    this.IsIE4 = this.IsIE && !document.getElementById ? true : false;
    this.IsDOM = document.getElementById ? true : false;
    this.IsIEmac = ((this.IsIE) && (this.IsMac)) ? true : false;
    this.JavaScript = new JavaScriptSupport(this);
    //this.Flash = new FlashSupport(this);
}

function JavaScriptSupport(browserCapabilities) {
    this.SniffVersions = _JavaScriptSupport_SniffVersion;
    this.SniffVersions();
    this.Browser = browserCapabilities;
    this.Version1_0 = window["JavaScript1_0"];
    this.Version1_1 = window["JavaScript1_1"];
    this.Version1_2 = window["JavaScript1_2"];
    this.Version1_3 = window["JavaScript1_3"];
    this.Version1_4 = window["JavaScript1_4"];
}

function _JavaScriptSupport_SniffVersion() {
    window["JavaScript1_0"] = false;
    window["JavaScript1_1"] = false;
    window["JavaScript1_2"] = false;
    window["JavaScript1_3"] = false;
    window["JavaScript1_4"] = false;

    document.write('<SCR' + 'IPT LANGUAGE="JavaScript"\>\n');
    document.write('JavaScript1_0 = true;\n');
    document.write('</SCR' + 'IPT\>\n');

    document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.1"\>\n');
    document.write('JavaScript1_1 = true;\n');
    document.write('</SCR' + 'IPT\>\n');

    document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.2"\>\n');
    document.write('JavaScript1_2 = true;\n');
    document.write('</SCR' + 'IPT\>\n');

    document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.3"\>\n');
    document.write('JavaScript1_3 = true;\n');
    document.write('</SCR' + 'IPT\>\n');

    document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.4"\>\n');
    document.write('JavaScript1_4 = true;\n');
    document.write('</SCR' + 'IPT\>\n');
}


function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");

        if (pair[0] == variable) {
            return pair[1];
        }
        else {

        }

    }
    return "";
}

function findControl(controlid) {

    for (y = 0; y < document.forms.length; y++) {
        f = document.forms[y];


        for (r = 0; r < f.length; r++) {

            if (f.elements[r].id.indexOf(controlid) > -1) {
                return f.elements[r].id;
            }

        }
    }


}

function findAControl(tag, controlid) {

    var f = document.getElementsByTagName(tag);
    for (y = 0; y < f.length; y++) {
        if (f[y].id.indexOf(controlid) > -1) {
            return f[y].id;
        }
    }


}

function isValidEmail(email, required) {
    if (required == undefined) {   // if not specified, assume it's required
        required = true;
    }
    if (email == null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length == 0) {
        if (required) {
            return false;
        }
        return true;
    }
    if (!allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >= 0) { // two periods in a row is not valid
        return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
        return false;
    }
    return true;
}

function allValidChars(email) {
    var parsed = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i = 0; i < email.length; i++) {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1)
            continue;
        parsed = false;
        break;
    }
    return parsed;
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

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 stripCharsInBag(s, bag) {
    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++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone) {
    s = stripCharsInBag(strPhone, validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


