$(document).ready(
	function() {
		$('span#idpwd').hide();

//Tell-A-Friend Components	
		$("form#tafForm").validate({
			rules: {
				tafName: "required",
				tafEmail: {
					required: true,
					email: true
				},
				tafFriend1: {
					required: true,
					email: true
				},
				tafFriend2: {
					email: true
				},
				tafFriend3: {
					email: true
				}
			}, messages: {
				tafEmail: {
					email: "Email address not valid."
				},
				tafFriend1: {
					email: "Email address not valid."
				},
				tafFriend2: {
					email: "Email address not valid."
				},
				tafFriend3: {
					email: "Email address not valid."
				}
			}, errorPlacement: function(error, element) {
				element.parent().next().html(error);
			}, submitHandler: function(form) {
				$.getJSON('tellafriendSubmit.php', $(form).serialize(), function(data){
					switch(data.code) {
						case 0:
							alert('You have successfully sent a link to this website to your friends!');
							$('div#tafDialog').dialog('close');
						break;
						case 1:
							alert('System error. Please try again later!');
						break;
					}
				});
			}
		});
		$('div#tafDialog').dialog({
			autoOpen		: false,
			bgiframe		: true,
			resizable		: false,
			width			: 500,
			position		: 'center',
			modal			: true,
			buttons			: {
				'Cancel': function () {
					$(this).dialog('close');
				}, 'Submit': function () {
					$("form#tafForm").submit();
				}
			}
		});

		$('a.tafBtn').click(
			function(event) {
				event.preventDefault();
				$('div#tafDialog').dialog('open');
			}
		);
		




//Contact Us Components
		$('div#contactDialog').dialog({
			autoOpen		: false,
			bgiframe		: true,
			resizable		: false,
			width			: 600,
			position		: 'center',
			modal			: true,
			buttons			: {
				'Cancel': function () {
					$(this).dialog('close');
				}, 'Submit': function () {
					$("form#contactForm").submit();
				}
			}
		});
		$('a.openContact').click(
			function(event) {
				event.preventDefault();
				$('div#contactDialog').dialog('open');
			}
		);
		$("form#contactForm").validate({
			rules: {
				contactName: "required",
				contactEmail: {
					required: true,
					email: true
				},
				contactNumber: {
					required: false,
					digits: true
				},
				contactMessage: "required"
			}, messages: {
				contactNumber: {
					digits: "Contact number must be numeric."
				},
				contactEmail: {
					email: "Email address is not valid."
				}
			}, errorPlacement: function(error, element) {
				element.parent().next().html(error);
			}, submitHandler: function(form) {
				$.getJSON('contactSubmit.php', $(form).serialize(), function(data){
					switch(data.code) {
						case 0:
							alert('Thank you for your interest. We will get back to you shortly.');
							$('div#contactDialog').dialog('close');
						break;
						case 1:
							alert('System error. Please try again later!');
						break;
					}
				});
			}
		});

//Mailing List Components	
		$("form#mlForm").validate({
			rules: {
				mlEmail: {
					required: true,
					email: true
				}
			}, messages: {
				mlEmail: {
					email: "Email address not valid."
				}
			}, errorPlacement: function(error, element) {
				element.parent().next().html(error);
			}, submitHandler: function(form) {
				$.getJSON('mailingList.php', $(form).serialize(), function(data){
					switch(data.code) {
						case 0:
							alert('You have successfully joined the mailing list!');
							$('div#mlDialog').dialog('close');
						break;
						case 1:
							alert('System error. Please try again later!');
						break;
						case 2:
							alert('Email address is already in our mailing list!');
						break;
					}
				});
			}
		});
		$('div#mlDialog').dialog({
			autoOpen		: false,
			bgiframe		: true,
			resizable		: false,
			width			: 400,
			position		: 'center',
			modal			: true,
			buttons			: {
				'Cancel': function () {
					$(this).dialog('close');
				}, 'Submit': function () {
					$("form#mlForm").submit();
				}
			}
		});

		$('a.mailingList').click(
			function(event) {
				event.preventDefault();
				$('div#mlDialog').dialog('open');
			}
		);
		

//Registration Components
		$('a#submitReg').click(
			function(event) {
				event.preventDefault();
				$("form#regForm").submit();
			}
		);
		$("form#regForm").validate({
			rules: {
				name: "required",
				pEmail: {
					required: true,
					email: true
				},
				sEmail: {
					required: true,
					email: true
				},
				phone: {
					required: true,
					minlength: 8
				},
				password: {
					required: true,
					minlength: 8
				},
				confirm_password: {
					required: true,
					minlength: 8,
					equalTo: "#password"
				},
				vercode: "required"
			}, messages: {
				password: {
					minlength: "Must be at least 8 characters."
				},
				confirm_password: {
					minlength: "Must be at least 8 characters.",
					equalTo: "Passwords do not match."
				},
				pEmail: {
					email: "Email address not valid."
				},
				sEmail: {
					email: "Email address not valid."
				}
			}, errorPlacement: function(error, element) {
				element.parent().next().html(error);
			}, submitHandler: function(form) {
				$.getJSON('register.php', $(form).serialize(), function(data){
					switch(data.code) {
						case 0:
							alert('You have successfully created an account!\nYou may login with your Primary Email Address.');
						break;
						case 1:
							alert('System error. Please try again later!');
						break;
						case 2:
							alert('Please check your security code and try again!');
						break;
						case 3:
							alert('These email addresses have already been registered with us. Please try again!');
						break;
						case 4:
							alert('Your primary email address is the same your secondary email address. Please try again!');
						break;
						case 5:
							alert('Please enter a profession/designation and try again!');
						break;
					}
				});
			}
		});



//Login Components
		$('span#idpwd > input[type="password"]').focus(
			function () {
				$(this).unbind('focus').val('');
			}
		);
		$('span#idpwd > input[type="text"]').focus(
			function() {
				$(this).unbind('focus').val('');
			}
		);
		$('a#login').click(
			function(event) {
				event.preventDefault();
				$('span#register').hide();
				$('span#idpwd').show();
				$(this).unbind('click').click(
					function() {
						$.getJSON('login.php', {
							username: $('span#idpwd > input[type="text"]').val(),
							password: $('span#idpwd > input[type="password"]').val()
						}, function(data) {
							switch(data.code) {
								case 0:
									$('span#logout > span').text($('span#idpwd > input[type="text"]').val());
									$('span#register').hide();
									$('span#idpwd').hide();
									$('a#login').hide();
									$('span#logout').show();
								break;
								case 1:
									alert('Server error, please try again later!');
								break;
								case 2:
									alert('Wrong username or password!');
								break;
							}
						});
					}
				);
			}
		);
	}
);