// variables
var timer;

/**
* detekcja przegladarki
*/
function getBrowser()
{
  var ns4 = (document.layers)? true:false;
  var ns6 = (document.getElementById)? true:false;
  var ie4 = (document.all)? true:false;
  if (ie4)
  {
    if((navigator.userAgent.indexOf('MSIE 5')> 0)||(navigator.userAgent.indexOf('MSIE 6')> 0)||window.opera)
    {
      return('ie5');
    } else {
      return('ie4');
    }
    if (ns6)
    {
      ns6=false;
    }
  } 
  if (ns6) {
    return('ns6');
  } else {
    return('ns4');
  }
  
}


/**
  * Funkcja sprawdza poprawność wpisu w polu (dozwolone są tylko cyfry 0-9 i TYLKO JEDEN znak "."
  * @param object obiekt typu input text
  */
function maskaKwota(pole) {
    var myRegExp = /[^0-9,.-]/g;
    str = pole.value;
    str = str.replace(myRegExp,"");
    wart = str.replace(',', '.');
    tab = wart.split('.');
    wart = tab[0];
    if (tab.length > 1) {
      if (tab[1]!='undefined') {
        wart = wart + '.' + tab[1];
      }
    }

    var oEvent = window.event;

    // pomijamy zmiany dla klawisza TAB - bo tracil sie focus
    if (oEvent && ((oEvent.which && oEvent.which!=9 && oEvent.which!=16 && oEvent.which!=37 && oEvent.which!=39) ||
       (oEvent.keyCode && oEvent.keyCode!=9 && oEvent.keyCode!=16 && oEvent.keyCode!=37 && oEvent.keyCode!=39))) {
        pole.value = wart;
        pole.focus();

    } else if (getBrowser()=='ns6') {
      pole.value = wart;
      pole.focus();
    }
    pole.value = wart;
}

/**
  * Funkcja sprawdza poprawność wpisu w polu (dozwolone są tylko cyfry 0-9
  * @param object obiekt typu input text
  */
function maskaInt(field) {
        var str, field;
        var myRegExp = /[^0-9]/g;
        str = field.value;
        str = str.replace(myRegExp,"");

        var oEvent = window.event;
        // pomijamy zmiany dla klawisza TAB - bo tracil sie focus
        if (oEvent && ((oEvent.which && oEvent.which!=9 && oEvent.which!=16 && oEvent.which!=37 && oEvent.which!=39) ||
           (oEvent.keyCode && oEvent.keyCode!=9 && oEvent.keyCode!=16 && oEvent.keyCode!=37 && oEvent.keyCode!=39))) {
            field.value = str;
            field.focus();

        }  else if (getBrowser()=='ns6') {
        	field.value = str;
            field.focus();
        }
}

/**
  * Funkcja sprawdza poprawność wpisu w polu (dozwolone są tylko cyfry 0-9 i TYLKO JEDEN znak "."
  * @param object obiekt typu input text
  */
function maskaFloat(field) {
        var str, field;
        var myRegExp = /[^0-9,.]/g;
        str = field.value.replace(',','.');
        str = str.replace(myRegExp,"");

        var oEvent = window.event;
        // pomijamy zmiany dla klawisza TAB - bo tracil sie focus
        if (oEvent && ((oEvent.which && oEvent.which!=9 && oEvent.which!=16 && oEvent.which!=37 && oEvent.which!=39) ||
           (oEvent.keyCode && oEvent.keyCode!=9 && oEvent.keyCode!=16 && oEvent.keyCode!=37 && oEvent.keyCode!=39))) {
            field.value = str;
            field.focus();

        }  else if (getBrowser()=='ns6') {
        	field.value = str;
            field.focus();
        }
}

/**
  * Funkcja sprawdza poprawność wpisu w polu (dozwolone są tylko cyfry 0-9 i TYLKO JEDEN znak "."
  * @param object obiekt typu input text
  */
function maskaProcent(field) {
        var str, field;
        var myRegExp = /[^0-9,.]/g;
        str = field.value.replace(',','.');
        str = str.replace(myRegExp,"");
        if (parseFloat(str) < 0) {
          str = 0;
        } else if (parseFloat(str) > 100) {
          str = 100;
        }

        var oEvent = window.event;
        // pomijamy zmiany dla klawisza TAB - bo tracil sie focus
        if (oEvent && ((oEvent.which && oEvent.which!=9 && oEvent.which!=16 && oEvent.which!=37 && oEvent.which!=39) ||
           (oEvent.keyCode && oEvent.keyCode!=9 && oEvent.keyCode!=16 && oEvent.keyCode!=37 && oEvent.keyCode!=39))) {
            field.value = str;
            field.focus();

        }  else if (getBrowser()=='ns6') {
        	field.value = str;
            field.focus();
        }
}

/**
  * Funkcja sprawdza poprawność wpisu w polu - prawidłowy format daty to YYYY-MM-DD
  * @param object obiekt typu input text
  */
function maskaDaty(field) {

	var str, field;
	var myRegExp = /[^0-9]/g;
	str = field.value;
	str = str.replace(myRegExp,"");
  
  tmp = str;
  str = str.substr(0,4);
  if (tmp.length>4)
    str += "-" + tmp.substr(4,2);
  if (tmp.length>6)
    str += "-" + tmp.substr(6,2);

  var oEvent = window.event;

  // pomijamy zmiany dla klawisza TAB - bo tracil sie focus
  if (oEvent && ((oEvent.which && oEvent.which!=9 && oEvent.which!=16) ||
     (oEvent.keyCode && oEvent.keyCode!=9 && oEvent.keyCode!=16))) {
    field.value = str;
    field.focus();
  } else if (getBrowser()=='ns6') {
    field.value = str;
    field.focus();
  }
}

/**
  * Usuwa białe znaki z lewej strony tekstu
  */
function LTrim( value ) {
  var re = /\s*((\S+\s*)*)/;
  return value.replace(re, "$1");
}

/**
  * Usuwa białe znaki z prawej strony tekstu
  */
function RTrim( value ) {
  var re = /((\s*\S+)*)\s*/;
  return value.replace(re, "$1");
}

/**
  * Usuwa białe znaki z lewej i prawej strony tekstu
  */
function trim( value ) {
  return LTrim(RTrim(value));
}

/**
  * Reset elementu html-a
  */
function elemReset(e) {
   switch (e.type) {
      case 'select-one'       :  e.selectedIndex = 0;
                                 break;
      case 'select-multiple'  :  for (var i=0; i<e.options.length; i++) {
                                    e.options[i].selected=false;
                                 }
                                 break;
      case 'textarea'         : 
      case 'text'             :
      case 'hidden'           :
                                 e.value = '';
                                 break;
      case 'radio'            :  e.checked = false;
                                 break;
   }
}

/**
  * Pobiera wartość elementu HTML
  */
function getValue(e) {
   var v = ""
   var tmp_array = new Array();
   switch (e.type) {
      case 'select-one'       :  v = e.options[e.selectedIndex].value;
                                 break;
      case 'select-multiple'  :  for (var i=0; i<e.options.length; i++) {
                                    if (e.options[i].selected) {
                                      tmp_array.push(e.options[i].value);
                                    }
                                 }
                                 v = tmp_array.join(",");
                                 break;
      case 'textarea'         : 
      case 'text'             :
      case 'hidden'           :
                                 v = e.value;
                                 break;
      case 'radio'            :  if (e.checked) {
                                   v = true;
                                 } else {
                                   v = false;
                                 }
                                 break;
   }
   return v;
}

/**
  * Pobiera wartość elementu SELECT
  */
function getSelectValue(id) {
  s = $(id); 
  if (s) return s.options[s.selectedIndex].value;
}

/**
 * Pobiera tekst elementu SELECT
 */
function getSelectText(id) {
 s = $(id); 
 if (s) return s.options[s.selectedIndex].text;
}

/**
 * Round number to 'dec' precision 
 * @param Float num
 * @param Integer dec
 */
function round2(num, dec){
  if (typeof(dec) == "undefined") dec = 0; else dec = Math.floor( dec );
  if (isNaN(num + dec) || dec < 0 || dec > 12) return Math.round( num );
  var n = Math.pow( 10, dec );
  n = Math.round( num * n ) / n;  
  return n.toFixed(dec);
}

/**
  * Przekazaniu kursora na pole "username"
  */
function goToLogin() {
  login = $('username');
  if (login) {
    login.focus();
  }
}

function changeCountry(country) {
  var region = $('id_region');
  if (region) {
	  countryStr = $('id_regiont :selected').text();
	  if (countryStr.toUpperCase() == 'POLSKA') {
	    region.disabled = false;
	  } else {
        region.selectedIndex = 0;
	    region.disabled = true;
	  }
  }
}

function hideLogo() {
	//Effect.BlindUp('splash', { duration: 3.0 });
	//Effect.BlindDown('main_page', { duration: 3.0 });	
	$('main_page').show();
	$('main_page').setStyle({opacity: 0.0});
	new Effect.Opacity('splash', { from: 1.0, to: 0.0, duration: 3.0 });
	new Effect.Opacity('main_page', { from: 0.0, to: 1.0, duration: 3.0 });
	window.setTimeout('showMainPage()',3000);
}

function showMainPage() {
	$('splash').hide();
}

function skipIntro() {
	window.clearTimeout(timer);
	hideLogo();
}
