
// Check if a string is empty.
function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}



// Check if a string has a space.
function HasSpace(s)
{
	var i;
	for (i = 0; i < s.length; i++)
    {
        // Check that current character is number or letter.
        var c = s.charAt(i);
        if (c == ' ')
        {
			return true;
	         break;
		}
    }
    // All characters are numbers or letters.
    return false;
}


// Check if length of string is with in the given range
function isLenInRange(s,nMin,nMax)
{
	return (s.length >= nMin) && (s.length <= nMax);
}


// Trim a string.
function trimString(inString)
{
	var outString;
	var startPos;
	var endPos;
	var ch;
	var emptyflag;
	
	emptyflag=false;

	// where do we start?
	startPos = 0;
	ch = inString.charAt(startPos);
	
	while ((ch == " ") || (ch == "\b") || (ch == "\f") || (ch == "\n") || (ch == "\r") )
	 {
		startPos++;

            //*****this part returns ("") if the string contains only the characters which are checked above****
               if(startPos==inString.length )
		 { return (""); }

		ch = inString.charAt(startPos);
	}
	
	// where do we end?
	endPos = inString.length - 1;
	ch = inString.charAt(endPos);
	
	while ((ch == " ") || (ch == "\b") || (ch == "\f") || (ch == "\n") || (ch == "\r") || (ch == "\n"))
	 {
		endPos--;
		ch = inString.charAt(endPos);
	}
	
	// get the string
	outString = inString.substring(startPos, endPos + 1);

	return outString;
}



// This validation searches through string's characters one by one. If character is not in bag, append to returnString.
function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
	{   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}



// Check if a character is an alphabet.
function isLetter(c)
{
		return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}



// Check if a character is a decimal digit.
function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"));
}



// Check if a character is a particular character.
function isThisChar(c, ThisChar)
{
	return (c == ThisChar);
}



// Check if a string is an integer.
function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
	{   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")))
			return false;
    }
    return true;
}



// Check if a string is an Alphabetic String.
function isAlphabeticString(s)
{   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!(isLetter(c) || (c == ' ')))
        {
			return false;
			break;
		}
    }
    return true;
}



// Check if a string is an Alphanumeric string.
function isAlphanumericString(s)
{   
	var i;

	for (i = 0; i < s.length; i++)
	{   
    	var c = s.charAt(i);

		if(!isLetter(c))
		{
		   if(!isDigit(c))
			{	
				if(!isThisChar(c, "_"))
			  	{
					return false;				
				}
			}
		}	
	}
	return true;
}



// Check if a string is an Alphanumeric string with space.
function isAlphanumericStringwithspace(s)
{
	var i;

	for (i = 0; i < s.length; i++)
    {   
    	var c = s.charAt(i);

		if(!isLetter(c))
		{
		   if(!isDigit(c))
			{
	 		  if(!isThisChar(c, "_"))
			  {
				  if(!(c == ' '))
				  {
					return false;
				  }
              }
           }
        }
     }
   return true;
}



// Date Validation.
function isDateValid(day, month, year)
{
	var days;

	if(month == 2) {
		if(year % 4 == 0) {
			days = 29;			
		} else {
			days = 28;
		}
	} else {
		if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) {
			days = 31;
		} else {
			days = 30;		
		}
	}

	if(day > days) {
		return false;
	} else {
		return true;
	}
}


//General Text
function isValidGenText(address)
{
	if(address.length > 0)
	{
		for(var i=0;  i < address.length; i++)
		{
			var ch = address.charAt(i);
			if(!((ch == "/") || (ch == ",") || (ch == "\n") || (ch == "\r") || (ch == ".") || (ch == "-") || (ch == "'") || (ch == ":") || (ch == " ") || (ch == "#") || ((ch >= "a") && (ch <= "z")) || ((ch >= "A") && (ch <= "Z")) || ((ch >= "0") && (ch <= "9"))))
			{
				return false;
			}			
		}
	}
	return true;
}


//Address Validation.
function isValidAddress(address)
{
	if(address.length > 0)
	{
		for(var i=0;  i < address.length; i++)
		{
			var ch = address.charAt(i);
			if(!((ch == "/") || (ch == ",") || (ch == "\n") || (ch == "\r") || (ch == ".") || (ch == "-") || (ch == "'") || (ch == ":") || (ch == " ") || (ch == "#") || ((ch >= "a") && (ch <= "z")) || ((ch >= "A") && (ch <= "Z")) || ((ch >= "0") && (ch <= "9"))))
			{
				return false;
			}			
		}
	}
	return true;
}



// Pin code Validation.
function isValidPinCode(pincode)
{
	if(!isInteger(pincode))
		return false;
	else if(pincode.charAt(0) == "0")
		return false;
    else if(pincode.length != 6)
			return false;
	else if(pincode== "000000")
			return false;
	else
		return true;
}



//Phone Number Validation.
function isValidPhoneNumber(Phone)
{
	var ch	=	Phone.charAt(0);

	if(!isInteger(Phone))
		return false;
    if(Phone.length !=8) 
		return false;
	if(Phone==("00000000")) 
		return false;
	if(ch=='0')
		return false;
	return true;
}




function StrOfBag(Str, bag)
{
	var i;
    var lStrOfBag = true;
    for (i = 0; i < Str.length; i++)
	{ 
        var c = Str.charAt(i);
        if (bag.indexOf(c) == -1){
			lStrOfBag = false;
			break;
		}
	}
    return lStrOfBag;
}



//  Email Validation function.
function ValidEmail(Str)
{

	if ((Str == null) || (Str.length < 6)) {
		return false;
	}

	var AmpPos = Str.indexOf("@");

	if (AmpPos < 2) {
		return false;
	}

	var firstChar = Str.substring(0,1);
	if  ((firstChar >= "0") && (firstChar <= "9")){
		return false;
	}


	if (( firstChar == ".") || ( firstChar == "-") || ( firstChar == "_")){
		return false;
	}

	var bag = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-@.";
	
	if ( !StrOfBag(Str, bag) ){
		return false;
	}

	var lastChar = Str.substring((Str.length-1),Str.length);

	if ((lastChar == ".") || (lastChar == "-") || (lastChar == "_") || (lastChar == "@")){
		return false;
	}
	
	var beforeAmp = Str.substring(0,AmpPos);

	var afterAmp = Str.substring((AmpPos +1), Str.length);

	if (afterAmp.length < 2){
		return false;
	}

	if (afterAmp.indexOf("@") > -1){
		return false;
	}

	var DotPos = afterAmp.indexOf(".");
	
	var beforeDot = afterAmp.substring(0, DotPos);

	if (beforeDot.length < 2){
		return false;
	}
	
	var afterDot = afterAmp.substring((DotPos +1), afterAmp.length)

	if (afterDot.length < 2){
		return false;
	}

	return true;

}



//Email Address Validation 1
function isEmailAddr(email)
{
  var result = false;
  var str = new String(email);
  var index = str.indexOf("@");

  if (index > 0)
  {
    var pindex = str.indexOf(".",index);
    if ((pindex > index+1) && (str.length > pindex+1))
	result = true;
  }
  return result;
}



//Email Address Validation 2
function ValidateEmailAddress(email)
{
	var at="@";
	var dot=".";
	var str=email.value;
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (!isEmailAddr(str))
	{
		alert("Please enter E-mail ID as: yourname@yourdomain.com");
		email.focus();
	    return false;
	}
   
	if(lstr < 3)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID.");
		email.focus();
		return false;
	}

	if (str.indexOf(at)==-1)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID");
		email.focus();
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID");
		email.focus();
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID");
		email.focus();
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID");
		email.focus();
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID");
		email.focus();
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID");
		email.focus();
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		alert("Invalid E-mail ID. Please re-enter E-mail ID");
		email.focus();
		return false;
	}

	return true;
}



// Validation Script for Setting the focus when the form loads.
function onLoadSetFocus(obj)
{
	  obj.focus();
	   return true;
}

