/**************************************************************************************/
/************************ Convert The Number In Decimal **************************
	// This function takes two arguments:
	//   (string || number)  val
	//   (integer)  places
	//   (string)  sep
	// val is the numeric string or number to parse
	// places represents the number of decimal
	// places to return at the end of the parse.
	// sep is an optional string to be used to separate
	// the whole units from the decimal units (default: '.')
	// Convert No 12.00568 to 12.01 

/**************************************************************************************/
function checkText_Number(str)
{
	 
	for (var i = 0; i < str.length; i++)
	{      
			var ch = str.substring(i, i + 1);
			     
			if ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)&& (ch < "0" || "9" < ch) && ch != "+" && ch != "-" && ch != " ")
			{   
				return false;         
			}      
	}   
		return true;
}

	
function parseDecimal(val,places,sep) {

	val = '' + val;
		// Implicitly cast val to (string)
	
	if (!sep) {
		sep = '.';
		// If separator isn't specified, then use a decimal point '.'
	}
	
	if (!places) { places = 0; }
	places = parseInt(places);
		// Make sure places is an integer
	
	if (!parseInt(val)) {
		// If val is null, zero, NaN, or not specified, then
		// assume val to be zero.  Add 'places' number of zeros after
		// the separator 'sep', and then return the value.  We're done here.
		val = '0';
		if (places > 0) {
			val += sep;
			while (val.substring((val.indexOf(sep))).length <= places) {
				val += '0';
			}
		}
		return val;
	}
	
	if ((val.indexOf('.') > -1) && (sep != '.')) {
		val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
			// If we're using a separator other than '.' then convert now.
	}
		
	if (val.indexOf(sep) > -1) {
		// If our val has a separator, then cut our value
		// into pre and post 'decimal' based upon the separator.
		pre = val.substring(0,val.indexOf(sep));
		post = val.substring(val.indexOf(sep)+1);
	} else {
		// Otherwise pre gets everything and post gets nothing.
		pre = val;
		post = '';
	}
	
	if (places > 0) {
		// If we're dealing with a decimal then...
		
		post = post.substring(0,(places+1));
			// We care most about the digit after 'places'
		
		if (post.length > places) {
			// If we have trailing decimal places then...
			
			//alert (parseInt(post.substring(post.length - 1)));

			if ( parseInt(post.substring(post.length - 1)) > 4 ) {
				post = '' + Math.round(parseInt(post) / 10);
				//post = '' + post.substring(0,post.length - 2) + (1/Math.pow(10,places));
				//post = ('' + post.substring(0,post.length - 2)) + (parseInt(post.substring(post.length - 1)) + 1);
			} else {
				post = '' + Math.round(parseInt(post));
			}
		}
		
		if (post.length > places) {
			post = '' + Math.round(parseInt(post.substring(0,places)));
		} else if (post.length < places) {
			while (post.length < places) {
				post += '0';
			}
		}
	
	} else {

		if (parseInt((post.substring(0,1))) > 4) {
			pre = '' + (parseInt(pre) + 1);
		} else {
			pre = '' + (parseInt(pre));	
		}
		post = '';
	}
	
	sep = (post.length > 0) ? sep : '';
		// Should we use a separator?
	
	val = pre + sep + post;
		// Rebuild val

	return val;
}

/*
 * This 	Method is for checking whether the user input exceeds the limit in TextAres
 *
 */

 //onKeyDown="textLimit(this.form.Comments,this.form.remLen,225);" 
// onKeyUp="textLimit(this.form.Comments,this.form.remLen,225);"

function textLimit(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
	countfield.value = maxlimit - field.value.length;
}

/*
 * This 	Method is for checking whether the user input is string or not
 *
 */

function checkString(str){
	
	for (var i = 0; i < str.length; i++){      
			var ch = str.substring(i, i + 1);
			     
			if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)))
			{         
				return false;         
			}      
		}   
		return true;
}


/*
 * This Method is for checking whether the user inputs a string  or ' ' or '.'  or '-'
 *
 */

function checkText(str){
	 
	for (var i = 0; i < str.length; i++){      
			var ch = str.substring(i, i + 1);
			     
			if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ' && ch!="-" && ch!="."){         
				return false;         
			}      
		}   
		return true;
}

/*
 * This 	Method is for checking whether the user inputs a number or not
 *
 */

function checkNumber(str){
	
	for (var i = 0; i < str.length; i++){      
			var ch = str.substring(i, i + 1);      
			if ((ch < "0" || "9" < ch))
				{         
				return false;         
			}      
	}
	return true;
}





function CheckNumericValue(e)
{
   var key;
   key = e.which ? e.which : e.keyCode;
   if ((key>=48 && key<=57) || key == 40 || key==41 || key == 44 || key ==8 || key==32 || key == 45 || key == 46)
       {return true;}
   else
   {     
       return false;
   }
}

function CheckNumberOnly(e)
{
   var key;
   key = e.which ? e.which : e.keyCode;
   if (key>=48 && key<=57) 
       {return true;}
   else
   {     
       return false;
   }
}

function BlockNumericValue(e)
{    

   var ckey;
   ckey = e.which ? e.which : e.keyCode;
   if ((ckey>=65 && ckey<=90) || (ckey>=97 && ckey<=122) || ckey==46 || ckey==32 || ckey==8) 
       {return true;}
   else
   {       
       return false;
   }
}
function CheckZipCode(e)
{
   var key;
   key = e.which ? e.which : e.keyCode;
   if ((key>=48 && key<=57) || key == 44 || key ==8 || key==32 || key == 45 || key == 46 || (key>=65 && key<=90) || (key>=97 && key<=122))
       {return true;}
   else
   {       
       return false;
   }
}

function Num_Alpha(e)
{
   var ckey;
   ckey = e.which ? e.which : e.keyCode;
   if ((ckey>=65 && ckey<=90) || (ckey>=48 && ckey<=57) || (ckey>=97 && ckey<=122) || ckey==46 || ckey==32 || ckey==8) 
       {return true;}
   else
   {       
       return false;
   }   
}

function checkQNumber(str){
	
	for (var i = 0; i < str.length; i++){      
			var ch = str.substring(i, i + 1);      
			if ((ch < "0" || "9" < ch) && (ch != "+") && (ch != "-") && (ch != "(") && (ch != ")"))
				{         
				return false;         
			}      
	}
	return true;
}

/*
 * This Method is for checking whether the user inputs a decimal value
 *
 */

function checkDecimal(str)
{   
	
	for (var i = 0; i < str.length; i++){      
		var ch = str.substring(i, i + 1);      
		if ((ch < "0" || "9" < ch) && ch != '.')
		{         
			
			return false;         
		}      
	}   
	return true;   
}

/*
 * This Method is for checking whether the user inputs a alpha numeric string
 *
 */

function checkAlphaNumeric(str)
{
	 
	for (var i = 0; i < str.length; i++)
	{      
			var ch = str.substring(i, i + 1);
			     
			if ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)&& (ch < "0" || "9" < ch) && ch != "+" && ch != "-" && ch != " " && ch != "." && ch != "," && ch != "\"" && ch != "'")
			{   
				return false;         
			}      
	}   
		return true;
}


function checkDate(str)
{
for (var i = 0; i < str.length; i++)
	{      
			var ch = str.substring(i, i + 1);
			if ((ch < "0" || "9" < ch) && (ch != "/"))
			{
				return false;
			}
	}
return true;
}

function checkAddress(str)
{
	 
	for (var i = 0; i < str.length; i++)
	{      
			var ch = str.substring(i, i + 1);
	
			if (ch == "'")
			{       
				return false;         
			}      
	}   
	
		return true;
}

//ravi
//email start...
				
function checkEmail(emailStr)
				{
						
						/* The following variable tells the rest of the function whether or not
						to verify that the address ends in a two-letter country or well-known
						TLD.  1 means check it, 0 means don't. */

						var checkTLD=1;

						/* The following is the list of known TLDs that an e-mail address must end with. */

						var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

						/* The following pattern is used to check if the entered e-mail address
						fits the user@domain format.  It also is used to separate the username
						from the domain. */

						var emailPat=/^(.+)@(.+)$/;

						/* The following string represents the pattern for matching all special
						characters.  We don't want to allow special characters in the address. 
						These characters include ( ) < > @ , ; : \ " . [ ] */

						var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

						/* The following string represents the range of characters allowed in a 
						username or domainname.  It really states which chars aren't allowed.*/

						var validChars="\[^\\s" + specialChars + "\]";

						/* The following pattern applies if the "user" is a quoted string (in
						which case, there are no rules about which characters are allowed
						and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
						is a legal e-mail address. */

						var quotedUser="(\"[^\"]*\")";

						/* The following pattern applies for domains that are IP addresses,
						rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
						e-mail address. NOTE: The square brackets are required. */

						var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

						/* The following string represents an atom (basically a series of non-special characters.) */

						var atom=validChars + '+';

						/* The following string represents one word in the typical username.
						For example, in john.doe@somewhere.com, john and doe are words.
						Basically, a word is either an atom or quoted string. */

						var word="(" + atom + "|" + quotedUser + ")";

						// The following pattern describes the structure of the user

						var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

						/* The following pattern describes the structure of a normal symbolic
						domain, as opposed to ipDomainPat, shown above. */

						var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

						/* Finally, let's start trying to figure out if the supplied address is valid. */

						/* Begin with the coarse pattern to simply break up user@domain into
						different pieces that are easy to analyze. */

						var matchArray=emailStr.match(emailPat);

						if (matchArray==null) {

						/* Too many/few @'s or something; basically, this address doesn't
						even fit the general mould of a valid e-mail address. */

						//alert("Email address seems incorrect (check @ and .'s)");
						return false;
						}
						var user=matchArray[1];
						var domain=matchArray[2];

						// Start by checking that only basic ASCII characters are in the strings (0-127).

						for (i=0; i<user.length; i++) {
						if (user.charCodeAt(i)>127) {
						alert("Ths username contains invalid characters.");
						return false;
						   }
						}
						for (i=0; i<domain.length; i++) {
						if (domain.charCodeAt(i)>127) {
						alert("Ths domain name contains invalid characters.");
						return false;
						   }
						}

						// See if "user" is valid 

						if (user.match(userPat)==null) {

						// user is not valid

						alert("Invalid Email Id.");
						return false;
						}

						/* if the e-mail address is at an IP address (as opposed to a symbolic
						host name) make sure the IP address is valid. */

						var IPArray=domain.match(ipDomainPat);
						if (IPArray!=null) {

						// this is an IP address

						for (var i=1;i<=4;i++) {
						if (IPArray[i]>255) {
						alert("Destination IP address is invalid!");
						return false;
						   }
						}
						return true;
						}

						// Domain is symbolic name.  Check if it's valid.
						 
						var atomPat=new RegExp("^" + atom + "$");
						var domArr=domain.split(".");
						var len=domArr.length;
						for (i=0;i<len;i++) {
						if (domArr[i].search(atomPat)==-1) {
						alert("The domain name does not seem to be valid.");
						return false;
						   }
						}

						/* domain name seems valid, but now make sure that it ends in a
						known top-level domain (like com, edu, gov) or a two-letter word,
						representing country (uk, nl), and that there's a hostname preceding 
						the domain or country. */
/*
						if (checkTLD && domArr[domArr.length-1].length!=2 && 
						domArr[domArr.length-1].search(knownDomsPat)==-1) {
						alert("The address must end in a well-known domain or two letter " + "country.");
						return false;
						}
*/
						// Make sure there's a host name preceding the domain.

						if (len<2) {
						alert("This address is missing a hostname!");
						return false;
						}

						// If we've gotten this far, everything's valid!
						return true;
						}
						
//  End -->

//Ravi
function checkUserID(str)
{
	for (var i = 0; i < str.length; i++)
	{      
			var ch = str.substring(i, i + 1);
			if ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch < "0" || "9" < ch) && ch != "-" && ch != "_")
			{   
				return false;         
			}      
	}   
		return true;
}

//Swaroop

//Date Validation

function ChkMonthDays(MonIndex,SelDay,SelYear)
	{
		if(MonIndex==4 || MonIndex==6 || MonIndex==9 || MonIndex==11)
		{
			if(parseInt(SelDay)==31)
			{
				return false;
			}
		} 
		if(MonIndex==2)	//For Feb.
		{
			if((parseInt(SelYear) % 4)!=0)
			{
				if(parseInt(SelDay)>28)
				{
					return false;
				}
			}
			else if((parseInt(SelYear) % 400)==0)
			{
				if(parseInt(SelDay)>29)
				{
					return false;
				}
			}
			else if((parseInt(SelYear) % 100)==0)
			{
				if(parseInt(SelDay)>28)
				{
					return false;
				}
			}
			else
			{
				if(parseInt(SelDay)>29)
				{
					return false;
				}
			}
		}
		return true;
	}



function y2k(number) { return (number < 1000) ? number + 1900 : number; }

// To calculate the date differeence and no of days between 2 dates 

function daysElapsed(date1,date2) {

    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0) - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
//	alert(difference);
	return difference/1000/60/60/24;
 
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


 function CheckDecimalno(source, arguments)
    { 
        var s =arguments.Value;
        var isDecimal_re     = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/; 
        if(String(s).search (isDecimal_re) == -1 )
        {
                arguments.IsValid = false;
                return;
        }
        return;
    }

