// uzivatelska funkce; zobrazovani a skryvani zprav

function showMsg(text,nextAction){

      hs.Expander.prototype.onBeforeClose = function(sender) { eval(nextAction)};
      return hs.htmlExpand(null, { objectType: 'ajax', src: '/ajax/showmsg.php?text='+utf8encode(text), wrapperClassName: 'draggable-header', headingText: 'Informace', preserveContent: false } );

}

function hideMsg(){
      hs.close();
      hs.Expander.prototype.onBeforeClose = '';
}

// uzivatelska funkce; obarvovani vstupnich formularovych poli pro ucely kontrolnich funkci

function iActive(stav,elem){
  switch(stav){
  case 'on':      elem.style.borderColor='#DF9304'; elem.style.backgroundColor='#ffffff'; break;
  case 'off':     elem.style.borderColor='#A8A8A8'; elem.style.backgroundColor='#ffffff'; break;
  case 'err':     elem.style.borderColor='#BE1F2D'; elem.style.backgroundColor='#f4acb1'; break;
  }
}

// uzivatelska funkce; inicializace obarvovani vstupnich poli
function inputColorInit(){

    var inputs=document.getElementsByTagName("input");
    for (var i=0;i<inputs.length;i++){
         addEvent(inputs[i], 'focus', function(){ iActive('on',this) });
         addEvent(inputs[i], 'blur', function(){ iActive('off',this) });

    }

    var selects=document.getElementsByTagName("select");
    for (var i=0;i<selects.length;i++){
         addEvent(selects[i], 'focus', function(){ iActive('on',this) });
         addEvent(selects[i], 'blur', function(){ iActive('off',this) });
    }

    var textareas=document.getElementsByTagName("textarea");
    for (var i=0;i<textareas.length;i++){
         addEvent(textareas[i], 'focus', function(){ iActive('on',this) });
         addEvent(textareas[i], 'blur', function(){ iActive('off',this) });
    }

}

function addEvent(obj, event, funct) {
   if (obj.attachEvent) { //IE
     obj['e' + event + funct] = funct;
     obj['x' + event + funct] = function() {
           obj['e' + event + funct](window.event);
         }
     obj.attachEvent('on' + event, obj['x' + event + funct]);
   } else // other browser
     obj.addEventListener(event, funct, false);
}

addLoadEvent(inputColorInit);

function GetEditorContents(EditorName)	{
   if (isdefined('FCKeditorAPI')){
   content = FCKeditorAPI.GetInstance(EditorName).GetXHTML( true ) ;
   return content;
   }
}

function add2cart(productID){

     return hs.htmlExpand(null, { objectType: 'ajax', src: '/ajax/to-cart.php?productID='+utf8encode(productID), wrapperClassName: 'draggable-header', headingText: 'Přidat do košíku', preserveContent: false } );

}

var vars = new Object();

function setVars(name,value){ vars[name] = value; }


function checkLogin(formName){

    var err = '';
    if (formName == null) formName = 'srcLogin';
    form = document.forms[formName];

    elem = form.elements['customerLogin'];
    iActive('off',elem);
    if (elem.value=='')                            { err += 'Zadejte přihlašovací jméno (zákazníka).<br>'; iActive('err',elem); }

    elem = form.elements['customerPassword'];
    iActive('off',elem);
    if (elem.value=='')                            { err += 'Zadejte přihlašovací heslo.<br>'; iActive('err',elem); }


    if (err!=''){
       showMsg(err);
    }else{
       ajaxPost(formName,'/system/ajax/?m=login&action=login','null');
       hs.close();
    }

}

// uzivatelska funkce; kontrola kontaktniho formulare
function checkNewPassword(){

    var err = '';
    form = document.forms['srcNewPassword'];

    elem = form.elements['customer_email'];
    iActive('off',elem);
    if (!filter_email.test(elem.value))                            { err += 'Zadaná e-mailová adresa není platná.<br>'; iActive('err',elem); }

    if (err!=''){
       showMsg(err);
    }else{
       ajaxPost('srcNewPassword','/system/ajax/?m=login&action=newPassword','null');
       hs.close();
    }

}

function loginStatus(status){

  var nextAction = '';
  var text       = '';

  switch (status){
  case '0': text = "Přihlášení se nezdařilo. Špatné uživatelské jméno nebo heslo."; break;
  case '1': document.srcLogin.submit(); break;
  }

  if (text!='')       showMsg(text);

}

function newPasswordStatus(status){

  var nextAction = '';
  var text       = '';

  switch (status){
  case '1': text = "Nové heslo bylo zasláno na zadaný e-mail."; break;
  case '0': text = "Vygenerování nového hesla se nazdařilo. Heslo nebylo odesláno."; break;
  case '-1': text = "Nové heslo bylo vygenerováno, ale nezdařilo se jeho zaslání.<br /><br />Kontaktujte nás."; break;
  }

  if (text!='')       showMsg(text);

}


