//supporting function for the Function BiggerDate
function doCompare(d1,d2)
{
	if (d1 == d2)
	{
		return 0
	}
	if (d1 > d2)
	{
		return 1
	}
	else
		return 2
}

//this function accept 2 dates in dd/mm/yyyy format only and return 
// 0 if both dates are equal
// 1 if first is bigger
// 2 if second is bigger
function BiggerDate(date1,date2)
{
	temp1= new String(date1)
	temp2= new String(date2)
	y1=parseInt(temp1.substring(6,10),10)
	y2=parseInt(temp2.substring(6,10),10)
	m1=parseInt(temp1.substring(3,5),10)
	m2=parseInt(temp2.substring(3,5),10)
	d1=parseInt(temp1.substring(0,2),10)
	d2=parseInt(temp2.substring(0,2),10)

	switch  (doCompare(y1,y2))
		{
			case 0:
					switch  (doCompare(m1,m2))
					{
						case 0:
								switch (doCompare(d1,d2))
								{
									case 0:
											return 0
											break
									case 1:
											return 1
											break
									case 2:
											return 2
											break
								}
								break
							case 1:
									
									return 1
									break
							case 2:
									return 2
									break
					}
					break
			case 1:
					return 1
					break
			case 2:
					return 2
					break
		}
}

//function for testing Numeric
function isNumeric(temp)
{
	var str=new String()
	str=temp
	bag = new String("0123456789.")
	for (i=0;i<str.length  ;i++ )
	{
		if (bag.indexOf(str.charAt(i)) == -1)
		{
			return false
		}
	}
	return true
}

// valid date
function isDate(dd, mm, yyyy)
{
	if ((dd.charAt(0)) == "0")
	{
		dd = dd.substring(1);
	}	
	if ((mm.charAt(0)) == "0")
	{
		mm = mm.substring(1);
	}
	if (yyyy.length != 4)
		return false;	
	var dt = new Date(yyyy, mm-1, dd);
	if (dt == "NaN") 
	{	    
	    return false;
	}
	else if (dt.getFullYear() != parseInt(yyyy) || dt.getMonth() != (parseInt(mm)-1) || dt.getDate() != parseInt(dd)) 
		return false;
	else
		return true;		
}

//function for testing alphabetic
function isText(temp)
{
	var str=new String()
	if (isBlank(temp))
	{
		return false
	}

	str=temp
	bag = new String(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.()-_,!%&*")
	for (i=0;i < str.length   ;i++ )
	{
		if (bag.indexOf(str.charAt(i)) == -1)
		{
			return false
		}
	}
	return true
}

//function for testing password
function isPassword(temp)
{
	var str=new String()
	if (isBlank(temp))
	{
		return false
	}

	str=temp
	bag = new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456790!@#$%^&*-_=+")
	for (i=0;i < str.length   ;i++ )
	{
		if (bag.indexOf(str.charAt(i)) == -1)
		{
			return false
		}
	}
	return true
}

//function for testing blank
function isBlank(str)
{
	if (str == "")	
		return true
	else
		return false
}

//function for testing valid email
function isEmail(str)
{
	var bag = new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-")
	//spaces not allowed
	if (str.indexOf(" ") != -1) 
	{
		return false
	}
	// @ character is must
	if (str.indexOf("@") == -1) 
	{
		return false
	}
	// . character is must
	if (str.indexOf(".") == -1) 
	{
		return false
	}
	// atleast 3 characters after @ 
	if (str.length - str.indexOf("@") <= 3 ) 
	{
		return false
	}
	// next to @ must not be .
	if (str.charAt(str.indexOf("@")+1 ) == "." )
	{
		return false
	}
	// last char must not be .
	if (str.charAt(str.length-1) == ".")
	{
		return false
	}
	// only chars in bag are allowed in email
	for (i=0;i < str.length  ;i++ )
	{
		if (bag.indexOf(str.charAt(i)) == -1)
		{
			return false
		}
	}
	return true	
}
// this function checks for a valid time
// str must be pass in the 24 hrs hh:mm format
function isTime(str)
{
	var hrs
	var min
	var bag=new String("0123456789:")
	
	if (str.length != 5)
	{
		return false
	}
	for (i=0;i < str.length  ;i++ )
	{
		if (bag.indexOf(str.charAt(i)) == -1)
		{
			return false
		}
	}
	hrs = parseInt(str.substring(0,2))
	min = parseInt(str.substring(3,5))
	if (hrs > 23) 
		return false
	if (min > 59)
		return false
		
	return true
}
// CHECKING FOR ALPHABET VALUES

function isnotalpha(str)
{
 var re = /[^a-z\. ]/i;
 
 return (re.test(str));        
}


// CHECKING FOR NUMERIC VALUES

function isnotnum(str)
{
  var re = /[^\d]/; 
 
  return (re.test(str));
}



// DATE VALIDATIONS

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";

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 strMonth=dtStr.substring(0,pos1)
	var strDay=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 : mm/dd/yyyy")
		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 (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strYear.length != 4 || year==0){
		alert("Please enter a valid 4 digit year")
		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(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }


