function isEmail(strString) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strString)){
		return true;
	}
	return false;
}

function isDate(strString) {
	if (/^([0-3][0-9])\/([0-1][0-9])\/([0-9]{2,4})$/.test(strString)){
		return true;
	}
	return false;
}

function isDateTime(strString) {
	if (/^([0-3][0-9])\/([0-1][0-9])\/([0-9]{2,4}) ([0-2][0-9]):([0-5][0-9])$/.test(strString)){
		return true;
	}
	return false;
}

function isTime(strString) {
	if (/^([0-2][0-9]):([0-5][0-9])$/.test(strString)){
		return true;
	}
	return false;
}

function isText(strString) {
	if (strString.length <1) {
		return false;
	} else {
		return true;
	}
}

function isNumeric(strString) {
   var strValidChars = " 0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++) {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1) {
		 blnResult = false;
	  }
   }
   return blnResult;
}

function isPostcode(postcode) {
	
	var test = postcode; 

	size = test.length;
	
	if (size == 0) {
		return true;
	}
	
	test = test.toUpperCase(); //Change to uppercase
	
	while (test.slice(0,1) == " ") //Strip leading spaces
	{
		test = test.substr(1,size-1);
		size = test.length;
	}
	
	while(test.slice(size-1,size)== " ") //Strip trailing spaces
	{
		test = test.substr(0,size-1);
		size = test.length;
	}
	
	//document.find_venue_form.search_postcode.value = test; //write back to form field
	if ((size < 6 || size > 8)
	|| (!(isNaN(test.charAt(0))))
	|| (isNaN(test.charAt(size-3)))
	|| (!(isNaN(test.charAt(size-2))))
	|| (!(isNaN(test.charAt(size-1))))
	|| (!(test.charAt(size-4) == " ")))
	{ //Code length rule
		return false;
	}
	 
	count1 = test.indexOf(" ");
	count2 = test.lastIndexOf(" ");
	if (count1 != count2){//only one space rule
		return false;
	}
	
	return true;

}

function chkDueDateInFuture(duedate) {

	var today = new Date();
	var theDate = new Date();
	
	var elem = duedate.split('/');
	day = elem[0];
	month = elem[1]-1;
	year = elem[2];

	theDate.setFullYear(year,month,day);

	if (theDate<today) {
		alert('Invalid Task Due Date');
		return false;
	} else {
		return true;
	}

}

function chkForm(form) {
	
	var errMessage = "";
	
	// check any required fields
	var fieldList = $(form.name).getElementsByClassName('req');

	for (var i = 0, j = fieldList.length; i < j; i++) {
		fieldList[i].removeClassName('checkfield');

		if(thisField = fieldList[i].hasClassName('text')) {
			if(!isText(fieldList[i].value)) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}			
		}
		if(thisField = fieldList[i].hasClassName('numeric')) {
			if(!isNumeric(fieldList[i].value)) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('email')) {
			if(!isEmail(fieldList[i].value)) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('datetime')) {
			if(!isDateTime(fieldList[i].value)) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('date')) {
			if(!isDate(fieldList[i].value)) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('time')) {
			if(!isTime(fieldList[i].value)) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('postcode')) {
			if(!isPostcode(fieldList[i].value)) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
	}

	// check any optional fields
	var fieldList = $(form.name).getElementsByClassName('opt');

	for (var i = 0, j = fieldList.length; i < j; i++) {
		fieldList[i].removeClassName('checkfield');

		if(thisField = fieldList[i].hasClassName('text')) {
			if( (fieldList[i].value != '') && (!isText(fieldList[i].value)) ) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}			
		}
		if(thisField = fieldList[i].hasClassName('numeric')) {
			if( (fieldList[i].value != '') && (!isNumeric(fieldList[i].value)) ) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('email')) {
			if((fieldList[i].value != '') && (!isEmail(fieldList[i].value))) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('datetime')) {
			if((fieldList[i].value != '') && (!isDateTime(fieldList[i].value))) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('date')) {
			if((fieldList[i].value != '') && (!isDate(fieldList[i].value))) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('time')) {
			if((fieldList[i].value != '') && (!isTime(fieldList[i].value))) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
		if(thisField = fieldList[i].hasClassName('postcode')) {
			if((fieldList[i].value != '') && (!isPostcode(fieldList[i].value))) {
				errMessage += fieldList[i].title + "\n";
				fieldList[i].addClassName('checkfield');
			}
		}
	}
		
	if (errMessage != "") {
		alert(errMessage);
		return false;
	} else {
		//if (form.submit.value != null) {
			//form.submit.disabled = true;
			//form.submit.value = 'Submitting';
		//}
		return true;
	}

}

// Compares 2 password values on form and checks they are the same
// fields must be called
// password
// and confirmpassword
function comparePasswords(form) {
	if ((form.password.value != '') || (form.confirmpassword.value != '')) {
		if(form.password.value != form.confirmpassword.value) {
			alert('The passwords entered do not match.');
			return false;
		}
	}
	return true;
}



