﻿
function disableAllTabs()
{
    var count = 10;
    var i = 2;
    for (i; i < count; i++)
    {
        $('#rootwizard').bootstrapWizard('disable', i);
        $('#rootwizard li:eq(i) a').attr("disabled", "disabled");
    }
}


function setMaxFieldLength(id, length)
{
    $("#" + id).on("input", function ()
    {
        var temp = $(this).val();
        if (temp.length > length)
        {
            $(this).val(temp.substring(0, length));
        }
    });
}

function convertDate(d)
{
    var first = d;
    var str = "";
    if (new Date(d) != "Invalid Date")
    {
        if (!first.match(/[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]/))
        {
            str = first.substring(5, 7) + "/" + first.substring(8) + "/" + first.substring(0, 4);
        }
        else
        {
            str = d;
        }

    }
    console.log(str);

    return str;
}

function editNav(num)
{
    //$('#rootwizard').find("a[href*='#submit']").trigger('click');

    switch (num)
    {
        case 0:
            $('#rootwizard').find("a[href*='#Location']").trigger('click');
            break;

        case 1:
            $('#rootwizard').find("a[href*='#reportingparty']").trigger('click');
            break;

        case 2:
            $('#rootwizard').find("a[href*='#property']").trigger('click');
            break;

        case 3:
            $('#rootwizard').find("a[href*='#vehicle']").trigger('click');
            break;

        case 4:
            $('#rootwizard').find("a[href*='#information']").trigger('click');
            break;

        case 5:
            $('#rootwizard').find("a[href*='#images']").trigger('click');
            break;

        case 6:
            $('#rootwizard').find("a[href*='#additionalnames']").trigger('click');
            break;

        default:

            break;
    }
}








function displayError(validFields)
{
    var errorMessage = "The following fields are required:\n";
    for (var i = 1; i < validFields.length; i++)
    {
        errorMessage += validFields[i];
        if (i + 1 != validFields.length)
        {
            errorMessage += "\n";
        }
    }
    alert(errorMessage);
    //alert(validFields[0]);
    document.getElementById(validFields[0]).focus();
}





function getMappedValue(table, val)
{
    var x = "";
    for (var i = 0; i < table.length; i++)
    {
        if (table[i].value == val)
        {
            x = table[i].innerHTML;
        }
    }

    return x;
}





function selectedReportTypes()
{
    var reportOptions = document.getElementsByName("ReportTypeOption");
    for (var i = 0; i < reportOptions.length; i++)
    {
        if (reportOptions[i].checked)
        {
            reportOptions[i].click();
            return i;
        }
    }
    return -1;
}

function checkIfPropertyVehicle(index)
{
    var reportTable = document.getElementById("ReportTypeTable");
    console.log("test");
    propIsTrue = reportTable.rows[index + 1].cells[4].innerHTML;
    vehiIsTrue = reportTable.rows[index + 1].cells[5].innerHTML;
    mediaIsTrue = reportTable.rows[index + 1].cells[6].innerHTML;
    nameIsTrue = reportTable.rows[index + 1].cells[7].innerHTML;
    requireProperty = reportTable.rows[index + 1].cells[8].innerHTML;
    requireVehicle = reportTable.rows[index + 1].cells[9].innerHTML;
    console.log(mediaIsTrue);
    //alert("Prop: " + prop + "\nVehicle: " + vehi);
    document.getElementById("PropertyDescriptionLabel").style.color = 'black';
    if (propIsTrue == 'True')
    {
        propIsTrue = true;
        $('#rootwizard').bootstrapWizard('display', 6);
    }
    else
    {
        propIsTrue = false;
        $('#rootwizard').bootstrapWizard('hide', 6);
    }

    if (vehiIsTrue == "True")
    {
        vehiIsTrue = true;
        $('#rootwizard').bootstrapWizard('display', 7);
    }
    else
    {
        vehiIsTrue = false;
        $('#rootwizard').bootstrapWizard('hide', 7);
    }
    if (mediaIsTrue == "True")
    {
        mediaIsTrue = true;
        $('#rootwizard').bootstrapWizard('display', 8);
    }
    else
    {
        mediaIsTrue = false;
        $('#rootwizard').bootstrapWizard('hide', 8);
    }
    if (nameIsTrue == "True")
    {
        nameIsTrue = true;
        $('#rootwizard').bootstrapWizard('display', 5);
    }
    else
    {
        nameIsTrue = false;
        $('#rootwizard').bootstrapWizard('hide', 5);
    }

    if (requireProperty == 'True' && requireVehicle != 'True')
    {
      
        document.getElementById("PropertyDescriptionLabel").style.color = 'red';


        if (propertyInfoTable.length > 0)
        {
            $('#rootwizard').bootstrapWizard('enable', 9);
            $('#rootwizard li:eq(9) a').removeAttr('disable');
        }
        else
        {
            $('#rootwizard').bootstrapWizard('disable', 9);
        }
    }

    else if (requireProperty != 'True' && requireVehicle == 'True')
    {
        if (vehicleInfoTable.length > 0)
        {
            $('#rootwizard').bootstrapWizard('enable', 9);
            $('#rootwizard li:eq(9) a').removeAttr('disable');
        }
        else
        {
            $('#rootwizard').bootstrapWizard('disable', 9);
        }
    }

    else if (requireProperty == 'True' && requireVehicle == 'True')
    {
        document.getElementById("PropertyDescriptionLabel").style.color = 'red';
        if (propertyInfoTable.length > 0 && vehicleInfoTable.length > 0)
        {
            $('#rootwizard').bootstrapWizard('enable', 9);
            $('#rootwizard li:eq(9) a').removeAttr('disable');
        }
        else
        {
            $('#rootwizard').bootstrapWizard('disable', 9);
        }
    }

    if(requireProperty == "Split" && requireVehicle == "Split")
    {
        console.log("One is required");
        if (propertyInfoTable.length > 0 || vehicleInfoTable.length > 0)
        {
            $('#rootwizard').bootstrapWizard('enable', 9);
            $('#rootwizard li:eq(9) a').removeAttr('disable');
        }
        else
        {
            $('#rootwizard').bootstrapWizard('disable', 9);
        }
    }

}

function ReportTypeOptionClick()
{
    //console.log("reporttypeoptionclick")
    var index = selectedReportTypes();
    if (index != -1)
    {
        //alert(index);
        //Add complete removal of stuff from tables
        //console.log("selected index is good")
        checkIfPropertyVehicle(index);
        $('#rootwizard').bootstrapWizard('enable', 2);
        $('#rootwizard li:eq(2) a').removeAttr('disable');
    }
    //console.log(index)
    return index;
}



function stripPhoneNumber(num)
{
    var temp = num.toString();
    var ret = "";
    for (var i = 0; i < temp.length; i++)
    {
        if (temp[i].match(/[0-9]/))
        {
            ret += temp[i];
        }
    }
    return ret;
}




function getStr(arr)
{
    var str = "";
    for (var i = 0; i < arr.length; i++)
    {
        str += arr[i];
        if (i != arr.length)
        {
            str += "|";
        }
    }
    return str;
}


function getTabHelp()
{
    //alert(tabvalue);

    switch (tabvalue)
    {
        case "1":
            document.getElementById("moduletitle").innerHTML = "Welcome";
            document.getElementById("tabhelptext").innerHTML = "This is your Online Crime Report. Fill in each page with as much information as you can.<hr />The navigation buttons at the bottom of the screen allow you move between available tabs. At the end of the report you will be able to review all of the information that you have entered. After the report has been submitted, you will recieve an email and be given the option to print it.";
            break;
        case "2":
            document.getElementById("moduletitle").innerHTML = "Report Type Help";
            document.getElementById("tabhelptext").innerHTML = "The table on this page shows all of the possible report types that you are able to choose from. <hr /> Click/select the row that best describes the type of report that you would like to make.";
            break;
        case "3":
            document.getElementById("moduletitle").innerHTML = "Reporting Party Help";
            document.getElementById("tabhelptext").innerHTML = "Complete as much of the information that you have available.<hr /> The following fields are required before continuing: <br /><b>First Name</b><br /><b>Last Name</b><br /><b>Email Address</b>";
            break;
        case "4":
            document.getElementById("moduletitle").innerHTML = "Location Help";
            document.getElementById("tabhelptext").innerHTML = "Complete as much of the information that you have available.<br />The Use Previous Address button will autofill the address, apt #, city, state, and zip code from the Reporting Party tab if it was filled out.<br /><hr /> The following fields are required before continuing: <br /><b>Address</b><br /><b>City</b><br /><b>State</b><br /><b>Zip Code</b><br /><b>Time Occured</b>";
            break;

        case "5":
            document.getElementById("moduletitle").innerHTML = "Identify Help";
            document.getElementById("tabhelptext").innerHTML = "Complete as much of the information that you have available.<br />The save button will insert the information into the table below when all of the required fields are completed.<br /> The clear button will reset all of the entry fields.<hr /> The following fields are required before continuing: <br /><b>Involvement</b><br /><b>Description</b>";
            break;

        case "6":
            document.getElementById("moduletitle").innerHTML = "Property Help";
            document.getElementById("tabhelptext").innerHTML = "Complete as much of the information that you have available.<br /> The save button will insert the information into the table below when all of the required fields are completed.<br /> The clear button will reset all of the entry fields.<hr /> The following fields are required before continuing: <br /><b>Reason for Report</b><br /><b>Property Type</b><br /><b>Quantity</b><br /><b>Value</b><br /><b>Brand/Make</b>";
            break;
        case "7":
            document.getElementById("moduletitle").innerHTML = "Vehicle Help";
            document.getElementById("tabhelptext").innerHTML = "Complete as much of the information that you have available.<br /> The save button will insert the information into the table below when all of the required fields are completed.<br /> The clear button will reset all of the entry fields.<hr /> The following fields are required before continuing: <br /><b>Make</b><br /><b>Model</b><br /><b>Year</b>";
            break;

        case "8":
            document.getElementById("moduletitle").innerHTML = "Media Help";
            document.getElementById("tabhelptext").innerHTML = "All media files must have a comment explaining the contents of the file.<hr />Click the Upload File button to choose a file.<br />Allowed File Formats:<br /><b>JPG</b><br /><b>PNG</b>";
            break;

        case "9":
            document.getElementById("moduletitle").innerHTML = "Information Help";
            document.getElementById("tabhelptext").innerHTML = "This page is for any other information concerning this report.<hr />This would include things such as: <br />Subject Description <br />What you were doing during the incident <br />How often the happens <br /> Etc";
            break;
        case "10":
            document.getElementById("moduletitle").innerHTML = "Submit Help";
            document.getElementById("tabhelptext").innerHTML = "This is the end of the report and the summary of the information you have given.<hr />The navigation buttons at the bottom of the screen allow you to go to the previous screen and the next screen.You will be able to go back and change the information at the end before tou submit the report.You will also be able to print the report after it has been submitted.";
            break;
        default:
            break;
    }
}


function replaceBar(str)
{
    var temp = "";
    for (var i = 0; i < str.length; i++)
    {
        if (str[i] != "|" || str[i] != "\"")
        {
            temp += str[i];
        }
    }
    return temp;
}
