validation = {
	validate : function(url, form, model,caller, tinymce ,callbackurl){

		var params = {}; 
		if ( $(caller).attr('type') == 'submit' || $(caller).attr('type') == 'image' || $(caller).is('form')) { // Its a button
			to_save = "1/";
			save =true;
		}else{
			to_save ="0/";
			save = false;
		}

		inputs = $("#" +form).find("input[type=text], input[type=password],input[type=textarea], input[type=hidden], input[type=submit], option:selected, input:checked, textarea");
 		inputs.each(function() { 
				params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; 
		});
	 
		//remove all errors
		var rand_no = Math.random();
		$.ajax({
			type:"POST",
			url: url + to_save + "?rand=" + rand_no,
			data : params,
			dataType: "json",
			timeout: 20000,
			success: function(errors){
				msg_errors='';
				counter =0;
				$.each(errors, function(field,error) {
					tmp_arr = explode('_',field);
					tmp_id = model;
						
					$.each(tmp_arr, function(ind, value) {
						tmp_id+= upperFirst(value);
					});	
						msg_errors+='<li class="error">'+ error + '</li>';
					
					if (counter==0){
						firstErr =	$('#'+tmp_id);
					}
					counter++;
				});
					
				if (counter>0){
					$('div.error-message ul').html(msg_errors);

					if ($('div.error-message').hasClass('hide')){
						$('div.error-message').removeClass('hide');	
						$('div.error-message').addClass('show');
					}
							
					//fix for tinyMCE text area
					if ( $(firstErr).attr('type') == 'textarea' && tinymce) {
							firstErr = $('#' + $(firstErr).attr('id') + '_tbl');
					}
					
					$("html,body").scrollTo({top:$('#'+form).offset().top },'slow');
				}else{

					if  (save) {
						 window.location=callbackurl;
					}
						
					$('div.error-message ul').html('');
					$('div.error-message').removeClass('show');	
					$('div.error-message').addClass('hide');
				}
				
				$('#submenu,#content,#sidebar').equalHeight(50);
			}
		});
	},
	close : function(){
		$('div.error-message').removeClass('show');	
		$('div.error-message').addClass('hide');
	},
	
	tinyMce:function(form){
		inputs = $("#" +form).find(" textarea ");

			inputs.each(function() { 
	 		txt=   $('#' + this.id + '_ifr').contents().find('#tinymce').html();
	 		this.value = txt;
		});
	}
}


function explode(b, s) {
    return s.split(b);
}

function upperFirst(s) {
	return s.substr(0, 1).toUpperCase() + s.substr(1);	
}
