﻿/******************************************************************************************
* @autor: Michael Daxberger (md@isag.at)
******************************************************************************************/

/**
 * Simple setter method
 *
 * @param form (the form number)
 * @param element (the element number in the specified form)
 */
function setFocus(form, element) {
  document.forms[form].elements[element].focus();
}

/**
 * simple image changer method
 */
function changeImg(objImg, img) {
  objImg.src = img;
}

/**
 * Opens a new window for a specified file 
 */
function openWindow(file, width, height) {
  win = window.open(file, "win", "width=" + width + ",height=" + height + ",scrollbars=auto");  
  win.focus();
}

/**
 * Closes the current window
 */
function closeWindow() {
  this.close();
}

/**
 * Trim function - removes white spaces at
 * the beginning / end of a string
 */
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}


/**
 * Checks the validity of a form value
 * This one is just called from checkMandatoryFields()
 */
function isValid(value) {
  value = trim(value);
  if (value != null &&
      value != "") {
    return true;    	
  }
  return false;
}

/**
 * Checks the validity of an email address
 * This one is just called from checkMandatoryFields()
 */
function isValidEmail(email) {
  email = trim(email);
  if (email.indexOf("@") == -1 ||
      email.indexOf("@") == 0  ||
      email.indexOf(".") == -1 ||
      email.length < 6) {
    return false;    	
  }
  return true;
}

/**
 * Checks the validity of a phone number
 * This one is just called from checkMandatoryFields()
 */
function isValidPhone(phone) {
  var isCorrectNumber = true;
  var validChars = "0123456789 ./-+";
  phone = trim(phone);
  for (j=0; j < phone.length; j++) {
    if (validChars.indexOf(phone.charAt(j)) == -1) {
      isCorrectNumber = false;
      break;
    }
  }
  if (phone.length < 4) {
    isCorrectNumber = false;
  }
  return isCorrectNumber;
}

/**
 * Checks all mandatory fields of a form with
 * CLASS="mandatory" - Fields
 */
function checkMandatoryFields() {
  var allFilled = true;
  var fields = document.forms[0].elements;
  for (i=0; i < fields.length; i++) {
    if (fields[i].className == "mandatory") {
      if (!isValid(fields[i].value)) {
      	alert("Bitte füllen Sie alle markierten Eingabefelder aus.");
      	document.forms[0].elements[i].focus();
      	allFilled = false;
      	break;
      }
      if (fields[i].name && fields[i].name.toLowerCase().indexOf("mail") != -1) {
      	if (!isValidEmail(fields[i].value)) {
      	  alert("Bitte geben Sie eine korrekte E-Mail Adresse an.");
          document.forms[0].elements[i].select();
          allFilled = false;
          break;
        }
      }
      if (fields[i].name && (fields[i].name.toLowerCase().indexOf("phon") != -1 ||
                             fields[i].name.toLowerCase().indexOf("fon") != -1 ||
                             fields[i].name.toLowerCase().indexOf("tel") != -1)) {
      	if (!isValidPhone(fields[i].value)) {
      	  alert("Bitte geben Sie eine korrekte Telefonnummer an.");
          document.forms[0].elements[i].select();
          allFilled = false;
          break;
        }
      }
    }
  }
  
  return allFilled;
}