/*
contanct.js
validation of form entrys for contact.html
from ariel.de
oBu 26-02-03
last edited 26-02-03 oBu
*/

function trim(str){
	// trim removes all leading and trailing spaces of string str
	// remove all leading spaces of str
	var i = 0;
	while ((i < str.length) && (str.charAt(i) == ' ')){
		i++;
	}
	str = str.substring(i);
	// remove all trailing spaces of str
	i = str.length;
	while ((i > 0) && (str.charAt(i-1) == ' ')){
		i--;
	}
	str = str.substring(0,i);
	return str;
}

function isTextFieldEmpty(objForm, fieldname){
	/* isTextFieldEmpty checks if field "fieldname" in form "form" is empty or only
    contains white space characters */
	return ( trim(objForm.elements[fieldname].value)=='' );
}

function isListboxSelected(objForm, fieldname){
  /* isListboxSelected checks if an item in the listbox "fieldname" in form "formname"
    is selected. It assumes that when an item with value '' is selected, none of the
    options are selected. This is the case in all required listbox fields on the Contact Us page. 
  */
	iSelIndex = objForm.elements[fieldname].options.selectedIndex;
	return ( !((iSelIndex == -1) || (objForm.elements[fieldname].options[iSelIndex].value == '')) );
}

function isInt(strNumber){
	// isInt checks if the string strNumber is an integer.
	return ( !(isNaN(parseInt(strNumber))) );
}


function checkAge(refAge, birthMonth, birthYear){
	/* checkAge checks if a person with birth month birthMonth and birth year birthYear is of age 
	refAge or older than refAge. Birth day will be the first of the month in the check.*/
	var birthDay = '1';
	var dtBirth = new Date(birthYear, birthMonth, birthDay);
	var dtCurrent = new Date();
	var dtRef = new Date(dtCurrent.getYear()-refAge, dtCurrent.getMonth(), dtCurrent.getDate());
	return (dtBirth <= dtRef);
}

function isValidYear(pstrYear){
	/* isValidYear checks if pstrYear contains a valid birth year. If so, isValidYear returns true,
	otherwise isValidYear returns false */
  
	if (isInt(pstrYear)){
		var iYear = parseInt(pstrYear);
		var bYear = ((iYear.toString()).length == 4) && (iYear>=1900);
		return bYear;
	}else{
		return false;
	}
}

function isValidEmailAddress(pstrEmailAddress){
	// isValidEmailAddress checks if the string pstrEmailAddress contains an @ symbol
	return (pstrEmailAddress.indexOf('@') != -1)
}

function check_form(objForm){
	/* check_form performs the validation of the fields in the Contact Us form */
	var bCheckForm = false;
	if (isTextFieldEmpty(objForm, 'feedback_message')){
		alert('Please enter a message!');
		objForm.feedback_message.focus();
	}
	else if (isTextFieldEmpty(objForm, 'email_address')){
		alert('Please enter your email address!');
		objForm.email_address.focus();
	}else if (!isValidEmailAddress(objForm.email_address.value)){
		alert('You have entered an invalid email address');
		objForm.email_address.focus();
	}else if (!isListboxSelected(objForm, 'title')){
		alert('Please select a title.');
		objForm.title.focus();
	}else if (isTextFieldEmpty(objForm, 'first_name')){
		alert('Please enter your first name!');
		objForm.first_name.focus();
	}else if (isTextFieldEmpty(objForm, 'surname')){
		alert('Please enter your surname!');
		objForm.surname.focus();
	}else if (!isListboxSelected(objForm, 'birth_month')){
		alert('Please select your month of birth!');
		objForm.birth_month.focus();
	}else if (isTextFieldEmpty(objForm, 'birth_year')){
		alert('Please enter the year of your birth');
		objForm.birth_year.focus();
	}else if (!isValidYear(objForm.birth_year.value)){
		alert('Your birth year is invalid. \nPLease enter four digits, higher than 1900');
		objForm.birth_year.focus();
	}else if (!checkAge (14, objForm.birth_month.value, objForm.birth_year.value)){
		alert('Sorry, we may not corrsepond with customers younger than 14.');
		objForm.birth_year.focus();
	}else{
		bCheckForm = true;
	}
	return bCheckForm;
}
