// JavaScript Document

// Check for empty input

function checkData(formObj){ //check if valid first name
	if (emptyField(formObj.firstName)){
		formObj.firstName.focus(); //not valid so set focus
		alert("Please enter a first name!");
		return false; //stops the form being submitted
	}
	//this point is reached when the first name field contains a valid entry
	else if(emptyField(formObj.lastName)){ //check if valid last name
		formObj.lastName.focus(); //not valid so set focus
		alert("Please enter a last name!");
		return false; //stops the form being submitted
	}	
	//this point is reached when the first and last name fields contains valid entries
	else if(emptyField(formObj.eMail)){ //check if valid eMail
		formObj.eMail.focus(); //not valid so set focus
		alert("Please enter an eMail address!");
		return false; //stops the form being submitted
	}	
	//this point is reached when the first and last name and eMail fields contain valid entries
	else if(emptyField(formObj.comments)){ //check if valid eMail
		formObj.comments.focus(); //not valid so set focus
		alert("Please enter your query!");
		return false; //stops the form being submitted
	}	
	else{
		return true; //submits the form
	}
}

function emptyField(fieldObj){ // used to check if a field contains valis data or not
	// returning true will mean there is no entry or only blank or tab
	// spaces, returning false will mean there is at least one non blank or non tab character
var result = true;
if(fieldObj.value.length == 0){ //check if no entry
	result = true;
}
// this point is reached only if there is some kind of entry,
// which could be just blank spaces
else{
	var i, ch;
	// test each character in the entry
	for (i=0; i< fieldObj.value.length; i++){
		ch = fieldObj.value.charAt(i);
		if(ch !=' ' && ch != '\t'){
		result = false;
		break;
	}
}
}
return result;
}

function checkEmail(fieldObj){
	// returning true means email address is invalid
	var result = false;
	var emailVal = fieldObj.value; // store email field's value
	if(emailVal.length < 6){
		result = true;
	}
	else if ((emailVal.indexOf("@") == -1) && (emailVal.indexOf(".") == -1) ){
		result = true;
	}
	return result;
}


