function init() {
    oStringMask = new Mask("(###) ###-####");
    oStringMask.attach(dojo.byId("telephone"));

    dijit.byId("mitigationPrevention").attr("checked", false);
    dijit.byId("preparedness").attr("checked", false);
    dijit.byId("response").attr("checked", false);
    dijit.byId("recovery").attr("checked", false);
}

function ioIframeGetJson(x) {
    dojo.io.iframe.send({
        url: "/forms/bestPracticesApplication/src/upload_file",
        method: "POST",
        form: "frmIO",
        handleAs: "json",
        timeoutSeconds: 500000,
        preventCache: true,
        load: function(response, ioArgs) {
            //alert(response.textdata);
            //alert(response.status);
            if(response.status == '') {
                dojo.byId('formArea').style.display = "none";
                dojo.byId('confirmationArea').style.display = "block";
                dojo.byId('processingArea').style.display = "none";

                if (response.textdata == "<br><br>") {
                    dojo.byId("attachedDocumentsConfirmation").innerHTML = '<span style="font-weight:normal;">No Files Were Submitted.</span>';
                } else {
                    dojo.byId("attachedDocumentsConfirmation").innerHTML = '<span style="font-weight:normal;">'+response.textdata+'</span>';
                }

                showConfirmation();

            } else if (response.status == 'CAPTCHAException') {
                dojo.byId('formArea').style.display = "block";
                dojo.byId('confirmationArea').style.display = "none";
                dojo.byId('processingArea').style.display = "none";
                alert("WORD VERIFICATION ERROR: Please type the characters on the picture below.");
                Recaptcha.reload();
            } else if (response.status == 'EmailException') {
                dojo.byId('formArea').style.display = "block";
                dojo.byId('confirmationArea').style.display = "none";
                dojo.byId('processingArea').style.display = "none";
                alert("An error occurred while the system was working on your request. Please contact txssc@txstate.edu for more information.");
            } else {
                alert('Submition Error!');
            }
            return response;
        },
        error: function(response, ioArgs) {
			dojo.byId('formArea').style.display = "block";
			dojo.byId('confirmationArea').style.display = "none";
			dojo.byId('processingArea').style.display = "none";
            alert("An Error Has Occurred! Please try again later.");
            return response;
        }
    });
}

function resetForm () {
    dojo.byId("frmIO").reset();
    
    dijit.byId("mitigationPrevention").attr("checked", false);
    dijit.byId("preparedness").attr("checked", false);
    dijit.byId("response").attr("checked", false);
    dijit.byId("recovery").attr("checked", false);
    
    dojo.byId("needOnGoingTrainingYes").checked = false;
    dojo.byId("needOnGoingTrainingNo").checked = false;

    var rows = document.getElementById('fileUploadTable').getElementsByTagName('tr');
    for (var i = 1; i < (rows.length-1); i) {
        rows[i].parentNode.removeChild(rows[i]);
        fileAttachedCounter--;
    }
    if (fileAttachedCounter < 4) {
        dojo.byId('attachFileLink').style.display = "block";
    }
    
    verifyTitleBestPractice();
    verifyPurposeOfSpecificBestPractice();
    verifyBriefDescriptionBestPractice();

    dojo.byId("nameOfPhaseWarningArea").style.backgroundColor = "#FFFFFF";
    dojo.byId("nameOfPhaseWarningIconArea").innerHTML = '';

    dojo.byId("titleBestPracticeWarningArea").style.backgroundColor = "#FFFFFF";
    dojo.byId("titleBestPracticeWarningIconArea").innerHTML = '';

    dojo.byId("purposeBestPracticeWarningArea").style.backgroundColor = "#FFFFFF";
    dojo.byId("purposeBestPracticeWarningIconArea").innerHTML = '';

    dojo.byId("briefDescriptionBestPracticeWarningArea").style.backgroundColor = "#FFFFFF";
    dojo.byId("briefDescriptionBestPracticeWarningIconArea").innerHTML = '';

    dijit.byId("titleBestPractice").focus();
    dijit.byId("purposeBestPractice").focus();
    dijit.byId("briefDescriptionBestPractice").focus();

    //dijit.byId("firstName").focus();

    dijit.byId("usedTimeYears").attr("value", 0);
    dijit.byId("usedTimeMonths").attr("value", 0);
    dijit.byId("startupCost").attr("value", 0);
    dijit.byId("implementationTimeYears").attr("value", 0);
    dijit.byId("implementationTimeMonths").attr("value", 0);

}

function submitForm () {

    if (isFormValid()) {
        dojo.byId('formArea').style.display = "none";
        dojo.byId('confirmationArea').style.display = "none";
        dojo.byId('processingArea').style.display = "block";

        ioIframeGetJson(1);
        
    } else {
        scroll(0,0);
        dijit.byId("firstName").focus();
        showInvalidFormError();
    }

    return true;
}

function showForm () {
    dojo.byId('formArea').style.display = "block";
    dojo.byId('confirmationArea').style.display = "none";
    dojo.byId('processingArea').style.display = "none";

    resetForm();

    Recaptcha.reload();

    scroll(0 ,0);
}

function showConfirmation () {
    dojo.byId('formArea').style.display = "none";
    dojo.byId('confirmationArea').style.display = "block";
    dojo.byId('processingArea').style.display = "none";

    var tempStandardNumber = dijit.byId("standardNumber0").attr("displayedValue");
    if (tempStandardNumber == "") {
        tempStandardNumber = dijit.byId("standardNumber1").attr("displayedValue");
    } else {
        tempStandardNumber += " and " + dijit.byId("standardNumber1").attr("displayedValue");
    }

    var tempNamePhase = "";
    if (dijit.byId("mitigationPrevention").attr("value") != "" && dijit.byId("mitigationPrevention").attr("value") != false) {
        tempNamePhase += dijit.byId("mitigationPrevention").attr("value") + "<br>";
    }
    if (dijit.byId("response").attr("value") != "" && dijit.byId("response").attr("value") != false) {
        tempNamePhase += dijit.byId("response").attr("value") + "<br>";
    }
    if (dijit.byId("preparedness").attr("value") != "" && dijit.byId("preparedness").attr("value") != false) {
        tempNamePhase += dijit.byId("preparedness").attr("value") + "<br>";
    }
    if (dijit.byId("recovery").attr("value") != "" && dijit.byId("recovery").attr("value") != false) {
        tempNamePhase += dijit.byId("recovery").attr("value");
    }

    var tempUsedTime = dijit.byId("usedTimeYears").attr("displayedValue")+ " years and " + dijit.byId("usedTimeMonths").attr("displayedValue") + " months.";

    var tempImplementationTime = dijit.byId("implementationTimeYears").attr("displayedValue")+ " years and " + dijit.byId("implementationTimeMonths").attr("displayedValue") + " months.";

    var tempOngoingTraining = '';
    if (dojo.byId("needOnGoingTrainingYes").checked == true) {
        tempOngoingTraining = "YES";
    } else if (dojo.byId("needOnGoingTrainingNo").checked == true) {
        tempOngoingTraining = "NO";
    } else {
        tempOngoingTraining = "No Response";
    }

    dojo.byId("emailInfoConfirmation").innerHTML = '<span style="font-weight:bolder;">'+dijit.byId("email").attr("value")+'</span>';

    dojo.byId("applicantNameConfirmation").innerHTML = '<span style="font-weight:normal;">'+dijit.byId("firstName").attr("value")+' '+dijit.byId("middleInitial").attr("value")+' '+dijit.byId("lastName").attr("value")+'</span>';
    dojo.byId("telephoneConfirmation").innerHTML = '<span style="font-weight:normal;">'+dijit.byId("telephone").attr("value")+'</span>';
    dojo.byId("contactDistrictConfirmation").innerHTML = '<span style="font-weight:normal;">'+dijit.byId("contactDistrict").attr("value")+'</span>';
    dojo.byId("uilClassificationConfirmation").innerHTML = '<span style="font-weight:normal;">'+dijit.byId("uilClassification").attr("displayedValue")+'</span>';
    dojo.byId("emailConfirmation").innerHTML = '<span style="font-weight:normal;">'+dijit.byId("email").attr("value")+'</span>';
    dojo.byId("namePhaseConfirmation").innerHTML = '<span style="font-weight:normal;">'+tempNamePhase+'</span>';
    dojo.byId("standardNumberConfirmation").innerHTML = '<span style="font-weight:normal;">'+tempStandardNumber+'</span>';
    dojo.byId("titleBestPracticeConfirmation").innerHTML = ''+dijit.byId("titleBestPractice").attr("value")+'';
    dojo.byId("purposeSpecificBestPracticeConfirmation").innerHTML = ''+dijit.byId("purposeBestPractice").attr("value")+'';
    dojo.byId("usedTimeConfirmation").innerHTML = '<span style="font-weight:normal;">'+tempUsedTime+'</span>';
    dojo.byId("startupCostConfirmation").innerHTML = '<span style="font-weight:normal;">'+dijit.byId("startupCost").attr("value")+'</span>';
    dojo.byId("implementationTimeConfirmation").innerHTML = '<span style="font-weight:normal;">'+tempImplementationTime+'</span>';
    dojo.byId("ongoingTrainingConfirmation").innerHTML = '<span style="font-weight:normal;">'+tempOngoingTraining+'</span>';
    dojo.byId("briefDescriptionBestPracticeConfirmation").innerHTML = ''+dijit.byId("briefDescriptionBestPractice").attr("value")+'';

    scroll(0 ,0);
}

function isFormValid () {
    var isformValid = true;

    dijit.byId("firstName").focus();
    dijit.byId("middleInitial").focus();
    dijit.byId("lastName").focus();
    dijit.byId("telephone").focus();
    dijit.byId("contactDistrict").focus();
    dijit.byId("uilClassification").focus();
    dijit.byId("email").focus();
    dijit.byId("usedTimeYears").focus();
    dijit.byId("usedTimeMonths").focus();

    if (!dijit.byId("firstName").isValid()) {
        isformValid = false;
    }

    if (!dijit.byId("middleInitial").isValid()) {
        isformValid = false;
    }

    if (!dijit.byId("lastName").isValid()) {
        isformValid = false;
    }

    if (!dijit.byId("telephone").isValid()) {
        isformValid = false;
    }

    if (!dijit.byId("contactDistrict").isValid()) {
        isformValid = false;
    }

    if (!dijit.byId("uilClassification").isValid()) {
        isformValid = false;
    }

    if (!dijit.byId("email").isValid()) {
        isformValid = false;
    }

    if ((dijit.byId("mitigationPrevention").attr("value") == 0) &&
        (dijit.byId("preparedness").attr("value") == 0) &&
        (dijit.byId("response").attr("value") == 0) &&
        (dijit.byId("recovery").attr("value") == 0)) {
        dojo.byId("nameOfPhaseWarningArea").style.backgroundColor = "#F9F7BA";
        dojo.byId("nameOfPhaseWarningIconArea").innerHTML = '<img src="../js/dojo/dijit/themes/tundra/images/warning.png">';
        isformValid = false;
    }

    if (dojo.byId("titleBestPractice").value.length == 0) {
        dojo.byId("titleBestPracticeWarningArea").style.backgroundColor = "#F9F7BA";
        dojo.byId("titleBestPracticeWarningIconArea").innerHTML = '<img src="../js/dojo/dijit/themes/tundra/images/warning.png">';
        isformValid = false;
    }

    if (dojo.byId("purposeBestPractice").value.length == 0) {
        dojo.byId("purposeBestPracticeWarningArea").style.backgroundColor = "#F9F7BA";
        dojo.byId("purposeBestPracticeWarningIconArea").innerHTML = '<img src="../js/dojo/dijit/themes/tundra/images/warning.png">';
        isformValid = false;
    }

    if (dojo.byId("briefDescriptionBestPractice").value.length == 0) {
        dojo.byId("briefDescriptionBestPracticeWarningArea").style.backgroundColor = "#F9F7BA";
        dojo.byId("briefDescriptionBestPracticeWarningIconArea").innerHTML = '<img src="../js/dojo/dijit/themes/tundra/images/warning.png">';
        isformValid = false;
    }

    if (!dijit.byId("usedTimeYears").isValid()) {
        isformValid = false;
    }

    if (!dijit.byId("usedTimeMonths").isValid()) {
        isformValid = false;
    }

    if (dijit.byId("implementationTimeMonths").attr("displayedValue") == "") {
        dijit.byId("implementationTimeMonths").attr("value", 0);
    }

    return isformValid;
}

function showInvalidFormError () {
    alert("FORM VALIDATION FAILED.\nPlease check your form for invalid fields.");
}

function showPaperConfirmation () {
//openwindow('courseApplication/paperConfirmation.php'+getFormParametersString());
}

function openwindow(link) {
    window.open(link, "contractApplicationForm", "menubar=1,resizable=1,width=775,height=800,scrollbars=1");
}

var tempCounter = 1;
var fileAttachedCounter = 0;
function showAttachAnotherFile () {
    if (fileAttachedCounter < 4) {
        fileAttachedCounter++;

        var tbl = document.getElementById('fileUploadTable');
        var lastRow = tbl.rows.length - 1;
        var row = tbl.insertRow(lastRow);
        row.id = "attachAnotherFileInput"+tempCounter;

        var cellLeft = row.insertCell(0);
        cellLeft.id = "inputFileArea"+tempCounter;
        var tempFI = document.createElement('input');
        tempFI.type = "file";
        tempFI.name = "fFile"+tempCounter;
        tempFI.id = "fFile"+tempCounter;
        cellLeft.appendChild(tempFI);

        var cellMiddle = row.insertCell(1);
        cellMiddle.width = "5";

        var cellRight = row.insertCell(2);
        var tempA = document.createElement('a');
        tempA.href = "javascript:hideAttachAnotherFile('"+row.id+"')";
        var textNode = document.createTextNode("Remove");
        tempA.appendChild(textNode);
        cellRight.appendChild(tempA);

        tempCounter++;
    }
    if (fileAttachedCounter == 4) {
        dojo.byId('attachFileLink').style.display = "none";
    }
}

function hideAttachAnotherFile (rowId) {
    document.getElementById(rowId).parentNode.removeChild(document.getElementById(rowId));

    fileAttachedCounter--;

    if (fileAttachedCounter < 4) {
        dojo.byId('attachFileLink').style.display = "block";
    }
}

function verifyTitleBestPractice () {
    var charLeft = 150 - dojo.byId("titleBestPractice").value.length;
    if (charLeft < 0) {
        charLeft = 0;
    }
    dojo.byId("titleBestPracticeCharCounter").innerHTML = "(" + charLeft + " characters left)";
    if (dojo.byId("titleBestPractice").value.length > 150) {
        dojo.byId("titleBestPractice").value = dojo.byId("titleBestPractice").value.substring(0, dojo.byId("titleBestPractice").value.length - (dojo.byId("titleBestPractice").value.length - 150));
    }
}

function verifyPurposeOfSpecificBestPractice () {
    var charLeft = 300 - dojo.byId("purposeBestPractice").value.length;
    if (charLeft < 0) {
        charLeft = 0;
    }
    dojo.byId("purposeBestPracticeCharCounter").innerHTML = "(" + charLeft + " characters left)";
    if (dojo.byId("purposeBestPractice").value.length > 300) {
        dojo.byId("purposeBestPractice").value = dojo.byId("purposeBestPractice").value.substring(0, dojo.byId("purposeBestPractice").value.length - (dojo.byId("purposeBestPractice").value.length - 300));
    }
}

function verifyBriefDescriptionBestPractice () {
    var charLeft = 3000 - dojo.byId("briefDescriptionBestPractice").value.length;
    if (charLeft < 0) {
        charLeft = 0;
    }
    dojo.byId("briefDescriptionBestPracticeCharCounter").innerHTML = "(" + charLeft + " characters left)";
    if (dojo.byId("briefDescriptionBestPractice").value.length > 3000) {
        dojo.byId("briefDescriptionBestPractice").value = dojo.byId("briefDescriptionBestPractice").value.substring(0, dojo.byId("briefDescriptionBestPractice").value.length - (dojo.byId("briefDescriptionBestPractice").value.length - 3000));
    }
}

function manageUsedTimeYearsOnFocus () {
    if (dijit.byId("usedTimeYears").getValue() == "0") {
        dijit.byId("usedTimeYears").attr("value", "");
    }
}

function manageUsedTimeYearsOnBlur () {
    if (dijit.byId("usedTimeYears").getValue() == "") {
        dijit.byId("usedTimeYears").attr("value", "0");
    }
}

function manageImplementationTimeYearsOnFocus () {
    if (dijit.byId("implementationTimeYears").getValue() == "0") {
        dijit.byId("implementationTimeYears").attr("value", "");
    }
}

function manageImplementationTimeYearsOnBlur () {
    if (dijit.byId("implementationTimeYears").getValue() == "") {
        dijit.byId("implementationTimeYears").attr("value", "0");
    }
}

function manageStartupCostOnFocus () {
    if (dijit.byId("startupCost").getValue() == "0") {
        dijit.byId("startupCost").attr("value", "");
    }
}

function manageStartupCostOnBlur () {
    if (!dijit.byId("startupCost").isValid()) {
        dijit.byId("startupCost").attr("value", 0.00);
    }
}