


  // !!!!!!
  // Don't delete this functions. They are used in child pages.

  var msg_MustFill = "Τα πεδία που είναι χαρακτηρισμένα με * πρέπει να συμπληρωθούν υποχρεωτικά.";
  var msg_BadSymbol = "You must not use any quotation marks ('') in your entries.";
  var msg_BadSymbol2 = "You must not use any (^) characteristic in your entries.";
  var msg_WrongNumberFormat = "You have not entered a valid number.";
  var msg_TooMuchDigits1 = "You have entered too many decimal places. Up to ";
  var msg_TooMuchDigits2 = " places are permitted.";
  var msg_WrongDate = "Τρόπος καταχώρησης ημερομηνίας: 05/11/01 ή 05/11/2001";
  var msg_noonefilled = "Please enter at least one search word.";
  var msg_textareatoolong = "The text which has been entered is too long. The maximum permissible character length is ";

  // internal
  var msg_FormatError = "FormatError: ";


  function EmptyString(str){
    return (AllTrim(str) == "");
  }
  
  function AllTrim(str){
    for(var i = 0; i < str.length; i++)
      if(str.charAt(i) != " ")
        break;
    str = str.substr(i, str.length-i);
  
    for(var i = str.length-1; i > 0; i--)
      if(str.charAt(i) != " ")
        break;
    str = str.substr(0, i+1);
  
    return str;
  }
  
  function getFileExtension(sFileName){
    var nPos = sFileName.lastIndexOf(".");

    if(nPos == -1)
      return "";

    return sFileName.substring(nPos+1, sFileName.length);
  }


  // FUNCTION: ValidateFormData(frm, aFields, isQuery) - validate data in form fields
  // RETURNS: true, false
  // PARAMETERS: 
  //   frm - validated form
  //   aFields - described below
  //   isQuery - if not null, shows that form is for search. And at least one field must be
  //             filled.
  //
  // Array aFields contrains data about field name and field format in form
  // to validate. Ex: {"Name:*S", "Aart_ID:I"}
  // Format of array element "<FieldName>:<FieldFormat>"
  // FieldFormat is '[*]<S|ID|N[,s]|D|T|CB>'
  //   asteric means that field must be filled.
  //   S - string field
  //   ID - combobox, select-one field 
  //   N[,s] - number field, where <s> is maximum number of digits after (, or .)
  //   D - date field
  //   T[,maxlength] - textarea field, not tested for (") and other bad symbols
  //   CB - checkbox, this fields will not tested always, simply informative.

  function ValidateFormData(frm, aFields, isQuery){

    var FieldName;
    var Format;
    var Asteric;
    var Extra;

    var isOneFilled = false;

    // isQuery
    if(isQuery != null)
      isQuery = true;
    else
      isQuery = false;

    for(var i = 0; i < aFields.length; i++){

      F = aFields[i].split(":");
      if(F.length != 2){
        alert(msg_FormatError + aFields[i]);
        return false;
      }

      // name
      FieldName = F[0];
      Format = F[1];

      // asteric
      Asteric = false;
      F = Format.lastIndexOf("*");
      if(F == 0)
        Asteric = true;
      else if(F > 0){
        alert(msg_FormatError + aFields[i]);
        return false;
      }


      // type and check
      if(Format.indexOf("S") != -1){  // string field
        frm[FieldName].value = AllTrim(frm[FieldName].value);

        if(Asteric && !CheckAstericField(frm, FieldName))
          return false;
        if(!CheckFieldForBadSymbols(frm, FieldName))
          return false;

      }else if(Format.indexOf("ID") != -1){  // combobox field

        if(Asteric && !CheckAstericField(frm, FieldName))
          return false;

      }
      else if(Format.indexOf("M") != -1){  // mail field

        if(!checkmail(frm, FieldName))
          return false;

      }
      else if(Format.indexOf("T") != -1){  // textarea field

        F = Format.split(",")
        Extra = -1;
        if(F.length > 1){
          Extra = parseInt(F[1]);
        }

        if(Asteric && !CheckAstericField(frm, FieldName))
          return false;
        if(!CheckTextarea(frm, FieldName, Extra))
          return false;

      }else if(Format.indexOf("CB") != -1){  // check box

        // nothing to do

      }else if(Format.indexOf("N") != -1){  // number field
        frm[FieldName].value = AllTrim(frm[FieldName].value);

        F = Format.split(",")
        Extra = -1;
        if(F.length > 1){
          Extra = parseInt(F[1]);
        }

        if(Asteric && !CheckAstericField(frm, FieldName))
          return false;
        if(!CheckFieldForNumberFormat(frm, FieldName, Extra))
          return false;

      }else if(Format.indexOf("D") != -1){  // date field
        frm[FieldName].value = AllTrim(frm[FieldName].value);

        if(Asteric && !CheckAstericField(frm, FieldName))
          return false;
        if(!CheckFieldForDateFormat(frm, FieldName))
          return false;

      }

      if(isQuery){
        isOneFilled = (isOneFilled || CheckAstericField(frm, FieldName, true));
      }
    }

    if(isQuery && !isOneFilled){
      alert(msg_noonefilled);
      return false;
    }

    return true;
  }

  function CheckTextarea(frm, fieldName, maxLength) {
      if (maxLength != -1) {
          if (frm[fieldName].value.length > maxLength) {
              frm[fieldName].focus();
              alert(msg_textareatoolong + maxLength);
              return false;
          } 
      }
      return true;
  }

  function CheckFieldForBadSymbols(frm, FieldName)
  {
    if(frm[FieldName].value.indexOf("\"") != -1)
    {
      frm[FieldName].focus();
      alert(msg_BadSymbol);
      return false;
    }
    if(frm[FieldName].value.indexOf("^") != -1)
    {
      frm[FieldName].focus();
      alert(msg_BadSymbol2);
      return false;
    }
    return true;
  }

function checkmail(frm, FieldName)
{
 
	str=frm[FieldName].value;
	a=str.indexOf('@');
	d=str.indexOf('.');
	if (a<0 || d<0) {	
		
		 
	//	alert('Πρέπει να δώσετε ένα έγκυρο e-mail');
		frm[FieldName].focus();
	//	alertMsg = "Παρακαλώ συμπληρώστε τα παρακάτω πεδία:\n -e-mail";
		return false;
	}
	else return true;
}

  function CheckAstericField(frm, FieldName, isSilent){
    var f = frm[FieldName];
    if( ((f.type == "text" || f.type == "textarea" || f.type == "file" || f.type == "password") && EmptyString(f.value)) ||
        (f.type == "select-one" && f.selectedIndex == 0) )
    {
      if(!(isSilent != null && isSilent == true)){
        frm[FieldName].focus();
        alert(msg_MustFill);
      }
      return false;
    }
    return true;
  }

  function CheckFieldForNumberFormat(frm, FieldName, needDelim){
    str = AllTrim(frm[FieldName].value);

    var Delim = -1;

    for(var i = 0; i < str.length; i++)
    {
      if(str.charAt(i) >= "0" && str.charAt(i) <= "9"){
        if(needDelim > -1 && Delim > -1){
          Delim = Delim + 1;
          if(Delim > needDelim){
            frm[FieldName].focus();
            alert(msg_TooMuchDigits1 + needDelim + msg_TooMuchDigits2);
            return false;
          }
        }
      }else if(str.charAt(i) == "." || str.charAt(i) == ","){
        if(Delim > -1){
          frm[FieldName].focus();
          alert(msg_WrongNumberFormat);
          return false;
        }
        if(needDelim == 0){
          frm[FieldName].focus();
          alert(msg_TooMuchDigits1 + needDelim + msg_TooMuchDigits2);
          return false;
        }

        Delim = 0;
      }else{
        frm[FieldName].focus();
        alert(msg_WrongNumberFormat);
        return false;
      }
    }
    return true;
  }

  // this function allows empty date
  function CheckFieldForDateFormat(frm, FieldName){ 
    if(frm[FieldName].value.length == 0)
      return true;

    var ret = CheckDateFormat(frm[FieldName].value);
    if(ret.length == 0){
      frm[FieldName].focus();
      alert(msg_WrongDate);
      return false;
    }else{
      frm[FieldName].value = ret;
      return true;
    }
  }

  function CheckDateFormat(strDate){
    var parsedDate = strDate.split("/");
    if (parsedDate.length != 3)
      return "";

    var day = parsedDate[0];
    var month = parsedDate[1];
    var year = parsedDate[2];

    var nD = parseInt(day, 10);
    var nM = parseInt(month, 10) - 1;
    var nY = parseInt(year, 10);

    if(nY < 40) {
      nY = 2000 + nY;
    } else if(nY < 100) {
      nY = 1900 + nY;
    };

    var objDate = new Date(nY, nM, nD);
    if(nD != objDate.getDate() || nM != objDate.getMonth() || nY != objDate.getFullYear())
      return "";
 
    if(day.length < 2)
      day = "0" + day;

    if(month.length < 2)
      month = "0" + month;

    return day + "/" + month + "/" + nY;
  }

  // str must be already checked with CheckDateFormat
  function parseGermanDate(str) {
      var parts = str.split(".");
      return new Date(parseInt(parts[2]), parseInt(parts[1]) - 1, parseInt(parts[0]));
  }
