String.prototype.trim = function ()
{ 
    return this.replace(/^\s+|\s+$/, ''); 
}

var IE_all_cache = new Object();

function IE_getElementById(id)
{
    if (IE_all_cache[id] == null)
    {
        IE_all_cache[id] = document.all[id];
    }

    return IE_all_cache[id];
}

if (document.all)
{
  if (!document.getElementById)
  {
    document.getElementById = IE_getElementById;
  }
}

function waitForDownload()
{
    if (waitingTime <= 0)
    {
        document.getElementById("wait_to_download").style.display = 'none';
        document.getElementById("download_link").style.display = 'inline';
        clearInterval(downloadTimer);
    }
    else
    {
        document.getElementById("remaining_secs").innerHTML = waitingTime;
    }

    waitingTime --;
}

if (window.parent != window)
{
    window.parent.location = window.location;
}


function showRating( total , average ){
	$('#rating_star').rater(null , {maxvalue:10,curvalue:average,enabled:false});
	$('#rating_text').html("(" + msg_rating_total + total + ")" );
	$('#rating_average').css({visibility:'visible'});
	return true;
}

function postRating(frm){
	var rating 	= frm['rating'].value;
	if(rating==0){
		$.prompt(msg_raging_err);
		return false;
	}
	var posturl = "/rating.php";
	//var postStr    = "idname="+ idname +"&rating="+ rating+"&r="+getRand();
	var postStr    ={idname: idname, rating : rating , r : getRand() };

	$.ajax({
            type: "POST",
            url: posturl,
            data: postStr,
            dataType:"json", 
			error : function (XMLHttpRequest, textStatus, errorThrown){
				alert(textStatus);
			},
			success:function( json ){
				if ( json.status!=200 ){
            		$.prompt(json.msg);
            		return false;
				}
				showRating( json.rating[0].c_total , json.rating[0].average );
           }
    });
	return false;
}

function postMsg(frm){
	var userName 	= frm['name'].value;
	var userTitle   = frm['title'].value;
	var comment    	= frm['comment'].value;
	
	if(userName==''){
		$.prompt(errmsg1);
		return false;
	
	}
	if(userTitle==''){
		$.prompt(errmsg1);
		return false;
	}

	if(comment==''){
		$.prompt(errmsg1);
		return false;
	}
	
	var posturl = "/comment.php";
	//var postStr    = "idname="+ idname +"&name="+ userName +"&title="+ userTitle +"&comment="+ comment+"&r="+getRand();
	var postStr    ={idname: idname, name : userName , title : userTitle , comment : comment , r : getRand() };

	$.ajax({
            type: "POST",
            url: posturl,
            data: postStr,
            dataType:"json", 
			error : function (XMLHttpRequest, textStatus, errorThrown){
				alert(textStatus);
			},
			success:function( json ){
				if ( json.status!=200 ){
            		$.prompt(json.msg);
            		return false;
				}
				$('#retmsg').css({display:''});
	     		commentList(1);
           }
    });
	frm.reset();
	return false;
}

function getRand(){
	var now=new Date(); 
	return now.getSeconds();
}

function commentList(page){
	var xmlfile = "/comment_list.php?page="+page+"&idname="+idname+"&r="+getRand()+"&l="+l;

	$.ajax({
		type: "get",
		dataType: "json",
		url: xmlfile,
		success: function(json){
			if ( json.status!=200 ){
            	$.prompt(json.msg);
            	return false;	
            }
			
			var len 	= json.comment.length;
           	if(len < 1 ){
           		return false;
        	}
        	
        	var chtml	= '';
           	for(var i=0; i<len; i++){
				chtml	+= '<div class="'+json.comment[i].sty+'">';
				chtml	+= '<p class="info7">';
				chtml	+= '<img class="ilustracao" src="/images/comment-dot.jpg" alt="arrow"  />';
				chtml	+= '<p><strong style=color:red>'+json.comment[i].title+'</strong><br/>'+msg_comment_by+': '+json.comment[i].name+' (2009/02/26)</p>';
				chtml	+= '</p>';
				chtml	+= '<p class="info8"><img class="ilustracao" src="/images/info.gif" alt="arrow" /><b>'+msg_comment+' :</b> ';
				chtml	+= json.comment[i].comment+'</p>';
				chtml	+= '</div>';
           	}
			$("#comment2").html( chtml );
		}
	})
}
