function reverseDisplay (object,display) {
  if (typeof (display) == 'undefined') {
    var display = "block";
  }
  if (typeof (object) == 'string') {
    object = document.getElementById (object);
  }
  
  if (object.style.display == "none" || object.style.display == '') { object.style.display = display; alert ('a'); return false; }
  if (object.style.display == display) { object.style.display = "none"; alert ('b'); return false; }
}
// JavaScript Document
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
     
function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
	     
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function loginForm (formObject) {
 var _ajax = new XHConn();
  inputs = formObject.getElementsByTagName ('input');
  var pars ='';
  
  for (var i=0; i<inputs.length; i++){
    pars += inputs[i].name+'='+inputs[i].value+'&';
  }
  /*
   xmlhttp.open(sMethod, sURL+"?"+sVars+'&nocache='+new Date().getTime(), true);
  */
  
  _ajax.connect(formObject.action+'&ajax', "POST", pars, parseLogin);
  
}

function parseLogin (ans) {
    if (ans.responseText.match ("OK")) {
     document.getElementById ('loginError').innerHTML = 'czekaj...';
   document.location = "?site=services";
  } else {
    document.getElementById ('loginError').innerHTML = ans.responseText;
  }
  return false;
}
function dbg (string) {
  if (document.all) {
    alert (string);
  } else {
    console.log (string);
  }
}
function radioFree (name) {
  var c = document.getElementsByTagName ('input');
  for (var a=0; a<c.length; a++){
    if (c[a].type == "radio" || c[a].type == 'checkbox'){
      
      if (c[a].name == name) {
        c[a].checked = false;
      }
    }
  }
}
function buttonizePage (){
  
  var inps = document.getElementsByTagName ('input');
  
  for (var i=0; i<inps.length; i++){
    var inp = inps[i];
    
    if (inp.type == "submit") {
      inp.className = "btn";
    
      if (document.all) {
	inp.onmouseover = function () {
	  this.className = "btnOver";
	}
	inp.onmouseout = function () {
	  this.className = "btn";
	}
      }
    }
  }
}

function isset (id) {
  if (typeof (id) == "undefined") { return false; }
  else { return true; }
}
function wizardMaker (id) {
        
        this.steps = Array ();
        this.onShow = Array (); // Tablica kodu wykonywanego podczas pokazywania panelu z krokami
        this.onNext = Array (); // Tablica kodu wykonywanego podczas zatwierdzania przejŚcia do następnych zakładek (np. walidacja)
        this.nextButtonLabel = "Dalej";
        this.previousButtonLabel = "Wstecz";
        this.nextStep = '';
        this.id = id;
        this.previousStep = '';
        this.stepsFloat = Array ();
        this.history = Array ();
        
        
        
        this.init = function (id,firstStepId) {
            this.currentStep = null;
            this.form = document.getElementById (id);
            this.steps = document.getElementsByTagName ("fieldset");
            this.hideSteps();          
            this.showStep (firstStepId);
        }
        
        
        this.getStepById = function (id) {
          for (var a=0; a<this.steps.length; a++) {
            var step = this.steps[a];
            if (step.id == id) {
              return step;
            }
          }
          //return this.steps.getElementById (id);
        }
        
        this.float = function (from,to) {
          this.stepsFloat [from] = to;
        }
        
        this.showStep = function (id) {
          this.history.push (id);
          
          if (isset (this.stepsFloat[id])) {
            this.nextStep = this.stepsFloat[id];
          }
          var step = this.getStepById(id);
          step.style.display = "block";
          var buttons = step.getElementsByTagName ('button');
          if (buttons.length == 0) {
            var addButtons = true;
          } else {
            var addButtons = true;
            for (var a=0; a<buttons.length; a++){
              if (buttons[a].className == 'wizardButtonNext' || buttons[a].className == 'wizardButtonPrevious'){
                var addButtons = false;
              }
            }
          }
          if (addButtons) { this.addButtons (step); }
          this.currentStep = step;
          eval (this.onShow[step.id]);
          
        }
        /**
        *
        **/        
        this.hideSteps = function () {
          for (var a=0; a<this.steps.length; a++){
              this.steps[a].style.display="none";
          }
          
        }
        
        this.getPreviousStep = function() {
          if (this.history.length == 1) {
            return false;
          } else {
            
            
            var ans = this.history [this.history.length-2];
            this.history.pop();
            this.history.pop();
            return ans;
          }
          
        }
        
        this.addButtons = function (step) {
          var buttonNext = document.createElement("button");
          var buttonPrevious = document.createElement("button");
          buttonNext.className = "wizardButtonNext";
          buttonNext.innerHTML = "Dalej";
          buttonNext.id = this.id + '_next_'+step.id;
          buttonNext.onclick =function () {
            var wizardId = this.id.split ('_')[0];
            
            var _wizard = (eval(wizardId));  // Do obiektu wizard odwołujemy się jako do zmiennej lokalnej "_wizard"
            
            if (_wizard.nextStep.substr (0,3) == 'err') {
              var msg = _wizard.nextStep.split(':')[1];
              alert (msg); // To nie jest alert debugowy, tylko prawdziwy
              return false;
            }
            propagate = true;
            eval (_wizard.onNext[_wizard.currentStep.id]);
            
            if (propagate == false) {
              return false;
            }
            if (_wizard.nextStep == 'submit') {
              this.form.submit();
              return true;
            }
             
            _wizard.previousStep = (_wizard.currentStep.id);
            _wizard.hideSteps();
            _wizard.showStep (_wizard.nextStep);
           
            return false;
          }
          
          buttonPrevious.className = "wizardButtonPrevious";
          buttonPrevious.innerHTML = "Wstecz";
          buttonPrevious.id = this.id + '_prev_'+step.id;
          buttonPrevious.onclick = function () {
            var wizardId = this.id.split ('_')[0];
            var _wizard = (eval(wizardId));  // Do obiektu wizard odwołujemy się jako do zmiennej lokalnej "_wizard"
            var previousStep = _wizard.getPreviousStep();
            if (previousStep == false) {
              history.back();
            } else {
              _wizard.hideSteps();
              _wizard.showStep (previousStep);
            }                        
            return false;
          }
	  var fch = step.firstChild
          step.insertBefore(buttonPrevious,fch);
          step.insertBefore(buttonNext,fch);
        }
      }
      
function regulamin () {
    var frmI = document.getElementById ('frmReg').getElementsByTagName ('input');
    for (var i=0; i<frmI.length; i++){
      var inp=frmI[i];
      if (inp.name == 'type' && inp.checked == true) {
	  var service = inp.value;
      }
    }
    if (service != 'orto' && service != 'mnts' && service != 'premium') {
      return false;
    } else {
	 _ajax = new XHConn();
	 _ajax.connect('regulamin_'+service+'.txt', "GET", '', pasteRegulamin);
    }
}
      
function pasteRegulamin (ans) {
  document.getElementById ('regulaminTxt').innerHTML = ans.responseText;
}


function regAcc (ob) {
 
  if (ob.checked == "yes" || ob.checked == true) {
    
    wiz.onsubmit = '';
  } else {
    wiz.onsubmit = 'alert ("Zaakceptuj regulamin"); propagate = false;';
  }
}