/**
 * @author jlopez
 */
function validateEmail(email) {
	//return /^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test(email);
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

function ypListComments() {
	jQuery.getJSON(path+"php/yp-comments-ajax.php", {op: 'list', media_id: media_id}, function(json){
		var t = '';
		for (w=0;w<json.length;w++) {
			t += "<div style='' id=comment-"+ json[w].id +">";
			t += "<table width='100%'>";
			t += "    <tr>";
			t += "        <td width='20'><img src='"+path+"img/iconos/comment.png' /></td>";
			t += "        <td style='font-weight: bold;' align='left'>"+json[w].name+"</td>";
			t += "        <td align='right' class='sub-title'>" + json[w].date + ' ' + json[w].time + "</td>";
			t += "    </tr>";
			t += "    <tr>";
			t += "        <td align='left' colspan='3'>" + json[w].comment + "</td>";
			t += "    </tr>";
			t += "</table>";
			t += "</div>";
		}
		jQuery('#comments').html(t);
	});
}
function ypAddComment() {
	var comment = jQuery('#comment').val();
	var name = jQuery('#name').val();
	var email = jQuery('#email').val();
	
	if (name == '') {
		alert("Debe introducir un nombre");
		return false;
	}
	
	if (email == '') {
		alert("Debe introducir un email");
		return false;
	} else {
		if (!validateEmail(email)) {
			alert("Debe introducir un email valido");
			return false;
		}
	}
	email = escape(email);
	
	if (comment == '') {
		alert("Debe introducir un comentario");
		return false;
	}
	
	jQuery.get(path+"php/yp-comments-ajax.php", {op: 'add-comment', comment: comment, media_id: media_id, name: name, email: email }, function(data) {
		ypListComments();
	});
	return false;
}
function ypCountComments() {
	jQuery.get(path+"php/yp-comments-ajax.php", {op: 'count-comments', media_id: media_id }, function(data) {
		var t = '';
		t += "<div id='commentsNumber>";
		t += data;
		t += "</div>";
	});
}
jQuery(document).ready(function(){
	ypListComments();
});

