/*
FUNCTIONS
 1. addEvent
 2. isEmpty
 3. notValid
 4. isInteger
 5. isFloat
 6. isValidDate
 7. isValidTime
 8. isValidEmailAddress
 9. centerOnScreen
10. existsInList
11. setComboField
12. getComboFieldValue
13. getComboFieldText
14. setRadioField
15. getTheDate
16. hideStatus
17. truncate
18. replaceDoubleQuotesWithSingle
19. trim
20. stripSpaces
21. fixAmpersand
22. openWindow
23. openWindowAt
24. textCounter (counter)
25. setCounterValue (counter)
26. findNearestInList
27. rAll
28. browserDetection
29. OSDetection
30. blockEnter
31. emptyContent
*/

function addEvent(o, e, f){
	if (o.addEventListener) {
		o.addEventListener(e, f, true);
		return true;

	} else if (o.attachEvent) {
		return o.attachEvent("on" + e, f);
	}
	else {
		return false;
	}
}

function isEmpty(source, description) {
	if (source.value.length == 0) {
		alert('Πρέπει να εισαχθεί ' + description);
		source.focus();
		return true;
	} else {
		if (notValid(source, description)) {
			return true;
		}
		return false;
	}
}

function notValid(source, description) {
	if (source.value.indexOf('"') != -1) {
		alert("Πρέπει να εισαχθεί σωστά " + description + "\nΔεν επιτρέπεται η χρήση του χαρακτήρα \".\nΧρησιμοποιείστε αντί αυτού τον χαρακτήρα '.");
		source.focus();
		return true;
	} else {
		return false;
	}
}

function isInteger(source, description) {
	if (isNaN(parseInt(source.value, 10))) {
		alert("Πρέπει να εισαχθεί σωστά " + description + "\nΑποδεκτές τιμές μόνο ακέραιοι αριθμοί.");
		source.focus();
		return false;
	} else {
		source.value = parseInt(source.value, 10);
		return true;
	}
}

function isFloat(source, description) {
	if (isNaN(parseFloat(source.value))) {
		// Δοκιμάζω μήπως είναι ακέραιος.
		if (isNaN(parseInt(source.value, 10))) {
			alert("Πρέπει να εισαχθεί σωστά " + description + "\nΑποδεκτές τιμές μόνο ακέραιοι και δεκαδικοί αριθμοί.");
			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], 10))) {
			return false;
		} else {
			comp[i] = parseInt(comp[i], 10);
		}
	}
	if (comp.length != 3) {
		return false;
	}
	if ((comp[1] < 1) || (comp[1] > 12)) {
		return false;
	} else if (months[parseInt(comp[1], 10)] < comp[0]) {
		if (!((comp[1] == 2) && (comp[0] == 29) && ((comp[2] % 4) == 0) && ((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 = "Πρέπει να εισαχθεί σωστά η email διεύθυνση!";
	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;
}

function centerOnScreen(WIN, WIDTH, HEIGHT) {
	WIN.moveTo((screen.width - WIDTH) / 2, (screen.height - HEIGHT) / 2);
}

function existsInList(list, value) {
	for (i = 0; i< list.options.length; i++) {
		if (list.options[i].value.toLowerCase() == value.toLowerCase()) {
			return true;
		}
	}
	return false;
}

function setComboField(list, value) {
	for (i = 0; i < list.options.length; i++) {
		if (list.options[i].value == value) {
			// μπορώ επίσης να χρησιμοποιήσω και το list.selectedIndex = i;
			list.options[i].selected = true;
			return;
		}
	}
}
function getComboFieldValue(id) {
	return document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
}

function getComboFieldText(id) {
	return document.getElementById(id).options[document.getElementById(id).selectedIndex].text;
}

function setRadioField(list, value) {
	for (i = 0; i < list.length; i++) {
		if (list[i].value == value) {
			// μπορώ επίσης να χρησιμοποιήσω και το list.selectedIndex = i;
			list[i].checked = true;
			return;
		}
	}
}

function getTheDate() {
	var dayarray = new Array("Κυριακή", "Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο");
	var montharray = new Array("Ιανουαρίου", "Φεβρουαρίου", "Μαρτίου", "Απριλίου", "Μαΐου", "Ιουνίου", "Ιουλίου", "Αυγούστου", "Σεπτεμβρίου", "Οκτωβρίου", "Νοεμβρίου", "Δεκεμβρίου");

	var mydate = new Date();
	var year = mydate.getYear();

	if (year < 1000) year += 1900;

	var day = mydate.getDay();
	var month = mydate.getMonth();
	var daym = mydate.getDate();

	if (daym < 10) daym = "0" + daym;

	var hours = mydate.getHours();
	var minutes = mydate.getMinutes();
	var seconds = mydate.getSeconds();
//	var dn = "πμ";

// Για να φαίνεται η ώρα σε 24ωρη βάση.
//	if (hours >= 12) dn = "μμ";

//	if (hours > 12) hours = hours - 12;
//	if (hours == 0) hours = 12;
	if (minutes <= 9) minutes = "0" + minutes;
	if (seconds <= 9) seconds = "0" + seconds;

	//change font size here
	//var cdate = dayarray[day] + " " + daym + " " + montharray[month] + " " + year + ", " + hours + ":" + minutes + ":" + seconds //+ " " + dn;
	  var cdate = dayarray[day] + " " + daym + " " + montharray[month] + " " + year + ", " + hours + ":" + minutes  //+ " " + dn;

	if (document.all) document.all.clock.innerHTML = cdate;
	else if (document.getElementById) document.getElementById("clock").innerHTML = cdate;
	else document.write(cdate);
}

// Μην εμφανίζεις τίποτα στο statusbar όταν το ποντίκι είναι πάνω από link.
function hideStatus() {
	window.status = "";
	return true;
}

if (document.layers && !is.ns ) document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);

document.onmouseover = hideStatus;
document.onmouseout = hideStatus;

function truncate(field, limit) {
	if (field.value.length > limit) {
		field.value = field.value.substring(0, limit - 1);
	}
}

function replaceDoubleQuotesWithSingle(source) {
	re=/"/gi;
	source.value = source.value.replace(re, "'");
}

function trim(str) {
	str = str.replace(/^\s+/gi, "");
	str = str.replace(/\s+$/gi, "");
	return str;
}

function stripSpaces(source) {
	re=/ /gi;
	return source.replace(re, "");
}

function fixAmpersand(source, replacement) {
	re=/\&/gi;
	return source.replace(re, replacement);
}

function openWindow(destination, title, width, height, wantScrollbars, wantStatusBar, wantLocation, isResizable) {
	var features =	"alwaysRaised=yes" +
			",location=" + wantLocation +
			",scrollbars=" + wantScrollbars +
			",status=" + wantStatusBar +
			",resizable=" + isResizable +
			",width=" + width +
			",height=" + height +
			",left=" + (screen.width - width) / 2 +
			",top=" + (screen.height - height) / 2;
	return window.open(destination, title, features);
}

function openWindowAt(destination, title, width, height, wantScrollbars, wantStatusBar, wantLocation, isResizable, left, top) {
	var features =	"alwaysRaised=yes" +
			",location=" + wantLocation +
			",scrollbars=" + wantScrollbars +
			",status=" + wantStatusBar +
			",resizable=" + isResizable +
			",width=" + width +
			",height=" + height +
			",left=" + left +
			",top=" + top;
	return window.open(destination, title, features);
}

// Αρχή διαδικασίας counter.
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}

function setCounterValue(field, limit) {
	eval("textCounter(document.getElementById('" + field + "'), document.getElementById('" + field + "_chars_left'), " + limit + ")");
}
// Τέλος διαδικασίας counter.

function findNearestInList(list, token) {
	for (var i = 0; i < list.length; i++) {
		if (token != "") {
			if (list.options[i].text.toLowerCase().indexOf(token.toLowerCase()) == 0) {
				list.options[i].selected = true;
				break;
			}
		}
	}
}

function rAll(source, item, replacement) {
	eval("re=/" + item + "/gi;");
	source.value = source.value.replace(re, replacement);
}

function browserDetection() {
	var detect = navigator.userAgent.toLowerCase();
	var BROWSER;

	if (checkIt(detect, 'konqueror')) BROWSER = "Konqueror";
	else if (checkIt(detect, 'safari')) BROWSER = "Safari"
	else if (checkIt(detect, 'omniweb')) BROWSER = "OmniWeb"
	else if (checkIt(detect, 'opera')) BROWSER = "Opera"
	else if (checkIt(detect, 'webtv')) BROWSER = "WebTV";
	else if (checkIt(detect, 'icab')) BROWSER = "iCab"
	else if (checkIt(detect, 'msie')) BROWSER = "IE"
	else if (checkIt(detect, 'gecko')) BROWSER = "Mozilla"
	else if (!checkIt(detect, 'compatible')) BROWSER = "Netscape"
	else BROWSER = "An unknown browser";

	return BROWSER;
}

function OSDetection() {
	var detect = navigator.userAgent.toLowerCase();
	var OS;

	if (checkIt(detect, 'konqueror')) OS = "Linux";

	if (!OS) {
		if (checkIt(detect, 'linux')) OS = "Linux";
		else if (checkIt(detect, 'x11')) OS = "Unix";
		else if (checkIt(detect, 'mac')) OS = "Mac"
		else if (checkIt(detect, 'win')) OS = "Windows"
		else OS = "an unknown operating system";
	}

	return OS;
}

function checkIt(detect, string) {
	place = detect.indexOf(string) + 1;

	return place;
}

function blockEnter(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13 || charCode == 3) {
		return false;
	} else {
		return true;
	}
}

function emptyContent() {
	if (arguments[0])
		while (arguments[0].firstChild)
			arguments[0].removeChild(arguments[0].firstChild);
}