﻿// ON DOCUMENT READY
$(document).ready(function() {

 // DISABLE ENTER KEY ON INPUTS
    $('input').keypress(function(e) {
        if (e.keyCode == 13) return false;
    });

    $('input.frzipcodesearch').keypress(function(e) {
        if (e.keyCode == 13) {
            if ($(this).val() > 10000) {
                searchfrZip();
            } else {
                $('#locations').html("<p>Please enter a five digit zip code.</p>");
            }
        }
    });

    $('#btn-fn-find-restaurant').click(function(event) { //locations page
        event.preventDefault();
        if ($('input.frzipcodesearch').val() > 10000) {
            searchfrZip();
        } else {
            $('#locations').html("<p>Please enter a five digit zip code.</p>");
        }
    });
});

function searchfrZip() {
	findPOISByDistance($('input.frzipcodesearch').val(), $("#radius").val());
}

function findPOISByDistance(zipcode, radius) {
	var zipexp = /^\d{5}$/;
	if ( zipcode.match(zipexp) ) {
		//hbxCall("/locations/fm", "zipcode/"+zipcode);
		//Perform the search
	    $.get("ajax/find_stores_in_range.asp",
	        {
	            zipcode: zipcode,
	            radius: radius
	        },
	        function(results) {
	            $("#locations").html(results);
	        }
	    );
	} else {
		displaySearchResults("");
	}
}

function FixNoteLength() 
{
    if ($get("ctl00_Body_lblCharsRemaining") != null)
    {
        $get("ctl00_Body_lblCharsRemaining").value = (500 - $get("ctl00_Body_txtOrganizationDescription").value.length);
        if ($get("ctl00_Body_lblCharsRemaining").value < 0)
        {
            var max = $get("ctl00_Body_txtOrganizationDescription").value.substring(0,500);
            $get("ctl00_Body_txtOrganizationDescription").value = max;
        }
    }

    if ($get("ctl00_Body_myFunRaiser_lblCharsRemaining") != null)
    {
        $get("ctl00_Body_myFunRaiser_lblCharsRemaining").value = (500 - $get("ctl00_Body_myFunRaiser_txtOrganizationDescription").value.length);
        if ($get("ctl00_Body_myFunRaiser_lblCharsRemaining").value < 0)
        {
            var max = $get("ctl00_Body_myFunRaiser_txtOrganizationDescription").value.substring(0,500);
            $get("ctl00_Body_myFunRaiser_txtOrganizationDescription").value = max;
        }
    }
    
    if ($get("ctl00_Body_myNewFunRaiser_lblCharsRemaining") != null)
    {
        $get("ctl00_Body_myNewFunRaiser_lblCharsRemaining").value = (500 - $get("ctl00_Body_myNewFunRaiser_txtOrganizationDescription").value.length);
        if ($get("ctl00_Body_myNewFunRaiser_lblCharsRemaining").value < 0)
        {
            var max = $get("ctl00_Body_myNewFunRaiser_txtOrganizationDescription").value.substring(0,500);
            $get("ctl00_Body_myNewFunRaiser_txtOrganizationDescription").value = max;
        }
    }
}

function FixFunRaiserNoteLength() 
{
    var max = 2000 - $get("ctl00_Body_myFunRaiser_lblNotes").innerHTML.length
    if ($get("ctl00_Body_myFunRaiser_txtCharsLeft") != null)
    {
        $get("ctl00_Body_myFunRaiser_txtCharsLeft").value = (max - $get("ctl00_Body_myFunRaiser_txtNewNote").value.length);
        if ($get("ctl00_Body_myFunRaiser_txtCharsLeft").value < 0)
        {
            var max = $get("ctl00_Body_myFunRaiser_txtNewNote").value.substring(0,max);
            $get("ctl00_Body_myFunRaiser_txtNewNote").value = max;
        }
    }
}

// the date format prototype
function GetLongDate(indate)
{
    var output = "";
    
    var outdate = new Date(indate);
    output = outdate.getDayName() + ", " + outdate.getMonthName() + ", " + outdate.getFullYear();
    return outdate;
}

function getStartEndTime(selectedvalue)
{
    switch (selectedvalue) 
    {
        case "1":
            return "5:00 p.m.";
            break;
        case "2":
            return "5:30 p.m.";
            break;
        case "3":
            return "6:00 p.m.";
            break;
        case "4":
            return "6:30 p.m.";
            break;
        case "5":
            return "7:00 p.m.";
            break;
        case "6":
            return "7:30 p.m.";
            break;
        case "7":
            return "8:00 p.m.";
            break;
    }
}

function ShowPDFFlyer(SourcePageType)
{
    var params;
    var sourcepage;
    
    if (SourcePageType == 1 || SourcePageType == 3 || SourcePageType == 5) //funraiser edit, admin edit, newadmin edit
    {
        if (SourcePageType == 1)
            sourcepage = "ctl00_Body_";
        else if (SourcePageType == 3)
            sourcepage = "ctl00_Body_myFunRaiser_";
        else
            sourcepage = "ctl00_Body_myNewFunRaiser_";
        
        if ($get(sourcepage + "radioFullPage").checked == true)
            params = "&flyerpagelayout=F";
        else
            params = "&flyerpagelayout=H";
        
        params += "&Status=" + $get(sourcepage + "hiddenStatusID").value;
        var arrivaltime = "5:00 pm to 8:00 pm";
        var arrivaltime2 = "";

//        if ($get(sourcepage + "Arrive0").checked == true)
//        {
//            arrivaltime = "Most guests will arrive between";
//            arrivaltime2 = getStartEndTime($get(sourcepage + "ddlbetweenStart").value);
//            arrivaltime2 += " and " + getStartEndTime($get(sourcepage + "ddlbetweenEnd").value);
//        }
//        if ($get(sourcepage + "Arrive1").checked == true)
//        {
//            arrivaltime = "Most guests will arrive";
//            arrivaltime2 = "around " + getStartEndTime($get(sourcepage + "ddlallatonce").value);
//        }
//        if ($get(sourcepage + "Arrive2").checked == true)
//        {
//            arrivaltime = "Guests will arrive throughout the";
//            arrivaltime2 = "evening from 5:00 to 8:00 p.m.";
//        }
        if (SourcePageType == 3 || SourcePageType == 5)
        {
            if ($get(sourcepage + "Arrive3").checked == true)
            {
                arrivaltime = $get(sourcepage + 'txtFromTime').value + " to " + $get(sourcepage + 'txtToTime').value;
            }
            if ($get(sourcepage + "Arrive4").checked == true)
            {
                arrivaltime = "This is a catering order.";
                arrivaltime2 = "Pickup at " + $get(sourcepage + 'txtPickupTime').value;
            }
        }
        params += "&arrivaltime=" + arrivaltime + "&arrivaltime2=" + arrivaltime2;
        params += "&store_id=" + $get(sourcepage + 'hiddenStore_ID').value;
        
        var outdate = new Date($get(sourcepage + 'txtFunRaiserStart').value);
        
        var outdate2;
        if (SourcePageType > 2)
            outdate2 = new Date($get(sourcepage + 'txtFunRaiserEnd').value);
           
        params += "&startdate=" + outdate.toLocaleDateString();
        if (SourcePageType > 2)
        {
            if ($get(sourcepage + 'txtFunRaiserEnd').value != $get(sourcepage + 'txtFunRaiserStart').value)
                params += " - ";
            params += "&enddate=" + outdate2.toLocaleDateString();
        }
        else
            params += "&enddate=";
            
        params += "&organizationname=" + encodeURIComponent($get(sourcepage + 'txtOrganizationName').value);
        params += "&Flyer2ndLine=" + encodeURIComponent($get(sourcepage + 'txtFlyer2ndLine').value);
        params += "&Flyer3rdLine=" + encodeURIComponent($get(sourcepage + 'txtFlyer3rdLine').value);
        params += "&Status=" + $get(sourcepage + "hiddenStatusID").value;
        
     }
     if (SourcePageType == 2 || SourcePageType == 4 || SourcePageType == 6) //review, admin review, newadminreview
     {
        if (SourcePageType == 2)
            sourcepage = "ctl00_Body_";
        else if (SourcePageType == 4)
            sourcepage = "ctl00_Body_myFunRaiser_";
        else
            sourcepage = "ctl00_Body_myNewFunRaiser_";
        
        params = "&flyerpagelayout=" + $get(sourcepage + "hiddenFlyerType").value;
        
        var arrivaltime = "5:00 p.m. to 8:00 p.m.";
        var arrivaltime2 = '';
        
        if (SourcePageType == 4)
            arrivaltime = $get('ctl00_Body_myFunRaiser_hiddenReviewTime').value;
        else if (SourcePageType == 6)
            arrivaltime = $get('ctl00_Body_myNewFunRaiser_hiddenReviewTime').value;
        
        params += "&arrivaltime=" + arrivaltime + "&arrivaltime2=" + arrivaltime2;
        params += "&store_id=" + $get(sourcepage + 'hiddenStore_ID').value;
        
        params += "&startdate=" + $get(sourcepage + 'lblReviewDate').innerHTML;
        
        if (SourcePageType > 2)
            params += "&enddate=" + $get(sourcepage + 'lblReviewDate2').innerHTML;
        else
            params += "&enddate=" + $get(sourcepage + 'lblReviewDate2').innerHTML;


        var orgname = $('#' + sourcepage + 'lblOrganizationName').html();
        var flyer2 = $('#' + sourcepage + 'lblAdditionalInfo').html();
        var flyer3 = $('#' + sourcepage + 'lblAdditionalInfo2').html();
        var decoded = $("<div/>").html(orgname).text();

        params += "&organizationname=" + encodeURIComponent(decoded);
        decoded = $("<div/>").html(flyer2).text();
        params += "&Flyer2ndLine=" + encodeURIComponent(decoded);
        decoded = $("<div/>").html(flyer3).text();
        params += "&Flyer3rdLine=" + encodeURIComponent(decoded);
        params += "&StatusID=" + $get(sourcepage + "hiddenStatusID").value;

    }   
     
    window.open('../FunRaisers/FunRaiserPDF.aspx?FileName=FunRaiser.pdf' + params, '_blank')
}

// FAQ's
//$(document).ready(function() {
//    $('#funraiser-faq-link').colorbox({ width: '80%', inline: true, href: "#fun-faqs" });
//    $('p#faq-pane-links a').click(function(evt) {
//        evt.preventDefault();
//        var link = $(this).attr('href');
//        var thistop = $('#cboxLoadedContent #fun-faqs').offset().top;
//        var scrolltotop = $(link).offset().top;
//        $('#cboxLoadedContent #fun-faqs').animate({ scrollTop: (scrolltotop - thistop) }, 500);
//    });
//});
$(function() {
    var $click = null;
    var $colorbox = null;
    function addHandler() {
        if ($click) $click.unbind('click');
        $click = $('p#faq-pane-links a').click(function(evt) {
            evt.preventDefault();
            var link = $(this).attr('href');
            var thistop = $('#cboxLoadedContent #fun-faqs').offset().top;
            var scrolltotop = $(link).offset().top;
            $('#cboxLoadedContent #fun-faqs').animate({ scrollTop: (scrolltotop - thistop) }, 500);
        });
    }
    function addCB() {
        if ($colorbox) $colorbox.unbind('colorbox');
        $colorbox = $('#funraiser-faq-link').colorbox({ width: '80%', inline: true, href: "#fun-faqs" });
    }
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(addHandler);
    addHandler();
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(addCB);
    addCB();
});

$(document).ready(function()
{
    $('#ctl00_Body_IDFunRaiserAskQuestion').colorbox({ width: '50%', inline: true, href: "#fun-Question" });
 });   
    



        
//--------------------------------------------------
function ConfirmFunRaiserCancel(Title) 
{

    if (Title.length == 0)
        Title = $get('ctl00_Body_myFunRaiser_lblOrganizationName').innerHTML;
    var WinSettings = "center:yes;resizable:no;dialogHeight:170px;dialogWidth:610px;"
    // ALTER BELOW LINE - supply correct URL for Child Form
    
    var returnvalue = window.showModalDialog("../manage/ConfirmCancelFunRaiser.aspx", Title, WinSettings);
    if (returnvalue == '1')
        return true;
    
    return false;
}
//--------------------------------------------------
