
var clsInputFieldColorizer = 

Class.create();
clsInputFieldColorizer.prototype = {

    initialize : function(form){
        formElementsArray = Form.getElements($(form));
        for (var i=0;i<formElementsArray.length;i++)
        {
            Element.addClassName(formElementsArray[i],'standard');
            formElementsArray[i].onblur  = this.blur;
            formElementsArray[i].onfocus = this.focus;
        }
    },

    blur : function(){
        Element.removeClassName(this,'focus');
        Element.addClassName(this,'standard');
    },

    focus : function(){
        Element.removeClassName(this,'standard');
        Element.addClassName(this,'focus');
    }
}


var clsCrossForm =

Class.create();
clsCrossForm.prototype = {

    initialize : function(formName,sourcePHP){
        
        //   initial checking     
        if (!window.XMLHttpRequest && !window.ActiveXObject ) return null;
        
        //            inic
        this.formName = formName;
        this.sourcePHP = sourcePHP;
        this.form = Form.getElements(formName);
        
        //        use my input field colorizer class, optionally
  //      new clsInputFieldColorizer(this.formName);
        //        set event handlers on the form elements
        this.form.each(
            function (formElem,index){
                new Form.Element.EventObserver(formElem,this.checkField.bind(this));
                // set the missing id attribs
//                if (!formElem.id) formElem.id = formElem.name;
            }.bind(this)
        );
    },

    checkField : function(formElem){

        this.currentField = formElem;

        switch (this.currentField.type){

            case 'checkbox' :

                var currentCheckBoxesCollection = $A(this.form).findAll(
                function(checkBox){
                  return (checkBox.name == this.currentField.name && checkBox.checked);
                }.bind(this));
    
                var queryString = currentCheckBoxesCollection.collect(
                function(checkBox){
                    var objectCheckBoxHash = function(name,value){this[name] = value;}
                    var checkBoxes = new objectCheckBoxHash(this.currentField.name,checkBox.value);
                    return $H(checkBoxes).toQueryString();
                }.bind(this)).join("&");

            break;

            default :  var queryString = Form.Element.serialize(this.currentField);
        }

        if (!queryString) queryString = this.currentField.name + '=';

        var myAjax = new Ajax.Request(
        this.sourcePHP,
        {
            method: 'post',
            parameters: queryString + '&cFormAjax=' + this.formName,
            onFailure : function(response) {alert("Faliure, there's some kind of error in ajax communication line");},
            onComplete: this.showResponse.bind(this)
        });
    },

    showResponse : function (transport,objectJSON){
        
        // if we've got some kind of error during error checking
        if (objectJSON.result)
        {
            var errorResultContainer =  Builder.node('div',{'id':'errorResultContainer'+objectJSON.id,'class':'errorMessages'});
            errorResultContainer.innerHTML = '<p>'+objectJSON.result+'</p>';
             
                var searchLabelTag = Element.up(this.currentField.id,'div',0)
              
                if (searchLabelTag!=undefined && searchLabelTag.previousSibling!=undefined && searchLabelTag.previousSibling.className=="errorMessages")
                {
                    Element.remove(searchLabelTag.previousSibling);
                }
                searchLabelTag.parentNode.insertBefore(errorResultContainer,searchLabelTag);
                Element.removeClassName('errorResultContainerPic'+objectJSON.id,'errorNo');
                Element.addClassName('errorResultContainerPic'+objectJSON.id,'errorYes');
                    
        }
        else
        {
           
            var errorResultContainer =  Builder.node('div',{'id':'errorResultContainerPic'+objectJSON.id,'class':'errorPic errorNo'});
            
            var searchLabelTag = Element.up(this.currentField.id,'div',0)
              
            if (searchLabelTag!=undefined && searchLabelTag.previousSibling!=undefined && searchLabelTag.previousSibling.className=="errorMessages")
            {
                Element.remove(searchLabelTag.previousSibling);
            }
                                  
            var searchLabelTag = $(this.currentField.id);
            while (searchLabelTag.tagName!='LABEL')
            {
                searchLabelTag = searchLabelTag.previousSibling || searchLabelTag.parentNode;
            }
          
            // if we find an exists one, remove it
            if (searchLabelTag.previousSibling!=undefined && searchLabelTag.previousSibling.className.indexOf('errorPic')!==false)
            {
                Element.remove(searchLabelTag.previousSibling);
            }
            searchLabelTag.parentNode.insertBefore(errorResultContainer,searchLabelTag); 
        }
        
        
        
  
       
    }
}
