﻿//Clearing date format text
function fnClear(obj){
	if (obj.value == "mm/dd/yyyy"){
		obj.value = "";		
	}
}
//Checking the date format and reset the text if invalid date format occurs
function fnReset(obj)
{
	if(obj.value == "") obj.value = "mm/dd/yyyy";
}
//Replacing the unnecessary character in specified text
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
//-----Date Validation ------	
	// Declaring valid date character, minimum year and maximum year
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2030;
	
	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 chkDate(obj){
		var daysInMonth = DaysArray(12)
		var pos1=obj.indexOf(dtCh)
		var pos2=obj.indexOf(dtCh,pos1+1)
		var strMonth=obj.substring(0,pos1)
		var strDay=obj.substring(pos1+1,pos2)
		var strYear=obj.substring(pos2+1)
		strYr=strYear
			if (strDay.charAt(0)=="0" && strDay.length>1 && strDay.length<3) strDay=strDay.substring(1)
			if (strMonth.charAt(0)=="0" && strMonth.length>1 && strMonth.length<3) 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: mm/dd/yyyy");
				return false;
			}
			if (strMonth.length<1 || month<1 || month>12 ||strMonth.length>2){
				alert("Please enter a valid month.");
				return false;
			}
			if (strDay.length<1||strDay.length>2 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
				alert("Please enter a valid day.");
				return false;
			}
						
			if ((strYear.length != 2) && (strYear.length != 4 || year==0 || year<minYear || year>maxYear)){
				if (strYear.length == 4) alert("Please enter a valid year (1900-2030).");				
				else alert("Please enter a valid year.");				
				return false;
			}	
			
			if (obj.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(obj, dtCh))==false){
				alert("Please enter a valid date.");
				return false;
			}
			return true;
	}
//function Compare Today with Entered Date
function CompareFromToDates(FDate,TDate)
{Fromdate = new Date(FDate);			
 Todate = new Date(TDate)			
 var frmDate= ChangeDate(Fromdate);			
 var tDate=ChangeDate(Todate);
 if (Date.parse(frmDate) > Date.parse(tDate)) return -1;//From Date is Greater than ToDate
 else if(Date.parse(frmDate) < Date.parse(tDate)) return 1;//From Date is less than ToDate
 else if(Date.parse(frmDate) == Date.parse(tDate)) return 0;//Both Dates are Equal
}
//function Compare Today with Entered Date
function CompareToday(EDate)
{
 currdate = new Date();			
 userdate = new Date(EDate)			
var cntDate= ChangeDate(currdate);			
var usrDate=ChangeDate(userdate);
if (Date.parse(cntDate) > Date.parse(usrDate)) return -1; //Entered Date is Less than Current Date 
else if(Date.parse(cntDate) < Date.parse(usrDate)) return 1; // Entered Date is Greater than Current Date 
else if(Date.parse(cntDate) == Date.parse(usrDate)) return 0;//Both Dates are Equal 
}
//Change to mm/dd/yyyy format
function ChangeDate(CDate)
{CngDate = new Date(CDate)			
	if (CngDate.getYear() >= 0 && CngDate.getYear() <= 30){
		var ChangedDate= (CngDate.getMonth()+1)+"/"+CngDate.getDate()+"/"+(CngDate.getYear()+2000);
	}
	else if (CngDate.getYear() >= 31 && CngDate.getYear() <= 99){
		var ChangedDate= (CngDate.getMonth()+1)+"/"+CngDate.getDate()+"/"+(CngDate.getYear()+1900);
	}
	else{
		var ChangedDate= (CngDate.getMonth()+1)+"/"+CngDate.getDate()+"/"+(CngDate.getYear());
	}
	return ChangedDate;
}

//---------------------------
// Validate Email
function chkEmailIds(obj, msg)
{
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/;
	var strEmail = obj.value;
	strEmail = strEmail.replace(/;/g,",");
	var strMailId = "";		
	if(strEmail.indexOf(",") != -1)
	{
		var splitEmail = strEmail.split(",");
		for (var x=0; x<splitEmail.length; x++)
		{
			strMailId = splitEmail[x];
			strMailId = strMailId.replace( /^\s+/g,'').replace(/\s+$/g,'');
			
			for (var z=x+1; z<splitEmail.length; z++)
			{
				if(strMailId == splitEmail[z].replace( /^\s+/g,'').replace(/\s+$/g,''))
				{
					if(msg =="E-Mail") alert("Email address " + strMailId + " is already entered. \nPlease enter a different email address.");
					else alert("Email address " + strMailId + " is already entered. \nPlease enter a different " + msg + " email address.");
					obj.focus();
					return false;
				}
			}
			
			if(!regex.test(strMailId))
			{
					alert("Please enter a valid " + msg );
					obj.focus();
					return false;
			}
			var strEmailDomain = splitEmail[x].split("@");
			strEmailDomain = strEmailDomain[1].split(".");
				
			/*for (var z=1; z < strEmailDomain.length; z++)
			{
				strEmailDomain[z] = strEmailDomain[z].replace( /^\s+/g,'').replace(/\s+$/g,'');
				if ((strEmailDomain[z].length < 2) || (strEmailDomain[z].length > 3))
				{
					alert("Please enter a valid " + msg + " email address.");		
					obj.focus();
					return false;
				}
			}*/
				
		} 				
	}
	else if(obj.value.length > 0 )
	{	
		strMailId = strEmail.replace( /^\s+/g,'').replace(/\s+$/g,'');
		if(!regex.test(strMailId))
		{
			alert("Please enter a valid " + msg);		
			obj.focus();
			return false;
		}
		var strEmailDomain = strEmail.split("@");
		
		strEmailDomain = strEmailDomain[1].split(".");
		/*for (var y=1; y <strEmailDomain.length; y++)
			{	
				strEmailDomain[y] = strEmailDomain[y].replace( /^\s+/g,'').replace(/\s+$/g,'');
				
				if ((strEmailDomain[y].length < 2) || (strEmailDomain[y].length > 3))
				{
					alert("Please enter a valid " + msg + " email address.");		
					obj.focus();
					return false;
				}
				
			}*/
	}
	return true;
}
