


function setError(prefix, msg, type){
	if( type == 'err' ){
		//$('#input_' + prefix).addClass('inputerror');
		$('#label_' + prefix).css('color','#DA8913');
		$('#label_' + prefix).html(msg);
	}
	else if( type == 'ok' ){
		//$('#input_' + prefix).addClass('default');
		$('#label_' + prefix).css('color','#409000');
		$('#label_' + prefix).html(msg);
	}
}



function sendObjectQuery(){
	//debugger
	var adress = root_url + '/message/send';
	$.ajax({
   	type: 'POST',
   	url: adress ,
   	data: $("#formemail").serialize() ,
		beforeSend: function(){
			$('#progress').addClass("progressbar").fadeIn();
		},
   	success: function(json){ 
			if( json.status == 'error' ){
			
				if( json.error.name.code == 'empty' ){
					setError('name', json.error.name.msg, 'err');
				}
				else{
					setError('name', 'Imię Nazwisko. Ok.', 'ok');
				}
				if( json.error.email.code == 'empty' ){
					setError('email', json.error.email.msg, 'err');
				}
				else if( json.error.email.code == 'wrong' ){
					setError('email', json.error.email.msg, 'err');
				}
				else{
					setError('email', 'Adres Email. Ok.', 'ok');
				}
				if( json.error.message.code == 'empty' ){
					setError('message', json.error.message.msg, 'err');
				}
				else{
					setError('message', 'Treść wiadomości. Ok.', 'ok');
				}
				$('#progress').removeClass("progressbar").fadeOut();
			}
			else if( json.status == true ){
				//$('#progress').removeClass("progressbar").fadeOut('slow');
				window.location = json.redirtect_url;
				//alert(json.redirtect_url);
			}
		},
		complete: function(){
			setAjaxCorner();
	  	},
   	dataType: 'json'
 	});
	
}

 

function addObject($action){
	//debugger
	var adress = root_url + '/formobject/' + $action;
	
	$.ajax({
   	type: 'POST',
   	url: adress ,
   	data: $("#addobject").serialize() ,
		beforeSend: function(){
			$('#progress').addClass("progressbar").fadeIn();
		},
   	success: function(json){ 
			//alert(json.status);
			if( json.status == 'error' ){
				if( json.error.length > 0){
					for( $i=0; $i<json.error.length; $i++  ){
						//alert( json.error[$i].out+':'+json.error[$i].err );
						if( json.error[$i].err == 1 ){
							
							$('#' + json.error[$i].out).removeClass('oklabel');
							$('#' + json.error[$i].out).addClass('errorlabel').fadeIn('slow');
							
							if( json.error[$i].type != 'empty' ){
								$('#' + json.error[$i].out).html(json.error[$i].msg);
							}
						}
						else if( json.error[$i].err == 2 ){
							$('#' + json.error[$i].out).removeClass('errorlabel');
							$('#' + json.error[$i].out).addClass('oklabel').fadeIn('slow');
							if( json.error[$i].msg != '' ){
								$('#' + json.error[$i].out).html(json.error[$i].msg);
							}
						}
					}
				}
				$.scrollTo( '#addobject', 800);
			}
			else{
				$("#addobject").submit();
			}
			$('#progress').removeClass("progressbar").fadeOut();
			
		},
		complete: function(){
			setAjaxCorner();
	  	},
   	dataType: 'json'
 	});
	
}

function checkForm(){

	var confirmed = $('[name=regulamin]:checked').val();
	
	if( confirmed == '1'){
		window.location = root_url + '/obiekt/potwierdzenie';
	}
	else{
		$('#label_reglamin').css('color','#FF0000');
		$('#label_reglamin').html('Musisz zaakceptować regulamin aby dodać obiekt!');
		$('#label_reglamin').effect("pulsate", { times:2 }, 300);
	}
}

function pulsate(div){
	$(div).effect("pulsate", { times:5 }, 300);
}

function addObjectType(){
  
	if( ! $("#input_type_id").val() ){
		setError('type_id', 'Pole <b>Typ obiektu jest obowiązkowe</b>', 'err');
	}
	else{
		$("#typeobject").submit();
	}

}

function setError(prefix, msg, type){
	if( type == 'err' ){
		//$('#input_' + prefix).addClass('inputerror');
		$('#label_' + prefix).css('color','#D80000');
		$('#label_' + prefix).html(msg);
	}
	else if( type == 'ok' ){
		//$('#input_' + prefix).addClass('default');
		$('#label_' + prefix).css('color','#409000');
		$('#label_' + prefix).html(msg);
	}
}
