var isplaying ;
var playreset ;
var req;
var isfavorited;

function toggleIsFavorited(sid) {
    alink = document.getElementById('favlink');
    favrow = document.getElementById('myfav');
    
    createXHO();
    if (req == false) {
        document.forms['togfavnoajax'].submit();
        return;
    }
    
    req.open("GET", "http://www.icompositions.com/music/ajaxdata/makefav.php?sid="+sid+"&a="+((isfavorited)?"remove":"add")+"&rand="+Math.floor(Math.random()*999999), false);
    req.send(""); 
    
    isfavorited = !isfavorited;
    
    if (isfavorited) {
        alink.innerHTML = '<img src="/core/images/music-unfavorite.gif" alt="Remove from Favorites" border="0" />';
        favrow.style.display="table-row";
    } else {
        alink.innerHTML = '<img src="/core/images/music-favorite.gif" alt="Add to Favorites" border="0" />';
        favrow.style.display="none";
    }
}

	function formatTime(time) {
		minutes = Math.floor(time/60) ;
		seconds = time - (minutes*60) ;
		if(seconds < 10) { seconds = "0" + seconds } 
		newtime = minutes + ":" + seconds ;
		return newtime ;
	}
	
	function updateTime() {
	    var cst = 750 ;
		if(document.getElementById('showtime').innerHTML != formatTime(Math.round(document.song.GetTime() / cst))) {
    		document.getElementById('showtime').innerHTML = formatTime(Math.round(document.song.GetTime() / cst));	
	       	document.getElementById('showremtime').innerHTML = "-" + formatTime(Math.round(((document.song.GetDuration() - document.song.GetTime()) / cst))) ;		
    	}
    	setTimeout("updateTime()",10)
    }
    
function toggleLyrics() {
	document.getElementById('showLyrics').style.display = 'none' ;
	document.getElementById('lyrics').style.display = 'block' ;
}

function swapContents(box, str1, str2) {
	if (box.innerHTML == str1) box.innerHTML = str2;
	else box.innerHTML = str1;
}

function showAJAXPostedComment() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            if (req.responseText.substring(0,1) == "#") {
                alert("AJAX Error "+req.responseText.substring(1,req.responseText.length - 1));
                return false;
            }
            da = req.responseText.substring(1,req.responseText.length - 1).split("|");
            //alert(req.responseText);
            buildCommentRow(da[0],da[1],da[14],da[4],da[5],da[6],da[13],da[15],da[7],da[8],da[9],da[10],da[11],da[12],'','');
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function postComment(form) {
    comment_sid = form.sid.value;
    comment_body = form.combody.value ;
        while (comment_body.indexOf('&') != -1)
            comment_body = comment_body.replace('&','!!!AMPERSAND!!!');
        while (comment_body.indexOf('+') != -1)
            comment_body = comment_body.replace('+','!!!PLUS!!!');
    comment_rating1 = form.rating1.value;
    comment_rating2 = form.rating2.value;
    comment_rating3 = form.rating3.value;
    comment_rating4 = form.rating4.value;
    last_comment_id = form.last_comment_id.value;
    didrate = ((comment_rating1 + comment_rating2 + comment_rating3 + comment_rating4) != 0);
    
    createXHO();
    if (req == false) {
        return true;
    }
    
    if (!didrate && comment_body == "") {
        alert("Please rate or comment and then resubmit.");
        return false;
    }
    
  
  new Ajax.Updater('newcomments','/music/ajaxdata/postcomment2.php', {method:'post', postBody:"sid="+comment_sid+"&body="+comment_body+"&rating1="+comment_rating1+"&rating2="+comment_rating2 + "&rating3="+comment_rating3 + "&rating4="+comment_rating4+"&last_comment_id="+last_comment_id+"&rand="+Math.floor(Math.random()*999999),evalScripts:true});

    form.rating1.selectedIndex=0;
    form.rating2.selectedIndex=0;
    form.rating3.selectedIndex=0;
    form.rating4.selectedIndex=0;
    if(document.getElementById('ratingselects') && didrate)
        document.getElementById('ratingselects').style.display="none";
    form.sendcomment.disabled = 'disabled';
    document.getElementById('commentspinner').style.display="block";
    
    return false;
}

function createXHO() {
	req = false;
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
}

function showAllDownloads(sid) {
  document.getElementById('downloadbox').innerHTML = '<div align="center"><img src="/core/images/spinner.gif" alt="processing" /></div>' ;
  new Ajax.Updater('downloadbox','/music/ajaxdata/showdownloads.php', {method:'post', postBody:"sid="+sid,evalScripts:true});

}

function showAllFavorites(sid) {
  document.getElementById('favoritesbox').innerHTML = '<div align="center"><img src="/core/images/spinner.gif" alt="processing" /></div>' ;
  new Ajax.Updater('favoritesbox','/music/ajaxdata/showfavorites.php', {method:'post', postBody:"sid="+sid,evalScripts:true});

}