function validMail(string){var mail=/^.+@.+\..{2,4}$/; return mail.test(string);}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function sendComment(uuid){	$('#odeslat-btn').attr("disabled","disabled");var name = $('#jmeno').val();var surname = $('#prijmeni').val();var comment = $('#komentar').val();	if (jQuery.trim(comment) == ''){$('#comment-empty').show();$('#odeslat-btn').attr("disabled","");return false;}	$.post('/smallapp/excelentpromo/send-comment/', {name:name, surname:surname, comment:comment, uuid:uuid}, sendCommentCallback, 'json');}
function sendCommentCallback(data){switch (data.status){case 'ALL_OK':$('#komentar').val('');	
var ret = '<div class="koment-top"></div><div class="koment-text">'+data.comment+'</div><div class="koment-bottom">'+data.name+' | '+data.date+'</div>';
var html = $('#komentare').html() + ret;$('#komentare').html(html);
$('#odeslat-btn').attr("disabled","");break;case 'ERROR_EMPTY':$('#comment-empty').show();	$('#odeslat-btn').attr("disabled","");break;case 'ERROR_DB':	$('#comment-dberror').show();$('#odeslat-btn').attr("disabled","");break;}}
function setDefault(pid, uuid){$.post('/smallapp/excelentpromo/set-default/', {pid:pid, uuid:uuid}, setDefaultCallback, 'json');}
function setDefaultCallback(data){if (data.status == 'ALL_OK'){$('#user_photo_'+data.old).removeClass("default");$('#user_photo_'+data.current).addClass("default");}}
function emailProfile(uuid){
$('#mail-btn').attr("disabled","disabled");
var from = $('#from').val();var fromemail = $('#fromemail').val();var to = $('#to').val();var body = $('#body').val();var errors = new Array();
var recipients = to.split(",");
if(recipients.length>1){for(var key in recipients){if(!validMail(recipients[key])){errors.push("Jedna nebo více adres příjemce má nesprávný formát");break;}}
}else{if(!validMail((trim(to)))){errors.push("Zadaný e-mail příjemce má nesprávný formát");}}
if(trim(from) == ""){errors.push("Musíte uvést své jméno");}
if(trim(fromemail) == ""){errors.push("Musíte uvést svůj e-mail");}
else if(!validMail((trim(fromemail)))){errors.push("Zadaný e-mail má nesprávný formát");}
if(trim(body) == ""){errors.push("Musíte napsat text vzkazu");}
if(errors.length == 0){$.post('/smallapp/excelentpromo/send-profile/', {from:from, fromemail:fromemail, to:to, body:body, uuid:uuid}, sendProfileCallback, 'json');} 	else {$('#messages').html(errors.join("<br />"));$('#mail-btn').attr("disabled","");}}
function sendProfileCallback(data){switch(data.status){	
case 'EMPTY_FROM':$('#messages').html('Musíte uvést své jméno');break;
case 'EMPTY_FROMEMAIL':	$('#messages').html('Musíte uvést svůj e-mail');break;
case 'EMPTY_TO':$('#messages').html('Jedna nebo více adres příjemce má nesprávný formát');break;
case 'EMPTY_BODY':$('#messages').html('Musíte napsat text vzkazu');	break;
case 'NO_PROFILE':$('#messages').html('Profil neexistuje');	break;
case 'ERROR_SEND':$('#messages').html('Chyba při odesílání');break;
case 'ALL_OK':$('#messages').html('');$('#mail-btn').attr("disabled","");$.modal.close();break;}}
function showNext(uuid){$.get('/smallapp/excelentpromo/get-next/', {uuid:uuid}, showNextCallback)}
function showNextCallback(data){$('#karta').html(data)}
function editComment(id){var comment = $("#koment_"+id).html();$("#koment_"+id).html('<textarea id="ta_'+id+'" cols="" rows="" style="width:100%">'+comment+'</textarea><br /><button onclick="saveComment('+id+'); return false;">Save</button> <button onclick="cancelComment('+id+'); return false;">Cancel</button>');}
function cancelComment(id){$("#koment_"+id).html($("#ta_"+id).val());}
function saveComment(id){var c = $("#ta_"+id).val();$.post('/smallapp/excelentpromo/save-comment/', {id:id, c:c}, saveCommentCallback, 'json')}
function saveCommentCallback(data){if (data.status == 'ALL_OK'){$("#koment_"+data.id).html(data.c);} else {alert('Error while saving comment');}}
function deleteComment(id){$.post('/smallapp/excelentpromo/delete-comment/', {id:id}, deleteCommentCallback, 'json');}
function deleteCommentCallback(data){if (data.status == 'ALL_OK'){$("#koment_"+data.id).remove();$("#koment_t"+data.id).remove();$("#koment_b"+data.id).remove();} else {alert('Error while deleting comment');}}
function deletePhoto(id){$.post('/smallapp/excelentpromo/delete-photo/', {id:id}, deletePhotoCallback, 'json');}
function deletePhotoCallback(data){if (data.status == "ALL_OK"){$("#user_photo_"+data.id).remove();} else {alert('Při odstranění fotky došlo ke chybě');}}
function adminDeletePhoto(id, puid){$.post('/smallapp/excelentpromo/admin-delete-photo/', {id:id, uuid:puid}, adminDeletePhotoCallback, 'json');}
function adminDeletePhotoCallback(data){if (data.status == "ALL_OK"){/*$("#user_photo_"+data.id).remove();*/ alert('Foto bylo odstraněno.');} else {alert('Při odstranění fotky došlo ke chybě');}}
function adminDeleteProfile(uuid){$.post('/smallapp/excelentpromo/admin-delete-profile/', {uuid:uuid}, adminDeleteProfileCallback, 'json');}
function adminDeleteProfileCallback(data){if (data.status == "ALL_OK"){/*$("#user_photo_"+data.id).remove();*/ alert('Profil byl smazan.');} else {alert('Při odstranění profilu došlo ke chybě');}}
