﻿function GetCommentCount(id){
//id = object been rated (Item/Post/User).
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType("text/xml");
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert("Cannot create an XMLHTTP instance");
        return false;
    }
    http_request.onreadystatechange = function() { ReturnCommentCount(http_request,id); };
    var rnd = Math.round(Math.random() * 1000000000);
    http_request.open("GET", "/utils2/comments.aspx?i="+id+"&rnd="+rnd, true);
    http_request.send(null);
}
function ReturnCommentCount(http_request,id) {if (http_request.readyState == 4) {if (http_request.status == 200) {document.getElementById("c"+id).innerHTML=http_request.responseText;}}}

function Rating(id,uid,type,rate,logged){
/*
id = object been rated (Item/Post/User).
uid = user that the rated object belong to.
type = is this request a Load of rating bar or a Click on rating bar (L/C).
logged = this is the logged UID.
*/
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType("text/xml");
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert("Cannot create an XMLHTTP instance");
        return false;
    }
    http_request.onreadystatechange = function() { ReturnRating(http_request,id); };
    var rnd = Math.round(Math.random() * 1000000000);
    http_request.open("GET", "/utils2/rating.aspx?i="+id+"&u="+uid+"&t="+type+"&r="+rate+"&l="+logged+"&rnd="+rnd, true);
    http_request.send(null);
}

function ReturnRating(http_request,id) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
			document.getElementById(id+"0").innerHTML = http_request.responseText;
        } else {
            document.getElementById(id+"0").innerHTML = "Thank you for voting! Your vote will show up in few moments.";
        }
    }
}

function RateOver(id){
    CleanStars(id)
    var number = id.substring(id.length-1,id.length);
        for (i=1 ;i<=number;i++){
	        document.getElementById(id.substring(0,id.length-1)+i).src = "/images/sys/icons/star_over.gif";
    }
}

function CleanStars(id){
    document.getElementById(id.substring(0,id.length-1)+1).src = "/images/sys/icons/star.gif";
    document.getElementById(id.substring(0,id.length-1)+2).src = "/images/sys/icons/star.gif";
    document.getElementById(id.substring(0,id.length-1)+3).src = "/images/sys/icons/star.gif";
    document.getElementById(id.substring(0,id.length-1)+4).src = "/images/sys/icons/star.gif";
    document.getElementById(id.substring(0,id.length-1)+5).src = "/images/sys/icons/star.gif";
}

function RateOut(id,calcRating){
    CleanStars(id)
    if(calcRating>=0.1 && calcRating<=1){PaintStar(id,1,0);}
    if(calcRating>1 && calcRating<=1.5){PaintStar(id,1,1);}
    if(calcRating>1.5 && calcRating<=2){PaintStar(id,2,0);}
    if(calcRating>2 && calcRating<=2.5){PaintStar(id,2,1);}
    if(calcRating>2.5 && calcRating<=3){PaintStar(id,3,0);}
    if(calcRating>3 && calcRating<=3.5){PaintStar(id,3,1);}
    if(calcRating>3.5 && calcRating<=4){PaintStar(id,4,0);}
    if(calcRating>4 && calcRating<=4.5){PaintStar(id,4,1);}
    if(calcRating>4.5 && calcRating<=5){PaintStar(id,5,0);}
}

function PaintStar(id,number,half){
    for (i=1 ;i<=number;i++){
		document.getElementById(id.substring(0,id.length-1)+i).src = "/images/sys/icons/star_on.gif";
    }
    if(half==1){
        document.getElementById(id.substring(0,id.length-1)+i).src = "/images/sys/icons/star_half.gif";
    }
}
function unLoginAlert() { 
document.getElementsByTagName("body")[0].removeChild(document.getElementById("loginPrompt"));  
} 

function loginAlert(obj){
if(document.getElementById("loginPrompt")!=null){
    document.getElementsByTagName("body")[0].removeChild(document.getElementById("loginPrompt"));  
}
    var text;
    promptbox = document.createElement('div'); 
    promptbox.setAttribute ('id' , 'loginPrompt');
    document.getElementsByTagName('body')[0].appendChild(promptbox);
    promptbox = eval("document.getElementById('loginPrompt').style");
    promptbox.position = 'absolute';
    var thePos = findPos(obj).split(",");
    if(thePos[0]>175){
        promptbox.left= thePos[0]-175;
    }else{
        promptbox.left= thePos[0];
    }
    promptbox.top = thePos[1];
    text='<TABLE border="0" cellspacing="0" cellpadding="7" bgcolor="#E7EFF2" style="border: 1px solid #797979;">'
    text=text+'<TR><td align="left" style="font-size: 12px; vertical-align:middle;">'
    text=text+'<strong> Oops...In order to rate, first </strong><a href="/RatingLogin/" style="font-size: 12px;">Login</a> <strong>or</strong> <a href="/BecomeItWriter/" style="font-size: 12px;">Sign Up</a>.'
    text=text+'  <a href="#" onclick=unLoginAlert();><img src="/images/sys/buttons/btn_close.gif" border=0 /></a>'
    text=text+'</td></TR></TABLE>'
    document.getElementById('loginPrompt').innerHTML = text;
}
