$(document).ready(function () {

	$('.lbOn').click( function(event) {
		event.preventDefault();
		var url = this.href;
		$.ajax({
			type: "POST",
			url: url,
			data: {'detailtype':'paygo'},
			success: function(data) {
				$('#lightbox-container').addClass('popup-paygo-phone-detail');
				$('#lightbox-container-content').html(data);
				$('#lightbox-container').jqmShow();
			}
		});
	});

	$('.lbOnPlans').click( function(event) {
		event.preventDefault();
		var url = this.href;
		$.ajax({
			type: "POST",
			url: url,
			data: {'detailtype':'paygo'},
			success: function(data) {
				$('#lightbox-container').addClass('popup-paygo-plans-detail');
				$('#lightbox-container-content').html(data);
				$('#lightbox-container').jqmShow();
			}
		});
	});

	$('#helplink').click(
		function(event) {
			event.preventDefault();
			$('#lightbox-container').addClass('paygoPopup');
			$('#lightbox-container-content').html( $('#helpmechoose').html() );
			$('#lightbox-container').jqmShow();
		}
	);

	$('.paygo-plan-select').click(
		function(event) {
			event.preventDefault();
			var ratecode = $(this).attr('id').split('-')[1];
			$('#cbx-'+ratecode).attr('checked', true);
		}
	);

	$('#paygo-plan-form').ajaxForm(
			{
				dataType: 'json',
				beforeSubmit: function(formData, jqForm, options) {
					if (ajaxBlocked($(jqForm).find('button'))) return false;
				},
				success: function(data) {
					if (data.success) {
						document.location = data.nextstep;
					} else {
						ajaxUnblock();
						$('#lightbox-container-content').html(data.message);
						$('#lightbox-container').addClass( 'popup-package-conflict' );
						$('#lightbox-container').jqmShow();
					}
				}

			}
	);

	$('.cancel-paygo-activation').click(

			function(event) {
				var clicked = this;
				event.preventDefault();
				$.ajax(
					{
						type: 'POST',
						url: clicked.href,
						success: function(content) {
							$('#lightbox-container-content').html(content);
							$('.cancel-paygo-activation-confirmed').click(
							function(event) {
								var clicked = this;
								event.preventDefault();

								$.ajax(
								{
									dataType: 'json',
									data: { confirmed: true },
									type: 'POST',
									url: clicked.href,
									success: function (data) {
										document.location = data.redirect;
									},
									error: function(request, status, errorThrown) {
										alert(status + ' ' + clicked.href);
									}
								});
							}
						);
						$('#lightbox-container').jqmShow();
					}
				});
			}
	);

	$('.paygo-activation-allowed').click(
			function(event) {
				var clicked = this;
				event.preventDefault();
				$.ajax(
				{
					type: 'POST',
					url: paygoActivationCheckService,
					dataType: 'json',
					success: function (data) {
						if (data.isallowed) {
							document.location = clicked.href;
						} else {
							$('#lightbox-container-content').html(data.message);
							$('#lightbox-container').jqmShow();
						}
					},
					error: function(request, status, errorThrown) {
						alert(status + ' ' + clicked.href);
					}
				});
			}
	);


	//print-confirmation

	$('.print-confirmation').click(function(e){

		if (window.print) {
			window.print() ;
		} else {
			var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
			document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
			WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
		}
	});


	//interstitial
	$('.paygo-data-popup-trigger').click(
			function(event) {
				event.preventDefault();
				$('#lightbox-container').addClass('paygodataPopup');
				$('#lightbox-container-content').html( $('.interstitial-popup').html() );
				$('#lightbox-container').jqmShow();
			}
		);

	//what is pay as you go data
	$('#paygo_hint').click(
			function(event) {
				event.preventDefault();
				var c = '<div id="pay_as_you_go_data_popup_content"><h3>Pay As You Go Data</h3>With Pay As You Go Data status set to \'on\', when you exceed your data allowance, more data will automatically be purchased using funds from your account balance, so you will never need to be offline.</div>';

				$('#lightbox-container').addClass('paygodataPopup');
				$('#lightbox-container-content').html( c );
				$('#lightbox-container').jqmShow();
			}
		);


	/*
	This script is written by Eric (Webcrawl@usa.net)
	For full source code, installation instructions,
	100's more DHTML scripts, and Terms Of
	Use, visit dynamicdrive.com
	*/

//	function printit(){
//
//		if (window.print) {
//		    window.print() ;
//		} else {
//		    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
//		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
//		    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
//		}
//	}


	//////// DETAILS POPUP ////////
	/*$('a.paygo-details').click(function(event) {
		var phonedetailspopup = $(this).attr('href');
		var clicked = this;
		event.preventDefault();
		$.ajax(
		{
			type: 'POST',
			url: phonedetailspopup,
			//dataType: 'json',
			success: function (data) {
				if (data.isallowed) {
					document.location = clicked.href;
				} else {
					$('#lightbox-container-content').html(data.message);
					$('#lightbox-container').jqmShow();
				}
			},
			error: function(request, status, errorThrown) {
				alert(status + ' ' + clicked.href);
			}
		});
	});*/

});

///////////////////////// FORM VALIDATION /////////////////////////
function validateMyInfoValidation(formname,button) {
	var form = $(formname);
	var numfields = form.length;
	var rqd = "<h3>Required Fields:</h3><p>";
	var req = true;
	for(var i=0;i<numfields;i++) {
		if((form[i].value == "")||(form[i].value.substr(0,6) == "select")) {
			// PUT ERROR MESSAGING HERE
			rqd += form[i].name+"<br />";
			req = false;
		}
		if(form[i].name == "email") {
			// EMAIL ERROR MESSAGING
			if(IsEmailValid(form.email) == false) {
				rqd += "<br />Your email address is not valid.";
				req = false;
			}
		}
	}
	if(req == false) {
		rqd += "</p>";
		// CALL POPUP
		showErrorMessage(button,rqd);
		return false;
	}
	return true;
}
///////////////////////// ERROR MESSAGE POPUP /////////////////////////
function showErrorMessage(relativetoid,message) {
	$('#popupcontent').update(message);
	popup('popup',relativetoid);
}
///////////////////////// FORM EMAIL VALIDATION /////////////////////////
function IsEmailValid(FormName) {
	var EmailOk  = true;
	var Temp     = FormName;
	var AtSym    = Temp.value.indexOf('@');
	var Period   = Temp.value.lastIndexOf('.');
	var Space    = Temp.value.indexOf(' ');
	var Length   = Temp.value.length - 1;   // Array is from 0 to length-1

    if 	((AtSym < 1) ||  	       		   // '@' cannot be in first position
    	(Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    	(Period == Length ) ||             // Must be atleast one valid char after '.'
    	(Space  != -1))                    // No empty spaces permitted
	{
		EmailOk = false;
		Temp.focus();
	}
	return EmailOk;
}



