/**@return a descriptive string about the object*/
function toDescriptiveString( object ) {
    var message = ""
    for ( i in object )   try {    message += i+": "+object[i]+"\n"   } catch (ex) {}
    return message
}

function isEmpty(obj) {
    for (index in obj)
        return false;
    return true;
}

/**
 * Changes the feedback type. Possible values for parameter type are:
 * <ul>
 *   <li>normal</li>
 *   <li>error</li>
 *   <li>success</li>
 * </ul>
 */
function setFeedbackType(type) {
    $('#feedbacks .content').attr('class', 'content ' + type + '-message');
}

/**@return true if there isn't some error, false otherwise.*/
function checkAjaxErrors(obj, data, objFeedback) {
    setFeedbackType('error');
    if ((data.errors == null || data.errors.length == 0) && (data.fieldErrors == null || isEmpty(data.fieldErrors))) {
        obj.html('');
        return true;
    } else {
        var message = '';
		
        message += '<ul>';
        if (data.errors != null)
            for (error in data.errors)
                message += '<li>' + data.errors[error] + '</li>';
        if (data.fieldErrors != null) 
            for (fieldErrors in data.fieldErrors)
                message += '<li>' + data.fieldErrors[fieldErrors] + '</li>';
        message += '</ul>';
        obj.html(message);

        if (objFeedback)
            objFeedback.show();
        else
            $("#feedbacks").show();

        return false;
    }
}

function setPasswordLevel(targetId, level) {
    if (targetId != null && level != null && level >= 0 && level <= 100)
        $("#" + targetId).css("margin-left", level + "px");
}

function showHelpBox(obj, e) {
    var objInfoBox = obj.parentNode.getElementsByTagName("div")[0];
    var objLink = obj;
		
    if (objInfoBox.style.visibility == "visible") {
        $(objInfoBox).css("visibility", "hidden");
        $(objInfoBox).attr("class","info-box");
        document.onmousedown = function() {}
    } else {
        if (typeof(event) != "undefined")
            var mouse = event;
        else
            var mouse = e;
		
        var topValue = mouse.clientY + document.documentElement.scrollTop  - (objInfoBox.offsetHeight + 45);
        var leftValue = mouse.clientX + document.documentElement.scrollLeft - (objInfoBox.offsetWidth + 10);
        var classInfoBox = "info-box info-box-type-1";
		
        if (topValue < 0 && leftValue < 0) {
            topValue = mouse.clientY + document.documentElement.scrollTop + 45;
            leftValue = mouse.clientX + document.documentElement.scrollLeft + 10;
            classInfoBox = "info-box info-box-type-4";
        } else if (topValue >= 0 && leftValue < 0) {
            leftValue = mouse.clientX + document.documentElement.scrollLeft + 10;
            classInfoBox = "info-box info-box-type-2";
        } else if (topValue < 0 && leftValue >= 0) {
            topValue = mouse.clientY + document.documentElement.scrollTop + 45;
            classInfoBox = "info-box info-box-type-3";
        }
		
        $(objInfoBox).attr("class",classInfoBox);
		
        $(objInfoBox).css("visibility", "visible");
        $(objInfoBox).css("top", topValue + "px");
        $(objInfoBox).css("left", leftValue + "px");
		
        document.onmousedown = function(e) {
            if (typeof(event) != "undefined")
                var mouse = event;
            else
                var mouse = e;
		
            if (!checkClick(mouse.clientX, mouse.clientY, getLeftPosition(objInfoBox), getTopPosition(objInfoBox), getWidth(objInfoBox), getHeight(objInfoBox)) &&
                !checkClick(mouse.clientX, mouse.clientY, getLeftPosition(objLink), getTopPosition(objLink), getWidth(objLink), getHeight(objLink))) {
                showHelpBox(objLink);
            }
				
        }
    }
}

function checkMaxSize(obj, maxsize, counter) {
    if (obj.value.length > maxsize) {
        obj.value = obj.value.substring(0,maxsize);
    }
    
    if (counter != null) {
        document.getElementById(counter).innerHTML = maxsize - obj.value.length;
    }
}

function getTopPosition(obj) {
    var finalValue = 0;
	
    while (obj != document && obj != null) {
        finalValue += obj.offsetTop;
        obj = obj.offsetParent;
    }
	
    return finalValue;
}

function getLeftPosition(obj) {
    var finalValue = 0;
	
    while (obj != document && obj != null) {
        finalValue += obj.offsetLeft;
        obj = obj.offsetParent;
    }
	
    return finalValue;
}

function getWidth(obj) {
    return obj.offsetWidth;
}

function getHeight(obj) {
    return obj.offsetHeight;
}

function checkClick(mouseX, mouseY, boxX, boxY, boxW, boxH) {
    if (mouseX + document.documentElement.scrollLeft > boxX && mouseX + document.documentElement.scrollLeft < boxX + boxW) {
        if (mouseY + document.documentElement.scrollTop > boxY && mouseY + document.documentElement.scrollTop < boxY + boxH) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

function setupCaptchaAjaxValidationForm( successURL, getCaptchaFunction ) {
    $(
        function() {
            $( "[method=post]" ).ajaxForm(
                {
                    success:
                        function( data ) {
                            if( checkAjaxErrors( $("#errors"), data ) )    location = successURL
                            else if( typeof(data.fieldErrors.captchaResponse) != "undefined" )  getCaptchaFunction()
                        },
                    dataType: "json"
                }
            )
        }
    )
}