﻿$(document).ready(function()
{
    if(LoadRosaceaQuizCookie())
    {
        HidePanelOne();
        HidePanelTwo();
        ShowPanelThree();
    }  
    else
    {
        HidePanelTwo();
        HidePanelThree();
        ShowPanelOne();    
    }  
    
    $("#InvalidZipCode").hide();
    
    $("#quiz-controls-back").click( function()
    {
        window.location.href = "/rosacea-quiz-results.aspx?results=true";
    });
        
    $("#quiz-controls-retake").click( function()
    {
        HidePanelThree();
        ShowPanelOne();
    });
    
    $("#quiz-controls-prev").click( function()
    {
        HidePanelTwo();
        ShowPanelOne();
    });  
            
    $("#quiz-controls-next").click( function()
    {
        //if(($(this).attr('class') == "on"))
        //{
            HidePanelOne();
            ShowPanelTwo();
        //}
    });
    
    $("#quiz-controls-results").click( function()
    {
        var zipCode = $("#ZipCode_TextBox").val();
        
        if(IsValidZipCode(zipCode))
        {
            $("#InvalidZipCode").hide();
            SetRosaceaQuizCookie();
            window.location.href = "/rosacea-quiz-results.aspx?results=true";
        }
        else
        {
            $("#InvalidZipCode").show("slow");
        }
    });
        
    $(".checkBoxOff").live("click", function()
    {
        UpdateRadioButtons($(this).attr("id"));
    });
    
    $(".checkBoxOn").live("click", function()
    {
        UpdateRadioButtons($(this).attr("id"));
    });    
});


IsValidZipCode = function(zipCode) 
{
   return /^(\d{5})?$/.test(zipCode);
}

HidePanelOne = function()
{
    $("#PanelOneTitle").hide();
    $("#PanelOneTwoImage").hide();
    $("#PanelOneContent").hide();
    $("#PanelOneControls").hide();
    
    $("#quiz-controls-dot1").hide();
    $("#quiz-controls-dot2").hide();    
}

HidePanelTwo = function()
{
    $("#PanelTwoTitle").hide();
    $("#PanelOneTwoImage").hide();
    $("#PanelTwoContent").hide();
    $("#PanelTwoControls").hide();
    
    $("#quiz-controls-dot1").hide();
    $("#quiz-controls-dot2").hide();    
}

HidePanelThree = function()
{
    $("#PanelThreeTitle").hide();
    $("#PanelThreeImage").hide();
    $("#PanelThreeContent").hide();
    $("#PanelThreeControls").hide();   
    
    $("#quiz-controls-dot1").hide();
    $("#quiz-controls-dot2").hide();       
}

ShowPanelOne = function()
{
    $("#PanelOneTitle").show(); //fadeIn('slow');
    $("#PanelOneTwoImage").show();
    $("#PanelOneContent").show(); //fadeIn('slow');
    $("#PanelOneControls").show(); //fadeIn('slow');
    
    $("#quiz-controls-dot1").show();
    $("#quiz-controls-dot2").show();  
    
    if( $("#quiz-controls-dot1").css("background-image").indexOf("off") > 0)
        SwapDots();
}

ShowPanelTwo = function()
{
    $("#PanelTwoTitle").show(); //fadeIn('slow');
    $("#PanelOneTwoImage").show();
    $("#PanelTwoContent").show(); //fadeIn('slow');
    $("#PanelTwoControls").show(); //fadeIn('slow');
    
    $("#quiz-controls-dot1").show();
    $("#quiz-controls-dot2").show();  
    
    if( $("#quiz-controls-dot2").css("background-image").indexOf("off") > 0) 
        SwapDots();
}

ShowPanelThree = function()
{
    $("#PanelThreeTitle").show(); //fadeIn('slow');
    $("#PanelThreeImage").show();
    $("#PanelThreeContent").show(); //fadeIn('slow');
    $("#PanelThreeControls").show(); //fadeIn('slow');
    
    $("#quiz-controls-dot1").hide();
    $("#quiz-controls-dot2").hide();  
}

LoadRosaceaQuizCookie = function()
{
    SetOptionOff("Question0001");
    SetOptionOff("Question0002");
    SetOptionOff("Question0003");
    SetOptionOff("Question0004");
    SetOptionOff("Question0005");
    
    SetOptionOff("Question0101");
    SetOptionOff("Question0102");
    
    SetOptionOff("Question0201");
    SetOptionOff("Question0202");
    SetOptionOff("Question0203");
    SetOptionOff("Question0204");
    
    SetOptionOff("Question0301");
    SetOptionOff("Question0302");
    
    SetOptionOff("Question0401");
    SetOptionOff("Question0402");
    SetOptionOff("Question0403");
    
    $("#ZipCode_TextBox").val("");
    
    //
    
    var cookieValue = GetCookie("RosaceaQuiz");
    
    if(cookieValue == null)
        return false;
    
    //
        
    var cookieValues = cookieValue.split("|");
    
    if(cookieValues[0] == "1") SetOptionOn("Question0001"); 
    if(cookieValues[1] == "1") SetOptionOn("Question0002"); 
    if(cookieValues[2] == "1") SetOptionOn("Question0003"); 
    if(cookieValues[3] == "1") SetOptionOn("Question0004"); 
    if(cookieValues[4] == "1") SetOptionOn("Question0005"); 
    
    if(cookieValues[5] == "10") SetOptionOn("Question0101"); 
    else if(cookieValues[5] == "01") SetOptionOn("Question0102"); 
    
    if(cookieValues[6] == "1000") SetOptionOn("Question0201"); 
    else if(cookieValues[6] == "0100") SetOptionOn("Question0202"); 
    else if(cookieValues[6] == "0010") SetOptionOn("Question0203"); 
    else if(cookieValues[6] == "0001") SetOptionOn("Question0204"); 

    if(cookieValues[7] == "10") SetOptionOn("Question0301"); 
    else if(cookieValues[7] == "01") SetOptionOn("Question0302"); 

    if(cookieValues[8] == "100") SetOptionOn("Question0401"); 
    else if(cookieValues[8] == "010") SetOptionOn("Question0402"); 
    else if(cookieValues[8] == "001") SetOptionOn("Question0403"); 

    $("#ZipCode_TextBox").val(cookieValues[9]);
    
    //
    
    //if(ValidateNext())
    //    $("#quiz-controls-next").addClass("on");
    //else
    //    $("#quiz-controls-next").removeClass("on");
    
    return true;
}

SetRosaceaQuizCookie = function()
{
    var cookieValue = "";
    
    cookieValue += (($("#Question0001").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //1
    cookieValue += (($("#Question0002").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //2
    cookieValue += (($("#Question0003").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //3
    cookieValue += (($("#Question0004").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //4
    cookieValue += (($("#Question0005").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //5
    
    cookieValue += (($("#Question0101").attr('class') == "checkBoxOn") ? "1" : "0");
    cookieValue += (($("#Question0102").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //6

    cookieValue += (($("#Question0201").attr('class') == "checkBoxOn") ? "1" : "0");
    cookieValue += (($("#Question0202").attr('class') == "checkBoxOn") ? "1" : "0");
    cookieValue += (($("#Question0203").attr('class') == "checkBoxOn") ? "1" : "0");
    cookieValue += (($("#Question0204").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //7

    cookieValue += (($("#Question0301").attr('class') == "checkBoxOn") ? "1" : "0");
    cookieValue += (($("#Question0302").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //8
    
    cookieValue += (($("#Question0401").attr('class') == "checkBoxOn") ? "1" : "0");
    cookieValue += (($("#Question0402").attr('class') == "checkBoxOn") ? "1" : "0");
    cookieValue += (($("#Question0403").attr('class') == "checkBoxOn") ? "1" : "0") + "|"; //9
    
    cookieValue += $("#ZipCode_TextBox").val(); //10
    
    SetCookie("RosaceaQuiz", cookieValue, 30);
}

/*
ValidateNext = function()
{
    var validate = false;

    validate |= ($("#Question0001").attr('class') == "checkBoxOn");
    validate |= ($("#Question0002").attr('class') == "checkBoxOn");
    validate |= ($("#Question0003").attr('class') == "checkBoxOn");
    validate |= ($("#Question0004").attr('class') == "checkBoxOn");
    validate |= ($("#Question0005").attr('class') == "checkBoxOn");
    
    return validate;
}
*/

UpdateRadioButtons = function(optionId)
{
    var optionNumber = optionId.substring(8, 10);
    
    switch(optionNumber)
    {
        case "00":
            if($("#" + optionId).attr('class') == "checkBoxOn") 
                SetOptionOff(optionId);
            else 
                SetOptionOn(optionId);
            
            /*    
            if(ValidateNext())
                $("#quiz-controls-next").addClass("on");
            else
                $("#quiz-controls-next").removeClass("on");
            */
            
            break;
        
        case "01":
            SetOptionOff("Question0101");
            SetOptionOff("Question0102");
            SetOptionOn(optionId);
            break;

        case "02":
            SetOptionOff("Question0201");
            SetOptionOff("Question0202");
            SetOptionOff("Question0203");
            SetOptionOff("Question0204");
            SetOptionOn(optionId);
            break;

        case "03":
            SetOptionOff("Question0301");
            SetOptionOff("Question0302");
            SetOptionOn(optionId);
            break;

        case "04":
            SetOptionOff("Question0401");
            SetOptionOff("Question0402");
            SetOptionOff("Question0403");
            SetOptionOn(optionId);
            break;
        
        default:
            break;
    }            
}

SetOptionOn = function(elementId)
{
    $("#" + elementId).removeClass("checkBoxOff");
    $("#" + elementId).addClass("checkBoxOn");
}

SetOptionOff = function(elementId)
{
    $("#" + elementId).removeClass("checkBoxOn");
    $("#" + elementId).addClass("checkBoxOff");
}

SwapDots = function()
{
    var dotImage = $("#quiz-controls-dot1").css("background-image");
    $("#quiz-controls-dot1").css("background-image", $("#quiz-controls-dot2").css("background-image"));
    $("#quiz-controls-dot2").css("background-image", dotImage);
}


//////////////////

SetCookie = function(name, value, days) 
{
    if (days) 
    {
        var date = new Date();
        date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
        var expires = "; expires=" + date.toGMTString();
    }
    else 
    {
        var expires = "";
    }
    
    document.cookie = name + "=" + value + expires + "; path=/";
}

GetCookie = function(name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    
    for(var i = 0; i < ca.length; i++) 
    {
        var c = ca[i];
        
        while (c.charAt(0)==' ') 
            c = c.substring(1, c.length);
        
        if (c.indexOf(nameEQ) == 0) 
            return c.substring(nameEQ.length, c.length);
    }
    
    return null;
}

DeleteCookie = function(name) 
{
    SetCookie(name, "", -1);
}




//$("#%=NotOnTreatment_CheckBox.ClientID%>").prop("checked", false);
//$("#%=NoneOfTheAbove_CheckBox.ClientID%>").prop("checked", false);   
