//This Script Contains
//1) Date Validations
//2) isAlphabetic
//3) isSpecialchar
//4) chkSpaces
//5) emailCheck

/////////////D A T E - F U N C T I O N //////////////////////////////
/////Here we have to pass two date fields as (dd-mm-yyyy) format///////

var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function compareDates(sd,ed)
{
	var pos1=sd.indexOf(dtCh)
	var pos2=sd.indexOf(dtCh,pos1+1)
	var strStartDay=sd.substring(0,pos1)
	var strStartMonth=sd.substring(pos1+1,pos2)
	var strStartYear=sd.substring(pos2+1)
	strSYr=strStartYear
	if (strStartDay.charAt(0)=="0" && strStartDay.length>1)	strStartDay=strStartDay.substring(1)
	if (strStartMonth.charAt(0)=="0" && strStartMonth.length>1)	strStartMonth=strStartMonth.substring(1)
	for (var i = 1; i <= 3; i++)
	{
		if (strSYr.charAt(0)=="0" && strSYr.length>1)
		strSYr=strSYr.substring(1)
	}
	smonth=parseInt(strStartMonth)
	sday=parseInt(strStartDay)
	syear=parseInt(strSYr)
		
	var pos11=ed.indexOf(dtCh)
	var pos22=ed.indexOf(dtCh,pos11+1)
	var strEndDay=ed.substring(0,pos11)
	var strEndMonth=ed.substring(pos11+1,pos22)
	var strEndYear=ed.substring(pos22+1)
	strEYr=strEndYear
	if (strEndDay.charAt(0)=="0" && strEndDay.length>1)	strEndDay=strEndDay.substring(1)
	if (strEndMonth.charAt(0)=="0" && strEndMonth.length>1)	strEndMonth=strEndMonth.substring(1)
	for (var i = 1; i <= 3; i++) 
	{
		if (strEYr.charAt(0)=="0" && strEYr.length>1)	strEYr=strEYr.substring(1)
	}
	emonth=parseInt(strEndMonth)
	eday=parseInt(strEndDay)
	eyear=parseInt(strEYr)

	if(eyear < syear)
	{
		alert("End date should be beyond the startdate");
		return false;
	}
	else if(eyear > syear)
	{
	}
	else if(eyear =syear)
	{
		if(emonth >= smonth)
		{
			if(emonth > smonth)	{}
			else if(eday > sday)
			{
			}
			else
			{
				alert("End date should be beyond the	startdate");
				return false;
			}
		}
		else{
			alert("End date should be beyond the startdate");
			return false;
		}
	}	
	return true;
}

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1)
			returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 ==
0))) ? 29 : 28 );
}

function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1)		strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1)	strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1)	strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
	{
		alert("The date format should be : dd-mm-yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 &&	day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		alert("Please enter a valid 4 digit year between "+minYear+"	and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 ||	isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert("Please enter a valid date")
		return false
	}
	return true
}

function ValidateForm(sDate,eDate)
{
	var sdt=sDate;		//document.frmSample.txtStartDate;
	var edt =eDate; 	//document.frmSample.txtEndDate;
	if(sdt.value != "")	{
		 if (isDate(sdt.value)==false){
			sdt.focus()
			return false
		}
	}
	else { 
		alert("Please enter the Start Date ");
		sdt.focus();
		return false;
	}
	if(edt.value != ""){
		if(isDate(edt.value)==false){
			edt.focus();
			return false
		}
	}
	else {
		alert("Please enter the End Date ");
		edt.focus()
		return false;
	}
	if(compareDates(sdt.value,edt.value)== false)
	{
		edt.focus();
		return false;
	}

  return true;
}


function daysBetween(date1, date2)
{
	if (date1.indexOf("-") != -1) { date1 = date1.split("-"); } else if (date1.indexOf("/") != -1) { date1 = date1.split("/"); } else { return 0; }
	if (date2.indexOf("-") != -1) { date2 = date2.split("-"); } else if (date2.indexOf("/") != -1) { date2 = date2.split("/"); } else { return 0; }

	if (parseInt(date1[0], 10) >= 1000)
	{
		var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
	}
	else if (parseInt(date1[2], 10) >= 1000)
	{
		var sDate = new Date(date1[2]+"/"+date1[0]+"/"+date1[1]);
	}
	else
	{
		return 0;
	}

	if (parseInt(date2[0], 10) >= 1000)
	{
		var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
	}
	else if (parseInt(date2[2], 10) >= 1000)
	{
		var eDate = new Date(date2[2]+"/"+date2[0]+"/"+date2[1]);
	}
	else
	{
		return 0;
	}

	var one_day = 1000*60*60*24;
	var daysApart = Math.abs(Math.ceil((sDate.getTime()-eDate.getTime())/one_day));

	return daysApart;
}

/////////////////// ALPHABETIC - F U N C T I O N //////////////////////////////

function isAlphabetic(sText)
{
   var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_. ";
   var IsAlpha=true;
   var Char;

   for (i = 0; i < sText.length && IsAlpha == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsAlpha = false;
         return false;
         }
      }
   return IsAlpha;
}

/////////////////// SPECIAL CHARACTOR - F U N C T I O N //////////////////////////////
function isSpecialchar(sText)
{
   var inValidChars = "[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\\~\=\|]/";
   var IsAlpha=true;
   var Char;

   for (i = 0; i < sText.length && IsAlpha == true; i++)
      {
      Char = sText.charAt(i);
      if (inValidChars.indexOf(Char) > 0)
         {
         IsAlpha = false;
         return false;
         }
      }
   return IsAlpha;
}

/////////////////// SPACES CHECKING - F U N C T I O N //////////////////////////////
function chkSpaces(fldVal) {
    var isEmpty = false;
    var tmpVal  = "";

        for (x=0; x<fldVal.length; x++) {
        if (fldVal.substring(x, x+1) != " ") {
            tmpVal = fldVal.substring(x, x+1);
                }
        }

        if (tmpVal.length > 0) {
                isEmpty = true;
        }

        return isEmpty;
}
/////////////////// SPECIAL CHAR CHECKING - F U N C T I O N //////////////////////////////
function specialChar(sText)
{
   var ValidChars = "[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\\~\=\|]/";
   var IsAlpha=true;
   var Char;


   for (i = 0; i < sText.length && IsAlpha == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) > 0)
         {
         IsAlpha = false;
         return false;
         }
      }
   return IsAlpha;
}
/////////////////// EMAIL CHECKING - F U N C T I O N //////////////////////////////
function emailCheck(sText)
{
	var isEmail=true;
	if(sText.value=="")
	{
		  alert("Please enter a value for\"Email\"field.");
	  	  isEmail=false;
		  return isEmail;
	}
	
	if (sText.value != "")
	{
                var str=sText;
                var filter=/^.+@.+\..{2,3}$/

	        if (!filter.test(str))
		{
                        alert("Please enter valid Email");
			isEmail=false;
	                return isEmail;
	        }
	}
return isEmail;
}


/////////////////// Only Date n month Validation - Funtion //////////////////////////////

function isMonthnYear(dtStr)
{

	var pos1=dtStr.indexOf(dtCh)

	var strMonth=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos1+1)
	var numMonth = isInteger(strMonth) // new added
	var numYear = isInteger(strYear)  //new added

	strYr=strYear

	if (strMonth.charAt(0)=="0" && strMonth.length>1)	strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0)=="0" && strYr.length>1)	strYr=strYr.substring(1)
	}

	var month=parseInt(strMonth)

	var year=parseInt(strYr)
	
	//new addition started
	
	if(!numMonth)
	{
		alert("Date Should be in Numerics");
		return false
	}
	if(!numYear)
	{
		alert("Please Enter Valid Date");
		return false
	}
	
	//new addition ended


	if (pos1==-1)
	{
		alert("The date format should be : mm-yyyy")
		return false
	}

	if (strMonth.length<1 || month<1 || month>12)
	{
		alert("Please enter a valid month")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		alert("Please enter a valid 4 digit year between "+minYear+"	and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 ||	isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert("Please enter a valid date")
		return false
	}
	return true
}




