include_once("/lib/moofx/source/moo.fx.js");
include_once("/lib/moofx/source/moo.fx.utils.js");

Event.observe(window, 'load', updateButtonsClass, false);

function turnontab(id) {

 $('tab-'+id).className='runas';
  $(id).style.visibility='visible';
  $(id).style.display='block';
}
function turnofftab(id) {
  $('tab-'+id).className='';
  $(id).style.visibility='hidden';
  $(id).style.display='none';
}


function noenter() {
  return !(window.event && window.event.keyCode == 13); 
}
function getSelectedValue(id,method) {
	var s = $(id);
	for(var i=0; i<s.options.length; i++){
    	if(s.options[i].selected){
			if (method == 1)
				return s.options[i].value;
			else 
				return s.options[i].text;
    	}
	}	
} 

function showMessageForm(id,message)
{
	$(id).removeClassName("xmlLoadMessage");
	$(id).removeClassName("xmlErrorMessage");
	$(id).addClassName("xmlErrorMessage");
	$(id).innerHTML=message;

}
function initer() {
	
}

var url ;
var cpurl;
var globale;
var includedfiles = new Array();

function ExtAjax () {
	
	var obj  = new Object();
	obj._messagefx = null;
	obj.response = null;
	obj.method =  'get';
	obj.buttons = new Object();
	obj.pressed = null;
	obj.myEventHandler = null;
	obj.params = new Object();
	obj.uniqueurl = true;
	// id formularza
	obj.form = null;
	// id pola w ktorym pokaze sie zawartosc msg
	obj.messagefield = null;
	// id buttona ktory odpowiada za wyslanie formularza
	obj.submit = null;
	// czy na czas komunikacji zablokowac formularz ?
	obj.lockform = true;
	
	// odblokowac forme po udanej operacji ?
	obj.unlockform = true;

	obj.adebug = false;

	// lista elementow formy ktore nalezy odblokowac
	obj._unlockelements = null;
	obj.ajax = null;
	obj.ajaxBody = null;
	obj.messagefx = null;
	obj.request = null;
	// form action=czyli url	
	obj.action = null;	
	// zawartosc pola ktore bedzie pokazywane gdy trwa komunikacja z serwerem
	obj.connectionmsg = 'Prosz\u0119 czeka\u0107...';
	obj.onSuccessJSON = null;
	obj.onErrorJSON = null;
	obj.onComplete = null;
	obj.onUninitialized = null;
	obj.onLoading = null;
	obj.onInteractive = null;
	obj.onSuccess = null;
	obj.onXML = null;
	obj.onFailure = null;
	obj.onValidate = null;
	obj._formid = null;
	obj._submitid = null;

	obj.doUnlockForm = function() {
	  // musimy z aktualizowac form
	  obj.form.getElements();
		if ( obj.lockform ) 
			for (var i = 0; i < obj._unlockelements.length; i++) {        
				 if (obj._unlockelements[i].type == 'button' )
					obj._unlockelements[i].disabled = false;			  
				else
				{
					obj._unlockelements[i].enable();
				}	
			}
	}
	
	obj._onComplete = function (response) {
		if ( obj.response["result"] > 0 )
		{
			if (obj.unlockform)
				obj.doUnlockForm();
		} else
			obj.doUnlockForm();
			
		obj.notifyEvent("Complete",response);
	}

	obj._onSuccessData = function (result) {
		obj.notifyEvent("SuccessData",result);
	}

	obj._onErrorData = function (result) {
		obj.notifyEvent("ErrorData",result);
	}

	obj._onUninitialized = function () {
		obj.notifyEvent("Uninitialized",null);
	}

	obj._onLoading = function () {
		obj.notifyEvent("Loading",null);
	}

	obj._onInteractive = function () {
		obj.notifyEvent("Interactive",null);
	}
	obj._doShowResponseMessage = function () {
			
			obj._onShowMessage(obj.response["msg"],obj.response["result"] > 0 ? false : true);
			
          	if (  obj.response["result"] > 0 )
				obj.notifyEvent("SuccessData", obj.response);
			else
			{
				if (obj && obj.response && obj.response['result'] == -9999)
				{
				
	    		setTimeout('document.location.href = "'+obj.response["body"]["redirect"]+'"',3000);     		 
				}			
				obj.notifyEvent("ErrorData", obj.response);
			}
	}
	
	obj._onSuccess = function (response,json) {
		obj.ajaxBody = response.responseText;
		obj.response = new Array("body");
		if ( obj.ajaxBody == null ) {
			obj.response["result"] = 0;		
			obj.response["msg"] = "B\u0142\u0105d po\u0142\u0105czenia z  serwerem.";
			
		} else if ( obj.ajaxBody != "" && json == null) {
			obj.response["result"] = 0;		
			obj.response["msg"] = "Nieznany format danych.";
		} else if ( obj.ajaxBody == null && json == null ) {
	
			obj.response["result"] = 0;		
			obj.response["msg"] = "Brak odpowiedzi serwera.";
		}
		else
		// to zmienilem (dodano powyzsze else)
			obj.response=json;

          //obj.response = xml2array(response.responseText);
			//notifyEvent("Success", obj.response, response.responseText);
		obj._doShowResponseMessage();
	}
	obj._runMessageFx = function () {
		obj._messagefx.stop();
		obj.messagefx._start(1,0);
	}
	obj._onShowMessage = function(message,iserror) {


		$(obj.messagefield).removeClassName("xmlLoadMessage");
		if ( obj.messagefield && $(obj.messagefield) ) {			

			if ( iserror )
				$(obj.messagefield).addClassName("xmlErrorMessage");
			else
				$(obj.messagefield).removeClassName("xmlErrorMessage");

          	$(obj.messagefield).innerHTML =  message;
          	
		}
		if (obj._messagefx == null)
			obj._messagefx = new PeriodicalExecuter(obj._runMessageFx, 11);
		else
			obj._messagefx.registerCallback();
//		obj._messagefx.
	//	obj.messagefx._start(1,0);
	
	}
	
	obj._onFailure = function () {
		notifyEvent("Failure",null);
	}
	obj._onValidate = function(button) {
		return obj.notifyEvent("Validate",obj,button);
	}	
	obj.notifyEvent = function(funcname) {
		var a = [];
		if ( obj["on"+funcname] ) {
			a.push(obj);
			for (var x=1; x< arguments.length; x++)
				a.push(arguments[x]);
			return obj["on"+funcname].apply(null,a);
		}
		else
			return 1;
	}
	
	obj._getButtons = function() {
	 globale = obj.execute;
		for (b in obj["buttons"])
			Event.observe($(b), 'click', obj.execute, false);
  
      Event.observe(document, 'keypress', function(e){
	     var code;
	     if (!e) var e = window.event;
	     if (e.keyCode) code = e.keyCode;
	     else if (e.which) code = e.which;
	     if (code == 13) {
	       //alert("Nacisnij przycisk");
	     }
      });	
  }
	
	obj.assignVars = function(args) {
		gb = false;
		for (a in args) {
			//if (typeof obj[a] == "undefined")
			if (a == "params") {
				for (b in args[a])
					obj[a][b] = args[a][b];
			} else if (a == "buttons") {
				gb = true;
				for (b in args[a])
					obj[a][b] = args[a][b];
			} else
				obj[a] = args[a];
										
		}
		
		if ( gb )
			Event.observe(window, 'load', obj._getButtons, false);
	}

	obj._getFormID = function () {
	 if (obj._formid != null) {
	 	obj.form = $(obj._formid);

	 	if ( !obj.messagefield )
	 		obj.messagefield = obj._formid + "-message";
	 	
	 }
	}

	obj.getFormID = function (formid) {
		obj._formid = formid;
		//obj._submitid = submitid;
		Event.observe(window, 'load', obj._getFormID, false);
	}
	obj.addValue = function(name,value) {
  //      if (typeof obj.params[name] == "undefined")
//            obj.params[name] = value; else
      obj.params[name] = value; 
       // if (typeof obj.params[name] != "object")
    //        obj.params[name] = [ obj.params[name], value ]; else
      //  obj.params[name][obj.params[name].length] = value;		
	}	
	obj._initAjax = function () {
	}
	
	obj.simpleExec = function () {

		if ( obj.messagefx == null )
		{
			obj.messagefx = new Fx.Style(obj.messagefield,'opacity',{duration: 1500, fps: 34 });
		}
		p='';
	
		if ( obj.method == "get" && obj.uniqueurl)
			obj.params[encodeURIComponent('uniqid-'+new Date().getTime().toString().substr(5))] =
            new Date().getTime().toString().substr(5) + Math.floor(Math.random() * 100).toString();


		for (var param in obj.params) {
			p += (p.length > 0) ? "&" : "";
			if (typeof obj.params[param] != "object")
				p += encodeURIComponent(param) + "=" + encodeURIComponent(obj.params[param]); 
			else {
				for (var x = 0; x < obj.params[param].length; x++)
					p += encodeURIComponent(param) + "=" + encodeURIComponent(obj.params[param][x]) + "&";
				p = p.slice(0, -1); 							
			}                  
		}
		if ( obj.messagefield && $(obj.messagefield) ) {			

			obj.messagefx.clearTimer();
			obj.messagefx.set(1);
			if (obj._messagefx != null) {
				obj._messagefx.stop();
			}
			$(obj.messagefield).removeClassName("xmlErrorMessage");
			$(obj.messagefield).addClassName("xmlLoadMessage");
			$(obj.messagefield).innerHTML = obj.connectionmsg;
		}
		
		obj.response = new Array();		
		obj.response["msg"]="Nieznana odpowied\u017a\u000a serwera";
		obj.response["result"]="0";
		obj.ajaxBody = "";	
		if (obj.method == 'get') {
			obj.ajax  = new Ajax.Request( obj.url,
			{
				method: "get",
				parametrs: p,
				
				onComplete: obj._onComplete,
				onUninitialized: obj._onUninitialized,
				onLoading: obj._onLoading,
				onInteractive: obj._onInteractive,
				onSuccess: obj._onSuccess,
				onFailure: obj._onFailure
			}
			);
		} else {
			obj.ajax  = new Ajax.Request( obj.action,
			{
				method: "post",
				parameters: p,

				onComplete: obj._onComplete,
				onUninitialized: obj._onUninitialized,
				onLoading: obj._onLoading,
				onInteractive: obj._onInteractive,
				onSuccess: obj._onSuccess,
				onFailure: obj._onFailure
			});
		}
		
	return obj;	
	}
	
	obj.execute = function(bid) {

		if ( obj.messagefx == null )
		{
			obj.messagefx = new Fx.Style(obj.messagefield,'opacity',{duration: 1500, fps: 34 });
		}
		
		obj.pressed = Event.element(bid).readAttribute("id");
		
		if ( !obj._onValidate(Event.element(bid)) )
			return -1;
			
		if ( ( obj.action == null || obj.action == undefined ) && (obj.form.action != undefined && obj.form.action != null))
			obj.action = obj.form.action;
		if  (obj.form.action != undefined && obj.form.action != null && obj.form.readAttribute("method"))
			obj.method = obj.form.readAttribute("method").toLowerCase();	

		
			
		els = obj.form.getElements();
		if ( obj.lockform )		{
			obj._unlockelements = new Array();
			for (var b in obj["buttons"]) {			
				if ($(b).disabled)
					continue;
				obj._unlockelements[obj._unlockelements.length]=$(b);
				$(b).disabled = true;
					
			}
		}
			
		for (var i = 0; i < els.length; i++) {
			var e = els[i];
			if (e.disabled)
				continue;
//			if (obj.params[e.name])
	//			continue;
			switch (e.type) {
				case 'select-one':
					obj._unlockelements[obj._unlockelements.length]=e;
					if (e.selectedIndex >= 0)
						obj.addValue(e.name, e.options[e.selectedIndex].value);				
					if ( obj.lockform )
						e.disable();
				break;
				case 'select-multiple':
					obj._unlockelements[obj._unlockelements.length]=e;
					for (var j = 0; j < e.options.length; j++)
						if (e.options[j].selected)
							obj.addValue(e.name, e.options[j].value); 					
					if ( obj.lockform )
					e.disable();
				break;
				case 'radio':
				case 'checkbox':
					obj._unlockelements[obj._unlockelements.length]=e;
					if (e.checked)
						obj.addValue(e.name,e.value);
					if ( obj.lockform )
					 e.disable();
				break;
								
				case "aslider":
					//alert('slider '+e.value);
				break;
				case "text":
				case "password":
				case "hidden":
				case "textarea":
					obj._unlockelements[obj._unlockelements.length]=e;
					obj.addValue(e.name,e.value);
					if ( obj.lockform )
						e.disable();
				break;
				
				default:
					//alert('slider '+e.type);
				break;
			}
			
		}
		
		p='';
	
		if ( obj.method == "get" && obj.uniqueurl)
			obj.params[encodeURIComponent('uniqid-'+new Date().getTime().toString().substr(5))] =
            new Date().getTime().toString().substr(5) + Math.floor(Math.random() * 100).toString();
        
 		obj.params["submitby"] = encodeURIComponent(Event.element(bid).name);
 		
		for (var param in obj.params) {
			p += (p.length > 0) ? "&" : "";
			if (typeof obj.params[param] != "object")
				p += encodeURIComponent(param) + "=" + encodeURIComponent(obj.params[param]); 
			else {
				for (var x = 0; x < obj.params[param].length; x++)
					p += encodeURIComponent(param) + "=" + encodeURIComponent(obj.params[param][x]) + "&";
				p = p.slice(0, -1); 							
			}                  
		}
		//alert(p);
		if ( obj.messagefield && $(obj.messagefield) ) {			

			obj.messagefx.clearTimer();
			obj.messagefx.set(1);
			if (obj._messagefx != null) {
				obj._messagefx.stop();
			}
			$(obj.messagefield).removeClassName("xmlErrorMessage");
			$(obj.messagefield).addClassName("xmlLoadMessage");
			$(obj.messagefield).innerHTML = obj.connectionmsg;
		}
		
		obj.response = new Array();		
		obj.response["msg"]="Nieznana odpowied\u017a\u000a serwera";
		obj.response["result"]="0";
		obj.ajaxBody = "";	
		if (obj.method == 'get') {
		
			obj.ajax  = new Ajax.Request( obj.url,
			{
				method: "get",
				params: p,
				onComplete: obj._onComplete,
				onUninitialized: obj._onUninitialized,
				onLoading: obj._onLoading,
				onInteractive: obj._onInteractive,
				onSuccess: obj._onSuccess,
				onFailure: obj._onFailure
			}
			);
		} else {
			obj.ajax  = new Ajax.Request( obj.action,
			{
				method: "post",
				parameters: p,

				onComplete: obj._onComplete,
				onUninitialized: obj._onUninitialized,
				onLoading: obj._onLoading,
				onInteractive: obj._onInteractive,
				onSuccess: obj._onSuccess,
				onFailure: obj._onFailure
			});
		}
		
	}
	return obj;	
}

ExtAjax.get = function(args) {

	var eajax = new ExtAjax();
	eajax.assignVars(args);
    eajax.messagefield="dummymessagefield";
    eajax.method = "get";
    eajax.lockform = false;
    return eajax.simpleExec();
}

ExtAjax.submitForm = function(formid, args) {
	//alert($('loginprompt'));
	//if (formid == 'undefined' || args == null)
	//	return -1;
	var eajax = new ExtAjax();
	eajax.getFormID(formid);
	eajax.assignVars(args);
	
//	return eajax.execute();		
}



function include_once(script_filename, script_id, randomise) {
  // Inject javascript or css unless one exists with the same id attribute
  var head = document.getElementsByTagName('head').item(0);
  for(var i=0; i<head.childNodes.length; i++) {

    var n = head.childNodes[i];
    var nType = n.nodeName.toLowerCase();
    if ((nType == 'script' || nType == 'link') && script_id != '' && (n.id == script_id)) {
      // duplication
      return;
    }
  }
  include_dom(script_filename, script_id, randomise);
}


function include_dom(script_filename, script_id, randomise) {
  // inject a js or css file into the current DOM
  var t = script_filename.substring(script_filename.lastIndexOf('.')+1);
  if (randomise) {
    if (script_filename.lastIndexOf('?') == -1) {
      script_filename += '?rand='+Math.random();
    }
    else {
      script_filename += '&rand='+Math.random();
    }
  }
  var head = document.getElementsByTagName('head').item(0);

  if (t == 'css'){
    // this is a CSS file
    var file = document.createElement('link');
    file.rel = 'stylesheet';
    file.type = 'text/css';
    if (script_id != "") {
      file.id = script_id;
    }
    file.href = script_filename;
  }
  else {
    // here for file that end in .js, or anything else that we want to execute
    // within a script tags
    var file = document.createElement('script');
    file.language = 'javascript';
    file.type = 'text/javascript';
    if (script_id != '') {
      file.id = script_id;
    }
    file.src = script_filename;
  }
  head.appendChild(file);
  return false;
}
function initscripts(aurl,acpurl) {
	url = aurl;
	cpurl = acpurl;
}

function focusFButton()
{
	this.className = "formbutton hover";
}
function unfocusFButton()
{
//	if (this.enabled)
	this.className = "formbutton";
}

function updateButtonsClass() {
	var divs = $$(".formbutton");
	for(i=0; i <divs.length;i++)
	{
      if(divs[i].className == 'formbutton')
      {
//      	alert(divs[i].onMouseOver ? "ma" : "niema");
			divs[i].onmouseover = focusFButton;
			divs[i].onmouseout = unfocusFButton;

        // divs[i].onmouseover = "this.className='tre'";
//         divs[i].className = 'two';
//         classname = 'two';
      }
    }
}

function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 

function pause(numberMillis) {
  var now = new Date();
  var exitTime = now.getTime() + numberMillis;
  while (true) {
    now = new Date();
    if (now.getTime() > exitTime)
   return;
  }
}

function cpuserLogout()
{
	setTimeout('document.location.href = "https://hostit.pl/panel/logout.html"',100);
  return void(0);     		 

	$("thebody").innerHTML='<div id="closesession" class="onemessage">Zamykanie sesji...</div>'+$("thebody").innerHTML;
	ExtAjax.get({
    	url: cpurl + "logout.html",
    	onSuccessData : function(obj,result) { 
    		$("closesession").innerHTML=result["msg"];
    		setCookie(result["body"]['cookie_name'],result["body"]['cookie_value'],result["body"]['cookie_expire']);
    		setTimeout('document.location.href = "'+result["body"]["redirect"]+'"',1000);     		 
    	 	//document.location = result["body"]["redirect"]; 
    	 }
	});
	return void(0);
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
} 

function setCookie(name,value,seconds)
{
	seconds=2000;
	if (seconds)
	{
		var date = new Date();
		date.setTime(date.getTime()+seconds*1000);
		var expire = "; expires="+date.toGMTString();		
	}
	else
		var expire = "";		
		
	document.cookie=name+"="+value+expire+"; path =/";
}
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}     