/*
	Usage: 
	
	<FORM onSubmit="return checkWholeForm(this)">
*/
function checkContactForm(theForm) 
{
	
	var error = "";
	error += isEmpty("The name field ", theForm.Name.value);
	error += isEmpty("The ContactNo field ", theForm.ContactNo.value);
	error += isEmpty("The email field ", theForm.email.value);    
	
	error += isEmpty("The Enquiry field ", theForm.Enquiry.value);	
	if (error != "") 
	{
		alert(error);
		return false;
	}
	
	/*Check User Information*/
	//error += checkUsername(trim(theForm.Name.value));
	
	/*Check User Details*/
	error += checkEmail(theForm.email.value);
	error += checkNumeric(theForm.ContactNo.value);
	
	if (error != "") 
	{
		alert(error);
		return false;
	}
	
	return true;
}
function checkSearchForm(theForm) 
{
	var error = "";

	error += isEmpty("The search field ", theForm.SEARCH_KEY.value);
	if (error != "") 
	{
		alert(error);
		return false;
	}

	error += checkKeyword(theForm.SEARCH_KEY.value);
	if (error != "") 
	{
		alert(error);
		return false;
	}
	
	return true;
}

function checkRegisterForm(theForm) 
{
	var error = "";
	var temp = "";
	
	
	/*Check Empty Field*/
	error += isEmpty("The User ID field ",theForm.nick_name.value);
	error += isEmpty("The Full Name field " ,theForm.name.value);
	error += isEmpty("The Email field " , theForm.email.value);
	error += isEmpty("The Password field " , theForm.password.value);
	error += isEmpty("The Re-type Password field " , theForm.rePassword.value);
	error += isEmpty("The Gender field " , theForm.gender.value);
	error += isEmpty("The Birth Date field " , theForm.birthDate.value);
	error += isEmpty("The Address field " , theForm.address.value);
	error += isEmpty("The Country field " , theForm.country.value);
	error += isEmpty("The Postal Code field " , theForm.postalCode.value);	
	
	if (error != "") 
	{
		alert(error);
		return false;
	}
	
	/*Check User Information*/
	error += checkUsername(theForm.nick_name.value);
	error += checkUsername(theForm.name.value);
	
	/*Check Re-Typed Password*/
	error += checkReEnteredPassword(theForm.password.value,theForm.rePassword.value);
	
	/*Check User Details*/
	error += checkEmail(theForm.email.value);
	error += checkNumeric(theForm.postalCode.value);
	error += checkDate(theForm.birthDate.value);
	
	return true;
}

function checkEmail (strng) 
{
	var error="";
	
	strng = trim(strng);
	
	if (strng == "") 
	{
		error = "You didn't enter an email address.\n";
	}
	
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) 
	{ 
		error = "Please enter a valid email address.\n";
	}
	
	else 
	{
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) 
		{
			error = "The email address contains illegal characters.\n";
		}
	}
	return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkNumeric (strng) 
{
	strng = trim(strng);
	var error = "";
	
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) 
	{
		error = "The phone number contains illegal characters.";
	}
	
	return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) 
{
	
	var error = "";
	if (strng == "") 
	{
		error = "You didn't enter a password.\n";
	}
	
	var illegalChars = /[\W_]/; // allow only letters and numbers
	
	if ((strng.length < 6) || (strng.length > 8)) 
	{
		error = "The password is the wrong length.\n";
	}
	else if (illegalChars.test(strng)) 
	{
		error = "The password contains illegal characters.\n";
	} 
	else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) 
	{
		error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
	}  
	
	return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) 
{
	var error = "";
	
	var illegalChars = /\W/; // allow letters, numbers, and underscores
	if (strng.length < 1)
	{
		error = "The username is the wrong length.\n";
	}
	
	else if (illegalChars.test(strng)) 
	{
		error = "The username contains illegal characters.\n";
	} 
	return error;
}       


// non-empty textbox




function isEmpty(msg, strng) 
{
	strng = trim(strng);
	
	
	var error = "";
	
	if (strng == "") 
	{
		
		error = msg + " have to be filled.\n"
	}
	
	
	else if (strng.length == 0) 
	{
		error = msg + " have to be filled.\n"
	}
	
	else if (strng == "NA")
	{
		error = msg + " have to be filled.\n"	
	}
	
	return error;	  
}


// was textbox altered

function isDifferent(strng) 
{
	var error = ""; 
	if (strng != "Can\'t touch this!") 
	{
		error = "You altered the inviolate text area.\n";
	}
	return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) 
{
	var error = "";
	if (!(checkvalue)) 
	{
		Error = "Please check a radio button.\n";
	}
	return error;
}

// valid selector from dropdown list

function checkDropdown(choice) 
{
	var error = "";
	if (choice == 0) 
	{
		error = "You didn't choose an option from the drop-down list.\n";
	}    
	return error;
}    


function checkReEnteredPassword(Pword,rePword) 
{
	var error = "";
	if (!(Pword == rePword)) 
	{
		error = "The Re-typed password did not tele.\n";
	}  
	    
	 
	return error;
}    


function checkKeyword(str) 
{
	
	var error = "";
	var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]\_\|\{\}\!\@\#\$\%\^\&\*\-\+\=\<\>\?\~\`]/;
	// allow only letters, numbers, and underscores
	if (illegalChars.test(str)) 
	{
		error = "The keyword contains illegal characters.\n";
	}

	return error;
}


function checkDate (strng) 
{
	var error = "";
	
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) 
	{
		error = "The phone number contains illegal characters.";
	}
	
	return error;
}

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