if (top != self)
  top.location = self.location;

//------------------------------------------------------------------------------------
// Submit form in Contact element
//------------------------------------------------------------------------------------
function submitContactForm() {
  if (!checkContactForm(document.contact)) {
    return false;
  }
  document.contact.submit();
}


//------------------------------------------------------------------------------------
// Check form in Contact element
//------------------------------------------------------------------------------------

function checkContactForm(form) {

  // check name
  form.name.value = trim(form.name.value);
  if (form.name.value.length == 0) {
    alert("Bitte geben Sie Ihren Namen ein.");
    form.name.focus();
    return false;
  }
  form.surname.value = trim(form.surname.value);
  if (form.surname.value.length == 0) {
    alert("Bitte geben Sie Ihren Vornamen ein.");
    form.surname.focus();
    return false;
  }
  form.company.value = trim(form.company.value);
  if (form.company.value.length == 0) {
    alert("Bitte geben Sie den Namen Ihrer Firma ein.");
    form.company.focus();
    return false;
  }

  // check contact - email or tel or fax or address
  form.eMail.value = trim(form.eMail.value);
  form.phone.value = trim(form.phone.value);
  form.fax.value = trim(form.fax.value);
  if ((form.eMail.value.length == 0)) {
    alert("Bitte geben Sie Ihre E-Mail Adresse ein.");
    form.eMail.focus();
    return false;
  }

  // check email if not empty
  if (form.eMail.value.length > 0) {
    if (!checkEmailAddress(form.eMail.value)) {
      alert("Bitte geben Sie eine g�ltige E-Mail Adresse ein.");
      form.eMail.focus();
      return false;
    }
  }

  // check text
  form.text.value = trim(form.text.value);
  if (form.text.value.length == 0) {
    alert("Bitte füllen Sie das Feld f�r Anmerkungen / Fragen aus.");
    form.text.focus();
    return false;
  }
  return true;
}

//------------------------------------------------------------------------------------
// Check if given email address is valid.
// Valid address is not empty and contains one '@' and at least one '.' character after '@'
//------------------------------------------------------------------------------------
function checkEmailAddress(address) {
	if (trim(address) == "")
		return false;
	var index1 = address.indexOf("@");
	var index2 = address.indexOf(".", index1); 
	if ((index1 < 0) || (index2 < 0))
		return false;
	if (trim(address.substring(0, index1)) == "")
		return false;
	if (trim(address.substring(index1 + 1, index2)) == "")
		return false;
	if (trim(address.substring(index2 + 1)) == "")
		return false;

	return true;
}

//------------------------------------------------------------------------------
// Remove spaces at the begining and the end of the given string
//------------------------------------------------------------------------------
function trim(value) {
	if (!value || (value == null))
		return "";
	if (value.length == 0)
		return "";

	var i = 0, startPos = 0, endPos = 0;
	while (i < value.length) {
		if (value.charAt(i) != ' ')
			break;
		i++;
	}
	if (i == value.length)
		return "";
	startPos = i;

	i = value.length - 1;
	while (i > 0) {
		if (value.charAt(i) != ' ') {
			endPos = i;
			break;
		}
		--i;
	}
	if (endPos == 0)
		endPos = startPos;
	return value.substring(startPos, endPos + 1);
}

//------------------------------------------------------------------------------------
// Replace substring in string with another substring
// sourceString .. source text
// oldString    .. substring to be replaced
// newString    .. substring to be used as a replacement
// replaceAll   .. flag if replace all (true) or only first (false) occurence of oldString
//------------------------------------------------------------------------------------

function strReplace(sourceString, oldString, newString, replaceAll) {
  // check inputs
  if (!sourceString)
    sourceString = "";
  if (!oldString)
    oldString = "";
  if (!newString)
    newString = "";
  if (!replaceAll)
    replaceAll = false;

  // check next action
	if (sourceString == null)
		return "";
	if (sourceString.length == 0)
		return sourceString;
	if (oldString == null || oldString.length == 0)
		return sourceString;
	if (newString == null)
		newString = "";

	var indexOld = 0;
	var indexNew = sourceString.indexOf(oldString, indexOld);
	if (indexNew < 0)
		return sourceString;

	var lengthOld = oldString.length;
	var sbTemp = "";

	while (indexNew >= 0) {
		sbTemp += sourceString.substring(indexOld, indexNew) + newString;
		indexOld = indexNew + lengthOld;
		if (!replaceAll)
			break;
		indexNew = sourceString.indexOf(oldString, indexOld);
	}
	sbTemp += sourceString.substring(indexOld);

	return sbTemp;
}

/*
	Inputvalidation for E-Mail Adresses
*/
function checkEmail(field, errortext){
	var ret; 
	if(field!=null){
		ret=true;
		var str=field.value;
		filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if(filter.test(str) == false){
			if(errortext!=null && errortext.length>0){
				alert(errortext);
			}
			ret=false;
			setFocusTo(field);
		}	
	}else{
		ret=false;
	}
	return ret;
}

/*
	Check the length of the entered text in a Form field
*/
function checkMinLength(field, minLength, errortext){
	var ret;
	ret=true;
	if(field!=null){
		if(minLength>0 && field.value.length<minLength){
			ret=false
			if(errortext!=null && errortext.length>0){
				alert(errortext);
			}
		}
	
		if(!ret){
			setFocusTo(field);
		}	
	}else{
		ret=false;
	}
	return ret;
}

/*
	Check the length of the entered text in a Form field
*/
function checkMaxLength(field, maxLength,errorText){
	var ret;
	ret=true;
	if(field!=null){
		if(minLength>0 && field.value.length>maxLength){
			ret=false
			if(errortext!=null && errortext.length>0){
				alert(errortext);
			}
		}
	
		if(!ret){
			setFocusTo(field);
		}	
	}else{
		ret=false;
	}
	return ret;
}

function checkMinMaxLength(field,minLength,maxLength, errortext){
	var ret;
	ret=true;
	if(field!=null){
		if((minLength>0 && field.value.length<minLength) || (maxLength>0 && field.value.length>maxLength)){
			ret=false
			if(errortext!=null && errortext.length>0){
				alert(errortext);
			}
		}
	
		if(!ret){
			setFocusTo(field);
		}	
	}else{
		ret=false;
	}
	return ret;

}


/*
	Little help function to set the focus to the given form field
*/
function setFocusTo(field){
	if(field!=null){
		field.focus();
		field.select();
	}
}
