﻿var commentXmlHttp;
var rateXmlHttp;
var NUMBER_OF_STARS = 5;

function init_rating() {
  var ratings = document.getElementsByTagName('div');
  for (var i = 0; i < ratings.length; i++) {
    if (ratings[i].className != 'rating')
      continue;
      
    var rating = ratings[i].firstChild.nodeValue;
    ratings[i].removeChild(ratings[i].firstChild);
    if (rating > NUMBER_OF_STARS || rating < 0)
      continue;
    for (var j = 0; j < NUMBER_OF_STARS; j++) {
      var star = document.createElement('img');
			if (rating == 0) {
        star.setAttribute('src', '/images/stars/rating_off.gif');
        star.className = 'off';
			} else if (rating > 0 && rating <= 0.25) {
        star.setAttribute('src', '/images/stars/rating_half.gif');
        star.className = 'half';
        rating = 0;
			} else if (rating > 0.25 && rating <= 0.50) {
        star.setAttribute('src', '/images/stars/rating_half.gif');
        star.className = 'half';
        rating = 0;
			} else if (rating > 0.50 && rating < 1) {
        star.setAttribute('src', '/images/stars/rating_half.gif');
        star.className = 'half';
        rating = 0;
			} else if (rating >= 1) {
        star.setAttribute('src', '/images/stars/rating_on.gif');
        star.className = 'on';
        rating--;
			}
      /*if (rating >= 1) {
        star.setAttribute('src', '/images/stars/rating_on.gif');
        star.className = 'on';
        rating--;
      }
      else if(rating == 0.5) {
        star.setAttribute('src', '/images/stars/rating_half.gif');
        star.className = 'half';
        rating = 0;
      }
      else {
        star.setAttribute('src', '/images/stars/rating_off.gif');
        star.className = 'off';
      }*/
      var widgetId = ratings[i].getAttribute('id').substr(7);
      star.setAttribute('id', 'star_'+widgetId+'_'+j);
      star.onmouseover = new Function("evt", "displayHover('"+widgetId+"', "+j+");");
      star.onmouseout = new Function("evt", "displayNormal('"+widgetId+"', "+j+");");
      ratings[i].appendChild(star);
    } 
  }
}

function displayHover(ratingId, star) {
  for (var i = 0; i <= star; i++) {
    document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', '/images/stars/rating_over.gif');
  }
}

function displayNormal(ratingId, star) {
  for (var i = 0; i <= star; i++) {
    var status = document.getElementById('star_'+ratingId+'_'+i).className;
    document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', '/images/stars/rating_'+status+'.gif');
  }
}

function comment(article,section,url)
{
	if (!document.getElementById('comment_text').value) {
		alert('ورود متن پیام اجباری است');
		return;
	}
	
	if (!document.getElementById('comment_captcha').value) {
		alert('ورود کد امنیتی اجباری است');
		return;
	}
	
	var title = document.getElementById('comment_title').value;
	var text = document.getElementById('comment_text').value;
	var captcha = document.getElementById('comment_captcha').value;
	var parameters = "action=comment&article="+article+"&section="+section+"&title="+title+"&text="+text+"&captcha="+captcha;
	commentXmlHttp = getXmlHttpObject();
	
	document.getElementById('commentDiv').style.cursor="wait";
	commentXmlHttp.onreadystatechange=function () {
		if (commentXmlHttp.readyState == 4 || commentXmlHttp.readyState == "complete") {
			document.getElementById('comment_result').innerHTML = commentXmlHttp.responseText;
			if (commentXmlHttp.responseText =='<span class="successfulStyle">پیام شما ثبت گردید.</span>') {
				document.getElementById('comment_title').value = '';
				document.getElementById('comment_text').value = '';
				document.getElementById('comment_captcha').value = '';
			}
			document.getElementById('commentDiv').style.cursor="default";
		}
	};
	document.getElementById('comment_result').innerHTML = '';	
	commentXmlHttp.open("POST",url,true);
	commentXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	commentXmlHttp.setRequestHeader("Content-length", parameters.length);
	commentXmlHttp.setRequestHeader("Connection", "close");
	commentXmlHttp.send(parameters);
	
}

function rate()
{
	if (rateXmlHttp.readyState == 4 || rateXmlHttp.readyState == "complete")
	{
	}
}

function getXmlHttpObject()
{
	var commentXmlHttp=null;
	try {
		commentXmlHttp=new XMLHttpRequest();
	} catch (e) {
		try {
			commentXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			commentXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return commentXmlHttp;
}