/* ***********************************************

juice.js v1.0

all the good-stuff inside the berry

Functions to add interaction, animation and polish

Last updated - 22.04.09 - Hugh Williams

*********************************************** */

/* ******************** FORM VALIDATION ********************* */

function focusForm(x) {
	document.getElementById(x).value="";
}
function blurForm(x) {
	switch(x) {
		case "register_email":
		var newText = "enter your email to register";
		break;
	}
	document.getElementById(x).value = newText;
}
function validate_email(field) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2)  {
			return false;
		} else {
			return true;
		}
	}
}
function validate_form(thisform) {
	var valid = true;
	var alertText = "";
	var problemCount = 0;
	with (thisform) {
		if (validate_email(email) == false) {	
			valid = false;
			alertText += "The email address was not valid.\n";
			problemCount++;
		}
	}
	if(valid == true) {
		return true;
	} else {
		retText = problemCount == 1 ? "There was a problem:\n\n" : "There were " + problemCount + " problems:\n\n";
		alert(retText + alertText);
		return false;
	}
}

/* ******************** ANIMATION ********************* */

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) { 
    var delta = maxValue - minValue; 
    var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
    return Math.ceil(stepp) 
} 

function doPosChangeMem(elem,startPos,endPos,steps,intervals,powr) {
	if (elem.posChangeMemInt) window.clearInterval(elem.posChangeMemInt);
	var actStep = 0;
	elem.posChangeMemInt = window.setInterval(
		function() {
			elem.currentPos = [
				easeInOut(startPos[0],endPos[0],steps,actStep,powr),
				easeInOut(startPos[1],endPos[1],steps,actStep,powr)
				];
			elem.style.left = elem.currentPos[0]+"px";
			elem.style.top = elem.currentPos[1]+"px";
			actStep++;
			if (actStep > steps) window.clearInterval(elem.posChangeMemInt);
		}
		,intervals);

}

function moveToPos(xp, id) {
	var p = document.getElementById("pointer");
	p.style.visibility = "visible";
	
	var d = document.getElementById("desc0");
	d.style.visibility = "hidden";
	d.style.display = "none";
			
	if (!p.currentPos) p.currentPos = [0,-32]; //if no mem is set, set it first;

	for(i = 1; i<4;i++) {
		var d = document.getElementById("desc" + i);
		if(i != id) {
			d.style.visibility = "hidden";
			d.style.display = "none";
		} else {
			d.style.visibility = "visible";
			d.style.display = "";
		}
	}
	
	doPosChangeMem(p,p.currentPos,[xp,-32],15,15,0.4);
}

function moveOff() {
	var p = document.getElementById("pointer");
	p.style.visibility = "hidden";
	
	var d = document.getElementById("desc0");
	d.style.visibility = "visible";
	d.style.display = "";
	
	for(i = 1; i<4;i++) {
		var d = document.getElementById("desc" + i);
		d.style.visibility = "hidden";
		d.style.display = "none";
	}
}

/* ******************** CSS MANIPULATION ********************* */

function showElem(el) {
	var d = document.getElementById(el);
	d.style.visibility = "visible";
	d.style.display = "";
	//alert(el);
}

function hideElem(el) {
	var d = document.getElementById(el);
	d.style.visibility = "hidden";
	d.style.display = "none";
}

function displayEdit(n) {
	//alert(n);
	showElem("row_edit_" + n);

}

function hideEdit(n) {
	//alert(n);
	hideElem("row_edit_" + n);
}
