var g_error_message = "Error(s) detected:\n\n";
var g_error_string = "- ";
var ErrorCollectionMessages = "";
var errorCollection = new Array();
var errorInForm = false;

// Globals

// Class FieldValidator 
var FieldValidator = function() {
}

FieldValidator.prototype = {
Validate: function(name, val) {
        for (idx in RegExLibArray) {
            if (document.forms[0][RegExLibArray[idx][0]].value != null) {
                if (RegExLibArray[idx][0] == name.toLowerCase() && RegExLibArray[idx][1] != "" ) { 
                    var re = new RegExp(RegExLibArray[idx][1], "g");
                    if (!re.test(trim(val))) {   //throw new Exception(g_error_message + g_error_string + RegExLibArray[idx][2]);
                        errorCollection[idx] = RegExLibArray[idx][2];
                        errorInForm = true;
                    }
                }
            }
        }
        return !errorInForm;
    }
};

var ErrorCollectionException = function(ErrorCollection) {
    for( idx in ErrorCollection )
        ErrorCollectionMessages += g_error_string + ErrorCollection[idx] + '\n'; 
}

var Exception = function(message) {
    this.message = message;
}

ErrorCollectionException.prototype.toString = function() {
    return g_error_message + ErrorCollectionMessages;    
}

Exception.prototype.toString = function() {
    return this.message;
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}


function formValidate()
{
    try {
    
        setCaptcharesult(); //force JS date calc--if bot, this will fail.
        if (navigator.userAgent.indexOf("4.0") == 0) { 
            alert('Unknown browser detected. Please use IE, Firefox, Netscape or similar modern browser.');
            return false;
        }
            var lastIndx = 0;        
			var objForm = document.forms[0];
			// alert(objForm);
			if (objForm) 
			{
			    for (var i = 0; i < objForm.length; i++) 
				{
				    if (objForm[i].name) 
				    {
				        lastIndx = i;
				        //  alert('found elements:' + objForm.length );                
				        //document.getElementById(objForm[i].name).style.backgroundColor = "#ffffff";
				        var element = objForm[i];
				        var ename = objForm[i].name;
				        var evalue = objForm[i].value;
				        var objFieldCheck = new FieldValidator();

				        //alert(objFieldCheck + ' name: ' + ename + ' evalue: ' + evalue + ' element ' + element.toString() ); 
				        objFieldCheck.Validate(ename, evalue);
				        objFieldCheck = null;
				    }
				}

				if ( errorInForm ) {
				    throw new ErrorCollectionException( errorCollection );
				}

			}
    	}
    catch (e) 	{
        	alert(e.toString());
            //document.getElementById(objForm[lastIndx].name).style.backgroundColor = "#ffff99";
        	//objForm[lastIndx].focus();
        	ErrorCollectionMessages = "";
        	errorInForm = false;
        	errorCollection = null;
        	errorCollection = new Array();
        	return false;        
    }

    return true;
}

function setCaptcharesult() { //captcharesult
    document.forms[0].captcharesult.value = new Date().getDate();
}
