var process = true;
var headers = null;
function validatePlainText(id,rq) {
  elem = document.getElementById(id);
  label = document.getElementById(id+"_label");
  if (elem.value) {
    label.className = "";
    return Url.encode(elem.value);
  } else {
    if (rq) label.className = "alert";
    return false;
  }
}

function updateFolderName(id,ref) {
  source = document.getElementById(id);
  folderInput = document.getElementById(id+"_folder");
  folderVal = document.getElementById(id+"_folder_val");
  str = source.value.toLowerCase().replace(/(\:|\?|\!|\{|\}|\[|\]|\~|\"|\'|<|>|\*|\?|\:|\||\\|\/| )/g,'_');
  str = str.replace(/_(?=_)/g,'');
  str = str.replace(/(á)/g,'a');
  str = str.replace(/(ě|é)/g,'e');
  str = str.replace(/(š)/g,'s');
  str = str.replace(/(č)/g,'c');
  str = str.replace(/(ř)/g,'r');
  str = str.replace(/(ž)/g,'z');
  str = str.replace(/(ý)/g,'y');
  str = str.replace(/(í)/g,'i');
  str = str.replace(/(ú|ů)/g,'u');
  str = str.replace(/(ň)/g,'n');
  str = str.replace(/(ó)/g,'o');
  str = str.replace(/(ť)/g,'t');
  str = str.replace(/(ď)/g,'d');
  if(str.length>26) str = str.substr(0,26);
  folderVal.innerHTML = str;
  folderInput.value = ref + str;
}

function validateAntiSpam(id,captcha) {
  label = document.getElementById(id+'_label');
  if (document.getElementById(id).value == captcha) {
    label.className = "";
    return true;
  } else {
    label.className = "alert";
    return false;
  }
}

function validateTextField(id,rq) {
  elem = document.getElementById(id);
  label = document.getElementById(id+"_label");
  tinyMCE.execCommand('mceRemoveControl', false, 'Content');
  if (elem.value) {
    label.className = "";
    tinyMCE.execCommand('mceAddControl', false, id);
    return Url.encode(elem.value);
  } else {
    if (rq) label.className = "alert";
    tinyMCE.execCommand('mceAddControl', false, id);
    return false;
  }
}

function submitLogin() {
  headers = '';
  process = true;
  if (validatePlainText('Login',1))
    headers += "&login="+validatePlainText('Login');
  else 
    process = false;
  if (validatePlainText('Password',1))
    headers += "&pass="+validatePlainText('Password');
  else 
    process = false;
  if (process) {
    switchSection('','admin',headers);
  } else {
    myWarn('unfill');
  }
  return false;
}

function submitNewsSet(id) {
  process = true;
  headers = null;  
  if (validatePlainText('Topic',1))
    headers = "&topic="+validatePlainText('Topic');
  else
    process = false;
  if (validateTextField('Content',1))
    headers += "&content="+validateTextField('Content');
  else
    process = false;    
  if (process) {
    if (id) {
      headers += "&el_id="+id;  
      adminSwitchSection(false,'news','action=edit'+headers,'post');  
    } else {
      adminSwitchSection(false,'news','action=add'+headers,'post');
    }
  } else {
    myWarn('unfill');
  }
  return false;
}

function checkLimit(ref,limit) {
  id = ref.id;
  cont = ref.value;
  current = cont.length;
  if (current > limit) {
    ref.value = cont.substr(0,limit);
  }
  left = limit-current;
  if (left<1) left = 0;
  document.getElementById(id+'_limiter').innerHTML = left; 
}

function submitContactForm(captcha) {
  process = true;
  headers = '';
  if (val = validatePlainText('Name',1))
    headers += '&name='+val;
  else
    process = false;
  if (val = validatePlainText('Mail',1))
    headers += '&mail='+val;
  else
    process = false;    
  if (val = validatePlainText('Content',1))
    headers += '&content='+val;
  else
    process = false;
  if (val = validatePlainText('City'))
    headers += '&city='+val;    
  if (val = validatePlainText('Cell'))
    headers += '&cell='+val;    
  if (!validateAntiSpam('Captcha',captcha)) process = false; 
  if (headers) headers += "&sent=true";
  if (process) {
    switchSection(null,'kontakt',headers);
  } else {
    myWarn('unfill');
  }
  return false;
}

function submitChatAddForm(captcha) {
  process = true;
  headers = '';
  if (val = validatePlainText('chatName',1))
    headers += '&name='+val;
  else
    process = false;
  if (val = validatePlainText('chatMail',1))
    headers += '&mail='+val;
  else
    process = false;    
  if (val = validatePlainText('chatText',1))
    headers += '&text='+val;
  else
    process = false;
  if (!validateAntiSpam('Captcha',captcha)) process = false; 
  if (headers) headers += "&sent=true";
  if (process) {
    drawChat(headers);
  } else {
    myWarn('unfill');
  }
  return false;
}

function submitGalleryCatSet(id) {
  process = true;
  headers = null;
  if (val = validatePlainText('Name',1)) 
    headers = "&name="+val+"&folder="+Url.encode(document.getElementById('Name_folder').value)+"&formerfolder="+Url.encode(document.getElementById('FormerFolder').value);
  else
    process = false;
  if (val = validateTextField('Content',0)) 
    headers += "&desc="+val;
  if (process) {
    if (id) {
      headers += "&el_id="+id;
      adminSwitchSection(false,'gallery','action=edit'+headers);    
    } else {
      adminSwitchSection(false,'gallery','action=add'+headers);
    }
  } else {
    myWarn('unfill');
  }
  return false;
}

function submitDivisionsSet(id) {
  process = true;
  headers = null;
  if (val = validatePlainText('Name',1)) 
    headers = "&name="+val;
  else
    process = false;
  if (process) {
    if (id) {
      headers += "&el_id="+id;
      adminSwitchSection(false,'divisions','action=edit'+headers);    
    } else {
      adminSwitchSection(false,'divisions','action=add'+headers);
    }
  } else {
    myWarn('unfill');
  }
  return false;
}

function submitScrapbooksCatSet(id) {
  process = true;
  headers = null;
  if (val = validatePlainText('Name',1)) 
    headers = "&name="+val+"&folder="+Url.encode(document.getElementById('Name_folder').value)+"&formerfolder="+Url.encode(document.getElementById('FormerFolder').value);
  else
    process = false;
  if (val = validateTextField('Content',0)) 
    headers += "&desc="+val;
  if (val = validatePlainText('ParentId',0)) 
    headers += "&parentid="+val;  
  if (process) {
    if (id) {
      headers += "&el_id="+id;
      adminSwitchSection(false,'scrapbooks','action=edit'+headers);    
    } else {
      adminSwitchSection(false,'scrapbooks','action=add'+headers);
    }
  } else {
    myWarn('unfill');
  }
  return false;
}

function submitAlterContent(site) {
  headers = '&content='+validateTextField('Content')+'';
  adminSwitchSection(false,'content','site='+site+headers,'post');
  return false;
}

function myWarn(objection) {
  switch(objection) {
    case "unfill":
    alert('Vyplňte prosím všechny požadované údaje!');
    break;
  }
}
