// ON DOCUMENT READY - AFFECT FORMS
/* ************************************************************************** */
$(document).ready(function() {
	
});


// 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 = '';var str = '';
	var req = $(".required").get();
	var cForm = $("#contactForm").get();
	for (var i = 0;i<req.length;i++) {
		var thisItem = escape(trim(req[i].value));
		var sep = (i==0) ? '' : '&';
		str += sep + req[i].id + '=' + thisItem;
		if(thisItem.length<1) err += req[i].id+"\n";
	}

	if (err.length>0) { alert("The following fields are required:\n\n"+err); }
	else {

		$("#submitButton").val("Sending...");

		$.ajax({
			type: 	"POST",
			url:	"includes/ajax/processContactForm.php",
			data:	str,
			success: function(data) {
				
				var outStr = (data=='SUCCESS') ?
					'Your email has been sent.  Thank you for your interest in Suntava.' :
					'There was a problem while delivering your email.  Please try again at another time.';
					
				alert(outStr);					
				$("#submitButton").val("Send Email");

			} // end of success function
		}); // end of ajax
	} // end if no errors
} // end of function
/* 	************************************************************************** */




// USER REGISTRATION FORM VALIDATION
/* 	************************************************************************** */
function submitRegistrationForm() {
	var err = '';var str = '';var errStr = '';
	var req = $(".required").get();
	var cForm = $("#contactForm").get();
	for (var i = 0;i<req.length;i++) {
		var thisItem = escape(trim(req[i].value));
		var sep = (i==0) ? '' : '&';
		str += sep + req[i].id + '=' + thisItem;
		if(thisItem.length<1) err += req[i].id.replace('_',' ')+"\n";
	} // end for

	if (err.length>0) { alert("The following fields are required:\n\n"+err); }
	else {	
		var em = trim($("#Email").val());
		var un = trim($("#Username").val());
		var p1 = trim($("#Password_1").val());
		var p2 = trim($("#Password_2").val());
		
		// CHECK MATCHING PASSWORDS	
		if (p1!=p2) {
			errStr += "Problem: The password fields do not match.\n\nPlease retype both password fields with the same desired password.\n\n";
			$("#Password_1").val('').focus();
			$("#Password_2").val('');
		} // end if
		
		// CHECK USERNAME LENGTH 
		if (un.length<5) {
			errStr += "Problem: The username you've entered is too short.\n\nPlease select a username that is 5 or more characters long.\n\n";
			$("#Username").val('').focus();
		} // end if			
		// CHECK PASSWORD LENGTH 
		if (p1.length<5) {
			errStr += "Problem: The password you've entered is too short.\n\nPlease select a password that is 5 or more characters long.\n\n";
			$("#Password_1").val('').focus();
			$("#Password_2").val('');
		} // end if
		
		// CHECK ILLEGAL USERNAME
		var isIllegal = /[^a-zA-Z0-9_]/.test(un);
		if (isIllegal) {
			errStr += "Problem: The username you've entered contains illegal characters.\n\nPlease use letters and numbers only.\n\n";
			$("#Username").val('').focus();
		} // end if
		// CHECK ILLEGAL PASSWORD
		var isIllegal2 = /[^a-zA-Z0-9_]/.test(p1);
		if (isIllegal2) {
			errStr += "Problem: The password you've entered contains illegal characters.\n\nPlease use letters and numbers only.\n\n";
			$("#Password_1").val('').focus();
			$("#Password_2").val('');
		} // end if
		
		if (errStr.length>0) { alert(errStr) }
		else {

			$("#submitButton").val("Sending...");

			$.ajax({
				type: 	"POST",
				url:	"includes/ajax/processUserRegForm.php",
				data:	str,
				success: function(data) {

					if (data=="FAIL:USER_EXISTS") {
						alert("Problem:  The username you've entered already exists.\n\nPlease try another username.");	
						$("#Username").val('').focus();
					} else if (data=="FAIL:EMAIL_EXISTS") {
						alert("Problem:  The email address you've entered already exists in our system.\n\nPlease enter another email address.");
						$("#Email").val('').focus();
					} else if (data=="FAIL:DB_INSPROB") {
						alert("Problem:  The server encountered a database error while processing your request.\n\nPlease try again later.");
					} else if (data=="FAIL:EMAIL_PROB") {
						alert("Problem:  The server was unable to send your account verification email.\n\nPlease try again later.")
					} else {

						$("#regFormArea").html(data);
					
					} // end if
					$("#submitButton").val("Submit Registration Form");
				} // end of success function
			}); // end AJAX call
		} // end if no errors
	} // end if no blank fields
} // 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
/* 	************************************************************************** */