/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var tmCh= ":";
var minYear=1900;
var maxYear=2100;

function isBlank(s){
//	alert(s);
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (c != " ") {
			return true;
		}
	}
	alert("This field can't be blank");
    return false;
}

function isInteger(s){
	var i;
	alert (s.length);
	if (s.length != 0) {
	for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) {
			alert("Please enter an integer : "+s);
			return false;
		}
    }
	}
    return true;
}

function isNumber(s){
	var i;
	var j;
//	alert(s);
	if (s.length != 0) {
	var c = s.charAt(0);
	if (((c >= "0") && (c <= "9")) || (c == '.') || (c == "+") || (c == "-")) {
		j=0;
	}
	else {
		alert("Please enter a number : "+s);
		return false;
	}
    for (i = 1; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c >= "0") && (c <= "9")) || (c == '.')) {
			if (c==".") j++;
			if (j==2)
			{
				alert("Please enter a number : "+s);
				return false;
			}
		}
		else {
			alert("Please enter a number : "+s);
			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 daysInMonth(i) {
	var n = 31
	if (i==4 || i==6 || i==9 || i==11) {n = 30}
	if (i==2) {n = 29}
	return n
}

function isTime(tmStr){
//	var daysInMonth = DaysArray(12)
	var i;
    var hr;
	var min;
	var pos1=tmStr.indexOf(tmCh)
	var strHour=tmStr.substring(0,pos1)
	var strMin=tmStr.substring(pos1+1,tmStr.length)
//	alert(strHour+":"+strMin)
	if (strHour.charAt(0)=="0" && strHour.length>1) 
		strHour=strHour.substring(1)
	if (strMin.charAt(0)=="0" && strMin.length>1) 
		strMin=strMin.substring(1)

    for (i = 0; i < strMin.length; i++){   
        // Check that current character is number.
        var c = strMin.charAt(i);
        if (((c < "0") || (c > "9"))) {
			alert("Please Enter Valid Minute : "+strMin);
			return false;
		}
    }
	hr=parseInt(strHour);
	min=parseInt(strMin);
//	alert(hr+":"+min+" "+pos1);
	if (pos1==-1){
		alert("The time format should be : HH:MM")
		return false
	}
	if (strMin.length<1 || min<0 || min>60){
		alert("Please enter a valid minute: "+strMin)
		return false
	}

	if (strHour.length<1 || hr<0 || hr>23){
		alert("Please enter a valid Hour: "+strHour)
		return false
	}
    return true
}

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,dtStr.length)
//	alert(strDay+"-"+strMonth+"-"+strYear)
	strYr=strYear
	if (dtStr.length != 0) {
	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(strYear);
//	alert(day+"-"+month+"-"+year)
//	alert(strMonth);
	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: "+strMonth)
		return false
	}
//	alert("c"+daysInMonth(2))
	if (strDay.length<1 || day<1 || day>31 || day > daysInMonth(month)){
// || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]		alert(Day);
		alert("Please enter a valid day: "+strDay)
		return false
	}
//	alert("c"+strDay+"-"+strMonth+"-"+strYear+" "+strHour+":"+strMin)
	strYr=strYear
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear+": "+strYear);
		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(x){
	var dt
	dt=x.deadline.value
	var timed
	timed=x.TIMED.value
//	alert(timed=='N')
	if (timed=='N') {
      return true
	}
	if (isDate(dt)==false){
//		dt.focus()
		return false
	}
    return true
 }

