var mControls = {};

var mData;
var mInitData;

var gInit = false;

function savePageState() {
    if (!gInit) {
        $("#_State").attr("value", JSON.stringify(mData));
    }
}

function initializeData() {
    var stateData = $("#_State").attr("value");
    if (stateData != "") {
        mData = eval('(' + stateData + ')');
        mInitData = eval('(' + stateData + ')');
    }
    else {
        mData = {
            AbutmentDesign: null,
            AbutmentType: null,
            Address1: null,
            Address2: null,
            Application: null,
            BridgeFinish: null,
            BridgeFinishColor: null,
            BridgeSpan: null,
            BridgeStyle: null,
            BridgeType: null,
            BridgeWidth: null,
            City: null,
            Company: null,
            Country: null,
            DeckType: null,
            DesignCode: null,
            Email: null,
            EndMarket: null,
            EndSubMarket: null,
            Fax: null,
            FirstName: null,
            Funding: null,
            LastName: null,
            MarketingSource: null,
            MarketingSourceOther: null,
            Phone: null,
            ProjectCity: null,
            ProjectCounty: null,
            ProjectName: null,
            ProjectState: null,
            RoleInProject: null,
            LifeSafetyRail: null,
            RailHeight: null,
            State: null,
            Title: null,
            UniformLiveLoad: null,
            VehicularLoad: null,
            Zip: null
        }
        mInitData = mData;
    }
}

function loadPageState() {
    mControls.FirstName.setValue(mInitData.FirstName);
    mControls.LastName.setValue(mInitData.LastName);
    mControls.Title.setValue(mInitData.Title);
    loadRoleInProject(mInitData.RoleInProject);
    mControls.Company.setValue(mInitData.Company);
    mControls.Address1.setValue(mInitData.Address1);
    mControls.Address2.setValue(mInitData.Address2);
    mControls.City.setValue(mInitData.City);
    loadState(mInitData.State);
    mControls.Zip.setValue(mInitData.Zip);
    mControls.Country.setValue(mInitData.Country);
    mControls.Email.setValue(mInitData.Email);
    mControls.Phone.setValue(mInitData.Phone);
    mControls.Fax.setValue(mInitData.Fax);
    loadMarketingSource(mInitData.MarketingSource);
    mControls.MarketingSourceOther.setValue(mInitData.MarketingSourceOther);
    mControls.ProjectName.setValue(mInitData.ProjectName);
    mControls.ProjectCity.setValue(mInitData.ProjectCity);
    mControls.ProjectCounty.setValue(mInitData.ProjectCounty);
    loadProjectState(mInitData.ProjectState);
    loadEndMarket(mInitData.EndMarket);
    loadEndSubMarket(mInitData.EndSubMarket);
    loadFunding(mInitData.Funding);
    loadBridgeType(mInitData.BridgeType);
    loadApplication(mInitData.Application);
    loadBridgeStyle(mInitData.BridgeStyle);
    loadBridgeWidth(mInitData.BridgeWidth);
    setupBridgeSpan(mInitData.BridgeSpan);    
    loadBridgeFinish(mInitData.BridgeFinish);
    loadBridgeFinishColor(mInitData.BridgeFinishColor);
    loadDeckType(mInitData.DeckType);
    loadAbutmentType(mInitData.AbutmentType);
    loadDesignCode(mInitData.DesignCode);
    loadUniformLiveLoad(mInitData.UniformLiveLoad);
    loadVehicularLoad(mInitData.VehicularLoad);
    loadAbutmentDesign(mInitData.AbutmentDesign);
    loadLifeSafetyRail(mInitData.LifeSafetyRail);
    loadRailHeight(mInitData.RailHeight);
}

function instantiateWrappers() {
    var c;

    c = new textBox1("FirstName", mData, "FirstName");
    mControls.FirstName = c;
    c.isValid = function(value, strict) {
        return validateRegEx(/^\S+/, value, strict);
    };
    c.errorTip = "Your first name must be entered.";

    c = new textBox1("LastName", mData, "LastName");
    mControls.LastName = c;
    c.isValid = function(value, strict) {
        return validateRegEx(/^\S+/, value, strict);
    };
    c.errorTip = "Your last name must be entered.";

    c = new textBox1("Title", mData, "Title");
    mControls.Title = c;

    c = new select1("RoleInProject", mData, "RoleInProject");
    mControls.RoleInProject = c;
    c.isValid = function(value, strict) {
        return validateSelect(value, strict);
    };
    c.errorTip = "You role in the project must be selected.";

	c = new textBox1("Company", mData, "Company");
	mControls.Company = c;

	c = new textBox1("Address1", mData, "Address1");
	mControls.Address1 = c;
	c.isValid = function(value, strict) {
	    return validateRegEx(/^\S+/, value, strict);
	};
	c.errorTip = "Your address must be entered.";

	c = new textBox1("Address2", mData, "Address2");
	mControls.Address2 = c;

	c = new textBox1("City", mData, "City");
	mControls.City = c;
	c.isValid = function(value, strict) {
	    return validateRegEx(/^\S+/, value, strict);
	};
	c.errorTip = "Your city must be entered.";

	c = new select1("State", mData, "State");
	mControls.State = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "Your state must be selected.";

	c = new textBox1("Zip", mData, "Zip");
	mControls.Zip = c;
	c.isValid = function(value, strict) {
	    return validateRegEx(/^\S+/, value, strict);
	};
	c.errorTip = "Your ZIP code must be entered.";

	c = new textBox1("Country", mData, "Country");
	mControls.Country = c;

	c = new textBox1("Email", mData, "Email");
	mControls.Email = c;
	c.isValid = function(value, strict) {
	return validateRegEx(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, value, strict);
	};
	c.errorTip = "Your e-mail address must be entered.";

	c = new textBox1("Phone", mData, "Phone");
	mControls.Phone = c;
	c.isValid = function(value, strict) {
	    return validateRegEx(/^\S+/, value, strict);
	};
	c.errorTip = "Your phone number must be entered.";

	c = new textBox1("Fax", mData, "Fax");
	mControls.Fax = c;

	c = new select1("MarketingSource", mData, "MarketingSource");
	mControls.MarketingSource = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "Marketing source must be selected.";

	c = new textArea1("MarketingSourceOther", mData, "MarketingSourceOther");
	mControls.MarketingSourceOther = c;

	c = new textBox1("ProjectName", mData, "ProjectName");
	mControls.ProjectName = c;
	c.isValid = function(value, strict) {
	    return validateRegEx(/^\S+/, value, strict);
	};
	c.errorTip = "The project name must be entered.";

    c = new textBox1("ProjectCity", mData, "ProjectCity");
	mControls.ProjectCity = c;
	c.isValid = function(value, strict) {
	    return validateRegEx(/^\S+/, value, strict);
	};
	c.errorTip = "The city where the project is located must be entered.";

	c = new textBox1("ProjectCounty", mData, "ProjectCounty");
	mControls.ProjectCounty = c;
	c.isValid = function(value, strict) {
	    return validateRegEx(/^\S+/, value, strict);
	};
	c.errorTip = "The county where the project is located must be entered.";

	c = new select1("ProjectState", mData, "ProjectState");
	mControls.ProjectState = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The state where the project is located must be selected.";

	c = new select1("EndMarket", mData, "EndMarket");
	mControls.EndMarket = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The project end market must be selected.";

	c = new select1("EndSubMarket", mData, "EndSubMarket");
	mControls.EndSubMarket = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The project end sub-market must be selected.";

	c = new select1("Funding", mData, "Funding");
	mControls.Funding = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The project funding must be selected.";

	c = new select1("BridgeType", mData, "BridgeType");
	mControls.BridgeType = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The bridge type must be selected.";

	c = new select1("Application", mData, "Application");
	mControls.Application = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The application must be selected.";

	c = new select1("BridgeStyle", mData, "BridgeStyle");
	mControls.BridgeStyle = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The bridge style must be selected.";

	c = new selectTextBox2("BridgeSpan", mData, "BridgeSpan");
	mControls.BridgeSpan = c;
	c.errorTip = "The bridge span must be within the range supported by DYOB.";

	c = new select2("BridgeWidth", mData, "BridgeWidth");
	mControls.BridgeWidth = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The bridge width must be selected.";

	c = new select1("BridgeFinish", mData, "BridgeFinish");
	mControls.BridgeFinish = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The bridge finish must be selected.";

	c = new select1("BridgeFinishColor", mData, "BridgeFinishColor");
	mControls.BridgeFinishColor = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The bridge finish color must be selected.";

	c = new select1("DeckType", mData, "DeckType");
	mControls.DeckType = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The deck type must be selected.";

	c = new select1("AbutmentType", mData, "AbutmentType");
	mControls.AbutmentType = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The abutment type must be selected.";

	c = new select1("DesignCode", mData, "DesignCode");
	mControls.DesignCode = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The design code must be selected.";

	c = new select1("UniformLiveLoad", mData, "UniformLiveLoad");
	mControls.UniformLiveLoad = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The uniform live load must be selected.";

	c = new select1("VehicularLoad", mData, "VehicularLoad");
	mControls.VehicularLoad = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The vehicular load must be selected.";

	c = new select1("AbutmentDesign", mData, "AbutmentDesign");
	mControls.AbutmentDesign = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The abutment design must be selected.";

	c = new select1("LifeSafetyRail", mData, "LifeSafetyRail");
	mControls.LifeSafetyRail = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The life safety rail must be selected.";

	c = new select1("RailHeight", mData, "RailHeight");
	mControls.RailHeight = c;
	c.isValid = function(value, strict) {
	    return validateSelect(value, strict);
	};
	c.errorTip = "The rail height must be selected.";
}

$(document).ready(function() {

    gInit = true;
    initializeData();
    createToolTip();
    instantiateWrappers();

    $("#AbutmentType").change(function() {
        if (!gInit) {
            loadAbutmentDesign();
        }
    });

    $("#Application").change(function() {
        if (!gInit) {
            loadBridgeStyle();
        }
    });

    $("#BridgeFinish").change(function() {
        if (!gInit) {
            loadBridgeFinishColor();
        }
    });

    $("#BridgeFinishColor").change(function() {
        var image = $("#BridgeFinishColorImage");
        image.attr("src", "images/Finish_" + mData.BridgeFinishColor + ".png");
    });

    $("#BridgeSpanSelect").change(function() {
        if (mControls.BridgeSpan.isSelectActive) {
            var control = $(this);
            if (!gInit) {
                loadAbutmentType();
            }
        }
    });

    $("#BridgeSpanText").change(function() {
        if (mControls.BridgeSpan.isTextActive) {
            var control = $(this);
            if (!gInit) {
                loadAbutmentType();
            }
        }
    });

    $("#BridgeStyle").change(function() {
        var image = $("#BridgeStyleImage");
        image.attr("src", "images/BridgeStyle_" + mData.BridgeStyle + ".png");
        var image = $("#BridgeStyleImageIso");
        image.attr("src", "images/BridgeStyle_" + mData.BridgeStyle + "_Iso.png");
        if (!gInit) {
            loadBridgeWidth();
            setupBridgeSpan();
            loadRailHeight();
            loadLifeSafetyRail();
        }
    });

    $("#BridgeType").change(function() {
        if (!gInit) {
            loadApplication();
            loadDeckType();
            loadBridgeStyle();
            loadAbutmentType();
            loadDesignCode();
            loadUniformLiveLoad();
            loadVehicularLoad();
        }
    });

    $("#BridgeWidth").change(function() {
        if (!gInit) {
            setupBridgeSpan(mData.BridgeSpan);
        }
    });

    $("#EndMarket").change(function() {
        if (!gInit) {
            loadEndSubMarket();
        }
    });

    $("#Funding").change(function() {
        if (!gInit) {
            loadDesignCode();
        }
    });

    $("#ProjectState").change(function() {
        if (!gInit) {
            loadDesignCode();
        }
    });

    $("form").submit(function() {
        var result = validateForm();
        if (!result) {
            alert("Please review your entries and correct those with an\nerror icon.\n\nHovering over an error icon displays error information.");
        }
        return result;
    });

    loadPageState();

    gInit = false;
});

function loadAbutmentDesign(value) {
    var options = new Array();

    if (mData.AbutmentType == "CIP") {
        options = getOptionsByCode("AbutmentDesign", ["CONTECH", "Others"]);
    }
    else if (mData.AbutmentType == "Other") {
        options = getOptionsByCode("AbutmentDesign", ["CONTECH", "Others"]);
    }
    else if (mData.AbutmentType == "Precast") {
        options = getOptionsByCode("AbutmentDesign", ["CONTECH"]);
    }
    populateSelect("#AbutmentDesign", options, value);
}

function loadAbutmentType(value) {
    var options = new Array();
    var bridgeType = mData.BridgeType;
    var lengthValue = mData.BridgeSpan;

    if ((!isNaN(lengthValue)) && (lengthValue > 0)) {
        if (bridgeType == "Pedestrian") {
            if (lengthValue <= 100) {
                options = getOptionsByCode("AbutmentType", ["CIP", "Precast", "Other"]);
            }
            else {
                options = getOptionsByCode("AbutmentType", ["CIP", "Other"]);
            }
        }
        else if (bridgeType == "Vehicular") {
            if (lengthValue <= 75) {
                options = getOptionsByCode("AbutmentType", ["CIP", "Precast", "Other"]);
            }
            else {
                options = getOptionsByCode("AbutmentType", ["CIP", "Other"]);
            }
        }
    }
    populateSelect("#AbutmentType", options, value);
}

function loadApplication(value) {
    var bridgeType = mData.BridgeType;
    if (hasValue(value) || (loadApplication.bridgeType != bridgeType)) {
        loadApplication.bridgeType = bridgeType;
        var options = getOptionsByParentCode("Application", "BridgeTypeCode", mData.BridgeType);
        var count = options.length;
        if ((loadApplication.count != count) || (count > 0)) {
            loadApplication.count = count;
            populateSelect("#Application", options, value);
        }
    }
}

function loadBridgeFinish(value) {
    var options = getOptions("BridgeFinish");
    populateSelect("#BridgeFinish", options, value);
}

function loadBridgeFinishColor(value) {
    var bridgeFinish = mData.BridgeFinish;
    if (hasValue(value) || (loadBridgeFinishColor.bridgeFinish != bridgeFinish)) {
        loadBridgeFinishColor.bridgeFinish = bridgeFinish;
        var options = getOptionsByParentCode("BridgeFinishColor", "BridgeFinishCode", mData.BridgeFinish);
        var count = options.length;
        if ((loadBridgeFinishColor.count != count) || (count > 0)) {
            loadBridgeFinishColor.count = count;
            populateSelect("#BridgeFinishColor", options, value);
        }
    }
}

function setupBridgeSpan(value) {
    var bridgeStyle = mData.BridgeStyle;

    // get detail
    var table = db["BridgeSpan"];
    var tableLen = table.length;
    var listItem = null;
    for (var i = 0; i < tableLen; i++) {
        var tableItem = table[i];
        if (tableItem["BridgeStyleCode"] == bridgeStyle) {
            listItem = tableItem;
            break;
        }
    }

    if ((listItem === null) || (hasValue(listItem.List))) {
        mControls.BridgeSpan.errorTip = "The bridge span must be selected.";
        mControls.BridgeSpan.isValid = function(value, strict) {
            if (!hasValue(strict)) {
                strict = false
            }
            return (hasSelection(value) ||
                (!strict && !hasSelection(value)));
        };

        var options;
        if (listItem === null) {
            options = new Array();
            mControls.BridgeSpan.hideInfoIcon();
            mControls.BridgeSpan.infoTip = "";
            $("#BridgeSpanComment_lbl").html("");
        }
        else {
            options = getCSVOptionsByParentCode("BridgeSpan", "BridgeStyleCode", bridgeStyle);
            mControls.BridgeSpan.showInfoIcon();
            mControls.BridgeSpan.infoTip = "The spans listed are our most popular spans.<br>Please contact your CONTECH representative for other spans.";
            $("#BridgeSpanComment_lbl").html("");
        }

        mControls.BridgeSpan.showSelect();
        populateSelect("#BridgeSpanSelect", options, value);
    }
    else {
        var max = listItem.Max;

        if (isNumeric(mData.BridgeWidth)) {
            var width = parseFloat(mData.BridgeWidth);
            max = Math.min(max, width * 20);
        }

        mControls.BridgeSpan.showInfoIcon();
        mControls.BridgeSpan.infoTip = "The span range is based on our most popular spans.<br>Please contact your CONTECH representative for other spans.";
        $("#BridgeSpanComment_lbl").html("Range is " + listItem.Min + " to " + max);
        mControls.BridgeSpan.errorTip = "The bridge span must be in the range from " + listItem.Min + " to " + max + "."
        mControls.BridgeSpan.isValid = function(value, strict) {

            if (!hasValue(strict)) {
                strict = false
            }
            if (isNumeric(value)) {
                var parsedValue = parseFloat(value);
                return ((parsedValue >= listItem.Min) && (parsedValue <= max));
            }
            else {
                return (!strict && !hasInput(value));
            }
        };

        mControls.BridgeSpan.showText();

        if (hasInput(value)) {
            setValue("#BridgeSpanText", value);
        }
        else {
            setValue("#BridgeSpanText", "");
        }
    }

}

function loadBridgeStyle(value) {
    var options = new Array();
    var application = mData.Application
    var bridgeType = mData.BridgeType
   
    if (bridgeType == "Pedestrian") {
        if (application != "SEL" && application != "NA") {
            if (application == "Pedestrian_BuildingSkywalk") {
                options = getOptionsByCode("BridgeStyle", ["Pedestrian_Gateway"]);
            }
            else {
                options = getOptionsByParentCode("BridgeStyle", "BridgeTypeCode", "Pedestrian");
            }
        }
    }
    else if (bridgeType == "Vehicular") {
        if (application != "SEL" && application != "NA") {
            options = getOptionsByParentCode("BridgeStyle", "BridgeTypeCode", "Vehicular");
        }
    }
    populateSelect("#BridgeStyle", options, value);
}

function loadBridgeWidth(value) {
    var bridgeStyle = mData.BridgeStyle;
    if (hasValue(value) || (loadBridgeWidth.bridgeStyle != bridgeStyle)) {
        loadBridgeWidth.bridgeStyle = bridgeStyle;
        var options = getCSVOptionsByParentCode("BridgeWidth", "BridgeStyleCode", mData.BridgeStyle);
        var count = options.length;
        if (count > 0) {
            mControls.BridgeWidth.showInfoIcon();
            mControls.BridgeWidth.infoTip = "The widths listed are our most popular widths.<br>Please contact your CONTECH representative for other widths.";
        }
        else {
            mControls.BridgeWidth.hideInfoIcon();
            mControls.BridgeWidth.infoTip = "";
        }
        if ((loadBridgeWidth.count != count) || (count > 0)) {
            loadBridgeWidth.count = count;
            populateSelect("#BridgeWidth", options, value);
        }
    }
}

function loadBridgeType(value) {
    var options = getOptions("Type");
    populateSelect("#BridgeType", options, value);
}

function loadDeckType(value) {
    var bridgeType = mData.BridgeType;
    if (hasValue(value) || (loadDeckType.bridgeType != bridgeType)) {
        loadDeckType.bridgeType = bridgeType;
        var options = getOptionsByParentCode("DeckType", "BridgeTypeCode", mData.BridgeType);
        var count = options.length;
        if ((loadDeckType.count != count) || (count > 0)) {
            loadDeckType.count = count;
            populateSelect("#DeckType", options, value);
        }
    }
}

function loadDesignCode(value) {
    var bridgeType = mData.BridgeType;
    var funding = mData.Funding;
    var projectState = mData.ProjectState;

    var codes = new Array();
    
    if ((bridgeType != "SEL") && (bridgeType != "NA") &&
    (funding != "SEL") && (funding != "NA") &&
    (projectState != "SEL") && (projectState != "NA")) {

        codes.push("AASHTO");

        if ((bridgeType == "Pedestrian") && (
        (funding == "County") || (funding == "City") ||
        (funding == "Private") || (funding == "Other"))) {
            codes.push("AISC");
        }

        if (projectState == "USA_CA") {
            codes.push("CALTRANS");
        }
    }

    var options = getOptionsByCode("DesignCode", codes);
    populateSelect("#DesignCode", options, value);
}

function loadEndMarket(value) {
    var options = getOptions("EndMarket");
    populateSelect("#EndMarket", options, value);
}

function loadEndSubMarket(value) {
    var endMarket = mData.EndMarket;
    if (hasValue(value) || (loadEndSubMarket.endMarket != endMarket)) {
        loadEndSubMarket.endMarket = endMarket;
        var options = getOptionsByParentCode("EndSubMarket", "EndMarketCode", mData.EndMarket);
        var count = options.length;
        if ((loadEndSubMarket.count != count) || (count > 0)) {
            loadEndSubMarket.count = count;
            populateSelect("#EndSubMarket", options, value);
        }
    }
}

function loadFunding(value) {
    var options = getOptions("Funding");
    populateSelect("#Funding", options, value);
}

function loadMarketingSource(value) {
    var options = getOptions("MarketingSource");
    populateSelect("#MarketingSource", options, value);
}

function loadProjectState(value) {
    var options = getOptions("State");
    populateSelect("#ProjectState", options, value);
}

function loadRoleInProject(value) {
    var options = getOptions("AccountType");
    
    populateSelect("#RoleInProject", options, value);
}

function loadLifeSafetyRail(value) {
    var bridgeStyle = mData.BridgeStyle;
    if (hasValue(value) || (loadLifeSafetyRail.bridgeStyle != bridgeStyle)) {
        loadLifeSafetyRail.bridgeStyle = bridgeStyle;
        var options = getOptionsByParentCode("LifeSafetyRail", "BridgeStyleCode", bridgeStyle);
        var count = options.length;
        if ((loadLifeSafetyRail.count != count) || (count > 0)) {
            loadLifeSafetyRail.count = count;
            populateSelect("#LifeSafetyRail", options, value);
        }
    }
}

function loadRailHeight(value) {
    var bridgeStyle = mData.BridgeStyle;
    if (hasValue(value) || (loadRailHeight.bridgeStyle != bridgeStyle)) {
        loadRailHeight.bridgeStyle = bridgeStyle;
        var options = getOptionsByParentCode("RailHeight", "BridgeStyleCode", bridgeStyle);
        var count = options.length;
        if ((loadRailHeight.count != count) || (count > 0)) {
            loadRailHeight.count = count;
            populateSelect("#RailHeight", options, value);
        }
    }
}

function loadState(value) {
    var options = getOptions("State");
    populateSelect("#State", options, value);
}

function loadUniformLiveLoad(value) {
    var bridgeType = mData.BridgeType;
    var options = new Array();
    if (bridgeType == "Pedestrian") {
        options = getOptions("UniformLiveLoad");
    }
    populateSelect("#UniformLiveLoad", options, value);
}

function loadVehicularLoad(value) {
    var bridgeType = mData.BridgeType;
    if (hasValue(value) || (loadVehicularLoad.bridgeType != bridgeType)) {
        loadVehicularLoad.bridgeType = bridgeType;
        var options = getOptionsByParentCode("VehicularLoad", "BridgeTypeCode", bridgeType);
        var count = options.length;
        if ((loadVehicularLoad.count != count) || (count > 0)) {
            loadVehicularLoad.count = count;
            populateSelect("#VehicularLoad", options, value);
        }
    }
}

// validation
function isNumeric(value) {
    return !(isNaN(value) || isNaN(parseFloat(value)))
}

function hasValue(value) {
    return ((typeof value != "undefined") && (value !== null));
}

function hasInput(value) {
    return (hasValue(value) && (value != "")); 
}

function hasSelection(value) {
    return (hasValue(value) && (value != "SEL") && (value != "NA") && (value != ""));
}

function setValue(selector, value) {
    var control = $(selector);
    if (hasValue(value)) {
        control.attr("value", value);
    }
    else {
        control.attr("value", "");
    }
    control.trigger("change");
}

function getOptions(tableName) {
    var table = db[tableName];
    var options = new Array();
    var tableLen = table.length;
    for (var i = 0; i < tableLen; i++) {
        var tableItem = table[i];
        options.push({ "Value": tableItem.Code, "Display": tableItem.Desc, "IsDefault": tableItem.IsDefault });
    }
    return options;
}

function getOptionsByCode(tableName, codes) {
    var table = db[tableName];
    var options = new Array();
    var tableLen = table.length;
    for (var i = 0; i < tableLen; i++) {
        var tableItem = table[i];
        var codesLen = codes.length;
        for (var j = 0; j < codesLen; j++) {
            if (tableItem.Code == codes[j]) {
                options.push({ "Value": tableItem.Code, "Display": tableItem.Desc, "IsDefault": tableItem.IsDefault });
                break;
            }
        }
    }
    return options;
}

function getOptionsByParentCode(tableName, parentCodeName, parentCode) {
    var table = db[tableName];
    var options = new Array();
    var tableLen = table.length;
    for (var i = 0; i < tableLen; i++) {
        var tableItem = table[i];
        if (tableItem[parentCodeName] == parentCode) {
            options.push({ "Value": tableItem.Code, "Display": tableItem.Desc, "IsDefault": tableItem.IsDefault });
        }
    }
    return options;
}

function getCSVOptionsByParentCode(tableName, parentCodeName, parentCode) {
    var table = db[tableName];
    var options = new Array();
    var tableLen = table.length;
    for (var i = 0; i < tableLen; i++) {
        var tableItem = table[i];
        if (tableItem[parentCodeName] == parentCode) {
            var list = tableItem.List.replace(/,\s*/, ",").split(",");
            var listLen = list.length;
            for (var j = 0; j < listLen; j++) {
                options.push({ "Value": list[j], "Display": list[j], "IsDefault": false });
            }
        }
    }
    return options;
}

function populateSelect(selector, options, newValue) {
    var hasDefault = false;
    var select = $(selector);
    var len = options.length;
    var i;    

    // check for default
    for (i = 0; i < len; i++) {
        if (options[i].IsDefault) {
            hasDefault = true;
        }
    }

    // get current value
    var value = select.attr("value");

    // override current value if newValue requested
    if ((typeof newValue != "undefined") && newValue !== null) {
        value = newValue
    }

    // change default as needed
    if ((value != "SEL") || (value != "NA")) {
        // check for current value in new list
        var hasValue = false;
        for (i = 0; i < len; i++) {
            if (options[i].Value == value) {
                hasValue = true;
                hasDefault = true;
                break;
            }
        }

        // set default to current value
        if (hasValue) {
            for (i = 0; i < len; i++) {
                if (options[i].Value == value) {
                    options[i].IsDefault = true;
                }
                else {
                    options[i].IsDefault = false;
                }
            }
        }
    }

    // clear options
    select.empty();

    // build options
    switch (len) {
        case 0:
            select.append($("<option></option>").val("NA").attr("selected", true).html(" -- N/A -- "));
            select.attr("disabled", true);
            break;
        case 1:
            select.append($("<option></option>").val(options[0].Value).attr("selected", true).html(options[0].Display));
            select.removeAttr("disabled");
            break;
        default:
            if (!hasDefault) {
                select.append($("<option></option>").val("SEL").attr("selected", true).html(" -- Select -- "));
            }

            for (i = 0; i < len; i++) {
                if (options[i].IsDefault) {
                    select.append($("<option></option>").val(options[i].Value).attr("selected", true).html(options[i].Display));
                }
                else {
                    select.append($("<option></option>").val(options[i].Value).html(options[i].Display));
                }
            }
            select.removeAttr("disabled");
    }
    select.trigger("change");
};

function updateSelect(control, value) {
    if (value != 'sel') {
        control.children("option[value='sel']").remove();
    }
}

function addToolTip(element, sourceObject, sourceField, title) {
    element.hoverIntent(
        function() {
            var control = $(this);
            var content = sourceObject[sourceField];

            if (hasValue(content) && content != "") {
                var container = $("#toolTipContainer");
                container.hide();
                var offset = control.offset();
                $("#toolTipTitle").html(title);
                $("#toolTipContent").html(content);
                container.css({ left: offset.left - container.width() + control.width() + 5, top: offset.top - container.height() - 6 }).fadeIn(200);
            }
        },
        function() {
            var control = $(this);
            var container = $("#toolTipContainer")
            container.fadeOut(200);
        }
    );
}

function createToolTip() {
    $("body").append("<div class=\"toolTipContainer\" id=\"toolTipContainer\"><div class=\"toolTipTitle\" id=\"toolTipTitle\"></div><div class=\"toolTipContent\" id=\"toolTipContent\"></div></div>");
    $("#toolTipContainer").bgiframe();
}

function customAlert(message) {
    $('#customAlert').modal({
        appendTo: '#Page',
        close: false,
        position: ["20%", ],
        overlayId: 'alertModalOverlay',
        containerId: 'alertModalContainer',
        onShow: function(dialog) {
            dialog.data.find('.message').append(message);
        }
    });
}

function validateRegEx(regEx, value, strict) {
    return (regEx.test(value) || (!strict && !hasInput(value)));
}

function validateSelect(value, strict) {
    return (hasValue(value) && (value != "SEL")) || (!strict);
}

function select1(id, data, field) {
    this.errorTip = "Error"
    this.infoTip = "Info"
    this.id = id;

    this.hideErrorIcon = function() {
        $("#" + this.id + "_eimg").hide();
    };

    this.hideInfoIcon = function() {
        $("#" + this.id + "_iimg").hide();
    };

    this.isValid = function(value, strict) {
        return true;
    };

    this.populateSelect = function populateSelect(options, newValue) {
        populateSelect("#" + this.id, options, newValue)
    };

    this.showErrorIcon = function() {
        $("#" + this.id + "_eimg").show();
    };

    this.showInfoIcon = function() {
        $("#" + this.id + "_iimg").show();
    };

    this.validate = function(strict) {
        if (this.isValid($("#" + this.id).attr("value"), strict)) {
            this.hideErrorIcon();
            return true;
        }
        else {
            this.showErrorIcon();
            return false;
        }
    };

    addToolTip($("#" + id + "_eimg"), this, "errorTip", "Error Information");
    addToolTip($("#" + id + "_iimg"), this, "infoTip", "Information");
    this.hideErrorIcon();
    this.hideInfoIcon();

    var base = $("#" + id);

    base.data("wrapper", this);

    base.change(function() {
        var control = $(this);
        var value = control.attr("value");
        data[field] = value;
        savePageState();
        updateSelect(control, value);
        control.data("wrapper").validate(false);
    });
}

function select2(id, data, field) {
    this.errorTip = "Error"
    this.infoTip = "Info"
    this.id = id;

    this.hideErrorIcon = function() {
        $("#" + this.id + "_eimg").hide();
    };

    this.hideInfoIcon = function() {
        $("#" + this.id + "_iimg").hide();
    };

    this.isValid = function(value, strict) {
        return true;
    };

    this.populateSelect = function populateSelect(options, newValue) {
        populateSelect("#" + this.id, options, newValue)
    };

    this.showErrorIcon = function() {
        $("#" + this.id + "_eimg").show();
    };

    this.showInfoIcon = function() {
        $("#" + this.id + "_iimg").show();
    };

    this.validate = function(strict) {
        if (this.isValid($("#" + this.id).attr("value"), strict)) {
            this.hideErrorIcon();
            return true;
        }
        else {
            this.showErrorIcon();
            return false;
        }
    };

    addToolTip($("#" + id + "_eimg"), this, "errorTip", "Error Information");
    addToolTip($("#" + id + "_iimg"), this, "infoTip", "Information");
    this.hideErrorIcon();
    this.hideInfoIcon();

    var base = $("#" + id);

    base.data("wrapper", this);

    base.change(function() {
        var control = $(this);
        var value = control.attr("value");
        data[field] = value;
        savePageState();
        updateSelect(control, value);
        control.data("wrapper").validate(false);
    });
}

function selectTextBox2(id, data, field) {
    this.errorTip = "Error"
    this.infoTip = "Info"
    this.id = id;

    this.hideErrorIcon = function() {
        $("#" + this.id + "_eimg").hide();
    };

    this.hideInfoIcon = function() {
        $("#" + this.id + "_iimg").hide();
    };

    this.isValid = function(value, strict) {
        return true;
    };

    this.populateSelect = function populateSelect(options, newValue) {
        populateSelect("#" + this.id + "Select", options, newValue)
    };

    this.setTextValue = function(value) {
        setValue("#" + this.id + "Text", value);
    }

    this.showErrorIcon = function() {
        $("#" + this.id + "_eimg").show();
    };

    this.showInfoIcon = function() {
        $("#" + this.id + "_iimg").show();
    };

    this.showSelect = function() {
        $("#" + this.id + "Select").show().removeAttr("disabled");
        $("#" + this.id + "Text").hide().attr("disabled", true);
        this.isSelectActive = true;
        this.isTextActive = false;
    };
    
    this.showText = function() {
        $("#" + this.id + "Text").show().removeAttr("disabled");
        $("#" + this.id + "Select").hide().attr("disabled", true);
        this.isSelectActive = false;
        this.isTextActive = true;
    };

    this.validate = function(strict) {
        if (this.isValid($("#" + this.id + (this.isTextActive ? "Text" : "Select")).attr("value"), strict)) {
            this.hideErrorIcon();
            return true;
        }
        else {
            this.showErrorIcon();
            return false;
        }
    };

    addToolTip($("#" + id + "_eimg"), this, "errorTip", "Error Information");
    addToolTip($("#" + id + "_iimg"), this, "infoTip", "Information");

    var baseSelect = $("#" + id + "Select");

    baseSelect.data("wrapper", this);

    baseSelect.change(function() {
        var control = $(this);
        var value = control.attr("value");
        data[field] = value;
        savePageState();
        updateSelect(control, value);
        control.data("wrapper").validate(false);
    });

    var baseText = $("#" + id + "Text");

    baseText.data("wrapper", this);

    baseText.change(function() {
        var control = $(this);
        data[field] = control.attr("value");
        savePageState();
        control.data("wrapper").validate(false);
    });

    this.hideErrorIcon();
    this.hideInfoIcon();
    this.showText();
}

function textBox1(id, data, field) {
    this.errorTip = "Error"
    this.infoTip = "Info"
    this.id = id;

    this.hideErrorIcon = function() {
        $("#" + this.id + "_eimg").hide();
    };

    this.hideInfoIcon = function() {
        $("#" + this.id + "_iimg").hide();
    };

    this.isValid = function(value, strict) {
        return true;
    };

    this.setValue = function(value) {
        setValue("#" + this.id, value);
    }

    this.showErrorIcon = function() {
        $("#" + this.id + "_eimg").show();
    };

    this.showInfoIcon = function() {
        $("#" + this.id + "_iimg").show();
    };

    this.validate = function(strict) {
        if (this.isValid($("#" + this.id).attr("value"), strict)) {
            this.hideErrorIcon();
            return true;
        }
        else {
            this.showErrorIcon();
            return false;
        }
    };

    addToolTip($("#" + id + "_eimg"), this, "errorTip", "Error Information");
    addToolTip($("#" + id + "_iimg"), this, "infoTip", "Information");
    this.hideErrorIcon();
    this.hideInfoIcon();

    var base = $("#" + id);

    base.data("wrapper", this);

    base.change(function() {
        var control = $(this);
        data[field] = control.attr("value");
        savePageState();
        control.data("wrapper").validate(false);
    });
}

function textArea1(id, data, field) {
    this.errorTip = "Error"
    this.infoTip = "Info"
    this.id = id;

    this.hideErrorIcon = function() {
        $("#" + this.id + "_eimg").hide();
    };

    this.hideInfoIcon = function() {
        $("#" + this.id + "_iimg").hide();
    };

    this.isValid = function(value, strict) {
        return true;
    };

    this.setValue = function(value) {
        setValue("#" + this.id, value);
    }

    this.showErrorIcon = function() {
        $("#" + this.id + "_eimg").show();
    };

    this.showInfoIcon = function() {
        $("#" + this.id + "_iimg").show();
    };

    this.validate = function(strict) {
        if (this.isValid($("#" + this.id).attr("value"), strict)) {
            this.hideErrorIcon();
            return true;
        }
        else {
            this.showErrorIcon();
            return false;
        }
    };

    addToolTip($("#" + id + "_eimg"), this, "errorTip", "Error Information");
    addToolTip($("#" + id + "_iimg"), this, "infoTip", "Information");
    this.hideErrorIcon();
    this.hideInfoIcon();

    var base = $("#" + id);

    base.data("wrapper", this);

    base.change(function() {
        var control = $(this);
        data[field] = control.attr("value");
        savePageState();
        control.data("wrapper").validate(false);
    });
}

function validateForm() {
    var retVal = true;
    for (var key in mControls) {
        if (!mControls[key].validate(true)) {
            retVal = false;
        }
    }
    return retVal;
}

