google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");

google.setOnLoadCallback(function() {
$(document).ready(function() {
        init();
    });
});

function init(){		
	initOverlay();
	whatsThis();
	waterMark();	
	initValidation();
	initVideo();
	initContactValidation();
}

function initOverlay(){
	$(".productOverlay").click(function(){
		$(".overlay").fadeIn('fast').css('z-index', '99999');
		return false;
	});
	
	$(".closeOverlay").click(function(){
		$(".overlay").fadeOut('fast', function(){
			$(".overlay").css('z-index', '0');
		});
		return false;
	});

}

function whatsThis(){
	var pos = $(".whatsthisContainer").offset();  
 	var width = $(".whatsthisContainer").width();
	
	$(".whatsthis").css( { "left": (pos.left + width) + "px", "top":pos.top + "px" } );
	
	$("#whatsthisBtn").click(function(){
		$(".whatsthis").show();
		return false;
	});
	
	$(".whatsthis .close").click(function(){
		$(".whatsthis").hide();
		return false;							  	
	});
}

function waterMark(){
	$(":input[type=text].date.dd").focus(function(){
		if($(this).val() == "DD")
		{
			$(this).css('color','#000').val("");
		}
	});
	
	$(":input[type=text].date.dd").blur(function(){
		if($(this).val() == "")
		{
			$(this).css('color','#d5d7d9').val("DD");
		}
	});
	
	$(":input[type=text].date.mm").focus(function(){
		if($(this).val() == "MM")
		{
			$(this).css('color','#000').val("");
		}
	});
	
	$(":input[type=text].date.mm").blur(function(){
		if($(this).val() == "")
		{
			$(this).css('color','#d5d7d9').val("MM");
		}
	});
}

function initValidation(){
	$(".registerForm input[type=submit]").click(function(){ return(validateForm()) });
	$(".heat").click(function(){ return(checkHeats(this)) });
}

function validateForm(){
	
	var formInvalid = false;
	var emailInvalid = false;
	
	var valid = true;
	
	if($(".required").val() == "" || $(".required").val() == null)
	{
		formInvalid = true;
		valid = false;
	}
	if($(".acceptTerms:checked").length < 3)
	{
		formInvalid = true;
		valid = false;
	}
	
	if($(".dateRequired").val() == "" || $(".dateRequired").val() == null || $(".dateRequired").val() == "DD" || $(".dateRequired").val() == "MM")
	{
		formInvalid = true;
		valid = false;
	}
	
	if($("select.date").val() == "Please select year")
	{
		formInvalid = true;
		valid = false;
	}
	
	if(($(".heat:checked").length == 0) && ($(".expressionSession:checked").length == 0))
	{
		formInvalid = true;
		valid = false;
	}
	
	if(!IsValidEmail($(".emailAddress").val()))
	{
		emailInvalid = true;
		valid = false;
	}
											
	
	
	if(valid)
	{
		return true;
	}
	else
	{
		var error = "";
		if(formInvalid)
		{
			error += "Please fill in all fields marked with *\n\n";
		}
		if(emailInvalid)
		{
			error += "You must supply a valid email address.";
		}
		alert(error);
		return false;
	}
}

function IsValidEmail(email)
{
	var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	return filter.test(email);
}

function initContactValidation(){
	$(".contactForm input[type=submit]").click(function(){ return(validateContactForm()) });
}

function validateContactForm(){
	
	var formInvalid = false;
	var emailInvalid = false;
	
	var valid = true;
	
	if($(".required").val() == "" || $(".required").val() == null)
	{
		formInvalid = true;
		valid = false;
	}
	
	if(!IsValidEmail($("#email").val()))
	{
		emailInvalid = true;
		valid = false;
	}								
	
	if(valid)
	{
		return true;
	}
	else
	{
		var error = "";
		if(formInvalid)
		{
			error += "Please fill in all fields marked with *\n\n";
		}
		if(emailInvalid)
		{
			error += "You must supply a valid email address.";
		}
		alert(error);
		return false;
	}
}

function checkHeats(that){
	if($(".heat:checked").length > 1)
	{
		$(".heat:checked").removeAttr("checked");
		$(that).attr('checked', true);
		return true;
	}
	else
	{
		return true;
	}
}

function initVideo(){
	$("#videoLink").fancybox({
		'padding'           : 0,
		'width'				: 640,
		'height'			: 360,
        'autoScale'         : false,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
		'titleShow'			: false,
		'type'              : 'iframe'
	});
}
