/**
 * This file contains the 'getAvailableTimes' function which is one of the main functions to getting the times that are available for the given
 * DirectBuy Center. This function can be called various times depending on if you click the 'more dates' button.
 * @author free beachler <fbeachler@gmail.com>
 */ 

var datesArray = [];
var blnPaginationOn = false;
var requestedCountMax = 3;
var wsHelperRegionCode;
var firstName;
var appointmentId;
var postcode;
var spouseName;
var leadSubmitDate;

/**
 * get our avaliable times from the AJAX call & send out to browser
 * @param blnIncrementLimit (tells us to increment our current count)
 * @return void
 */
function getAvailableTimes(blnIncrementLimit,blnViewPrevious) {
	if (wsHelperRegionCode) {
		//show spinner
		$('#col_1_spinner').show();
		url = "/appt_setter/getAvailableTimes.php?regionCode=" + wsHelperRegionCode;
		// Look up our Available times and populate them into a JSON Variable
		$.get(url, function(transport) {
			datesArray = eval(transport);
			//alert(transport);
			if (blnIncrementLimit) {
				for (i=1; i<=3; i++) {
					if (requestedCountMax < datesArray.length) {
						requestedCountMax++;
					} else {
						$('#col_1_spinner').hide();
						break;
					}
				}
			} else if (blnViewPrevious) {
				requestedCountMax-=15;
			}
			var availableDateCount = 1;
			var availableDatesText = '';

			//SMELLY HACK TO DEAL WITH THIS ALGORITHM
			if (requestedCountMax == datesArray.length) {
				//hide spinner
				$('#col_1_spinner').hide();
			}
			
			//we can only display fifteen rows of data in our first Column. Adjust as neccessary
			var fifteenRowWindow = requestedCountMax;
			for(i=0; i<datesArray.length; i++) {
				while(fifteenRowWindow > 15) {
					i+=15;
					fifteenRowWindow-=i;
					blnPaginationOn = true;
				}

				if(blnViewPrevious) {
					fifteenRowWindow = 15;
					requestedCountMax = 15;
				}
//				if(availableDateCount <= requestedCountMax) {
				if(availableDateCount <= fifteenRowWindow) {
					availableDatesText += ("<a id='date_" + availableDateCount + "' style='padding-right: 10px; display: block; cursor: pointer;' onclick='setSelectedDate(this.id,\"" + datesArray[i].Date + "\"); return false;'>" + datesArray[i].DateDisplay + "</a>\n");
//					console.log(requestedCountMax+":"+fifteenRowWindow+":"+availableDateCount+":"+blnPaginationOn);
					availableDateCount++;
				}
			}
			//show 'end of results' message if we've got space
			if((datesArray.length == requestedCountMax) && (availableDateCount > 16)){
				availableDatesText += ("<a onclick='selectPrevDates(); return false;' span style='display:block; cursor:pointer; padding-right:0.5em; color:#999'>All Dates Shown<br />(See Previous)</a>\n");
			}
			if(blnPaginationOn) {
				$('#select_more_dates').css ( "top", '-1000px' );
				$('#select_prev_dates_small').css ( "top", '380px' );
				$('#select_more_dates_small').css ( "top", '380px' );
			}
			//hide spinner
			$('#col_1_spinner').hide();
			//populate the dates with the dynamic results
			$('#appointmentDynamicDates').html ( availableDatesText );
		});
	} else {
		$('#col_1_spinner').hide();
	}
}
