// Mailing list signup overlay, PHPList on the back-end

dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.SimpleTextarea");
dojo.require("dijit.form.Button");

dojo.addOnLoad ( init );

function init() {
	var signupLink = dojo.byId("Signup");
	dojo.connect(signupLink, "onclick", signupWindow);
}

function signupWindow(event) {
	dojo.stopEvent(event);
	var signupWindow = new dijit.Dialog({
		id: 'SignUp',
		draggable: false,
		autofocus: false,
		style: "width: 586px; text-align: right;"
	});
	dojo.style(signupWindow.closeButtonNode, "display", "none");
	dijit.byId("SignUp").titleNode.innerHTML = '<img src="images/header_signup.gif" width="266" height="15" border="0" style="position: relative; top: 3px; left: -298px;" />';

	var first_name = new dijit.form.TextBox({
 		id: 'first_name',
 		name: 'first_name',
 		propercase: true,
 		trim: true,
 		style: 'width: 264px; height: 20px; background-image: url(images/backgrounds/signup_first_name.gif); font: 12px helvetica; padding: 2px 6px 0 6px; color: #4D4D4D; float: left'
 	});
 	
	var last_name = new dijit.form.TextBox({
 		id: 'last_name',
 		name: 'last_name',
 		propercase: true,
 		trim: true,
 		style: 'width: 264px; height: 20px; background-image: url(images/backgrounds/signup_last_name.gif); font: 12px helvetica; padding: 2px 6px 0 6px; color: #4D4D4D; float: right;'
 	});

	var address = new dijit.form.TextBox({
 		id: 'address',
 		name: 'address',
 		style: 'width: 554px; height: 20px; background-image: url(images/backgrounds/signup_address.gif); font: 12px helvetica; padding: 2px 6px 0 6px; color: #4D4D4D; margin-bottom: 20px;'
 	});
 	
 	text = document.createElement('p');
 	text.style.textAlign = 'left';
 	text.style.font = 'bold 11px Helvetica';
 	text.style.marginBottom = '3px';
 	text.style.color = '#4D4D4D';
 	text.innerHTML = 'Do you have some friends who should know about <i>StayUp Online</i>? Enter their email address below and we will introduce them to the website on your recommendation!';

	var receipient = new dijit.form.TextBox({
 		id: 'receipient1',
 		name: 'receipient',
 		style: 'width: 554px; height: 20px; background-image: url(images/backgrounds/signup_receipient.gif); font: 12px helvetica; padding: 2px 6px 0 6px; color: #4D4D4D;'
 	});

	buttonDiv = document.createElement('div')
	buttonDiv.style.textAlign = 'left';
 	addButton = document.createElement('img');
 	addButton.src = 'images/add_friend.gif';
 	addButton.style.cursor = 'pointer';
 	addButton.id = 'addButton2';
 	buttonDiv.appendChild(addButton);

	var sendButton = new dijit.form.Button({label: "SEND", id: "SignupSend", style: "color: green;" });
	sendButton.attr('disabled', true);
	var cancelButton = new dijit.form.Button({label: "CANCEL", id: "Cancel"});

	signupWindow.startup();
	signupWindow.containerNode.appendChild(first_name.domNode);
	signupWindow.containerNode.appendChild(last_name.domNode);
	signupWindow.containerNode.appendChild(address.domNode);
	signupWindow.containerNode.appendChild(text);
	signupWindow.containerNode.appendChild(receipient.domNode);
	signupWindow.containerNode.appendChild(buttonDiv);
	signupWindow.containerNode.appendChild(cancelButton.domNode);
	signupWindow.containerNode.appendChild(sendButton.domNode);
	signupWindow.show();

	dojo.connect(dojo.byId('SignUp'), 'onkeyup', function() {
		var testSubject = dijit.byId('first_name').getValue();
		var testAddress = dijit.byId('last_name').getValue();
		var testBody = dijit.byId('address').getValue();
		if(testSubject!='' && testAddress!= '' && testBody!=''){
			sendButton.attr('disabled', false);
		} else {
			sendButton.attr('disabled', true);
		}
	});


	dojo.connect(dojo.byId("SignupSend"), "onclick", function(){
		var parameters = {
			url: "signup/?p=subscribe",
			load: function(data){
			},
			error: function(data){
				signupWindow.destroyDescendants();
				signupWindow.containerNode.innerHTML = '<img src="images/email_sent_error.gif" width="586" height="259" border="0" /><br />';
				signupWindow.containerNode.appendChild(closeButton.domNode);
				dojo.connect(dojo.byId("Close"), "onclick", function(){ signupWindow.destroyRecursive(); closeButton.destroy(); signupWindow.hide(); });
			},
			handle: function(response, ioArgs){
				// Is there a receipient email for tell-a-friend?

				var startNum = 1;
				var totalFriends = '';
				
				do {
					if(startNum == 1) {
						totalFriends = dijit.byId('receipient' + startNum).getValue();
					} else {
						totalFriends += ', ' + dijit.byId('receipient' + startNum).getValue();			
					}
					startNum++;
				} while(dijit.byId('receipient' + startNum));

				if(totalFriends != '') {
					var parameters = {
						url: "helpers/tell_a_friend.php",
						load: function(data){
						},
						error: function(data){
						},
						handle: function(response, ioArgs){
						},
						timeout: 2000,
						content: {
							attribute2: last_name.value,
							attribute1: first_name.value,
							receipients: totalFriends,
							replyTo: address.value
						}
					};
					dojo.xhrPost(parameters);
				}
				
				var closeButton = new dijit.form.Button({label: "CLOSE", id: "Close"});
				signupWindow.destroyDescendants();
				var thanksMessage = '<div style="height: 116px;"><h1 style="color: #333333; font: bold 20px helvetica; text-align: center; margin-top: 40px; margin-bottom: 5px;">Thank you for joining the family!</h1><p style="color: #666666; font: normal 14px helvetica; text-align: center; margin-top: 0">We look forward to keeping you up-to-date on the latest <b>StayUp</b> news. As our <br />full site launch and first print issue approaches, you will be the first to know.<br />';
				if(totalFriends != '') {
					thanksMessage += 'An additional thanks for helping spread the <b>StayUp</b> gospel to your friends!';
				}
				thanksMessage += '</p></div>';
				signupWindow.containerNode.innerHTML = thanksMessage;
				signupWindow.containerNode.appendChild(closeButton.domNode);
				dojo.connect(dojo.byId("Close"), "onclick", destroySignUp);
			},
			timeout: 2000,
			content: {
				attribute2: last_name.value,
				attribute1: first_name.value,
				email: address.value,
				'list[1]': 'signup',
				subscribe: 'Subscribe'
			}
		};
		dojo.xhrPost(parameters);
	});
	dojo.connect(dojo.byId("Cancel"), "onclick", destroySignUp);
 	dojo.connect(dojo.byId('first_name'), 'onfocus', function() {
 		dojo.byId('first_name').style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId('first_name'), 'onblur', function() {
		var entry = dijit.byId('first_name').getValue();
		if(entry==''){
			dojo.byId('first_name').style.backgroundImage = 'url(images/backgrounds/signup_first_name.gif)';
		}
	});
 	dojo.connect(dojo.byId('last_name'), 'onfocus', function() {
 		dojo.byId('last_name').style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId('last_name'), 'onblur', function() {
		var entry = dijit.byId('last_name').getValue();
		if(entry==''){
			dojo.byId('last_name').style.backgroundImage = 'url(images/backgrounds/signup_last_name.gif)';
		}
	});
 	dojo.connect(dojo.byId('address'), 'onfocus', function() {
 		dojo.byId('address').style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId('address'), 'onblur', function() {
		var entry = dijit.byId('address').getValue();
		if(entry==''){
			dojo.byId('address').style.backgroundImage = 'url(images/backgrounds/signup_address.gif)';
		}
	});

 	dojo.connect(dojo.byId('receipient1'), 'onfocus', function() {
 		this.style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId('receipient1'), 'onblur', function() {
		var entry = dijit.byId('receipient1').getValue();
		if(entry==''){
			dojo.byId('receipient1').style.backgroundImage = 'url(images/backgrounds/signup_receipient.gif)';
		}
	});
 	dojo.connect(dojo.byId('addButton2'), 'onclick', addFriend );
}

var number = 2;

function addFriend() {
	var newName = 'receipient' + number;
	var name = new dijit.form.TextBox({
 		id: newName,
 		name: newName,
 		style: 'width: 554px; height: 20px; background-image: url(images/backgrounds/signup_receipient.gif); font: 12px helvetica; padding: 2px 6px 0 6px; color: #4D4D4D;'
 	});
 	dijit.byId('SignUp').containerNode.insertBefore(name.domNode, buttonDiv);

 	dojo.connect(dojo.byId(newName), 'onfocus', function() {
 		this.style.backgroundImage = 'none';
 	});
	dojo.connect(dojo.byId(newName), 'onblur', function() {
		var entry = dijit.byId(newName).getValue();
		if(entry==''){
			this.style.backgroundImage = 'url(images/backgrounds/signup_receipient.gif)';
		}
	});
	number++;
}

function destroySignUp() {
	var dialog = dijit.byId("SignUp");
	dialog.destroyRecursive();
	dialog.hide();
}
