﻿//Code that runs when the SportsLite.master page loads
$(window).load(function() {
	markRequiredFields();

	//debugger;
	if ($("#divASANo").length > 0) {

		if ($(".optASA").length) {
			$(".optASA input:checked").each(function() {
				var txtID = $("#divASANo input[type=text]").attr("id").replace("_txtNumber", "");
				setASADisplay(($(this).attr("value") == "Yes"), txtID);
			});
		}
	}

	if ($("#divCCNo").length > 0) {
		if ($(".optCCNo").length) {
			$(".optCCNo input:checked").each(function() {
				var txtID = $("#divCCNo input[type=text]").attr("id").replace("_txtField", "");
				setCCNoDisplay(($(this).attr("value") == "Yes"), txtID);
			});
		}
	}

	if (typeof (calculateTotal) != "undefined") {
		calculateTotal();
	}	
});

function getExtAddFee() {
	var addFee = 0;
	//Get total additonal fees for all external validators
	$("#extAddFee input[type=text]").each(function() {
		addFee += parseFloat($(this)[0].value);
	});
	return addFee;
}


function getASAAddFee() {
	return asaFee;
}

var asaFee = 0;
function setASAFee(amount) {
	asaFee = amount;
	calculateTotal();
}

function getCCAddFee() {
	return ccFee;
}

var ccFee = 0;
function setCCFee(amount) {
	ccFee = amount;
	calculateTotal();
}

function setASADisplay(isYesSelected, txtID) {
	if (isYesSelected) {
		setASAFee(0);
		$("#divASANo").slideDown('fast');
		ValidatorEnable($("#" + txtID + "_rfvNumber")[0], true);
	}
	else {		
		setASAFee(parseFloat($("#extASAFee input[type=text]").attr("value")));
		$("#divASANo").slideUp('fast', function() { $("#" + txtID + "_txtNumber")[0].value = ''; });
		ValidatorEnable($("#" + txtID + "_rfvNumber")[0], false);
	}
}

function setCCNoDisplay(isYesSelected, txtID) {	
	if (isYesSelected) {		
		$("#divCCNo").slideDown('fast');
		ValidatorEnable($("#" + txtID + "_rfvField")[0], true);
		//$("#divCCOrder").slideUp('fast');
		//$("#divCCOrder input[type=checkbox]").each(function() { $(this)[0].checked = false; });
	}
	else {		
		$("#divCCNo").slideUp('fast', function() {
			$("#" + txtID + "_txtField")[0].value = '';
			//$("#divCCOrder").slideDown('fast');
		});
		ValidatorEnable($("#" + txtID + "_rfvField")[0], false);
		$(".ccStatus").text("");
	}
}

//Marks all required fields with a red asterix
function markRequiredFields() {
	if (typeof (Page_Validators) != "undefined") {
		var validators = Page_Validators;
		var i = 0;
		for (i = 0; i < validators.length; i++) {
			if (validators[i].enabled == null && validators[i].id.indexOf("rfv") > 0) {
				if ($(validators[i]).prev().html() != "*") {
					$("<span style='color:red'>*</span>").insertBefore($(validators[i]));
				}
			}
		}
	}
}


function showHelpTip(targetControlId, helpDivId) {
	var distance = 10;
	var time = 250;
	var hideDelay = 500;

	var trigger = $('#' + targetControlId);
	var hideDelayTimer = null;
	// tracker	
	var beingShown = false;
	var shown = false;
	var popup = $('div#' + helpDivId + ' ul');
	$([trigger.get(0), popup.get(0)]).mouseover(function() {
		// stops the hide event if we move from the trigger to the popup element
		trigger.css({ "cursor": "pointer" });

		if (hideDelayTimer) clearTimeout(hideDelayTimer);

		// don't trigger the animation again if we're being shown, or already visible
		if (beingShown || shown) {
			return;
		}
		else {
			beingShown = true;
			popup.css({
				bottom: -60,
				left: 50,
				display: 'block' // brings the popup back in to view				
			})

			// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
		}
	})
				.mouseout(function() {
					// reset the timer if we get fired again - avoids double animations
					if (hideDelayTimer) clearTimeout(hideDelayTimer);

					// store the timer so that it can be cleared in the mouseover if required
					hideDelayTimer = setTimeout(function() {
						hideDelayTimer = null;
						popup.animate({
							bottom: '-=' + distance + 'px',
							opacity: 0
						}, time, 'swing', function() {
							// once the animate is complete, set the tracker variables
							shown = false;
							// hide the popup entirely after the effect (opacity alone doesn't do the job)
							popup.css('display', 'none');
						});
					}, hideDelay);
				});
}


function initializeLightBox(target, lightBoxId) {
	//Display the enlarged image when the thumbnail image is clicked
	$("#" + target).click(function(e) {
		//Cancel the link behavior
		e.preventDefault();

		showLightBox(lightBoxId);
	});

	//if close button is clicked
	$('#' + lightBoxId + ' #lightBox .window #close').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#' + lightBoxId + ' #lightBox #mask, .window').fadeOut(300);
	});

	//if mask is clicked
	$('#' + lightBoxId + ' #lightBox #mask').click(function() {
		$('#' + lightBoxId + ' #lightBox #mask, .window').fadeOut(300);
	});

	//If Esc is pressed (ie keyCode = 27)
	$(document).keyup(function(e) {
		if (e.keyCode == 13 || e.keyCode == 27) {
			$('#' + lightBoxId + ' #lightBox #mask, .window').fadeOut(300);
		}
	});
}

function close() {
	$('#lightBox #mask, .window').fadeOut(300);
}

function showLightBox(lightBoxId) {
	var id = $('#' + lightBoxId + ' #lightBox #dialog');

	//Get the screen height and width   
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	var top = $(document)[0].body.scrollTop;

	//transition effect
	$('#' + lightBoxId + ' #lightBox #mask').css({ "width": maskWidth, "height": maskHeight, "top": top, "left": 0, "opacity": 0.8 }).fadeIn(300);

	//Get the window height and width   
	var winH = $(window).height();
	var winW = $(window).width();

	//Set the popup window to center   
	$(id).css('top', winH / 2 - $(id).height() / 2);
	$(id).css('left', winW / 2 - $(id).width() / 2);

	$(id).fadeIn(200);	
}

window.status = "Mr Price Sport";
