// -----------------------------------------------------------
// Application:    CGP ANSI Registration Forms
// File:           jslib_newsub.js
// Author:         ACS
// Date Written:   01/17/2007
// Description:    Functions for form processing.
//
// -----------------------------------------------------------
//
// -----------------------------------------------------------
// Allowed ANSI Transactions.
// -----------------------------------------------------------
//
// Which submission checkbox is used for the transaction?
// Example: "837I" uses chk_submission1[]
//          "270"  uses chk_submission2[]
// 1 = chk_submission1[] - Transactions supported by PC-ACE.
// 2 = chk_submission2[] - Transactions not supported by PC-ACE.
//
var arrSubmission = new Array(9);
arrSubmission["837I"] = "1";
arrSubmission["837P"] = "1";
arrSubmission["837D"] = "1";
arrSubmission["835"]  = "1";
arrSubmission["270"]  = "2";
arrSubmission["276"]  = "1";
arrSubmission["278"]  = "2";
arrSubmission["820"]  = "2";
arrSubmission["834"]  = "2";
//
// The number of times the chk_submission?[] checkbox has been "turned" on.
// If cnt > 0 then chk_submission?[] selection is required.
//
var arrSubmissionCnt = new Array(2);
arrSubmissionCnt["1"] = 0;                  // chk_submission1[]
arrSubmissionCnt["2"] = 0;                  // chk_submission2[]
//
// By Payer, the number of times the submission checkboxes
// have been turned on.
//
var arrPayerCnt = new Array(12);
arrPayerCnt["nmic1"]  = 0;       arrPayerCnt["nmic2"]  = 0;
arrPayerCnt["meda1"]  = 0;       arrPayerCnt["meda2"]  = 0;
arrPayerCnt["medb1"]  = 0;       arrPayerCnt["medb2"]  = 0;
arrPayerCnt["mcdia1"] = 0;       arrPayerCnt["mcdia2"] = 0;
arrPayerCnt["wsi1"]   = 0;       arrPayerCnt["wsi2"]   = 0;
arrPayerCnt["mcd1"]   = 0;       arrPayerCnt["mcd2"]   = 0;
// -----------------------------------------------------------
// NEW SUBMITTER FORM - Reset all of the boxes to initial load state.
// -----------------------------------------------------------
function resetNewSub(chkd) {
    var x, i;
	// De-select all of the payer boxes.
    var thisName = 'chk_payer' + '[]';
    var thisField = document.forms[0].elements[thisName];
   	for (i = 0; i < thisField.length; i++) {
        thisField[i].checked = false;        
        // De-select and disable the transaction boxes.
        var thisName2 = 'chk_' + thisField[i].value + '[]';
	    var thisField2 = document.forms[0].elements[thisName2];
   	    for (var x = 0; x < thisField2.length; x++) {
            thisField2[x].checked = false;
            thisField2[x].disabled = true;
        }
    }
    var trs = document.getElementsByTagName("tr");
	     for (var i=0; i<trs.length; i++) {
		if (trs[i].className.match(/afterFirst/)) {
			trs[i].className = (chkd)? trs[i].className.replace(/afterFirst/, "afterFirstShow") : trs[i].className.replace(/afterFirstShow/, "afterFirst");
		}
	}
    // De-select submission boxes.
    var thisName = 'chk_submission1' + '[]';
    var thisField = document.forms[0].elements[thisName];
    for (i = 0; i < thisField.length; i++) {
        thisField[i].checked = false;
        thisField[i].disabled = true;
    }
    var thisName = 'chk_submission2' + '[]';
    var thisField = document.forms[0].elements[thisName];
    for (i = 0; i < thisField.length; i++) {
        thisField[i].checked = false;
        thisField[i].disabled = true;
    }
    var trs = document.getElementsByTagName("tr");
	     for (var i=0; i<trs.length; i++) {
		if (trs[i].className.match(/afterFirst/)) {
			trs[i].className = (chkd)? trs[i].className.replace(/afterFirst/, "afterFirstShow") : trs[i].className.replace(/afterFirstShow/, "afterFirst");
		}
	}
    // De-select network access boxes.
    var thisName = 'chk_access' + '[]';
    var thisField = document.forms[0].elements[thisName];
    for (i = 0; i < thisField.length; i++) {
        thisField[i].checked = false;
    }
       var trs = document.getElementsByTagName("tr");
	     for (var i=0; i<trs.length; i++) {
		if (trs[i].className.match(/afterFirst/)) {
			trs[i].className = (chkd)? trs[i].className.replace(/afterFirst/, "afterFirstShow") : trs[i].className.replace(/afterFirstShow/, "afterFirst");
		}
	}
	var trs = document.getElementsByTagName("tr");
	     for (var i=0; i<trs.length; i++) {
		if (trs[i].className.match(/afterFirst/)) {
			trs[i].className = (chkd)? trs[i].className.replace(/afterFirst/, "afterFirstShow") : trs[i].className.replace(/afterFirstShow/, "afterFirst");
		}
	}
}
// -----------------------------------------------------------
// Validate New Submitter Form on submission.
// -----------------------------------------------------------
function validateNewSub(theForm) {
    var i, thisName, thisField, payer_name;
    var msg;
    var payer_selected = false;
    var mcd_selected = false;
    var triwestnd_selected = false; 
    var triwestwy_selected = false;

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Transaction must be selected if payer is selected.
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    thisName = 'chk_payer[]';
    thisField = theForm.elements[thisName];
   	for (i = 0; i < thisField.length; i++) {
        if (thisField[i].checked) {
            payer_selected = true;
            payer_name = thisField[i].value;
            if (payer_name == "mcd") { mcd_selected = true; }
            if (payer_name == "triwestnd") { triwestnd_selected = true; }
            if (payer_name == "triwestwy") { triwestwy_selected = true; }
            if (!chk4ANSI(theForm, payer_name)) {
                msg = "ANSI Transaction selection required.\n\n";
                msg = msg+ "Line of Business selected: '" + payer_name + "'\n";
                alert(msg);
                return (false);
            }
        }
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Payer is required.
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (!payer_selected) {
        msg = "Line(s) of Business is required.\n\n";
        msg = msg + "Please check the Line(s) of Business you want to partner with.\n";
        alert(msg);
        return (false);
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Medicaid ND requires PC-ACE software.
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (mcd_selected) {
        thisName = 'chk_submission1[]';
        thisField = theForm.elements[thisName];
        if (thisField[0].checked == false) {
            msg = "Noridian Administrative Services only accepts Medicaid\n";
            msg = msg + "claims submitted via PC-ACE Pro32.\n\n";
            msg = msg + "Please check the PC-ACE Pro32 software checkbox\n";
            msg = msg + "located under Method of Submission/Remittance section.";
            alert(msg);
            return (false);
        }
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Method of Submission/Remittance required.
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if (arrSubmissionCnt["1"] > 0) {
        thisName = 'chk_submission1[]';
        thisField = theForm.elements[thisName];
        if ((thisField[0].checked == false) && (thisField[1].checked == false)) {
            msg = "Method of Submission/Remittance is required.\n\n";
            msg = msg + "You have selected transactions supported by PC-ACE.\n";
            msg = msg + "Please check the PC-ACE Pro32 and/or the Vendor/Other checkbox.";
            alert(msg);
            return (false);
        }
    }
    if (arrSubmissionCnt["2"] > 0) {
        thisName = 'chk_submission2[]';
        thisField = theForm.elements[thisName];
        if ((thisField[0].checked == false) && (thisField[1].checked == false)) {
            msg = "Method of Submission/Remittance is required.\n\n";
            msg = msg + "You have selected transactions NOT supported by PC-ACE.\n";
            msg = msg + "Please check either the Vendor/Other or the Direct checkbox.";
            alert(msg);
            return (false);
        }
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Method of Electronic Access (connection) required.
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    thisName = 'chk_access[]';
    thisField = theForm.elements[thisName];
    if ((thisField[0].checked == false) && (thisField[1].checked == false)) {
            msg = "Method of Electronic Access (connection) is required.\n\n";
            msg = msg + "Please check either the Network Service Vendor or the Direct Dial checkbox.";
            alert(msg);
            return (false);
    }
    //
    // Everything is okay. Submit the form.
    return (true);
}
// -----------------------------------------------------------
// Have any transactions been selected for the payer?
// -----------------------------------------------------------
function chk4ANSI(myForm, myPayer) {
    trans_flag = false;
    x = 0;
    var thisName2 = 'chk_' + myPayer + '[]';
    var thisField2 = myForm.elements[thisName2];
    while ((!trans_flag) && (x < thisField2.length)) {
        if (thisField2[x].checked) {
            trans_flag = true;
        }
        x++;
    }
    return (trans_flag);
}
// -----------------------------------------------------------
// Enable/Disable ANSI Transaction options based on payer selection.
// -----------------------------------------------------------
function onoffTrans(myPayer) {

var tmpName, checkboxObj, payerName;
    // Get ansi checkbox for selected payer.
	tmpName = 'chk_' + myPayer.value + '[]';              // Ex. chk_bcbsnd[]
	checkboxObj = document.forms[0].elements[tmpName];
	if (myPayer.checked) {
        enableChk(checkboxObj);
	} else {
        disableChk(checkboxObj);
        for (var x in arrSubmissionCnt) {
            payerName = myPayer.value + x;
            arrSubmissionCnt[x] -= arrPayerCnt[payerName];
            arrPayerCnt[payerName] = 0;
            if (arrSubmissionCnt[x] == 0) {
                tmpName = 'chk_submission' + x + '[]';
                checkboxObj = document.forms[0].elements[tmpName];
                disableChk(checkboxObj);
            }
        }
	}
}
// -----------------------------------------------------------
// Check the submission checkboxes.
// Form Fields: chk_submission1[] and chk_submission2[]
// -----------------------------------------------------------
function onoffSubmission(myANSI) {
    var payerName, tmpName, checkboxObj, submissionName;

    // Example: myANSI.value = "873I"
    // arrSubmission["837I"] = "1"
    // tmpName = "chk_submission1[]
    //
    // NMIC does not support PC-ACE which is form field chk_submission1[]
    payerName = myANSI.name.substr(4, (myANSI.name.length -6));   
        submissionName = arrSubmission[myANSI.value];    
    tmpName = 'chk_submission' + submissionName + '[]';
    checkboxObj = document.forms[0].elements[tmpName];
    //
    payerName = payerName + submissionName;     // Example: nmic2

	if (myANSI.checked) {
        enableChk(checkboxObj);
        arrSubmissionCnt[submissionName]++;
        arrPayerCnt[payerName]++;
	} else {
        if (arrSubmissionCnt[submissionName] == 1) {
            disableChk(checkboxObj);
        }
        arrSubmissionCnt[submissionName]--;
        arrPayerCnt[payerName]--;
    }
}