/* ******************** 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);
}

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

var ids = Array("first_name","last_name","gender","dob_d","dob_m","dob_y", "username","password","password_confirm",
					"address","town","county","region","postcode","country","telephone","mobile","email", 
					"ethnicity","ethnicity_other","school","school_other","disability","disability_needs","access","access_needs","hearsay","hear_about",
					"audition_type", "audition_centre_ID", "batch_ID", "department",
					"billing_address", "billing_city", "billing_postcode", "tandc", "cardholder_present");

function updateApplication(c, n) {
	if(n != 0) {
		c += n;
	}
	
	// do all the update of session for app...

	var res = new Object();
	
	for(var i = 0; i < ids.length; i++) {
		var thisID = ids[i];
		if(document.getElementById(thisID)){
			var thisElement = document.getElementById(thisID);
			
			
			if(thisElement.type == "radio"){
				// Handle Radio Groups
				var lookingFor = thisElement.name;
				var radioGroup = document.getElementsByName(lookingFor);
				for(r=0; r < radioGroup.length; r++) {
					if(radioGroup[r].checked) {
				    	var val = radioGroup[r].value;
				    }
				}
			} 
			
			
			else if(thisElement.type == "checkbox") {
				// Handle Checkboxes
				var val = thisElement.checked ? "checked" : "";
			}  
			
			
			else {
				var val = thisElement.value;
			}
			
			
			res[thisID] = val;		
		}
	}
	
	post_to_url("apply_" + c + ".php", res);
}

function sendApplication() {
	//alert("This will now post your details over to Barclays\n\nIn Testing Mode...");
	post_to_url("apply_payment_1.php");
	//post_to_url("apply_ideas_tap.php");
}

function submitPayment() {
	var form = document.getElementById("form1");
    form.submit();
}

function sendToIdeasTap() {
	post_to_url("apply_ideas_tap_2.php");
}

function showOther(id, other_id) {
	var el = document.getElementById(id)
	var text = el.value.toLowerCase();
	
	if(el.type == "radio"){
		// Handle Radio Groups
		var lookingFor = el.name;
		var radioGroup = document.getElementsByName(lookingFor);
		for(r=0; r < radioGroup.length; r++) {
			if(radioGroup[r].checked) {
		    	text = radioGroup[r].value;
		    }
		}
	}	
	
	if(strstr(text, 'other') || strstr(text, 'specify') || text == 1) {
		showElem(other_id);
	} else {
		hideElem(other_id);
	}
}

function strstr( haystack, needle, bool ) {
	var pos = 0;
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}