// JavaScript Document

function navigation(){
	
	$('div.navigation p a').each( function (){
		var windowPath = location.pathname;
		var path = windowPath.toString().substr(1); //console.log($(this).attr('href') +'=='+ path);
		var href = $(this).attr('href');
		if(href.indexOf(path) > -1){
			$(this).addClass('on');	
		}
	});
}

function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function checkForm(){
	var passedCheck = true;
	$('form input.text').each( function (){
		if($(this).val() == $(this).attr('title') || $(this).val() == ''){
			passedCheck = false;
			$(this).css('background-color','#FF99CC');
		}
	});
	
	return passedCheck;
}

function submitForm(){
	$('#submit').click( function (){
		if(checkForm()){
			var data = $('form').serialize();
			$.ajax({ 
				type: "GET",
				url: '/ajaxHandler.php?action=submitForm&' + data,
				dataType: "text",
				success: function(text) {
					$('form').html('<p><em><strong>Thank you for joining my mailing list.</strong></em></p>');
				}
			});
		}
	});
	
	$('#submit2').click( function (){
		if(checkForm()){
			var data = $('form').serialize();
			$.ajax({ 
				type: "GET",
				url: '/ajaxHandler.php?action=submitForm&whichForm=resource_center&' + data,
				dataType: "text",
				success: function(text) {
					setCookie('resource_center','true',180);
					window.location = '/whitepapers.html';
				}
			});
		}
	});
	
	$('#submit3').click( function (){
		if(checkForm()){
			var data = $('form').serialize();
			$.ajax({ 
				type: "GET",
				url: '/ajaxHandler.php?action=submitForm&whichForm=seminar&' + data,
				dataType: "text",
				success: function(text) {
					setCookie('resource_center','true',180);
					window.location = '/Thank-you.html';
				}
			});
		}
	});
}

$(document).ready(function() {
	navigation();
	submitForm();
})
