function submitComment(){

var send='yes';

for(var i=0;i<document.comment.identity.length;i++)
	if(document.comment.identity[i].checked == true)
		identity=document.comment.identity[i].value;	

	if(document.comment.comment.value == ''){
		alert(commentError1);
		document.comment.comment.focus();
		send='no';		
	} else if(identity == 'other'){
		if(document.comment.otherName.value == ''){
			alert(commentError2);
			document.comment.otherName.focus();
			send='no';
		} else if(document.comment.otherEmail.value == ''){
			alert(commentError3);
			document.comment.otherEmail.focus();
			send='no';
		} else if(!validEmail(document.comment.otherEmail.value)){
			alert(commentError4);
			document.comment.otherEmail.focus();
			send='no';
		}
		document.comment.meuser.value=document.comment.otherName.value;

	} else if(identity == 'anonymous'){
			document.comment.meuser.value='anonymousUser';
	}

	if(send == 'yes'){
		AJAXGet('/imageComment.php?identity='+identity+'&webpage='+document.comment.webpage.value+'&otherEmail='+document.comment.otherEmail.value+'&meuser='+document.comment.meuser.value+'&comment='+document.comment.comment.value+'&imageID='+_("imageID").value+'&nm='+ranNum,submitCommentHandle,true)
	}
}



function validEmail(address){
	at=address.indexOf("@");
	dot=address.indexOf(".");
	if (at == -1 || dot == -1) {
	    return 0;
	} else {
		return 1;
	}
}

function submitCommentHandle(){

			document.getElementById('commentDiv').innerHTML = "تم إضافة التعليق";  
			/*document.comment.comment.value="";
			document.comment.otherName.value="";
			document.comment.otherEmail.value="";
			document.comment.webpage.value="";*/
			
			getComments();
}

function showIdentityData(id)
        {
                if(id == 'other' )
                {
	            	document.getElementById('choose_other').style.display = 'block';
                }
                else if(id == 'anonymous')
                {
                    document.getElementById('choose_other').style.display = 'none';
                } 
                else if(id == 'logged')
                {
                    document.getElementById('choose_other').style.display = 'none';
                }
 
        }

        
        
function getComments(){
	ranNum= Math.round(Math.random()*10000000);
	
	http.open('get', '/getCommentsAjax.php?imageID='+_("imageID").value+'&meuser='+_("userName").value+'&nm='+ranNum);
	http.onreadystatechange = getCommentsHandle;
	http.send(null);
}

function getCommentsHandle(){
	if(http.readyState == 4){
		if (http.status == 200){
			var showCommentDiv="";
			response=http.responseXML;

			
			
		    if(response.getElementsByTagName('Comment')[0].getElementsByTagName('commentCount')[0].childNodes[0].nodeValue != 0)
            	var countComments = response.getElementsByTagName("Comment").length;
            else
            	var countComments = 0;			

	if(countComments == 0)
  	{
  	document.getElementById('showCommentsDiv1').innerHTML = "<b>"+noComment+"</b><br /><br />";
  	}   else {   			
			
  	
for(var i=0;i<countComments;i++)	{

	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('realname')[0].childNodes[0] != null)
		var realname = response.getElementsByTagName('Comment')[i].getElementsByTagName('realname')[0].childNodes[0].nodeValue;

	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('commentID')[0].childNodes[0] != null)
		var commentID = response.getElementsByTagName('Comment')[i].getElementsByTagName('commentID')[0].childNodes[0].nodeValue;
             	
	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('sender')[0].childNodes[0] != null)
		var sender = response.getElementsByTagName('Comment')[i].getElementsByTagName('sender')[0].childNodes[0].nodeValue;

	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('identity')[0].childNodes[0] != null)
		var identity = response.getElementsByTagName('Comment')[i].getElementsByTagName('identity')[0].childNodes[0].nodeValue;
		
	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('content')[0].childNodes[0] != null)
		var content = response.getElementsByTagName('Comment')[i].getElementsByTagName('content')[0].childNodes[0].nodeValue;	

	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('image')[0].childNodes[0] != null)
		var image = response.getElementsByTagName('Comment')[i].getElementsByTagName('image')[0].childNodes[0].nodeValue;	
            	
	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('url')[0].childNodes[0] != null)
		var url = response.getElementsByTagName('Comment')[i].getElementsByTagName('url')[0].childNodes[0].nodeValue;	

	if(response.getElementsByTagName('Comment')[i].getElementsByTagName('email')[0].childNodes[0] != null)
		var email = response.getElementsByTagName('Comment')[i].getElementsByTagName('email')[0].childNodes[0].nodeValue;	
          
            	
				showCommentDiv = showCommentDiv + '<ul class="nobul"><li><div class="pad">';
				showCommentDiv = showCommentDiv + '<img src="'+image+'" border="0" alt="" align="'+commentsAlign+'" class="margpic bord" width="45" height="45" />';

				if (identity == 'logged'){
					showCommentDiv = showCommentDiv + '&nbsp;<a href="/User/'+sender+'/New">'+realname+'</a>';
				} else if (identity == 'other'){
					if(url == "none" || url =="http://")
						showCommentDiv = showCommentDiv + '&nbsp;' + sender + ' <span dir="rtl">('+email+')</span>';
					else
						showCommentDiv = showCommentDiv + '&nbsp; <a href="'+url+'" target="_blank">'+sender+'</a> <span dir="rtl">('+email+')</span>';
				} else if (identity == 'anonymous'){
						showCommentDiv = showCommentDiv + '&nbsp;' + commentAnynous;
				}

				showCommentDiv = showCommentDiv + '</div>';
				showCommentDiv = showCommentDiv + '<div class="padr">&nbsp;'+content+'</div>';
				if(allowDeleteComments == 'yes')
					showCommentDiv = showCommentDiv + '&nbsp;<a href="javascript:deleteComment('+commentID+')">'+commentDelete+'</a>';
				showCommentDiv = showCommentDiv + '<br />';
				showCommentDiv = showCommentDiv + '</li>';

				showCommentDiv = showCommentDiv + '</ul>';
				showCommentDiv = showCommentDiv + '<br />';
				showCommentDiv = showCommentDiv + '<br />';
	}
		
	document.getElementById('showCommentsDiv1').innerHTML = showCommentDiv;
	
				}
		}
	}
}   
        

function deleteComment(commentID){
	if(confirm(commentDeleteConfirm) == true){
		ranNum= Math.round(Math.random()*10000000);
		
		http.open('get', '/deleteCommentsAjax.php?commentID='+commentID+'&meuser='+_("userName").value+'&nm='+ranNum);
		http.onreadystatechange = deleteCommentHandle;
		http.send(null);
	}
}

function deleteCommentHandle(){
	
	if(http.readyState == 4){
		if (http.status == 200){
			getComments();
		}
	}
}  


function checkSet(){
getComments();
showNew();

}

function addToCart(){
	if(!document.sortak.picture.checked && !document.sortak.mug.checked){
		alert("الرجاء إختيار كيفية الطباعة");
	} else {
		document.sortak.submit();
	}
}