/*
 * This function is called after the completion of the ajax validation request.
 * It looks through the JSON data. And displays the errors if it needs or if there
 * are no errors, it evaluates the code in yzValidator_eval_onsuccess hidden tag.
 */
function yzAV_JSONUpdater(request, json, hidden_id)
{

  var numElementsInResponse = json.length;
  var elementId;
  var elementText;
  //Looks for the form error <div> by its class name
  var formErrorDivs = $$('.form_error');
  var numFormErrorDivs = formErrorDivs.length;
  //Loops through all of the form error div's to
  //delete any error messages from previous errors
  for(var i = 0; i < numFormErrorDivs; i++)
  {
        Element.update(formErrorDivs[i].id, "");
        Element.removeClassName(formErrorDivs[i].id, "lineError");
        Element.removeClassName(formErrorDivs[i].id, "lineInfo");
        Element.hide(formErrorDivs[i].id);        
  }
  //Evaluate the code in yzValidator_eval_onsuccess tag if there are no errors
  if(numElementsInResponse == 0)
  {
    eval($(hidden_id).value);
  } else
  {

    
    //If there are errors, loop through the JSON data and
    //update each form_error div with its corresponding message
    for (var i = 0; i < numElementsInResponse; i++)
    {
        elementId=json[i][0];
        elementText=json[i][1];
        var ele = $(elementId);
        if(ele) {
            ele.update("<span>"+elementText+"</span>");
            ele.addClassName("lineError");
            if(navigator.userAgent.indexOf("Opera") == 0)
                ele.show();
            else
                new Effect.Appear(ele, { duration: 0.5 });
        }
    }
  }
}


function yzAV_JSONUpdaterElement(request, json, element) {
    var numElementsInResponse = json.length;
    var elementId;
    var elementText;
    var errorDiv = $("error_for_" + element.id);
    
    
    // clear element error field
    if (errorDiv){
        Element.hide(errorDiv.id);
        Element.update(errorDiv.id, "");
        Element.removeClassName(errorDiv,"lineError");
        Element.removeClassName(errorDiv,'lineInfo');
    }
  
    
    if(numElementsInResponse == 0) {
        // no errors
        
    } else {
        //If there are errors, loop through the JSON data and
        //update each form_error div with its corresponding message
        for (var i = 0; i < numElementsInResponse; i++) {
            elementId=json[i][0];
            elementText=json[i][1];
            Element.update(elementId, "<span>"+elementText+"</span>");
            Element.addClassName(elementId,"lineError");
            //Element.show(elementId);
            new Effect.Appear(elementId);
        }
    }
}

// add Element prototype methods
Element.addMethods({
    ypValidate: function(element) {
        var wrapper = element.up().previous().firstDescendant();
        var in_error = wrapper.hasClassName('lineError');

        //if(!in_error) {
        //    element.onkeyup = function() { ypDelayedInput(this,this.sfValidate.bind(this)); };
        //    element.onblur = function() {
        //        var infoBox = wrapper.select('div.lineInfoBox').first();
        //        if(infoBox) {
        //            infoBox.remove();
        //        }
        //    };
        //    
        //    var tpl = new Template('<div class="lineInfoBox"><div class="lineInfoBorder">#{hint}</div><div class="lineInfoBg-1"></div><div class="lineInfoBg-2"></div><div class="lineInfoBg-4"></div></div>');
        //    var hintText = element.getAttribute('title');
        //    
        //    if(hintText) {
        //        wrapper.hide();
        //        wrapper.addClassName('lineInfo');
        //        wrapper.innerHTML = tpl.evaluate({hint:hintText});
        //        new Effect.Appear(wrapper);
        //    }
        //}
    },

    sfValidate: function(element) {
        if (arguments.length == 2){
            element.callback = arguments[1];
        }else{
            element.callback = yzAV_JSONUpdaterElement;
        };
        var params = "el=" + element.name + "&" + $(element.form.name).serialize();
        var req = new Ajax.Request(ElementValidationURL, {
             asynchronous: true,
             evalScripts:  false,
             onComplete: function(request, json) {
                 element.callback(request, json, element);
             },
             parameters: params
         });
    }
});
