var loaded = null;

function addLi(t) {
	var ul = t.getElementsByTagName('ul');
	
	ul[0].appendChild( buildLi('') );
	highlightInput(ul[0]);
}

function dropLi(t) {
	var u = t.parentNode;
	u.removeChild(t);
	highlightInput(u);
}

function dropSquar(t) {
	var i = t.getElementsByTagName('input');
	if (i[0]) {
		var drop = confirm("Are you sure you want to drop this Squar?\n\n\""+i[0].value+'"');
	} else {
		var drop = true;
	}
	
	if (drop)
		t.parentNode.removeChild(t);
}

function addSquar(a) {
	document.getElementById('fs').appendChild( buildSquar(a) );
}

function moveUp(t) {
	Move(t, 'up');
}

function moveDown(t) {
	Move(t, 'down');
}

function Move(t, dir) {
	var theLi = t;
	var theUl = t.parentNode;
	
	var theLis = theUl.getElementsByTagName('li');
	
	for (var i = 0; i < theLis.length; i++) {
		if (theLis[i] == theLi) {
			var theLiId = i;
		}
	}
	
	if ((dir == "up") && (theLiId != 0)) {
		var movedLiId = theLiId - 1;
		theUl.insertBefore(theLis[theLiId], theLis[movedLiId]);
	}
	if ((dir == "down") && (theLiId < theLis.length - 1)) {
		var movedLiId = theLiId + 1;
		theUl.insertBefore(theLis[movedLiId], theLis[theLiId]);
	}
	highlightInput(theUl);
}

function highlightAllInput() {
	var theUls = document.body.getElementsByTagName('ul');
	
	for (var j = 0; j < theUls.length; j++) {	
		highlightInput(theUls[j]);
	}
}

function highlightInput(ul) {
		var theInputs = ul.getElementsByTagName('input');
		
		for (var i = 0; i < theInputs.length; i++) {
			theInputs[i].style.borderColor = '#000000';
		}
		
		if (theInputs[0])
			theInputs[0].style.borderColor = '#6CB900';
		
		if (theInputs.length - 1 >= 0)
			theInputs[theInputs.length - 1].style.borderColor = '#B90009';
}

function saveFS() {
	if (loaded) {
		var fs = document.getElementById('fs');
		var a = new Array();
		
		var uls = fs.getElementsByTagName('ul');

		for (var u = 0; u < uls.length; u++) {
			var inputs = uls[u].getElementsByTagName('input');
			a[u] = new Array();
			
			for (var i = 0; i < inputs.length; i++) {
				a[u][i] = inputs[i].value;
			}
		}
		setCookie('fsquar', a.toJSONString());
	}
}

function loadFS() {
	if (!loaded) {
		setStatus("Loading");
		var get = getCookie('fsquar');
		document.getElementById('fs').innerHTML = '';
		if (get != null) {
			var a = eval('(' + getCookie('fsquar') + ')');
	
			for (var i = 0; i < a.length; i++) {
				addSquar(a[i]);
			}	
			
		} else {
			addSquar();
			addSquar();

		}	

		loaded = true;
		
	document.getElementById('build').innerHTML = "Build Essay";
	document.getElementById('build').onclick = function(){
	buildEssay();
	};
		clearStatus();
	}
}

function onLoad() {
	loadFS();
}

function setStatus(m) {
	document.getElementById('msg').innerHTML = m;
	document.getElementById('status').style.display = "block";
}	

function clearStatus() {
	document.getElementById('status').style.display = "none";
}

function clearFS() {
	var c = confirm("Are you sure you want to clear your 4 squar?");
	
	if (c) {
		setCookie('fsquar', 0);
		loaded = null;
		loadFS();
	}
}