/* scformvalidation.js
 *
 *  GII's form validation Javascript, common to all applications.
 *
 */

function stripNonNumbers( _input ) {

	var result = "";
	var l = _input.length;
	var i = 0;

	for ( i=0; i<l; i++ ) {
		var c = _input.substr(i,1);
		if ( !isNaN( parseInt( c ) ) ) { result += c; }
	}

	return result;
}

/*
 * No function overloading in JS; see below. - NS
 *
 * function valid_phone( _currentValue ) {
 *
 * 	_currentValue = stripNonNumbers( _currentValue );
 * 	if ( (_currentValue.length < 8) || (_currentValue.length > 32) ) { return false; }
 *		else { return true; }
 * }
 */

function valid_phone( _currentValue, _min, _max ) {
	_min = (typeof(_min) != 'undefined' ? _min : 8);
	_max = (typeof(_max) != 'undefined' ? _max : 32);

	_currentValue = stripNonNumbers( _currentValue );
	if ( ( _currentValue.length < _min ) || ( _currentValue.length > _max ) ) { return false; }
	else { return true; }
}

function valid_email( _currentValue, min, max ) {

	var _emailError = isEmail( _currentValue, min, max );
	if ( _emailError.length > 0 ) { return false; }
	else { return true; }
}

function isEmail( _input, min, max ) {

	var err = "";
	var blankSpace = " ";
	var foundAt = false;
	var foundDot = false;

	var l = _input.length;
	var i = 0;

	if ( ( _input.length < (min) ) || ( _input.length > (max) ) ) {
		err = "Email address must use between " + min + " and " + max + " characters.";
		return err;
	}

	for ( i=0; i<l; i++ ) {
		var c = _input.substr(i,1);

		if ( c == blankSpace ) {
			err = "Email address cannot contain spaces.";
			return err;
		}
		if ( c == "@" ) { foundAt = true; }
		if ( c == "." ) { foundDot = true; }
	}

	if ( foundAt === false ) { err = "Email address must contain an \'@\' symbol to be valid.";  }
	if ( foundDot === false ) { err = "Email address must contain a \'.\' symbol to be valid.";  }

	return err;
}

function trimLeadingZeros( n ) {

	while ( true ) {

		if ( ( n.length > 1 ) && ( n.indexOf( "0" ) === 0 ) ) {
			 n = n.substr( 1 );
		}
		else {
			break;
		}
	}
	return n;
}

function validateInt( n ) {

	if ( ( n === null ) || ( n === undefined ) || ( n.length < 1 ) ) {
		return false;
	}
	n = trimLeadingZeros( n );
	return ( "" + parseInt(n) ) == n;
}

function isValidDate() {

	if( validateInt(this.year) === false ) { return false; }
	if( validateInt(this.month) === false ) { return false; }
	if( validateInt(this.date) === false ) { return false; }

	if ( ( this.year > 0 ) && ( this.year < 3000 ) ) {}
	else { return false; }
	if ( ( this.month >= 0 ) && ( this.month <= 12 ) ) {}
	else { return false; }
	if ( ( this.date >= 0 ) && ( this.date <= 31 ) ) {}
	else { return false; }

	return true;
}

function dateToString() {

	return this.year + "/" + this.month + "/" + this.date;
}

function compareDate( _date ) {

	if ( ( this.year*1 == _date.year*1 ) &&
	     ( this.month*1 == _date.month*1 ) &&
	     ( this.date*1 == _date.date*1 ) ) { return 0; }

	if ( this.year*1 < _date.year*1 ) { return -1;}
	if ( this.year*1 > _date.year*1 ) { return 1; }

	if ( this.month*1 < _date.month*1 ) { return -1; }
	if ( this.month*1 > _date.month*1 ) { return 1; }

	if ( this.date*1 < _date.date*1 ) { return -1; }
	if ( this.date*1 > _date.date*1 ) { return 1; }

	return 0;
}

function getGIIDateNow() {

	var now = new Date();
	var newDate = new GIIDate( now.getFullYear(), now.getMonth() + 1, now.getDate() );
	return newDate;
}

function GIIDate( y, m, d ) {

	y += ""; m += ""; d += "";
	this.year = y;
	this.month = trimLeadingZeros( m );
	this.date = trimLeadingZeros( d );

	this.compareDate = compareDate;
	this.isValidDate = isValidDate;
	this.getGIIDateNow = getGIIDateNow;
	this.toString = dateToString;
}


function validateNewsletterForm( f, min, max ) {

	var _error_start = "Sorry, but there was a problem with the information you entered.  Please correct the following errors:\n\n";
	var _error_mid = "";
	var _error_end = "Click OK to continue.";
	var _tab = "- ";
	var _focusField = null;

	if ( !valid_email( f.email.value, min, max ) ) {
		if ( _focusField === null ) { _focusField = f.email; }
		_error_mid += _tab + 'Please enter a valid e-mail address where where the newsletter will be sent.\n';
	}

	if ( _error_mid.length > 0 ) {
		window.alert ( _error_start + _error_mid + "\n" + _error_end );
		_focusField.focus();
		return false;
	}

	return true;
}

function blankBox( box ) {

 box.value = "";
}


function MM_jumpMenu(targ,selObj,restore){ //v3.0
 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
 if (restore) { selObj.selectedIndex=0; }
}


/* ------------------------------------
 *  FORM MANAGER validation
 * ------------------------------------
 *
 * This is temporary.  John created these functions to provide client-side validation
 * for forms whose goal is to generate an email to the user.  These belong ultimately
 * as part of a Form Manager plugin, and they should be integrated with the existing
 * validation code we have above.  In the meantime, they're here under temporary names.
 * - NS 9 February 2009
 */

 function scpFormMgr_validateForm( f ) {

 	var _error_start = "Sorry, but there was a problem with the information you entered.  Please correct the following errors before continuing:\n\n";
 	var _error_mid = "";
 	var _error_end = "Click OK to continue.";
 	var _tab = "- ";
 	var _focusField = null;

 	var requiredFields = f.requiredFields.value;
 	arrayOfStrings = requiredFields.split( "," )
 	var numRequiredFields = arrayOfStrings.length;
 	var fieldName;

 	var formElements = f.elements;
 	var numFormElements = f.elements.length;

 	for ( i=0; i<numRequiredFields; i++ ) {

 		fieldName = arrayOfStrings[i];
 		for ( j=0; j<numFormElements; j++ ) {
 			if ( fieldName == formElements[j].name ) {
 				if ( ( formElements[j].value.length == 0 ) || ( formElements[j].value == null ) )  {
 					if ( _focusField == null ) _focusField = formElements[j];
 					_error_mid += _tab + "Enter " + fieldName + ".\n";
 				}
 			}
 		}
 	}

 	if ( _error_mid.length > 0 )
 	{
 		alert ( _error_start + _error_mid + "\n" + _error_end );
 		_focusField.focus();
 		return false;
 	}

 	return true;
 }

 function scpFormMgr_activateForm( f ) {

 	if ( scpFormMgr_validateForm( f ) )  {

 		f.action = "/mysitecaddy/actions.sitecaddy.websitecaddy.email.do";
 		f.submit();
 	}
}