//  Function to remove whitespace from a string
function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test( strValue ) ) {
       strValue = strValue.replace( objRegExp, '' );
       if( strValue.length == 0 )
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test( strValue ) ) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace( objRegExp, '$2' );
    }
  return strValue;
}

//  Function to validate that a password and confirmation match
function ValidatePassword( sPassword, sConfirm ) {
	var bOK = false;
	if ( sPassword.toString() == sConfirm.toString() ) {
		bOK = true;
	}
	return bOK;
}

//  Function to validate the String length
function ValidateStringLength( sString, nMin, nMax ) {
	var bOK = false;
	if ( !isNaN( nMin ) && !isNaN( nMax ) ) {
		bOK = ( sString.length < parseInt( nMin ) || sString.length > parseInt( nMax  ) ) ? false : true;
	}
	return bOK;
}


//  Function to validate an email address
function ValidateEmail( sValue) {

	var bOK = false
	if ( sValue.indexOf ('@') > 0 && sValue.indexOf ('.') > 0 ) {
		bOK = true;
	}
	return bOK;
}

/*
 * Checks if a variable is null, undefined or an empty string. 
 * Returns true or false
 */

function ValidateVariable( oVar ){
	var bOK = true;
	if( oVar == null || typeof( oVar ) == "undefined" ){ bOK = false; }
	return bOK;
}

function ValidateString( sVar ){
	var bOK = true;
	bOK = ValidateVariable( sVar );
	if( sVar == "" ){ bOK = false; }
	return bOK;
}
function ValidateArray( sVar, nLength ){
	var bOK = false;
	if( !checkNumber( nLength ) ){ nLength = 1; }
	if( ValidateVariable( sVar ) && sVar.length>=nLength){ bOK = true; }
	return bOK;
}

/**
 * Checks if a number is valid. Returns the message provided if not.
 */
function checkNumber( nValue, sMSG, aExtra ){
	var bOK = false;
	nValue = parseInt( nValue );
	if( !isNaN( nValue ) ){
		bOK = true;
	}
	if( aExtra != null ){
		var bTempOK = true;
		for( var sExtra in aExtra ){
			if( !eval( nValue +sExtra+aExtra[ sExtra ] ) ){
				bTempOK = false;
			}
		}
		bOK = bTempOK;
	}
	
	if( bOK == true ){
		return nValue;
	} else {
		if( sMSG != null && typeof( sMSG ) != "undefined" ){
			return sMSG;
		} else {
			return bOK;
		}
	}
}

/**
 * Checks if a date is valid. Returns the message provided if not.
 */

function checkDate( nValue, sMSG, aExtra, bFull ){
	
	var sRetVal;
	var sCheck = checkNumber( nValue, null, aExtra );
	if( sCheck ){
		sRetVal = GetHumanDate( sCheck, bFull );
	} else {
		sRetVal = sMSG;
	}
	return sRetVal;
}

/**
 * Checks if a string is valid. Returns the message provided if not.
 */
function checkString( sValue, sMSG, aExtra ){
	
	var bOK = false;
	//Output( "Checking value: "+sValue );
	//sValue = parseInt( sValue );
	if( sValue != null 
			&& typeof( sValue ) != "undefined" ){
		bOK = true;
	}
	if( aExtra != null ){
		var bTempOK = true;
		for( var sExtra in aExtra ){
			if( !eval( sValue +sExtra+aExtra[ sExtra ] ) ){
				bTempOK = false;
			}
		}
		bOK = bTempOK;
	}
	
	if( bOK == true ){
		return sValue;
	} else {
		if( sMSG != null && typeof( sMSG ) != "undefined" ){
			return sMSG;
		} else {
			return bOK;
		}
	}
}



