/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function checkSummaryForms(arr) {
    var check = true;
    jQuery.each(arr, function(key, val) {
        if(val == false && $('[name="'+key+'"]').is(":visible")) check = false;
    });
    return check;
}

function formValidate(formID, fieldsToCheck, fieldsRequires, editable, delay)
{
    fieldsToCheck = (formID + ' ' + fieldsToCheck);

    var buff; //for checkbox
    var comparepassword = false;
    var real_comment_state = false;
    var real_source_state = false;
    //INIT
    $(formID+' input[type="submit"]').attr('disabled','disabled');
    //default delay for keyup
    if(delay===undefined) delay = 500;
    var fieldsValidateValue = {};
    //clone array - check all
    $.extend(fieldsValidateValue, fieldsRequires);

    //Fill tab with false value
    jQuery.each(fieldsRequires, function(key, val) {
        fieldsValidateValue[key] = false;
    }); //END of INIT

    //Catch an EVENT
    $(fieldsToCheck).bind("keyup change", function() {
    
    //For checkbox missing value
    if(this.type == 'checkbox' && !$(this).is(':checked')) buff = 0;
    if(this.type == 'checkbox' && $(this).is(':checked')) buff = 1;

    if (this.value != this.lastValue || this.type == 'checkbox') {      
      
    if (this.timer) clearTimeout(this.timer);

    //Checking if this field is in fieldsRequires array
    if(fieldsRequires[this.name] != undefined) {
      $(formID+' input[type="submit"]').attr('disabled','disabled');
    //Global defines
    var data;
    var field = this;

            $('#'+field.name).show().html('<div id="check_note"><img src="../form/ajax-loader.gif" height="16" width="16" /> Sprawdzam ...</div>');

            this.timer = setTimeout(function () {
            data = field.value;
            if(buff==0 && field.type == 'checkbox') data = 0;

            $.ajax({
                type: "POST",
		url: "../form/ajaxQuery.php?validate="+fieldsRequires[field.name],
                data: 'value='+data,
                dataType: 'json',
                cache: false,
		success: function(obj){
                    //var obj = jQuery.parseJSON(msg);
                    if(obj.valid) {
                        //GOOD
                        fieldsValidateValue[field.name] = true;
                        $(field).attr("class", "report-true-input");
                        $('#'+field.name+'').attr("class", "report-box-true").html(obj.report).show();
                        if(field.name == 'repassword') comparepassword = true;
                        // ---------------------- Comment and Source option   ---------------------- //
                        if(field.name == 'comment') real_comment_state = true;
                        if(field.name == 'source') real_source_state = true;
                        if(field.name == 'comment') {
                            if( real_source_state == false && ($(formID+' input[name="source"]').val() == 'http://' || $(formID+' input[name="source"]').val() == '')) {
                            fieldsValidateValue['source'] = true;
                            $(formID+' input[name="source"]').attr("class", "report-true-input");
                            $('#source').attr("class", "report-box-true").html('').show();
                        }}
                        if(field.name == 'source') {
                            if( real_comment_state == false && $(formID+' textarea[name="comment"]').val() ==  '') {
                            fieldsValidateValue['comment'] = true;
                            $(formID+' textarea[name="comment"]').attr("class", "report-true-input");
                            $('#comment').attr("class", "report-box-true").html('').show();
                        }}
                        // -------------------------------------------------------------------------- //
                    } else {
                        //WRONG
                        fieldsValidateValue[field.name] = false;
                        $(field).attr("class", "report-false-input");
                        $('#'+field.name+'').attr("class", "report-box-false").html(obj.report).show();
                        // ---------------------- Comment and Source option   ---------------------- //
                        if(field.name == 'comment') real_comment_state = false;
                        if(field.name == 'source') real_source_state = false;
                        if(field.name == 'comment') {
                            if(real_source_state == false)
                            {
                                fieldsValidateValue['source'] = false;
                                $(formID+' input[name="source"]').attr("class", "report-false-input");
                                $('#source').attr("class", "report-box-false").html('Niepoprawny adres').show();
                            }
                        }
                        if(field.name == 'source') {
                            if(real_comment_state == false)
                            {
                                fieldsValidateValue['comment'] = false;
                                $(formID+' textarea[name="comment"]').attr("class", "report-false-input");
                                $('#comment').attr("class", "report-box-false").html('Minimalna ilość znaków to: 3').show();
                            }
                        }
                        if(real_comment_state == true && real_source_state == false && ($(formID+' input[name="source"]').val() == '' || $(formID+' input[name="source"]').val() == 'http://')) {
                            fieldsValidateValue['source'] = true;
                            $(formID+' input[name="source"]').attr("class", "report-true-input");
                            $('#source').attr("class", "report-box-true").html('').show();
                        }
                        if(real_source_state == true && real_comment_state == false && $(formID+' textarea[name="comment"]').val() == '') {
                            fieldsValidateValue['comment'] = true;
                            $(formID+' textarea[name="comment"]').attr("class", "report-true-input");
                            $('#comment').attr("class", "report-box-true").html('').show();
                        }
                        // -------------------------------------------------------------------------- //
                    }
                    //PASSWORD TWO FIELDS TOGETHER
                    if(fieldsValidateValue['password'] == true && fieldsValidateValue['repassword'] == true || fieldsValidateValue['password'] == true && comparepassword) {
                        if($(formID+' input[name="password"]').val() != $(formID+' input[name="repassword"]').val()) {
                            fieldsValidateValue['repassword'] = false;
                            $('#fail-pass').show();
                        } else {
                            fieldsValidateValue['repassword'] = true;
                            $('#fail-pass').hide();
                        }
                    }       
                    //SUBMIT Manipulation
                    if(checkSummaryForms(fieldsValidateValue)) $(formID+' input[type="submit"]').removeAttr('disabled');
                    else $(formID+' input[type="submit"]').attr('disabled','disabled');
                }
            });

            }, delay);
    }

    this.lastValue = this.value;
    }

    });

    //EDITABLE
    if(editable == true) $(fieldsToCheck).trigger('change');
}
