function removeLightBox(t){

	$('#error_overlay').fadeOut("fast",function(){										
		if (document.getElementById('error_overlay')){
			document.body.removeChild(document.getElementById('error_overlay'));
		}
	});
	
	$('#error_cont').fadeOut("fast",function(){																				 
		if (document.getElementById('error_cont')){
			document.body.removeChild(document.getElementById('error_cont'));
		}		
	});			
	
	clearTimeout(t);
}

function LightBox(errors,valid,delay){
	if (!errors)
		errors='';
	if (!valid)
		valid='';
	$.ajax({
		url:'index.php?c=validator&template=blank&valid='+valid+'&errors='+errors+'&s=custom',
		type:'GET',	   
		success:function(msg){
			if (msg){
				createLightBox(msg,delay);
			}
		}
	});
}

function createLightBox(msg,delay){
	if (delay){	
		var t=setTimeout(function(){
			removeLightBox(t);	
		},delay);
	}
														
	var objBody = document.getElementsByTagName("body").item(0);																		
						
									
					
	if (!document.getElementById('error_overlay')){

		var errOverlay = document.createElement("div");
		errOverlay.setAttribute('id','error_overlay');
		objBody.appendChild(errOverlay);	
				
		$(errOverlay).click(function(){
			removeLightBox(t);
		});								
				
	} 
							
				
					
	if (!document.getElementById('errContainer')){
							
		$(errContainer).click(function(){
			removeLightBox(t);
		});																
							
		var errContainer = document.createElement("div");
		errContainer.setAttribute('id','error_cont');
						
		objBody.appendChild(errContainer);																			
												
	}
					

	
	if ($('#error_overlay').css('opacity')!=.7){	
		$('#error_overlay').css({display:'block',opacity:0});				
		$('#error_overlay').fadeTo("fast", .7);
	}

	$("#error_cont").html(msg);
												
	$('#error_cont').css({marginTop:'10%'});
						
	$('#error_cont').fadeIn("slow");
}

$(document).ready(	
	function () {	
			
		var timer=4000;

		$("form").bind("submit",function(){


			var targetContent = $("input[name='act']", this);									

			if (targetContent.length && this.className!='disable_prevalidate'){					
				var submited_form=this;	
				
				var options = {
					url: "index.php?validator=true",
					success:function(msg){
						eval("var response="+msg);
						var act=$(targetContent).attr("value");
						
		
						
						if (response.errors && response.errors[act]){
							LightBox(response.errors[act].join(';'),'',timer);
						} else if (response.valid && response.valid[act]){
																					
							var options = {
								success: function(){
									LightBox('',response.valid[act].join(';'),timer);
								}
							}
							$(submited_form).ajaxSubmit(options); 
							
						} else {
							$(submited_form).unbind("submit");
							submited_form.submit();
							
							return true;							
						}
					}
				}
				
				$(this).ajaxSubmit(options); 
				
				return false;				
			}
	
			
		});
	}
);
