// Constants - texts
var T_TEXTBOX_EMPTY = " - tato položka je povinná";
var T_SELECT_EMPTY = "is not selected";
var T_CHECKBOX_EMPTY = " - tato položka je povinná";
var T_RADIO_EMPTY  = "option is not selected";
var T_EMAIL_WRONG = "format is wrong";
var T_TEXT_LONG = "test is too long";
var T_TEXT_LONG_BLUR = "You have exceeded the character limit (%1). Please review your text.";

//-------------------------------------
if((typeof HTMLElement != 'undefined') && (HTMLElement.prototype.__defineGetter__ != 'undefined')){
  HTMLElement.prototype.__defineGetter__("innerText", function () {
    var r = this.ownerDocument.createRange();
    r.selectNodeContents(this);
    return r.toString();
  }); 
}

// Change the content of the tag with entered id to the string str
var Err = "";
var LastErr = "";
function ChangeInnerHTML (id, str){
  if ((document.all) && (document.all(id))) {
    document.all(id).innerHTML = str;
  }
  else if ((document.getElementById) && (document.getElementById(id))) {
    document.getElementById(id).innerHTML = str;
  }
}

// Function for Email validation
function ValidEmail(Email){
  var LastChar = Email.length - 1;
  var CharPos = Email.indexOf("@");
  if ((CharPos < 1) || (CharPos == LastChar)) return false;
  // position of first .
  var CharPos = Email.indexOf(".");
  if (CharPos < 1) return false;
  // position of last .
  var CharPos = Email.lastIndexOf(".");
  if (CharPos == LastChar) return false;
  return true;
}

// Function test, if item has any content - if user write something to
// the field or select any option
function IsEmpty (Item)	{
  var Res = false;
  switch (Item.type){
    // Edit fields
    case 'text':
    case 'password':
    case 'file':
    case 'textarea': {
      Res = (Item.value == '');
      if (Res) AddErrItem (Item.name, T_TEXTBOX_EMPTY);
      if ((!Res) && (Item.type=='text') && ((Item.name.substring (4, 9)).toLowerCase() == 'email')){
        Res = !ValidEmail (Item.value);
        if (Res) AddErrItem (Item.name, T_EMAIL_WRONG);
      }  
      if ((!Res) && (Item.type=='textarea')) {
        var len = (Item.name.substring (4));
        var x = (len.split ("_"));
        len = parseInt (x[0]); 
        if (len > 0){
          if (Item.value.length > len){
            AddErrItem (Item.name, T_TEXT_LONG);
            Res = true;
          } 
        } 
      }
      break;
    }
    // combobox and listbox
    case 'select-one': {
      if ((Item.selectedIndex == -1) || (Item.options[Item.selectedIndex].value=='EMPTY')){ 
        Res = true;
        AddErrItem (Item.name, T_SELECT_EMPTY);
      }  
      break;
    }
    // combobox and listbox with multiple selection
    case 'select-multiple': {
      Res = true;
      if (Item.selectedIndex > -1){
        for (var i=0; i<Item.options.length;i++)
          if ((Item.options[i].selected) && (Item.options[i].value != 'EMPTY')){
            Res = false;
            break;
          }
      } 
      AddErrItem (Item.name, T_SELECT_EMPTY);
      break;
    }
    // radio button
    case 'radio': {
      GetLabelCaption (Item.name);
      Res = true;
      var Radios=Item.form.elements[Item.name];
      for (var i=0; i<Radios.length;i++){
        if (Radios[i].checked){
          Res = false;
          break;
        }
      }
      if (Res) AddErrItem (Item.name, T_RADIO_EMPTY);
      break;
    }
    // checkbox
    case 'checkbox': {
      if (Item.checked) Res = false;
      else {
        Res = true;
        AddErrItem (Item.name, T_CHECKBOX_EMPTY);
      }
      break;
    }
  }
  return (Res);
}

// Function set the style.color of the label identified by ID
// if Empty is true, the color is red otherwise black
function SetLabelStyle (Id, Empty){
  if (document.all){
    LabelItem = document.all(Id);
  } else {
    if (document.getElementById)
      LabelItem = document.getElementById(Id);
    else
      LabelItem = 0;
  }

  if (LabelItem){
    // Label for the empty item found
    if (Empty)
      LabelItem.style.color = 'red';
    else
      LabelItem.style.color = 'black';
  }
}

function GetLabelCaption (ItemName){
  var LabelID = "label_" + ItemName.substring (4);
  var Label = "";
  if (document.getElementById) Label = document.getElementById(LabelID);
  if (!Label) return ("");
  var Caption = Label.innerText;
  while (Caption.charAt(0) == ' ') Caption = Caption.substring (1); 
  while ((Caption.charAt(Caption.length-1) == ' ') || (Caption.charAt(Caption.length-1) == '*') || (Caption.charAt(Caption.length-1) == ':')) 
    Caption = Caption.substring (0, Caption.length-1); 
  return (Caption);  
}

function AddErrItem (ItemName, Str){
  var S = "<li><b>" + GetLabelCaption (ItemName) + "</b> " + Str + "</li>";
  if (S != LastErr) Err += S;
  LastErr = S; 
}

// Function for validation of form Frm
function Validate (Frm)	{
  var Item;
  var LabelID="";
  var Res=true;
  var Empty=false;
  Err = "";
  for (i=0;i<Frm.elements.length;i++){
    Item = Frm.elements[i];
    if ((Item.name) && (Item.name.substring (0, 4) == "man_")){
      // Get the id of the label
      LabelID = "label_" + Item.name.substring (4);
      // Check if the field is empty
      Empty = IsEmpty(Item);
      SetLabelStyle (LabelID, Empty)
      Res = (Res && !Empty);
    }
  }

  if (!Res){ // Show the message with empty fields
    Err = "<h2>Please fill in all mandatory fields</h2><ul>" + Err + "</ul>";
    
    ChangeInnerHTML ('ErrResultMsg', Err)
    if (document.getElementById) document.getElementById('ErrResultMsg').style.display = 'block';
    else {
      Err = Err.replace (/<li>/g,'\n\t- ');  
      Err = Err.replace ('</h2>','\n');  
      Err = Err.replace (/<[\w|\/]*>/g,''); 
      alert (Err);
    }   
  }      
  else {
    ChangeInnerHTML ('ErrResultMsg', '&nbsp;');
    if (document.getElementById) document.getElementById('ErrResultMsg').style.display = 'none';
  }
  return (Res);
}

//--------------------------
// function, which checks the length of text in TextArea (onBlur)
// TEXTAREA onblur="return(checkTextArea(this, 500));"
function checkTextArea (ta, len){
  if (ta.value.length > len){
    alert (T_TEXT_LONG_BLUR.replace('%1', len));
    ta.focus();
    return true;
  } else return false;
}
