/*
 * SimpleModal Contact 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: contact.js 39 2007-10-26 20:46:32Z emartin24 $
 *
 */

$(document).ready(function () {
	$('.bmSend').click(function (e) {
		e.preventDefault();
		
		var href = $(this).attr("href");
		
		if(href=="#"){
			href = "/Pages/form_sendToCol1.htm";
		}
		
		// load the contact form using ajax
		$.get(href, function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				overlay: 90,
				overlayId: 'contactModalOverlay',
				containerId: 'contactModalContainer',
				iframeId: 'contactModalIframe',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		    
			var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
			var borderHTML = (!badBrowser) ? '<div class="border_tl"></div><div class="border_tr"></div><div class="border_bl"></div><div class="border_br"></div><div class="border_t"></div><div class="border_b"></div><div class="border_l"></div><div class="border_r"></div>' : "";
			
			$('#contactModalContainer').prepend(borderHTML);
			
			var currentTitle = document.title;   
			$('#contactModalContainer .subject').append(currentTitle);
			//var currentURL = location.href;
			//$('#contactModalContainer .subject .send2ColUrl').html(currentURL);
		});
	});
});

function adjustModalShadows(){
	var height = $("#contactModalContainer").height()-20;
	var borderHeight = (!$.browser.msie) ? height : height-1;
	var borderWidth = $("#contactModalContainer").width()-20;
	$("#contactModalContainer .border_l, #contactModalContainer .border_r").height(borderHeight);
	$("#contactModalContainer .border_t, #contactModalContainer .border_b").width(borderWidth);	
}

function animateAdjustModalShadows(offset){
	offset += 12;
	
	$("#contactModalContainer .border_l, #contactModalContainer .border_r").animate({
		height: offset																						 
	});	
}

var contact = {
	message: null,
	open: function (dialog) {
		dialog.overlay.fadeIn(200, function () {
			adjustModalShadows();
			dialog.container.fadeIn(200, function () {
				dialog.content.fadeIn(200, function () {
					adjustModalShadows();
					
					$('#contactModalContainer #inpName1').focus();
				});
			});
		});
	},
	show: function (dialog) {
		$('#contactModalContainer .send').click(function (e) {
			e.preventDefault();
			// validate form
			//var str = $('#contactModalContainer form').serialize() + encodeURIComponent(document.title);
			//alert(str);
			if (contact.validate()) {     
				$('#contactModalContainer .message').fadeOut(function () {
					$('#contactModalContainer .message').removeClass('error').empty();
				});
				$('#contactModalContainer .title').html('Sending...');
				$('#contactModalContainer form').fadeOut(200);
				
				animateAdjustModalShadows(80);
				$('#contactModalContainer .content').animate({
					height: '80px'
				}, function () {
					$('#contactModalContainer .loading').fadeIn(200, function () {
						$.ajax({
							url: '/Pages/SendToColleague.aspx', // This page needs to take queryString parameters and send them to an smtp server, then write some confirmation content to the page.
							data: $('#contactModalContainer form').serialize() + '&title='+encodeURIComponent(document.title)+'&url='+window.location,
							dataType: 'html',
							complete: function (xhr) {
								$('#contactModalContainer .loading').fadeOut(200, function () {
									$('#contactModalContainer .title').html('Thank you!');
									$('#contactModalContainer .message').html('Thank you. Your colleague has been sent this link. Please close this window to return to the page you were viewing.').fadeIn(200);
								});
							},
							error: contact.error
						});
					});
				});
			}
			else {
				if ($('#contactModalContainer .message:visible').length > 0) {
					$('#contactModalContainer .message div').fadeOut(200, function () {
						$('#contactModalContainer .message div').empty();
						contact.showError();
						$('#contactModalContainer .message div').fadeIn(200);
						adjustModalShadows();
					});
				}
				else {
					$(contact.showError);
					adjustModalShadows();
				}
				
			}
		});
	},
	close: function (dialog) {
		dialog.content.fadeOut(200, function () {
			dialog.container.fadeOut(200, function () {
				dialog.overlay.fadeOut(200, function () {
					$.modal.remove(dialog);
				});
			});
		});
	},
	error: function (xhr) {
		alert("AJAX Error: " + xhr.statusText);
	},
	validate: function () {
		contact.message = "The following requires your attention before proceding: <ul>";
		initlength = contact.message.length;
		$('#contactModalContainer input').removeClass('error');
		$('#contactModalContainer textarea').removeClass('error');
		var yourname = $("#contactModalContainer #inpName1").val();
		if (!yourname) {
			contact.message += "<li>You did not enter your name. </li>";
			$('#contactModalContainer #inpName1').addClass('error');
		}
		else {
 		    //check for the length of the name
		    var filter = /.{2,50}$/;
		    if (!filter.test(yourname)) {
				contact.message += "<li>Your name must be between 2 and 50 characters.</li>";
				$('#contactModalContainer #inpName1').addClass('error');
		    }
		}
		var email1 = $("#contactModalContainer #inpEmail1").val();
		if (!email1) {
			contact.message += "<li>Your email is required.</li>";
			$("#contactModalContainer #inpEmail1").addClass('error');
		}
		else {
			// Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
			var filter = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
			if (!filter.test(email1)) {
				contact.message += "<li>The Your email entered is invalid.</li>";
				$("#contactModalContainer #inpEmail1").addClass('error');
			}
		}
		var collname = $("#contactModalContainer #inpName2").val();
		if (!collname) {
			contact.message += "<li>Colleague's name is required.</li>";
			$("#contactModalContainer #inpName2").addClass('error');
		}
		else
		{
   		    //check for the length of the name
		    var filter = /.{2,50}$/;
		    if (!filter.test(collname)) {
				contact.message += "<li>Colleague's name must be between 2 and 50 characters.</li>";
				$("#contactModalContainer #inpName2").addClass('error');
		    }
		}
		var email2 = $("#contactModalContainer #inpEmail2").val();
		if (!email2) {
			contact.message += "<li>Colleague's email is required.</li>";
			$("#contactModalContainer #inpEmail2").addClass('error');
		}
		else {
			// Regex from: http://regexlib.com/REDetails.aspx?regexp_id=599
			var filter = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
			if (!filter.test(email2)) {
				contact.message += "<li>Colleague's email is invalid.</li>";
				$("#contactModalContainer #inpEmail2").addClass('error');
			}
		}
		/*if (!$("#contactModalContainer #inpSubject").val()) {
			contact.message += "Subject is required. ";
		}*/
		if (!$('#contactModalContainer #txtMessage').val()) {
			contact.message += "<li>You did not enter a Message.</li>";
			$('#contactModalContainer #txtMessage').addClass('error');
		}

		if (contact.message.length > initlength) {
			contact.message += "</ul>";
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#contactModalContainer .message').html($('<div class="errors"></div>').append(contact.message)).fadeIn(200);
	}
};

