﻿
var cycleMessage = "";
var wint = 0;

var showCycle = 0;
var cycleMessage = "";
var curMessage = "";



//ClearMessage();

// start meal plan code
function mpCbResult(result) {


    if (result == -1) {
        alert("Web service soft fail")
    }
    if (result == 0) {
        alert("This recipe is already in your NutritionPlan")
    }
    
    resAr = result.split("|")

    if (resAr.length > 1) {

        document.getElementById("mealPlanId").value = resAr[0]

        showCycle = 0;
        cycleMessage = "Recipe Added";
        ShowMessageStart()
        //document.getElementById("ppMessage").value = "Recipe Added";
        
    }
    else {
        if (result == "1") {
            showCycle = 0;
            cycleMessage = "Recipe Removed";
            ShowMessageStart()
            
        }
        
    }

}


function ClearMessage() {
    curMessage = "";
    cycleMessage = "";
    showCycle = 6;
    ShowMessageStart();
}

function ShowMessageStart() {

    //alert('message start');
    
    if (typeof(wint) != "undefined") {
        window.clearInterval(wint)
    }
    if (showCycle < 7) {
        if (curMessage.length == 0) {
            curMessage = cycleMessage;
        }
        else {
            curMessage = "";
        }
        showCycle++;
        wint = window.setInterval(showvalue, 150)


    }
    else {
        curMessage = "";
    }

}

function showvalue() {

    
    window.clearInterval(wint)
    document.getElementById("ppMessage").value = curMessage;
    wint = window.setInterval(ShowMessageStart, 150)
   
    
//    if (document.getElementById("ingfilter").value != buffer) { // if still inputing set another interval and then check again
//        buffer = document.getElementById("ingfilter").value
//        wint = window.setInterval(showvalue, 400)
//        return false;
//    }
//    flag = 0
//    getselIngreds()
}

// get a subset for the select list based on the keyin in the box above the select list
function getIngsSubset(sub) {
    if (flag == 0) {
        flag = 1
        wint = window.setInterval(showvalue, 800)
        buffer = sub.value
    }


}




function SetMealPlan(obin) {

    //document.getElementById("recid")

    if (obin.id != null) {
        controlIdAr = obin.id.split("-")
    }
    else {
        controlIdAr = obin.split("-")
    }

    if (document.getElementById("mealPrepSwitch").value == 0) { // it is a meal plan add
        //service.MyService.callService(mpResult,"insertMealPlanRecipe", document.getElementById("recid").value, iuser, retArray[0], retArray[1], retArray[2],document.getElementById("siteid").value )

        Xurl = "LocalServices.aspx"

        pars = "&userno=" + iuser + "&mealplanid=" + document.getElementById("mealPlanId").value + "&mpname=" + document.getElementById("mealPlanName").innerHTML + "&recid=" + document.getElementById("recid").value + "&dtid=" + controlIdAr[1] + "&site=" + document.getElementById("siteid").value

        if (controlIdAr[0] == "c") { // coming from the checkbox

            if (document.getElementById(obin.id).checked == true) { // add the recipe
                callService(mpCbResult, "insertMealPlanRecipe", pars);
            }
            else { //remove the recipe
                callService(mpCbResult, "RemoveMealFromPlanByDayTime", pars);
            }
        }
        else { // coming from the link
            callService(mpCbResult, "insertMealPlanRecipe", pars);
            document.getElementById("c-" + controlIdAr[1]).checked = true;
        }
    }
    else { // it is a prep plan add


        Xurl = "LocalServices.aspx"

        pars = "&user=" + iuser + "&preplanid=" + retArray[1] + "&preplanname=" + retArray[2] + "&recid=" + document.all["recid"].value

        callService(BPresult, "insertPrepPlanRecipe", pars);
    }

}

function ShowMealPlan() {

    document.getElementById("ifShoppingList").src = "mealPlanDetailNoHeader.aspx?recno=" + document.getElementById("mealPlanId").value
    

}

function add2mealPlan(switchIn) {
    
        
        // the switchin comes from the side
        // the call from recipeFindA has no switchin
    if (switchIn != "new") {

        if (document.getElementById("mealplans").innerHTML == "Build Nutrition Plan") {
            document.getElementById("mealPlanTd").className = "show"
            document.getElementById("mealplans").innerHTML = "Hide Nutrition Plan"

            if (iuser == 0) {
                alert("Sorry you must register or login before using this feature")
                getElementByIdEx("pnlNewUser", "div").className = "memberTable"
                return
            }
            if (document.getElementById("recid").value.length == 0) {
                alert("Please choose a recipe first")
                return
            }

            // first make an ajax call to see if there is a default meal plan
            pars = "&userid=" + iuser;

            callService(TestPlanReturn, "CheckDefaultPlan", pars);


        }
        else { // hide the panel
            document.getElementById("mealPlanTd").className = "hide"
            document.getElementById("mealplans").innerHTML = "Build Nutrition Plan"

        }
    }
    else { // coming from the new button on the left

        ShowPlanModal();

    }




}


function TestPlanReturn(result) {

    
    
    retArray = result.split("|")
    // retArray[0] = mealplan index , retArray[1] = mealplan name 

        
    
    if (retArray[0] > 0) {
        // if result is > 0 there is a default plan so just set it up
        document.getElementById("mealPlanId").value = retArray[0];
        document.getElementById("mealPlanName").innerHTML = retArray[1];

        pars = "&planid=" + retArray[0]
        callService(GetPlanReturn, "getPlanArray", pars);

    }
    else {
        ShowPlanModal();
    }

}

function ShowPlanModal() {

    // else get meal plan
    retVal = showModalDialog('mealplanbuilder2.aspx?mpuser=' + iuser, 0, 'dialogWidth:404px; dialogHeight:350px;status:no;resizable:no;unadorned:yes');
    if (retVal.length == 0) {
        document.getElementById("mealPlanTd").className = "hide"
        document.getElementById("mealplans").innerHTML = "Build Nutrition Plan"
        return;
    }

    if (retVal == -1) {  // user cancelled
        document.getElementById("mealPlanTd").className = "hide"
        document.getElementById("mealplans").innerHTML = "Build Nutrition Plan"
        return;
    }
    retArray = retVal.split("|")
    // retArray[0] = mealplan index , retArray[1] = mealplan name , retArray[2] = mealplan / prepplan switch


    document.getElementById("mealPlanId").value = retArray[0];
    document.getElementById("mealPlanName").innerHTML = retArray[1];
    document.getElementById("mealPrepSwitch").value = retArray[2];

    
    // now get the already selected recipe array.
    if (document.getElementById("mealPlanId").value != 0) { // not a new meal plan - so get the meals

        pars = "&planid=" + document.getElementById("mealPlanId").value

        callService(GetPlanReturn, "getPlanArray", pars);

    }
    else { // if it is a new plan (mealPlanId = 0) it wont be written out until the first meal is chosen
        // just clear the meals
        var ckboxes = document.getElementsByTagName("input");
        for (f = 0; f < ckboxes.length; f++) {
            ckboxes[f].checked = false;
        }
    }



}


function GetPlanReturn(result) {

    planAr = result.split("~");
    
    var ckboxes = document.getElementsByTagName("input");
        
    // first clear the boxes
    for (f = 0; f < ckboxes.length; f++) {
        ckboxes[f].checked = false;
    }

    // then fill them
    for (i = 0; i < planAr.length - 1; i++) {
        document.getElementById(planAr[i]).checked = true;
    }
}



function BPresult(result) {


    ////		if (result > 0){
    ////			alert("This recipe has been successfully added to your Prep/Baking Plan")
    ////		}
    if (result == 0) {
        alert("This recipe is already in your Prep/Baking Plan")
    }
}


// end meal plan code
