/*******************************************************************************
ERROR HANDLING
*******************************************************************************/

// Global instance variable to hold clientSide array of pageError objects
var pageErrors = new Array();
// Global instance variable to ensure that clientSide validation occurs first
var clientSide = true;
// Global instance variable to define how to seperate each error that is thrown
var delimiter = "\n";

// Generic error object constructor
function pageError(message,type,field){
	this.message = (message)?message:"";
	this.type = (type)?type:"specific";
	this.field = (field)?field:"";
}

// Generic error throwing method
function throwError(message,type,field){
	pageErrors[pageErrors.length] = new pageError(message,type,field);
}

// Application specific error presentation method can be any number of implimentations
function presentErrors(specificMessages,genericMessages){
	// if there are no generic error messages than only display specifics
	var errorString = "";
	errorString += (specificMessages != "")?specificMessages + delimiter:"";
	errorString += (genericMessages != "")?"You must complete the following required fields:" + delimiter + genericMessages:"";
	alert(errorString);
	// another example below of a possible presentation style for errors instead of alert box as above
	// delimiter would be changed to "<br>" and you would need a layer/div to write to
	//document.write('<font color="red">' + errorString + '</font>')
}

// Application specific method for collection of error messages, this should work for many
function collectErrors() {
	var specificMessages = "";
	var genericMessages = "";
	// If errors have been thrown on this page collect them into groupings
	if(pageErrors.length > 0){
		for (e=0;e<pageErrors.length;e++){
			if(pageErrors[e].type == "specific"){
					specificMessages += pageErrors[e].message + delimiter;
			}else if(pageErrors[e].type == "generic"){
					genericMessages += pageErrors[e].field + delimiter;
			}
		}
		// Call the presntation handler for diaplaying error messages
		presentErrors(specificMessages,genericMessages);
		// Reset pageErrors to ensure that everthing is revalidated on next submit
		pageErrors = new Array();
		return false;
	// If no "clientSide" errors have been thrown on this page, then submit the form to the server
	// serverSide validation can then take place
	}else{
		if (clientSide == true){
			//document.forms[0].submit();
			return true;
		}
	}
}


// These methods below are not necessary but are included as sample library functions that are used in the example
function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if not empty, otherwise false.
*************************************************/
	var strTemp = strValue;
	strTemp = trimAll(strTemp);
	if(strTemp.length > 0){
		return true;
	}  
	return false;
}

function  validateNumeric( strValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if is numeric, otherwise false.
******************************************************************************/
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 
	//check for numeric characters 
	return objRegExp.test(strValue);
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
	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 validateEmail(email) {
/************************************************
DESCRIPTION: Проверка e-mail на корректность

PARAMETERS: emailaddress;

RETURNS: true or false.
*************************************************/ 
	//matches email
    var thePat = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

	var gotIt = thePat.exec(email); 
	
	if(!gotIt){
		return false;
	}
	return true;
}

function isURL(url) {
/************************************************
DESCRIPTION: Определяем являеться ли URL адресом строка

PARAMETERS: url string;

RETURNS: true or false.
*************************************************/ 
	//matches url
    var thePat = /http:\/\//;
	var gotIt = thePat.exec(url); 
	
	if(!gotIt){
		return false;
	}
	return true;
}
