/*
+--------------------------------------------------------------------+
|                                                                    |
|  Website     : F1Study Website                                     |
|  File Name   : main.js                                             |
|  Type        : JavaScript Script File                              |
|  Description : Main JavaScript Functions to be Used in Website     |
|  Author      : Srinivasan A. Paul Joseph                           |
|  Created On  : January 15, 2005                                    |
|  Updated On  : March 16, 2005                                      |
|  Version     : 1.0.1                                               |
|                                                                    |
|  Copyright © 2004, MiPS Web Services Pvt. Ltd.                     |
|  All Rights Reserved.                                              |
|                                                                    |
|  Version History                                                   |
|  ^^^^^^^^^^^^^^^                                                   |
|  1.0.0 - Initial Write up of the Functions.                        |
|  1.0.1 - GetWords() function.                                      |
|                                                                    |
+--------------------------------------------------------------------+
*/

/*********************************************************************
 * Function    : TrimStr(strText)                                    *
 * Description : Trims the String by cutting Spaces from either side *
 * Arguments   : strText - Text to be Trimmed                        *
 * Returns     : Trimmed Text as a String                            *
 *********************************************************************/
function TrimStr(strText)
{
	var strIn = new String(strText), strOut, curX;
	
	strOut = "";
	curX = 0;
	//Check from Left
	for(i=0;i<strIn.length;i++)
	{
		if(strIn.charAt(i)==" ")
			curX++;
		else
			break;
	}
	for(i=curX;i<strIn.length;i++)
	{
		if(strIn.charAt(i)!=" ")
			strOut = strOut+strIn.charAt(i);
		else
			break;
	}
	return strOut;
}
 
/*********************************************************************
 * Function    : PadText(strText,lngLen,optChar,optAlign)            *
 * Description : Pads given Text leaving given Character in the side *
 *               given in optAlign using given Length of Characters. *
 * Arguments   : strText  - Text to be Padded                        *
 *               lngLen   - Length of Characters to Return           *
 *               optChar  - Character to be filled in the Sides      *
 *               optAlign - Alignment of Padded Text                 *
 *                          (0 - Left, 1 - Right, 2 - Center)        *
 * Returns     : Padded Text as a String                             *
 *********************************************************************/
function PadText(strText,lngLen,optChar,optAlign)
{
	var lftLen, rgtLen, strOut, strIn;
	
	strIn = new String(strText);
	strOut = "";
	//Check if the given Text is more or equal to the given Length
	if(strIn.length>=lngLen)
		return strIn;
	if(optAlign==0)
	{
		//Left Alignment
		lftLen = lngLen-strIn.length;
		for(x=0;x<lftLen;x++)
			strOut = strOut+optChar;
		strOut = strOut+strIn.toString();
		return strOut;
	}
	else if(optAlign==1)
	{
		//Right Alignment
		rgtLen = lngLen-strIn.length;
		strOut = strIn.toString();;
		for(x=0;x<rgtLen;x++)
			strOut = strOut+optChar;
		return strOut;
	}
	else
	{
		//Center Alignment
		rgtLen = Math.round((lngLen-strIn.length)/2);
		lftLen = (lngLen-strIn.length)-rgtLen;
		for(x=0;x<lftLen;x++)
			strOut = strOut+optChar;
		strOut = strOut+strIn.toString();
		for(x=0;x<rgtLen;x++)
			strOut = strOut+optChar;
		return strOut;
	}
}

/*********************************************************************
 * Function    : IsEmail(strMail)                                    *
 * Description : Checks whether the given Text is a Email Address.   *
 * Arguments   : strMail - Email Address to be Checked.              *
 * Returns     : True  - Email is Valid                              *
 *               False - Email is NOT Valid                          *
 *********************************************************************/
function IsEmail(strMail)
{
	var strIn = new String(strMail);
	
	if(strIn.length==0)
		return false;
	if(strIn.length<12)
		return false;
	if(strIn.indexOf("@")<3)
		return false;
	if(strIn.indexOf(".")<0)
		return false;
	return true;
}

/*********************************************************************
 * Function    : IsEmpty(strText,[chkSpace])                         *
 * Description : Checks whether the given Text is Empty.             *
 * Arguments   : strText    - Text to be checked whether Empty       *
 *               [chkSpace] - Optional. Checks for Spaces also.      *
 * Returns     : True  - Given Text is Empty                         *
 *               False - Given Text is NOT Empty                     *
 *********************************************************************/
function IsEmpty(strText)
{
	var strIn = new String(strText);
	
	if(strIn.length==0)
		return true;
	if(arguments.length==2)
	{
		if(TrimStr(strText)=="")
			return true;
	}
	return false;
}

/*********************************************************************
 * Function    : IsNumber(strText)                                   *
 * Description : Checks whether the given Text is a Number           *
 * Arguments   : strText    - Text to be checked whether Number      *
 * Returns     : True  - Given Text is a Number                      *
 *               False - Given Text is NOT a Number                  *
 *********************************************************************/
function IsNumber(strText)
{
	var strIn = new String(strText);
	
	if(strIn.length==0)
		return false;
	else
	{
		for(i=0;i<strIn.length;i++)
			if((strIn.charAt(i)>="0" && strIn.charAt(i)<="9") || strIn.charAt(i)==".")
				continue;
			else
				return false;
		return true;
	}
}

/*********************************************************************
 * Function    : TextComp(txtLeft,txtRight,cmpMode)                  *
 * Description : Compares two Texts for Equals/Greater/Lesser        *
 * Arguments   : txtLeft  - Left Side Text to be Compared            *
 *               txtRight - Right Side Text to be Compared with      *
 *               cmpMode  - Comparison Mode (0 - Binary, 1 - Text)   *
 *********************************************************************/
function TextComp(txtLeft,txtRight,cmpMode)
{
	var strLeft=new String(txtLeft);
	var strRight=new String(txtRight);
	if(strLeft.length!=strRight.length)
		return false;
	else
	{
		if(cmpMode==0)
		{
			if(strLeft==strRight)
				return true;
			else
				return false;
		}
		else
		{
			if(strLeft.toLowerCase()==strRight.toLowerCase())
				return true;
			else
				return false;
		}
	}
}

/*********************************************************************
 * Function    : GetIndex(txtLeft,txtRight,cmpMode)                  *
 * Description : Returns the Index of position where txtRight found  *
 * Arguments   : txtLeft  - Left Side Text to be Checked             *
 *               txtRight - Right Side Text to be Checked with       *
 *               cmpMode  - Comparison Mode (0 - Binary, 1 - Text)   *
 *********************************************************************/
function GetIndex(txtLeft,txtRight,cmpMode)
{
	var strLeft=new String(txtLeft);
	var strRight=new String(txtRight);
	if(cmpMode==0)
	{
		//Binary Comparison
		return strLeft.indexOf(strRight);
	}
	else
	{
		//Text Comparison
		return strLeft.toLowerCase().indexOf(strRight.toLowerCase());
	}
}

/*********************************************************************
 * Function    : GetWords(strText)                                   *
 * Description : Returns the Number of words found in a String       *  
 * Arguments   : strText - String that needs to be counted for words *
 *********************************************************************/
function GetWords(strText)
{
	var strStart=new String(strText);
	var lngWords=0,lngCnt=0,lngCheck=true;
	
	while(lngCnt<strText.length)
	{
		if(strText.charAt(lngCnt)!=" ")
		{
			if(lngCheck==true)
			{
				lngWords++;
				lngCheck=false;
			}
		}
		else
			lngCheck=true;
		lngCnt++;
	}
	return lngWords;
}
