// FUNCTIONS
//  1. isEmpty
//  2. notValid
//  3. isInteger
//  4. isFloat
//  5. isValidDate
//  6. isValidTime
//  7. isValidEmailAddress
//  8. isValidWebAddress
//  9. centerOnScreen
// 10. existsInList
// 11. setComboField

function isEmpty(source, description) {
	if (source.value.length == 0) {
		alert('You must insert '+description);
		source.focus();
		return true;
	} else {
		if (notValid(source, description)) {
			return true;
		}
		return false;
	}
}

function notValid(source, description) {
	if (source.value.indexOf('"') != -1) {
		alert("You must insert "+description +"correctly!\nThe usage of character \" is not allowed.\nUse character ' instead.");
		source.focus();
		return true;
	} else {
		return false;
	}
}

function isInteger(source, description) {
	if (isNaN(parseInt(source.value))) {
		alert("You must insert "+description +" correctly!\nOnly integer numbers are allowed.");
		source.focus();
		return false;
	} else {
		source.value = parseInt(source.value);
		return true;
	}
}

function isFloat(source, description) {
	if (isNaN(parseFloat(source.value))) {
		// Δοκιμάζω μήπως είναι ακέραιος.
		if (isNaN(parseInt(source.value))) {
			alert("You must insert "+description +"correctly!\nOnly integer and decimal numbers are allowed.");
			source.focus();
			return false;
		} else {
			return true;
		}
	} else {
		source.value = parseFloat(source.value);
		return true;
	}
}

function isValidDate(source) {
	months = new Array();
	months[1] = 31;
	months[2] = 28;
	months[3] = 31;
	months[4] = 30;
	months[5] = 31;
	months[6] = 30;
	months[7] = 31;
	months[8] = 31;
	months[9] = 30;
	months[10] = 31;
	months[11] = 30;
	months[12] = 31;
	if (source.value.indexOf('/') < 0) {
		return false;
	}
	comp = source.value.split('/');
	for (i = 0; i < comp.length; i++) {
		if (isNaN(parseInt(comp[i]))) {
			return false;
		} else {
			comp[i] = parseInt(comp[i]);
		}
	}
	if (comp.length != 3) {
		return false;
	}
	if ((comp[1] < 1) || (comp[1] > 12)) {
		return false;
	} else if (months[parseInt(comp[1])] < comp[0]) {
		if (!((comp[1] == 2) && (comp[0] == 29) && ((comp[2] % 400) == 0))) {
			return false;
		}
	}
	if (comp[0].length == 1) {
		comp[0] = "0" + comp[0];
	}
	if (comp[1].length == 1) {
		comp[1] = "0" + comp[1];
	}
	source.value = comp.join('/');
	return true;
}

function isValidTime(source) {
	if (source.value.indexOf(':') < 0) {
		return false;
	}
	comp = source.value.split(':');
	if (comp.length != 2) {
		return false;
	}
	if ((comp[0].length == 0) || (comp[1].length == 0) ) {
		return false;
	}
	if ((comp[0] < 0) || (comp[0] > 23)) {
		return false;
	} else if ((comp[1] < 0) || (comp[1] > 59)) {
		return false;
	}
	if (comp[0].length == 1) {
		comp[0] = "0" + comp[0];
	}
	if (comp[1].length == 1) {
		comp[1] = "0" + comp[1];
	}
	source.value = comp.join(':');
	return true;
}

function isValidEmailAddress(source) {
	var message = "You must insert your email address correclty!";
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:'/`~!#$%^&*+=|{}?\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = source.value.match(emailPat);
	if (matchArray == null) {
		alert(message);
		source.focus();
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];
	if (user.match(userPat) == null) {
		alert(message);
		source.focus();
		return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i <= 4; i++) {
			if (IPArray[i] > 255) {
				alert(message);
				source.focus();
				return false;
			}
		}
		return true;
	}
	var domainArray = domain.match(domainPat);
	if (domainArray == null) {
		alert(message);
		source.focus();
		return false;
	}
	var atomPat = new RegExp(atom, "g");
	var domArr = domain.match(atomPat);
	var len = domArr.length;
	if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 3) {
		alert(message);
		source.focus();
		return false;
	}
	if (len < 2) {
		alert(message);
		source.focus();
		return false;
	}
	return true;
}

// Has a bug in web address that contain IPs.
function isValidWebAddress(source) {
	var message = "You must insert the web address correctly!\nNote:\nThe web address must also include the protocol f.e. http://www.somesite.gr";
	if (	(source.value.indexOf("http://")!=0) ||
		(source.value.indexOf(".")<0) ||
		(source.value.indexOf(" ")!=-1)||
		(source.value.indexOf("\\")!=-1)||
		(source.value.indexOf("+")!=-1)||
		(source.value.indexOf("*")!=-1)||
		(source.value.indexOf("{")!=-1)||
		(source.value.indexOf("}")!=-1)||
		(source.value.indexOf("[")!=-1)||
		(source.value.indexOf("]")!=-1)||
		(source.value.indexOf("(")!=-1)||
		(source.value.indexOf(")")!=-1)||
		(source.value.indexOf("`")!=-1)||
		(source.value.indexOf("~")!=-1)||
		(source.value.indexOf("!")!=-1)||
		(source.value.indexOf("@")!=-1)||
		(source.value.indexOf("#")!=-1)||
		(source.value.indexOf("\$")!=-1)||
		(source.value.indexOf("%")!=-1)||
		(source.value.indexOf("&")!=-1)||
		((source.value.lastIndexOf(":")!=-1) && (source.value.lastIndexOf(":")!=4))||
		(source.value.indexOf(";")!=-1)||
		(source.value.indexOf("'")!=-1)||
		(source.value.indexOf("\"")!=-1)||
		(source.value.indexOf(">")!=-1)||
		(source.value.indexOf("<")!=-1)||
		(source.value.indexOf(">")!=-1)||
		(source.value.indexOf("?")!=-1)||
		(source.value.indexOf(",")!=-1)||
		((source.value.length-source.value.lastIndexOf("."))<3)) {
			alert(message);
			source.focus();
			return false;
	}
	return true;
}

function centerOnScreen(WIN, WIDTH, HEIGHT) {
	WIN.moveTo((screen.width - WIDTH) / 2, (screen.height - HEIGHT) / 2);
}

function existsInList(list, value) {
	for (i=0; i< list.length; i++) {
		if (list.options[i].text.toLowerCase() == value.toLowerCase()) {
			return true;
		}
	}
	return false;
}

function setComboField(list, value) {
	for (i = 0; i < list.length; i++) {
		if (list.options[i].value == value) {
			// μπορώ επίσης να χρησιμοποιήσω και το list.selectedIndex = i;
			list.options[i].selected = true;
			return;
		}
	}
}
