$(document).ready(function(){

$("#footer [title]").tooltip({offset: [0,0]});
$("#addguide").click(function(){$("#addguidepanel").slideToggle("slow", function() { });});
$("#addcomment").click(function(){$("#addcommentpanel").slideToggle("slow", function() { });});

addComment=function(){
$("#errormsg").html('');
$("#nameError").html('');
$("#emailError").html('');
$("#txtError").html('');
var cname=$("#name").val();
var cemail=$("#email").val();
var ctxt=$("#txt").val();
var error=false;
if(!notEmpty(cname)){$('#nameError').html('ΠΛΗΚΤΡΟΛΟΓΙΣΤΕ ΚΑΤΙ'); error=true;}
if(!notEmpty(cemail)){$('#emailError').html('ΠΛΗΚΤΡΟΛΟΓΙΣΤΕ ΚΑΤΙ'); error=true;}
if(!notEmpty(ctxt)){$('#txtError').html('ΠΛΗΚΤΡΟΛΟΓΙΣΤΕ ΚΑΤΙ'); error=true;}
if(!error){
if(cname.length>20) {$('#nameError').html('ΠΛΗΚΤΡΟΛΟΓΙΣΤΕ ΟΝΟΜΑ ΜΙΚΡΟΤΕΡΟ ΑΠΟ 20 ΧΑΡΑΚΤΗΡΕΣ'); error=true;}
if(!validateName(cname)) {$('#nameError').html('ΜΗ ΑΠΟΔΕΚΤΟΙ ΧΑΡΑΚΤΗΡΕΣ'); error=true;}
if(cemail.length>50) {$('#emailError').html('ΠΛΗΚΤΡΟΛΟΓΙΣΤΕ EMAIL ΜΙΚΡΟΤΕΡΟ ΑΠΟ 50 ΧΑΡΑΚΤΗΡΕΣ'); error=true;}
if(!validateEmail(cemail)){$('#emailError').html('ΜΗ ΑΠΟΔΕΚΤΟ EMAIL'); error=true;} 
if(ctxt.length>200) {$('#txtError').html('ΠΛΗΚΤΡΟΛΟΓΙΣΤΕ ΣΧΟΛΙΟ ΜΙΚΡΟΤΕΡΟ ΑΠΟ 200 ΧΑΡΑΚΤΗΡΕΣ'); error=true;}
if(!validateComment(ctxt)){$('#txtError').html('ΜΗ ΑΠΟΔΕΚΤΟ ΣΧΟΛΙΟ'); error=true;}  
}
if(!error){
$.post("comment.handler.php",{
			com_name: cname,
			com_email: cemail,
			com_comment: ctxt
	}, function(msg) { 
if(msg==1){
$("#addcommentpanel").slideToggle("slow", function() { });
$("#name").val('');
$("#email").val('');
$("#txt").val('');
$("#nameError").html('');
$("#emailError").html('');
$("#txtError").html('');
$("#comments").load("comments.php",function(){$('html, body').animate({scrollTop:500}, 500);});
}else
{
if(msg=="error") $("#errormsg").html('Χμ!!!!, ΠΑΡΑΚΑΛΩ ΞΑΝΑΔΟΚΙΜΑΣΤΕ ΕΙΣΑΓΩΝΤΑΣ ΣΧΟΛΙΟ ΧΩΡΙΣ UNSAFE CHARACTERS, :-)');
else $("#errormsg").html('ΚΑΤΙ ΠΗΓΕ ΣΤΡΑΒΑ, ΠΑΡΑΚΑΛΩ ΞΑΝΑΔΟΚΙΜΑΣΤΕ');
}
});
}
}
});


//validation functions

validateName=function(string){
if(!string.match(/[^\w \xC0-\xFF]/g)) return true;
else return false;
}

validateEmail=function(string){
if(string.match(/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim)) return true;
else return false;
}

trimComment=function(string){

return string.match(/^\s*|\s*$/gm);

}
validateComment=function(string){
//if(!string.match(/\/\*[\s\S]*?\*\//gm))
 return true;
//else return false;
}

function notEmpty(elem){
	if(elem.length == 0){
		return false;
	}
	return true;
}

function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		return false;
	}
}

function isAlphabet(elem){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function isAlphanumeric(val){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(val.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function lengthRestriction(val, min, max){
	if(val.length >= min && val.length <= max){
		return true;
	}else{
		return false;
	}
}


