function showStackTrace(trace)
{
	var trace = Url.decode(trace);
	trace = trace.replace(/\n/g, "<br/>");
	if(trace == 0 || trace == '' || trace == null)
		trace = "We have no stack trace for this submission in the database." ;
	alertPopUp("Stack trace", '<div style="text-align: left;padding:10px 0px 10px 10px; font-weight:bold; text-align:venter; color:#333333;">' + trace + '</div>');
}

function changeTitle(tab, oldTitle, link)
{
	oldTitle = Url.decode(oldTitle);
	var fields = '<div class="popUpRow">Insert new title of the challenge:</div>'
				+ '<div class="popUpRow"><input type="hidden" name="o" value="title"/><input type="hidden" name="tab" value="' + tab + '"/>'
				+ '<input type="text" name="title" style="width:200px" value="' + oldTitle + '"/></div>';
	formPopUp(link, 'Change title', fields);
}

function evalBothChanged(submit){
	if($('#evalBothCheck').attr('checked'))
	{
		if(!submit)
		{
			document.getElementById('evalSingleDiv').style.display = 'block';
			document.getElementById('evalDouble').style.display = 'none';
		}
		else 
			confirmPopUpFull("$('#singleEvalForm').submit();", "$('#evalBothCheck').attr('checked', false);", "Warning",
					"Preliminary evaluation procedure will overwrite the final one. Are you sure you want to continue?");
	} else {
		document.getElementById('evalSingleDiv').style.display = 'none';
		document.getElementById('evalDouble').style.display = 'block';	
	}
}

function datasetBothChanged(){
	
	if(document.getElementById('datasetBothCheck').checked){
		$('#datasetBothCheck').attr('checked', true);
		$('#datasetSingleDiv').css('display', 'block');
		$('#datasetDouble').css('display', 'none');			
			/*confirmPopUpFull(" document.getElementById('datasetSingleDiv').style.display = 'block';document.getElementById('datasetDouble').style.display = 'none'; $('#datasetBothCheck').attr('checked', true);	$('#singleDatasetForm').submit(); $('#mainPopUp').remove();$('#popUpBg').remove();", 
					"$('#datasetBothCheck').attr('checked', false);", "Warning",
					"Preliminary dataset will overwrite the final one. Are you sure you want to continue?");*/
	} else {
		$('#datasetBothCheck').attr('checked', false);
		$('#datasetSingleDiv').css('display', 'none');
		$('#datasetDouble').css('display', 'block');	
	}
}


function timeBothChanged(){
		
	if(document.getElementById('timeBothCheck').checked){
		document.getElementById('timeSingle').style.display = 'none';
		document.getElementById('timeDouble').style.display = 'block';
		
			confirmPopUpFull("$('#timeoutOption').val('singleTimeout'); document.getElementById('timeSingle').style.display = 'block';document.getElementById('timeDouble').style.display = 'none'; $('#mainPopUp').remove();$('#popUpBg').remove();",
					"$('#timeBothCheck').attr('checked', false); $('#timeoutOption').val('doubleTimeout'); document.getElementById('timeSingle').style.display = 'none'; document.getElementById('timeDouble').style.display = 'block';	", 
					"Warning",
					"Preliminary timeout will overwrite the final one. Are you sure you want to continue?");		
	} else {
		document.getElementById('timeSingle').style.display = 'none';
		document.getElementById('timeDouble').style.display = 'block';
		$('#timeoutOption').val('timeout');	
	}
}

function timeBothChanged_old(submit){
	if(document.getElementById('timeBothCheck').checked){
		if(!submit){
			document.getElementById('timeSingle').style.display = 'block';
			document.getElementById('timeDouble').style.display = 'none';
		} else
			confirmPopUpFull("$('#timeoutOption').val('singleTimeout'); $('#timeoutForm').submit();",
					"$('#timeBothCheck').attr('checked', false);", "Warning",
					"Preliminary timeout will overwrite the final one. Are you sure you want to continue?");
	} else {
		document.getElementById('timeSingle').style.display = 'none';
		document.getElementById('timeDouble').style.display = 'block';	
	}
}

function editTrack(trackName){
	var plains = $("#" + trackName + "Row .plain");
	var inputs = $("#" + trackName + "Row .input");
	if(plains.hasClass("hidden")){
		plains.removeClass("hidden");
		inputs.addClass("hidden");
	} else {
		inputs.removeClass("hidden");
		plains.addClass("hidden");
	}
}

function publish(status)
{
	var msg = "";
	if(status != 'New')
		confirmPopUp("document.getElementById('publishForm').submit(); document.getElementById('messageBox').innerHTML = '';", "Warning", 
				"The challenge will become visible to everyone on Challenges list.\n\nDo you want to proceed?");
	else
		document.getElementById('publishForm').submit();
	document.getElementById('messageBox').innerHTML = msg;
}

function deleteChallenge(status)
{
	var msg = "";
	if(status != 'New')
		confirmPopUp("document.getElementById('deleteForm').submit(); document.getElementById('messageBox').innerHTML = ''; ", "Warning", 
				"Are you sure you want to delete this challenge?");
	else
		document.getElementById('deleteForm').submit();
	document.getElementById('messageBox').innerHTML = msg;
}

function unpublish()
{
	document.getElementById('unpublishForm').submit();
}

//parsing from YYYY-MM-DD HH:MM:SS date format into Date javascript object
function parseDate(string)
{
	var dateArr = string.replace(/-/g, '/').split(' ');
	if(dateArr.length == 1)
		var timeArr = new Array(0, 0, 0);
	else
		var timeArr = dateArr[1].split(':');
	var date = new Date(dateArr[0]);
	if(isNaN(date.getTime()))
		return null;
	if(timeArr.length > 0)
		date.setHours(timeArr[0]);
	if(timeArr.length > 1)
		date.setMinutes(timeArr[1]);
	if(timeArr.length > 2)
		date.setSeconds(timeArr[2]);
	if(isNaN(date.getTime()))
		return null;
	return date;
}

function changeIcon(url, oldVal)
{
	html = '<div class="popUpRow">File path in repository:</div>';
	html += '<div class="popUpRow" style="text-align:center;"><input type="hidden" name="o" value="icon"/><input type="text" name="icon" value="' + oldVal + '" style="width:210px;" /></div>';
	formPopUp(url, "Change icon", html);
}

function changePrize(url, prizeVal, prizeCur, trackName, tab, list)
{
	var html = '<div class="popUpRow" style="text-align:center;"><div style="width:58%;float:left;text-align:right;overflow:auto;">';
	html += '<input type="hidden" name="o" value="prize"/><input type="hidden" name="tr" value="' + trackName + '"/><input type="hidden" name="tab" value="' + tab + '"/>';
	html += '<input type="text" name="value" value="' + prizeVal + '" style="width: 100px;"/></div>';
	html += '<div style="width:42%;float:left;overflow:auto;"><select name="currency">';
	$.each(list, function(i, el){
		if(el == prizeCur)
			html += '<option selected="1">' + el + '</option>';
		else
			html += '<option>' + el + '</option>';
	});
	html += '</select></div></div>';
	formPopUp(url, "Change prize", html);
	$('#popupCurrency').autocomplete(list, {
		width: 0,
		minChars: 0,
		scrollHeight: 240,
		max: 239,
		formatItem: function(row) { return row[0]; }
	});
}

function addNewMember(element)
{	
	var memberList = document.getElementById('membersList');
	
	var newDiv = document.createElement('div');
	newDiv.setAttribute('class','simpleInputContainer');
	newDiv.setAttribute('style','float:none;width:250px;overflow:auto;display:block;');
	
	var newDivTxt = document.createElement('div');
	newDivTxt.setAttribute('style','float:left;width:20px;overflow:auto;display:block; padding-top:5px;');
	
	if(memberList.childNodes.length%2 == 0)
		var newTxt = document.createTextNode(((memberList.childNodes.length/2) + 1) + '. ');
	else
		var newTxt = document.createTextNode(((memberList.childNodes.length/2) + 1.5) + '. ');
	
	newDivTxt.appendChild(newTxt);

	var newButton = document.createElement('div');
	newButton.setAttribute('class','minus');
	
	newButton.onclick = function () {
		if(this.nextSibling.value) 
			this.nextSibling.value='';
		};
	
	//newButton.setAttribute.onclick = memberList.childNodes[1].firstChild.onclick;
	
	newDiv.appendChild(newButton);
	
	var newInput = document.createElement('input');
	newInput.setAttribute('class','simpleInput');
	newInput.setAttribute('type','text');
	
	if(memberList.childNodes.length%2 == 0)
		newInput.setAttribute('name','member_'+((memberList.childNodes.length/2))- 1);
	else
		newInput.setAttribute('name','member_'+((memberList.childNodes.length/2) - 0.5));
	
	newDiv.appendChild(newInput);
		
	
	memberList.appendChild(newDivTxt);
	memberList.appendChild(newDiv);
}

/* Challenge Prelim Final Dataset Division */

/* when bar was pressed */
function dragCrossFader(e) 
{
	var cfader = document.getElementById('crossfader');	
	var target = e.target != null ? e.target : e.srcElement; 
	
	/* turns off broswers drag/drop function */
	target.ondragstart = function() { return false; }; 
		
	/* microsfot wants to be "original" :( */	
	if (e == null) { e = window.event; }
	
	/* parse current style left value */
	var cur_X = parseInt(cfader.style.left);
	
	/* if somehow the value of current x position of bar is not a number */
	if(cur_X == NaN || cur_X =='')
		cur_X = 0;
	
	/* count current x value */	
	dx = Math.abs(cur_X - e.clientX);

	MoveCF = true;
	cfader.onmousemove = moveCrossFader;
	document.onmouseup = dropCrossFader;
	document.onmouseup = dropCrossFader;
}

/* when bar was released */
function dropCrossFader(e) 
{
	var cfader = document.getElementById('crossfader');
	MoveCF = false;
	cfader.onmousemove = function () {};
}

/* when bar is moved (and previously pressed) */
function moveCrossFader(e)
{	
	var cfader = document.getElementById('crossfader');
	
	/* microsfot wants to be "original" :( */
	if (e == null) { e = window.event; }
	
	var newPos = (e.clientX - dx);
	
	/* if bar goes beyond the bondary corrects its position */
	if(newPos < 18)
		newPos = 18;
	if(newPos > 260)
		newPos = 260;
	
	setPercentage(newPos);	
	
	newPos = newPos;
	/* sets new position according to position of cursor */
	if(MoveCF)
	{
		cfader.style.left = newPos + 'px';
	} 
}

/* Challenge Training Test Dataset Division */

/* when bar was pressed for training/test split */
function dragCrossFaderTraining(e) 
{
	var cfader = document.getElementById('crossfader_training');	
	var target = e.target != null ? e.target : e.srcElement; 
	
	/* turns off broswers drag/drop function */
	target.ondragstart = function() { return false; }; 
		
	/* microsfot wants to be "original" :( */	
	if (e == null) { e = window.event; }
	
	/* parse current style left value */
	var cur_X = parseInt(cfader.style.left);
	
	/* if somehow the value of current x position of bar is not a number */
	if(cur_X == NaN || cur_X =='')
		cur_X = 0;
	
	/* count current x value */	
	dx = Math.abs(cur_X - e.clientX);

	MoveCF = true;
	cfader.onmousemove = moveCrossFaderTraining;
	document.onmouseup = dropCrossFaderTraining;
	document.onmouseup = dropCrossFaderTraining;
}

/* when bar was released for training/test split */
function dropCrossFaderTraining(e) 
{	
	var cfader = document.getElementById('crossfader_training');	
	MoveCF = false;
	cfader.onmousemove = function () {};
}

/* when bar is moved (and previously pressed) for training/test split */
function moveCrossFaderTraining(e)
{
	var cfader = document.getElementById('crossfader_training');

	/* microsfot wants to be "original" :( */
	if (e == null) { e = window.event; }
	
	var newPos = (e.clientX - dx);
	
	/* if bar goes beyond the bondary corrects its position */
	if(newPos < 18)
		newPos = 18;
	if(newPos > 260)
		newPos = 260;

	setPercentageTraining(newPos);

	newPos = newPos;
	/* sets new position according to position of cursor */
	if(MoveCF)
	{
		cfader.style.left = newPos + 'px';
	}	 
}

/* pop up's wizard panel */
function popUpWizard(element_id) 
{
	showHideElement(element_id);
}

/* change current element ie. show it and hide others */
function switchElements(element_id,elements_a) 
{
	hideElements(elements_a);
	showHideElement(element_id);
}

function setPercentage(position)
{
	$('#datasetSplit').html(Math.round((position-18)/2.43) + '% of Test into Preliminary');
	$('#datasetSplitPrelimProc').html('' + Math.round((position-18)/2.43) + '%');
	$('#datasetSplitFinalProc').html('' + (100 - Math.round((position-18)/2.43)) + '%');
	$('#prelimPercForm').val(Math.round((position-18)/2.43));
	$('#finalPercForm').val(100 - Math.round((position-18)/2.43));
}

function setPercentageTraining(position)
{
	$('#datasetSplit2').html(Math.round((position-18)/2.43) + '% in Training');
	$('#datasetSplitTrainProc').html('' + Math.round((position-18)/2.43) + '%');
	$('#datasetSplitTestProc').html('' + (100 - Math.round((position-18)/2.43)) + '%');
	$('#trainingPercForm').val(Math.round((position-18)/2.43));
	$('#testPercForm').val(100 - Math.round((position-18)/2.43));	
}

/* it moves bar when it's pad clicked */
function MoveCrossfaderBar(parent_id,element_id,event)
{
	pos_x = event.offsetX?(event.offsetX):event.pageX;

	var barPos = pos_x -  findPosX(parent_id) - 13;
	if(barPos<18)
		barPos = 18;
	if(barPos>260)
		barPos = 260;

	element_id.style.left = barPos + 'px';
	
	if(parent_id.getAttribute('id').match(/training$/))
		setPercentageTraining(barPos);
	else
		setPercentage(barPos);
}

function CrossFaderTrainingRight(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

	var cfader = document.getElementById('crossfader_training');
	var intPerc = parseInt(cfader.style.left.replace('px',''));
	cfader.style.left = Math.round(intPerc + 2.42) + 'px';
	setPercentageTraining(intPerc);
}

function CrossFaderTrainingLeft(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	var cfader = document.getElementById('crossfader_training');
	var intPerc = parseInt(cfader.style.left.replace('px',''));
	cfader.style.left = Math.round(intPerc - 2.42) + 'px';
	setPercentageTraining(intPerc);
}

function CrossFaderRight(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	var cfader = document.getElementById('crossfader');
	var intPerc = parseInt(cfader.style.left.replace('px',''));
	cfader.style.left = Math.round(intPerc + 2.42) + 'px';
	setPercentage(intPerc);
}

function CrossFaderLeft(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
	var cfader = document.getElementById('crossfader');
	var intPerc = parseInt(cfader.style.left.replace('px',''));	
	cfader.style.left = Math.round(intPerc - 2.42) + 'px';
	setPercentage(intPerc);
}

/* show hide elements */
function showHideElement(element_id)
{
	var element = document.getElementById(element_id);
	
	if(element.style)
	{
		if(element.style.display == '' || element.style.display == 'none')
			element.style.display = 'block';
		else
			element.style.display = 'none';
	}
	else
		element.setAttribute('style','display:block;');
}

/* hide elements */
function hideElements(elements_a)
{
	for(var t in elements_a)
	{
		var element = document.getElementById(elements_a[t]);
		
		if(element.style)
		{
			element.style.display = 'none';
		}
		else
		if(element.setAttribute)
		{
			element.setAttribute('style','display:none;');
		}
	}
}

/* make enable/disable */
function enableDisable(element_id,dis)
{
	var el_1 = document.getElementById(element_id);
	
	if(!dis)
	{
		if(el_1.className == "disabled")
			el_1.className = "";
		else
			el_1.className = "disabled";
	}
	else
	if(dis == false)
	{			
		el_1.className = "";
	}
	else
	{
		el_1.className = "disabled";
	}
}

