$(document).ready(function(){

	$('#working')
    		.hide()  // sakrij
    		.ajaxStart(function() {
        $(this).show();
   		 })
    	.ajaxStop(function() {
        $(this).hide();
    	});


	var msg1 = 'Please fill all the fields';
	var msg2 = 'Please enter the correct email address';
	var senderror = 'Some kind of error occurred!!!';
	
	$('#clear').click(function(){
		
		$('#contactform')[0].reset();
		
		$('.selector span').html('Select one...');	
		$('.error-response').hide(200);
		$('input').removeClass('error');
		
	});
	
	
	$('#submit').click(function(){
		
		$('input').removeClass('error');
		$('.error-response').hide(200);
		
		
		var firstname = $("#firstname").val();
		
		if (firstname == "") {
      	$("#firstname").addClass('error');
      	$("#firstname").focus();
			
			$('.error-response').html(msg1);
			$('.error-response').show(300);
			
      	return false;
    	}
    	
    	
    	var lastname = $("#lastname").val();
		
			if (lastname == "") {
      	$("#lastname").addClass('error');
      	$("#lastname").focus();
			
			$('.error-response').html(msg1);
			$('.error-response').show(300);
			
      	return false;
    	}
    	
    	
    	var jobtitle = $("#jobtitle").val();
		
			if (jobtitle == "") {
      	$("#jobtitle").addClass('error');
      	$("#jobtitle").focus();
			
			$('.error-response').html(msg1);
			$('.error-response').show(300);
			
      	return false;
    	}
    	
    	var organization = $("#organization").val();
		
			if (organization == "") {
      	$("#organization").addClass('error');
      	$("#organization").focus();
			
			$('.error-response').html(msg1);
			$('.error-response').show(300);
			
      	return false;
    	}
    	
    	var phone = $("#phone").val();
		
			if (phone == "") {
      	$("#phone").addClass('error');
      	$("#phone").focus();
			
			$('.error-response').html(msg1);
			$('.error-response').show(300);
			
      	return false;
    	}
    	
    	var email = $("#email").val();
		
			if (email == "") {
      	$("#email").addClass('error');
      	$("#email").focus();
			
			$('.error-response').html(msg1);
			$('.error-response').show(300);
			
      	return false;
    	}


			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			if(!emailReg.test(email)) {
				$("input#email").addClass('error');
    			$("input#email").focus();
				
				$('.error-response').html(msg2);
				$('.error-response').show(300);				
      			return false;
			}	
			
			
			var statement = $("#statement").val();
			if (statement == "none") {
      	$('.error-response').html(msg1);
				$('.error-response').show(300);
			
      	return false;
    	}
    	
			var interest = $("#interest").val();
			if (interest == "none") {
      	$('.error-response').html(msg1);
				$('.error-response').show(300);
			
      	return false;
    	}
			
			//$('#contactform').submit();
			
			var data = "firstname=" + firstname + "&lastname=" + lastname + "&jobtitle=" + jobtitle + "&organization=" + organization + "&phone=" + phone + "&email=" + email + "&statement=" + statement + "&interest=" + interest;
			
			$.ajax({
			type: "POST",
			dataType: 'text',
			url: base_url + '/sendmail.php',
			data: data,
			async: true,
			success: function(output){			
				
				$('#success').html(output);
				$('#success').show(300);
				$('#contactform')[0].reset();
				$('.selector span').html('Select one...');
				$('.error-response').hide(200);
								
			}, 
			error: function(){				
				$('.error-response').html(senderror);
				$('.error-response').show(300);																			
			}	
			
			});

	});



});

