// Function that focuses a cursor in a given field
function focusCursor(field,sel) {
	if (field.value.length==0 && sel==1) {
		field.focus();
		field.select();
	} else {
		field.focus();
	}
}

// Year validation
function validateYear(field) {
	var yearlength=4
	if (!(field.value.length == yearlength) && !(field.value.length==0)) {
		alert("The year must be entered in as 4 digits.");
		field.focus();
		field.select();
	}
}

// Year validation
function validatePasswordLength(field) {
	var minlength=5
	if (!(field.value.length >= minlength) && !(field.value.length==0)) {
		alert("Passwords must be at least 5 characters long.");
		field.focus();
		field.select();
	}
}

// Validation of a number range
function validateNumRange(field,minNum,maxNum,nullField) {
	if (field.value == "" && nullField == 0) {
		alert("Please enter a whole number between " + minNum + " and " + maxNum + ".");
		field.select();
		field.focus();
	}
	else if (!(field.value =="") && (isNaN(field.value) || field.value < minNum || field.value > maxNum || field.value.indexOf('.') != -1)) {
		alert("Please enter a whole number between " + minNum + " and " + maxNum + ".");
		field.select();
		field.focus();
	}
}

// Validation of a number value
function validateNum(field) {
	if (isNaN(field.value) || field.value == "" || field.value.indexOf('.') == -1) {
		alert("Please enter whole numbers only.");
		field.select();
		field.focus();
	}
}

// Validation of a currency entry
function validateCurrency(field,nullField) {
	if (field.value != "" || nullField==0) {
		num = field.value;
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num)) {
			num = "0";
			alert("You have entered an invalid amount. Please enter the value in dollars and cents.");
		}
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		field.value = num + "." + cents;
	}
}

// Validation of a currency entry
function validateCurrency3(field,nullField) {
	if (field.value != "" || nullField==0) {
		num = field.value;
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num)) {
			num = "0";
			alert("You have entered an invalid amount. Please enter the value in dollars and cents.");
		}
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*1000+0.50000000001);
		cents = num%1000;
		num = Math.floor(num/1000).toString();
		if(cents<100) cents = "00" + cents;
		else if (cents<10) cents = "0" + cents;
		field.value = num + "." + cents;
	}
}

// Validation of a decimal entry
function validateDecimal(field) {
	num = field.value;
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {
		num = "0";
		alert("You have entered an invalid amount. Please enter a numeric value, rounded off to 2 decimal places.");
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	field.value = num + "." + cents;
}


function validateEmail(field) {
	var emailStr = field.value;
	/* 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 +")*$")
	/*  The following variable determines the message received by the user if the e-mail address isn't valid */
	var errorMsg = "This doesn't appear to be a valid e-mail address."
	var emailError = 0;

	/* 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. */
		// Email address seems incorrect (check @ and .'s)
		emailError = 1;
	} else {
		var user=matchArray[1]
		var domain=matchArray[2]

		// See if "user" is valid 
		if (user.match(userPat)==null) {
		    // The username doesn't seem to be valid.
			emailError = 1;
		}

		/* 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) {
					// Destimation IP address is invalid
					emailError = 1;
			    }
		    }
		}
	
		// Domain is symbolic name
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			// The domain name doesn't seem to be valid
			emailError = 1;
		}
	
		/* 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 to four letter word, or two letter country.
			emailError = 1;
		}
		
		// Make sure there's a host name preceding the domain.
		if (len<2) {
			// This address is missing a hostname
			emailError = 1;
		}
	}
	if (emailError==1) {
		alert(errorMsg);
		field.select();
		field.focus();
	}
	// If we've gotten this far, everything's valid!
}

function requiredField(field,alertMsg,ifFocus) {
	if (!(field.value)) {
		alert(alertMsg);
		if (ifFocus == 1) {
			field.focus();
		}
		return false;
	}
	else { 
		return true;
	}
}

function trim(variable) { 
	var strText = variable + ""
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

// Function for click confirmation
function clickConfirm(theMessage) {
	if (confirm(theMessage)) {
		return true;
	}
	else {
		return false;
	}
}