$(document).ready(function() {  
	
	/* REMOTE SECTION */  
	/* Limit dragging to visible window */
	//$(".drag").draggable({ containment: 'html', scroll: false });
	$(".drag").draggable();
	
	$(window).load(function(){
		$(".map img:first").fadeIn(1300)                  
	});  
	
	/* WORLD MAP */
	var mapHoverSpeed = 250;
	$("#world-map area").hover(
		function(){
			// check the title of the area hovered over against the hover image's alt to find which hover image to use   
			var regionTitle = this.title.split("_")[0];   
			$(".map-hover").each(function(){
				if (this.alt==regionTitle) {
					$(this).fadeIn(mapHoverSpeed);
				}
			})
		},
		function(){   
			$(".map-hover").each(function(){   
				if (!$(this).hasClass("map-hover-on")) {   
					$(this).fadeOut(mapHoverSpeed); 
				}
			 })
		}
	); 
	$("#world-map area").click(function(){
		var regionTitle = this.title.split("_")[0];
		$(".map-hover").each(function(){
			if (this.alt==regionTitle) {
				$(this).addClass("map-hover-on");
			}
		})   
	});   
	                        
	
	/* LAUNCH REMOTE FORM FROM ANCHOR CLICK AND SET HIDDEN REGION VALUE */
	$(".launch-form").click(function(){
		$(this.hash).fadeIn(500);   
		
		//set title and id
		var regionTitle = this.title.split('_')[0];
		var regionID = this.title.split('_')[1];
		$("#region").val(regionTitle);
		$("#regionID").val(regionID);
		
		$("#display-region").html(regionTitle);    
		//$(".btn").css("background-color","#"+this.rel)
		$("#remote-form-container").css("background-color","#"+this.alt)     
		return false;
	});
	
	/* CLOSE REMOTE FORM FROM ANCHOR CLICK AND SET HIDDEN REGION TO NULL */
	$(".close-form").click(function(){
		$(this.hash).fadeOut(500);           
		$("#region").val("");  
		$(".map-hover").fadeOut(mapHoverSpeed).removeClass("map-hover-on"); 
		return false;
	});
	
	/***** AJAX POST ****/
	$("#remote-contact").submit(function(e) {
		e.preventDefault();
		
		//test email validity
		var validatedemail = $("#txtEmail").val();
		var emailtosend = '';
		if(validatedemail != 0)
		{
			if(isValidEmailAddress(validatedemail))
			{
				emailtosend = $("#txtEmail").val();				
			}			
		}	
		
		$("fieldset").removeClass("error");
		$(".buttons input").hide();
		$(".buttons img").show();
		 $.post(
			"/remote/", 
			{ 
				txtLastName: $("#txtLastName").val(),
				txtFirstName: $("#txtFirstName").val(),
				txtEmail: emailtosend, 
				txtCompany: $("#txtCompany").val(),		
				txtPhone: $("#txtPhone").val(),
				txtCity: $("#txtCity").val(),
				txtAIM: $("#txtAIM").val(),
				txtState: $("#txtState").val(),     
				regionID: $("#regionID").val(), 
				ddlCategory: $("select[name=ddlCategory]").val(),
				ddlCategory2: $("select[name=ddlCategory2]").val(),
				txtWebsite: $("#txtWebsite").val(),
				txtComments: $("#txtComments").val()
			}, 
			function (data, textStatus, xhr) {
				if(data == 'success') {	
					//show success messaging
					//alert(data);
					$("form").hide();
					$("#message").fadeIn(500);   
				}
				else {
					//loop through response and show error messaging
					var errors = data.split(',');
					$.each(errors, function(index, value)  {
						setErrorClass($.trim(value));	
					});
					
				} 
				$(".buttons input").show();
				$(".buttons img").hide();           
			
	    });										 
	});
}); 

function setErrorClass(id)
{
	$("input[name="+id+"]").parents("fieldset").addClass("error");
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

