
/* 
	airfront vs studioskylab
	page specific js file
*/


var PLAJoin = {
	
	querypaths : {
		title : "#title",
		url_title : "#url_title",
		project_name : "#field_id_25",
		location : "#field_id_28",
		when_start : "#field_id_29",
		what_do : "#field_id_26",
		how_start : "#field_id_30",
		future_plans: "#field_id_32",
		c_n : "#field_id_55",
		c_t : "#field_id_33",
		c_e : "#field_id_34",
		formSubmitButton : "#joinsubmit",
		formErrorClass : "frow-error"
	},
	
	jsLabels : {
		
	},
	
	_init : function() {
		var self = this;
		
		$j(PLAJoin.querypaths.formSubmitButton).click(function(e){

				$j(".frow-error").remove();
				var hasError = false;
				var frow_error_opening = '<p class="' + PLAJoin.querypaths.formErrorClass + '">';
				var frow_error_closing = '</p>';
				
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				
				filename = $(".ngen-file-input").val();
				
				var projectNameVal = $j(PLAJoin.querypaths.project_name).val();
				if(projectNameVal == '') {
					$j(PLAJoin.querypaths.project_name).after(frow_error_opening + 'Please enter your project name.' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.project_name).parent()).fadeIn();
					hasError = true;
				}

				var locationVal = $j(PLAJoin.querypaths.location).val();
				if(locationVal == '') {
					$j(PLAJoin.querypaths.location).after(frow_error_opening + 'Please enter a location' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.location).parent()).fadeIn();
					hasError = true;
				}

				var when_startVal = $j(PLAJoin.querypaths.when_start).val();
				if(when_startVal == '') {
					$j(PLAJoin.querypaths.when_start).after(frow_error_opening + 'Please enter where you started' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.when_start).parent()).fadeIn();
					hasError = true;
				}

				var what_doVal = $j(PLAJoin.querypaths.what_do).val();
				if(what_doVal == '') {
					$j(PLAJoin.querypaths.what_do).after(frow_error_opening + 'Please enter what do you do' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.what_do).parent()).fadeIn();
					hasError = true;
				}

				var futurePlansVal = $j(PLAJoin.querypaths.future_plans).val();
				if(futurePlansVal == '') {
					$j(PLAJoin.querypaths.future_plans).after(frow_error_opening + 'Please enter your future plans' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.future_plans).parent()).fadeIn();
					hasError = true;
				}

				var how_startVal = $j(PLAJoin.querypaths.how_start).val();
				if(how_startVal == '') {
					$j(PLAJoin.querypaths.how_start).after(frow_error_opening + 'Please how you started' + frow_error_closing)
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.how_start).parent()).fadeIn();
					hasError = true;
				}

				var contactNameVal = $j(PLAJoin.querypaths.c_n).val();
				if(contactNameVal == '') {
					$j(PLAJoin.querypaths.c_n).after(frow_error_opening + 'Please give a contact name' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.c_n).parent()).fadeIn();
					hasError = true;
				}

				var contactNumVal = $j(PLAJoin.querypaths.c_t).val();
				if(contactNumVal == '') {
					$j(PLAJoin.querypaths.c_t).after(frow_error_opening + 'Please how you started' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.c_t).parent()).fadeIn();
					hasError = true;
				}

				var emailFromVal = $j(PLAJoin.querypaths.c_e).val();
				if(emailFromVal == '') {
					$j(PLAJoin.querypaths.c_e).after(frow_error_opening + 'Please enter your email address' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.c_e).parent()).fadeIn();
					hasError = true;
				} else if(!emailReg.test(emailFromVal)) {	
					$j(PLAJoin.querypaths.c_e).after(frow_error_opening + 'Please enter a valid email address' + frow_error_closing);
					$j("." + PLAJoin.querypaths.formErrorClass, $j(PLAJoin.querypaths.c_e).parent()).fadeIn();
					hasError = true;
				}

				var perm = $("#image_permission:checked").val();
				
				if (perm == 1) {

					if (filename == '') {

						alert ("Please select an image to upload!");
						hasError = true;
					
					} else {
					
						$("#joinsubmit").val("Checking file type...");

						$.ajax({
						   type: "POST",
						   url: "/_client/script/lib/check.php",
						   data: "field_id_56=" + filename,
						   async: false,
						   success: function(msg){

								if (msg != "jpg") {
								
									$("#joinsubmit").val("Send!");
									alert ("The file type " + msg + " is not allowed. Please try uploading a JPEG picture file again.");
									hasError = true;
									return false;
								
								} else {
								
									$("#joinsubmit").val("Submitting...");
									return true;
								
								}
							
						   }

						 });

					}
					
				} else {
					
					alert ("Please ensure that you have checked 'Do you have the right to upload this picture?' box to continue");
					
				}

				if(hasError == false) {	
					return true;
				} else {
					$("#joinsubmit").val("Send!");
					e.preventDefault();
				}
				
		});	
		
	}
	
}

$j(document).ready(function(){
	PLAJoin._init();
});
