	// This function for any code to be executed on page ready.
	$(function() {
		$("#mainContent").show();
		$("#sendContent").hide();
		$("#replyContent").hide();
		$("#unwelContent").hide();

		$('.mainButton').hover(
			function(){
				$(this).css("background-position","-200px 0");
			    },
			function(){
				$(this).css("background-position","0 0");
			}
		);

		$('#Happy').click(
			function() {
				changeContent("main");
			}
		);

		$('.mainButton').click(
			function() {
				changeContent($(this).attr("id"));
			}
		);

		$('.continue').hover(
			function(){
				$(this).css("background-position","-125px 0");
			    },
			function(){
				$(this).css("background-position","0 0");
			}
		);

		$('.continue').click(
			function() {
				window.location = $(this).attr("id")+".html";
			}
		);

		$('.order').hover(
			function(){
				$(this).css("background-position","-125px 0");
			    },
			function(){
				$(this).css("background-position","0 0");
			}
		);

/*
        // This controls the from mobile number field.
        $('#toNum').keypress(function(e) {
            if(e.which == 8 || e.which == 13)
                return true;
            if(e.which < 48 || e.which > 57)
                return false;
            if($(this).val().length > 10)
				return false;
        });

        $('#toNum').keyup(function(e) {
            if($(this).val().length == 11)
            {
                // Validate the number now!
                var tmpStr = $(this).val().substring(0,2);
                if(tmpStr == "07" || tmpStr == "01")
                    $(this).css("color","white");
				else
                    return true;

                $("#messageText").attr("disabled", "").focus();
                return false;
            }
            else
                $(this).css("color","red");
        });
*/
/*
        // This controls the from mobile number field.
        $('#fromNum').keypress(function(e) {
            if(e.which == 8 || e.which == 13)
                return true;
            if(e.which < 48 || e.which > 57)
                return false;
            if($(this).val().length > 10)
				return false;
        });

        $('#fromNum').keyup(function(e) {
            if($(this).val().length == 11)
            {
                // Validate the number now!
                var tmpStr = $(this).val().substring(0,2);
                if(tmpStr == "07" || tmpStr == "01")
                    $(this).css("color","black");
				else
                    return true;

                return false;
            }
            else
                $(this).css("color","red");
        });
*/
        // This controls the from mobile number field.
        $('#txtRef').keypress(function(e) {

		    if(e.which == 8 || e.which == 13)
                return true;

			if($(this).val().length > 6)
				return false;

			// Allow 0-9, a-z and A-Z through.
			if(e.which > 47 || e.which < 58)
                return true;
            if(e.which > 64 && e.which < 91)
                return true;
            if(e.which > 96 && e.which < 123)
                return true;

			return false;
        });

        $('#txtRef').keyup(function(e) {
            if($(this).val().length == 7)
            {
                // Validate the number now!
                $(this).css("color","black");

                return false;
            }
            else
                $(this).css("color","red");
        });

	    // This controls the from mobile number field.
        $('#senderID').keypress(function(e) {

		    if(e.which == 8 || e.which == 13)
                return true;

			if($(this).val().length > 10)
				return false;

			if(e.which == 32)
                return true;

            if(e.which > 64 && e.which < 91)
                return true;
            if(e.which > 96 && e.which < 123)
                return true;

			return false;
        });

        $('#fromNum').keyup(function(e) {
            if($(this).val().length == 11)
                 return true;
            else
                 return false;
        });

        $("#orderTxt").click(function() {

				var tmpStr = "";

				if($("#toNum").val() == "")
					tmpStr += "Your Valentine must have a number!\n\r";

				if($("#messageText").val() == "")
					tmpStr += "What no message! Don't be harsh.";

				if(tmpStr.length > 0)
				{
					alert("Errors detected :\n\r\n\r"+tmpStr);
					return false;
				}


                $.blockUI('<h4><img src="images/sm.gif" /><br /><br/>Processing texts..</h4>');
                $.post("backend.php", {
                        toNum: $("#toNum").val(),
                        fromNum: $("#fromNum").val(),
                        senderID: $("#senderID").val(),
                        txtRef: $("#txtRef").val(),
                        message: $("#messageText").val(),
                        sendTime: $("#countryFlag").text(),
                        country: defaultFlag,
                        hourDiff: timeDiff
                }, function(xml) {
						$.unblockUI();
                        checkResult(xml);
                });
        });

        $("#reportTxt").click(function() {

				var tmpStr = "";

				if($("#fromNum").val() == "")
					tmpStr += "You must supply your mobile phone number!\n\r";

				if($("#txtRef").val() == "")
					tmpStr += "A txt reference number is required!\n\r";

				if($("#messageText").val() == "")
					tmpStr += "You must supply a reason.";

				if(tmpStr.length > 0)
				{
					alert("Errors detected :\n\r\n\r"+tmpStr);
					return false;
				}

                $.blockUI('<h4><img src="images/sm.gif" /><br /><br/>Processing report..</h4>');
                $.post("report.php", {
                        fromNum: $("#fromNum").val(),
                        txtRef: $("#txtRef").val(),
                        message: $("#messageText").val()
                }, function(xml) {
						$.unblockUI();
                        checkResult(xml);
                });
        });

	});

	function checkResult(xml)
	{
        if($("status", xml).text() == "0")
        {
            $.blockUI('<h4><img src="images/sm.gif"><br><br>Transferring to PayPal..</h4>');
            tmpStr = "paypal.php?tonum="+$("mobNum", xml).text()+"&transid="+$("transID", xml).text();
            document.location = tmpStr;
        }
        else
        {
			switch($("status", xml).text())
			{
				case "1" :	tmpStr = "No matching txt reference number and mobile number\r\n";
							tmpStr+= "in our database. Please check and re-submit!";
							break;
				case "2" :	tmpStr = "Unable to store your details, please try again.";
							break;
				case "3" :	tmpStr = "Thank-you for your report.";
							break;
				default : 	tmpStr = "Unknown fault occurred.";
						break;
			}
			alert(tmpStr);
			
		}
	}

	function changeContent(Section)
	{
		var tmpStr = "";

		if( ($("#sendContent").css("display") == "block") && (Section != "send") )
			tmpStr = "sendContent";
		else if( ($("#replyContent").css("display") == "block") && (Section != "reply") )
			tmpStr = "replyContent";
		else if( ($("#unwelContent").css("display") == "block") && (Section != "unwel") )
			tmpStr = "unwelContent";
		else if( ($("#mainContent").css("display") == "block") && (Section != "main") )
			tmpStr = "mainContent";

		if(tmpStr != "")
		{
			$("#"+tmpStr).fadeOut("fast", function()
				{$("#"+Section+"Content").fadeIn("fast");}
			);
		}
	}

	function textCounter()
	{
        var MaxLimit = 250;
        var tmpStr = $("#messageText").val();

        // Make sure no more than 140 characters are allowed.
        if(tmpStr.length > MaxLimit)
            $("#messageText").val(tmpStr.substring(0, (MaxLimit)));

        // Update the number of characters left display.
        $("#charText").text(MaxLimit - $("#messageText").val().length);
	}

	function modalOpen (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.show(200, function () {
				dialog.data.hide().fadeIn(200, function () {

					$("#roses").click(function (e) {
						e.preventDefault();

						$.get("./rosesarered.html", function(data){
							$("#title").text("Roses are red...");
							$("#subContent").html(data);

							$('input:button').click(function (e) {
								var tmpStr = $("#messageText").val();

								if(tmpStr.length > 0)
									tmpStr += "\n\r";

								tmpStr += $(this).parent().text();
								$("#messageText").val($.trim(tmpStr));

								textCounter();
							});

						});
					});

					$("#flirt").click(function (e) {
						e.preventDefault();

						$.get("./flirt.html", function(data){
							$("#title").text("Flirt and chatup lines...");
							$("#subContent").html(data);

							$('input:button').click(function (e) {
								var tmpStr = $("#messageText").val();

								if(tmpStr.length > 0)
									tmpStr += "\n\r";

								tmpStr += $(this).parent().text();
								$("#messageText").val($.trim(tmpStr));

								textCounter();
							});

						});
					});

					$("#roses18").click(function (e) {
						e.preventDefault();

						$.get("./rosesarered18.html", function(data){
							$("#title").text("Roses are red... 18 Rated");
							$("#subContent").html(data);

							$('input:button').click(function (e) {
								var tmpStr = $("#messageText").val();

								if(tmpStr.length > 0)
									tmpStr += "\n\r";

								tmpStr += $(this).parent().text();
								$("#messageText").val($.trim(tmpStr));

								textCounter();
							});
						});
					});

					$("#quotes").click(function (e) {
						e.preventDefault();

						$.get("./quotes.html", function(data){
							$("#title").text("Quotes of Love");
							$("#subContent").html(data);

							$('input:button').click(function (e) {
								var tmpStr = $("#messageText").val();

								if(tmpStr.length > 0)
									tmpStr += "\n\r";

								tmpStr += $(this).parent().text();
								$("#messageText").val($.trim(tmpStr));

								textCounter();
							});
						});
					});

					$("#love").click(function (e) {
						e.preventDefault();

						$.get("./love.html", function(data){
							$("#title").text("Quotes of Love");
							$("#subContent").html(data);

							$('input:button').click(function (e) {
								var tmpStr = $("#messageText").val();

								if(tmpStr.length > 0)
									tmpStr += "\n\r";

								tmpStr += $(this).parent().text();
								$("#messageText").val($.trim(tmpStr));

								textCounter();
							});
						});
					});

				});
			});
		});
	}

	function modalClose (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.hide(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		});
	}

