﻿

function getOptionsFromLocation(val)
{

    $.getJSON("/json.ashx?method=GetMembershipOptionsByLocation&locationId=" + val,

	function(data) {
	    var arr = data.results;
	    $("#RequiredOffers").html("");
		document.getElementById(clientId + "txtDiscountCode").value = "";
		document.getElementById(clientId + "txtManualDiscountCode").value = "";
		
	    $(data.results).each(function(i, val) {
	        var dc = document.getElementById(clientId + "txtDiscountCode").value;
	        var chked = ' ';
	        if (dc == this.eClubOfferId) chked = ' checked="true" ';
	        var str = '<div class="optional offer">';
	        str += '<input onclick="ieFix()" type="radio" id="rblSampleMemberOffer' + i + '" name="rblSampleMemberOffers" value="' + this.eClubOfferId + '"' + chked + ' />';
	        str += '<label for="rblSampleMemberOffer' + i + '">' + this.title + '</label>';
	        str += '<div class="comment">' + this.description + '</div>';
	        str += '</div>';
	        $("#RequiredOffers").append(str);
	        $("#rblSampleMemberOffer" + i).change(function() {
	            // $("#"+ clientId +"txtDiscountCode").get(0).value = $(this).val();
	            // $("#ctl16_ctl00_ctl00_txtDiscountCode").get(0).value = $(this).val();
	            document.getElementById(clientId + "txtDiscountCode").value = $(this).val();
				document.getElementById(clientId + "txtManualDiscountCode").value = "";				
	        });


	    });

	    // document.openResultsPanel();
	});

}

function getOptionsFromLocation(val,plan,payment)
{

    $.getJSON("/json.ashx?method=GetMembershipOptionsByLocation&locationId=" + val+"&Payment="+payment+"&Plan="+plan,

	function(data) {
	    var arr = data.results;
	    $("#RequiredOffers").html("");
		document.getElementById(clientId + "txtDiscountCode").value = "";
		document.getElementById(clientId + "txtManualDiscountCode").value = "";

	    $(data.results).each(function(i, val) {
	        var dc = document.getElementById(clientId + "txtDiscountCode").value;
	        var chked = ' ';
	        if (dc == this.eClubOfferId) chked = ' checked="true" ';
	        var str = '<div class="optional offer">';
	        str += '<input onclick="ieFix()" type="radio" id="rblSampleMemberOffer' + i + '" name="rblSampleMemberOffers" value="' + this.eClubOfferId + '"' + chked + ' />';
	        str += '<label for="rblSampleMemberOffer' + i + '">' + this.title + '</label>';
	        str += '<div class="comment">' + this.description + '</div>';
	        str += '</div>';
	        $("#RequiredOffers").append(str);
	        $("#rblSampleMemberOffer" + i).change(function() {
	            // $("#"+ clientId +"txtDiscountCode").get(0).value = $(this).val();
	            // $("#ctl16_ctl00_ctl00_txtDiscountCode").get(0).value = $(this).val();
	            document.getElementById(clientId + "txtDiscountCode").value = $(this).val();
				document.getElementById(clientId + "txtManualDiscountCode").value = "";
	        });

	    });

	    // document.openResultsPanel();
	});

}

function ieFix() {

    // forms[0].elements['text'+num]
    // $("#ctl19_ctl01_ctl01_txtDiscountCode").focus();

        setTimeout(function()
        {    
			document.getElementById(clientId + "txtManualDiscountCode").value = "";
          //  document.getElementById(clientId +"txtDiscountCode").focus();
            // $("#"+ clientId +"txtDiscountCode").get(0).focus();
            // document.getElementById('ctl16_ctl00_ctl00_txtDiscountCode').focus();
            clearTimeout();
        }, 100);
}

function setupHandlers()
{
    $("#"+ clientId +"ddlLocationOfInterest").change( function() {
        getOptionsFromLocation($(this).val());
    });
}


$(document).ready(function() {
    
    setupHandlers();
    
});