/*******************************************************************
File Name	:	tshcommon.js
Description	:	Common Javascript functions
Author		:	Niraj
Create Date	:	08-APR-2005
Modify Date	:	29-APR-2005

FUNCTIONS AT GLANCE
1.	ValidateEmailAddress	:	Validate email address
2.	ValidateImageFileName	:	Validate Image file Name (eitehr gif or jpg)
3.  ValidateUserName        :   Validate User Name . No space is allowed. Only alphanumeric character and underscore is allowed.
4.  ValidatePassword        :   Validate Password. Must be 4-20 characters long. No space is allowed.
*******************************************************************/


/**************************************************************
Method Name	:	ValidateEmailAddress
Description	:	Validate for the Email address
Parameter	:	1.	Email address (string)
Return		:	True if email address is correct else false.
**************************************************************/
function ValidateEmailAddress(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
		
	if (str.indexOf(" ")!=-1){
		return false;
	}

	return true;
}


/**************************************************************
Method Name	:	ValidateImageFileName
Description	:	Validate image file name. (either jpg or gif)
Parameter	:	1.	image file name (string)
Return		:	True if valid image file name else return false.
**************************************************************/
function ValidateImageFileName(sFileName) {
	sFileName = sFileName.toLowerCase();
	iJPGIndex = sFileName.indexOf('jpg');
	iGIFIndex = sFileName.indexOf('gif');
	
	if(iJPGIndex<0 && iGIFIndex<0) {
		return false;
	}

	return true;
}


/**************************************************************
Method Name	:	ValidatePassword
Description	:	Validate Password . Must be 4-20 characters long. No space is allowed.
Parameter	:	1.	password (string)
Return		:	True if valid password else return false.
**************************************************************/
function CheckPassword(sPassword) {
	if ((sPassword.length < 4) || (sPassword.length > 20) )	{
		return false;
	}

	var sChars = " ";
	for (var i = 0; i < sPassword.length; i++) {
		if (sChars.indexOf(sPassword.charAt(i)) != -1) {
			return false;
		}
	}
	return true;
}

/**************************************************************
Method Name	:	ValidateUserName
Description	:	Validate user name. No space is allowed. Only alphanumeric character and underscore is allowed.
Parameter	:	1.	user-name (string)
Return		:	True if valid user-name else return false.
**************************************************************/
function Checkusername(sUserName) {
	var strValidChars = "abcdefghijklmnopqrstuvwxyz0123456789_";
	var strChar;
	var blnResult = true;

	for (i = 0; i < sUserName.length && blnResult == true; i++){
		strChar = sUserName.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;				
		}
	}
	return blnResult;
}