function _get(n, d)
{
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=_get(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function _display_div(div)
{
	div.className = 'Displayed';
}
function _hide_div(div)
{
	div.className = 'Hidden';
}

function _change_bullet( bullet )
{
    var on = false;
    if (bullet != null)
    {
        if (bullet.src.indexOf("bulletRight") > -1)
        {
            bullet.src = "/images/icons/bulletDown.gif"
            on = true;
        }
        else
            bullet.src = "/images/icons/bulletRight.gif"
    }
    return on;
}
function _loading_indicator(object)
{
    var icon = new Image();
    icon.src = '/images/icons/indicator.gif';
    //icon.className = "loading";
		object.update('');
    object.appendChild(icon);
}
function _progress_indicator(object)
{
    var icon = new Image();
    icon.src = '/images/icons/progressbar_green.gif';
    icon.className = "loading";
		object.update('');
    object.appendChild(icon);
}
function _dialogue(page)
{
	new Ajax.Updater('dialogue', page, {method:'get'});
	_displayOverlay();
}
function _displayOverlay()
{
	//set the top of the overlay div:
	$('overlay').setStyle({top:document.body.scrollTop});
	$('dialogue').setStyle({top:$('dialogue').style.marginTop + document.body.scrollTop});
	$('overlay').show();
	$('dialogue').show();
}
function _hideOverlay()
{
	$('overlay').hide();
	$('dialogue').hide();
	$('dialogue').update('');
}

function _right_column_loading()
{
    $('divInfoWindow').innerHTML = "<p align=\"center\" style=\"margin-top:40px;\"><img src=\"/images/icons/loading.gif\" /></p>"
}
function _close_info_window()
{
//	new Effect.Fade('divInfoWindow', {duration:0.2});
	$('divInfoWindow').hide();
}
function _user_info_window(user_id)
{
	//new Effect.Fade('divInfoWindow', {duration:0.2});
	new Ajax.Updater('divInfoWindow',
		'/user/context_user_profile/' + user_id,
			{
			asynchronous:true,
			evalScripts:true,
			onLoading:function(request){_right_column_loading();},
			onComplete:function(request){
				new Effect.Appear('divInfoWindow', {duration:0.5});
				}
			}
		);
	return false;
}
function _info_window(text)
{
	if (text!='')
	{
		$('divInfoWindow').update('<div class="info_window">' + text + '</div>');
		$('divInfoWindow').show();
	}
}

function build_alias(str, text_field, span)
{
	newString = "";
	//go through each character in the string and make sure it's a letter:
	for (i=0; i < str.length; i++)
	{
		if (isAlphabet(str[i]))
		{
			newString += str[i].toLowerCase();
		}
	}
	if ( text_field != null )
		text_field.value = newString;
	if ( span != null )
		span.innerHTML = newString;
}
function isAlphabet(str)
{
	var alphaExp = /^[a-zA-Z0-9_-]+$/;
	if(str.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

function contact_host(host_id){
	_displayOverlay();
	new Ajax.Updater('dialogue', '/supervisor/hosts/contact_details/' + host_id,
	   {
	       method:'get'
	   });		
}
function contact_student(student_id){
	_displayOverlay();
	new Ajax.Updater('dialogue', '/supervisor/students/contact_details/' + student_id,
	   {
	       method:'get'
	   });		
}

	function padLeft(num, totalChars, padWith) {
	num = num + "";
	padWith = (padWith) ? padWith : "0";
	if (num.length < totalChars) {
		while (num.length < totalChars) {
			num = padWith + num;
		}
	} else {}

	if (num.length > totalChars) { //if padWith was a multiple character string and num was overpadded
		num = num.substring((num.length - totalChars), totalChars);
	} else {}

	return num;
}

function edit_notation(icon, notationable_type, notationable_id)
{	
	if (icon != null && icon != 'undefined')
		icon.toggle();
	
	if ($('past_notations_icon_' + notationable_type + '_' + notationable_id) != null)
		$('past_notations_icon_' + notationable_type + '_' + notationable_id).toggle();
		
	current_notation = icon.name;
	var form_html = '<form method="post" id="notation_form_'+notationable_type+'_'+notationable_id+'" onsubmit="save_notation(this,\''+notationable_type+'\',\''+notationable_id+'\');return false;">' +
	  '<input type="text" maxlength="149" id="notation_title_'+notationable_type+'_'+notationable_id+'" name="notation[title]" value="' + current_notation + '" class="notation_textfield" /></form>';
	
	$('notation_div_' + notationable_type + '_' + notationable_id).update(form_html);
	$('notation_title_' + notationable_type + '_' + notationable_id).focus();
	$('notation_title_' + notationable_type + '_' + notationable_id).select();
}
function save_notation(form, notationable_type, notationable_id)
{
	new Ajax.Updater('notation_div_' + notationable_type + '_' + notationable_id,'/notations/create?notationable_type=' + notationable_type + '&notationable_id=' + notationable_id,
	  {
		method:'post',
		parameters:Form.serialize(form),
		onLoading:function(){
		  $('notation_title_'+notationable_type+'_'+notationable_id).blur();
		  $('notation_title_'+notationable_type+'_'+notationable_id).disable();
		},
		onSuccess:function(){
		  $('notation_div_' + notationable_type + '_' + notationable_id).update('');
		}
	  });
}
function show_past_notations(icon, notationable_type, notationable_id)
{	
	//new Effect.Appear($('past_notations_div_' + notationable_type + '_' + notationable_id));
	
	new Ajax.Updater('past_notations_div_' + notationable_type + '_' + notationable_id,'/notations/past_notations?notationable_type=' + notationable_type + '&notationable_id=' + notationable_id,
	  {
		method:'get',
		onSuccess:function(){
		  new Effect.Appear($('past_notations_div_' + notationable_type + '_' + notationable_id));
		}
	  });
	  
}


