$(document).ready(function() {

	// Conditional Styling for portfolio page and services page - applies 20px left to every even div
	$('div.services-pod:nth-child(even)').css('margin-left', '20px');
	$('div.portfolio-cont:nth-child(even)').css('margin-left', '20px');
	
	function isString(str) //Checks if paramenter is a string and not empty
	{
		if(typeof str != 'string' || $.trim(str).length == 0)return false;
		return true;			
	}
	
	function isEmail(str) //Checks if string is an email address
	{
		if(!isString(str))return false; //Check if it is a string
		var b=/^.+@.+\..{2,6}$/; //RegExp to check if it is valid email address	
		if(!(b.test(str)))return false;
		var e=/[\(\)\<\>\,\;\:\\\"\[\]]/; //RegExp to check if there are illegal characters
		if(str.match(e))return false;
		return true;
	}	
	
	// Sidebar Validation stuffs - Start
	//----------------------------------------------------------------------------------------------------

	var sidebarRequest = $('#sidebarrequest');

	sidebarRequest.find('input[type="text"]').each(function(){ //This removes the current blur and focus events from the input fields. It might be removed if other validation functions are removed
		this.onfocus = null;
		this.onblur = null;
	});
	
	sidebarRequest.unbind('#sidebarSubmit').submit(function($e){ //Attach validation event
	
		var iName = sidebarRequest.find('#sidebarname');
		var iPhone = sidebarRequest.find('#sidebarphone');
		var iTime = sidebarRequest.find('#sidebartime');
				
		var error = false; //Flags whether there is an error
		var errorMsg = "The following errors have occured: \n\n";
		
		if(!isString($.trim(iName.val()))){ //If name is not correct
			errorMsg = errorMsg + " - Please enter your name\n";
			error = true;
		}
		
		if(iName.val() == 'Your Name'){
			errorMsg = errorMsg + " - Please enter your name\n";
			error = true;		
		}

		if(!isString($.trim(iPhone.val()))){ //If name is not correct
			errorMsg = errorMsg + " - Please enter a phone number\n";
			error = true;
		}

		if(iPhone.val() == 'Phone Number'){
			errorMsg = errorMsg + " - Please enter a phone number\n";
			error = true;		
		}

		if(!isString($.trim(iTime.val()))){ //If name is not correct
			errorMsg = errorMsg + " - Please enter the best time to call\n";
			error = true;
		}

		if(iTime.val() == 'Best Time to Call'){
			errorMsg = errorMsg + " - Please enter the best time to call\n";
			error = true;		
		}

		if(error){ //If there are errors
			$e.preventDefault(); //Prevent form from submitting
			alert(errorMsg);
		} else {
			// Organise the data
			var data = 'name=' + iName.val() + '&phone=' + iPhone.val() + '&bestime=' + iTime.val();
			
			// Start the Ajax
			$.ajax({
				url: "/wp-content/themes/intervelopment/sidebarprocess.php",
				type: "GET",
				data: data,
				cache: false,
				success: function(html){
					if (html==1){
						$('.sidebarform').fadeOut('slow', function(){
							$('.sidebarsuccess').fadeIn('slow');
						});
					} else {
						alert('Sorry, unexpected error. Please try again later.');
					}
				}
			});
			return false;
		}		
		
	});	
	
	// Sidebar Validation stuffs - End
	//----------------------------------------------------------------------------------------------------	
	
	// Contact Form in Content area Validation stuffs - Start
	//----------------------------------------------------------------------------------------------------

	var contactFormContent = $('#form-contact-content');

	contactFormContent.find('input[type="text"]').each(function(){
		this.onfocus = null;
		this.onblur = null;
	});

	contactFormContent.unbind().submit(function($e){
	
		var iName = contactFormContent.find('#name');
		var iEmail = contactFormContent.find('#email');
		var iPhone = contactFormContent.find('#phone');
		var iSubject = contactFormContent.find('#subject');
		var iEnquiry = contactFormContent.find('#enquiry');
		
		contactFormContent.find('.error').css('opacity',0).text('');
		
		var error = false;
		
		if(!isString($.trim(iName.val()))){
			iName.closest('.row').find('.error').text('Please enter your name.');
			error = true;
		}
		
		if(!isEmail($.trim(iEmail.val()))){
			iEmail.closest('.row').find('.error').text('Please enter a valid email address.');
			error = true;
		}
		
		if(!isString($.trim(iPhone.val()))){
			iPhone.closest('.row').find('.error').text('Please enter your phone number.');
			error = true;
		}
		
		if(!isString($.trim(iSubject.val()))){
			iSubject.closest('.row').find('.error').text('Please enter a subject.');
			error = true;
		}
		
		if(!isString($.trim(iEnquiry.val()))){
			iEnquiry.closest('.row').find('.error').text('Please enter your enquiry.');
			error = true;
		}
		
		if(error){
			$e.preventDefault();
			contactFormContent.find('.error').animate({opacity:1},250,'linear',function(){$(this).css('background-color','transparent');});
		} else {
			// Organise the data
			var data = 'name=' + iName.val() + '&email=' + iEmail.val() + '&phone=' + iPhone.val() + '&subject=' + iSubject.val() + '&enquiry=' + iEnquiry.val();
			
			// Start the Ajax
			$.ajax({
				url: "/wp-content/themes/intervelopment/contactprocess.php",
				type: "GET",
				data: data,
				cache: false,
				success: function(html){
					if (html==1){
						$('#contactSuccess').fadeIn('slow');
					} else {
						alert('Sorry, unexpected error. Please try again later.');
					}
				}
			});
			return false;
		}
		
	});	

	// Contact Form in Content area stuffs - End
	//----------------------------------------------------------------------------------------------------

	// SEO Form Validation stuffs - Start
	//----------------------------------------------------------------------------------------------------

	var seoFormContent = $('#form-seo-content');

	seoFormContent.find('input[type="text"]').each(function(){
		this.onfocus = null;
		this.onblur = null;
	});

	seoFormContent.unbind().submit(function($e){
	
		var iName = seoFormContent.find('#name');
		var iEmail = seoFormContent.find('#email');
		var iBlog = seoFormContent.find('#blog');
		var iUrl = seoFormContent.find('#url');
		var iKeyword = seoFormContent.find('#keyword');
		
		seoFormContent.find('.error').css('opacity',0).text('');
		
		var error = false;
		
		if(!isString($.trim(iName.val()))){
			iName.closest('.row').find('.error').text('Please enter your name.');
			error = true;
		}
		
		if(!isEmail($.trim(iEmail.val()))){
			iEmail.closest('.row').find('.error').text('Please enter a valid email address.');
			error = true;
		}
		
		if(!isString($.trim(iBlog.val()))){
			iBlog.closest('.row').find('.error').text('Please enter the name of the website.');
			error = true;
		}
		
		if(!isString($.trim(iUrl.val()))){
			iUrl.closest('.row').find('.error').text('Please enter the URL.');
			error = true;
		}

		if(iUrl.val() == 'http://'){
			iUrl.closest('.row').find('.error').text('Please enter the URL.');
			error = true;		
		}
		
		if(!isString($.trim(iKeyword.val()))){
			iKeyword.closest('.row').find('.error').text('Please enter 1 keyword.');
			error = true;
		}
		
		if(error){
			$e.preventDefault();
			seoFormContent.find('.error').animate({opacity:1},250,'linear',function(){$(this).css('background-color','transparent');});
		} else {
			// Organise the data
			//var data = 'name=' + iName.val() + '&email=' + iEmail.val() + '&phone=' + iPhone.val() + '&subject=' + iSubject.val() + '&enquiry=' + iEnquiry.val();
			var data = 'name=' + iName.val() + '&email=' + iEmail.val() + '&blog=' + iBlog.val() + '&url=' + iUrl.val() + '&keyword=' + iKeyword.val();
			
			// Start the Ajax
			$.ajax({
				url: "/wp-content/themes/intervelopment/seoprocess.php",
				type: "GET",
				data: data,
				cache: false,
				success: function(html){
					if (html==1){
						$('#seoSuccess').fadeIn('slow');
					} else {
						alert('Sorry, unexpected error. Please try again later.');
					}
				}
			});
			return false;
		}
		
	});	

	// SEO Form Validation stuffs - End
	//----------------------------------------------------------------------------------------------------
	
});
