﻿function PixMaster(params) {
	this.loginServicePath = params.loginServicePath;
}

PixMaster.prototype = 
{
	subscribe: function()
	{
		$('#singUpMessage').hide();

		var email = $('#singUpEmailAddress').val();
		
		if (!email)
		{
			$('#singUpMessage').show();
			$('#singUpMessage').html("Please provide e-mail address");
		}
		else if (!email.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/))
		{
			$('#singUpMessage').show();
			$('#singUpMessage').html("Please provide a valid e-mail");
		}
		else
		{
			$('#singUpMessage').hide();
			
			$.ajax({
				type: "POST",
				url: this.loginServicePath + '/NewsletterSubscribe',
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				data: $.toJSON({ email: email }),
				success: function(data) {
					var result = data.d;
					
					$('#singUpMessage').show();
					if (result == true) {
						$('#singUpMessage').html("Thanks for subscribing.");
						$('#singUpEmailAddress').val('');
					}
					else {
						$('#singUpMessage').html("Can not subscribe.");
					}
				},
				error: function(xhr, msg) {
					var response = $.toJSON(xhr.responseText);

					if (response.Message) {
						alert('Error occured during processing request:' + response.Message);
					}
					else {
						if (xhr.responseText == null || xhr.responseText == '') {
							alert('Error occured during processing request.');
						}
						else {
							alert(xhr.responseText);
						}
					}
				}
			});
		}
	}
}

/*
# url - The URL of the page to open. Example: "http://scriptasylum.com".
# w - The width of the window in pixels.
# h - The height of the window in pixels (doesn't include menubars).
# tb - Toolbar visible? 1 = yes, 0 = no.
# stb - Status bar visible? 1 = yes, 0 = no.
# L - Linkbar visible? 1 = yes, 0 = no.
# mb - Menubar visible? 1 = yes, 0 = no.
# sb - Scrollbars visible? 1 = yes, 0 = no.
# rs - Resizable window? 1 = yes, 0 = no.
# x - The horizontal position of the window from the left of the screen.
# y - The vertical position of the window from the top of the screen.    
*/
function openWindow(url, w, h, tb, stb, l, mb, sb, rs, x, y) {
	var t = (document.layers) ? ',screenX=' + x + ',screenY=' + y : ',left=' + x + ',top=' + y; //A LITTLE CROSS-BROWSER CODE FOR WINDOW POSITIONING
	tb = (tb) ? 'yes' : 'no'; stb = (stb) ? 'yes' : 'no'; l = (l) ? 'yes' : 'no'; mb = (mb) ? 'yes' : 'no'; sb = (sb) ? 'yes' : 'no'; rs = (rs) ? 'yes' : 'no';
	var x = window.open(url, 'newWin' + new Date().getTime(), 'scrollbars=' + sb + ',width=' + w + ',height=' + h + ',toolbar=' + tb + ',status=' + stb + ',menubar=' + mb + ',links=' + l + ',resizable=' + rs + t);
	x.focus();
}