﻿

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 getPlansFromLocation(val)
{

	var planQS = getParameterByName('plan');

    $.getJSON("/json.ashx?method=GetMembershipPlansByLocation&locationId=" + val,

	function(data) {
	    var arr = data.results;

		//document.getElementById(clientId + "ddlSelectOption").value = "";
		var controlId = "#"+ clientId +"ddlSelectOption";

		 $(controlId).html("")
                            .append("<option value=\"\"></option>");


		 $(data.results).each(function(i) {
							if(planQS != data.results[i].id)
                            $(controlId).append("<option value=\"" + data.results[i].id + "\">" + data.results[i].title + "</option>");
							else
							$(controlId).append("<option value=\"" + data.results[i].id + "\">" + data.results[i].title + "</option>");
						});

	});
}

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 getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


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();
    
});