// add some HTML content when page loads
function writeTags() {
// today date
date = new Date();
hour = date.getHours();
if(hour < 10) hour = "0"+hour;
minute = date.getMinutes();
if(minute <10) minute = "0"+minute;
day  = date.getDate();
if(day <10) day = "0"+day;
month = date.getMonth() + 1;
if(month <10) month = "0"+month;
yy = date.getYear();
year = (yy < 1000) ? yy + 1900 : yy;


// checks if element with a "commPrev" id exists at all
if (document.getElementById('commPrev')) {
// and adds HTML tags which will carry some content
document.getElementById('commPrev').innerHTML = "<div>  <span id=\"anuntTitle\" style=\"font-weight:bold;\"></span><\/div><div id=\"messagePrev\" style=\"margin-left:10px;\"><\/div><p style=\"margin-left:10px;\">Adaugat de <a href=\"\"><span id=\"namePrev\">&nbsp;<\/span><\/a> , in <span class=\"small_grey\">"+day+"."+month+"."+year+" "+hour+":"+minute+"</span><\/p>";
}//
}
// descrieres Preview Box
function commPrev() {
var komentarBox = document.getElementById('anunturi');
// checks if there is existence of form with an id "anunturi"
if (komentarBox) {
// when "numele" field looses focus
document.getElementById('numele').onblur = function() {
// write in the "namePrev" content of the "numele" field
document.getElementById('namePrev').innerHTML = document.getElementById('numele').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>');
// write uri into the href attribute of a link to "numele's" web site
document.getElementById('commPrev').getElementsByTagName('div')[0].getElementsByTagName('a')[0].setAttribute('href','http://'+document.getElementById('email').value.replace('http://',''));
// write in the "messagePrev" content of the "descriere" field
document.getElementById('messagePrev').innerHTML = '<p>'+document.getElementById('descriere').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>')+'<'+'\/p>';
}
// following are doing the same thing as above, but are triggered by "onkeyup" event
document.getElementById('post').onkeyup = function() {
document.getElementById('anuntTitle').innerHTML = '<p>'+document.getElementById('post').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>')+'<'+'\/p>';
}

document.getElementById('descriere').onkeyup = function() {
document.getElementById('messagePrev').innerHTML = '<p>'+document.getElementById('descriere').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>')+'<'+'\/p>';
}
document.getElementById('numele').onkeyup = function() {
document.getElementById('namePrev').innerHTML = document.getElementById('numele').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>');
}
document.getElementById('email').onkeyup = function() {
document.getElementById('commPrev').getElementsByTagName('div')[0].getElementsByTagName('a')[0].setAttribute('href','http://'+document.getElementById('email').value.replace('http://',''));
}
}
}
window.onload = function() {
writeTags();
commPrev();
}

/*
<div id="commPrev">
<noscript>
<p>Sorry, but preview only works with JavaScript enabled.</p>
</noscript>
</div>
*/
