﻿// JavaScript Document

function checkRequired(elemId, msgStr){
	elem = document.getElementById(elemId);
	if (elem.value == ""){
		alert(msgStr);
		elem.focus();
		return false;
	}
	return true;
}

function isCorrectDate(elemId, msgStr){
	var nowDate = new Date();
	
	elem = document.getElementById(elemId);
	if (elem.value < nowDate){
		alert(msgStr);
		elem.focus();
		return false;
	}
	return true;
}


function isDate(elemId) {
	dateStr = document.getElementById(elemId).value
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	var day
	var month
	var year
	
	if (matchArray == null) {
		alert("Please enter date in the format dd/mm/yyyy.");
		return false;
	}
	
	//month = matchArray[1]; // p@rse date into variables
	//day = matchArray[3];
	//year = matchArray[5];
	
	month = dateStr.substring(3,5)
	day = dateStr.substring(0,2)
	year = dateStr.substring(6,10)
	
	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

function isTime(elem){
	var time = document.getElementById(elem).value
	if(time.length != 5){
	    alert("Please enter time in format hh:mm");
		return false;
	}else{
		//chck first char is a number
		if(!isNaN(time.charAt(0))){
			//check second car is a number
			if(!isNaN(time.charAt(1))){
				//check third char is a colon
				if(time.charAt(2) == ":"){
					//check ourth char is a number
					if(!isNaN(time.charAt(3))){
						//check fifth char is a number
						if(isNaN(time.charAt(4))){
							alert("Please enter time in format hh:mm");
							return false;
						}//end fifth char check
					}else{
						alert("Please enter time in format hh:mm");
						return false;
					}//end fourth char check
				}else{
					alert("Please enter time in format hh:mm");
					return false;
				}//end third char check
			}else{
				alert("Please enter time in format hh:mm");
				return false;
			}//end second char check
		}else{
			alert("Please enter time in format hh:mm");
			return false;
		}//end first char check
	}//end main if
	return true;
}//end function