/* taxi functions */

function checkString( ts, u )
{
	var currentChar;

	for ( i=0; i<ts.length; i++ ) {
		currentChar = ts.charAt(i);
		if ( u.indexOf(currentChar) == -1 ) return false;
	}

	return true;
}

function isValidLength( ts, min, max )
{
	if ( ts.length >= min && ts.length <= max ) return true;
	else return false;
}

var ws = " \t\n\r";

function isEmpty( ts )
{
	if ( ts == null ) return true;
	if ( ts.length < 1 ) return true;
	if ( ts == "" ) return true;
	if ( checkString( ts, ws ) ) return true;
	return false;
}

function Contains( ts, u )
{
	var currentChar;

	for ( i=0; i<u.length; i++ ) {
		currentChar = u.charAt(i);
		if ( ts.indexOf(currentChar) == -1 ) return false;
	}

	return true;
}

function checkTaxi( pf )
{
	var ts = new String();
	var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var special = "A@BCEDFGIHJKMLNOPQSRTUVXZY[\]^_a`bcedfgihjkmlnopqsrtuvxzy{|}";
	var name = " -,'";
	var surname = "-' ";
	var mail = "@.-_";
	var num = "0123456789";
	var phone = "-./\+ ";
	var plus = "-. ,°#_&";
	var alphanum = alpha + num;

	ts = pf.firstname.value; /* first name validation */

	if ( isEmpty(ts) ) {
		alert("Please specify a First Name.");
		return false;
	}

	if ( !isValidLength(ts, 2, 30) || !checkString(ts, alpha + special + name) ) {
		alert("It does not appear that the first name you entered is valid.");
		return false;
	}

	ts = pf.lastname.value; /* last name validation */

	if ( isEmpty(ts) ) {
		alert("Please specify a Last Name.");
		return false;
	}

	if ( !isValidLength(ts, 2, 30) || !checkString(ts, alpha + special + surname) ) {
		alert("It does not appear that the last name you entered is valid.");
		return false;
	}

	ts = pf.email.value; /* e-mail validation */

	if ( isEmpty(ts) ) {
		alert("Please specify an E-mail Address.");
		return false;
	}

	if ( !isValidLength(ts, 5, 60) || !checkString(ts, alphanum + mail) || !Contains(ts, "@.") || ts.indexOf("@") != ts.lastIndexOf("@") ) {
		alert("It does not appear that the e-mail address you entered is valid.");
		return false;
	}

	ts = pf.phone.value; /* phone validation */

	if ( !isEmpty(ts) ) {
		if ( !isValidLength(ts, 5, 21) || !checkString(ts, num + phone) ) {
			alert("It does not appear that the phone number you entered is valid.");
			return false;
		}
	}

	ts = pf.people.value; /* people validation */

	if ( isEmpty(ts) ) {
		alert("Please specify the Number of People.");
		return false;
	}

	if ( !isValidLength(ts, 1, 3) || !checkString(ts, num) ) {
		alert("It does not appear that the number of people you entered is valid.");
		return false;
	}

	ts = pf.flight.value; /* flight validation */

	if ( isEmpty(ts) ) {
		alert("Please specify the Flight Number.");
		return false;
	}

	if ( !isValidLength(ts, 2, 8) || !checkString(ts, alphanum + plus) ) {
		alert("It does not appear that the flight number you entered is valid.");
		return false;
	}

	ts = pf.company.value; /* company validation */

	if ( isEmpty(ts) ) {
		alert("Please specify the Company you Fly.");
		return false;
	}

	if ( !isValidLength(ts, 2, 30) || !checkString(ts, alphanum + plus) ) {
		alert("It does not appear that the company name you entered is valid.");
		return false;
	}

	ts = pf.city.value; /* city validation */

	if ( isEmpty(ts) ) {
		alert("Please specify the City you come from.");
		return false;
	}

	if ( !isValidLength(ts, 2, 30) || !checkString(ts, alpha + plus) ) {
		alert("It does not appear that the city you entered is valid.");
		return false;
	}

	ts = pf.a_to.value; /* going to validation */

	if ( isEmpty(ts) ) {
		alert("Please specify the 'Going to' field.");
		return false;
	}

	if ( !isValidLength(ts, 2, 80) || !checkString(ts, alphanum + plus) ) {
		alert("It does not appear that the 'going to' field is valid.");
		return false;
	}

	if ( pf.jtype[0].checked ) {
		ts = pf.d_from.value; /* leaving from validation */

		if ( isEmpty(ts) ) {
			alert("Please specify the 'Leaving from' field.");
			return false;
		}

		if ( !isValidLength(ts, 2, 80) || !checkString(ts, alphanum + plus) ) {
			alert("It does not appear that the 'leaving from' field is valid.");
			return false;
		}
	}

	return true;
}