
function TrimString(pStringToTrim) {

	var string = pStringToTrim;
	var leftTrim = string.replace(/^(\s+)/, "");
	var fullTrim = leftTrim.replace(/(\s+)$/, "");

	return fullTrim;
	
}

function SubFieldChecker(){
	/*
	 * This function warns the user that specified fields are
	 * not filled in on submission of a form.
	 * For each parameter fieldname, it checks if the field is
	 * empty and warns the user.	The focus is returned to the
	 * first empty field found.
	 *
	 * Note: The first element of the arguments array is the form name.
	 *
	 * Author: j-p (20021207)
	 */

	var firstEmptyField = "";
	
	// Check if parameter string is empty.
	if (arguments.length == 0) {
		alert("No field name arguments: " +
	 "Please indicate the fields to check.");
	 
		return false;

	} else {
	  
		// Parameters received.  Loop through them.
		for (var i = 1; i < arguments.length; i++) {
	 
	 // current field and it's value.
	 var fieldName = arguments[i];

	 var fieldValue = 
		 TrimString(document.forms[arguments[0]].elements[fieldName].value);

	 // Is this field blank.
	 if ("" == fieldValue) {
		 
		 // If it's the first blank one found,
		 // save it's name for returning to in the form.
		 if ( "" == firstEmptyField)
			 firstEmptyField = fieldName;
			 
		 // Warn the user.
		 alert("No value entered for " + fieldName +
			 ".  Please fill in required field(s) before submitting.");

			 // Return to first empty field we found.
			 document.forms[arguments[0]].elements[firstEmptyField].focus();
			 return false;
		 
	 }
		}
		
		// No blank fields (the firstEmptyField was never initialized).
		if ("" == firstEmptyField) {
	  
	 return true;
	 
		} else {
	
	 // Return to first empty field we found.
	 document.forms[arguments[0]].elements[firstEmptyField].focus();
	 return false;
		}
	}
	
}


function emailCheck (fieldName, emailStr) {

	if (emailStr != "") {
/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.	It also is used to separate the username
	from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ]		*/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
	non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
	valid. */

/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
	  even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	fieldName.select();
	fieldName.focus();
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
	 // user is not valid
	 alert("The username doesn't seem to be valid.")
	  fieldName.select();
	  fieldName.focus();
	 return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	 // this is an IP address
	  for (var i=1;i<=4;i++) {
		 if (IPArray[i]>255) {
			  alert("Destination IP address is invalid!")
		 fieldName.select();
		 fieldName.focus();
		return false
		 }
	 }
	 return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
	  fieldName.select();
	  fieldName.focus();
	 return false
}

/* domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
	it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
	 domArr[domArr.length-1].length>4) {
	// the address must end in a two letter or three letter word.
	alert("The address must end in a three or four-letter domain (e.g. com, info), or two letter country.")
	  fieldName.select();
	  fieldName.focus();
	return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
	var errStr="This address is missing a hostname!"
	alert(errStr)
	  fieldName.select();
	  fieldName.focus();
	return false
}

// If we've gotten this far, everything's valid!
return true;

}
	//I've not tested this!! /j-p (20081222)
	return true;
}



function ForceMinLength(pField, pMinVal) {

	if (pField.value != "") {

		if (pField.value.length < pMinVal) {

			window.alert("Please type at least " + pMinVal +
					" characters for " + pField.name + " field.");
			pField.select();
			pField.focus();
			return false;
	 
		}

	}
	
	return true;
}

function EnsureAlpha(pField) {

	var pString = pField.value;

	if (pString == null) {

		return false;

	}

	if (pString.length == 0) {

		return false;

	} else {

		return true;

	}

/*
	if ((pString.charAt(0) >= "a") && (pString.charAt(0) <= "z") ||
		(pString.charAt(0) >= "A") && (pString.charAt(0) <= "Z")) {

		return true;

	} else {

		window.alert("Sorry... lastname must begin with a letter of the alphabet");
		document.lastname.select();
		document.lastname.focus();
		return false;

	}
	*/

} //ends function


function CompareFields(pForm, pField1, pField2) {

	if (pField2.value != "") {
		
		if (pField1.value != pField2.value) {

			alert(pField2.name + " is not the same as " + pField1.name);
			pField2.select();
			pField2.focus();
			return false;
		
		}
	}			
		
	return true;
}


function EnsureNumber(field_p) {
	if (isNaN(field_p.value) || field_p.value == 0) {
		field_p.value='';
	}
}

function NumberChecker(field_p){

	if(isNaN(field_p.value)){
		field_p.value='';
		field_p.focus();

	} else {

		field_p.value = Math.round(field_p.value);

	}
}


function EnsureChecked(form_p) {

	for (var i = 0; i < form_p.elements.length; i++) {

		if (form_p.elements[i].type == 'checkbox' &&
	 form_p.elements[i].checked == true) {
	 return true;

		}
	}

		alert("You must first select a topic to unwatch.");
		return false;
}

function bDayCheck(pFormName,pVal,pControlId,pMin,pMax,pPrevControlId,pDayId,pMonthId,pYearId) {
	
	/*
	 *  This function handles numeric input into a text input box.
	 *  Used for date validation where day, month and year are
	 *  three different input boxes.
	 *
	 *  Params: input value, input id name, acceptable range min,
	 *			 acceptable range max, id of previous
	 *			 input box in group.
	 */

	// Regex meaning any non-numeric data.
	var re = /\D/;
	var warnRangeNum = "Please insert a number from " + pMin + " to " + pMax;
	
	if (pVal != "") {
		/*
		 *  If it's the month value, check the day value
		 *  already exists.
		 *  If it's the year valie, check the month value
		 *  already exists.
		 */
		if (document.forms[pFormName].elements[pPrevControlId].value == "") {
	 
			alert("FOOBAR Error. You must provide a " + pPrevControlId + " value.");
			document.forms[pFormName].elements[pPrevControlId].value = "";
			document.forms[pFormName].elements[pControlId].value = "";
			document.getElementById(pPrevControlId).focus();
			return false;
			
		}
		
		// The value is numeric.
		if (pVal.search(re) == -1) {
	 
			// The value is not within the proper range.
			if ((pVal < pMin) || (pVal > pMax)) {

				alert(warnRangeNum);
				document.forms[pFormName].elements[pDayId].value = "";
				document.forms[pFormName].elements[pMonthId].value = "";
				document.forms[pFormName].elements[pYearId].value = "";
				document.forms[pFormName].elements[pControlId].focus();
				return false;
		
			} else {
				/* 
				 *  The value is less than 10 i.e. 1-9 then
				 *  prefix with a zero.
				 */
				if ((pVal < 10) && (pVal.length < 2)) {
					
					document.forms[pFormName].elements[pControlId].value = "0" + pVal;
				}
				
			}
		
		} else {

				alert(warnRangeNum);
				document.forms[pFormName].elements[pControlId].value = "";
				document.forms[pFormName].elements[pControlId].focus();
				return false;
				
		}

	} else {
	 
			/*
			*  If the textbox is blank on exiting, then blank
			*  all the other boxes too (to avoid submission
			*  of invalid data).
			*/
			document.forms[pFormName].elements[pDayId].value = "";
			document.forms[pFormName].elements[pMonthId].value = "";
			document.forms[pFormName].elements[pYearId].value = "";
			return false;
	}

	return true;
	
}

function EnsureChecked(form_p, checkboxGroup_p) {

	for (var i=0; i < form_p.elements.length; i++) {

		if (form_p.elements[i].name.indexOf(checkboxGroup_p) > -1) {

	 if (form_p.elements[i].checked) {

		 return true;
		 
	 }
		}
	}
	alert("Attention: You must make at least one choice from the " 
		+ checkboxGroup_p + " section.");
	return false;
}

function Justatest() {
	var testDate = new Date('22/12/2008');
	
	alert("ciao! " + testDate);
}

function IsLeadingZeroRequired(pNum) {
	
	if (pNum.length == 1){
		
		pNum = "0" + pNum;
		return pNum;
		
	} else {
		
		return pNum;
	}	
}

function EnsureCompatibleDates() {
/* Checks two dates to see if the second one comes
 * after the first.
 * 
 */
	var startDay = document.getElementById('start_day');
	var startMonth = document.getElementById('start_month');
	var startYear = document.getElementById('start_year');
	var startDate = new Date( startDay + '\\' + startMonth + '\\' + startYear);
	var endDate = new Date(document.getElementById('end_day')+ '/' + document.getElementById('end_month') + '/' + document.getElementById('end_year'));
	
	alert("the dates are: " + startDate + ", " + endDate);

	return false;
}