//////////////////////////////////////////////////////////////////////
// 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 lightbox_query_link(url,key,value,filename,add_empty){
	var output="";
	var added=false;

	if (url){
		name=url.split("#");		
		
		if (name[0])
			hu = name[0].split('?');		
		else hu=Array();
	} else
		hu=Array();
	
	if (hu[0])
		filename=hu[0];
	else if (!filename)
		filename='';

	if (hu[1]){	
		
		gy = hu[1].split("&");		
		
		for (i=0;i<gy.length;i++) {
			ft = gy[i].split("=");
			output=output+"&"+ft[0]+"=";
		
			if (ft[0]!=key){
				output=output+ft[1];
			} else {
				output=output+value;
				added=true;
			}
		}			
	} 
	
	if ((!added && value) || (!added && add_empty)){
		output=output+"&"+key+"="+value;
	}
	
	if (output){
		output=output.substring(1);	
		
		if (name[1])
			output=output+"#"+name[1];
	}
	
	return filename+"?"+output;
}



function validator(){
	

		$("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){					
				var submited_form=this;	
				
				var options = {
					url: lightbox_query_link(submited_form.action,'validator',true),
					success:function(msg){
						eval("var response="+msg);
						var act=$(targetContent).attr("value");
						
						$("input, select, textarea",submited_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,submited_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"));
									submited_form.reset();
								}
							}
							$(submited_form).ajaxSubmit(options); 
							
						} else {
							$(submited_form).expire("submit");
							submited_form.submit();
							
							return true;							
						}
					}
				}
				
				$(this).ajaxSubmit(options); 
				
				return false;				
			}
	
			
		});	
}
$(document).livequery(validator);

