// GDS javascript

// in_array function as in php
function in_array( val , arr )
{
	for(var i = 0, l = arr.length; i < l; i++) {
		if(arr[i] == val) {
			return true;
		}
	}
	return false;
}

// Rate item on given url
function rateIt(item,url)
{      
	el = document.getElementById('gdsratingwork_'+item).innerHTML = '<img src="/stc/img/rating_working.gif" alt="Working" />';
    new ajax (url, {onComplete: rateWrapper });
}

// When mouseover or mouseout
function rateSet(post,stars)
{
	for(var i=1;i<=stars;i++)
	{
		el = document.getElementById('gdsstar_'+post+'_'+i);
		el.src = '/stc/img/rating_star_full.gif';
	}
	for(var i=stars+1;i<=5;i++)
	{	
		el = document.getElementById('gdsstar_'+post+'_'+i);
		el.src = '/stc/img/rating_star_empty.gif';
	}
}

// Wrapper for process
function rateWrapper(response)
{
	// Response params in order, postId, Vote, Votes, Sum
	var rp = response.split(':');		
	var avg = Math.round(rp[3]/rp[2]);
	for(var i=1;i<=avg;i++)
	{
		el = document.getElementById('gdsstar_'+rp[0]+'_'+i);
		el.src = '/stc/img/rating_star_full.gif';
		el.onmouseout = function() {rateSet(rp[0],avg)}
	}
	for(var i=avg+1;i<=5;i++)
	{	
		el = document.getElementById('gdsstar_'+rp[0]+'_'+i);
		el.src = '/stc/img/rating_star_empty.gif';
		el.onmouseout = function() {rateSet(rp[0],avg)}
	}
	el = document.getElementById('gdsratingvotes_'+rp[0]).innerHTML = rp[2];
	el = document.getElementById('gdsratingvote_'+rp[0]).innerHTML = rp[3];
	el = document.getElementById('gdsratingwork_'+rp[0]).innerHTML = '';
}

// Check length of search keys
function checkSrch(form,min_length)
{
   if(form.keys.value.length < min_length)
   {
        alert('Keywords must have length at least '+min_length+' characters');
        return false;    
   }
   return true;
}

// Check all checkboxes in form
function checkAll(fname,check)
{
	var f = document.forms[fname];
	for(var i=0;i<f.length;i++)
	{	
		if(f[i].type == 'checkbox')			
			f[i].checked = check;
	}
	return false;
}

// Check form valu
function checkForm(f,txt)
{
	/* check if something is checked
	var checked = false;
	for(var i=0;i<f.length;i++)
	{	
		if((f[i].type == 'checkbox') && (f[i].checked))	
		{
			alert('hovno');
			checked = true;
			break;
		}			
	}
	if(!checked)
	{	f.act['0'].selected = 1;
		return false;
	}*/		
		
	switch(f.act.value)
	{
		case '0':
			return false;
		case 'seldelthread':
			if(confirm(txt))
				return true;
			else
				f.act['0'].selected = 1;
				return false
			break;				
	}
	return true;
}

// Smiley and codes to textarea
function gdsCode(cnt,val,val2)
{
	// ie support
	if (document.selection)
	{
		cnt.focus();
		sel = document.selection.createRange();
		if (val2)
			sel.text = val+sel.text+val2;
		else
			sel.text = val;
	}
	// mozilla support
	else if (cnt.selectionStart || cnt.selectionStart == '0') 
	{
		var startPos = cnt.selectionStart;
		var endPos = cnt.selectionEnd;

		if (val2)
			cnt.value = cnt.value.substring(0, startPos)+ val + cnt.value.substring(startPos, endPos) + val2 + cnt.value.substring(endPos, cnt.value.length);
		else
			cnt.value = cnt.value.substring(0, startPos)+ val + cnt.value.substring(endPos, cnt.value.length);

	} 
	else 
	{
		if (val2)
			cnt.value += val+val2;
		else
			cnt.value += val;			
	}
	cnt.focus();
}

// hide all inputs for adding file except one
function hide_files(i)
{
	for(i;i<=3;i++)
	{
		var el = document.getElementById('gds_file_'+i);
		el.style.display = "none";
	}
}

// Add file input field
function show_file(val,i)
{
	if(val.length > 1)
	{
		var el = document.getElementById('gds_file_'+i);
		el.style.display = "table-row";
	}
}

// hide file
function hide_file(i)
{
	eval('var inp = document.gds_form.file_'+i);
	inp.style.display = "hide";
}

//Show/Hide top tool actions
function showTools()
{
	el = document.getElementById( 'toollist' );
	if ( el.style.display == "block" ) {
		el.style.display = 'none';
		document.getElementById("menutitle").className="";}
	else {
		el.style.display = 'block';
		document.getElementById("menutitle").className="active";}
}

//Hide top tool actions
function hideTools()
{
	el = document.getElementById( 'toolul' );
	if( el.className == "moveout" ){
		document.getElementById( 'toollist' ).style.display = 'none';
		document.getElementById("menutitle").className="";
	}
}

//Show/Hide bottom tool actions
function showBottomTools()
{
	el = document.getElementById( 'toolbottomlist' );
	if ( el.style.display == "block" ) {
		el.style.display = 'none';
		document.getElementById("menutitlebottom").className="";}
	else {
		el.style.display = 'block';
		document.getElementById("menutitlebottom").className="active";}
}

//Hide bottom tool actions
function hideBottomTools()
{
	el = document.getElementById( 'toolbottomul' );
	if( el.className == "moveoutbottom" ){
		document.getElementById( 'toolbottomlist' ).style.display = 'none';
		document.getElementById("menutitlebottom").className="";
	}
}

function moveonTools( el )
{
	el.className = "moveon";
}

function moveonBottomTools( el )
{
	el.className = "moveonbottom";
}

function moveoutTools( el )
{
	el.className = "moveout";
}

function moveoutBottomTools( el )
{
	el.className = "moveoutbottom";
}

function nohoverTools() {
	el = document.getElementById( 'tools' );
	el.className = '';
}

function nohoverBottomTools() {
	el = document.getElementById( 'toolsBottom' );
	el.className = '';
}

function popup(url, width, height)
{
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	newwin=window.open(url,'_blank', params);
	if (window.focus) {newwin.focus()}
	return false;

}

// Check all checkboxes in form
function checkAll(fname,check)
{
	var f = document.forms[fname];
	for(var i=0;i<f.length;i++)
	{
		if(f[i].type == 'checkbox')
			f[i].checked = check;
	}
	return false;
}

//Work with attachments inputs in post form
function hideAllInputs()
{
	var freeInputs = 0;
	for(i=1;i<=fileVars.cnt;i++)
	{
		var el = document.getElementById('fileInput'+i);
		if( ! in_array( i , fileVars.forceShow ))
		{
			el.style.display = "none";
			freeInputs++;
		}
		else
		{
			document.getElementById('fileDel'+i).style.display = 'inline';
		}
	}

	if( freeInputs == fileVars.cnt )
	{
		document.getElementById('fileAddFirst').style.display = 'table-row';
	}
	else if( freeInputs )
	{
		document.getElementById('fileAdd').style.display = 'table-row';
	}
}

function showInput()
{
	var inputId = false;
	var freeInputs = 0;
	for(i=1;i<=fileVars.cnt;i++)
	{
		if( ! fileVars.states[ i ] )
		{
			freeInputs++;
			if( ! inputId )
				inputId = i;
		}
	}
	fileVars.states[ inputId ] = 1;
	document.getElementById('fileInput'+inputId).style.display = 'table-row';
	document.getElementById('fileDel'+inputId).style.display = 'inline';
	if( freeInputs == fileVars.cnt )
	{
		document.getElementById('fileAddFirst').style.display = 'none';
		document.getElementById('fileAdd').style.display = 'table-row';
	}
	else if( freeInputs <= 1 )
		document.getElementById('fileAdd').style.display = 'none';
}

function hideInput( i )
{
	document.getElementById('fileInput'+i).style.display = 'none';
	document.getElementById('fileEl'+i).value = '';
	fileVars.states[ i ] = 0;

	var freeInputs = 0;
	for(i=1;i<=fileVars.cnt;i++)
	{
		if( ! fileVars.states[ i ] )
			freeInputs++;
	}

	if( freeInputs == fileVars.cnt )
	{
		document.getElementById('fileAddFirst').style.display = 'table-row';
		document.getElementById('fileAdd').style.display = 'none';
	}
	else
	{
		document.getElementById('fileAddFirst').style.display = 'none';
		document.getElementById('fileAdd').style.display = 'table-row';
	}
}
