﻿function dropDown(answer , question){
    var x = document.getElementById(answer);
    var y = document.getElementById(question);

    if(x.style.display == "none"){
        x.style.display ="";
        y.className = "selected";
    }else{
    x.style.display="none";
    y.className=""
    }
}

var isStateOpen = false;
var redirectURL = "http://nj.greenmarketfundraising.com/"
//var redirectURL = "http://njgmf.tecture.net/"

$(document).ready(function(){
    $("div.products:first-child").addClass('last');

    //Checks to see if a new cookies needs to be set for the state value
    $('.ChooseStatePopUp').jqm();
    var stateAbbr = getCookie('stateCookie');
    if(stateAbbr == "")
    {
        isStateOpen = true;
        $('.ChooseStatePopUp').jqmShow();
    }else if(stateAbbr.toLowerCase() == "nj"){
        window.location = redirectURL;
    }
    //This is for the dropdown list where you choose your state
    
    $('.chooseStateBtn').click(function(){
        //alert("changed: " + $(this).val());
        if($('.chooseStateDDL').val() != ""){
            var d = new Date();
            $.ajax({
                type: "POST",
                data: "stateAbbr=" + $('.chooseStateDDL').val() + "&time=" + d.getDate(),
                url: "ajax/SelectAState.aspx",
                cache: false,
                success: function(data){
                    if($('.chooseStateDDL').val().toLowerCase() == "nj"){
                        window.location = redirectURL;
                    }else{
                        $('.ChooseStatePopUp').jqmHide();
                        isStateOpen = false;
                    }
                }
            });
        }
    });
    
    //This is to check and apply a code on checkout
    $('.applyOrgCode').click(function(){
        applyCode($('.orgCodeTB').val());
    });
    
    //This is organization search functionality
    $('.orgSearchPopUp').jqm();
    $('.openOrgSearch').click(function(){
        $('.orgSearchPopUp').jqmShow();
    });
    $('.tbOrgNameSearch').keypress(function(e){
        //alert(e.which);
        if((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)){
            searchOrganizations();
            return false;
        }
    });
    $('.OrgSearch').click(function(){
        searchOrganizations();
    });
    $('.searchAgain').click(function(){
        $('.showOrgBanner').hide();
        $('.searchOrgBanner').show();
        applyCode("");
    });
});

function searchOrganizations(){
    if($('.tbOrgNameSearch').val() != ""){
        var d = new Date();
        $.ajax({
            type: "GET",
            data: "orgKeyword=" + $('.tbOrgNameSearch').val() + "&time=" + d.getDate(),
            url: "ajax/SearchOrganizations.aspx",
            cache: false,
            success: function(data){
                //alert(data);
                $('.OrgSearchResults').html(data);
            }
        });
    }else{
        $('.OrgSearchResults').html("Please enter a search term to continue.");
    }
}

function applyCode(code){
    //alert("applying");
    var d = new Date();
    $('.orgCodeTB').val(code);
    $.ajax({
        type: "GET",
        data: "orgCode=" + code + "&time=" + d.getDate(),
        url: "ajax/ApplyACode.aspx",
        cache: false,
        success: function(data){
            if(data != "None"){
                $('.showOrgBanner').show();
                $('.searchOrgBanner').hide();
            }
            $('.orgName').html(data);
            getParticipantIDs();
        }
    });
}
function getParticipantIDs(){
    var d = new Date();
    var items = "";
    items += "<option value=\"\">please select</option>"
    $.ajax({
        type: "GET",
        data: "time=" + d.getDate(),
        url: "ajax/GetParticipantId.aspx",
        cache: false,
        success: function(data) {
            if (data.length > 0) {
                var Ids = data.split('|');
                for (var i = 0; i < Ids.length - 1; i++) {
                    items += "<option value=\"" + Ids[i] + "\">" + Ids[i] + "</option>";
                }
                $('.participantIDSection').show();
            } else {
            items += "<option value=\"0\" selected=\"selected\">0</option>";
                $('.participantIDSection').hide();
            }
            $('.ddlParticipant').removeAttr("disabled");
            $('.ddlParticipant').html(items);
        }
    });
}
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}
    
function inputClear(textField){
    var myField = document.getElementByName(textField);
    
    if(myField.value.length > 1){
        myField.value=""
    }
}

function switchimg(towhat,theimg){
    var oDiv = document.getElementById(theimg);
    oDiv.src = towhat;
}