
var myid =0;
var uniqid =0;
var currentlyrated = false;
  
var BO = new detectBrowser();  

	var starImages = new Array("images/rating/0stars.png",
	"images/rating/1stars.png",
	"images/rating/2stars.png",
	"images/rating/3stars.png",
	"images/rating/4stars.png",
	"images/rating/5stars.png");
	var nullStarMessage = "images/rating/0.gif"
	var starMap = new Array('0,0,22,20',
	'23,0,36,20',
	'37,0,50,20',
	'51,0,64,20',
	'65,0,78,20',
	'79,0,101,20');
	var starMessages = new Array("images/rating/0.gif",
	"images/rating/1.gif",
	"images/rating/2.gif",
	"images/rating/3.gif",
	"images/rating/4.gif",
	"images/rating/5.gif",
	"images/rating/saved.gif");

	var savedRatings = new Array();
	var changedRatings = new Array();
	var starTwinkler = new Array();
	var msgTwinkler = new Array();
	var isRatingsBarChanged = false;
	var delayTime = 700;
	var allImages = new Array();
function preloadImages()
{
	for (i=0; i < preloadImages.length ;i++)
	{
	allImages[i] = new Image();
	allImages[i].src = preloadImages.arguments[i];
	}
}
preloadImages(starImages);
preloadImages(starMessages);

function SaveRatingAjax(id,usrid, ratingType, ratingValue)
{
	rating = ratingValue;
	var submitURL = id + "," + ratingValue + "," + usrid; 
	
	myid = id;

	isRatingsBarChanged = true;
	currentlyrated = true;
	wsJudgeRating.Rate(submitURL, onWsRate);           
	
}
function onWsRate(results)
{    	
    if(BO["safari"])
    {
        window.location.reload( true );
      
    }
    else
    {  
	   document.getElementById("ltlstars_"+ myid).innerHTML  = ReturnStarHTML(myid,results.userid,results.myrate);
	 }
	currentlyrated = false;
}

function SwapStars(id, rating)
{
	if (rating == undefined)
	{
		rating = 0;
	}
	document.images["stars." + id ].src = starImages[rating];
}
function SwapStarMsg(id, rating)
{

	if (rating == undefined)
	{
		if ( changedRatings[id] ) 
		{
		
			document.images["messages." + id  ].src = starMessages[6];
		} 
		else 
		{
	
			document.images["messages." + id  ].src = nullStarMessage;
		}
	} 
	else 
	{
		document.images["messages." + id ].src = starMessages[rating];
	}
}

function SaveStarsAjax(id,usrid, rating)
{
    
    if ((rating==1)||(rating==2)||(rating==3)||(rating==4)||(rating==5))
    {
        savedRatings[id] = rating;
        changedRatings[id] = 1;
	   
        SaveRatingAjax(id,usrid, 'onetofive', rating);	  
    }
    else
    {
        alert("Rating Value out of the bound, Values can only be 1/2/3/4/5. Current rating value: " + rating);
    }
}

function StarMouseOver(id, rating)
{
//alert("StarMouseOver:" + id + ":" + rating);

	if (starTwinkler[id] != 0)
	{
	window.clearTimeout(starTwinkler[id]);
	starTwinkler[id] = 0;
	}
	if (msgTwinkler[id] != 0)
	{
	window.clearTimeout(msgTwinkler[id]);
	msgTwinkler[id] = 0;
	}
  SwapStars(id, rating);
  SwapStarMsg(id, rating); 
}
function StarMouseOut(id,rating)
{
//alert("StarMouseOut:" + rating);
    if(!currentlyrated)
    {
	    starTwinkler[id] = window.setTimeout("SwapStars('"+id+"','" + rating + "')", delayTime);
	    msgTwinkler[id] = window.setTimeout("SwapStarMsg('"+id+"','" + rating + "')", delayTime);
	}
}
function DisplayStarsAjax (id, usrid, rating)
{
//alert("DisplayStarsAjax:" + id);

	var starID = "stars." + id;
	starTwinkler[id] = 0;
	msgTwinkler[id] = 0;
	document.write("<map name='starmap" + id +"'>");
	var i = 0;
	for (i = 1; i < 6; i++) 
	{
	document.write("<area shape=rect " + 
	"coords='" + starMap[i] + "' " +
	"onMouseOver=\"StarMouseOver('" + id + "'," + i + ");\" " +
	"onMouseOut=\"StarMouseOut('" + id + "','" + rating + "');\" " +
	"onClick=\"SaveStarsAjax('" + id + "','" + usrid + "',"+ i + ");" +
	"\" >");
	}
	
	document.write("</map>");
	document.write("<img vspace=2 title = 'Rate This' src='" + starImages[rating] + "'");
	document.write(" border='0' usemap='#starmap" + id);
	document.write("' id='" + starID + "'>");
}
function DisplayMsgAjax (id, rating)
{
//alert("DisplayMsgAjax:" + id);
	var msgID = "messages." + id;
	if ( rating == undefined ) 
	{
		document.write("<img vspace=2 height=11 src='" + nullStarMessage + "'");
	}
	else 
	{
	document.write("<img vspace=2 height=11 src='" + starMessages[rating] + "'"); 
	}
	document.write("' id='" + msgID + "'>");
}
function setInnerHTML( element, html, count ) 
{    
    element.innerHTML = html;     
    if( ! count )         
        count = 1;    
    if( html != '' && element.innerHTML == '' && count < 5 ) 
    {         
        ++count;         
        setTimeout( function() { setInnerHTML( element, html, count );     }, 50 );     
    } 
 } 
function ReturnStarHTML(id,userid,rating)
{
var starID = "stars." + id;
    var s;
	starTwinkler[id] = 0;
	msgTwinkler[id] = 0;
	s = "<map name='starmap" + id +"'>";
	var i = 0;
	for (i = 1; i < 6; i++) 
	{
	    s = s + "<area shape=rect " + 
	    "coords='" + starMap[i] + "' " +
	    "onMouseOver=\"StarMouseOver('" + id + "'," + i + ");\" " +
	    "onMouseOut=\"StarMouseOut('" + id + "','" + rating + "');\" " +
	    "onClick=\"SaveStarsAjax('" + id + "','" + userid + "',"+ i + ");" +
	    "\" >";
	}	
	s = s + "</map>";
	s = s + "<img vspace=2 title = 'Rate This' src='" + starImages[rating] + "'";
	s = s + " border='0' usemap='#starmap" + id;
	return s + "' id='" + starID + "'>";
}

function toggleLayer( whichLayer )
{  var elem, vis;  
    if( document.getElementById ) // this is the way the standards work    
        elem = document.getElementById( whichLayer );  
    else if( document.all ) // this is the way old msie versions work      
        elem = document.all[whichLayer];  
    else if( document.layers ) // this is the way nn4 works    
        elem = document.layers[whichLayer];  vis = elem.style;  
        
        // if the style.display value is blank we try to figure it out here  
    if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)    
        vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';  
    
    vis.display = (vis.display==''||vis.display=='block')?'none':'block';
    
}
function UpdateComments(commentsid)
{

    var strComments = document.getElementById("Comments_" + commentsid).value;   
  
	var submitURL = strComments+ "|" + commentsid; 
	
	uniqid = commentsid;
	
	wsJudgeRating.Review(submitURL, onWsReview); 

}

function onWsReview(results)
{  
	document.getElementById("ltlComment_"+ uniqid).innerHTML  =  results.replace(/\n/g,"<br />");   
	toggleLayer("commentform_" + uniqid);
}
function detectBrowser() {   
    var BO = new Object();   
    BO["ie"]        = false /*@cc_on || true @*/;   
    BO["ie4"]       = BO["ie"] && (document.getElementById == null);   
    BO["ie5"]       = BO["ie"] && (document.namespaces == null) && (!BO["ie4"]);   
    BO["ie6"]       = BO["ie"] && (document.implementation != null) && (document.implementation.hasFeature != null);   
    BO["ie55"]      = BO["ie"] && (document.namespaces != null) && (!BO["ie6"]);   
    /*@cc_on  
    BO["ie7"]       = @_jscript_version == '5.7';  
    @*/  
    BO["ns4"]       = !BO["ie"] &&  (document.layers != null) &&  (window.confirm != null) && (document.createElement == null);   
    BO["opera"]     = (self.opera != null);   
    BO["gecko"]     = (document.getBoxObjectFor != null);   
    BO["khtml"]     = (navigator.vendor == "KDE");   
    BO["konq"]      = ((navigator.vendor == 'KDE') || (document.childNodes) && (!document.all) && (!navigator.taintEnabled));   
    BO["safari"]    = (document.childNodes) && (!document.all) && (!navigator.taintEnabled) && (!navigator.accentColorName);   
    BO["safari1.2"] = (parseInt(0).toFixed == null) && (BO["safari"] && (window.XMLHttpRequest != null));   
    BO["safari2.0"] = (parseInt(0).toFixed != null) && BO["safari"] && !BO["safari1.2"];   
    BO["safari1.1"] = BO["safari"] && !BO["safari1.2"] && !BO["safari2.0"];   
    return BO;   
}   
  
