



// COMMON JAVASCRIPT FUNCTIONS
/* 	************************************************************************** */
function ucfirst(str) { return str.substr(0,1).toUpperCase() + str.substr(1); };

function checkRequiredFields(items) {
	var err = '';
	$(items).each(function() {
		if (escape(jQuery.trim($(this).val())).length<1) {
			var name = $(this).attr("name");
			err += ($(this).attr("rel")) ? $(this).attr("rel") : ucfirst(name);
			err += "\n"; 
		}		
	});
	return (err.length>0) ? err : false;
}; 




// LOGIN FORM SUBMISSION
/* ************************************************************************** */
function submitLoginForm(userField,passField,submitField) {

	var user = $("#"+userField).val();
	var pass = $("#"+passField).val();
	$("#"+submitField).blur();
	
	if (user.length>0 && pass.length>0) {
		
		user = escape(trim(user));
		pass = md5(trim(pass));
		
			$.ajax({
				type: 	"POST",
				url:	"includes/ajax/processLogin.php",
				data:	"user="+user+"&pass="+pass,
				success: function(data) {
					
					if (data=='NO') {
						
						alert("The username or password you entered is incorrect.\n\nPlease try again.");
						
					} else if (data=='YES') {
						
						top.location.href = absPath;
						
					} else if (data.indexOf("YES:">-1)) {
						
						var loc = data.replace("YES:",'');
						top.location.href = loc;
						
					} else {
						
						alert("There was a problem trying to login.\n\nPlease try again later.");	
						
					}  // end if
	
				} // end of success function
			}); // end of ajax
			
	} else {
		
		alert("Please enter your username and password.")
		
	} // end if

	
}; // end of function
/* 	************************************************************************** */





// CONTACT FORM VALIDATION
/* 	************************************************************************** */
function submitContactForm() {
	var err = checkRequiredFields($("#contactForm .required"));
	var str = $("#contactForm .ajaxData").serialize();
	
	var url = $("#contactForm").attr("action");
	
	if (err) { alert("The following fields are required:\n\n"+err); }	
	else {
		$("#submitButton").val("Sending...");
		$.ajax({
			type: 	"POST",
			url:	url,
			dataType: "json",
			data:	'sentFrom=ajax&'+str,
			success: function(data) {
				
				
				if (data.msg=='SUCCESS') {
					
					$("#contactFormArea").html("<h2 class='noMargin'>Your email has been sent</h2><p>Thank you for your interest in the Colon Cancer Coalition.</p>");
					
				} else if (data.msg=="EMPTYFIELDS") {					
					
					alert("One or more of the required fields was left blank.");
					$("#submitButton").val("Click to Send Email");
					
				} else if (data.msg=="FAIL") {	
									
					$("#contactFormArea").html("<h2 class='noMargin'>An error has occurred</h2><p>Our server has encountered a problem while sending your email.<br>If you would still like to contact "+data.data.directEmailName+", please use the email address below:<br><br><a href='mailto:"+data.data.directEmailAddress+"'>"+data.data.directEmailAddress+"</a><br><br>We apologize for any inconvenience.");
					
				}
				
			} // end of success function
		}); // end of ajax
	} // end if no errors
	return false;
} // end of function
/* 	************************************************************************** */






// TRIM() - to remove leading and trailing whitespace.
/* 	************************************************************************** */
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
/* 	************************************************************************** */
