//<![CDATA[

function validateFormOnSubmit(theForm, source) {
var reason = "";
//alert(source);
	switch(source) {
		case "formNewRide":
			from = document.getElementById("fromAddress").value;
			if (from.lastIndexOf(',') >= 0) { 
				var matches = from.match(/,/g); // try to match ','
				var count = matches.length; // this is how many! 

				if (count >= 3) {
					var a = from.lastIndexOf(',');
					var b = from.substring(0,a); 
					var c = b.lastIndexOf(',');
					from = b.substring(0,c);
				} else if (count == 2) {
					var a = from.lastIndexOf(',');
					from = from.substring(0,a); 
				}
			}	

			to = document.getElementById("toAddress").value;
			if (to.lastIndexOf(',') >= 0) { 
				var matches = to.match(/,/g); // try to match ','
				var count = matches.length; // this is how many! 
				
				if (count >= 3) {
					var a = to.lastIndexOf(',');
					var b = to.substring(0,a); 
					var c = b.lastIndexOf(',');
					to = b.substring(0,c);
				} else if (count == 2) {
					var a = to.lastIndexOf(',');
					to = to.substring(0,a); 
				}
			}	
	
			document.getElementById("tripName").value = 'From ' + from + ' To ' + to ;
			document.getElementById("time").value = document.getElementById("timeList").value;

			reason += validateEmpty(theForm.fromLatResult);
			reason += validateEmpty(theForm.toLatResult);
			reason += validateEmail(theForm.email);
			reason += validateEmpty(theForm.date);

			if (reason != "") {
				alert("Some fields need correction:\n\n" + reason);
				return false;
			} 
			return true;
			break;

		case "register":
			reason += validateUsername(theForm.username);
			reason += validateEmail(theForm.email);
			reason += validatePassword(theForm.pass);
			reason += validateEmpty(theForm.secretNumber);
			if (reason != "") {
				alert("Some fields need correction:\n\n" + reason);
				return false;
			}
			return true;
			break;    
	
		case "login":
			break;    
	
		case "forgot":
			reason += validateEmail(theForm.email);
			if (reason != "") {
				alert("Please make sure your email is correct:\n\n" + reason);
				return false;
			}
			return true;
			break;    
	
		case "formCity":
			reason += validateEmpty(theForm.infolat1);
			reason += validateEmpty(theForm.date);
			reason += validateEmpty(theForm.name);
			reason += validateLists(theForm.frequency);

			if (reason != "") {
				alert("Some fields need correction:\n\n" + reason);
				return false;
			}
			return true;
			break;

		case "formAirport":
			reason += validateEmpty(theForm.airportsList);
			reason += validateEmpty(theForm.infolat2);
			reason += validateEmpty(theForm.date);
			reason += validateEmpty(theForm.name);
			reason += validateLists(theForm.frequency);
			reason += validateLists(theForm.toFrom);

			if (reason != "") {
				alert("Some fields need correction:\n\n" + reason);
				return false;
			}
			return true;
	
			break;
	
		default:
	
			break;
	}
}

///////////////////////////////////////
// ValidatePassword

function validatePassword(fld) {
  var error = "";
  var pass = document.getElementById('pass');
  var pass2 = document.getElementById('pass2');
  
    if (pass.value == "" || pass2.value == "") {
        pass.style.background = 'Yellow'; 
        pass2.style.background = 'Yellow'; 
        error = "- Please fill out both passwords fields, with the same password.\n"
    } else if ((pass.value.length < 6) || (pass2.value.length < 6)) {
        pass.style.background = 'Yellow'; 
        pass2.style.background = 'Yellow'; 
        error = "- Make sure your passwords is at least 6 characters long.\n"
    } else if (pass.value != pass2.value) {
        pass.style.background = 'Yellow'; 
        pass2.style.background = 'Yellow'; 
        error = "- Make sure you typed the same password twice.\n"
    } else {
        pass.style.background = 'White'; 
        pass2.style.background = 'White'; 
    }
    return error;  
}
  
///////////////////////////////////////
// ValidateEmpty

function validateEmpty(fld) {
    var error = "";

    if ( (fld.value.length == 0) || (fld.value == 'Name that trip (e.g.: Uptown to East Village) ?') || (fld.value == 'What date ?') || (fld.value == 'Type the name of the city, airport, or country !') ) {

	    switch(fld.name) {

		// NEW FORM
		case "fromLatResult":
		    error = "- Make sure you selected a departure point, address or airport ! \n";
		    document.getElementById("fromAddress").style.background = 'Yellow';
		    break;
		case "toLatResult":
		    error = "- Make sure you selected an arrival point, address or airport ! \n";
		    document.getElementById("toAddress").style.background = 'Yellow';
		    break;
		case "date":
		    error = "- Please select the date of your trip.\n";
		    document.getElementById("jsDate").style.background = 'Yellow';
		    break;

		case "secretNumber":
		    error = "- You didn't enter the number.\n";
		    fld.style.background = 'Yellow';
		    break;

		// OLD FORM
		case "infolat1":
		    error = "- Make sure you selected both a departure point and arrival point on the map: click to select the two locations !.\n";
		    fld.style.background = 'Yellow';
		    break;
		case "infolat2":
		    error = "- Make sure you selected a departure OR arrival point on the map!.\n";
		    fld.style.background = 'Yellow';
		    break;
		case "airportsList":
		    error = "- Make sure you selected the airport you're going to or coming from!\n";
		    fld.style.background = 'Yellow';
		    break;    
		case "name":
		    error = "- Please give a meaningfull name to your trip (like 'JFK to East Village', 'Downtown to Uptown West', ...).\n";
		    fld.style.background = 'Yellow';
		    break;
		default:
		    break;
	    }

    } else {
	fld.style.background = 'White';
	document.getElementById("fromAddress").style.background = 'White';
	document.getElementById("toAddress").style.background = 'White';
	document.getElementById("jsDate").style.background = 'White';
    }
    return error;  
}

///////////////////////////////////////
// ValidateLists

function validateLists(fld) {
    var error = "";

    var listId = fld.selectedIndex;
    var listValue = fld.options[listId].text;
    //alert('Id: '+listId+' - Value: '+listValue);

    if (listId == 0) {
        fld.style.background = 'Yellow';

	    switch(fld.name) {
		case "frequency":
		    error = "- Please select a frequency for your trip.\n";
		    break;
		default:
		    break;
	    }

    } else {
        fld.style.background = 'White';
    }
    return error;
}

///////////////////////////////////////
// ValidateUsername

function validateUsername(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "- You didn't enter a username.\n";
    } else if (fld.value.length < 5) {
        fld.style.background = 'Yellow'; 
        error = "- Please make sure your username is at least 6 characters.\n";
    } else {
 	var value = fld.value;
        usernameCheck(value);
    }
    return error;
}


function trim(s) {
  return s.replace(/^\s+|\s+$/, '');
}

///////////////////////////////////////
// ValidateEmail

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "" || fld.value == "yourname@youremail.com") {
        fld.style.background = 'Yellow';
        error = "- You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "- Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "- The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

/////////////////////////////////////////
// AJAX VERIFICATIONS
/////////////////////////////////////////

var xmlHttp;

function usernameCheck(value) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}	
	var url="userRegisterProcessor.php";
	url = url+"?username="+value;
	url = url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged(value) {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="Complete") { 
		var checkCount = xmlHttp.responseText;
		if (checkCount > 0) {
			alert('This user name is already used ... Please choose another one.');
			document.getElementById('username').style.background = 'Yellow';
			document.getElementById('username').focus();
		} else {
			document.getElementById('username').style.background = 'White';
		}
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) { //Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}



//]]>
