$(document).ready(function(){
	// price controls
	$("#calc-price-slider-range").slider({
		range: false,
		min: 20,
		max: 150,
		step: 5,
		values: [100],
		slide: function(event, ui) {
			$("#calc-price").val(ui.values[0]);
			$("#calc-current-price").html('$' + ui.values[0]);

		},
		stop: function(event, ui) {
		  $('#calc-form-filter').submit();
		}
	});

	$("#calc-price").val($("#calc-price-slider-range").slider("values", 0));
	$("#calc-price-text").html('$' + $("#calc-price-slider-range").slider("values", 0));

	// minutes controls
	$("#calc-minutes-slider-range").slider({
		range: false,
		min: 100,
		max: 1500,
		values: [600],
		step: 50,
		slide: function(event, ui) {
			$("#calc-minutes").val(ui.values[0]);
			$("#calc-current-minutes").html(ui.values[0] + 'min');

		},
		stop: function(event, ui) {
		  $('#calc-form-filter').submit();
		}
	});

	$("#calc-minutes").val($("#calc-minutes-slider-range").slider("values", 0));
	$("#calc-minutes-text").html('$' + $("#calc-minutes-slider-range").slider("values", 0));

	$('.toggle').toggle(  
       function(){ // you can add as much here as you'd like
    	   var currentId = $(this).attr('id');
            $('#'+currentId+'-img').attr('src','/images/calculator/'+currentId+'-selected.png');
            $('#'+currentId+'-input').attr('value','1');


       }, function() { // same here
    	   var currentId = $(this).attr('id');  

            $('#'+currentId+'-img').attr('src','/images/calculator/'+currentId+'.png');
            $('#'+currentId+'-input').attr('value','0');

       });
	
	//Popup Calculator Results.
	$('#savings-calc-form').ajaxForm({
		success: function(data) {
			$('#lightbox-container').addClass( 'popup-savings-calculator' );
			$('#lightbox-container-content').html(data);
			$('#lightbox-container').jqmShow(); 
		}
	});
	$('.jqmClose').click(function(){
		$('#printcontent').hide();
	});
});

function tellFriend() {
	$('#savings-calculator-content').hide("normal");
	$('#tell-a-friend-content').show("normal");
}
function closeTellFriend(event) {
	$('#tell-a-friend-content').hide("normal");
	$('#savings-calculator-content').show("normal");
	if(event !== undefined) {
		event.preventDefault();
	}
}
function printThis() {
//USING FIX BELOW	
//	// PRINT FUNCTION DOESN'T INCLUDE POPUP SO APPEND POPUP CONTENT TO PARENT
//	var popupContent = $('#lightbox-container-content').html();
//	$('#content').append('<div id="printcontent">'+popupContent+'</div>');
//	window.print();
	var popupContent = $('#lightbox-container-content').html();
	
	$('.jqmClose').trigger("click");

	printablePopupWindow(popupContent);

}

//popup
function printablePopupWindow(popupContent)
{
	var myWin = window.open("", "","toolbar=0,menubar=0,location=0,resizable=0,status=0,scrollbars=0,width=750,height=480");
	
	var screenW = 640, screenH = 480;
	if (parseInt(navigator.appVersion)>3) {
	 screenW = screen.width;
	 screenH = screen.height;
	}
	else if (navigator.appName == "Netscape" 
	    && parseInt(navigator.appVersion)==3
	    && navigator.javaEnabled()
	   ) 
	{
	 var jToolkit = java.awt.Toolkit.getDefaultToolkit();
	 var jScreenSize = jToolkit.getScreenSize();
	 screenW = jScreenSize.width;
	 screenH = jScreenSize.height;
	}
	myWin.moveTo((screenW / 2) - (750)/2,(screenH / 2) - (480)/2);
	myWin.document.write("<html><head>" +
	"<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/css/layout.css\" />" +
	"<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/css/typography.css\" />" +
	"<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/css/utility.css\" />" +
    "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/css/popup.css\" />" +
    "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"/css/buttons.css\" />" +
    "<style>#bottom-links{display:none;}#lightbox-container-content{padding: 10px;}</style>" +
    "<title>cricket savings calculator</title>" + 
	"</head>" +
	"<body>");
	myWin.document.write("<img src=\"images/print/logo-print.png\"/><hr/>");
	myWin.document.write(popupContent);
	//myWin.document.write ('<div><a href="#" onclick="javascript:window.close();">Close this window</a></div>');	
	myWin.document.write("</body></html>");
	myWin.document.title = "Your Savings";
	
	if(navigator.appName == "Netscape")
	{
		myWin.print();
	}else
	{
		//ie
		myWin.document.execCommand('print', false, null);
	}
}

/////////////////////////////////
var submitTellafriendBlock = false;
function submitTellafriend(url, formobj) {
	var frmElements = $('input');
	$.each( frmElements, function(n, item) {
		if ($(item).attr('rdi:required') == 'true') {
			if($(item).val() == '') {
				$('#'+item.name + '-err').show();
				if ($('#'+item.name + '-errformat')) {
					$('#'+item.name + '-errformat').show();
				}
			} else {
				$('#'+item.name + '-err').hide();
			}
		}
	});
	if (validateTellafriend()) {
		if (!submitTellafriendBlock) {
			submitTellafriendBlock = true;
			
			$.ajax({
				type: "POST",
				url: url,
				data: formobj.serialize(),
				success: function(response) {
					closeTellFriend();
					$('#savings-calculator-content').append(response);
				    submitTellafriendBlock = false;
				}
			});
		}
	}
}

function validateTellafriend() {
	var isValid = true;
	var frmElements = $("#hm-savings-tellafriend input");
	frmElements.each(
		function(item) {
			if ($(item).attr('rdi:required') == 'true' && (item.val() == '')) {
				$(item.name + '-err').show();
				isValid = false;
			}
		}
	);
	
	if (isValid) {
		//Validate emails
		var email = $("#email").val();
		if (!$.trim(email).match(/^[\w-_]+(\.[\w-_]+)*@([\w-_]+\.)+[a-zA-Z]{2,7}$/)) {
			isValid = false;
			$('#email-errformat').show();
		} else {
			$('#email-errformat').hide();
		}
		
		var femailv = $("#femail").val();
		var arrFemail = femailv.split(',');
		$.each( arrFemail,
			function(n, item) {
				if (!$.trim(item).match(/^[\w-_]+(\.[\w-_]+)*@([\w-_]+\.)+[a-zA-Z]{2,7}$/)) {
					isValid = false;
					$('#femail-errformat').show();
				} else {
					$('#femail-errformat').hide();
				}
			}
		);
	}
	
	return isValid;
}

function showPostalCode(chkbox) {
	if(chkbox.checked == true) {
		$('#tell_friend_zip').show();
		$('#zipcode').show();
		$('#zipcode').attr("rdi:required", "true");
	} else {
		$('#tell_friend_zip').hide();
		$('#zipcode').hide();
		$('#zipcode').attr("rdi:required", "false");
		$('#zipcode-err').hide();
	}
}