function trimstr(strInput)
{
	if (strInput == "") { return strInput }
	while (strInput.charAt(0) == " ") {
		if (strInput != "") { strInput = strInput.slice(1) }
	}
	if (strInput == "") { return strInput }
	while (strInput.charAt(strInput.length-1) == " ") {
		if (strInput != "") { strInput = strInput.slice(0, -1) }
	}
	return strInput
}
function checkEmail(sStr)
{
	if ((sStr == "") || (sStr.indexOf('@') == -1) || (sStr.indexOf('@') == 0) || (sStr.indexOf('.') == -1) || (sStr.indexOf('.') == sStr.length-1) || (sStr.indexOf(' ') != -1) || sStr.indexOf('@') > sStr.indexOf('.') || (sStr.indexOf('.') - sStr.indexOf('@') == 1))
	{ 
		alert("Invalid Email Adress. Please Re-enter.");
		return false;
	}
	return true;
}

function checkDateTime(strDateTimeVal)
{
	//assuming input val is in the format mm/dd/yy h:m:s AM/PM
	var strDateTimeParts = strDateTimeVal.split(" ")
	if (strDateTimeParts.length > 3)
	{
		alert("Please enter a date time value in the format mm/dd/yy h:m:s AM/PM")
		return false
	}
	if (!checkDate(strDateTimeParts[0])) return false;
	if (strDateTimeParts.length >= 2 && !checkTime(strDateTimeParts[1])) return false;
	if (strDateTimeParts.length == 3 && strDateTimeParts[2].toUpperCase() != 'AM' && strDateTimeParts[2].toUpperCase() != 'PM') { alert('Please specify AM or PM'); return false; }
	return true;
}

function checkTime(strTimeVal)
{
	//assuming input val is in the format h:m:s
	var strTimeParts = strTimeVal.split(":")
	if (strTimeParts.length != 3)
	{
		alert("Please enter a time value in the format h:m:s")
		return false
	}
	if (isNaN(parseInt(strTimeParts[0],10)) || parseInt(strTimeParts[0],10) < 0 || parseInt(strTimeParts[0],10) > 12)
	{
		alert("Please enter an hour between 0 and 12")
		return false
	}
	if (isNaN(parseInt(strTimeParts[1],10)) || parseInt(strTimeParts[1],10) < 0 || parseInt(strTimeParts[1],10) > 59)
	{
		alert("Please enter a minute between 0 and 59")
		return false
	}
	if (isNaN(parseInt(strTimeParts[2],10)) || parseInt(strTimeParts[2],10) < 0 || parseInt(strTimeParts[2],10) > 59)
	{
		alert("Please enter a second between 0 and 59")
		return false
	}
    return true;
}
			function checkDate(strDateVal)
			{
			//split the date value on the slash character to verify there are 2 slashes
			var strDateParts = strDateVal.split("/")
			if (strDateParts.length != 3) {
				alert("Please enter a date value in the format mm/dd/yy")
				return false
			}
			var i
			//validate that some values are entered
			for (i=0;i<strDateParts.length;i++)
			{
				if (strDateParts[i].length == 0 || isNaN(parseInt(strDateParts[i])))
				{
					alert("Please enter a date value in the format mm/dd/yy")
					return false
				}
			}
			strYear = strDateParts[2];
			strMonth = strDateParts[0];
			strDay = strDateParts[1];
			//now validate the month
			if (parseInt(strMonth,10) < 1 || parseInt(strMonth,10) > 12)
			{
				alert("Please enter a valid month")
				return false
			}
			//next let's validate the year since we have to use it in our leap year calculations
			//the bound parameters are governed by the limits of the smalldatetime data type in SQL which is what this value will be passed to - also, for SQL, for the year 2079, the max date is June 6, so let's validate that as well
			//iYear = parseInt(strYear,10) + 2000;
			iYear=strYear //Valentin
			if (iYear < 1900 || iYear > 2079)
			{
				alert("Please enter a valid year between 1900 and 2079")
				return false
			}
			if (iYear == 2079)
			{
				if (parseInt(strMonth,10) > 6)
				{
					alert("For the year 2079, the maximum date is June 6!!")
					return false
				}
				if (parseInt(strMonth,10) == 6 && parseInt(strDay,10) > 6)
				{
					alert("For the year 2079, the maximum date is June 6!!")
					return false
				}
			}			
			var bIsLeapYear = false
			if (iYear % 4 == 0) { bIsLeapYear = true }
			if (bIsLeapYear)
			{
				var strMonthDays = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31)
			}
			else
			{
				var strMonthDays = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31)
			}
			//now validate the days
			if (parseInt(strDay,10) < 1 || parseInt(strDay,10) > strMonthDays[parseInt(strMonth,10)])
			{
				alert("Please enter a valid day")
				return false
			}
			return true
		}
		function setaccess(control, control2)
		{
				var i
				if (!control) { return false }
				var accessstr = ""
				if (!control.length) { 
					if (control.checked) { accessstr = control.value }
				}
				else {
					for (i=0; i < control.length; i++) {
						if (control[i].checked) {
							if (accessstr != "") { accessstr = accessstr + "," }
							accessstr = accessstr + control[i].value
						}
					}
				}
				if (accessstr == "") {
					alert("Please check an item!")
					return false
				}
				control2.value = accessstr
				return true
			}
			function checkall(control, control2)
			{
				var i
				if (!control) { return false }
					if (!control.length) { 
						if (control2.value == "Check All") { control.checked = true;	}				  
						else { control.checked = false; }
					}
				else {
					for (i=0; i < control.length ; i++ ) {
						if (control2.value == "Check All") { control[i].checked = true;	}				  
						else { control[i].checked = false; }
					}
				}
				if (control2.value == "Check All") { control2.value="UnCheck All"; }
				else { control2.value = "Check All"; }			
			}

function valCommonElements(thisForm)
{
	//validate the common elements on the page if they exist
	if (thisForm.ElevateDate && trimstr(thisForm.ElevateDate.value) != '' && !checkDateTime(trimstr(thisForm.ElevateDate.value)))
	{
		thisForm.ElevateDate.focus();
		return false;
	}
	if (thisForm.TakeDownDate && trimstr(thisForm.TakeDownDate.value) != '' && !checkDateTime(trimstr(thisForm.TakeDownDate.value)))
	{
		thisForm.TakeDownDate.focus();
		return false;
	}
	return true;
}