$(document).ready(function() {
	
	// Place ID's of all required fields here.
		required = ["firstname", "lastname", "email", "phone"];
		// If using an ID other than #email or #error then replace it here
		email = $("#email");
		errornotice = $("#error");
		// The text to show up within a field when it is incorrect
		emptyerror = "Required Field.";
		emailerror = "Please enter a valid e-mail.";
	
		$("#form_contactlisting").submit(function(){	
			//Validate required fields
			for (i=0;i<required.length;i++) {
				var input = $('#'+required[i]);
				if ((input.val() == "") || (input.val() == emptyerror)) {
					input.addClass("needsfilled");
					input.val(emptyerror);
					errornotice.fadeIn(750);
				} else {
					input.removeClass("needsfilled");
				}
			}
			// Validate the e-mail.
			if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
				email.addClass("needsfilled");
				email.val(emailerror);
			}
	
			//if any inputs on the page have the class 'needsfilled' the form will not submit
			if ($(":input").hasClass("needsfilled")) {
				return false;
			} else {
				errornotice.hide();
				return true;
			}
		});
		
		// Clears any fields in the form when the user clicks on them
		$(":input").focus(function(){		
		   if ($(this).hasClass("needsfilled") ) {
				$(this).val("");
				$(this).removeClass("needsfilled");
		   }
		});
		
		
		$('#message').val('Do not include confidential or private information in this form.');
	 
	 	/*this is for tabs */
		$(".middle-content").hide(); //Hide all content
		$('#recently-answered-questions').show();
		$("ul.tabs li:first").addClass("active").show(); //Activate first tab
		$(".tab_content:first").show(); //Show first tab content
		
	 	/*this is for paginatin*/ 
		$("#recently-answered-questions").children(".page").hide(); //hide the pages
		$("#recently-asked-questions").children(".page").hide();
		$("#more-questions").children(".page").hide();
		
		$("#recently-answered-questions").children(".page").removeClass("active"); //set current to the page we are on
		$("#recently-answered-questions .page.1").addClass("active").show(); //Activate first pages
		$("#recently-asked-questions .page.1").addClass("active").show(); 
		$("#more-questions .page.1").addClass("active").show(); 
		
		$("ul.tabs li").click(function() {
			$("ul.tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(".middle-content").hide(); //Hide all tab content
			
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			
			return false; //kill the link
		}); 
	 
 		$(".pagination-link").click(function() {
 			var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
 			$(this).parent().children("a").removeClass("active");
 			$(this).addClass("active");
 			$(this).parent().parent().children().removeClass("active");
  			$(this).parent().parent().children(".page").hide();
  			$(this).parent().parent().children(activeTab).show(); //Add "active" class to selected tab
 			$(this).parent().parent().children(activeTab).addClass("active"); //Add "active" class to selected tab
			
 			return false; //kill the link
		}); 
		
		$('#message').focus(function() {
				if($(this).hasClass('defaultTextActive')) {
					$(this).val('');
		    			$(this).css('color', '#000');   
		    			$(this).removeClass('defaultTextActive');
				}	    		
		});
		
		$('#message').blur(function() {
		    if(!$(this).val() ) {
		    		$(this).css('color', '#666');
				$(this).val('Do not include confidential or private information in this form.');
		    		$(this).addClass(' defaultTextActive');
		    }
		});
		
		$('#qa_detail').keyup(function(){
				limitChars('qa_detail', '2000', 'limitqadetail'); 
		});
			
		$('#qa_title').keyup(function(){
				limitChars('qa_title', '120', 'limitqatitle'); 
		});
		
		cleanValidator.init({
			formId: 'form_ask',
			inputColors: ['#FFFFFF', '#FFFFFF'],
			errorColors: ['#FFFF99', '#CF3339'],
			isRequired: ['qa_title','qa_detail','qa_state','qa_category','qa_email'],
			isEmail: ['qa_email'],
			isTermCheck: ['terms_agreed']
		});
		
		
		
	
	$('input.code-error').css('background', '#FAD2D2');
			
});
