function leftTrim(str)
{
	while (str.substring(0,1) == ' ')
	{ 
		str = str.substring(1,str.length);
	}
	return str;
}
function rightTrim(str)
{
	while (str.substring(str.length-1,str.length) == ' ')
	{
		str = str.substring(0,str.length-1); 
	}
	return str;
}
function trim(str)
{
	return leftTrim(rightTrim(str));
} 
function isEmailValid(sEmail)
{
	if (sEmail == "")
	{
		alert("Please fill in your email address.");
		return false;
	}
	else if (!emailCheck(sEmail))
	{
		return false;
	}
	return true;
}

/**
 * this function is used to validate names. Names like first and last names cannot contain spaces,
 * however, names like team names and league names can contain spaces. Pass true or false as second 
 * argument accordingly.
 */
function isNameValid(str1, bAllowSpaces, errVar)
{
	if (isBlank(str1))
	{
		alert ("Please fill in " + errVar);
		return false;
	}
	
	var val = "\"%\\";
	if (bAllowSpaces)
		val += "";
	else
		val += " ";

	for (i=0; i < str1.length; i++)
	{
		var s = str1.charAt(i);
		if (val.indexOf(s) != -1)
		{
			if (bAllowSpaces)
				alert (errVar + " cannot contain \", \\, % ");
			else
				alert (errVar + " cannot contain \", \\, % or a space");
			return false;
		}
	}
	
	return true;
}

function isPasswordValid(sPwd)
{
	if (sPwd == "")
	{
		alert("Please fill in the password.");
		return false;
	}
	else if (sPwd.length < 6)
	{
		alert("Password must be at least 6 characters");
		return false;
	}
	else if(!isValidPassword(sPwd))
	{
		alert("Password cannot contain special characters like \', \", %, \\");
		return false;
	}
	return true;
}

function validateEmailPwd(emailval,passval)
{
	if (emailval == "")
	{
		alert("Please fill in your email address.");
		return "emailNull"
	}
	else if (!emailCheck(emailval))
	{
		return "emailNull"
	}
	else if (passval == "")
	{
		alert("Please fill in your password.");
		return "passwordNull" 
	}
}

function emailCheck (emailStr) 
{
	var emailPat = /^(.+)@(.+)$/
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars = "\[^\\s" + specialChars + "\]"
	var quotedUser = "(\"[^\"]*\")"
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom = validChars + '+'
	var word = "(" + atom + "|" + quotedUser + ")"
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray = emailStr.match(emailPat)
	var user,domain=null,IPArray,domainArray,atomPat,domArr,len,errStr;
	
	if (matchArray == null)
	{
		alert("Email address is incorrect (check @ and .'s)")
		return false
	}

	user=matchArray[1]
	domain=matchArray[2]	
	if (user.match(userPat) == null)
	{
		alert("Email address' username is invalid.")
		return false
	}

	//checks if there is a domain ie after '.'
	domainArray = domain.match(domainPat)
	if (domainArray == null)
	{
		alert("Email address' domain name is invalid.")
		return false
	}
	
	//donno why this is used
	IPArray = domain.match(ipDomainPat);
  
	if (IPArray != null) 
	{
		for (var i=1;i<=4;i++)
		{	
			if (IPArray[i]>255)
			{
				alert("Destination IP address is invalid!")
				return false
			}
		}
		return true
	}

//checks if there is a '.' after domain 
	atomPat = new RegExp(atom,"g")
	domArr = domain.match(atomPat)
	len = domArr.length
	if (len<2)
	{
		errStr = "Email address is missing a hostname!"
		alert(errStr);
		return false
	}
	if (domArr[len-1].length<2 || domArr[len-1].length>3)
	{
		alert("Email address must end in a three-letter domain, or two letter country.")
		return false
	}
	return true;
}

function isBlank(str)
{
	if (str == "")
		return true;

	return false;
}

function isNumeric(str1)
{
	var val = "0123456789";
	for (i=0; i < str1.length; i++)
	{
		var s = str1.charAt(i);
		if (val.indexOf(s) == -1)
			return false;
	}
	return true;
}

function isAlphaNumeric(str1)
{
	var val = "_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (i=0; i < str1.length; i++)
	{
		var s = str1.charAt(i);
		if (val.indexOf(s) == -1)
			return true;
	}
	return false;
}

function isValidPassword(str1)
{
	var val = "'\"%\\";
	for (i=0; i < str1.length; i++)
	{
		var s = str1.charAt(i);
		if (val.indexOf(s) != -1)
			return false;
	}
	return true;
}

function isPinValid (sPinNumber)
{   
	var sum = 0;
	var digit = 0;
	var addend = 0;
	var timesTwo = false;
	var modulus = 0;

	if(sPinNumber == "")
	{
		alert("The Pin cannot be empty");
		return false;
	}
	if(!isNumeric(sPinNumber))
	{
		alert("The Pin should be numeric");
		return false;
	}
	for (i = sPinNumber.length - 1; i >=0; i--) 
	{
		digit = parseInt (sPinNumber.substring (i, i + 1));
		if (timesTwo)
		{
			addend = digit * 2;
			if (addend > 9)
			{
				addend = addend - 9;
			}
		}
		else
		{
			addend = digit;
		}
		sum = sum + addend;
		timesTwo = !timesTwo;
	}
	modulus = sum % 10;

	if (modulus != 0)
	{
		alert ("The Pin does not seem to be valid. Please check again");
		return false;
	}

	return true;
}	

/**
 * returns the index of the element in the array, if present. -1 otherwise.
 */
function indexInArray(arr, elem)
{
	if (arr == null)
		return -1;

	for (var i=0; i < arr.length; i++)
	{
		if (arr[i] == elem)
		{
			return i;
		}
	}

	return -1;
}

function isValidDate(day, month, year) {
	var dt = new Date(parseInt(year), parseInt(month-1), parseInt(day));
	if (dt != null && parseInt(dt.getDate()) == parseInt(day) && (parseInt(dt.getMonth()) + 1) == parseInt(month) && parseInt(dt.getFullYear()) == parseInt(year)) {
		return true;
	}
	else {
		return false;
	}
}

function getMonth(mon) {
	if (mon.toLowerCase() == "jan")
		return 1;
	else if (mon.toLowerCase() == "feb")
		return 2;
	else if (mon.toLowerCase() == "mar")
		return 3;
	else if (mon.toLowerCase() == "apr")
		return 4;
	else if (mon.toLowerCase() == "may")
		return 5;
	else if (mon.toLowerCase() == "jun")
		return 6;
	else if (mon.toLowerCase() == "jul")
		return 7;
	else if (mon.toLowerCase() == "aug")
		return 8;
	else if (mon.toLowerCase() == "sep")
		return 9;
	else if (mon.toLowerCase() == "oct")
		return 10;
	else if (mon.toLowerCase() == "nov")
		return 11;
	else if (mon.toLowerCase() == "dec")
		return 12;
}
/** returns the index of the first duplicate element in the array */
function indexOfDuplicate(arr)
{
	for (var i=0; i < arr.length; i++) {
		for (var j=i+1; j < arr.length; j++) {
			if (arr[i] == arr[j])
				return j;
		}
	}
	return -1;
}
/** returns the index of the first duplicate element in the array */
function duplicateIndices(arr)
{
	for (var i=0; i < arr.length; i++) {
		for (var j=i+1; j < arr.length; j++) {
			if (arr[i] == arr[j])
				return new Array(i, j);
		}
	}
	return null;
}