// Tell-a-friend overlay

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

dojo.addOnLoad ( init );

function init() {
	var tellLink = dojo.byId("Tell");
	dojo.connect(tellLink, "onclick", tellWindow);
}

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

 	text = document.createElement('p');
 	text.style.textAlign = 'left';
 	text.style.font = 'bold 11px Helvetica';
 	text.style.marginBottom = '3px';
 	text.style.color = '#4D4D4D';
 	text.innerHTML = 'Enter your friend\'s email addresses to introduce them to <i>StayUp Online</i> on your recommendation!';

 	text2 = document.createElement('p');
 	text2.style.textAlign = 'left';
 	text2.style.font = 'bold 11px Helvetica';
 	text2.style.marginBottom = '3px';
 	text2.style.color = '#4D4D4D';
 	text2.innerHTML = 'Please enter your information below.';

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

	tellButtonDiv = document.createElement('div')
	tellButtonDiv.style.textAlign = 'left';
	tellButtonDiv.style.marginBottom = '20px';
 	tellAddButton = document.createElement('img');
 	tellAddButton.src = 'images/add_friend.gif';
 	tellAddButton.style.cursor = 'pointer';
 	tellAddButton.id = 'tellAddButton3';
 	tellButtonDiv.appendChild(tellAddButton);

	signupTextDiv = document.createElement('div')
	signupTextDiv.id = 'SignUpText';
	signupTextDiv.style.textAlign = 'left';
	signupTextDiv.style.marginBottom = '5px';
	signupTextDiv.style.marginTop = '0';
	signupTextDiv.style.width = '565px';
	signupTextDiv.style.height = '35px';

	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; margin-bottom: 10px; float: left;'
 	});
 	
	var last_name = new dijit.form.TextBox({
 		id: '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; margin-bottom: 10px; floaot: right;'
 	});

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

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

	buttonsDiv = document.createElement('div')
	buttonsDiv.style.textAlign = 'right';
 	buttonsDiv.appendChild(cancelButton.domNode);
 	buttonsDiv.appendChild(tellSendButton.domNode);

	tellWindow.startup();
	tellWindow.containerNode.appendChild(text);
	tellWindow.containerNode.appendChild(receipient.domNode);
	tellWindow.containerNode.appendChild(tellButtonDiv);
	tellWindow.containerNode.appendChild(text2);
	tellWindow.containerNode.appendChild(address.domNode);
	tellWindow.containerNode.appendChild(first_name.domNode);
	tellWindow.containerNode.appendChild(last_name.domNode);
	tellWindow.containerNode.appendChild(signupTextDiv);
	tellWindow.containerNode.appendChild(buttonsDiv);
	tellWindow.show();

	dojo.connect(dojo.byId('TellThem'), 'onkeyup', function() {
		var firstName = dijit.byId('first_name').getValue();
		var lastName = dijit.byId('last_name').getValue();
		var emailAddress = dijit.byId('address').getValue();
		var receipientAddress = dijit.byId('receipient1').getValue();
		if(firstName != '' && lastName != '' && emailAddress !='' && receipientAddress !=''){
			tellSendButton.attr('disabled', false);
		} else {
			tellSendButton.attr('disabled', true);
		}
	});

	dojo.connect(dojo.byId("Send"), "onclick", function(){
		// Send the HTML email to the listed receipient(s)
		// If the user is not in the database, and checkbox is checked, then add them to PHPList
		// If the user in not in the database, and checkbox is unchecked, do nothing
		// sendHTMLEmail();
		if(dijit.byId('check_me')) {
			var addOrNot = dijit.byId('check_me').getValue();
			if(addOrNot == 'on') {
				var parameters = {
					url: "signup/?p=subscribe",
					load: function(data){
					},
					error: function(data){
					},
					handle: function(response, ioArgs){
					},
					timeout: 2000,
					content: {
						attribute2: last_name.value,
						attribute1: first_name.value,
						email: address.value,
						'list[1]': 'signup',
						subscribe: 'Subscribe'
					}
				};
				dojo.xhrPost(parameters);
			}
		}
		
		var startNum2 = 1;
		var totalFriends2 = '';
		
		do {
			if(startNum2 == 1) {
				totalFriends2 = dijit.byId('receipient' + startNum2).getValue();
			} else {
				totalFriends2 += ', ' + dijit.byId('receipient' + startNum2).getValue();			
			}
			startNum2++;
		} while(dijit.byId('receipient' + startNum2));
		
		var parameters = {
			url: "helpers/tell_a_friend.php",
			load: function(data){
			},
			error: function(data){
			},
			handle: function(response, ioArgs){
				var closeButton = new dijit.form.Button({label: "CLOSE", id: "Close"});
				tellWindow.destroyDescendants();
				var thanksMessage = '<div style="height: 116px;"><h1 style="color: #333333; font: bold 20px helvetica; text-align: center; margin-top: 40px; margin-bottom: 5px;">Your friends can thank you later.</h1><p style="color: #666666; font: normal 14px helvetica; text-align: center; margin-top: 0">But we can thank you now, for helping spread the <b>StayUp</b> gospel!<br /> An email has been sent inviting your friends to come experience <i>StayUp Online</i><br /> for themselves and we look forward to having them';
				if(addOrNot == 'on') {
					thanksMessage += ', and you,';
				}
				thanksMessage += ' in the family.</p></div>';
				tellWindow.containerNode.innerHTML = thanksMessage;
				tellWindow.containerNode.appendChild(closeButton.domNode);
				dojo.connect(dojo.byId("Close"), "onclick", destroyTell);
			},
			timeout: 2000,
			content: {
				attribute2: last_name.value,
				attribute1: first_name.value,
				receipients: totalFriends2,
				replyTo: address.value
			}
		};
		dojo.xhrPost(parameters);


		
	});
	dojo.connect(dojo.byId("Cancel"), "onclick", destroyTell);
 	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)';
		} else {
			// Query db to check for existence of email address already
			var params = {
				url: "helpers/check_for_email.php",
				preventCache: true,
				handle: function(response, ioArgs){
					if(response == 'add') {
						addSignup();
					} else {
						removeSignup();
					}
				},
				error: function(data){
					alert(data);
				},
				timeout: 2000,
				content: {
					address: entry
				}
			};
			dojo.xhrPost(params);
		}
	});

 	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('tellAddButton3'), 'onclick', addTellFriend );
}

var number2 = 2;

function addTellFriend() {
	var newName = 'receipient' + number2;
	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('TellThem').containerNode.insertBefore(name.domNode, tellButtonDiv);

 	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)';
		}
	});
	number2++;
}

function addSignup() {
 	dojo.byId('SignUpText').innerHTML = '<p style="margin: 0; font: normal 11px helvetica; color: #C1272D;">Your email address is missing from our records. Would you like to subscribe to the <i>StayUp Online</i> mailing list?</p>';
 	var signUpCheckBox = new dijit.form.CheckBox({
 		id: 'check_me',
 		checked: true
 	});
 	dojo.byId('SignUpText').appendChild(signUpCheckBox.domNode);
 	label = document.createElement('span')
	label.style.color = '#333333';
	label.style.fontSize = '12px';
	label.style.fontFamily = 'Helvetica';
	label.style.fontWeight = 'bold'
	label.innerHTML = ' Yes, sign me up!';
 	dojo.byId('SignUpText').appendChild(label);
}

function removeSignup() {
	dijit.byId("check_me").destroy();
	dojo.byId('SignUpText').innerHTML = '';
}

function destroyTell() {
	var dialog = dijit.byId("TellThem");
	dialog.destroyRecursive();
	dialog.hide();
}
