//////////////////////////////////////////////////////////////////////
// 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 getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function removeLightBox(settings){	

	if (!settings)
		settings=new Array();	
	
	$('#error_overlay').fadeOut("fast",function(){										
		$("#error_overlay").remove();																	
	});

	switch (settings.effect){
	case 'slideIn':
		$('#error_cont').animate({marginTop:'11%',opacity:0},350,"easeOutExpo",function(){
			$("#error_cont").remove();																			
		});	
		break;
	default:		
	case 'fade':
		$('#error_cont').fadeOut("fast",function(){	
			$("#error_cont").remove();																			
		});
		break;
	}
	
	if (settings.t)
		clearTimeout(settings.t);
}

function LightBox(settings){	
	/*
	settings.delay=>'delay to remove lightbox',
	settings.msg=>'content to go in lightbox',
	settings.disable_overlay=>'hide background from showing',
	settings.disable_close=>true,
	settings.effect=>'effect to use', // fade or default	
	settings.width=>'900', // 900px, overrides default width set in CSS file
	
	settings.mode=>'default', default | errors | valid | info | confirm // errors and valid delimited by ;
	
	settings.confirm=>'javascript eval if true is clicked',
	*/
	
	if (!settings)
		settings=new Array();
		
	if (settings.mode=='confirm'){
		settings.disable_close=true;
		settings.disable_overlay=false;
		settings.delay=0;
	}
	
	//auto removes lightbox after XY secs (delay)
	if (settings.delay>0){	
		var t=setTimeout(function(){
			removeLightBox(settings);	
		},settings.delay);
		
		settings.t=t;							
	}
			
	$("#error_overlay").remove();			
	$("#error_cont").remove();	
	
	$('#error_overlay').unbind("click");	
	$("#error_cont").unbind("click");
	$("#error_cont_cancel").unbind("click");
	$("#error_cont_ok").unbind("click");	
																		
	if (!settings.disable_overlay){
		
		$("body").append('<div id="error_overlay"></div>');		
				
		if (!settings.disable_close){			
			$('#error_overlay').livequery("click",function(){
				removeLightBox(settings);
			});			
		}
		
		$("body").livequery("resize",function(){
			arrayPageSize=getPageSize();
			$('#error_overlay').css({width:arrayPageSize[0],height:arrayPageSize[1]});											 
		});
	}
									
								
	$("body").append('<div id="error_cont"></div>');			
	
	if (!settings.disable_close){	
		$("#error_cont").livequery("click",function(){
			removeLightBox(settings);
		});		
	}
																								
	
	if ($('#error_overlay').css('opacity')!=.5 && !settings.disable_overlay){	
		arrayPageSize=getPageSize();
		$('#error_overlay').css({display:'block',height:arrayPageSize[1],opacity:0});			
		$('#error_overlay').fadeTo("fast", .5);		
	}
	
	if (!settings.msg)
		settings.msg='';
		
	if (!settings.close_button)
		settings.close_button='Close';		
		
	$("#error_cont_close").livequery("click",function(){
		removeLightBox(settings);
		return false;
	});
	
	switch (settings.mode){
	case 'confirm':
		if (!settings.button_ok)
			settings.button_ok='OK';
	
		if (!settings.button_cancel)
			settings.button_cancel='Cancel';
			
		output='<div id="error_cont_button"><a href="javascript:void(0);" id="error_cont_ok">'+settings.button_ok+'</a><a href="javascript:void(0);" id="error_cont_cancel">'+settings.button_cancel+'</a></div>';		
	
		$("#error_cont").html('<div id="error_message" class="confirm">'+settings.msg+output+'</div>');		
		
		//removes previous click and adds new	
		$("#error_cont_cancel").bind("click",function(){
			removeLightBox(settings);											   
		});		

		//removes previous click and adds new		
		$("#error_cont_ok").bind("click",function(){
												  
			if (settings.confirm)
				call_user_func_array(settings.confirm,Array());												  
				
			removeLightBox(settings);							
		});
		
		break;
	case 'info':	
		output='<div id="error_message" class="info">';
		
		if (!settings.disable_close)
			output=output+'<a href="javascript:void(0);" id="error_cont_close">'+settings.close_button+'</a>';
		
		output=output+settings.msg+'</div>';
		
		$("#error_cont").html(output);	
		break;
	case 'valid':
	case 'errors':
		output='<div id="error_message"';
		
		if (settings.mode=='valid')
			output=output+' class="error_valid"';
			
		output=output+'>';
		
		if (!settings.disable_close)
			output=output+'<a href="javascript:void(0);" id="error_cont_close">'+settings.close_button+'</a>';		
		
		output=output+'<ul>';		
		message=settings.msg.split(';');

		if (message.length)
			for(i=0;i<=message.length-1;i++)
				output=output+'<li>'+message[i]+'</li>';			

		output=output+'</ul></div>';		
		
		$("#error_cont").html(output);
		break;
	default:
		$("#error_cont").html(settings.msg);
		break;
	}
	
	if (settings.width)
		$('#error_cont').css({width:settings.width,marginLeft:-(settings.width/2)});		

	
	//animation
	switch (settings.effect){
	case 'slideIn':		
		$('#error_cont').css({marginTop:'9%',display:'block',opacity:0});					
		$('#error_cont').animate({marginTop:'10%',opacity:0.9},800,"easeOutExpo");		
		break;
	default:		
	case 'fade':
		$('#error_cont').css({marginTop:'10%',display:'block'});			
		$('#error_cont').fadeIn("slow");	
		break;
	}

}


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');
				}
				
				LightBox({delay:timer,msg:response.errors[act].join(';'),mode:'errors'});														
			} else if (response.valid && response.valid[act]){
																		
				var options = {
					url:undefined,
					success: function(){
						LightBox({delay:timer,msg:response.valid[act].join(';'),mode:'valid'});																							
					}
				}
				$(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;				
		}
	});										  
});

