// checkform.js - v0.1 - 19 Mar 2003 - MP
// JavaScript functions for checking data entered into a form

// Valid characters:
var letters = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var specials = "àáèéìíòóöùúÀÁÈÉÌÍÒÓÙÚ";
var specialcharacters = "\\(\\)<>@&,;:°\\\\\\\"\\.\\[\\]";
var digits = "0123456789";
var nameString = letters + digits + specials + "-_. '";
var textString = letters + digits + specials + specialcharacters + "() \ /-~., '?!&;:";
var typeString = letters + digits + specials + specialcharacters + textString + "+() \ /-~., '?!&;:";
var commentString = textString + "\n\r€";
var phoneString = digits + " .-+()";
var emailString = letters + digits + /^(.+)@(.+)$/;
//var fileString = letters + digits + specialcharacters + "-_";
var alphanumericString = letters + digits;
var intString = digits + "-";
var floatString = digits +"-.";
var currencyString = digits + ".,";
var dateString = digits + "/";
var timeString = digits + ":";

function isEmpty(elem) {
	// Checks if a form element is empty:
	return ((elem.value == null) || (elem.value.length == 0))
}

function checkMinMax(elem, minValue, maxValue) {
	// Checks if the value of elem is between the minimum and maximum values:
	if ((elem.value >= minValue) && (elem.value <= maxValue)) {
		return true;
	} else {
		alert("Please enter a value for '" + elem.name + "' in the range: '" + minValue + "' to '" + maxValue + "'");
		elem.focus();
		return false;
	}		
}

function checkLength(elem, minLength, maxLength) {
	// Checks that the value of elem has the correct number of characters.
	//if ((elem.value.length >= minLength) && (elem.value.length <= maxLength)) {
	if ((elem.value.length <= 500)) {
		return true;
	} else {
		if ((elem.value.length > 500)) {
			alert("Too much text has been entered in the Information box, please delete some text.\nMaximum number of characters allowed is 500.");
		} 
		//else {
		//	alert("Please enter a value for '" + elem.name + "' with between " + minLength + " and " + maxLength + " characters.");
		//}
		elem.focus();
		return false;
	}
}

function compareFields(elem1, elem2, msg) {
	// Checks that elem1 and elem2 have the same value, otherwise
	// displays an error message.

    if (elem1.value == elem2.value) {
        return true;
    } else {
        alert(msg);
        elem1.value = "";
        elem2.value = "";
        elem1.focus();
        return false;
    }
}

function CorrectTypeSelected(elem, msg) {
	// Checks that elem does not Select Aircraft Type as a type, otherwise
	// displays an error message.
	
    $incorrect_text = "Select Aircraft Type";
    if (elem.value != $incorrect_text) {
        return true;
    } else {
        alert(msg);
        //elem.value = "";
        elem.focus();
        return false;
    }
}

function validText(elem, range, msg, isOptional) {
	// Checks if the text entered is within a valid range of characters.
	// Alerts the user if this is not the case. 
	// isOptional indicates if the field can be left blank.
 
	var isOK = true;
	if (isEmpty(elem)) {
		if (! isOptional) {
			alert(msg);
			elem.focus();
			return false;
		} else {
			return true;
		}	
	} else { 
		for (var i = 0; i < elem.value.length; i++) {
			isOK = isOK && (range.indexOf(elem.value.charAt(i)) != -1);
		}
		if (! isOK) {
			alert(msg);
			elem.focus();
		}
		return isOK;
	} 
}

function validEmail(elem, msg, isOptional)  {
	
	var isOK = true;
	if(!isOptional && elem.value =='') {
		alert(msg);
		elem.focus();
		return false;
		}
	with(elem.value) {
		if (length <8
			|| match(/[^A-Z0-9@\._-]/i)
			|| match(/(@.*@)|(@\.)|(\.@)|(^\.)|(\.{2})|(-{2})|(_{3})/)
			|| match(/(-@)|(\@-)|(\.-)|(-\.)/)
			|| !match(/^.+\@(\[?)[A-Z0-9\-\.]+\.([A-Z]{2,3}|[0-9]{1,3})(\]?)$/i)
		)
		{
			alert(msg);
			elem.value =''; //you can uncomment this to clear the field
			elem.focus();
			return false;
		}
	//alert('good one'); //remove this line in production mode
	return isOK;
	}
}

function validDate(elem, msg, isOptional) {
	// Checks if the date is valid text, 0-9 and "/", then checks if date is valid.
	// Alerts the user if this is not the case. 
	// isOptional indicates if the field can be left blank.
	if (isEmpty(elem)) {
		if (! isOptional) {
			alert(msg);
			elem.focus();
			return false;
		} else {
			return true;
		}
	} else {
		if (validText(elem,dateString,msg,isOptional)) {
			var tempArray, tempDate;
			// Split the date around '/' into dd/mm/yyyy:
			tempArray = elem.value.split("/");
			// JavaScript counts months from zero, so subtract 1 from month value:
			tempArray[1] = tempArray[1] - 1;
			tempDate = new Date(tempArray[2],tempArray[1],tempArray[0])
			// Use date functions to compare actual day/month/year values:
			if ((tempDate.getFullYear() == tempArray[2]) && (tempDate.getMonth() == tempArray[1]) && (tempDate.getDate() == tempArray[0])) {
				return true;
			} else {
				alert(msg)
				elem.focus();
				return false;
			}
		}	
	} 
}

function validTime(elem, msg, isOptional) {
	// Checks if the time is valid text, 0-9 and ":", then checks if time is valid.
	// Alerts this user if this is not the case.
	// isOptional indicates if the field can be left blank.
	if (isEmpty(elem)) {
		if (! isOptional) {
			alert(msg);
			elem.focus();
			return false;
		} else {
			return true;
		}
	} else {
		if (validText(elem,timeString,msg,isOptional)) {
			var tempArray
			// split time around ":" and check hours between 0 and 23, minutes between 0 and 59:
			tempArray = elem.value.split(":")
			if ((tempArray[0] >= 0) && (tempArray[0] <= 23) && (tempArray[1] >= 0) && (tempArray[1] <= 59)) {
				return true;
			} else {
				alert(msg);
				elem.focus();
				return false;
			}			
		}
	}
}

function checkPulldown(elem, msg, isOptional) {
	// Checks that user has selected an option from a pulldown menu.
	// If not, displays error message.
	// isOptional indicates if the pulldown can be left blank.
	if (elem.selectedIndex == 0 && !isOptional) {
		alert(msg);
		elem.focus();
		return false;
	} else {
		return true;
	}
}

function checkScores(elem1, elem2, msg) {
	// Function to check that the value in elem1 is greater than or equal to the value in elem2.
	// Prompts user if this is not the case.
	if (elem1.value >= elem2.value) {
		return true;
	} else {
		elem2.focus();
		alert(msg);
		return false;
	}
}

function connectedElements(elem1, elem2, msg) {
	// Function checks two connected form elements.
	// If there's a value in elem1, there must be a value in elem2 and vice-versa.
	// Prompts the user if this is not the case.
	
	if ((isEmpty(elem1) && isEmpty(elem2)) || (!(isEmpty(elem1)) && !(isEmpty(elem2)))) {
		return true;
	} else {
		elem1.focus();
		alert(msg);
		return false;	
	}
}


function checkUpload(elem, msg, isOptional) {

	
	if(!isOptional) {
	if(elem.value =='') {
		alert(msg);
		return elem.focus();
		} else {
		return true;
	}
}
}