function numbersonly(e,a)
{	
	var unicode=e.charCode? e.charCode : e.keyCode	
	if (unicode!=8 && unicode!=46 && unicode!=9 && unicode!=13)
	{ 
		if (unicode<48||unicode>57) //if not a number
		{
			alert("Please enter numeric value only.");
			return false ;//disable key press
		}
		
	}
}

function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}
// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}
/********************Blank space Validation*******************/
function dotandspace(txtboxvalue)
{
 var flag=0;
 var strText = txtboxvalue;
 if (strText!="")
 {
 var strArr = new Array();
 strArr = strText.split(" ");
 for(var i = 0; i < strArr.length ; i++)
 {
 if(strArr[i] == "")
 {
  flag=1;              
  break;
 }
 } 
 if (flag==1)
 {
 //alert("No space is allowed");
 return false;
  }
 }        
}

/**********Search Box**************/
function srch()
{
	if(document.searchfrm.searchtxt.value=='')
	{
		document.searchfrm.searchtxt.focus();
		return false;
	}
	if(dotandspace(document.searchfrm.searchtxt.value)==false)
	{
		document.searchfrm.searchtxt.focus();
		return false;
	}

}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
function isAllCharacters(objValue)
{
		var characters="' -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."
		var tmp
		var lTag
		lTag = 0
		temp = (objValue.length)
		for (var i=0;i<temp;i++)
		{
			tmp=objValue.substring(i,i+1)
			if (characters.indexOf(tmp)==-1)
			{
				lTag = 1
			}
		}
		if(lTag == 1)
			return false
		else
			return true
}

/*************************************************************************************************************
*
* isValidEmail it checks the Email-Id is Valid or Not 
*
**************************************************************************************************************/
	function isValidEmail(emailStr)
{
		if(emailStr=="")
		{
				alert("Please enter email address.");
				 return false;
				
		}
			/* 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="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
			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. "sg 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. sg@[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 sg.sg@somewhere.com, sg and sg 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("Opps.. it looks like you didn't enter a valid email address. Please correct your email and try again.")
				return false
			}
			var user=matchArray[1]
			var domain=matchArray[2]

			// See if "user" is valid 
			if (user.match(userPat)==null) {
			    // user is not valid
			    alert("Opps.. it looks like you didn't enter a valid email address. Please correct your email and try again.")
			    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("Opps.. it looks like you didn't enter a valid email address. Please correct your email and try again.")
					return false
				    }
			    }
			    return true
			}

			// Domain is symbolic name
			var domainArray=domain.match(domainPat)
			if (domainArray==null) {
			alert("Opps.. it looks like you didn't enter a valid email address. Please correct your email and try again.")
			    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>5) {
			   // the address must end in a two letter or three letter word.
			   alert("Opps.. it looks like you didn't enter a valid email address. Please correct your email and try again.")
			   return false
			}

			// Make sure there's a host name preceding the domain.
			if (len<2) {
			   var errStr="Opps.. it looks like you didn't enter a valid email address. Please correct your email and try again."
			  // alert(errStr)
			   return false
			}

			// If we've gotten this far, everything's valid!
			return true;
}
/************************************************Valid Url******************************************************************************/


function validUrl(url)                                 // Link Tracker : To check whether URL is Valid OR Not
{
  var i;
  var result=1;
  var urlPart;
  var urlCheck=url;
 
  if(urlCheck.length<5)
	 result=2;

  urlPart=urlCheck.split(":");

  if(urlPart[0]=="http" || urlPart[0]=="https")
	{
	 
	  urlPart=urlPart[1].split(".");
	 if((urlPart[urlPart.length-1].length < 2) || urlPart.length < 2)//|| (urlPart[urlPart.length-2].length < 2)
		  result=2;
      else
		  result=1;
	}
  else
	{
	  result=2;
	}

	if(result==2)
		return false;
	else 
	 {
		return true;
 	 }
 }


function isAllNumerics(objValue)
{
		var characters="0123456789."
		var tmp
		var lTag
		lTag = 0
		temp = (objValue.length)
		//alert(objValue);
		for (var i=0;i<temp;i++)
		{
			tmp=objValue.substring(i,i+1)
			if (characters.indexOf(tmp)==-1)
			{
				lTag = 1
			}
		}
		if(lTag == 1)
			return false
		else
		  if(objValue< 0)
		   {
			return false
		   }else{ 	
			return true
		  }
}

function checkDecimal(val_id)
{
	number = document.getElementById(val_id).value;
  decallowed = 2;  
   if (number.indexOf('.') == -1) number += ".";
  	dectext = number.substring(number.indexOf('.')+1, number.length);

   if (dectext.length > decallowed)
   {  
	document.getElementById("error").style.display="";
	document.getElementById("error").innerHTML="Enter a number with up to 2 decimal places.";
	document.getElementById(val_id).className = 'errorInput';
	document.getElementById(val_id).focus();
      //alert ("Enter a number with up to 2 decimal places.");
	document.getElementById(val_id).value= number.substring(0, number.length-1); 
      return false;
   }
   else
   {
     return true;
   }
}

 /// developed by Neha Shira... 28th Nov 2009
//function isValidDate(dateStr) 
function isValidDate_1(day, month, year)
{
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	/*
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		alert("Date is not in a valid format.")
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	*/
	//alert(day); alert(month); alert(year);
	if (day=="" || month =="" || year=="") { // check month range
		document.getElementById('errDate').innerHTML = "&nbsp;&nbsp;<img src='../../templates/default/images/icons/cancel.png'/> Please select proper date.";
		return false;
	}
	if (month < 1 || month > 12) { // check month range
		document.getElementById('errDate').innerHTML = "&nbsp;&nbsp;<img src='../../templates/default/images/icons/cancel.png'/> Month must be between 1 and 12.";
		return false;
	}
	if (day < 1 || day > 31) {
		document.getElementById('errDate').innerHTML = "&nbsp;&nbsp;<img src='../../templates/default/images/icons/cancel.png'/> Day must be between 1 and 31.";
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		document.getElementById('errDate').innerHTML = "&nbsp;&nbsp;<img src='../../templates/default/images/icons/cancel.png'/> Selected month doesn't have 31 days!";
		return false
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
		document.getElementById('errDate').innerHTML = "&nbsp;&nbsp;<img src='../../templates/default/images/icons/cancel.png'/> February " + year + " doesn't have " + day + " days!";
			return false;
		}
	}
	return true;  // date is valid
}
//  End -->
/***************************************date function************************************************************/
function isValidDate(checkDate)              // Check for valid date
{
	var datePart,datePart1,result,flag;
	now = new Date(); 
	var year =  now.getFullYear();
	var month = now.getMonth();
	month = month + 1;
	if(month<10) 
		month="0"+month;
	var nowdate = now.getDate();
	if(nowdate<10) 
		nowdate="0"+nowdate;
	var todaysdate = year+"-"+month+"-"+nowdate;
	if(checkDate!="") // Process : Check Date Field Is Not Blank
  	{
		datePart1=checkDate.split(' ');
		if(datePart1[0].indexOf("-")>0)    // Process : To Check Date Is Separated by . or / or - characters only
			var sp_char="-";
		else if (datePart1[0].indexOf("/")>0)
			var sp_char="/";
		else if (datePart1[0].indexOf(".")>0)
			var sp_char=".";
		else
		{
			alert("Please Enter Date in Specified format");
			return false;
		}
		datePart=datePart1[0].split(sp_char);
		var userdate = datePart[0]+"-"+datePart[1]+"-"+datePart[2];
		if(todaysdate>=userdate)
		{
			//alert("Please enter future date only");
			return false;
		}
		else
		{
			if(datePart.length==3)
			{	
				if(datePart[1].length==1)
				{
          				alert("Please enter month in 'mm' format. e.g. 02 or 07");
					return false;
        			}
				if(datePart[2].length==1)
				{
					alert("Please enter date in 'dd' format. e.g. 02 or 07");
					return false;
				}
			}
			else
			{
				alert("Please Enter Date in Specified format");
				return false;
			}
			if(datePart[1]=="08")
				datePart[1]="8";
			if(datePart[1]=="09") 
				datePart[1]="9";
			if(datePart[2]=="08")
				datePart[2]="8";
			if(datePart[2]=="09") 
				datePart[2]="9";
			if((Number(datePart[0])) && (Number(datePart[1])) && (Number(datePart[2]))) //Process : Whether Date Entered Has Numeric Entries 
			{
				if( (parseInt(datePart[0])<2101) && (parseInt(datePart[0])>year-1) && (parseInt(datePart[1])<13) && (parseInt(datePart[1])>0) && (parseInt(datePart[2])<32) && (parseInt(datePart[2])>0) ) //Process : Range Of Date Values
				{
          				if(parseInt(datePart[1])==2) // Process : Check For February Month	
					{
            					if(parseInt(datePart[2]) > 28)
						{
              						if((parseInt(datePart[0]) % 4 !=0)) //Leap Year Checking 
							{
								alert("Year "+parseInt(datePart[0]) +" : February 28 Days Only!");
								return false;
							}
					              else if(parseInt(datePart[2]) == 29)
					              {
								var subdatePart=datePart[0];
                subdatePart = subdatePart.substring(2);
                if(subdatePart=="00")        //check for centuary years not divisible by 400
                {

                  if((parseInt(datePart[0]) % 400 !=0))
                  {
                    alert("No Leap Year "+parseInt(datePart[0]) +" : February 28 Days Only!");
                    return false;
                  }
                  else
                    return true;

                }
                else
                  return true;

                alert("Leap Year "+parseInt(datePart[0]) +" : February 29 Days Only!");
                return false;
              }
              else if(parseInt(datePart[2]) > 29)
              {
                alert("February Month Cannot Have more than 29 days !");
                return false;
              }

            }
            else
		return true;

          } //End Of February Check

          else if((parseInt(datePart[1])==4 || parseInt(datePart[1])==6 || parseInt(datePart[1])==9 || parseInt(datePart[1])==11 ) && datePart[2] > 30)// || parseInt(datePart[1])==6 || parseInt(datePart[1])==9 || parseInt(datePart[1])==11)) //Check For Apr,Jun,Sep,Nov.
          {
		alert("This Month Has 30 Days"); 
		return false;
          }
          else
		return true;

        }
        else
        {
          alert('Invalid Year OR Month OR Day value!');
          return false;
        }
      }
      else   //Execute : When Date Field Is With Irrelavent Data [Like Char,special Char etc other than numbers ]
      {
        alert('Please Enter date in specified format.');
        return false;
      }
      return true;
    } 
  }
  else   // Execute : When Date Field Is Blank
  {
    alert('Date field cannot be left blank.');
    return false;
  }
}

