/**
 * gmh.js
 * Javascript source file for functions related to the gmh website 
 */

function media_select(target) {
	$('.submit_col_body').not('#'+target+' .submit_col_body').slideUp(); //.find(':radio').removeAttr('checked');
	$('input[name=media_type]:radio').not('#'+target+' :radio').removeAttr('checked');
	$('#'+target+' .submit_col_body').slideDown(); //.find(':radio').attr('checked', true);
	$('#'+target).find(':radio').attr('checked', true);
	
	/*
	 * previews have been deemed unneccesary for now for gmh submissions
	 * 
	if ( target == 'submit_image' ) {
		$('#preview_btn').show();
		$('#submit_btn').hide();
	}
	else if ( target == 'submit_story' ) {
		$('#preview_btn').hide();
		$('#submit_btn').show();		
	}
	*/
}



//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();
});

