// on load
$(function() {
	var dialog_subscribe_height = 294;

	if( $.browser.msie )
	{
		dialog_subscribe_height = 618;
	}

	$("#dialog_subscribe").dialog({
		show: 'fade',
		hide: 'fade',
		autoOpen: false,
		draggable: false,
		modal: true,
		resizable: false,
		closeOnEscape: true,
		width: 474,
		height: dialog_subscribe_height
	});
    setupAjax();
	setupEnter();
    setupSubscribeBtn();
	
	// Setup Dialog
	$('#dialog_response').dialog({
		show: 'fade',
		hide: 'fade',
		autoOpen: false,
		draggable: false,
		modal: true,
		resizable: false,
		closeOnEscape: true,
		width: 300,
		height: 250,
		buttons: {
			"Ok" : function() {
				$(this).dialog("close");
			}
		}
	});

});

setupEnter = function() {
	$("#btnEnter").click(function() {
		$("#dialog_subscribe").dialog('open');
	});
}

setupSubscribeBtn = function() {

    $("#txtemail").keypress(function(e) {
        if( e.which == 13 )
        {
			sendEmailSubscription();
        }
    });
	$("#btnSubscribe").keypress(function(e) {
        if( e.which == 32 ||  e.which == 13 )
        {
            sendEmailSubscription();
        }
    });
    $("#btnSubscribe").click(
        sendEmailSubscription
    );
}

sendEmailSubscription = function() {
    if( !emailValid($("#txtemail").val()) )
    {
        $("#dialog_response_text").html("Please enter a valid email address.");
		$('#dialog_response').dialog("open");
        return;
    }

    var s = 'email='+ $("#txtemail").val();

	var p = "../../web_service/pn_subscribeemail.php";

	// perform the ajax call
	$.ajax({
	    url: p,
		data: s,
		success: function(resp) {
		    var response = eval(resp);
		    if( response.result == "invalid email" ) {
		        $("#dialog_response_text").html("Please enter a valid email address.");
				$('#dialog_response').dialog("open");
			}else if (response.result == "duplicate email" ) {
				$("#dialog_response_text").html("You have already subscribed.");
				$('#dialog_response').dialog("open");
		    }else if (response.result == "error recording email" ) {
				$("#dialog_response_text").html("Error recording your email.");
				$('#dialog_response').dialog("open");
			}else if (response.result == "error verifying email" ) {
				$("#dialog_response_text").html("Error verifying your email.");
				$('#dialog_response').dialog("open");
		    }else {
				$("#txtemail").fadeOut(250, function() {
					$(this).remove();
				});
				$("#btnSubscribe").fadeOut(250, function() {
					$(this).remove();
					$("#responsearea").append("<span class='thanks red'>THANK YOU</span><span class='findus'>FIND US ON &#9660;</span>");
				});
			}
		},
		failure: function() {
		    alert("Failure sending/receiving data to/from server.");
		},
		error: function() {
		    alert("Error retrieving data from server.");
		}
	});
}

/* **************************************************************************************************
Function: setupAjax()
Desc: Performs ajax config
************************************************************************************************** */
setupAjax = function(){
    $.ajaxSetup({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        data: "",
		cache: false,
        dataFilter: function(data) {
            var msg;

            if (typeof (JSON) !== 'undefined' &&
            typeof (JSON.parse) === 'function')
                msg = JSON.parse(data);
            else
                msg = eval('(' + data + ')');

            if (msg.hasOwnProperty('d'))
                return msg.d;
            else
                return msg;
        }
    });
}

emailFocused = function() {
    if(document.getElementById('txtemail').value=='ENTER EMAIL ADDRESS')
    {
	    document.getElementById('txtemail').value = '';
    }
}

emailLostFocus = function() {
    if(document.getElementById('txtemail').value=='')
    {
	    document.getElementById('txtemail').value = 'ENTER EMAIL ADDRESS';
    }
}

emailValid = function(email) {
     var re =/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
     return email.match(re);
}

