var aDays = new Array();
aDays[0] = 31;
aDays[1] = 28;
aDays[2] = 31;
aDays[3] = 30;
aDays[4] = 31;
aDays[5] = 30;
aDays[6] = 31;
aDays[7] = 31;
aDays[8] = 30;
aDays[9] = 31;
aDays[10] = 30;
aDays[11] = 31;

function goDays(oldDate, no_days) {
	var year = oldDate.getFullYear();
	var month = oldDate.getMonth();
	var day = oldDate.getDate();
	var bMinus = (no_days < 0);
	
	// Make no_days +ve.
	if (bMinus) no_days = -no_days;
	
	for (var i = 0; i < no_days; i++) {
		if (bMinus) {
			if (day <= 1) {
				if (month == 2 && isLeapYear(year)) {
					month--;
					day = 29;
				} else {
					if (month <= 0) {
						year--;
						month = 11;
					} else {
						month--;
					}
					day = aDays[month];
				}
			} else {
				day--;
			}
		} else {
			if (day >= aDays[month]) {
				if (month == 1 && isLeapYear(year)) {
					day++;
				} else {
					day = 1;
					if (month >= 11) {
						month = 0;
						year++;
					} else {
						month++;
					}
				}
			} else {
				day++;
			}
		}
	}
	return new Date(year, month, day, oldDate.getHours(), oldDate.getMinutes(), oldDate.getSeconds(), 
			oldDate.getMilliseconds());
}

function goMonths(oldDate, no_months) 
{
	var year = oldDate.getFullYear();
	var month = oldDate.getMonth();
	var days = 0;
	var bMinus = (no_months < 0);
	
	// Make sure no_months +ve.
	if (bMinus) no_months = -no_months;
	
	for (var i = 0; i < no_months; i++) 
	{
		if (bMinus) 
		{
			if (month <= 0) 
			{
				month = 11;
				year--;
			}
			else month--;
			days = days + aDays[month];
			if (month == 1 && isLeapYear(year)) days++;
		} 
		else 
		{
			days = days + aDays[month];
			if (month == 1 && isLeapYear(year)) days++;
			
			if (month >= 11) 
			{
				month = 0;
				year++;
			} 
			else month++;
		}
	}
	
	return goDays(oldDate, (bMinus) ? -days : days);
}

function goWeeks(oldDate, no_weeks) 
{
	return goDays(oldDate, no_weeks * 7);
}

function goYears(oldDate, no_years) 
{
	return goMonths(oldDate, no_years * 12)
}

function isLeapYear(year) 
{
	return (year % 4 == 0 && (year % 100 != 0 && year % 400 == 0));
}

function validatePassword(field, name)
{
	// Make sure password is of required complexity.
	if (field.value.length < 8)
	{
		alert(name + " must be at least 8 characters long.");
		return false;
	}
	
	var hasUCAlpha = false;
	var hasLCAlpha = false;
	var hasDigit = false;
	
	for (var i = 0; i < field.value.length; i++)
	{
		var c = field.value.charAt(i);
		if (between(c,0,9))
			hasDigit = true;
		else if (between(c,'a','z'))
			hasLCAlpha = true;
		else if (between(c,'A','Z'))
			hasUCAlpha = true;
	}
	if (!hasUCAlpha || !hasLCAlpha || !hasDigit)
	{
		alert("Passwords must contain ALL of the following:\n\n* Uppercase Characters (A-Z)\n* Lowercase Characters (a-z)\n* Numeric Characters (0-9)\n\n(e.g. P4ssw0rD)");
		return false;
	}
	
	return true;
}

function validateSameFields(field1, name1, field2, name2)
{
	if (field1.value != field2.value)
	{
		alert("Please enter identical values in the \'" + name1 + "\' and \'" + 
				name2 + "\' fields.");
		return false;
	}
	return true;
}

function validateRadioGroup(rg, name) 
{
	var bValid = false;
	for (var i = 0; i < rg.length; i++)
	{
		if (rg[i].checked) {
			bValid = true;
			break;
		}
	}

	if (!bValid) alert("Please select an option for \'" + name + "\'");
	return bValid;
}

function validateCheckBox(field, name)
{
	if (!field.checked)
	{
		alert("Please select the \'" + name + "\' option");
		return false;
	}
	return true;
}

function validateField(field, name) 
{
	if (field.value == "" || field.value == -1) {
		alert("Please enter a value for \'" + name + "\'");
		return false;
	}
	return true;
}

function validateNumeric(field, name)
{
	if (!isNumeric(field.value, false))
	{
		alert("Please enter a valid numeric value for \'" + name + "\'");
		return false;
	}
	return true;
}

function validateDate(field, name) 
{
	var bValid = true;

	if (field.value != "") 
	{
		
		var strValue = new String(field.value);
		var tmp = strValue.split("/");
		
		var day = tmp[0];
		var month = tmp[1];
		var fullYear = tmp[2];

		if (day < 1 || day > 28) 
		{
			if (month == 2) 
			{
				if (day != 29 || (fullYear % 4 != 0 || (fullYear % 100 == 0 && fullYear % 400 != 0))) 
					bValid = false;
			}
			else if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) 
				bValid = false;
			else if (day > 31) 
				bValid = false;
		}
		
		if (month < 1 || month > 12) 
			bValid = false;
		if (fullYear/1000 < 1) 
			bValid = false;
		
		bValid = bValid && (tmp.length == 3);
	}
	if (!bValid) {
		alert("Please enter a valid date for \'" + name + "\' in the format dd/mm/yyyy.");
	}
	return bValid;
}

function formatPostCode(field) {
	var newVal = field.value.toUpperCase().replace(/ /g, "");
	newVal = newVal.substring(0, (newVal.length - 3)) + " " + 
			newVal.substring((newVal.length - 3));
	field.value = newVal;
}

function setCurrentDate(field) {
	var now = new Date();
	field.value = ((now.getDate() < 10) ? "0" : "") + now.getDate() + "/" + ((now.getMonth() + 1 < 10) ? "0" : "") + 
			(now.getMonth() + 1) + "/" + now.getYear();
}

function getLongWeek(year, week_no) {
	var date = new Date("January 1, " + year + " 00:00:00");
	if (date.getDay() != 0 && date.getDay() <= 4) week_no--;
	date = goWeeks(date, week_no);
	return date.getTime();
}

function floorDate(old_date) {
	old_date.setHours(0);
	old_date.setMinutes(0);
	old_date.setSeconds(0);
	old_date.setMilliseconds(0);
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) 
{
	if (string.search)
	{
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || 
				(!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

function between(value, start, end)
{
	return (value >= start) && (value <= end);
}

function validateEmailAddress(email) 
{
	var str = email.toString();
	if(str.indexOf("@") >= 0)
	{
    return true;
	}
	else
	{
    alert("Please enter a valid Email Address");
		return false;
	}
}

// Clears a given form, submits if a true value is passed in
function clearForm(myForm, submit) 
{
	var elements = myForm.elements; 

	for(i=0; i<elements.length; i++) 
	{      
		field_type = elements[i].type.toLowerCase();
		
		switch(field_type) 
		{
			case "text": 
			case "password": 
			case "textarea":
			case "hidden":	
				elements[i].value = ""; 
				break;
			      
			case "radio":
			case "checkbox":
				if (elements[i].checked) {
					elements[i].checked = false; 
				}
				break;
		
			case "select-one":
			case "select-multi":
				elements[i].selectedIndex = 0;
				break;
					
			default: 
				break;
		}
	}
	
	if (submit)
		myForm.submit();
}
