
var xmlHttp = createXmlHttpRequest();
var obj = '';

function createXmlHttpRequest() {
	var xmlHttp = false;
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlHttp = new XMLHttpRequest();
	}
	if (!xmlHttp) {
		alert("Ops sorry We found some error!!");
	}
	return xmlHttp;
}

/* function for send data thrugh post method */

function postAjax(source, values, respons, hanres) {
	
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
	
	obj = respons;
	
	xmlHttp.open("POST", source, true);
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", values.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = hanres;
	xmlHttp.send(values);
  } else {
	setTimeout('postAjax(source, values, respons, hanres)', 100000);
  }
}

/* functions for send and hendle respons for add new comment */

function postContent(){
	/* Query value that send to php.*/
	var commentValue = 'name=' + encodeURI( document.getElementById('name').value ) + '&YourComment=' + encodeURI( document.getElementById('YourComment').value );
	/*server side */
	var send_to = 'manage-comment.php';
	/*Div id for handle preloader image or errors.*/
	var respons = 'alert';
	postAjax(send_to, commentValue, respons, handleResponComment);
}

function handleResponComment(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			/*I more prefer use json as response value from php*/
			var JSONRespons = eval('(' + xmlHttp.responseText + ')');
			if(JSONRespons.status == 1){
				/*
				* if inserting new commend succeed, then we call commentResponse function to show the new comment.
				*/
				commentResponse(JSONRespons);
			}
			else{
				/*when new comment appeared, we have to re-enabel the form by calling enableForm() function using onload image event*/
				document.getElementById(obj).innerHTML = JSONRespons.message + '<img src="ajax-loading.gif" width="0" height="0" onload="enableForm();">';
			}
		} else {
			/*Incase we found errors on trancaction proccess.*/
			document.getElementById(obj).innerHTML = 'Error: ' + xmlHttp.statusText;
		}
	}
	else{
		/*
		* After submit new comment, we heve to diasable the form to prevent from re-submitng by user.
		* Also, show the preloader image, so user know his comment is being proceed.
		*/
		document.getElementById(obj).innerHTML = '<img src="ajax-loading.gif">';
		document.getElementById('name').disabled=true;
		document.getElementById('YourComment').disabled=true;
		document.getElementById('submit').disabled=true;
	} 
}


function commentResponse(JSONRespons){
	/*get listed comments*/
	var current_contents = document.getElementById('CommentList').innerHTML;
	/*Listed comments plus new comment that submited by user and inserted to database. */
	var newComment = current_contents + '<div class="Comment" id="' + JSONRespons.message_id + '"><div class="Remove"><a href="javascript:deleteContent(' + JSONRespons.message_id + ');">Remove</a></div><div class="SenderName"><img src="ajax-loading.gif" width="0" height="0" onload="enableForm();">' + JSONRespons.name + '</div><div class="CommentDate">' + JSONRespons.date + '</div><div class="CommentContent">' + JSONRespons.comment + '</div></div>'; 
	/*get current total comment */
	var currTotalComm = document.getElementById('numComment').innerHTML;
	/*current comment plus one */
	document.getElementById('numComment').innerHTML = parseInt(currTotalComm) + parseInt(1);
	/*show up the new listed comments*/
	document.getElementById('CommentList').innerHTML = newComment;
	/*reset the form*/
	document.getElementById('CommentForm').reset();
	/*remove the preloader image*/
	document.getElementById('alert').innerHTML = '';
}

function enableForm(){
	/*re-enable the form after all process done. */
	document.getElementById('name').disabled=false;
	document.getElementById('YourComment').disabled=false;
	document.getElementById('submit').disabled=false;
}

/* functions for send and hendle respons for delete a comment */

function deleteContent(messageID){
	var postValue = 'id=' + messageID;
	var send_to = 'manage-comment.php';
	var respons = 'alert';
	
	input_box = window.confirm('Are you sure want to delete this comment?');
	if (input_box==true){
		postAjax(send_to, postValue, respons, handleDeletedComment);
	}
}

function handleDeletedComment(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			var JSONRespons = eval('(' + xmlHttp.responseText + ')');
			if(JSONRespons.status == 1){
				deleteNow(JSONRespons.id);
			}
			else{
				document.getElementById(obj).innerHTML = JSONRespons.message;
			}
		} else {
			document.getElementById(obj).innerHTML = 'Error: ' + xmlHttp.statusText;
		}
	}
}

function deleteNow(id){
	var delete_comment = document.getElementById(id);
	var currTotalComm = document.getElementById('numComment').innerHTML;
	document.getElementById('numComment').innerHTML = parseInt(currTotalComm) - parseInt(1);
	delete_comment.parentNode.removeChild(delete_comment);
	enableForm();
	document.getElementById('alert').innerHTML = '';
}

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
else 
	countfield.value = maxlimit - field.value.length;
}

function str_replace(search, replace, subject) {
	var f = search, r = replace, s = subject;
	var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
	
	while (j = 0, i--){
		if(s[i]){
			while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
		}
	};

	return sa ? s : s[0];
}

var xmlHttp
function GetXmlHttpObject(){
	var xmlHttp = null;
	try{
	// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		// Internet Explorer
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function irate(ir,ii){
	xmlHttp = GetXmlHttpObject()
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "exec.php";
	var params = "id="+ii+"&rating="+ir+"&action=image_rating";
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			var res = xmlHttp.responseText;

			document.getElementById('rsul').className='star-rating1';
			if(navigator.appName == 'Microsoft Internet Explorer'){
				document.getElementById('rsli').style.setAttribute('width',res+'%'); // IE
			}else{
				document.getElementById('rsli').setAttribute('style','width:'+res+'%'); // Everyone else
			}
		}
	}
	xmlHttp.send(params);
}

function addfavorite(ii){
	xmlHttp = GetXmlHttpObject()
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "exec.php";
	var params = "id="+ii+"&action=add_favorites";
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			var res = xmlHttp.responseText;
			if(res) alert(res);
			document.getElementById('imglink').className='imglink1';
		}
	}
	xmlHttp.send(params);
}

function report(ii){
	xmlHttp = GetXmlHttpObject()
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "JSexec.php";
	var params = "id="+ii+"&action=report";
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			var res = xmlHttp.responseText;
			if(res) alert(res);
			document.getElementById('imglinkreport').className='imglink1';
		}
	}
	xmlHttp.send(params);
}

function addElement(res){
	var element=res;
	if(element==""){
		document.getElementById('msg').style.display="block";
		document.getElementById('msg').innerHTML = "Error! Insert a description for the element";
	}else{
		var container = document.getElementById('myList');
		var new_element = document.createElement('div');
		new_element.innerHTML = element;
		container.insertBefore(new_element, container.firstChild);
		document.getElementById('comment').value="";
		document.getElementById('remLen').value="76";
	}
}

function commentset(ii){
	xmlHttp = GetXmlHttpObject()
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "exec.php";
	var params = "id="+ii+"&comment="+str_replace("'","\\'",document.getElementById('comment').value)+"&action=commentsub";
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			var resu = xmlHttp.responseText;
				if(resu) addElement(resu);
		}
	}
	xmlHttp.send(params);

	xmlHttp2 = GetXmlHttpObject()
	if(xmlHttp2 == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "exec.php";
	var params = "id="+ii+"&action=commentcnt";
	xmlHttp2.open("POST",url,true);
	xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp2.setRequestHeader("Content-length", params.length);
	xmlHttp2.setRequestHeader("Connection", "close");
	xmlHttp2.onreadystatechange = function(){
		if(xmlHttp2.readyState == 4){
			var res = xmlHttp2.responseText;
			if(res){
				document.getElementById('commentcount').style.display="block";
				document.getElementById('commentcount').innerHTML = '<span class="comment">Comments:</span> ' + res;
			}
		}
	}
	xmlHttp2.send(params);
}

