//********************************************************//
//********************************************************//
//*********This JS File has the following fuction to check
//*********1.isText
//*********2.isEmail
//*********3.isNumber
//*********4.isPassword
//*********5.isDate
//*********6.isPhone
//*********7.isUserID
//*********8.IsValidTime
//********************************************************//
//********************************************************//


function isText(txtObj)
{   
	var str = txtObj.value;   
	// Return false if name field is blank.   
	if (str == "")
	{   
		alert("\nThis field is blank.\n\nPlease enter the value.")      
		txtObj.focus();      
		return false;      
	}   
	// Return false if characters are not a-z, A-Z, or a space and a dot.   
	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 != ',' && ch != ')' && ch != '(' && ch != '-')
		{         
			alert("\nThis field accepts only alphabets, space, dot & spaces.\n\nPlease re-enter !!.");
			txtObj.select();         
			txtObj.focus();         
			return false;      
		}      
	}   
}




function isEmail(mailfield)
{
	var mail=mailfield.value
	// Looking for a @,.//Looking for @ in the last position//looking for . in the first position
	if (mail.indexOf("@") == -1 || mail.indexOf("@")+1 == mail.length || mail.indexOf(".") == -1 || mail.indexOf(".")+1 == mail.length  || mail.substring(0,1) == ".")
 	{
 		 	alert("Invalid Email ID")
			mailfield.focus()
			mailfield.select()
     		return false;
		}
 		else
 		{
			//looking for . just before & after @		//looking for @ for the first position
			var n = mail.indexOf("@")
			var n1 = mail.indexOf(".",n)
 		 	var n2 = mail.lastIndexOf(".",n)
			if(n+1 == n1 || n-1 == n2)
 		 	{
 		 		alert("Invalid Email ID")
				mailfield.focus()
				mailfield.select()
				return false;
	 		}
	 	}
		//Looking for two consecutive .'s		//Looking for special characters
		var k=0
 		for(var j=0;j<mail.length;j++)
 		{
			if((mail.substring(j,j+1) >= 'a' && mail.substring(j,j+1) <= 'z') || (mail.substring(j,j+1) >= 'A' && mail.substring(j,j+1) <= 'Z') || (mail.substring(j,j+1) >= '0' && mail.substring(j,j+1) <= '9') || mail.substring(j,j+1) == '.' || mail.substring(j,j+1) == '@' || mail.substring(j,j+1) == '_')
 		 	{}
 		 	else
 		 	{
 		 		alert("Invalid Email ID")
				mailfield.focus()
				mailfield.select()
				return false;
			}
			if(mail.substring(j,j+1) == ".")
			{
				if(k+1 == j && j!=1)
				{
 		 			alert("Invalid Email ID")
					mailfield.focus()
					mailfield.select()
					return false;		
				}
				k = j
			}	
		}
		// looking for .,@ at the last position
		if(mail.lastIndexOf(".")+1 == mail.length || mail.lastIndexOf("@")+1 == mail.length)
		{
 		 	alert("Invalid Email ID")
			mailfield.focus()
			mailfield.select()
			return false;
		}
		//looking for more than one @
		if(mail.indexOf("@") != mail.lastIndexOf("@"))
		{
 		 	alert("Invalid Email ID")
			mailfield.focus()
			mailfield.select()
			return false;
		}
return true;
}



function isNumber(txtObj)
{
	var x=txtObj.value
	

		var anum=/(^\d+$)|(^\d+\.\d+$)/
		if (anum.test(x))
			return true
		else
		{
			alert("Please Enter a Valid Number!")
			txtObj.focus()
			txtObj.select()
			return false
		}
	
}



function isPassword(objpw1,objpw2)
{
	pw1 = objpw1.value;
	pw2 = objpw2.value;

	pw1 = objpw1.value;
	str = "' "
	for (i=0;i<=pw1.length-1;i++)
		{
		ch = pw1.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{
			if (ch==" ")
				alert("Space is not allowed");			
			else
				alert(ch + "   is not allowed");
			objpw1.focus();
			objpw1.select();
			return false;	
			}
		}
	if (pw1.length < 4)
	{
		alert("Password should be alteast 4 characters")
		objpw1.focus()
		objpw1.select()
		return false;
	}
	pw2 = objpw2.value;
	str = "' "
	for (i=0;i<=pw2.length-1;i++)
		{
		ch = pw2.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{
			if (ch==" ")
				alert("Space is not allowed");			
			else
				alert(ch + "   is not allowed");
			objpw2.focus();
			objpw2.select();
			return false;	
			}
		}
	if (pw1 != pw2)
	{
		alert ("\nPassword should be same. Please re-enter your password.")
		objpw2.focus()
		objpw2.select()
		return false;
	}
}

//
//isDate function starts here
//
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(dtObj)
{
    var dtStr=dtObj.value;
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2100;
	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 isPhone(txtObj)
{
	str = "0123456789+,-()"
	val = txtObj.value
	for (i=0;i<=val.length-1;i++)
		{
		ch = val.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{}
		else
			{
			alert("Enter a valid Phone Number\n");
			txtObj.focus();
			txtObj.select();
			return false;	
			}
		}
}



function isUserID(txtObj)
{
var str = txtObj.value;
if((str.substring(0,1)<"a" || str.substring(0,1)>"z") && (str.substring(0,1)<"A" || str.substring(0,1)>"Z"))
	{
	alert("The Field should begin with an alphabetic character.");
	txtObj.focus();
	txtObj.select();
	return false;
	}
for (var i = 1; 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 != '_')) 
	{
	alert("\nThe field accepts letters,numbers & underscore.\n\nPlease re-enter");
	txtObj.focus();
	txtObj.select();
	return false;
	}
	}
}
 
 
 
function isDigit(theDigit) 
{ 
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

for (j = 0; j < digitArray.length; j++) 
{if (theDigit == digitArray[j]) 
return true 
} 
return false 

} 
 
 
function isPositiveInteger(theString) 
{ 
var theData = new String(theString) 

if (!isDigit(theData.charAt(0))) 
if (!(theData.charAt(0)== '+')) 
return false 

for (var i = 1; i < theData.length; i++) 
if (!isDigit(theData.charAt(i))) 
return false 
return true 
} 
 
 
function isDate(s,f) 
{var a1=s.split("/"); 
var a2=s.split("-"); 
var e=true; 
if ((a1.length!=3) && (a2.length!=3)) 
{ 
e=false; 
} 
else 
{if (a1.length==3) 
var na=a1; 
if (a2.length==3) 
var na=a2; 
if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
{ if (f==1) 
{var d=na[1],m=na[0]; 
} 
else 
{var d=na[0],m=na[1]; 
} 
var y=na[2]; 
if (((e) && (y<1000)||y.length>4)) 
e=false 
if (e) 
{ 
v=new Date(m+"/"+d+"/"+y); 
if (v.getMonth()!=m-1) 
e=false; 
} 
} 
else 
{ 
e=false; 
} 
} 
return e 
} 
function checkDate(v) 
{ 
var s=v.a.value; 
if (isDate(s,0)) //dd/mm/yyyy format 
alert("The inputted date value is valid!"); 
else 
alert("The inputted date value is not valid!"); 
return false; 
} 


function IsValidTime(timeStr)
{
    var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
    var matchArray = timeStr.match(timePat);
    if (matchArray == null) {
    alert("Time is not in a valid format.");
    return false;
    }
    hour = matchArray[1];
    minute = matchArray[2];
    second = matchArray[4];
    ampm = matchArray[6];

    if (second=="") { second = null; }
    if (ampm=="") { ampm = null }

    if (hour < 0  || hour > 23) {
    alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
    return false;
    }
    if (hour <= 12 && ampm == null) {
    if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
    alert("You must specify AM or PM.");
    return false;
       }
    }
    if  (hour > 12 && ampm != null) {
    alert("You can't specify AM or PM for military time.");
    return false;
    }
    if (minute<0 || minute > 59) {
    alert ("Minute must be between 0 and 59.");
    return false;
    }
    if (second != null && (second < 0 || second > 59)) {
    alert ("Second must be between 0 and 59.");
    return false;
    }
    return true;
}
 
