/// <reference path="i.js" />

// jjm - Extended _validate to call a user-defined function using special keyword

// The value of special: is a string containing the invocation of a user-defined function that takes no arguments 

// (it operates on the form elements) and returns a boolean, specialError: is the message to post if the invocation returns false

// For example -- special: 'Validator.do_passwords_match()', specialError: 'The passwords do not match.'

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function QuickValidator(qvObj, formEl, errorEl){

    var _qvObj = qvObj;

    for(var i in _qvObj){

			_qvObj[i].isValid = false; //todo: create actual objects: instead of dynamically adding this property (for defaults);

			_qvObj[i].passedRegex = true;

			_qvObj[i].passedSpecial = true; // jjm

		}

    this.Valid = false;

    this.Validate = function(){

    	_validate();

        UpdateErrorList();

        for(var i in _qvObj){

            if(!_qvObj[i].isValid){

                return false;

            }

        }

        return true;

    }

    function _validate(){ 

    	for(var i in _qvObj){

            var name = _qvObj[i].name;

	     //alert("myObject is " + _qvObj[i].toSource());
            var value = $(_qvObj[i].inputEl).val();

            var valid = true;

            if(value.length >= _qvObj[i].mincharlength){

               if(_qvObj[i].regex != undefined){

                	var regex = _qvObj[i].regex;

                    if (regex.test(value)){

                    	_qvObj[i].passedRegex = true;  	

                    } else {

                    	_qvObj[i].passedRegex = false;

                    	NotValid(_qvObj[i]);

                    }

                }

                if (_qvObj[i].special != undefined){

                	var specialfn = _qvObj[i].special;

                	if (eval(specialfn)){

                		_qvObj[i].passedSpecial = true;

                	} else {

                		_qvObj[i].passedSpecial = false;

                		NotValid(_qvObj[i]);

                	}

                } 

                if (_qvObj[i].passedRegex && _qvObj[i].passedSpecial) validated(_qvObj[i]);

            }

            else {

            	_qvObj[i].passedRegex = true;

            	_qvObj[i].passedSpecial = true;

            	NotValid(_qvObj[i]);

            }

        }

    }

    function UpdateView(){

        $(formEl).find('.PassedValidation').parent().removeClass('Invalid').addClass('Valid');

        $(formEl).find('.FailedValidation').parent().removeClass('Valid').addClass('Invalid');

    }

    function NotValid(aQvObj, error){

		aQvObj.isValid = false;

        if($(aQvObj.inputEl).hasClass('TriedInput')){

            $(aQvObj.inputEl).removeClass('PassedValidation').addClass('FailedValidation');

            UpdateView();

        }

    }

    function validated(aQvObj){

        aQvObj.isValid = true;

        $(aQvObj.inputEl).removeClass('FailedValidation').addClass('PassedValidation');

        UpdateView();

    }

    

    function UpdateErrorList(){

        //redo this...

        $(errorEl).html('');

        var errors = '';

        var errorTemplate = '<li></li>';

        var message;

        for(var i in _qvObj){

            if(!_qvObj[i].isValid){

            	//message = (!_qvObj[i].passedRegex) ? _qvObj[i].regexError : _qvObj[i].message;

            	// jjm prioritize which error to display

            	if (!_qvObj[i].passedRegex) message = _qvObj[i].regexError;

				else if (!_qvObj[i].passedSpecial) message = _qvObj[i].specialError;

				else message = _qvObj[i].message;

            	$(errorEl).append($(errorTemplate).append(message));

            }

        }

    }

    

    //handle blur events

   $(formEl).find('*').blur(function(){

        _validate();

    })

    .change(function(){

        _validate();

    })

    .focus(function(){

        $(this).addClass('TriedInput');

    })

}


