//////////////////////////////////////////////////////////////////////
// validator				                                            //
//                                                                  //
// DOM - select object                                              //
// 								                                    //
// Copyright (C) 2006 - 2008  design project, www.dsnproject.hr     //
//								                                    //
// License: Commercial						                        //
//////////////////////////////////////////////////////////////////////
function call_user_func_array(func, parameters) {
    if (typeof func == 'string') {
        if (typeof this[func] == 'function') { func = this[func]; } else {
            func = (new Function(null, 'return ' + func))();
        }
        if (typeof func != 'function') {
            throw new Exception(func + ' is not a valid function');
        }
    }
    
    return func.apply(null, parameters);
}


function validator(form){
	var timer=4000;
	
	var options = {
		url: "index.php?validator=true",
		success:function(msg){
			eval("var response="+msg);
			var act=$($("input[name='act']", form)).attr("value");
			
			$("input, select, textarea",form).removeClass('req');
			
			if (response.errors && response.errors[act]){
																		
				if (response.errors_fields && response.errors_fields[act]){
					var select_string="";
					for(i=0;i<=response.errors_fields[act].length;i++){
						if (select_string)
							select_string=select_string+', ';
							
						select_string=select_string+'#'+response.errors_fields[act][i];
					}
					$(select_string,form).addClass('req');
				}
				
				alert(response.errors[act].join("\n"));
				
			} else if (response.valid && response.valid[act]){
																		
				var options = {
					url:undefined,
					success: function(){
						alert(response.valid[act].join("\n"));
					}
				}
				$(form).ajaxSubmit(options); 
				
			} else {
				$(form).expire("submit");
				form.submit();
				
				return true;							
			}
		}
	}
	
	$(form).ajaxSubmit(options); 
	
	return false;				
}

$(document).livequery(function(){
	$("form").livequery("submit",function(){	
										  
		var rel_att=$(this).attr("rel");
		var targetContent = $("input[name='act']", this);									

		if (targetContent.length && this.className!='disable_prevalidate' && !rel_att){	
	  		validator(this);
			return false;				
		}
	});										  
});

