﻿pLT=new Array(2);

pLT[0] = new Array("Remove group",'please enter old password','please enter new password','please confirm new password','new and confirmed passwords differ','password strength has to be at least weak','old and new password is the same','enter new e-mail address or mail code','enter new e-mail address OR mail code, not both','CAPS LOCK enganged','enter user name','enter password','enter text from picture','please fill out the form');
pLT[1] = new Array("Usunąć grupę",'podaj aktualne hasło','podaj nowe hasło','powtórz nowe hasło','nowe i aktualne hasła są takie same','siła hasła musi być co najmniej niska','nowe i aktualne hasła są takie same','podaj adres e-mail lub kod','podaj adres e-mail LUB kod','CAPS LOCK aktywny','podaj nazwę użytkownika','podaj hasło','wpisz tekst z obrazka','proszę wypełnić wymagane pola');


function colExp(id) {
   div = getObj("dv" + id);
   if(div) {
     pic = getObj("pdv" + id);
     if (div.style.display == "none" || !div.style.display) {
  	   div.style.display = "block";
	     pic.src = "../imgs/minus.gif";
     } else {
       div.style.display = "none";
  	   pic.src = "../imgs/plus.gif";
     }
   }
}

/* AJAX -----------------*/
var prAddr  = '/panel/loadHook';
var retrErr = 0;
var pars    = '';
var ajaxObj;

function XMLrequest(url) {
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    if (ajaxObj) {
      ajaxObj.loading = 1;
      ajaxObj.innerHTML = '<img src="/imgs/wait.gif" alt="please wait">';
      req.onreadystatechange = function() { XMLstateChange(); };
      req.open("GET", url, true);
      req.send("");
    }
  } else {
    alert('browser doesn\'t support XMLHTTP')
  }
}

function XMLstateChange(url) {
  if (req.readyState == 4) {
    if (!ajaxObj) {
      return false;
    }

    if (req.status == 200) {
      ajaxObj.innerHTML = req.responseText;
      retrErr = 0;
    } else {
      if (retrErr<3){
        retrErr++;
        XMLrequest(url);
      } else {
        alert('XMLHTTP error');
        ajaxObj.loading = 0;
      }
    }
  }
}

function addVar(nm,val){
  return '&'+nm+'='+encodeURIComponent(val)
}

function getC(nm) {
  var ac = document.cookie.split( ';' );
  var at = '';
  var cn = '';
  for ( i = 0; i < ac.length; i++ ) {
    at = ac[i].split( '=' );
    cn = at[0].replace(/^\s+|\s+$/g, '');
    if (cn == nm && at[1]) return at[1];
  }
  return null;
}

function getHook() {
  if (ajaxObj) {
    var SID = getC('sesID');
    if (SID) {
      var postPars = addVar('sesID',SID);
      XMLrequest(prAddr+'?pid='+ (Math.random() * 1234567890)+pars+postPars);
    } else {
      document.location.reload();
    }
  }
  return false;
}

function colLoad(id,oNm,pNm,pID) {
   ajaxObj = getObj("dv" + id);
   if(ajaxObj) {
     var pic = getObj("pdv" + id);
     if (ajaxObj.style.display == "none" || !ajaxObj.style.display) {
       ajaxObj.style.display = "block";
       pic.src = "/imgs/minus.gif";
     } else {
       ajaxObj.style.display = "none";
       pic.src = "/imgs/plus.gif";
     }
     if (!ajaxObj.loading){
       pars = addVar('oID',oNm);
       pars += addVar('pID',pNm);
       pars += addVar('sID',pID);

       getHook();
     }
   }
}
/* AJAX =================*/

/*chart functions -----------------*/
function showHide(id) {
  var obj = getObj(id);
  if (obj.style.display == 'none') { obj.style.display = 'block'; }
  else { obj.style.display = 'none'; }
}

function ShowChart(ico,cht) {
  SetAlpha(ico,100);
  cht.style.visibility = 'visible';
  ico.style.marginTop = '0';
}

function hideChart(ico,cht) {
  SetAlpha(ico,50);
  cht.style.visibility = 'hidden';
  ico.style.marginTop = '5';
}

function loadAct(id,shw) {
  var ico = getObj('ic'+id);
  var cht = getObj('ch'+id);
  if(ico && cht) {
    if (shw) {
      ShowChart(ico,cht);
    } else {
      hideChart(ico,cht);
    }
  }
}

function icAct(id) {
  var ico = getObj('ic'+id);
  var cht = getObj('ch'+id);
  if(ico && cht) {
    if (ico.style.opacity == .5) {
      ShowChart(ico,cht);
    } else {
      hideChart(ico,cht);
    }
  }
}

function mOver(id) {
  var ico = getObj('ic'+id);
  if(ico) {
    changeClassTxt('shortInfo','',ico.title);
  }
}

function mOut(id) {
  var ico = getObj('ic'+id);
  if(ico) {
    changeClassTxt('shortInfo','',' ');
  }
}
/*chart functions =================*/

/*port settings functions -----------------*/
function PassTabVis(show){
  var passT  = getObj('ps_table_div');
  var divWT  = getObj('pr_table_div');

  if (passT && divWT) {
    if (show) {
      divWT.style.visibility = 'hidden';
      passT.style.visibility = 'visible';
      document.forms[0].action = 'javascript:unInstall(\'1\')';
    } else {
      divWT.style.visibility = 'visible';
      passT.style.visibility = 'hidden';
      document.forms[0].action = '/panel/portSettings';
    }
  }
}

function getVal(id){
  var obj = getObj(id);
  if(obj && obj.value) {
    return obj.value;
  } else {
    return false;
  }
}

function getSelVal(id,nr){
  var obj = getObj(id);
  if(obj) {
    if (obj.selectedIndex > 0 || nr) {
      return obj.options[obj.selectedIndex].text;
    } else {
      return '*';
    }
  } else {
    return '**';
  }
}

function setVal(id,val){
  var obj = getObj(id);
  if(obj) {
    obj.value = val;
    return true;
  } else {
    return false;
  }
}

function unInstall(pid) {
  if (pid == 1) {  // do uninstall
    if ( checkInput('ps','ps_pass',6) == 0 ) {
      if( setVal('PSA','UN') ) {
        document.forms['ps'].action = "/panel/portSettings";
        document.forms['ps'].submit();
      }
    }
  }
  else if(pid != 0){    // get password
    if( setVal('PID',pid) ) {
      changeClassTxt('unButton','',pid.toUpperCase()+" - UNINSTALL ");
      PassTabVis(1);
    }
  }
  else {          // cancel
    setVal('PID','');
    PassTabVis();
  }
}

function block(pid) {
  if ( setVal('PID',pid) && setVal('PSA','BL') ) {
    document.forms['ps'].submit();
  }
}

function unBlock(pid) {
  if ( setVal('PID',pid) && setVal('PSA','UB') ) {
    document.forms['ps'].submit();
  }
}

/*port settings functions =================*/

/*group settings functions -----------------*/
function newGroup() {
  if ( setVal('act','newGroup') && setVal('portNm',getVal('createGr')) ) {
    document.forms['gs'].submit();
  }
}
function renGroup() {
  if ( setVal('act','renGroup') && setVal('portNm',getSelVal('GrNm1',1)) && setVal('newGr',getVal('renGr')) ) {
    document.forms['gs'].submit();
  }
}
function delGroup(langID) {
  if (langID == undefined) langID = 0;
  grName = getSelVal('GrNm2',1);
  if ( setVal('act','delGroup') && setVal('portNm',grName) && confirm(pLT[langID][0]+" "+grName+"?")) {
    document.forms['gs'].submit();
  }
}
function chPortGroup(nm) {
  if ( setVal('act','chPortGroup') && setVal('portNm',nm) && setVal('newGr',getSelVal('cg_'+nm)) && setVal('newNm',getVal('cn_'+nm)) ) {
    document.forms['gs'].submit();
  }
}

/*group settings functions =================*/

function checkChPassForm(langID) {
  if (langID == undefined) langID = 0;
  formNm = 'si';

  err1 = checkInput(formNm,'f_oldP',6);
  err2 = checkInput(formNm,'f_userP1',6);
  err3 = checkInput(formNm,'f_userP2',6);
  err4 = checkTxt(formNm,'f_userP1','f_userP2');
  var uNm      = getObj('f_uNm').value;
  var uPass    = getObj('f_userP1').value;
  var uPassOld = getObj('f_oldP').value;

  err5 = checkPS(uNm,uPass);
  err6 = (uPassOld == uPass);

  if (err1)
    changeClassTxt('mc1','warn',pLT[langID][1],'f_oldP');
  else if (err2)
    changeClassTxt('mc1','warn',pLT[langID][2],'f_userP1');
  else if (err3)
    changeClassTxt('mc1','warn',pLT[langID][3],'f_userP2');
  else if (err4)
    changeClassTxt('mc1','warn',pLT[langID][4],'f_userP1');
  else if (!err5)
    changeClassTxt('mc1','warn',pLT[langID][5],'f_userP1');
  else if (err6)
    changeClassTxt('mc1','warn',pLT[langID][6],'f_userP1');
  else document.forms[formNm].submit();
  
  return false;
}

function checkChMailForm(langID) {
  if (langID == undefined) langID = 0;
  formNm = 'si';

  if (document.forms[formNm].elements['f_newMail']) {
    err1 = checkInput(formNm,'f_newMail',1,1);
    er1 = checkInput(formNm,'f_newMail',6);
  } else { err1 = 1; er1 = 1; }

  if (document.forms[formNm].elements['f_mailCode']) {
    err2 = checkInput(formNm,'f_mailCode',1,1);
    er2 = checkInput(formNm,'f_mailCode',12);
  } else { err2 = 1; er2 = 1; }

  if (er1 & er2) {
    changeClassTxt('mc1','warn',pLT[langID][7]); }
  else if (!err1 && !err2) {
    changeClassTxt('mc1','err',pLT[langID][8]);
  }
  else document.forms[formNm].submit();

  return false;
}

var oldTxt, oldCls = String;

function capsDetect( e,langID ) {
  if (langID == undefined) langID = 0;
  if( !e ) { e = window.event; } if( !e ) { return; }

  var theKey = 0;
  if( e.which ) { theKey = e.which; }
  else if( e.keyCode ) { theKey = e.keyCode; }
  else if( e.charCode ) { theKey = e.charCode }

  var theShift = false;
  if( e.shiftKey ) { theShift = e.shiftKey; }
  else if( e.modifiers ) {
    if( e.modifiers & 4 ) {
      theShift = true;
    }
  }

  capsMsg = pLT[langID][9];
  bigKey  = (theKey > 64 && theKey < 91);
  smlKey  = (theKey > 96 && theKey < 123);
  caps    = (bigKey && !theShift) || (smlKey && theShift);

  if (caps) {
    var elem = getObj('mc1');
  	if(elem && elem.innerHTML != capsMsg) {
      oldTxt = elem.innerHTML;
      oldCls = elem.className;
      changeClassTxt('mc1','warn',capsMsg);
    }
  } else
  if (theKey > 31) {
    var elem = getObj('mc1');
  	if(elem && elem.innerHTML == capsMsg) {
      elem.innerHTML = oldTxt;
      elem.className = oldCls;
    }
  } else
  if (theKey == 13) { checkLoginForm(); }
}

function submitDetect( e ) {
  if( !e ) { e = window.event; } if( !e ) { return; }

  var theKey = 0;
  if( e.which ) { theKey = e.which; }
  else if( e.keyCode ) { theKey = e.keyCode; }
  else if( e.charCode ) { theKey = e.charCode }

  if (theKey == 13) { checkLoginForm(); }
}

function checkLoginForm(langID) {
  if (langID == undefined) langID = 0;
  formNm = 'si';

  err1 = checkInput(formNm,'f_name',6);
  err2 = checkInput(formNm,'f_pass',6);
  if (document.forms[formNm].elements['f_idnr'])
    err3 = checkInput(formNm,'f_idnr',5);
  else err3 = 0;

  if (err1)
    changeClassTxt('mc1','warn',pLT[langID][10],'f_name');
  else if (err2)
    changeClassTxt('mc1','warn',pLT[langID][11],'f_pass');
  else if (err3)
    changeClassTxt('mc1','warn',pLT[langID][12]);
  else document.forms[formNm].submit();

  return false;
}

function chObjStyle(nm,styleObj,val){
 var obj = getObj(nm);
 if (obj) {
   obj.style[styleObj] = val;
 }
}

function chInp(id,minLen){
  var obj = getObj(id);
  if(obj) {
    if (!obj.value || obj.value.length<minLen) {
      chObjStyle(id+'Txt','color','#F66');
    } else {
      chObjStyle(id+'Txt','color','#FFF');
      return 0;
    }
  }
  return 1;
}

function chVal(id,minLen,minV,maxV){
  var obj = getObj(id);
  if(obj) {
    var val = parseInt(obj.value);
    if (!obj.value || obj.value.length<minLen || isNaN(val) || val<minV || val>maxV) {
      chObjStyle(id+'Txt','color','#F66');
    } else {
      chObjStyle(id+'Txt','color','#FFF');
      return 0;
    }
  }
  return 1;
}

function chChb(id){
  var obj = getObj(id);
  if(obj) {
    if (!obj.checked) {
      chObjStyle(id+'Txt','color','#F66');
    } else {
      chObjStyle(id+'Txt','color','#FFF');
      return 0;
    }
  }
  return 1;
}

function checkPayForm(langID) {
  if (langID == undefined) langID = 0;

  var err =
    chInp('r_name',3) +
    chInp('r_street',3)+
    chInp('r_streetNumber',1)+
    chInp('r_city',3)+
    chInp('r_cityReg',6)+
    chVal('r_ports',1,1,999)+
    chInp('r_email',3)+
    chChb('r_accept');

  var company = getObj('r_company');
  if (company && company.value && company.value.length>0) {
    err += chInp('r_NIP',3) + chInp('r_company',1);
  } else {
    chObjStyle('r_NIPTxt','color','#FFF');
    chObjStyle('r_companyTxt','color','#FFF');
  }

  if (err)
    changeClassTxt('mc1','warn',pLT[langID][13]);
  else document.forms[0].submit();

  return false;
}

function acceptPayForm(langID) {
  if (langID == undefined) langID = 0;

  document.forms[0].submit();
  return false;
}
