// methods to show/hide "AJAX" form divs. assumes you have _form, _busy, and _link divs
function show_busy_div(name) {
	if($(name+'_form')) { Element.hide(name+'_form'); };
	if($(name+'_link')) { Element.hide(name+'_link'); };
	if($(name+'_busy')) { Element.show(name+'_busy'); };
}
function show_form_div(name) {
	if($(name+'_link')) { Element.hide(name+'_link'); };
	if($(name+'_busy')) { Element.hide(name+'_busy'); };
	if($(name+'_form')) { Element.show(name+'_form'); };
}
function show_link_div(name) {
	if($(name+'_busy')) { Element.hide(name+'_busy'); };
	if($(name+'_form')) { Element.hide(name+'_form'); };
	if($(name+'_link')) { Element.show(name+'_link'); };
}
function clear_form_div(name) {
	if($(name+'_form')) { $(name+'_form').innerHTML = ''; }
	if($(name+'_form')) { Element.hide(name+'_form'); };
	if($(name+'_busy')) { Element.hide(name+'_busy'); };
	if($(name+'_link')) { Element.show(name+'_link'); };
}
// methods to show/hide "AJAX" form spans in table rows.
function show_busy_row(name,id) {
	if($('tr_'+name+'_link_'+id)) { Element.hide('tr_'+name+'_link_'+id); };
	if($('tr_'+name+'_busy_'+id)) { Element.show('tr_'+name+'_busy_'+id); };
}
function show_link_row(name,id) {
	if($('tr_'+name+'_busy_'+id)) { Element.hide('tr_'+name+'_busy_'+id); };
	if($('tr_'+name+'_link_'+id)) { Element.show('tr_'+name+'_link_'+id); };
}
// methods to show/hide busy widgets
function show_busy_widget(name) {
	if($(name)) { Element.show(name); };
} 
function hide_busy_widget(name) {
	if($(name)) { Element.hide(name); };
}
// -------------------------------------------------------
function not_implemented_yet() { alert("This feature hasn't been implemented yet!"); return false; };
function popup_details(a) {
	return popup_window(a,false);
}
function popup_program(a) {
	return popup_window(a,true);
}
function popup_window(a,is_large) {
	var properties = 'titlebar=0,status=0,location=0,sizable=1,scrollbars=yes';
	if(is_large) {properties += ',width=800,height=600';} 
	else {properties += ',width=375,height=600';}
	
	var win = window.open(a.href,'details_window',properties);
	win.focus();
	return false;
}

// -------------------------------------------------------
function show_redcloth_help(url) {
	var features = 'width=600,height=500,resizable=yes,scrollbars=yes';
	window.open(url,'redcloth',features);
}


// validation methods
function validate_date(element,which_date) {
	var date_str = $F(element);
	if(date_str.length==0) { return true; }
	var pieces = date_str.split('/');
	var mon = -1;
	var day = -1;
	var year = -1;
	if(pieces.length == 3) {
		mon = Number(pieces[0]);
		day = Number(pieces[1]);
		year = Number(pieces[2]);
	} else {
		pieces = date_str.split('-');
		if(pieces.length == 3) {
			if(pieces[0]>'1900') {
				mon = Number(pieces[1]);
				day = Number(pieces[2]);
				year = Number(pieces[0]);
			} else {
				mon = Number(pieces[0]);
				day = Number(pieces[1]);
				year = Number(pieces[2]);
			}
		}
	}
	if((mon>=1&&mon<=12)&&(day>=1&&day<=31)&&(year>=1900)) {
		return true;
	}
	alert(which_date+' is invalid. Please enter date in MM/DD/YYYY format.');
	return false;
}

function check_for_preceptor()
{
	var somestring;
	somestring = "test: "
	for(var i=0; i< $('professions').length; i++)
	{
	    var option = $('professions').options[i];
	    if(option.selected && option.value == 4)
		{
			//alert("Person is a Pharamcist!");
			Element.show('preceptor_container');
	    return 
		}
	}
	// default hide
	Element.hide('preceptor_container');
}

function display_preceptor_schools()
{
	if($('is_preceptor').value == 1) { Element.show('preceptor_schools'); }
	else { Element.hide('preceptor_schools'); }
}

// function to position the assessment answer key correctly in the bottom of the left column
function position_key() {
	var win_width = window.innerWidth;
	if (win_width > 900) {
		win_width = (win_width - 900)/2 - 8;
	}
	$('answer_key').setStyle({'left': win_width + 'px'});
}

// function to update the answer key when a question is answered
function mark_answer(question_id) {
	$('question_indicator_' + question_id).innerHTML = '<img src="/images/icon_answered.gif" alt="√" />';
}

// function to generate a session code for courses
function generate_session_code() {
	var chars = new Array("a","b","c","d","e","f","g","h","j","k","m","n","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z","2","3","4","5","6","7","8","9"); // IE hack
  var password = '';
	for(var i = 0; i < 6; i++) {
		var rand = Math.floor(Math.random()*chars.length);
		password = password + chars[rand];
	}
	target = $('course_session_code');
	if (target.value == '') { target.value = password; }
	else if (confirm('Are you sure you want to replace the existing session code?')) {
		target.value = password;
	}
}

function display_question_example() {
  Element.hide('text_example');
  Element.hide('agree_gradient_example')
	$('question_text_here').replace('<p id="question_text_here">'+$('question_text').value+'</p>');
	if ($('question_question_type').value == 0) { Element.show('text_example'); } // text
	else if ($('question_question_type').value == 1) { Element.show('agree_gradient_example'); } //agree radios
}