/*
 * SimpleModal refer Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2007 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: refer.js 86 2008-01-10 23:15:01Z emartin24 $
 *
 */

var current_modal;

$(document).ready(function () {
	
	$('#admin-btn').attr({href:"javascript:;"});
	$('#privacy').attr({href:"javascript:;"});
	$('#site-terms').attr({href:"javascript:;"});
	$('#miniProtean-announce').attr({href:"javascript:;"});
	
	$('#sweepstakes-link:eq(0)').click(function (e) {
		e.preventDefault();
		$.get("/sweepstakes.php", function(data){
			$(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'sweepstakesContainer',
				onOpen: sweepstakes.open,
				onClose: sweepstakes.close
			});
		});
	});
    $('#avatar-selection-link:eq(0)').click(function (e) {
		e.preventDefault();
	$.get("/forum_addon/select_avatar.php", function(data){
			current_modal = $(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'avatarSelectionContainer',
				onOpen: avatarSelection.open,
				onClose: avatarSelection.close
			});
		});
	});
	$('#site-terms:eq(0)').click(function (e) {
		e.preventDefault();
		$.get("/site_terms.php", function(data){
			$(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'sitetermsContainer',
				onOpen: site_terms.open,
				onClose: site_terms.close
			});
		});
	});
	/* Hiding this to see if I can't call it from the head of the document
	And I successfully can.
	Take that.*/
	
	$('#miniProtean-announce:eq(0)').one("click", function (e) {
		e.preventDefault();
		$.get("/miniProtean_announce.php", function(data){
			$(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'miniProteanContainer',
				onOpen: site_terms.open,
				onClose: site_terms.close
			});
		});
	});
	/**/
	$('#privacy:eq(0)').click(function (e) {
		e.preventDefault();
		$.get("/privacy.php", function(data){
			$(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'privacyContainer',
				onOpen: privacy.open,
				onClose: privacy.close
			});
		});
	});
	$('#points-program:eq(0)').click(function (e) {
		e.preventDefault();
		$.get("/tetrastore/points_program.php", function(data){
			$(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'pointsContainer',
				onOpen: points.open,
				onShow: points.show,
				onClose: points.close
			});
		});
	});
	$('#refer-btn:eq(0)').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("/refer_friend.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'modalContainer',
				onOpen: refer.open,
				onShow: refer.show,
				onClose: refer.close
			});
		});
	});
	$('#admin-btn:eq(0)').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("/contact_admin.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlayId: 'modalOverlay',
				containerId: 'modalContainer',
				onOpen: comment.open,
				onShow: comment.show,
				onClose: comment.close
			});
		});
	});
});

// sweepstakes
var avatarSelection = {
	message: null,
	open: function (dialog) {
		if($.browser.msie) {
			$('#productTout').toggle();
			$('#product_image').toggle();
		}
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200);
			});
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
					if($.browser.msie) {
						$('#productTout').toggle();
						$('#product_image').toggle();
					}				
				});
			});
		});
	}
};

// sweepstakes
var sweepstakes = {
	message: null,
	open: function (dialog) {
		if($.browser.msie) {
			$('#productTout').toggle();
			$('#product_image').toggle();
		}		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200);
			});
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
					if($.browser.msie) {
						$('#productTout').toggle();
						$('#product_image').toggle();
					}
				});
			});
		});
	}
};

// site terms
var site_terms = {
	message: null,
	open: function (dialog) {
		if($.browser.msie) {
			$('#productTout').toggle();
			$('#product_image').toggle();
		}
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200);
			});
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
					if($.browser.msie){
						$('#productTout').toggle();
						$('#product_image').toggle();
					}
				});
			});
		});
	}
};



// privacy
var privacy = {
	message: null,
	open: function (dialog) {
		if($.browser.msie) {
			$('#productTout').toggle();
			$('#product_image').toggle();
		}
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200);
			});
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
					if($.browser.msie) {
						$('#productTout').toggle();
						$('#product_image').toggle();
					}
					});
			});
		});
	}
};

// points program
var points = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200);
			});
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
				});
			});
		});
	}
};

var comment = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function(){
			dialog.container.fadeIn(200,function(){
				dialog.data.fadeIn(200, function(){
					$('modalContainer #your_name').focus();
				});
			});
		});
	},
	show: function (dialog) {
		if($.browser.msie) {
			$('#productTout').toggle();
			$('#product_image').toggle();
		}
		$('#modalContainer .submitadmin').click(function (e) {		
			e.preventDefault();	
			if(comment.validate()){
				var str_data = $('form').serialize();
				$('#modalContainer form').fadeOut(200);
				$('#modalContainer .message').fadeOut(200);
				$('#modalContainer .admin_content').animate({
					height: '200px'
				}, function () {
					//$('#modalContainer .loading').fadeIn(200, function () {		
						$.ajax({
							url: '/contact_admin.php',
							data: str_data + '&action=send',
							dataType: 'html',
							complete: function (xhr) {
							
								$('#modalContainer .loading').fadeOut(200, function () {
									$('#modalContainer .title').html('Thank you!');
									$('#modalContainer .message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: refer.error
						});
					//});
				});	
			} else {
					if ($('#modalContainer .message:visible').length > 0) {
						$('#modalContainer .message div').fadeOut(200, function () {
							$('#modalContainer .message div').empty();
							comment.showError();
							$('#modalContainer .message div').fadeIn(200);
						});
					}
					else {
						$('#modalContainer .message').animate({
							height: '35px'
						}, comment.showError);
					}
			}
		});
	},
	close: function(dialog){
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
					if($.browser.msie) {
						$('#productTout').toggle();
						$('#product_image').toggle();
					}				
				});
			});
		});		
	},
	validate: function () {
		
		refer.message = '';
		
		var required = new Array();
		var invalid = new Array();
		
		var your_email = $('#modalContainer #your_email').val();
		if (your_email) {
			if (!refer.validateEmail(your_email)) {
				invalid.push('your email');
			}	
		}
		var your_message = $('#modalContainer #your_message').val();
		if (!your_message) {
			required.push('your message');
		}

		if ((required.length > 0) || (invalid.length > 0)) {
			
			if (required.length > 0)
				comment.message = '<p style="padding-bottom:6px">Please enter ' + required.join(', ') + '.</p>';
				
			if (invalid.length > 0) {
				comment.message += '<p>Please enter valid email address for ' + invalid.join(', ') + '.</p>';
			}
			
			return false;
		}
		else {
			return true;
		}
	},

	showError: function () {
		$('#modalContainer .message')
			.html($('<div class="error">').append(comment.message))
			.fadeIn(200);
	}
}

var refer = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#modalContainer #your_name').focus();
				});
			});
		});
	},
	show: function (dialog) {
		if($.browser.msie) {
			$('#productTout').toggle();
			$('#product_image').toggle();
		}
		$('#modalContainer .submit').click(function (e) {
			
			var str_data = $('form').serialize();	
			
			e.preventDefault();
			// validate form
			if (refer.validate()) {
				$('#modalContainer .message').fadeOut(function () {
					$('#modalContainer .message').removeClass('error').empty();
				});
				$('#modalContainer .title').html('Sending...');
				$('#modalContainer form').fadeOut(200);
				$('#modalContainer .intro').empty();
				$('#modalContainer .field').empty();
				$('#modalContainer .command').empty();
				$('#modalContainer .content').animate({
					height: '300px'
				}, function () {
					//$('#modalContainer .loading').fadeIn(200, function () {		
						$.ajax({
							url: '/refer_friend.php',
							data: str_data + '&action=send',
							dataType: 'html',
							complete: function (xhr) {
								$('#modalContainer .loading').fadeOut(200, function () {
									$('#modalContainer .title').html('Thank you!');
									$('#modalContainer .message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: refer.error
						});
					//});
				});
			}
			else {
				if ($('#modalContainer .message:visible').length > 0) {
					$('#modalContainer .message div').fadeOut(200, function () {
						$('#modalContainer .message div').empty();
						refer.showError();
						$('#modalContainer .message div').fadeIn(200);
					});
				}
				else {
					$('#modalContainer .message').animate({
						height: '50px'
					}, refer.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		dialog.data.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.close();
					if($.browser.msie) {
						$('#productTout').toggle();
						$('#product_image').toggle();
					}				
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		
		refer.message = '';
		
		var required = new Array();
		var invalid = new Array();
		
		if (!$('#modalContainer #your_name').val()) {
			required.push('your name');
		}

		var your_email = $('#modalContainer #your_email').val();
		if (!your_email) {
			required.push('your email');
		}
		else {
			if (!refer.validateEmail(your_email)) {
				invalid.push('your email');
			}
		}
		
		if (!$('#modalContainer #friend_name').val()) {
			required.push('friend\'s name');
		}

		var friend_email = $('#modalContainer #friend_email').val();
		if (!friend_email) {
			required.push('friend\'s email');
		}
		else {
			if (!refer.validateEmail(friend_email)) {
				invalid.push('friend\'s email');
			}
		}

		if (!$('#modalContainer #your_message').val()) {
			required.push('your message');
		}
		if ((required.length > 0) || (invalid.length > 0)) {
			
			if (required.length > 0)
				refer.message = '<p style="padding-bottom:6px">Please enter ' + required.join(', ') + '.</p>';
				
			if (invalid.length > 0) {
				refer.message += '<p>Please enter valid email address for ' + invalid.join(', ') + '.</p>';
			}
			
			return false;
		}
		else {
			return true;
		}
	},
	validateEmail: function (email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length)
			return false;

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email))
			return false;

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
			return false;

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"$/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				return false;
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
			return false;	

		return true;
	},
	showError: function () {
		$('#modalContainer .message')
			.html($('<div class="error">').append(refer.message))
			.fadeIn(200);
	}
};