// This set of function are general includes for validation
// They are designed in pairs the validation and the event function
// the event function will call the validation with the event src

function display_name(item) {
	var strDisplay = item.getAttribute("DisplayName");
	if (strDisplay==null || strDisplay=="")
		strDisplay="Field";
	return strDisplay;
}

function default_value(item) {
	// The code below is commented to avoid default value of the item to be restored if erroneous. 
	var strDefault = "";  //item.defaultValue;
	if (strDefault==null || strDefault=="")
		strDefault="";
	return strDefault;
}

function trim_string() {
	var ichar, icount;
	var strValue = this;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function date_toSimpleForm() {
	var toSimpleForm = new String;
	toSimpleForm = this.toLocaleString();
	toSimpleForm = toSimpleForm.substring(0,toSimpleForm.indexOf(' '));
	return toSimpleForm;
}


function es_non_blank() {
	var item = event.srcElement;
	event.returnValue = vs_non_blank(item);
}
function vs_non_blank(item) {
//	var strErrorMsg = display_name(item) + " must have a non-blank value";
	item.value=item.value.Trim();
	if (item.value.length==0) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
//		alert(strErrorMsg);
		return false;
	}
	item.style.backgroundColor = "";
	return true;
}


function es_valid_number() {
	var item = event.srcElement;
	event.returnValue = vs_valid_number(item);
}

function vs_valid_number(item) {
	return check_valid_number(item, false);
}

function es_valid_decimalnumber() {
	var item = event.srcElement;
	event.returnValue = vs_valid_decimalnumber(item);
}

function vs_valid_decimalnumber(item) {
	return check_valid_number(item, true);
}

function check_valid_number(item, bDecimal) {
//	var strErrorMsg = display_name(item) + " must be a valid numeric";
	var strDefault = default_value(item);
	if (strDefault.length==0) {
		//strDefault="0"; ? This line is commented as if the field is blank then 0 is assigned to that field which is logically wrong.
	}
	item.value=item.value.Trim();
	if (item.value.length==0)
		item.value=strDefault;
	if (bDecimal)
		var num = ".0123456789";
	else
		var num = "0123456789";
	
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		if (num.indexOf(item.value.charAt(intLoop)) == -1) {
			item.focus();
			item.style.backgroundColor = "#ffc0c0";
//			alert(strErrorMsg);
			return false;
		}
	}
	if (item.value.indexOf(".")!=item.value.lastIndexOf(".")) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
//		alert(strErrorMsg);
		return false;
	}
	item.style.backgroundColor = "";
	return true;
}

function es_valid_hours() {
	var item = event.srcElement;
	event.returnValue = vs_valid_hours(item);
}
function vs_valid_hours(item) {
//	var strErrorMsg = display_name(item);
	if (!vs_valid_decimalnumber(item))
	{
		item.style.backgroundColor = "#ffc0c0";
		return false;
	}
	var itemValue = new Number(item.value);
	if ((itemValue < 0 || itemValue > 80)) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
//		alert(strErrorMsg + " must have a value from 0 to 80 hours");
		return false;
	}
	itemValue *= 4;
	if ((itemValue)!=Math.ceil(itemValue)) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
//		alert(strErrorMsg + " must be a valid quartely increment");
		return false;
	}
	item.style.backgroundColor = "";
	return true;
}


function es_valid_date() {
	var item = event.srcElement;
	event.returnValue = vs_valid_date(item);
}

function vs_valid_date(item) {
	var month,day,year,varDate;
	varDate = item.value;
	var DateSeperator;
	
	DateSeparator = ReturnMeSeparator(varDate);
		
	day = varDate.substr(0,varDate.indexOf(DateSeparator))
	month = varDate.substring(varDate.indexOf(DateSeparator) + 1,varDate.lastIndexOf(DateSeparator))
	
	if (isNaN(Number(month))) {
		month = MonthNumber(month);
		if (month == '00') {
			item.focus();
			item.style.backgroundColor = "#ffc0c0";
			return false;
		}
	}
	
	year = varDate.substr(varDate.lastIndexOf(DateSeparator)  + 1 ,varDate.length)
	
	if (year.length <= 2) {
		if (parseInt(year) > 25)
			year = 1900 + parseInt(year);
		else
			year = 2000 + parseInt(year);
	}
	
	if ((day <= 31 && day > 0) && (month < 13 && month > 0) && (year <= 2025 && year >= 1900))
	{
		if (!CheckDate(month,day,year))
		{
			item.focus();
			item.style.backgroundColor = "#ffc0c0";
			return false;
		}
	}
	else
	{
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
		return false;
	}
	item.style.backgroundColor = "";
	item.value = LeadZero(Number(day))+ "/"+ LeadZero(Number(month))  + "/" + year.toString();
	return true;
/*
//	var strErrorMsg = display_name(item);
	if (isNaN(Date.parse(item.value))) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
		//alert(strErrorMsg + " must be a valid Date");
		return false;
	}
	var dtItem = new Date(Date.parse(item.value));
	//item.value = dtItem.toSimpleForm(); */
}

function es_item_selected() {
	var item = event.srcElement;
	event.returnValue = vs_item_selected(item);
}
function vs_item_selected(item) {
//	var strErrorMsg = display_name(item) + " must be a valid selection";
	if (item.selectedIndex==0) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
//		alert(strErrorMsg);
		return false;
	}
	item.style.backgroundColor = "";
	return true;
}

function es_valid_zip() {
	var item = event.srcElement;
	event.returnValue = vs_valid_zip(item);
}
function vs_valid_zip(item) {
//	var strErrorMsg = display_name(item) + " must be of the form 99999-9999";
	item.value=item.value.Trim();
	if (!(/^\d{5}$/.test(item.value) || /^\d{5}-\d{4}$/.test(item.value))) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
//		alert(strErrorMsg);
		return false;
	}
	item.style.backgroundColor = "";
	return true;
}

function es_valid_ssnbr() {
	var item = event.srcElement;
	event.returnValue = vs_valid_ssnbr(item);
}
function vs_valid_ssnbr(item) {
//	var strErrorMsg = display_name(item) + " must be of the form 999-99-9999";
	item.value=item.value.Trim();
	if (!(/^\d{3}-\d{2}-\d{4}$/.test(item.value))) {
		item.focus();
		item.style.backgroundColor = "#ffc0c0";
//		alert(strErrorMsg);
		return false;
	}
	item.style.backgroundColor = "";
	return true;
}

function es_valid_email() {
	var item = event.srcElement;
	event.returnValue = vs_valid_email(item);
}
function vs_valid_email(item) {
//	var strErrorMsg = display_name(item) + " is not a valid Email";
	if (item.value == "") return true; 
	// The above line checks for Blank Email address. If so then returns true as validating a blank email 
	//address returns an error
	item.value=item.value.Trim();
	if (!(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(item.value))) {
		//item.focus();
		item.style.backgroundColor = "#ffc0c0";
		//alert(strErrorMsg);
		return false;
	}
	item.style.backgroundColor = "";
	return true;
}

// build the validation object
function validation_setup() {
	this.eventNonBlank = es_non_blank;
	this.nonBlank = vs_non_blank;
	this.eventValidNumber = es_valid_number;
	this.validNumber = vs_valid_number;
	this.eventValidDecimalNumber = es_valid_decimalnumber;
	this.validDecimalNumber = vs_valid_decimalnumber;
	this.eventValidHours = es_valid_hours;
	this.validHours = vs_valid_hours;
	this.eventValidDate = es_valid_date;
	this.validDate = vs_valid_date;
	this.eventItemSelected = es_item_selected;
	this.itemSelected = vs_item_selected;
	this.eventValidZip = es_valid_zip;
	this.validZip = vs_valid_zip;
	this.eventValidSSNbr = es_valid_ssnbr;
	this.validSSNbr = vs_valid_ssnbr;
	this.eventValidEmail = es_valid_email;
	this.validEmail = vs_valid_email;
	return this;
}

// Extend the string object to include a trim function
String.prototype.Trim = trim_string;
// Extend the date object to include a simple form string conversion
Date.prototype.toSimpleForm = date_toSimpleForm;

// Construct the validation object
var validation = new Object;
validation = validation_setup();


// This set of function are for processing the key press event
// Used to restrict input on numerics and pure textual fields

function kp_integer() {
	if ((event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
}
function kp_numeric() {
	if ((event.keyCode != 46) && (event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
	if (event.keyCode == 46) {
		if (event.srcElement.value.indexOf(".") > -1)
			event.returnValue = false;
	}
}


function kp_character() {
	if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122))
		event.returnValue = false;
}
function kp_convert_upper() {
	if ((event.keyCode >= 97 && event.keyCode <= 122))
		event.keyCode -= 32;
}
function kp_convert_lower() {
	if ((event.keyCode >= 65 && event.keyCode <= 90))
		event.keyCode += 32;
}


function kp_setup() {
	this.Integer = kp_integer;
	this.Numeric = kp_numeric;
	this.Character = kp_character;
	this.ConvertUpper = kp_convert_upper;
	this.ConvertLower = kp_convert_lower;
	return this;
}

var keyPressInput = new Object;
keyPressInput = kp_setup();

// the functions below are for validation of Date
function CheckLeapYr(m_year)
{
m_yr = parseInt(m_year);
//1. Divide by 4 and if the REMAINDER is 0 then it is a leap year 
m_leap = m_yr%4;
//2. Divide by 100 and if the REMAINDER is 0 then it is NOT a leap year unless, 3. 
m_leap1=m_yr%100;
//Divide by 400 and if the REMAINDER is 0 then it is a leap year.
m_leap2=m_yr%400;
if (m_leap==0 && (m_leap1 !=0 || m_leap2==0))
	{
		return true;
	}
else
	{
	return false;
	}
}

function CheckDate(c_month,c_day,c_year)
{
	c_leap_yr = CheckLeapYr(c_year);
	if (c_month==2)
	{
		if (c_leap_yr)
		{
			if (c_day > 29)
			{
//				alert("February cannot have more than 29 days in a leap year"+"\n"+"Try changing the day or the month");
				return false;
			}
		}
		else
		{
			if (c_day > 28)
			{
//				alert("February cannot have more than 28 days if not a leap year"+"\n"+"try changing the day, the month or the year");
				return false;
			}
		}
	}
	if ((c_month==9 || c_month==4 || c_month==6 || c_month==11) && c_day>30)
	{
//		alert("Day cannot be greater than 30 for month selected");
		return false;
	}

	return true;
}

function LeadZero(nNum) {
    return (parseInt(nNum)<10 ? "0" : "" ) + parseInt(nNum).toString();
}

function ReturnMeSeparator(DateString) {
	DateString += '';
	DateString = Trim(DateString);
	
	var Separator;
	
	DateString = ReplaceMonthNumber(DateString);
	
	for (var k = 0; k < DateString.length; k++) {
		if (isNaN(Number(DateString.substr(k, 1))) || DateString.substr(k, 1) == ' ') {
			Separator = DateString.substr(k, 1);
			k = DateString.length;
		}
	}

	if (DateString.split(Separator).length == 3) {
		for (var k = 0; k < DateString.length; k++) {
			if (isNaN(Number(DateString.substr(k, 1)))) {
				if (DateString.substr(k, 1) != Separator) {
					return '';
				}
			}
		}
		return Separator;
	}
	else {
		// separators are not entered properly
		return '';
	}
}

function ReplaceMonthNumber(parmDateString) {
	parmDateString = parmDateString.toLowerCase();
	parmDateString = parmDateString.replace('jan', '01');
	parmDateString = parmDateString.replace('feb', '02');
	parmDateString = parmDateString.replace('mar', '03');
	parmDateString = parmDateString.replace('apr', '04');
	parmDateString = parmDateString.replace('may', '05');
	parmDateString = parmDateString.replace('jun', '06');
	parmDateString = parmDateString.replace('jul', '07');
	parmDateString = parmDateString.replace('aug', '08');
	parmDateString = parmDateString.replace('sep', '09');
	parmDateString = parmDateString.replace('oct', '10');
	parmDateString = parmDateString.replace('nov', '11');
	parmDateString = parmDateString.replace('dec', '12');
	return parmDateString;
}

function MonthNumber(parmMonthName) {
	parmMonthName = parmMonthName.toLowerCase();
	switch (parmMonthName) {
		case 'jan' :
			return '01';
		case 'feb' :
			return '02';
		case 'mar' :
			return '03';
		case 'apr' :
			return '04';
		case 'may' :
			return '05';
		case 'jun' :
			return '06';
		case 'jul' :
			return '07';
		case 'aug' :
			return '08';
		case 'sep' :
			return '09';
		case 'oct' :
			return '10';
		case 'nov' :
			return '11';
		case 'dec' :
			return '12';
		default  :
			return '00';
	}
}

function CheckComboDate(item_month, item_day, item_year, item, b_msg)
{
	var c_month, c_day, c_year;
	
	if (bInvalidDate == false)
	{
		c_month = item_month.value;
		c_day	= item_day.value;
		c_year	= item_year.value;
	
		c_leap_yr = CheckLeapYr(c_year);
		if (c_month == 2)
		{
			if (c_leap_yr)
			{
				if (c_day > 29)
				{
					if (b_msg == true)
					{
						bInvalidDate = true;
						item.focus();
						alert("February cannot have more than 29 days in a leap year"+"\n"+"Try changing the day or the month");
						if (item == item_year)
							bInvalidDate = false;
					}					
					return false;
				}
			}
			else
			{
				if (c_day > 28)
				{
					if (b_msg == true)
					{
						bInvalidDate = true;
						item.focus();
						alert("February cannot have more than 28 days if not a leap year"+"\n"+"try changing the day, the month or the year");
						if (item == item_year)
							bInvalidDate = false;
					}
					return false;
				}
			}
		}

		if ((c_month==9 || c_month==4 || c_month==6 || c_month==11) && c_day>30)
		{
			if (b_msg == true)
			{
				bInvalidDate = true;
				item.focus();
				alert("Day cannot be greater than 30 for month selected");
			}
			return false;
		}
		bInvalidDate = false;
	}
	else
		bInvalidDate = false;
	return true;
}

function CheckSubmitDate(item_month, item_day, item_year)
{
	var c_month, c_day, c_year;
	
	c_month = item_month.value;
	c_day	= item_day.value;
	c_year	= item_year.value;
	
	c_leap_yr = CheckLeapYr(c_year);
	if (c_month == 2)
	{
		if (c_leap_yr)
		{
			if (c_day > 29)
			{
				alert("February cannot have more than 29 days in a leap year"+"\n"+"Try changing the day or the month");
				return false;
			}
		}
		else
		{
			if (c_day > 28)
			{
				alert("February cannot have more than 28 days if not a leap year"+"\n"+"try changing the day, the month or the year");
				return false;
			}
		}
	}

	if ((c_month==9 || c_month==4 || c_month==6 || c_month==11) && c_day > 30)
	{
		alert("Day cannot be greater than 30 for month selected");
		return false;
	}

	return true;
}

function checkDayofWeek(dateVal){	
	var dtDay, dtMonth, dtYear , dtDaychk, day, DayName;
	dtDaychk = dateVal.value.substr(0,2);
	dtMonth = dateVal.value.substr(3,2);
	dtYear = dateVal.value.substr(6,4);
	var dayExactDate = dtMonth + "/" + dtDaychk +"/" + dtYear;			
	dayExactDate = new Date(dayExactDate);

	day = dayExactDate.getDay();

	if (day == 6 || day == 0) {
		DayName = (day == 6) ? 'Saturday' : 'Sunday';
		if (!confirm ("The date you have entered is a " + DayName + "! Do you really want to add this task or appointment to a weekend?.")) {
			dateVal.value = "";
			dateVal.focus();
			return false;
		}
		else {
			return true;
		}
	}
	return true;

}
function ConvertDateToStringObject(parmDateObj) {
	//parmDateObj should be Date object
	return LeadZero(Number(parmDateObj.getDate()))+ "/"+ LeadZero(Number(parmDateObj.getMonth() + 1))  + "/" + parmDateObj.getFullYear().toString();;
	
}

function shiftDaytoMonday(dateVal, dateName){
	var dtDay, dtMonth, dtYear , dtDaychk, day, DayName;
	dtDaychk = dateVal.value.substr(0,2);
	dtMonth = dateVal.value.substr(3,2);
	dtYear = dateVal.value.substr(6,4);
	var dayExactDate = dtMonth + "/" + dtDaychk +"/" + dtYear;			
	dayExactDate = new Date(dayExactDate);

	day = dayExactDate.getDay();

	if (day == 6 ){
		var temp = new Date(dayExactDate.getFullYear(), dayExactDate.getMonth(), dayExactDate.getDate() + 2);
		alert("Since the calculated date for " + dateName + " is a Saturday, this date has been shifted to a Monday")
		dateVal.value = ConvertDateToStringObject(temp);
		return dateVal.value
		
	}else if (day == 0){
		var temp = new Date(dayExactDate.getFullYear(), dayExactDate.getMonth(), dayExactDate.getDate() + 1);
		alert("Since the calculated date for " + dateName + " is a Sunday, this date has been shifted to a Monday")
		dateVal.value = ConvertDateToStringObject(temp);
		return dateVal.value
	}
	
}

//Function added by Ruchi on 14 Jan 04 to convert single digit numbers into 2 digit numbers
// function to convert single digit numbers into double digit numbers, especially for days and months
function ConvertSingleDigitToDouble(parmConvert){
	parmConvert = parmConvert + "";
	switch (parmConvert) {
		case '1': 
			return '01';
		case '2':	
			return '02';
		case '3':	
			return '03';
		case '4':	
			return '04';
		case '5':	
			return '05';
		case '6':	
			return '06';
		case '7':	
			return '07';
		case '8':	
			return '08';
		case '9':	
			return '09';
		case '10':	
			return '10';
		case '11':	
			return '11';
		case '12':	
			return '12';
		case '13':	
			return '13';
		case '14':	
			return '14';
		case '15':	
			return '15';
		case '16':	
			return '16';
		case '17':	
			return '17';
		case '18':	
			return '18';
		case '19':	
			return '19';
		case '20':	
			return '20';
		case '21':	
			return '21';
		case '22':	
			return '22';
		case '23':	
			return '23';
		case '24':	
			return '24';
		case '25':	
			return '25';
		case '26':	
			return '26';
		case '27':	
			return '27';
		case '28':	
			return '28';
		case '29':	
			return '29';
		case '30':	
			return '30';
		case '31':	
			return '31';
		default:	
			return '00';
}	
}