// rating
function ratingstar_toggle(content_id, score, toggle)
{
	for ( var i = 1; i <= score; i++ )
	{
		var obj = $('ratingstar'+content_id+'__'+i);
		obj.src = ( toggle == 'show' ) ? (obj.src).replace(/rate0/, 'rate1') : (obj.src).replace(/rate1/, 'rate0');
	}
}


function submit_rating(type, member_id, content_id, score)
{
	Ajax.request(virpath+'index.php?m=rating', {'method':'post','parameters':{'type':type,'score':score,'member_id':member_id,'content_id':content_id}, 'onComplete':function(res){
		if ( res && res[0] )
		{
			var response = res[0].split("\n");
			if ( response.length == 2 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('rating_'+type+'_response'+content_id).innerHTML = response[1];
					return true;
				}
			}
		}
	}});
}


// reports
function submit_report(type, member_id, content_id, noreason)
{
	var reason = $('report_'+type+'_reason'+content_id).value;
	if ( !reason ) { alert(noreason); return; }
	Ajax.request(virpath+'index.php?m=report', {'method':'post','parameters':{'type':type,'reason':reason,'member_id':member_id,'content_id':content_id}, 'onComplete':function(res){
		if ( res && res[0] )
		{
			var response = res[0].split("\n");
			if ( response.length == 2 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('report_'+type+'_response'+content_id).innerHTML = response[1];
					return true;
				}
			}
		}
	}});
}


// toggles
function row_toggle(name)
{
    var showRow = (browser.IE) ? "block" : "table-row";

    for (var i = 0;  document.getElementById(name+'_'+i) != null;  i++)
    {
        if (document.getElementById(name+'_'+i).style.display == "none")
            document.getElementById(name+'_'+i).style.display = showRow;
        else
            document.getElementById(name+'_'+i).style.display = "none";
    }
}

function showhide_field(name, show)
{
	var el = document.getElementById(name);
    if (el.style.display == "none"  &&  (show == '' || show == undefined)  ||  show == 1)
    {
        el.style.display = "block";
    }
    else
    {
        el.style.display = "none";
    }
}


//------------------------------------------------
// Misc
//------------------------------------------------
function confirmLink(question, url)
{
    var is_confirmed = confirm(question);

    if (is_confirmed && url != '')
        window.location = url;

    return is_confirmed;
}

function confirmForm(question, form)
{
    var is_confirmed = confirm(question);

    if (is_confirmed && form != '')
        eval('document.' + form + '.submit()');

    return is_confirmed;
}

function toggleMessages(form, check)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        if (form.elements[i].type == 'checkbox')
        {
            form.elements[i].checked = check;
        }
    }
}

function stylizeBooleanBox(prefix, boxid)
{
	var options = document.getElementById(prefix + boxid);
	for ( var i = 0, length = options.length; i < length; i++ )
	{
		options[i].style.background = ( options[i].value == 1 ) ? '#DEFAE0' : '#FBDDDD';
		if ( options[i].value == 1 && options[i].selected )
			document.getElementById(prefix + boxid).style.background = '#DEFAE0';
		else if ( options[i].value == 0 && options[i].selected )
			document.getElementById(prefix + boxid).style.background = '#FBDDDD';
	}
}


//------------------------------------------------
// Chat
//------------------------------------------------
var httpReceiveChat = null;
var httpSendChat = null;
var chatwincurr = 0;
var autocheck = 0;
var autodelay = 8;
var virpath = '';
chatwins = new Array();

function checkChat(path,check,delay)
{
    virpath = path;
    autocheck = check;
    autodelay = delay;
    httpReceiveChat = getHTTPObject();
    httpSendChat = getHTTPObject();
    receiveChatText();
}

function receiveChatText() {
	if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0) {
  	httpReceiveChat.open("GET",virpath+'chat.php?p=check', true);
    httpReceiveChat.onreadystatechange = handlehHttpReceiveChat;
  	httpReceiveChat.send(null);
	}
}

function handlehHttpReceiveChat() {
    if (httpReceiveChat.readyState == 4) {
        memberid = httpReceiveChat.responseText;
        if (memberid != "0") openChatWindow(memberid);
        if (autocheck) setTimeout('receiveChatText();', (autodelay*1000));
    }
}

function openChatWindow(id) {
    if (id > 0 && (typeof(chatwins['chat' + id]) != "object"  ||  chatwins['chat' + id].closed)) {
        chatwins['chat' + id] = window.open(virpath + 'index.php?m=chat&id=' + id, 'chat' + id, 'width=380,height=360,resizable=yes,scrollbars=no,toolbar=no,location=no,status=no,menubar=no');
        if ( chatwins['chat' + id] && chatwins['chat' + id].open)
        	chatwins['chat' + id].focus();
		else
			alert('Could not open a chat window. Please disable your popup blocker.');
    }
}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

