var parentId="userComments";
var x=0;
var lockComment=false;

function test(parte,id,lang) {
	x=parte;
	if(lockComment) return;
	var name = document.getElementById("name");
	var email = document.getElementById("email");
	var text = document.getElementById("commentText");
	var asc = document.getElementById("antiSpamCode");
	
	var lname = document.getElementById("lname");
	var lemail = document.getElementById("lemail");
	var ltext = document.getElementById("lcommentText");

	var alright = true;
	
	var errorText = "";
	
	if(name.value=="") {
		lname.style.color="#ed2025";
		alright = false;
		errorText +="Va rugam sa introduceti un nume.<br/>";
	} else {
		lname.style.color="#000000";
	}

	apos=email.value.indexOf("@");
	dotpos=email.value.lastIndexOf(".");

	if(email.value=="" || apos<1 || dotpos-apos<2 ) {
		lemail.style.color="#ed2025"; 
		alright = false;
		errorText +="Va rugam sa introduceti o adresa de email valida.<br/>";
	} else {
		lemail.style.color="#000000";
	}
	
	if(text.value=="") {
		ltext.style.color="#ed2025";
		alright = false;
		errorText +="Va rugam sa introduceti comentariul.<br/>";
	} else {
		ltext.style.color="#000000";
	}
	
   var ValidChars = "0123456789";
   var isNumeric = true;
   var Char;
 
   for (i = 0; i < asc.value.length && isNumeric == true; i++) { 
		Char = asc.value.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			isNumeric = false;
		}
	}
	
	if(!isNumeric || asc.value=="") {
		alright = false;
		errorText +="Va rugam sa introduceti un numar in dreptul codului.";
	} else {
		
	}

	
	
	if(alright) {
		comment(i,id,lang);
		document.getElementById("formErrors").innerHTML="";
	} else {
		document.getElementById("formErrors").innerHTML=errorText;
	}
	
}

function comment(x,idarticol,lang) {
	xmlHttp=getXMLobject();
	if(lockComment) return;
	var name = document.getElementById("name");
	var email = document.getElementById("email");
	var text = document.getElementById("commentText");
	var asc = document.getElementById("antiSpamCode");
	trimite(xmlHttp,'comment.php','POST','id='+idarticol+"&name="+name.value+"&email="+email.value+"&text="+text.value+"&asc="+asc.value+"&l="+lang,appendComment);
}

function appendComment() {
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200) {
			var result=parseInt(xmlHttp.responseText);
			var name = document.getElementById("name");
			var email = document.getElementById("email");
			var text = document.getElementById("commentText");
			
			switch(result) {
				case 0:
					lockComment=true;
					
					name.disabled="disabled";
					email.disabled="disabled";
					text.disabled="disabled";
					document.getElementById("formErrors").innerHTML="<p>Comentariul a fost trimis.</p>";
					
					// 								Append Comment
					var parent=document.getElementById(parentId);
					
					var name = document.getElementById("name");
					var email = document.getElementById("email");
					var text = document.getElementById("commentText");
					
					var userCommentDiv = document.createElement("div");
					userCommentDiv.setAttribute("className","userComment");
					userCommentDiv.setAttribute("class","userComment");

					// TOP
					var userCommentTopDiv = document.createElement("div");
					userCommentTopDiv.setAttribute("className","userCommentTop");
					userCommentTopDiv.setAttribute("class","userCommentTop");

					var uctSpan = document.createElement("span");
					
					if(x%2!=0) {
						uctSpan.style.cssFloat="right";
						uctSpan.style.styleFloat="right";
						uctSpan.style.margin="0px 10px 0px 0px";
					} else {
						uctSpan.style.cssFloat="left";
						uctSpan.style.styleFloat="left";
						uctSpan.style.margin="0px 0px 0px 10px";
					}
					
					
					uctSpan.appendChild(document.createTextNode(name.value));
					var img = document.createElement('img');
				    img.setAttribute('src', 'Images/comment_small.jpg');
					uctSpan.appendChild(img);
					
					// END TOP
					
					// TEXT 
					
					var userCommentTextDiv = document.createElement("div");
					userCommentTextDiv.setAttribute("className","userCommentText");
					userCommentTextDiv.setAttribute("class","userCommentText");
					userCommentTextDiv.appendChild(document.createTextNode(text.value));
					
					// END TEXT
					
					// BOTTOM
					
					var userCommentBottomDiv = document.createElement("div");
					userCommentBottomDiv.setAttribute("className","userCommentBottom");
					userCommentBottomDiv.setAttribute("class","userCommentBottom");
					
					var ucbSpan = document.createElement("span");
					
					if(x%2!=0) {
						ucbSpan.style.cssFloat="left";
						ucbSpan.style.styleFloat="left";
						ucbSpan.style.margin="-3px 10px 0px 10px";
					} else {
						ucbSpan.style.cssFloat="right";
						ucbSpan.style.styleFloat="right";
						ucbSpan.style.margin="-3px 10px 0px 10px";
					}
					
					ucbSpan.appendChild(document.createTextNode(Date()));
					
					// END BOTTOM
					
					userCommentTopDiv.appendChild(uctSpan);
					userCommentBottomDiv.appendChild(ucbSpan);
					userCommentDiv.appendChild(userCommentTopDiv);
					userCommentDiv.appendChild(userCommentTextDiv);
					userCommentDiv.appendChild(userCommentBottomDiv);
					
					parent.appendChild(userCommentDiv);
					
					// 								End Append
				break;
				case 1:
					document.getElementById("formErrors").innerHTML="Codul introdus este gresit.";
				break;
				default:
					document.getElementById("formErrors").innerHTML="Momentan serverul nu poate adauga comentarii. Va rugam sa reveniti.";
				break;
			}
		} 
	}
}