﻿
   
    function SetTotal(field) {
        var Loc;
        var Fed;
        var Pri;
        var Oth;
        var Tot;      
        
        if (field == "LandAcq") {
            Loc = document.getElementById('txtLandAcqLocalGov');
            Fed = document.getElementById('txtLandAcqFedGov');
            Pri = document.getElementById('txtLandAcqPrivate');
            Oth = document.getElementById('txtLandAcqOther');
            Tot = document.getElementById('lblLandAcqTotal');
        } 
        else if (field == "BuildingAcq") {
            Loc = document.getElementById('txtBuildingAcqLocalGov');
            Fed = document.getElementById('txtBuildingAcqFedGov');
            Pri = document.getElementById('txtBuildingAcqPrivate');
            Oth = document.getElementById('txtBuildingAcqOther');
            Tot = document.getElementById('lblBuildingAcqTotal');
        }
        else if (field == "BuildingConst") {
            Loc = document.getElementById('txtBuildingConstLocalGov');
            Fed = document.getElementById('txtBuildingConstFedGov');
            Pri = document.getElementById('txtBuildingConstPrivate');
            Oth = document.getElementById('txtBuildingConstOther');
            Tot = document.getElementById('lblBuildingConstTotal');
        }
        else if (field == "BuildingReno") {
            Loc = document.getElementById('txtBuildingRenoLocalGov');
            Fed = document.getElementById('txtBuildingRenoFedGov');
            Pri = document.getElementById('txtBuildingRenoPrivate');
            Oth = document.getElementById('txtBuildingRenoOther');
            Tot = document.getElementById('lblBuildingRenoTotal');
        }
        else if (field == "CapEquip") {
            Loc = document.getElementById('txtCapEquipLocalGov');
            Fed = document.getElementById('txtCapEquipFedGov');
            Pri = document.getElementById('txtCapEquipPrivate');
            Oth = document.getElementById('txtCapEquipOther');
            Tot = document.getElementById('lblCapEquipTotal');
        }
        else if (field == "InfraImprov") {
            Loc = document.getElementById('txtInfraImprovLocalGov');
            Fed = document.getElementById('txtInfraImprovFedGov');
            Pri = document.getElementById('txtInfraImprovPrivate');
            Oth = document.getElementById('txtInfraImprovOther');
            Tot = document.getElementById('lblInfraImprovTotal');
        }
        else if (field == "Fixed") {
            Loc = document.getElementById('txtPrivateFixedLocalGov');
            Fed = document.getElementById('txtPrivateFixedFedGov');
            Pri = document.getElementById('txtPrivateFixedPrivate');
            Oth = document.getElementById('txtPrivateFixedOther');
            Tot = document.getElementById('lblPrivateFixedTotal');
        }
        else if (field == "Other") {
            Loc = document.getElementById('txtOtherLocalGov');
            Fed = document.getElementById('txtOtherFedGov');
            Pri = document.getElementById('txtOtherPrivate');
            Oth = document.getElementById('txtOtherOther');
            Tot = document.getElementById('lblOtherTotal');
        }
        else if (field == "Subtotal") {
            Loc = document.getElementById('txtSubtotalLocalGov');
            Fed = document.getElementById('txtSubtotalFedGov');
            Pri = document.getElementById('txtSubtotalPrivate');
            Oth = document.getElementById('txtSubtotalOther');
            Tot = document.getElementById('lblSubtotalTotal');
        }
        else if (field == "ProjCost") {
            Loc = document.getElementById('txtProjCostLocalGov');
            Fed = document.getElementById('txtProjCostFedGov');
            Pri = document.getElementById('txtProjCostPrivate');
            Oth = document.getElementById('txtProjCostOther');
            Tot = document.getElementById('lblProjCostTotal');
        }

        
        var i =
            parseFloat(replaceEmpty(Loc.value, "0").replace(/,/g, ''))
            + parseFloat(replaceEmpty(Fed.value, "0").replace(/,/g, ''))
            + parseFloat(replaceEmpty(Pri.value, "0").replace(/,/g, ''))
            + parseFloat(replaceEmpty(Oth.value, "0").replace(/,/g, ''));

        Tot.value = CommaFormatted(CurrencyFormatted(i));
    }
    

    function SetFacilityAddress() {
        if (document.getElementById("chkSameAddress").checked == true) {
            var FacCounty = document.getElementById('ddlFacilityCounty')
            var CoCounty = document.getElementById('ddlCompanyCounty')
            var FacState = document.getElementById('ddlFacilityState')
            var CoState = document.getElementById('ddlCompanyState')

            if (CoState.options[CoState.selectedIndex].value != 'LA') {
                alert("Company Address cannot be copied because it is outside of Louisiana.\n\nOnly Louisiana facilities are eligible.")
                document.getElementById("chkSameAddress").checked = false
            }
            else {            
                document.getElementById('txtFacilityAddress').value = document.getElementById('txtCompanyAddress').value;
                document.getElementById('txtFacilityCity').value = document.getElementById('txtCompanyCity').value;
                document.getElementById('txtFacilityZIP').value = document.getElementById('txtCompanyZIP').value;
                
                FacState.selectedIndex = CoState.selectedIndex;
                LoadCounties(FacState); //this has to be done after state is set, functino will also set Facility County
            }
        }   
    }   


    //-----------------------------------------------------------------------------
    // validate submitted form
    function ValidateSubmit() {
        // the error message - as array so that we may pass by value
        var msg = new Array();
        msg[0] = getBaseErrorMsg();
        // assume no errors until we find one
        var error = false;
                     
        // set control variables
        var CompanyName = document.getElementById('txtCompanyName');
        var CompanyAddress = document.getElementById('txtCompanyAddress');
        var CompanyCity = document.getElementById('txtCompanyCity');
        var CompanyState = document.getElementById('ddlCompanyState');
        var CompanyCounty = document.getElementById('ddlCompanyCounty');
        var CompanyZIP = document.getElementById('txtCompanyZIP');

        var POCName = document.getElementById('txtPOCName');
        var POCEmail = document.getElementById('txtPOCEmail');
        var POCPhone = document.getElementById('txtPOCPhone');

        var FacilityAddress = document.getElementById('txtFacilityAddress');
        var FacilityCity = document.getElementById('txtFacilityCity');
        var FacilityState = document.getElementById('ddlFacilityState');
        var FacilityCounty = document.getElementById('ddlFacilityCounty');
        var FacilityZIP = document.getElementById('txtFacilityZIP');

        var ParentCoZIP = document.getElementById('txtParentCoZIP');

        var CapitalInvestment = document.getElementById('txtCapitalInvestment');
        var TaxCreditRequest = document.getElementById('txtTaxCreditRequest');
        var CurrentJobs = document.getElementById('txtCurrentJobs');
        var NewJobs = document.getElementById('txtNewJobs');
        var RetainedJobs = document.getElementById('txtRetainedJobs');
        var ConstructBeginYear = document.getElementById('txtConstructBeginYear');
        var ConstructPeriod = document.getElementById('txtConstructPeriod');
        var ConstructEmp = document.getElementById('txtConstructEmp');
        var ConstructAnnualPayroll = document.getElementById('txtConstructAnnualPayroll');
        var ConstructMaterials = document.getElementById('txtConstructMaterials');
        var EquipmentCost = document.getElementById('txtEquipmentCost');
        var OperationBeginDate = document.getElementById('txtOperationBeginDate');
        var FirstYearTaxableCost = document.getElementById('txtFirstYearTaxableCost');
        var CurrentAnnualTaxableCost = document.getElementById('txtCurrentAnnualTaxableCost');
        //var ReservedYear = document.getElementById('txtReservedYear');
        

        // reset background colors
        CompanyName.style.backgroundColor = DEFAULT_COLOR;
        CompanyAddress.style.backgroundColor = DEFAULT_COLOR;
        CompanyCity.style.backgroundColor = DEFAULT_COLOR;
        CompanyState.style.backgroundColor = DEFAULT_COLOR;
        CompanyCounty.style.backgroundColor = DEFAULT_COLOR;
        CompanyZIP.style.backgroundColor = DEFAULT_COLOR;

        POCName.style.backgroundColor = DEFAULT_COLOR;
        POCEmail.style.backgroundColor = DEFAULT_COLOR;
        POCPhone.style.backgroundColor = DEFAULT_COLOR;

        FacilityAddress.style.backgroundColor = DEFAULT_COLOR;
        FacilityCity.style.backgroundColor = DEFAULT_COLOR;
        FacilityState.style.backgroundColor = DEFAULT_COLOR;
        FacilityCounty.style.backgroundColor = DEFAULT_COLOR;
        FacilityZIP.style.backgroundColor = DEFAULT_COLOR;

        ParentCoZIP.style.backgroundColor = DEFAULT_COLOR;

        CapitalInvestment.style.backgroundColor = DEFAULT_COLOR;
        TaxCreditRequest.style.backgroundColor = DEFAULT_COLOR;
        CurrentJobs.style.backgroundColor = DEFAULT_COLOR;
        NewJobs.style.backgroundColor = DEFAULT_COLOR;
        RetainedJobs.style.backgroundColor = DEFAULT_COLOR;
        ConstructBeginYear.style.backgroundColor = DEFAULT_COLOR;
        ConstructPeriod.style.backgroundColor = DEFAULT_COLOR;
        ConstructEmp.style.backgroundColor = DEFAULT_COLOR;
        ConstructAnnualPayroll.style.backgroundColor = DEFAULT_COLOR;
        ConstructMaterials.style.backgroundColor = DEFAULT_COLOR;
        EquipmentCost.style.backgroundColor = DEFAULT_COLOR;
        OperationBeginDate.style.backgroundColor = DEFAULT_COLOR;
        FirstYearTaxableCost.style.backgroundColor = DEFAULT_COLOR;
        CurrentAnnualTaxableCost.style.backgroundColor = DEFAULT_COLOR;
        //ReservedYear.style.backgroundColor = DEFAULT_COLOR;


		// has a CompanyName been provided?
        if (isEmpty(CompanyName.value)) {
            msg[0] = appendError(msg[0], " Company Name must be filled in");
            CompanyName.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a CompanyAddress been provided?
        if (isEmpty(CompanyAddress.value)) {
            msg[0] = appendError(msg[0], " Company Address must be filled in");
            CompanyAddress.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a CompanyCity been provided?
        if (isEmpty(CompanyCity.value)) {
            msg[0] = appendError(msg[0], " Company City must be filled in");
            CompanyCity.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a CompanyState been provided?
        if (isEmpty(CompanyState.value)) {
            msg[0] = appendError(msg[0], " Company State must be filled in");
            CompanyState.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a CompanyCounty been provided?
        if (isEmpty(CompanyCounty.value)) {
            msg[0] = appendError(msg[0], " Company Parish must be filled in");
            CompanyCounty.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a CompanyZIP been provided?
        if (isEmpty(CompanyZIP.value)) {
            msg[0] = appendError(msg[0], " Company ZIP must be filled in");
            CompanyZIP.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
      
        // has a POCName been provided?
        if (isEmpty(POCName.value)) {
            msg[0] = appendError(msg[0], " Contact Name must be filled in");
            POCName.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a POCEmail been provided?
        if (isEmpty(POCEmail.value)) {
            msg[0] = appendError(msg[0], " Contact Email must be filled in");
            POCEmail.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a POCPhone been provided?
        if (isEmpty(POCPhone.value)) {
            msg[0] = appendError(msg[0], " Contact Phone must be filled in");
            POCPhone.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
           
        // has a FacilityAddress been provided?
        if (isEmpty(FacilityAddress.value)) {
            msg[0] = appendError(msg[0], " Facility Address must be filled in");
            FacilityAddress.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
        
        // has a FacilityCity been provided?
        if (isEmpty(FacilityCity.value)) {
            msg[0] = appendError(msg[0], " Facility City must be filled in");
            FacilityCity.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a FacilityState been provided?
        if (isEmpty(FacilityState.value)) {
            msg[0] = appendError(msg[0], " Facility State must be filled in");
            FacilityState.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        // FacilityState must be LA
        if (FacilityState.options[FacilityState.selectedIndex].value != "LA") {
            msg[0] = appendError(msg[0], " Facility State must be LA in order to be eligible");
            FacilityState.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a FacilityCounty been provided?
        if (isEmpty(FacilityCounty.value)) {
            msg[0] = appendError(msg[0], " Facility Parish must be filled in");
            FacilityCounty.style.backgroundColor = WARNING_COLOR;
            error = true;
        }
   
        // has a FacilityZIP been provided?
        if (isEmpty(FacilityZIP.value)) {
            msg[0] = appendError(msg[0], " Facility ZIP must be filled in");
            FacilityZIP.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //capital invetment is a money
        if (isDecimal(CapitalInvestment.value)==false) {
            msg[0] = appendError(msg[0], " Capital Investment must be money");
            CapitalInvestment.style.backgroundColor = WARNING_COLOR;
            error=true;
        }

        //tax credit request is a decimal
        if (isDecimal(TaxCreditRequest.value) == false) {
            msg[0] = appendError(msg[0], " Tax Credit Record must be money");
            TaxCreditRequest.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //current jobs is an integer
        if (isNumeric(CurrentJobs.value) == false) {
            msg[0] = appendError(msg[0], " Number of current jobs must be an integer");
            CurrentJobs.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //new jobs is an integer
        if (isNumeric(NewJobs.value) == false) {
            msg[0] = appendError(msg[0], " Number of new jobs must be an integer");
            NewJobs.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //retained jobs is an integer
        if (isNumeric(RetainedJobs.value) == false) {
            msg[0] = appendError(msg[0], " Number of retained jobs after modernization must be an integer");
            RetainedJobs.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //construct begin year is an integer
        if (isNumeric(ConstructBeginYear.value) == false) {
            msg[0] = appendError(msg[0], " Year construction begins after modernization must be an integer");
            ConstructBeginYear.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //construct annual payroll is a money
        if (isDecimal(ConstructAnnualPayroll.value) == false) {
            msg[0] = appendError(msg[0], " Total annual construction payroll must be money");
            ConstructAnnualPayroll.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //construct materials is a money
        if (isDecimal(ConstructMaterials.value) == false) {
            msg[0] = appendError(msg[0], " Construction material must be money");
            ConstructMaterials.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //equipment cost is a money
        if (isDecimal(EquipmentCost.value) == false) {
            msg[0] = appendError(msg[0], " Equipment purchases must be money");
            EquipmentCost.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //operation begin date is a date
        if (isValidDate(OperationBeginDate.value) == false) {
            msg[0] = appendError(msg[0], " Date Operatoin Begins must be a date");
            OperationBeginDate.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //first year taxable cost is money
        if (isDecimal(FirstYearTaxableCost.value) == false) {
            msg[0] = appendError(msg[0], " Value of first year Louisiana taxable operational purchases must be money");
            FirstYearTaxableCost.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //current annual taxable cost is money
        if (isDecimal(CurrentAnnualTaxableCost.value) == false) {
            msg[0] = appendError(msg[0], " Value of current annual taxable operational purchases must be money");
            CurrentAnnualTaxableCost.style.backgroundColor = WARNING_COLOR;
            error = true;
        }

        //reserved year is an integer
        //        if (isNumeric(ReservedYear.value) == false) {
        //            msg[0] = appendError(msg[0], " Reserved Year must be an integer");
        //            ReservedYear.style.backgroundColor = WARNING_COLOR;
        //            error = true;
        //        }
        
        // how'd we do? 
        if (error == true) {
            // not well
            alert(msg);
            return false;
        }
        // looks good
        return true;
    }


    //determines the dropdown for AJAX population
    function setTargetDropdown(source) {
        var ddl = document.getElementById("hdnDDL");
        
        if (source.id == "ddlCompanyState") {
            ddl.value = "CompanyCounty"
            return document.getElementById("ddlCompanyCounty");
        }

        else if (source.id == "ddlFacilityState") {
            ddl.value = "FacilityCounty"
            return document.getElementById("ddlFacilityCounty");
        }

        else if (source.id == "ddlParentCoState") {
            ddl.value = "ParentCoCounty"
            return document.getElementById("ddlParentCoCounty");
        }
    }

    //called by the onChange javascript event on the dropdown
    function LoadCounties(ddlState) {
        var ddlCounty = setTargetDropdown(ddlState);
        
        var State = ddlState.options[ddlState.selectedIndex].value;
        if (State.length > 0) {
            //DropDownLink is defined by Ajax.Net because that's the name of the type we registered
            PreCert.FillCountyDropdown(State, LoadCounties_CallBack);
        }
        else {
            //clear the county dropdown
            ddlCounty = setTargetDropdown(ddlState)
            ddlCounty.options.length = 0;
        }
    }

    //callback we told Ajax.Net to pass the response tos
    function LoadCounties_CallBack(response) {
        //if the server side code threw an exception
        if (response.error != null) {
            alert(response.error); //we should probably do better than this
            return;
        }

        var ddl = document.getElementById("hdnDDL").value
        if (ddl == "CompanyCounty") {
            var ddlCounty = document.getElementById("ddlCompanyCounty");
        }
        else if (ddl == "FacilityCounty") {
            var ddlCounty = document.getElementById("ddlFacilityCounty");
        }
        else if (ddl == "ParentCoCounty") {
            var ddlCounty = document.getElementById("ddlParentCoCounty");
        }
        
        //if we couldn't find the county drop down
        if (ddlCounty == null) {
            return; //couldn't find the county dropdown...
        }

        var counties = response.value;
        //if the response wasn't what we expected
        if (counties == null || typeof (counties) != "object") {
            return;
        }

        ddlCounty.options.length = 0; //reset the county dropdown

        //note that this is JavaScript casing and the L in length is lowercase for arrays
        for (var i = 0; i < counties.Rows.length; ++i) {
            ddlCounty.options[ddlCounty.options.length] = new Option(counties.Rows[i].CountyName, counties.Rows[i].CountyName);
        }

        
        var FacState = document.getElementById('ddlFacilityState');
        var CoState = document.getElementById('ddlCompanyState');
        if (document.getElementById("chkSameAddress").checked == true && ddl == "FacilityCounty" && FacState.selectedIndex == CoState.selectedIndex)  {
            var FacCounty = document.getElementById('ddlFacilityCounty');
            var CoCounty = document.getElementById('ddlCompanyCounty');
            FacCounty.selectedIndex = CoCounty.selectedIndex;
        }
    }



    function Toggle() {
        var chk = document.getElementById("ckOutsideUSA");

        if (chk.checked == true) {
            document.getElementById("ddlParentCoState").className = "hidden"
            document.getElementById("txtParentCoNonUSAState").className = ""
            document.getElementById("ddlParentCountry").className = ""
            document.getElementById("ddlParentCountry").className = ""
            document.getElementById("ddlParentCoCounty").className = "hidden"
            document.getElementById("lblParentCoCounty").innerHTML = "Parent company Country:"
        }
        else {
            document.getElementById("ddlParentCoState").className = ""
            document.getElementById("txtParentCoNonUSAState").className = "hidden"
            document.getElementById("ddlParentCountry").className = "hidden"
            document.getElementById("ddlParentCountry").className = "hidden"
            document.getElementById("ddlParentCoCounty").className = ""
            document.getElementById("lblParentCoCounty").innerHTML = "Parent company parish:"
        }
    }
