/*****************************************************************************************/
// General Functions
/*****************************************************************************************/
function trimSpaces(stringValue) {
        // Checks the first occurance of spaces and removes them
        for(i = 0; i < stringValue.length; i++) {
                if(stringValue.charAt(i) != " ") {
                        break;
                }
        }
        if(i > 0) {
                stringValue = stringValue.substring(i);
        }

        // Checks the last occurance of spaces and removes them
        strLength = stringValue.length - 1;
        for(i = strLength; i >= 0; i--) {
                if(stringValue.charAt(i) != " ") {
                        break;
                }
        }
        if(i < strLength) {
                stringValue = stringValue.substring(0, i + 1);
        }

        // Returns the string after removing leading and trailing spaces.
        return stringValue;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a string is a valid email address.
/////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(emailString) {
        splitVal = emailString.split('@');

        if(splitVal.length <= 1) {
                alert("Please enter a valid email address");
                return false;
        }
        if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
                alert("Please enter a valid email address");
                return false;
        }

        splitDomain = splitVal[1].split('.');
        if(splitDomain.length <= 1) {
                alert("Please enter a valid email address");
                return false;
        }
        if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
                alert("Please enter a valid email address");
                return false;
        }
		if (!CheckValidity(emailString.length,emailString,'E'))
		 {
			alert("The field 'Email ID' should not accept the character [<,>] and Double quotes.");
			return false;
		 }
        return true;
}


/////////////////////////////////////////////////////////////////////////////////////////
// Checks For the Cross scripting.
/////////////////////////////////////////////////////////////////////////////////////////

function CheckValidity(slen,stext,stype)
	{  
       for (var i = 0; i < slen; i++)
		{      
       var ch = stext.substring(i, i + 1);  
       if (stype=='E')  // for E mail
		  {
		   if (((ch>="a") && (ch<="z")) || ((ch>="A") && (ch<="Z")) ||
			  (ch=="@") || (ch==".") || (ch=="_") || ((ch>=0) && (ch<=9)))	
			 {continue;}	
			else
				{return false ;}
           }
		else if ((stype=='N') || (stype=='R'))   // For Name,Country,Profession,City/State 
		  { 
			 if (((ch>="a") && (ch<="z")) || ((ch>="A") && (ch<="Z")) ||
				(ch==' '))	
				{continue ;}
			 else
				{ return false;}
		  }
		else if ((stype=='A') ||  (stype=='M'))  // For Address ,Message,Country
		    { 
			  if ((ch==">") || (ch=="<") || (stext.charCodeAt(i)==34))    
				{return false;}
			  else
				{continue;}
		  }
		else if ((stype=='P') || (stype=='Z')) // For Phone,Zip Code
		  { 
			 if (((ch>="a") && (ch<="z")) || ((ch>="A") && (ch<="Z")) ||
				(ch=="-") || ((ch>=0) && (ch<=9)) || (ch==","))	
				{ continue;}
			 else
				{return false;}
		  }
		else if (stype=='U')  // for username,password
		  {
			if (((ch>="a") && (ch<="z")) || ((ch>="A") && (ch<="Z"))||
				(ch=="_")||	(ch=="-") || (ch=="*") || ((ch>=0) && (ch<=9)))
				{ continue; }
			else
				{ return false; }
		  }
		else if (stype=='L')  // for URL Links
		  {
			 if (((ch>="a") && (ch<="z")) || ((ch>="A") && (ch<="Z")) ||
				(ch=="-") || ((ch>=0) && (ch<=9)) || (ch=="."))	
				{ continue; }
			else
				{ return false; }
		  }
      }	
      return true ;
    }
