﻿// JScript File
/***************** Rating Control Functions Start ***************/

var strAvgRate;
var strAlreadyRated = 0;
var strAllowClick = 1;

window.onload = function init()
{
    var objMainRateDiv = document.getElementById("RatingCtrl1_divRateMain");
    strAvgRate = objMainRateDiv.getAttribute("avgrate");
    var objBackDiv = document.getElementById("RatingCtrl1_divRateMain").getElementsByTagName("div")[0];
    objBackDiv.id="rate"+strAvgRate; 
}

function ratemouseover(ratemsg,rate)
{
    var objBackDiv = document.getElementById("RatingCtrl1_divRateMain").getElementsByTagName("div")[0];
    var objMainRateDiv = document.getElementById("RatingCtrl1_divRateMain");
    var objMsgDiv = document.getElementById("RatingCtrl1_divRateMsg");
    if(strAlreadyRated == 0)
    {
        objBackDiv.id  = "rate"+rate;
        objMsgDiv.innerHTML = ratemsg;
    }
    else
    {
        objMsgDiv.innerHTML = objMainRateDiv.getAttribute("alreadyrated");
    }
}

function ratemouseout()
{
    var objBackDiv = document.getElementById("RatingCtrl1_divRateMain").getElementsByTagName("div")[0];
    var objMainRateDiv = document.getElementById("RatingCtrl1_divRateMain");
    var objMsgDiv = document.getElementById("RatingCtrl1_divRateMsg");
    if(strAlreadyRated == 0)
    {
        objBackDiv.id  = "rate"+strAvgRate;
        objMsgDiv.innerHTML = "";
    }
    else
    {
        objMsgDiv.innerHTML = "";
    }
}

function ratingnotallowed()
{
    strAlreadyRated =0;
    var objMainRateDiv = document.getElementById("RatingCtrl1_divRateMain");
    var objMsgDiv = document.getElementById("RatingCtrl1_divRateMsg");
    objMsgDiv.innerHTML = objMainRateDiv.getAttribute("alreadyrated");
}

//Global variable for AJAX use.
var xmlHttp;     
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
//netscape, safari, mozilla behave the same??? 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;

function ratesubmit(rate)
{
    try
    {
         if(strAlreadyRated == 0 && strAllowClick != 0)
         {
            //Disable click so long the response do not come back
            strAllowClick = 0;
            
            var objMainRateDiv = document.getElementById("RatingCtrl1_divRateMain");
            var objMsgDiv = document.getElementById("RatingCtrl1_divRateMsg");
            objMsgDiv.innerHTML = objMainRateDiv.getAttribute("wait");
            
            //Create the XMLHttpRequest object.
            xmlHttp = getXmlHttp();                
            //Call the function to process the response from the server
            //Follow the used syntax to pass any parameter to the onreadystatechange handlar.
            xmlHttp.onreadystatechange = stateChanged;
            
            //Send request to the server
            var requestURL = location.href+"&rate="+rate;  //& is to concat the previous querystring
            xmlHttp.open('GET',requestURL,true);
            xmlHttp.send(null); 
          }
    }
    catch(e)
    {
        //alert("Error : Sending request");        
    }
}

//Function to process the response received
function stateChanged()
{
    try
    {
        if (xmlHttp.readyState==4 ||xmlHttp.readyState=='complete')
        {        
            //Read response from the server and parse
            var strResponse = xmlHttp.responseText;
            var chStartEnd = strResponse.charAt(0);
            var objMainRateDiv = document.getElementById("RatingCtrl1_divRateMain");
            if(chStartEnd == "1")
            {
               var objMsgDiv = document.getElementById("RatingCtrl1_divRateMsg");
               objMsgDiv.innerHTML = objMainRateDiv.getAttribute("success");
               strAvgRate = strResponse.charAt(2); 
               var objBackDiv = document.getElementById("RatingCtrl1_divRateMain").getElementsByTagName("div")[0];
               objBackDiv.id  = "rate"+strAvgRate;
               strAlreadyRated = 1;
            }
            else
            {
                //not inserted
               var objMsgDiv = document.getElementById("RatingCtrl1_divRateMsg");
               objMsgDiv.innerHTML = objMainRateDiv.getAttribute("fail");
               strAlreadyRated = 0; // let him to give rating again
               strAllowClick = 1;
            }
        }        
     }
     catch(e)
     {
        //alert("Error : Processing response");
     }
}

//Creates and returns the XMLHttpRequest object.
function getXmlHttp()
{
    var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

        //Depending on the browser, try to create the xmlHttp object 
        if (is_ie){ 
            //The object to create depends on version of IE 
            //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
            var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';
            //alert("Detected the IE version : "+strObjName);
             
            //Attempt to create the object 
            try{ 
                objXmlHttp = new ActiveXObject(strObjName);                
            } 
            catch(e){ 
            //Object creation errored 
                alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
                return; 
            } 
        } 
        else if (is_opera){ 
            //Opera has some issues with xmlHttp object functionality 
            alert('Opera detected. The page may not behave as expected.'); 
            return; 
        } 
        else{ 
            // Mozilla | Netscape | Safari 
            //alert("Detected Mozilla | Netscape | Safari");
            objXmlHttp = new XMLHttpRequest(); 
        } 
         
        //Return the instantiated object 
        return objXmlHttp;
}
/***************** Rating Control Functions End ***************/

