/*
Page:           glass_rating.js
Created:        Sep'25 2008
Last Mod:       Sep'25 2008
Handles actions and requests for Glass rating
--------------------------------------------------------- 
Created : Thamizharasan.M
--------------------------------------------------------- */

var xmlhttp;
var clubid;
var dayid

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function commentsUpRating(BASE_URL,clubid,dayid,commentid,memberid) {
	//var theDiv = document.getElementById('up'+clubid+'_'+dayid+'_'+commentid);	
	if(memberid != ''){
		//alert('ajax_includes/ajax_comments_rating.php?clubid='+clubid+'&dayid='+dayid+'&commentid='+commentid+'&rating=UP');
		xmlhttp.open('get', BASE_URL+'ajax_includes/ajax_comments_rating.php?clubid='+clubid+'&dayid='+dayid+'&commentid='+commentid+'&rating=UP');
		//xmlhttp.onreadystatechange = handleResponse(clubid,dayid,commentid) ;
		xmlhttp.onreadystatechange = commenthandleResponse;
		xmlhttp.send(null);	
	}else{
		document.getElementById('loginMsg').style.display='block';	
	}
}

function commentsDownRating(BASE_URL,clubid,dayid,commentid,memberid) {
	//alert('up'+clubid+'_'+dayid+'_'+commentid);
	//var theDiv = document.getElementById('down'+clubid+'_'+dayid+'_'+commentid);	
	// switch UL with a loading div
	if(memberid != ''){
	//theDiv.innerHTML = '<div class="loading"></div>';
	//alert('ajax_includes/ajax_comments_rating.php?clubid='+clubid+'&dayid='+dayid+'&commentid='+commentid+'&rating=DOWN');
    xmlhttp.open('get', BASE_URL+'ajax_includes/ajax_comments_rating.php?clubid='+clubid+'&dayid='+dayid+'&commentid='+commentid+'&rating=DOWN');
    //xmlhttp.onreadystatechange = handleResponse(clubid,dayid,commentid) ;
	xmlhttp.onreadystatechange = commenthandleResponse;
    xmlhttp.send(null);
	}else{
		document.getElementById('loginMsg').style.display='block';	
	}
}

/*function musicMainRoomRating(club_id,day_id,musicid,room) {
	if(musicid != "0"){
		clubid = club_id;
		dayid = day_id;
		
		//alert("music_"+room+"_div_"+clubid+'_'+dayid);
		//var theDiv = document.getElementById("music_"+room+"_div_"+clubid+'_'+dayid);	
		// switch UL with a loading div
		//theDiv.innerHTML = '<div class="loading"></div>';
		//alert('ajax_includes/ajax_music_rating.php?clubid='+clubid+'&dayid='+dayid+'&musicid='+musicid+'&room='+room);
		xmlhttp.open('get', 'ajax_includes/ajax_music_rating.php?clubid='+clubid+'&dayid='+dayid+'&musicid='+musicid+'&room='+room);
		//xmlhttp.onreadystatechange = handleResponse(clubid,dayid,commentid) ;
		xmlhttp.onreadystatechange = mainRoomResponse;
		xmlhttp.send(null);
	}
}

//function handleResponse(clubid,dayid,commentid) {
function mainRoomResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
        var response = xmlhttp.responseText;
		//alert(response);
		//alert('up'+clubid+dayid+commentid);
		/*if(response.indexOf('|') != -1) {
            update = response.split('|');
            alert(update[0]+' | '+update[1]);
			document.getElementById(update[0]).innerHTML = update[1]+'%';
		}
		if(response != "voted"){
			if(response.indexOf('$') != -1) {
				musics = response.split('$');
				//alert(musics);        
				for(i=1; i<	musics.length; ++i){				
					if(response.indexOf('|') != -1) {
						update = musics[i].split('|');
						//alert(update[0]+' | '+update[1]);        
						document.getElementById(update[0]).innerHTML = update[1]+'%';
					}
				}
			}
		}else{
			alert('Already voted');	
		}
		//alert(clubid)
		//alert(document.getElementById('selmusic_main_room_'+clubid+'_'+dayid).selectedIndex);
		//return response;
		document.getElementById('selmusic_main_room_'+clubid+'_'+dayid).selectedIndex = 0;
		document.getElementById('selmusic_other_room_'+clubid+'_'+dayid).selectedIndex = 0;
		}
    }
}*/


function commenthandleResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var responsetext = xmlhttp.responseText;
			
			response		=	responsetext.split('#');
			if(response[0] == "login"){
				document.getElementById('loginMsg').style.display='block';
			}else if(response[0] != "voted"){
				
				var update = new Array();
				if(response[0].indexOf('|') != -1) {
					update = response[0].split('|');
					commentChangeText(update[0], update[1]);
					var	start  = update[0].indexOf('p');
					if(update[0].indexOf('p') != -1){
						var	start  = update[0].indexOf('p');	
					}else{
						var	start  = update[0].indexOf('n');	
					}
					var	lenght = update[0].length;
					var id_day = update[0].substr((start+1),lenght);
					var up	   = 'up'+id_day;
					var down   = 'down'+id_day; 
					document.getElementById(up).onclick = null;
					document.getElementById(down).onclick = null;
				}
			}else{
				//alert('Already voted');
				if(response[1].indexOf('|') != -1) {
					update = response[1].split('|');
					commentChangeText(update[0], update[1]);
				}
				
			}
		}
    }
}

function commentChangeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
	
}

