

interval = null;

function openProgressBar() 
{
	/* generate random progress-id */
	uuid = "";
	for (i = 0; i < 32; i++) {
		uuid += Math.floor(Math.random() * 16).toString(16);
	}
	/* patch the form-action tag to include the progress-id */
	document.getElementById("MainForm").action="/hosting/add/?X-Progress-ID=" + uuid;
	
	/* call the progress-updater every 1000ms */
	interval = window.setInterval(function () {
			fetch(uuid);
		},
		10000
	);
	return true;
}


function fetch(uuid) {
	req = new XMLHttpRequest();
	req.open("GET", "/upload_progress?X-Progress-ID=" + uuid, 1);
	req.setRequestHeader("X-Progress-ID", uuid);
	req.onreadystatechange = function () {
		if (req.readyState == 4) {
			if (req.status == 200) {
				/* poor-man JSON parser */
				var upload = eval(req.responseText);
				/* change the width if the inner progress-bar */
				if (upload) {
					if (upload.state == 'done' || upload.state == 'uploading') {
						wproc = Math.round(99 * upload.received / upload.size);
						if (wproc > 0 && wproc <= 100) {
							document.getElementById('SendButton').value = 'Czekaj...' + wproc + '%';
						}
					}
					/* we are done, stop the interval */
					if (upload.state == 'done') {
						window.clearTimeout(interval);
					}
				}
			}
		}
	}
	req.send(null);
}



function UpdateFile()
{
	var valid = true;
	var form = GET('MainForm');
	if (Trim(form['text_over'].value)) {
		if (!Trim(form['text_color'].value)) {
			alert('Wybierz kolor tekstu');
			form.elements['text_color'].focus();
			valid = false;
		}
	}
	if (valid) {
		button = GET('SendButton');
		button.value = 'Czekaj...';
		button.disabled = true;
	}
	return valid;
}


function DisableSaveButton(disable)
{
	button = GET('SendButton');
	if (!button) return false;
	if (disable) {
		oldLabel = button.value; 
		button.value = 'Czekaj...';
	} else {
		button.value = oldLabel;
	}
	button.disabled = disable;
	return true;
}

function AddGallery()
{
	var valid = true;
	var form = GET('MainForm');

	var file = false;
	var notJpeg = false;
	for(i=0; i<8; i++) {
		if (form['File'+i] && form['File'+i].value) {
			file=true;
			if (!form['File'+i].value.match(/\.(jpg|jpeg)$/i)) {
				notJpeg = true;
			}
		}
	}
	if (file==false) {
		alert('Wybierz obrazki do wysłania na serwer');
		valid = false;
	}
	if (notJpeg) {
		alert('Obrazki powinny być w formacie JPEG (końcówka nazwy pliku: .jpg albo .jpeg)');
		valid = false;
	}
	return valid;
}


function DeleteImage(id)
{
	if (confirm('Czy na pewno usunąć ten obrazek?')) {
		document.forms['MainForm'].elements['Action'].value = 'delete';
		document.forms['MainForm'].elements['Params'].value = id;
		document.forms['MainForm'].submit();
	}
}


function SaveDefaults()
{
	var valid = true;
	var form = GET('MainForm');
	if (Trim(form['text_over'].value)) {
		if (!Trim(form['text_color'].value)) {
			alert('Wybierz kolor tekstu');
			form.elements['text_color'].focus();
			valid = false;
		}
	}
	return valid;
}


function ResetStats(id)
{
	if (confirm('Czy wyzerować statystyki galerii?')) {
		document.forms['MainForm'].elements['Action'].value = 'reset';
		document.forms['MainForm'].submit();
	}
}


function FolderAction(id, action)
{
	if (action=='Edit') {		//zmiana nazwy folderu
		document.forms['MainForm'].elements['Action'].value = action;
		document.forms['MainForm'].elements['Value'].value = id;
		document.forms['MainForm'].submit();
	}
}


function ChangeOverlayType(value)
{
	var divPos = GET('divPos');
	var divText = GET('divText');
	var divImage = GET('divImage');
	if (value == 'none') {
		divPos.style.display = 'none';
		divText.style.display = 'none';
		divImage.style.display = 'none';
	}
	if (value == 'text') {
		divPos.style.display = 'block';
		divText.style.display = 'block';
		divImage.style.display = 'none';
	}
	if (value == 'image') {
		divPos.style.display = 'block';
		divText.style.display = 'none';
		divImage.style.display = 'block';
	}
}
