$(document).ready( function() {
    check_messages();
});

/* == Constants ============================================================= */
var r_email = new RegExp('[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?');
var r_redirect_base = new RegExp('^/');
var r_redirect_protocol = new RegExp('^http://');

/* == Functions ============================================================= */
function redirect(url){
    if(!url.match(r_redirect_protocol)){
        if(url.match(r_redirect_base)){
	    url = url_base()+url;
        }else{
	    url = url_path()+url;
	}
    }
    window.location = url;
}
function url_path(url){
    if(location.port){
	return location.protocol+'//'+location.host+':'+location.port+location.pathname;
    }else{
	return location.protocol+'//'+location.host+location.pathname;
    }
}
function url_base(url){
    if(location.port){
	return location.protocol+'//'+location.host+':'+location.port;
    }else{
	return location.protocol+'//'+location.host;
    }
}

/* == Msg =================================================================== */
function check_messages(){
    if($('DIV.msg_container')){
	var msgs = $('DIV.msg_container').find('SPAN.msg_container');
	for (var i=0; i < msgs.length; i++){
	    var el = $(msgs[i]);
	    var id = el.attr('id').trim().replace('msg_container_', '');
	    var val = el.html().trim();
	    trigger_msg(id, val);
	    el.remove();
	}
    }
}
function trigger_msg(id, val){
    alert(id+': '+val);
}

/* == Err =================================================================== */
function validate (form){
    form = $(form);
    if(form){
        var req = form.serializeArray();
        req[req.length] = {name : 'validate', value : 1};
		$.post(
			form.attr('action'),
			req,
			function(data){ process_errors(form, data) }
		);
	return false;
    }
    return true;
}

function process_errors (form, data){
    if(form && data.trim()){
	$('DIV.err_container').html(data);

	$('[err]').each(
	    function(){
		restore_err($(this));
	    }
	);
	$('[errcss]').each(
	    function(){
		restore_err($(this));
	    }
	);

	if($('DIV.err_container')){
	    var errs = $('DIV.err_container').find('SPAN.err_container');
	    if(errs.length && $('[err=stack]:first')){
		restore_trigger_err();
		restore_stack_err($('[err=stack]:first'));
	    }
	    for (var i=0; i < errs.length; i++){
		var el = $(errs[i]);
		if(el && el.attr('id')){
		    var id = el.attr('id').replace('err_container_', '').trim();
		    var val = el.html().trim();
		    if(id){
			if(id == 'redirect'){
			    redirect(val);
			    return;
			}
			trigger_err(id, val);
			var stack = 1;
			if($('[err='+id+']:first')){
			    $('[err='+id+']:first').each(
				function(){
				    show_err($(this), val);
				    stack = 0;
				}
			    );
			}
			if($('[errcss='+id+']:first')){
			    $('[errcss='+id+']:first').each(
				function(){
				    show_err($(this));
				    stack = 0;
				}
			    );
			}
			if(stack && $('[err=stack]:first')){
			    show_stack_err($('[err=stack]:first'), id, val);
			}
		    }
		}
		el.remove();
	    }
	}
    }
}
function trigger_err(id, val) {
}
function restore_trigger_err() {
}
function show_err(el, val){
    el = $(el);
    if(val){
	el.attr('err_def', el.html());
	el.html(val);
    }
    if(el.attr('class')){
	el.attr('class', el.attr('class').replace('err',
	    'err_show err_'+(el.attr('err') ? el.attr('err') : el.attr('errcss'))+'_show')
	);
    }
}
function restore_err(el){
    el = $(el);
    if(el.attr('err_def')){
	el.html(el.attr('err_def'))
    }
    if(el.attr('class')){
	el.attr('class', el.attr('class').replace('err_show', 'err'));
	el.attr('class', el.attr('class').replace('err_'+el.attr('err')+'_show', ''));
    }
}
function show_stack_err(el, id, val){
    el = $(el);
    if(el && id && val){
/*  TODOif(
	    id == 'user_not_found' ||
	    id == 'user_banned' ||
	){
*/
	    el.html(el.html()+val+'<br>');

                el.removeClass('err_stack');
                el.addClass('err_stack_show');

/*	}*/
    }
}
function restore_stack_err(el) {
    el = $(el);
    el.html('');
        el.removeClass('err_stack_show');
        el.addClass('err_stack');

}

/* == IE ==================================================================== */
if(typeof String.prototype.trim !== 'function') {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, ''); 
	}
}

