﻿
$(document).ready(function(event) {

    if (loggedIn == "true") {

        //if the page is a redirect to the entry form section
        if (pageSection == "entryform") {
            $("#photoEntriesSection").hide();
            $("#storiesSection").hide();
            $("#prizesSection").hide();
            $("#formSection").show();
            Petcentric.Web.Services.PromotionService.HasUserEnteredWinterWonderland(onCheckEntrySuccess);
            ToggleFormTabOn();
        } else if (pageSection == "thankyou") {
            $("#photoEntriesSection").hide();
            $("#storiesSection").hide();
            $("#prizesSection").hide();
            $("#formSection").show();
            ShowThankYouSubSection();
            ToggleFormTabOn();
        } else {
            $("#photoEntriesSection").hide();
            $("#storiesSection").hide();
            $("#formSection").hide();
            $("#prizesSection").show();
        }
    } else {
        $("#entryformsec").hide();
        $("#notloggedin").show();

        if (pageSection == "entryform" || pageSection == "thankyou") {
            $("#photoEntriesSection").hide();
            $("#storiesSection").hide();
            $("#prizesSection").hide();
            $("#formSection").show();
            ToggleFormTabOn();
        } else {
            $("#photoEntriesSection").hide();
            $("#storiesSection").hide();
            $("#formSection").hide();
            $("#prizesSection").show();
        }
    }



    //bind tab switch to 'enter contest now' button
    $("[id$='hlEnter1']").bind('click', function(event) {
        ToggleFormTabOn();
    });

    $("a.promonavlink").bind('click', function(e) {
        $("#formSection").hide();
    });

    //bind click to toggle tab image on the nav links
    $(".h09nav a").bind('click', function(event) {
        if ($(this).hasClass("off")) {
            var linkImg = $(this).children("img");
            toggleLinkImageButtonElement(this, linkImg, "on", "off", "on");
        }

        var otherTabs = $(this).siblings("a");
        for (i = 0; i < otherTabs.length; i++) {
            var linkElement = otherTabs[i];
            if ($(linkElement).hasClass("on")) {
                var linkImg = $(linkElement).children("img");
                toggleLinkImageButtonElement(linkElement, linkImg, "on", "off", "off");
            }
        }
    });

    //bind the 'official rules' button
    $(".h09rules").bind('click', function(event) {
        h09OpenContestRules();
    });

    //initialize gift select radio buttons
    $("input[name=prizeradio]").bind('click', function(event) {
        $("#prizeRadioError").hide(); //hide error msg when user selects a prize
    });

    //initialize text box
    $("[id$='txtPromoText']").bind("keypress", function(event) {
        $("#promoTextError").hide(); //hide error msg when user types in the text box
    });

    //initialize terms check box
    $("input[name='terms']").bind('click', function(event) {
        $("#termsError").hide(); //hide error msg when user checks the box
    });

    //initialize photo title text box
    $("input[name='phototitle']").bind('keypress', function(event) {
        $("#phototitleerror").hide(); //hide error msg when user types in the text box
    });

    //initialize the email confirm field
    $("[id$='txtEmailConfirm']").bind("keypress", function(event) {
        $("#emailConfirmError").hide();
    });

    //initialize the dob field
    $("[id$='txtDOB']").bind("keypress", function(event) {
        $("#dobError").hide(); //hide error msg when user types in the text box
    });
});

function h09OpenContestRules()
{
	window.open('Winter-Wonderland-Contest-Rules.aspx');
}

function OnClickSubmissionNoAuth(url) {
    ShowLoginPanel(url);
    return false;
}

function ShowPrizesSection() {
    $("#photoEntriesSection").hide();
    $("#storiesSection").hide();
    $("#formSection").hide();
    $("#prizesSection").show();
}
function ShowWinnersSection() {
    $("#photoEntriesSection").hide();
    $("#storiesSection").hide();
    $("#photoWinnersSection").show(); ;
}

//helper method to manipulate the src attribute, either add on '_on.gif' or remove '_on' .
//this method assumes that the naming convention of the toggle images follows the mentioned format, and is .gif
function getToggleImageSrc(toggle, src) {
    if (toggle == "on") {
        return src.substring(0, src.length - 4) + "_on.gif";
    }
    else if (toggle == "off") {
        return src.substring(0, src.length - 7) + ".gif";
    }
    return "";
}

//works for the format <a><img/></a>
function toggleLinkImageButtonElement(element, linkImage, toggleOnClass, toggleOffClass, toggleOption) {
    if (toggleOption == "off") {
        //alert('test 1, toggleLinkImageButtonElement: toggle to on, src - ' + $(linkImage).attr("src")); 
        $(element).removeClass(toggleOnClass).addClass(toggleOffClass);
        $(linkImage).attr("src", getToggleImageSrc(toggleOption, $(linkImage).attr("src")));
    } else if (toggleOption == "on") {
        //alert('test 1, toggleLinkImageButtonElement: toggle to off, src - ' + $(linkImage).attr("src")); 
        $(element).removeClass(toggleOffClass).addClass(toggleOnClass);
        $(linkImage).attr("src", getToggleImageSrc(toggleOption, $(linkImage).attr("src")));
    }
}

function ToggleFormTabOn() {
    //alert('test1 toggleformtabon');
    var onLink = $("div.h09nav a.on");
    var onLinkImg = $(onLink).children("img");
    //alert('test1 onLink' + onLink + ',' + onLinkImg);
    toggleLinkImageButtonElement(onLink, onLinkImg, "on", "off", "off");

    var formLink = $("[id$='hlEnter2']");
    var formLinkImg = $(formLink).children("img");
    //alert('test1 formLink' + formLink + ',' + formLinkImg);
    toggleLinkImageButtonElement(formLink, formLinkImg, "on", "off", "on");
}

function DisplayForm() {
    $("#photoEntriesSection").hide();
    $("#storiesSection").hide();
    $("#prizesSection").hide();
    $("#formSection").show();
    if (loggedIn == "true") {
        //see if user is already on the tab
        if ($("[id$='hlEnter2']").hasClass("off")) {
            //check to see whether user already participated
            //Petcentric.Web.Services.PromotionService.HasUserEnteredWinterWonderland(onCheckEntrySuccess);
            Petcentric.Web.Services.UserService.IsCurrentUserLoggedIn(onCheckUserLoginSuccess);
        }
    }
}

function onUserProfileUpdateSuccess(Result, EventArgs) {
    //testing purposes
/*
    var giftId = $("input[name=prizeradio]").filter(":checked").siblings("input[type=hidden]").attr("value");
    var txt = $("[id$='txtPromoText']").val();
    var photourl = $("#hfPhotoUrl").val();
    var phototitle = $("input[name=phototitle]").val();
    Petcentric.Web.Services.PromotionService.AddWinterWonderlandEntry(txt, photourl, phototitle, giftId, onAddPromotionSuccess, onAddPromotionFail);
*/
    
    if (Result.toString() == "true") {//userservice has successfully updated the profile info
        var giftId = $("input[name=prizeradio]").filter(":checked").siblings("input[type=hidden]").attr("value");
        var txt = $("#<%= txtPromoText.ClientID %>").val();
        var photourl = $("#hfPhotoUrl").val();
        var phototitle = $("input[name=phototitle]").val();
        Petcentric.Web.Services.PromotionService.AddWinterWonderlandEntry(txt, photourl, phototitle, giftId, onAddPromotionSuccess, onAddPromotionFail);
    } else if (Result.toString() == "false") {
        $("#entryformsec").hide();
        $("#submiterror").show();
        $("div.submitmessage").removeClass("on").addClass("off").hide();
    }
    
}

function onUserProfileUpdateFail(Result, EventArgs) {
    $("div.submitmessage").removeClass("on").addClass("off").hide();
    $("#entryformsec").hide();
    $("#submiterror").show();
}

function onAddPromotionFail(Result, EventArgs) {
    $("div.submitmessage").removeClass("on").addClass("off").hide();
    $("#entryformsec").hide();
    $("#submiterror").show();
}

function wordCount(txt) {
    txt = txt.replace(/(<([^>]+)>)/ig, ""); //strip HTML
    txt = txt.replace(/\s*((\S+\s*)*)/, "$1"); //ltrim
    txt = txt.replace(/((\s*\S+)*)\s*/, "$1"); //rtrim
    if (txt.match(/^ *$/)) {
        return 0;
    } else {
        return txt.split(/\s+/g).length;
    }
}

OnSubmitCroppedImage = function(url) {
    if ($("input[name=phototitle]").val() == "") {
        $("#phototitleerror").show(); //display error msg
    } else {
        $("#phototitleerror").hide();
        $("#hfPhotoUrl").attr("value", url);
        $.modal.close();
    }
}

onUploadError = function(result) {
}

function CheckDOB() {
    var dobTxtBox = $("[id$='txtDOB']");
    var date = $(dobTxtBox).val();
    var dateArray = date.split("/");
    if (dateArray && dateArray.length == 3) {
        var month = dateArray[0] - 1; //month starts at 0 for javascript date objects
        var date = dateArray[1];
        var year = dateArray[2];

        var birthDate = new Date();
        birthDate.setFullYear(year, month, date);
        birthDate.setFullYear(birthDate.getFullYear() + 21, month, date); //add 21 years to the birth date and then compare to the current date
        var currentDate = new Date();
        if (birthDate <= currentDate) {//if birth date + 21 years is smaller than the current date, then the user is at least 21 years old
            return true;
        } else {
            return false;
        }
    }
}

function ShowThankYouSubSection() {
    //hide the other subsections under formSection
    $("#entryformsec, #submiterror, #notloggedin, #alreadyentered, #submitcomplete").hide();
    $("#thankyousection").show();
}


