function sendAjaxForm(ajaxLink, formId, onSuccess) {
    
    if($('sendBtn')) {
        $('sendBtn').disabled = true;
    }

    new Ajax.Request(ajaxLink,
	{
		method: 'post',
		asynchronous : true,
        parameters: $(formId).serialize(true),
		onSuccess: onSuccess
	}
	);
    return false;
}


errorIds = new Array();
errorIdClasses = new Array();
focused = false;
focusedElement = null;

function cleanErrors() {
	
	for(i=0; i < errorIds.length; i++) {
		if($('error_'+errorIds[i])) {
			$('error_'+errorIds[i]).remove();
		}
		
		
		if($(errorIds[i])) {
			if(errorIdClasses[errorIds[i]]) {
				$(errorIds[i]).className = errorIdClasses[errorIds[i]];
			} else {
				$(errorIds[i]).className = '';
			}
		}
	}
	
	errorIds = new Array();
	errorIdClasses = new Array();
    focused = false;

}

function ajaxFormComplete(response) {
	json = response.responseText;
	obj = eval("("+json+")");
    anz = obj.error_anz;
    cleanErrors();
    //displayErrors
    if(anz > 0) {
        for(i = 0; i < anz; i++) {
            var ids = obj['errors'][i].id.split('|');
            var countIds = ids.length;
            var setError = false;
            if(countIds > 0) { //found errors

                var id = ids[0];
                if($(id)) {

                    //highlight errors
                    for(j = 0; j < countIds; j++) {
                        if($(ids[j])) {
                            if(errorIds.indexOf(ids[j]) == -1) {

                                if(j == 0) {
                                    var errorDiv = new Element('div');
                                    errorDiv.setAttribute('id', 'error_'+id);
                                    errorDiv.className = 'error';

                                    if(setError == false) {
                                        errorDiv.insert('<a name="error"></a>');
                                        setError = true;
                                    }
                                    errorDiv.insert(obj['errors'][i]['errorMsg'])

                                    $(id).previous(0).insert({before: errorDiv});
                                }

                                if($(ids[j]).className) {
                                    errorIdClasses[ids[j]] = $(ids[j]).className;
                                }

                                if($(ids[j]).type == 'text' || $(ids[j]).type == 'password') {
                                    $(ids[j]).className = 'formTextinputError';
                                } else if($(ids[j]).nodeName.toLowerCase() == 'select') {
                                    $(ids[j]).className = 'formSelectError';
                                }
                                errorIds.push(ids[j]);


                                if(focused != true) {
                                    doFocus = setFocus(ids[j]);
                                    focusedElement = ids[j];
                                }

                            }
                        }
                    }

                } else {
                    console.info(id+' not found');
                }
            }

        }

        self.location.href = '#error'
        if(focusedElement != '') {
            if($(focusedElement)) {
                $(focusedElement).focus();
            }
        }

        if($('sendBtn')) {
            $('sendBtn').disabled = false;
        }

    }

    //redirect
    if(obj.redirect) {
        self.location.href = obj.redirect;
    }

    //successFunction
    if(obj.successFunction) {
        eval(obj.successFunction);
    }
    

}

function setFocus(id) {
	if($(id)) {
		if($(id).type == 'text' || $(id).type == 'password') {
			$(id).focus();
			focused = true;
		    return true;
		}
	}
	return false;
}

var emptyInputArray = new Array();
function checkEmptyInput(id) {
	if(emptyInputArray.indexOf(id) == '-1') {
		$(id).value = '';
		emptyInputArray.push(id);
	}
}

function writeImageIframeUploader(attachToId, iframePath, width, height) {
    var iframe = new Element('iframe');
    iframe.setAttribute('src', iframePath);
    iframe.setAttribute('frameborder', 0);
    iframe.style.width = width+'px';
    iframe.style.height = height+'px';
    $(attachToId).update(iframe);
}

function showFrame(url, width, height) {
	var link = new Element('a');
	link.setAttribute('href', url);
	link.setAttribute('rel', 'lyteframe');
	link.setAttribute('rev', 'width: '+width+'px; height: '+height+'px; scrolling: no;');
	myLytebox.start(link, false, true);

}

function test() {
    console.info('test');
}

function showTip(text) {

	Tip(text,
		BGCOLOR, '#22344C',
		FONTCOLOR, '#ffffff',
		BORDERCOLOR, '#333333',
		OPACITY, 80
	)

}
