
// Use this function only in conditional expression just to check whether string is empty or not
function Trim(Untrimmed) 
{
	var Trimmed = ''
	for (var i = 0; i < Untrimmed.length; i++) 
	{
		if (Untrimmed.charCodeAt(i)!=32) 
			{
				Trimmed +=Untrimmed[i]
			}
	}
	return Trimmed
}


/*this function is used to check the validity of date*/
function isValidDate(dateStr) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY
	// Also separates date into month, day, and year variables
		
		var strError = "";
		var today ,tempDate;
		var daysMinus=-7;
		 today=new Date();
		 tempDate=new Date();
		// to check the date pattern for dd/mm/yy
		var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/;
		// To require a 4 digit year entry, use this line instead:
		//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2,4})$/;
		var matchArray = dateStr.match(datePat); // is the format ok?
		if (matchArray == null) 
		{
			strError = "Date is not in a valid format (mm/dd/yyyy)"; // -1
			return strError;
		}
		month = matchArray[1]; // parse date into variables
		day = matchArray[3];
		year = matchArray[4];
		if (month < 1 || month > 12) 
		{ // check month range
			strError = "Month must be between 1 and 12."; //-2
			return strError;
		}
		if (day < 1 || day > 31) 
		{
			strError = "Day must be between 1 and 31."; //-3
			return strError;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		{
			strError = "Month "+month+" doesn't have 31 days!"; //-4
			return strError;
		}
		if (month == 2) 
		{ // check for february 29th
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) 
			{
				strError = "February " + year + " doesn't have " + day + " days!"; //-5
				return strError;
			}
		}
		if (year < 1926) 
		{
				strError = "Year must be greater than equal to 1926!"; //-5
				return strError;
		}
		dateStr=new Date(dateStr);
		return strError;  // date is valid
}

//  This function checks whether given EmailId is valid or not
function emailCheck(emailStr) 
{
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"

  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"

  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

  var matchArray=emailStr.match(emailPat)
  if (matchArray==null) 
  {
     return false
  }
  var user=matchArray[1]
  var domain=matchArray[2]

  if (user.match(userPat)==null) 
  {
     // user is not valid
     //alert("The username doesn't seem to be valid.")
     //field.focus();   
     return false
  }

  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) 
 {
      for (var i=1;i<=4;i++) 
     {
        if (IPArray[i]>255) 
        {
     //        alert("Destination IP address is invalid!")
     //        field.focus();   
               return false
        }
     }
    return true
 }

  var domainArray=domain.match(domainPat)
  if (domainArray==null) 
  {
    //alert("The domain name doesn't seem to be valid.")
    //field.focus();   
    return false
  }

  var atomPat=new RegExp(atom,"g") 
  var domArr=domain.match(atomPat)
  var len=domArr.length
  if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) 
  {    
    //alert("The address must end in a three-letter domain, or two lettercountry.")
    //field.focus();   
    return false
 }

  if (len<2) 
 {
    var errStr="This address is missing a hostname!"
    //alert(errStr)
    //field.focus();   
    return false
 }
 return true;
}


//  This function will check whether given string is numeric or not
function IsNumeric(checkStr)
{
	var checkOK1 = "0123456789";
	var allValid = true;
	var allNum = "";
	
	for (i = 0; i < checkStr.length; i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0; j < checkOK1.length; j++)
				{
					if (ch == checkOK1.charAt(j))
						break;
				}
			if (j == checkOK1.length)
				{
					allValid = false;
					break;
				}
			allNum += ch;
		}		

	return allValid ;		
}

// Use this function to check whether the specified date is greater that today's date or not
function isFutureDate(strDate){
  var today=new Date();
  var tempDate = new Date(strDate);
  
  if (tempDate > today) {
	  return true ;
	}
	
  return false ;
}
  
// Function checks whether the passed string contains special characters or not //

	function containsSpecialCharacters(prefix)
	{
		var specialCharacters = ['\\','/','?',':','|','<','>','*','\"','%','^','$','+','(',')'];
		if(prefix == null) return false;
				
		for(i=0;i<specialCharacters.length;i++)
		{
			if(prefix.indexOf(specialCharacters[i])!=-1){
				return true;
			}
		}
		return false; 
	}

//Function is used to trim the string and return the untrimmed string it combines the //
// functionality of LTrim and Rtrim									  //

	function trimString(str){
		for(i=0;i<str.length; i++)
		{
			if(str.charAt(i) != ' '){
				break;	
			}
		}
		str = str.substring(i);
		for(i=str.length-1;i>=0; i--)
		{
			if(str.charAt(i) != ' '){
				break;
			}
		}
	
		str = str.substring(0,i+1)
		return str;
	}
	
	function isZipCodeValid(strCheck){	
		var expPattern1 = "^[0-9][0-9\-]*[0-9]$" ;
		var expPattern2 = "[\-][\-]" ;
		var result1 = strCheck.match(expPattern1);		
		var result2 = strCheck.match(expPattern2);		
		if (result1 == null){
			return false ;
		}
		if (result2 == null){
			return true ;
		}
		return false ;
	}
	
	function isPhoneNumberValid(strCheck){
		var expPattern1 = "^[0-9][0-9\-]*[0-9]$" ;
		var expPattern2 = "[\-][\-]" ;
		var result1 = strCheck.match(expPattern1);		
		var result2 = strCheck.match(expPattern2);		
		if (result1 == null){
			return false ;
		}
		if (result2 == null){
			return true ;
		}
		return false ;
	}
	
	/*This function validates that the given string has at the max two digits after decimal point*/
	function isDecimal(checkStr)
	{
		
		var expPattern1 = "^[0-9]*[\.]{0,1}[0-9]{0,2}$" ;
		var expPattern3 = "^[0-9]{1,3}[\.]{1}$" ;
	
		var result1 = checkStr.match(expPattern1) ;
		var result3 = checkStr.match(expPattern3) ;
	
		if (result1 == null)
		{
			return false ;
		}
		if (result3 == null)
		{
			return true ;
		}
		return false ;
	}

	/*This function validates that the price*/
	function isValidPercentage(checkStr)
	{
		
		var expPattern1 = "^[0-9]{1,3}[\.]{0,1}[0-9]{0,2}$" ;
		var expPattern3 = "^[0-9]{1,3}[\.]{1}$" ;
	
		var result1 = checkStr.match(expPattern1) ;
		var result3 = checkStr.match(expPattern3) ;
	
		if (result1 == null)
		{
			return false ;
		}
		if (result3 == null)
		{
			if (checkStr > 100){
				return false ;
			}
			return true ;
		}
		return false ;
	}
	