$(document).bind('reveal.facebox', function() { 
	$('#tga_overlay_box_speakup').empty();
	$('#facebox .content #thanx').hide();
	$('#facebox .content #tga_overlay_box_textarea_error').hide();
	$('#loading').hide();
	$('#facebox .content #submit').click(function () {		
		var name = $('#facebox .content input#tga_speakup_from_name');
		var location = $('#facebox .content input#tga_speakup_from_location');
		var email = $('#facebox .content input#tga_speakup_from_email');
		var comment = $('#facebox .content textarea#tga_speakup_from_comments');
		var person_type1 = $('#facebox .content input#tga_speakup_from_option1').attr('checked');
		var person_type2 = $('#facebox .content input#tga_speakup_from_option2').attr('checked');
		var person_type3 = $('#facebox .content input#tga_speakup_from_option3').attr('checked');
		var emailConsent = $('#facebox .content input#emailConsent').attr('checked');
		
		if (person_type1 == true) { person_type1 = 1;}
			else { person_type1=0; }
		if (person_type2 == true) { person_type2 = 1;}
			else { person_type2=0; }
		if (person_type3 == true) { person_type3 = 1;}
			else { person_type3=0; }
			
		if (emailConsent == true) { emailConsent = 1;}
			else { emailConsent=0; }
		
		checkFormValues = function(){
			var values_ok = 0;
			var formOK = false;
			//condition
			if (name.val()=='') {
				name.addClass('hightlight');
				values_ok -=1;
				formOK = false;
				//return false;
			} else {
				name.removeClass('hightlight');
				values_ok ++;
				formOK = true;
				
			}
			//condition
			if (location.val()=='') {
				location.addClass('hightlight');
				values_ok --;
				formOK = false;
				//return false;
			} else {
				location.removeClass('hightlight');
				values_ok ++;
				formOK = true;
			}
			//condition
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			if (!emailReg.test(email.val())){
				email.attr('value', 'Please enter a valid email address');
				email.addClass('hightlight');
				values_ok --;
			}
			else if (emailReg.test(email.val())){
				values_ok ++;
			}
			//condition
			if (email.val()=='') {
				email.addClass('hightlight');
				values_ok --;
				formOK = false;
				//return false;
			} 
			else {
				email.removeClass('hightlight');
				values_ok ++;
				formOK = true;
			}
			//condition
			if (comment.val().length>68){
				$('#facebox .content #tga_overlay_box_textarea_error').show();
				formOK = false;
				values_ok --;
			}
			else if (comment.val().length<=68){
				$('#facebox .content #tga_overlay_box_textarea_error').hide();
				values_ok ++;
			}
			//condition
			if (comment.val()=='') {
				comment.addClass('hightlight');
				values_ok --;
				formOK = false;
				//return false;
			} else {
				comment.removeClass('hightlight');
				values_ok ++;
				formOK = true;
			}
			if (values_ok>=5) return true
			else if (!values_ok<5) return false;
			
		}
		if (!checkFormValues()) return false;
		
		//getting colors
		var textcolor = $('#tga_overlay_box_speakup_text_color_choice li span').parent().attr('class');
		var placardcolor = $('#tga_overlay_box_speakup_placecard_color_choice li span').parent().attr('class');
		//organize the data properly
		var data = 'name=' + name.val() + '&email=' + email.val() + '&location='+ location.val() + '&textcolor='+textcolor + '&placardcolor=' + placardcolor + '&person_type1=' + person_type1 + '&person_type2=' + person_type2 + '&person_type3=' + person_type3 +'&emailConsent=' + emailConsent +'&comment=' + encodeURIComponent(comment.val());
		
		
		//show the loading sign
		$('#facebox .content #loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "http://www.catapultdigital.com/theGoodAgency/unison/submit2db.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			global: false,
			timeout: 1000000,
			//Do not cache the page
			cache: false,
			error: function(){
                        alert('Error loading document');
                },
			beforeSend: function(){
			},
			//success
			
			success: function (html) {
				//if process.php returned 1/true (send mail success)
				if (html==1) {	
					//hide the form
					var h_ = $('#facebox .content').height();
					$('#facebox .content').css('height', h_);
					$('#facebox .content form').fadeOut('slow');
					$('#facebox .content #tga_overlay_box_speakup_instruction').fadeOut('slow', function () {
						$("#facebox .content #thanx").fadeIn(100);
					  });

					
					
					//show the success message
					//$('.done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else {
					alert('Sorry, unexpected error. Please try again later.');	
					$('#facebox .content #loading').hide();
				}
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
		
		
	});	
});	
$(document).bind('close.facebox', function() {
		$('#facebox .content #submit').unbind('click');
});

