// Change the content of the tag with entered id to the string str
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) && (Item.type=='text') && ((Item.name.substring (4, 9)).toLowerCase() == 'email'))
        Res = !ValidEmail (Item.value);
      if ((!Res) && (Item.type=='textarea') && ((Item.name.substring (4, 7)).toLowerCase() == '500'))
        Res = (Item.value.length < 300);
      break;
    }
    // combobox and listbox
    case 'select-one': {
      if ((Item.selectedIndex == -1) || (Item.options[Item.selectedIndex].value=='EMPTY')) Res = true;
      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;
          }
      break;
    }
    // radio button
    case 'radio': {
      Res = true;
      var Radios=Item.form.elements[Item.name];
      for (var i=0; i<Radios.length;i++){
        if (Radios[i].checked){
          Res = false;
          break;
        }
      }
      break;
    }
    // checkbox
    case 'checkbox': {
      Res = !Item.checked;
      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 for validation of form Frm
function Validate (Frm)	{
  var Item;
  var LabelID="";
  var Res=true;
  var Empty=false;
  for (i=0;i<Frm.elements.length;i++){
    Item = Frm.elements[i];
    if (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
    ChangeInnerHTML ('ErrResultMsg', '<br><br><strong class="warning">Some of the mandatory fields remained empty or uncomplete or an email address is in wrong format. Please fill/correct the missing mandatory fields.</strong>');
  else {
    ChangeInnerHTML ('ErrResultMsg', '&nbsp;');
  }

  return (Res);
}


