// $Id: common.js,v 1.4 2004/09/24 20:00:51 tooshi Exp $
//

window.onerror = function( errMsg, errFile, errNo ) {
	alert( errMsg + "\n" + errFile + "\n" + (errNo-1) );
} // window.onerror

// post: skip leading and trailing whitespace and return everything in between
String.prototype.trim = function() {
	return this.replace(/(^[\s\xa0]*)|([\s\xa0]*$)/g, "");
} // String.prototype.trim

// post: returns true if a string is empty
String.prototype.empty = function() {
	return ( this.length == 0 );
} // String.prototype.empty

// post: returns true if a string is in the valid phone format.
//       e.g. 123-456-7890
String.prototype.isPhone = function() {
	return ( this.search(/^[\d]{3}-[\d]{3}-[\d]{4}$/) != -1 );
} // String.prototype.isPhone

// post: returns true if a string can be a phone #.
//       e.g. 1234567890, 123-456-7890
String.prototype.canBePhone = function() {
	return ( this.isPhone() || this.search(/^[\d]{3}-?[\d]{3}-?[\d]{4}$/) != -1 );
} // String.prototype.canBePhone

// post: return substr(this,0,3) + '-' + substr(this,3,3) + '-' + this.slice(6)
//       eg "12324567890".toPhone -> "123-456-7890"
String.prototype.toPhone = function() {
	return this.replace( /^(\d{3})-?(\d{3})-?(\d{4})/, "$1-$2-$3" );
} // String.prototype.toPhone

// post: returns true if a string is in the valid postal code format.
//       e.g. M1N 2P3
String.prototype.isPostal = function() {
	return ( this.search(/^[A-Za-z][\d][A-Za-z] [\d][A-Za-z][\d]$/) != -1 );
} // String.prototype.isPhone

// post: returns true if a string can be a postal code.
//       e.g. M1N2P3, M1N 2P3
String.prototype.canBePostal = function() {
	return ( this.isPostal() || this.search(/^[A-Za-z][\d][A-Za-z][\d][A-Za-z][\d]$/) != -1 );
} // String.prototype.canBePhone

// post: return substr(this,0,3) + ' ' + substr(this,3,3)
//       eg "M1N2P3".toPostal -> "M1N 2P3"
String.prototype.toPostal = function() {
	return this.replace( /^(\w{3}) ?(\w{3})/, "$1 $2" );
} // String.prototype.toPhone

// post: returns true if a string is in the valid email address format.
//       e.g. test@test.com
String.prototype.isEmail = function() {
	return ( this.search(/^[\w._-]+@([\w.-]+\.)+[\w.-]{2,4}$/) != -1 );
} // String.prototype.isEmail

// post: returns true if a string only contains /[0-9]+/
String.prototype.isNumeric = function() {
	return ( this.search(/^\d+$/) != -1 );
} // String.prototype.isNumeric

// post: returns true if a string only contains valid decimail
String.prototype.isPosReal = function( i ) {
	if ( !this.split(".")[1] ) return this.isNumeric();
	return ( this.search( "^\\d*\\.\\d{0," + i + "}$" ) != -1 );
} // String.prototype.isPosReal

// post: returns true if a string only contains valid decimail
String.prototype.isDate = function( i ) {
	return ( this.search( /^\d{8}$/ ) != -1 );
} // String.prototype.isPosReal

// post: returns true if a string is valid date formate e.g. yyyy-mm-dd
String.prototype.validDate = function( i ) {
	return ( this.search( /\d{4}-\d{1,2}-\d{1,2}/ ) != -1 );
} // String.prototype.validDate

function submitForm() {
	if ( !validate() ) return;
	target.submit();
} // submitForm

function invalidRequired( formField, fieldLabel ) {
	alert( '"' + fieldLabel +'" is a mandatory field, please fill out this field' );
	formField.select();
	formField.focus();
} // invalidRequired

function invalidRequiredPostal( formField, fieldLabel ) {
	alert( '"' + fieldLabel + '" is a mandatory field, and must be in the format: M1M2K3' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidRequiredNumeric( formField, fieldLabel ) {
	alert( '"' + fieldLabel +'" is a mandatory field, must be a numeric value with no decimals or commas.' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidRequiredDate( formField, fieldLabel ) {
	alert( '"' + fieldLabel +'" is a mandatory field, and must be in the format: YYYY-MM-DD' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidNumeric( formField, fieldLabel ) {
	alert( '"' + fieldLabel +'" must be a numeric value with no decimals or commas.' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidRequiredReal( formField, fieldLabel ) {
	alert( '"' + fieldLabel +'" is a mandatory field, and must be a numeric value with up to 2 decimals. Example 15.25' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidReal( formField, fieldLabel ) {
	alert( '"' + fieldLabel +'" must be a numeric value with up to 2 decimals. Example 15.25' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidRequiredPhone( formField, fieldLabel ) {
	alert( '"' + fieldLabel + '" is a mandatory field, and must be in the format: 4161234567' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidPhone( formField, fieldLabel ) {
	alert( '"' + fieldLabel + '" must be in the format: 4161234567' );
	formField.select();
	formField.focus();
} // invalidPostal

function invalidRequiredEmail( formField, fieldLabel ) {
	alert( '"' + fieldLabel + '" is a mandatory field, and must be in the format: test@test.com' );
	formField.select();
	formField.focus();
} // invalidRequiredEmail

function invalidEmail( formField, fieldLabel ) {
	alert( '"' + fieldLabel + '" must be in the format: test@test.com' );
	formField.select();
	formField.focus();
} // invalidEmail

function invalidDate( formField, fieldLabel ) {
	alert( '"' + fieldLabel + '" must be in the format: 20040517' );
	formField.select();
	formField.focus();
} // invalidComm

function textCounter( field, cntfield, maxlimit ) {
	if ( field.value.length > maxlimit ) // if too long...trim it!
		field.value = field.value.substring( 0, maxlimit );
	else // otherwise, update 'characters left' counter
		cntfield.value = maxlimit - field.value.length;
} // textCounter

function newWindow( url, parms, winTitle ) {
	window.open( url, winTitle, parms );
} // newWindow

function cancelForm( url ) {
	if ( !confirm( "Are you sure?" ) ) return;
	gotourl( url );
} // cancelForm

function gotourl( url ) {
	window.location = url;
} // gotourl

// pre: minUser & minPass must be defined
//		form is a valid form name
// post: returns true if user & pw passed the test
function validateUserPass( form ) {

	if ( form.user_name.value.trim().empty() ) {
		invalidRequired( form.user_name, "User Name" );
		return false;
	} // if

	if ( form.user_name.value.trim().length < minUser ) {
		alert( 'Minimum length for User Name is ' + minUser );
		form.user_name.select();
		form.user_name.focus();
		return false;
	} // if

	if ( form.password.value.trim().empty() ) {
		invalidRequired( form.password, "Password" );
		return false;
	} // if

	if ( form.password.value.trim().length < minPass ) {
		alert( 'Minimum length for Password is ' + minPass );
		form.password.select();
		form.password.focus();
		return false;
	} // if

	if ( form.password.value != form.rewrite.value ) {
        alert( "Your password entries did not match.");
		form.password.focus();
		return false;
	} // if

	if ( form.favourite.value.trim().empty() ) {
		invalidRequired( form.favourite, "Your Security Answer" );
		return false;
	} // if

	return true;

} // validateUserPass
