/*
        :::::::::::::::::::::::: COPYRIGHT ::::::::::::::::::::::::::

		(c) copyright 2002 - Infolever , info@infolever.com
				http://www.infolever.com
				
        :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::  
*/

// Intercept Javascript
// Javascript source to SO ISL Institute

function isValidDate(dateStr) {

// Checks for the following valid date formats:
// (YY/MM/DD YYYY/MM/DD) YY-MM-DD YYYY-MM-DD
// Also separates date into year, month and day variables

// To require a 2 digit year entry, use this line:
// var datePat = /^(\d{2})(\/|-)(\d{2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
//var datePat = /^(\d{2})(\/|-)(\d{2})\2(\d{4})$/;
var datePat = /^(\d{4})(-)(\d{2})\2(\d{2})$/;

var matchArray = dateStr.match(datePat); //is the format OK?
if (matchArray == null) {
alert("Date is not in a valid format.\nShould be (YYYY-MM-DD)");
return false;
}

//month = matchArray[1]; //parse date into variables
//day = matchArray[3];
//year = matchArray[4];
year = matchArray[1]; //parse date into variables
month = matchArray[3]; 
day = matchArray[4];


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 + " does not 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 + " does not have " + day + " days!");
return false;
	}
}
return true; //date is valid
}


function isValidTime(timeStr) {

// Checks for the following valid time formats:
// hh:mm
// Also separates time into hour and minute variables

var timePat = /^(\d{2})(\:)(\d{2})$/;

var mArray = timeStr.match(timePat); //is the format OK?
if (mArray == null) {
alert("Time is not in a valid format.\nShould be (hh:mm)");
return false;
}

//hour = matchArray[1]; //parse date into variables
//minute = matchArray[3];
hour = mArray[1]; //parse date into variables
minute = mArray[3];

if (hour < 0 || hour > 23) { //check hour range
alert("Hour must be between 0 and 23.");
return false;
}
if (minute < 0 || minute > 59) { //check minute range
alert("Minute must be between 0 and 59.");
return false;
}
return true; //time is valid
}

function TextCheck(textStr,field) {
//Checks for non valid characters

var char1 = /[a-zA-Z]{1}/;
var char2 = /[0-9]{1}/;

var check1 = textStr.search(char1);
var check2 = textStr.search(char2);

if ((check1 != -1) || (check2 != -1)){
return true
}
else {
alert("The field " + field + " is required")
return false
	}
}

function TextCheck2(textStr,field) {
//Checks for non valid characters (2 digits)

var char = /^[0-9]{1,2}$/;
var check = textStr.search(char);

if (check != -1){
return true
}
else {
alert("The field \"" + field + "\" is required\nCan only contain digits")
return false
	}
}

function TextCheck4(textStr,field) {
//Checks for non valid characters (4 digits)

var char = /^[0-9]{1,4}$/;
var check = textStr.search(char);

if (check != -1){
return true
}
else {
alert("The field \"" + field + "\" is required\nCan only contain digits")
return false
	}
}

function TestNumeric(numStr,field,fieldfocus,fieldselect) {
//Checks for non valid characters
if (numStr==""){
	alert("This is a numeric field\nCan only contain digits")
	fieldfocus
	fieldselect
	return false
	}

	if (!isNaN(numStr)) {
	return true
	}
	
	else {
	alert("This is a numeric field\nCan only contain digits")
	fieldfocus
	fieldselect
	return false
	}
}





function EmailCheck(emailStr) {
//Checks if the email address is valid
var emailPat1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
//var emailPat2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
//var emailPat2 = /^[a-zA-Z0-9\-\.]+[a-zA-Z0-9\-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
var emailPat2 = /^[a-zA-Z0-9\-\.]+[a-zA-Z0-9\-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/; // valid

if (!emailPat1.test(emailStr) && emailPat2.test(emailStr)) { // if syntax is valid
return true;
 }
if (emailStr.length != 0) {
	alert("Your email address:\n" + emailStr + "\nSeems incorrect. Please try again\n(Check the '@' and '.'s in the email address)");
	}
else {
	alert("Your email address seems incorrect. Please try again\n(Check the '@' and '.'s in the email address)");
	}
return false;
}

function changeto(highlightcolor){
highlightcolor='lightgrey'
source=event.srcElement
if (source.tagName=="TR"||source.tagName=="TABLE")
return
while(source.tagName!="TR")
source=source.parentElement
if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore")
source.style.backgroundColor=highlightcolor
}

function changeback(originalcolor){
originalcolor='white'
if (event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore")
return
if (event.toElement!=source)
source.style.backgroundColor=originalcolor
}


