﻿/* validates user entry on where to buy page */
/* zip - zip entry field id */
/* error - div containing error message to be shown */

function ValidateZipEntry(field, error)
{
    var zip = document.getElementById(field).value;
    
    if(zip.match(/^\d{5}$/))
    {
        return true;
    }
    else
    {
        $("#" + error).css("display", "block");
        document.getElementById(field).focus();
        return false;
    }
}

function AlignPrivacyBox()
{
    var padHeight = $("#reg_heading_row").innerHeight();
    var height = $("#registration").innerHeight();
    
    height = height - padHeight;
    
    $("#privacy_area").css("padding-top", padHeight);      
    $("#privacy_area").css("height", height);  
}

function ExpandAllFAQItems()
{
    $("#closeAll").css("display", "block");
    $("#expandAll").css("display", "none");

    $("#faq div.question").addClass("expanded");
}

function CloseAllFAQItems()
{
    $("#expandAll").css("display", "block");
    $("#closeAll").css("display", "none");
    
    $("#faq div.question").removeClass("expanded");
}

function ExpandFAQItem(id)
{
    var jQueryID = "#" + id;
   
    $("#" + id).addClass("expanded");
}

function CollapseFAQItem(id)
{
    var jQueryID = "#" + id;
   
    $("#" + id).removeClass("expanded");
}

function ToggleNutritionPanel(flavor)
{
    if(flavor == 'van')
    {
        $("#choc_link").removeClass("active");    
        $("#van_link").addClass("active");  
        $("#choc_panel").removeClass("np_panel");
        $("#choc_panel").addClass("np_panel_hidden");
        $("#van_panel").removeClass("np_panel_hidden");
        $("#van_panel").addClass("np_panel");
    }    
    
    else if(flavor == 'choc')
    {
        $("#van_link").removeClass("active");
        $("#choc_link").addClass("active");    
        $("#van_panel").removeClass("np_panel");
        $("#van_panel").addClass("np_panel_hidden");
        $("#choc_panel").removeClass("np_panel_hidden");
        $("#choc_panel").addClass("np_panel");    
    }    
}

/* validates that a user's zip is within the test market area */
function ValidateUserForCoupons(e)
{
    if(ValidateZipEntry('txt_coupon_zip', 'coupon_zip_error'))
    {
        var z = $("#txt_coupon_zip").val();
    
        CheckUserZipCode(z);
    }
    
    return false;
}

function CheckUserZipCode(z) 
{
    $.ajax({
        type: "GET",
        url: "/buy-sensiva/verify-zip-code.aspx",
        data: "zip=" + z,
        dataType: "xml",
        cache: false,
        success: function(xmlObj) {
            var valid = $(xmlObj).find("valid").text();
            
            if(valid == "true")
            {
                $("#coupon_signup_form").fadeIn("normal");
                $("#verify_zip").slideUp("fast");
                AlignPrivacyBox();                 
            }
            else
            {
                window.location.replace("/buy-sensiva/unavailable");
            }        
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert('There was an error submitting your request. Please try again later.');
        }
    }); 
}