/**
 * sbs.js
 * Javascript source file for functions related to the sbs website 
 */


//sets the username, location and email cookies
function setCookie( nam, loc, eml ) {
	document.cookie = "username=" + escape(nam) + ";";
	document.cookie = "location=" + escape(loc) + ";";
	document.cookie = "email=" + escape(eml) + ";";
}

//gets a cookie
function getCookie(c_name) {
	var i,x,y,c=document.cookie.split(";");
	for (i=0;i<c.length;i++) {
		x=c[i].substr(0,c[i].indexOf("="));
		y=c[i].substr(c[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
  }
}
}

//Fills in the name/location/email form if there is a stored cookie
function restoreFromCookie() {
	var username = getCookie("username");
	if (username!=null && username!="" && username!="undefined") {
		$("[name=username]").val(username);
}
	
	var location = getCookie("location");
	if (location!=null && location!="" && location!="undefined") {
		$("[name=location]").val(location);
}
	
	var email = getCookie("email");
	if (email!=null && email!="" && email!="undefined") {
		$("[name=email]").val(email);
}
}

//Resets the form for another submission
function submitAnother() {
	//clear form and restore name/location/email from cookie
	$('form :input').val("");
	restoreFromCookie();
	
	//Hide results from last submission and enable the form
	$('#results').slideUp();
	$('#content').val('');
	$('#submit_block').slideDown();
	$('#submit_btn').val('Submit');
	$('#submit_btn').show();
	$('input[type=submit]',$('#submit_form')).attr('enabled','enabled');

}

//Restore name/loaction/email feilds on pageload.
$(document).ready( function(){
	restoreFromCookie();
});

