﻿// JScript source code

/////////////////////////////////////////////////////////////////////////////////////////
///  Written By Jamil On 29/07/2002.
///  This function do a basic validation on the time and return a true or false flag.
/////////////////////////////////////////////////////////////////////////////////////////
function isTime(intime)
{
	var timeflag;
	var tokens, lasttoken
	var htoken, mtoken, stoken, itoken
	var usedTime;
	var re		= /[ - / :]/g;
	// initialize.
	timeflag = true;
	
	// make sure that we have someting to work on
	usedTime = (intime)?intime:"";
	usedTime = usedTime.replace(/(am|pm)/," $1")
	usedTime = SqueezeStr(usedTime);
	if(usedTime == "")
	{
		timeflag = false;
		return timeflag;
	}	
	
	// capture all tokens that made up this time
	tokens		= usedTime.split(re);
	lasttoken	= tokens[tokens.length-1];
	if(isInteger(lasttoken))
	{
		htoken = tokens[0]&&isInteger(tokens[0])?tokens[0]-0:0;
		mtoken = tokens[1]&&isInteger(tokens[1])?tokens[1]-0:0;
		stoken = tokens[2]&&isInteger(tokens[2])?tokens[2]-0:0;
		itoken = "";
	}
	else
	{
		htoken = tokens[0]&&isInteger(tokens[0])?tokens[0]-0:0;
		mtoken = tokens[1]&&isInteger(tokens[1])?tokens[1]-0:0;
		stoken = tokens[2]&&isInteger(tokens[2])?tokens[2]-0:0;
		itoken = lasttoken;
	}
	// interogate the cases where the time will not be considerd valid
	if((stoken<0) || (stoken>59) || (mtoken<0) || (mtoken>59) || (htoken<0) || (htoken>23))
	{
		timeflag = false;
		return timeflag;
	}
	else
	{
		itoken = itoken.toLowerCase();
		if((itoken != "am") && (itoken != "pm") && (itoken != ""))
		{
			timeflag = false;
			return timeflag;
		}

		if((itoken=="am") && (htoken>12))
		{
			timeflag = false;
			return timeflag;
		}
		
		if((itoken=="pm") && (htoken>12))
		{
			timeflag = false;
			return timeflag;
		}
	}
	
	return timeflag;
}

/////////////////////////////////////////////////////////////////////////////////////////
///  Written By Jamil On 29/07/2002.
///  This function do a basic validation of the email format and return TRUE or False.
/////////////////////////////////////////////////////////////////////////////////////////
function isEmail(emailstr)
{
	var emailpat=/^.+@.+\..{2,3}$/;
	var emailflag = true;	
	if ((emailstr != "") && (!emailpat.test(emailstr)))
	{
		emailflag = false;
	}
	return emailflag;
}

//////////////////////////////////////////////////////////////////////////////////////
///  Written By Jamil On 29/07/2002.
///  This function do a basic validation of the url format and return TRUE or False.
///  This function is not complete by anu\y mean since there is no standard grammar
///  for a URL.
//////////////////////////////////////////////////////////////////////////////////////
function isUrl(urlStr)
{
	var urlpat=/^http\:\/\/www\..+@.+\..{2,3}$/;
	var urlflag = true;	
	if ((urlStr != "") && (!urlpat.test(urlStr)))
	{
		emailflag = false;
	}
	return emailflag;
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 6 July 2006.
/// This function will return TRUE if the code is made of four digits
/// other than "0000" and "9999".
/////////////////////////////////////////////////////////////////////////////
function isPostcode(val)
{
	var regExp = /\D/;
		
	if(val=="")
	{
		return false;
	}
	if(regExp.test(val))
	{
		return false;	
	}
	if((val == "0000") || (val == "9999"))
	{
	    return false;
	}
	else
	{
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 8 August 2006.
/// This function will return TRUE if the value passed to it is made of length
/// digits and not made of all 9's or 0's.
/////////////////////////////////////////////////////////////////////////////
function isGoodPhone(inStr, inLen)
{
	var regExp = /\D/;
	var regExp9 = /^0+$/;
	var regExp0 = /^9+$/;
	
	// Clean white spaces ....
	inStr = NoSpaceStr(inStr);
	
	if(inStr.length != inLen)
	{
	    return false;
	}
	
	if(regExp.test(inStr) || regExp9.test(inStr) || regExp0.test(inStr))
	{
		return false;	
	}
	
	// return ...
	return true;
}




//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function will return TRUE if the number passed to it is an integer
/// otherwise a FALSE will be returned.
/////////////////////////////////////////////////////////////////////////////
function isInteger(val)
{
	var regExp = /\D/;
		
	if(val=="")
	{
		return false;
	}
	if(regExp.test(val))
	{
		return false;	
	}
	else
	{
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 27 Feb 2006
/// This function check the input against the an STD phone format ..
/// valid format (## #### ####).
/////////////////////////////////////////////////////////////////////////////
function isDigitalCode(val, digitsCount, allowBlank)
{
	var inStr  = cleanBlanks(val)
	var regExp = /\D/;

    // Default 
    if(arguments.length < 3)
    {
       alert("Wrong usage of the function isDigitalCode((val, digitsCount, allowBlank)")
    }
	
	// Check for Blank 
	if(inStr == "")
	{
	    if(allowBlank)
	    {
	        return true;
	    }
	    else
	    {
	        return false;
	    }
	}

	// Check the value ....	
	if((regExp.test(inStr)) || (inStr.length != digitsCount))
	{
		return false;	
	}
	else
	{
		return true;
	}
}



//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function will return TRUE if the string passed to it is alphabetic
/// otherwise a FALSE will be returned.
/////////////////////////////////////////////////////////////////////////////
function isAlpha(val)
{
	var regExp = /[^a-zA-Z]/;
		
	if(val=="")
	{
		return false;
	}
	if(regExp.test(val))
	{
		return false;	
	}
	else
	{
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 04/07/2003.
/// This function will check if the passed value is blank or not.
/// The value is blank if it is zero-length string or made of non printable 
/// character.
/////////////////////////////////////////////////////////////////////////////
function isBlank(str)
{
	var re = /[^\s]/g;
	if(!str)
	{
		return true;
	}
	else if( re.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function will return TRUE if the string passed to it is alphanumeric
/// otherwise a FALSE will be returned.
/////////////////////////////////////////////////////////////////////////////
function isAlphaNumeric(val)
{
	var regExp = /[^a-zA-Z0-9]/;
		
	if(val=="")
	{
		return false;
	}
	if(regExp.test(val))
	{
		return false;	
	}
	else
	{
		return true;
	}
}


//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function will return TRUE if the number passed to it is a decimal
/// otherwise a FALSE will be returned.
/// - This function allow comma grouping i.e. 12,345.56 is a decimal.
/////////////////////////////////////////////////////////////////////////////
function isDecimal(val)
{
	var regExp = /^[-+]?\d+$|^[-+]?[.]\d+$|^[-+]?\d+[.]$|^[-+]?\d+[.]\d+$|^[-+]?\d+(,\d{3})+$|^[-+]?\d+(,\d{3})+[.]\d+$/;
		
	if(val=="")
	{
		return false;
	}
	if(regExp.test(val))
	{
		return true;	
	}
	else
	{
		return false;
	}
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function is an exact copy of the isDecimal function except that it 
/// force the first character to the $ sign.
/// - At this stage the number of decimal is not limited.
/////////////////////////////////////////////////////////////////////////////
function isDollar(val)
{
	var regExp = /^[$]\d+$|^[$]\d+[.]\d+$|^[$]\d+(,\d{3})+$|^[$]\d+(,\d{3})+[.]\d+$/;
		
	if(val=="")
	{
		return false;
	}
	if(regExp.test(val))
	{
		return true;	
	}
	else
	{
		return false;
	}
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function will make sure that the value passed to it is a valid %
/// figure that is an integer between 0 and 100. 
/////////////////////////////////////////////////////////////////////////////
function isPercent(val)
{
	var clVal;
		
	if(val=="")
	{
		return false;
	}
	else
	{
		clVal = parseInt(val,10);
		if((!isInteger(val))||(isNaN(clVal))||(clVal<0)||(clVal>100))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// This Function Validate a given ABN passed to it and return true if the ABN is a valid ABN.
// The Steps involved in the Checking are :
// 1. Create the Weights Array this is given by the client and is the same for all ABN.
// 2. make sure that the ABN is made of 11 digits.
// 3. Claculate the sum of weighted digist of the ABN this is :
//    Sum = ( (d11-1)*(w11) + d10*w10 + d9*w9 + .... + d1*w1). Please Note that we substruct
//    1 from the eleventh digit of the ABN and only from this one.
// 4. Once Calculated The ABN is considered Valid if the remainder of the sum by 89 is 0
// General Note :
// - You could either pass the ABN number directly and set byval = 1 OR
//   Pass the element (text box ) holding the value and set vyval = 0
////////////////////////////////////////////////////////////////////////////////////////
function isABN(elem,byval)
{
	var ABN;
	var abnFlag = true;
	var characters = /\D/g;
	var Weights = new Array(10,1,3,5,7,9,11,13,15,17,19);
	var eleven;
	var sum = 0;
	var by89 = -1;
	var idx ;
	
	if(byval==1)
	{
		ABN = elem;
	}
	else
	{
		ABN = elem.value;
	}
	//Eliminate all spaces from the string if any
	ABN = ABN.replace(/\s/g,"");

	// if string is ZERO-length return.
	if( ABN == "")
	{
		abnFlag = false;
		return abnFlag;
	}

	//If ABN is not made out of 11 digits than set flag to false.
	if((characters.test(ABN)) || ( ABN.length != 11))
	{
		abnFlag = false;
		return abnFlag;
	}
		
	// Get The eleven digits of the number in a an array
	eleven = ABN.split("");

	for ( idx= 0; idx < 11;idx++)
	{
		if(idx == 0)
		{
			sum += (eleven[idx]-0-1)*Weights[idx];
		}
		else
		{
			sum += (eleven[idx]-0)*Weights[idx];
		}
	}
		
	by89 = sum % 89;
		
	if(by89!=0)
	{
		abnFlag = false;
	}
	return	abnFlag;
}

// isACN is only a mask it basically nake sure they are all digits
function isACN(elem,byval)
{
	var ACN;
	var acnFlag = true;
	var characters = /\D/g;
	var idx ;
	
	if(byval==1)
	{
		ACN = elem;
	}
	else
	{
		ACN = elem.value;
	}
	//Eliminate all spaces from the string if any
	ACN = ACN.replace(/\s/g,"");

	// if string is ZERO-length return.
	if( ACN == "")
	{
		acnFlag = false;
		return acnFlag;
	}

	//If ACN is not made out of 9 digits than set flag to false.
	if((characters.test(ACN)) || ( ACN.length != 9))
	{
		acnFlag = false;
		return acnFlag;
	}
	return	acnFlag;
}


//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 30/08/2003
/// This function will make sure that the value passed to it is a valid 
/// username.
/// A username is considered valid if:
/// 1. is not blank.
/// 2. 6 to 20 characters
/// 3. Contains no blank
/// 5. contains no non-alphanumeric charcaters
/////////////////////////////////////////////////////////////////////////////
function isUsername(str, minLen, maxLen)
{
	var ok		= true;
	var blk		= /\s/g;
	var alpha	= /\D/g;
	var forbid	= /\W/
	
	// Assume ...
	if(arguments.length < 3)
	{
		maxLen	= 20;
	}
	else if(arguments.length < 2)
	{
		minLen	= 6;
		maxLen	= 20;
	}

	// Check Bit By Bit .....
	if(!str)
	{
		ok = false;
	}
	else if(str == "")
	{
		ok = false;
	}
	else if ((str.length < minLen) || (str.length > maxLen))
	{
		ok = false;
	}
	else if(blk.test(str))
	{
		ok = false;
	}
	else if(forbid.test(str))
	{
		ok = false;
	}
	
	return ok
}

//////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 30/08/2003
/// This function will make sure that the value passed to it is a valid 
/// password.
/// A password is considered valid if:
/// 1. is not blank.
/// 2. 6 to 20 characters
/// 3. Contains no blank
/// 4. contain at least one digit and one character
/////////////////////////////////////////////////////////////////////////////
function isPassword(str, minLen, maxLen, wm)
{
	var ok			= true;
	var blk			= /\s/g;
	var digit		= /\d/g;
	var alpha		= /[A-Za-z]/g;
	var nonalpha	= /\W/g;
	
	// Assume ...
	if(arguments.length < 4)
	{
		wm = "R";
	}
	else if(arguments.length < 3)
	{
		maxLen	= 20;
		wm		= "R";	
	}
	else if(arguments.length < 2)
	{
		minLen	= 6;
		maxLen	= 20;
		wm		= "R";	
	}
	
	// Check Bit By Bit .....
	if(!str)
	{
		ok = false;
	}
	else if(str == "")
	{
		ok = false;
	}
	else if ((str.length < minLen) || (str.length > maxLen))
	{
		ok = false;
	}
	else if(blk.test(str))
	{
		ok = false;
	}
	
	// Are we in a strict mode ..... 
	if(wm == "S")
	{
		if(!alpha.test(str))
		{
			ok = false;
		}
		else if(!digit.test(str))
		{
			ok = false;
		}
		//else if(nonalpha.test(str))
		//{
		//	ok = false;
		//}
	}
	
	return ok
}


