var regexText		= /^[a-zA-Z0-9α-ωΑ-Ωάέήίόύώϊϋς.,?;:!()-_+=|\/"@#$%& ]*$/;
var regexEmail		= /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,6}$/;

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function attachPopUP() {
 var a=getElementsByClass('popup',null,'a');
 if (a) {
  for(i=0; i<a.length; i++){
   a[i].onclick=function(){
    var href=this.href;
    var args=this.className.split(" ");
    var width=parseInt(args[1])+40;
    var height=parseInt(args[2])+30;
    var scrollbars=(args[3]=="true") ? ",scrollbars=yes" : ",scrollbars=no";
    window.open(href,'popup','width='+width+',height='+height+scrollbars);
    return false;
   }
  }
 }
}

function validateInput(input, pattern, required, minLength) {
	if (required) {
		if (input.value.length<minLength || !pattern.test(input.value)) { return false; }
	} else {
		if (input.value!="") {
			if (input.value.length<minLength || !pattern.test(input.value)) { return false; }
		}
	}
	return true;
}

function validateContactForm(f) {
	var i=document.getElementById('fullname');
	if (!validateInput(i, regexText, true, 12)) {
		alert("Παρακαλούμε πληκτρολογήστε το Ονοματεπώνυμό σας");
		return false;
	}
	var i=document.getElementById('email');
	if (!validateInput(i, regexEmail, true, 10)) {
		alert("Παρακαλούμε πληκτρολογήστε ένα έγκυρο e-mail");
		return false;
	}
	var i=document.getElementById('message');
	i.value=i.value.replace(/<\/?.+?\s?>/gi,"");
	if (!validateInput(i, regexText, true, 10)) {
		alert("Παρακαλούμε πληκτρολογήστε το μήνυμά σας");
		return false;
	}
	var message="Να αποσταλεί το μήνυμα;";
	if (confirm(message)) {
		return true;
	} else {
		return false;
	}
}

function attachContactValidation() {
	var f=document.getElementById('contactForm');
	if (f) {
		f.onsubmit=function() { return validateContactForm(this); }
	}
}

function init() {
	attachPopUP();
	attachContactValidation();
}

if (document.getElementById && document.getElementsByTagName) {
	window.onload=init;
}
