function validateFields(username, password)
{
    uid = $("#" + username).val();
	pwd = $("#" + password).val();
	
	$.each(inputFields, function(x, val) //object name, object value
	{
		valSearch = val.search(/|/i);
		return (valSearch != -1) ? validate(x, val, "|") : '';	    
	}
	);
	//stop from being send request to the server if all fields are empty
	return ((uid && pwd) != '') ? true : false;
}
			 
function validate(x, val, seperator)
{
		valSplit = val.split("" + seperator + "");
		switch (valSplit[1]) //for required fields
		{
			case "require":
				return ($("#" + x).val() != "") ? minChar(x, valSplit[2]) : errorMessage(valSplit[3], 'require');
				
				break;
			case "numeric":
				numVal = $("#" + x).val();
				return (!isNaN(numVal)) ? minChar(x, valSplit[2]) : errorMessage(valSplit[3], 'numeric');
				break;
			default:
				return minChar(x, valSplit[1]);
				break;
	    }
}

function errorMessage(focusField, isNumeric)
{
    (isNumeric != 'numeric') ? $("." + focusField).show('slow') : $("." + focusField).replaceWith('should be ' + isNumeric);
	$("#" + focusField).focus();
	// if the field is no longer empty, remove the error message
	$("#" + focusField).keyup(function()
	{
	    ($("#" + focusField).val() != "") ? $("." + focusField).hide('slow') : "";
	}
	);
}
			 
function minChar(x, minVal)
{
	switch (minVal) //for string length
	{
		case minVal:
			return ($("#" + x).val().length >= minVal) ? '' : alert(valSplit[0] + " should be greater than " + minVal + " character.");
			
			break;
		default:
			break;
	}
}
