
function ActivateFlash()
{
    var objects = document.getElementsByTagName("object");
    for (var i = 0; i < objects.length; i++)
    {
      objects[i].outerHTML = objects[i].outerHTML;
    }    
}

//document.observe( 'dom:loaded', initializePage );

var SC =
{
	Form :
	{
		clearForm : function ( form )
		{
			o_form = Element.extend( form );
			o_form.getElements().each( function( elem_iterator )
			{
				SC.Form.setInputValue( elem_iterator, '' );
			} );
		},

		setInputValue : function( elem, value )
		{
			if ( elem.type == "text" )
			{
				elem.value = value;
			}
			else if ( elem.type == "radio" || elem.type == "checkbox" )
			{
				elem.checked = ( elem.value == value );
			}
			else if ( elem.type == "select-one" )
			{
				elem.selectedIndex = 0;
				for ( var i = 0; i < elem.options.length; i++ )
				{
					if ( elem.options[i].value == value )
					{
						elem.selectedIndex = i;
						break;
					}
				}
			}
			else if ( elem.type == "select-multiple" )
			{
				for ( var i = 0; i < elem.options.length; i++ )
				{
					elem.options[i].selected = ( elem.options[i].value == value );
				}
			}
			else if ( elem.type == "hidden" || elem.type == "button" || elem.type == "submit" ) { }
		}
	}
}
/*
function initializePage( event )
{
	$$('.js_dropdown_box h2, .js_dropdown_box_closed h2').each( function( elem_iterator )
	{
		elem_iterator.observe( 'click', expandDropdownBox );
	} );

	$$('.photo_thumb_container').each( function( elem_iterator )
	{
		elem_iterator.observe( 'click', swapProfilePhoto );
	} );
}

function expandDropdownBox( event )
{
	o_this_h2 = event.element();

	o_this_h2.ancestors().each( function( elem_iterator )
	{
		if ( elem_iterator.hasClassName( 'js_dropdown_box' ) )
		{
			elem_iterator.className = 'js_dropdown_box_closed';
			throw $break; // Pretend this is a "break"
		}
		else if ( elem_iterator.hasClassName( 'js_dropdown_box_closed' ) )
		{
			elem_iterator.className = 'js_dropdown_box';
			throw $break; // Pretend this is a "break"
		}
	} );
}
*/

function showHint( field_name )
{
	document.getElementById( 'hint_' + field_name ).className='hintcontainervisible';
}

function hideHint( field_name )
{
	document.getElementById( 'hint_' + field_name ).className='caseta_expl';
}

function effectSlide( object_id )
{
	div_object = document.getElementById( object_id );
	
	if( div_object.style.display == 'none' )
	{
		new Effect.SlideDown( object_id, {queue: {position:'front', scope: object_id, limit:1} });
		if( object_id == 'new_basic_search' )
		{
			document.getElementById( 'basic_search_iframe' ).className = 'visible';
		}
	}
	else
	{
		new Effect.SlideUp( object_id, {queue: {position:'end', scope: object_id, limit:1} });
		if( object_id == 'new_basic_search' )
		{
			document.getElementById( 'basic_search_iframe' ).className = 'hidden';
		}
	}
}

function effectSlideUp( object_id )
{
	new Effect.SlideUp( object_id );
}

function effectSlideDown( object_id )
{
	new Effect.SlideDown( object_id );
}

function effectVisible( object_id )
{
	document.getElementById( object_id ).className = 'visible';
}

function effectHide( object_id )
{
	document.getElementById( object_id ).className = 'hidden';
}

function effectFade( fade_object_id )
{
	new Effect.Fade( fade_object_id, { duration: .5 } );
}

function effectFadeAppear( fade_object_id, appear_object_id )
{
	new Effect.Fade(fade_object_id, {duration: .5, queue: {position:'front', scope: fade_object_id, limit:2} });
	new Effect.Appear(appear_object_id, {duration: .5, queue: {position:'end', scope: fade_object_id, limit:2} });
}

function effectTransition( fade_object_id, appear_object_id )
{
	new Effect.Fade(fade_object_id, {duration: 1, queue: {position:'front', scope: fade_object_id, limit:2} });
	new Effect.Appear(appear_object_id, {duration: 1, queue: {position:'end', scope: appear_object_id, limit:2} });
}

function effectSlideAppear( slide_object_id, appear_object_id )
{
	new Effect.SlideUp( slide_object_id, {queue: {position:'end', scope: slide_object_id, limit:1} });
	new Effect.Appear(appear_object_id, {duration: .5, queue: {position:'end', scope: slide_object_id, limit:2} });
}

function effectSlideUpDown( fade_object_id, appear_object_id ) {
	if (fade_object_id) {
		new Effect.SlideUp( fade_object_id, { queue: {position:'end', scope: fade_object_id} } );
	}	
	
	if (appear_object_id) {
		new Effect.SlideDown( appear_object_id, { queue: {position:'end', scope: fade_object_id} } );
	}	
}

function popup( url, width, height ) 
{
	window.open(url,'popup','width='+width+',height='+height+',scrollbars=yes,resizable=yes,toolbar=no,directories=no,menubar=no,status=no,left=100,top=100');
	return false;
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}
function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function posRight() {return posLeft()+pageWidth();}
function posBottom() {return posTop()+pageHeight();}

var button_has_been_clicked = false;

function getHasFormBeenSubmitted()
{
	return button_has_been_clicked;
}

function setHasFormBeenSubmitted()
{
	button_has_been_clicked = true;
}

function submitFormOnlyOnce( form_name )
{
	if( getHasFormBeenSubmitted() )
	{
		alert( 'This form is currently being submitted.  Please continue to hold.' );
	}
	else
	{
		document.forms[ form_name ].submit();
		setHasFormBeenSubmitted();
	}
}

function swapUserImage( direction, total_number_of_images )
{
	current_image_number = parseFloat( document.getElementById('current_image').value );
	//total_number_of_images = <?= count( $a_photos ) ?>;

	if( direction == 'next' )
	{
		next_image_number = current_image_number + 1;
	}
	else
	{
		next_image_number = current_image_number - 1;
	}
	//document.getElementById('photo_' + current_image_number).className='hidden';
	//document.getElementById('photo_' + next_image_number).className='visibile';
	effectFadeAppear( 'photo_' + current_image_number, 'photo_' + next_image_number );
	document.getElementById('current_image').value = next_image_number;
	
	if( document.getElementById('current_image').value == 1 )
	{
		document.getElementById('image_previous').className='hidden';
	}
	else
	{
		document.getElementById('image_previous').className='visible';
	}
	
	if( document.getElementById('current_image').value == total_number_of_images )
	{
		document.getElementById('image_next').className='hidden';
	}
	else
	{
		document.getElementById('image_next').className='visible';
	}
}

function createAjaxObject() 
{
	var ajax_object;

	try
	{	// Try the standard way
		ajax_object = new XMLHttpRequest();

	} catch( trymicrosoft )
	{	// Standard way is a no go!
		try
		{	// Try Microsoft variant 1
			ajax_object = new ActiveXObject( "Msxm12.XMLHTTP" );

		} catch( othermicrosoft )
		{	// Still failure!
			try
			{	// Try Microsoft variant 2
				ajax_object = new ActiveXObject( "Microsoft.XMLHTTP" );

			} catch( failed )
			{	// Well we're just incompetent now, aren't we
				ajax_object = null;

			}
		}
	}

	if( ajax_object == null )
	{
		alert( "Error creating request object!" );
	}

	return ajax_object;
}

function sendAjaxRequest( ajax_include, parameters, send_result_to ) 
{
	var ajax = createAjaxObject();
	var resistCache = new Date().getTime();
	
    url = ajax_include;
	ajax.open( 'get', url, true );
    ajax.onreadystatechange = function () { handleAjaxResponse( ajax ); };
    ajax.send( null );
}

function handleAjaxResponse( ajax )
{
    if( ajax.readyState == 4 )
    {
		if( ajax.status == 200 )
		{
			var response = ajax.responseText;
			var update = new Array();
			
			if( response.indexOf( '|' ) != -1 )
			{
				update = response.split( '|' );
				function_return = update[0] + '( "' + escape( update[1] ) +  '" )';
				eval( function_return );
			}
		}
		else
		{
			alert( "Error: Requested URL can not be accessed." );
		}
    }
}

// This controls the checkbox and css class for the CreateCalendar service.
function onOff( cellName, cancelBox, limit )
{
	var currentValue = document.getElementById( 'cal' + cellName ).checked;

	if( limit == 'yes' )
	{
		var oldClickCount = parseInt( document.getElementById( 'clicked_count' ).value );
		var curClickCount = 0;
	
		// Cancel the dual processing done by the onclick and actual checkbox click
		if( cancelBox == 'yes' )
		{
			if( currentValue == true )
			{
				document.getElementById( 'cal' + cellName ).checked = false;
			}
			else
			{
				document.getElementById( 'cal' + cellName ).checked = true;
			}
		}
		else
		{
			if( currentValue == true )
			{
				document.getElementById( 'cal' + cellName ).checked = false;
				document.getElementById( 'cell' + cellName ).className = 'caltd';
				curClickCount = oldClickCount - 1;
				document.getElementById( 'clicked_count' ).value = curClickCount;
				
				if( oldClickCount == 7 && curClickCount == 6 )
				{
					// Activate all the boxes
					for( i = 0; i < 7; i++)
					{
						for( j = 0; j < 8; j++)
						{
							document.getElementById( 'cal' + i + j ).disabled = false;
						}
					}
					
					document.getElementById( 'cal_limit_explain' ).style.color = 'black';
					document.getElementById( 'cal_limit_explain' ).style.fontWeight = 'normal';
				}
			}
			else
			{
				if( oldClickCount != 7 )
				{
					document.getElementById( 'cal' + cellName ).checked = true;
					document.getElementById( 'cell' + cellName ).className = 'seltd';
					curClickCount = oldClickCount + 1;
					document.getElementById( 'clicked_count' ).value = curClickCount;
		
					if( curClickCount == 7 )
					{
						// Deactivate all unchecked boxes
						for( i = 0; i < 7; i++)
						{
							for( j = 0; j < 8; j++)
							{
								if( document.getElementById( 'cal' + i + j ).checked == false )
								{
									document.getElementById( 'cal' + i + j ).disabled = true;
								}
							}
						}
						
						document.getElementById( 'cal_limit_explain' ).style.color = 'red';
						document.getElementById( 'cal_limit_explain' ).style.fontWeight = 'bold';
					}
				}
			}
		}
	}
	else
	{
		if( currentValue == true )
		{
			document.getElementById( 'cal' + cellName ).checked = false;
			document.getElementById( 'cell' + cellName ).className = 'caltd';
		}
		else
		{
			document.getElementById( 'cal' + cellName ).checked = true;
			document.getElementById( 'cell' + cellName ).className = 'seltd';
		}
	}
}

var myrules = {
	'.has_hint' : function(el){
		el.onfocus = function(){
			showHint( el.id );
		}
		el.onblur = function(){
			hideHint( el.id );
		}
	},
	
	'.has_hint_mouseover' : function(el){
		
		el.onmouseover = function(){
			showHint( el.id );
		}
		
		el.onmouseout = function(){
			hideHint( el.id );
		}
	}
};

Behaviour.register(myrules);


function pleaseWait()
{
	pleaseWait('');
}

// for home page search
function pleaseWait( susan_url )
{
	if (!susan_url) { susan_url = 'img/susansearch.gif'; }
	box_height = 400;
	box_width = 400;
	window_height = ( pageHeight() / 2 ) - ( box_height / 2 ) + posTop();
	window_width = ( pageWidth() / 2 ) - ( box_width ) + posLeft();
	document.getElementById("search_status").innerHTML="<table id='searching_box' style=\"position:absolute; top:" + window_height + "px; left:" + window_width + "px; z-index: 10000; background-color: white; border: 1px solid black; width: " + box_width + "px; height: " + box_height + "px;\"><tr><td align='center' valign='middle'><br /><br /><br /><br /><div style='color: #c90;font-size: 26px;font-weight:normal;'>Please wait as we run your search...</div><img src=\"" + susan_url + "\"></td></tr></table>";
	fixIESelect( 'searching_box', true );	
}

function fixIESelect(id, state)
{
	var DivRef = document.getElementById(id);
	var IfrRef = document.getElementById('ie_div');
	if(state)
	{
		DivRef.style.display = "block";
		IfrRef.style.width = DivRef.offsetWidth;
		IfrRef.style.height = DivRef.offsetHeight;
		IfrRef.style.top = DivRef.style.top;
		IfrRef.style.left = DivRef.style.left;
		IfrRef.style.zIndex = DivRef.style.zIndex - 1;
		IfrRef.style.display = "block";
	}
	else
	{
		DivRef.style.display = "none";
		IfrRef.style.display = "none";
	}
}

// Timer alerting support staff to timing out of checked out tickets
function checkoutTimer( count )
{
	if( count == 60 )
	{
		var stopTime = new Date();
		var reset_timer = confirm( "This ticket will be checked in due to inactivity in approximately one minute.  If you need additional time, click \"OK\" to reset this timer (no unsaved changes will be lost).  If you dismiss this dialog, or don't hit \"OK\" within the next minute, be aware that other members of the support staff will be able to access this ticket and prevent you from saving any changes." );
		var restartTime = new Date();
		count -= Math.ceil( ( restartTime.getTime() - stopTime.getTime() ) / 1000 );

		if( reset_timer && count > 0 )
		{
			document.getElementById("checkout_reload").value = "yes";
			document.form.submit();
		}
	}

	if( count > 0 )
	{
		count--;
		window.setTimeout( "checkoutTimer( " + count + " )", 1000 );
	}
	else
	{
		document.getElementById("checkin_box").style.display = "block";
		return 0;
	}
}

// Jumps from the supplied phone field to the next sequential one
function phoneJump( field_root, field_number )
{
	var target_number = parseInt( field_number ) + 1;
	var target_field = document.getElementById( field_root + "_" + target_number );
	var field_value = document.getElementById( field_root + "_" + field_number ).value;
	var field_length = field_value.length;
	
	if ( field_length == 3 && target_field.value == "" )
	{
		target_field.select();
	}
}

// Makes sure the supplied phone fields are numeric and of correct lengths
function phoneValidate( field_root )
{
    
	var valid_chars = "0123456789";
	var field_value, str_char, error_flag = "no";
	
	for( i = 1; i <= 3; i++ )
	{
		field_value = document.getElementById( field_root + "_" + i ).value;

		for( j = 0; j < field_value.length; j++ )
		{
			str_char = field_value.charAt( j );
			if( valid_chars.indexOf( str_char ) == -1 && error_flag == "no" )
			{
				error_flag = i;
			}
		}
	
		if( ( i != 3 && field_value.length != 3 && error_flag == "no" ) || ( i == 3 && field_value.length != 4 && error_flag == "no" ) )
		{
			error_flag = "length";
		}
	}
	
	if( error_flag != "no")
	{
		if( error_flag == "length" )
		{
			document.getElementById( field_root + "_length_error" ).style.display = "block";
			document.getElementById( field_root + "_num_error" ).style.display = "none";
		}
		else
		{
			document.getElementById( field_root + "_num_error" ).style.display = "block";
			document.getElementById( field_root + "_length_error" ).style.display = "none";
		}
		
		return 0;
	}
	else
	{
		document.getElementById( field_root + "_length_error" ).style.display = "none";
		document.getElementById( field_root + "_num_error" ).style.display = "none";
		
		return 1;
	}
}

// Jumps from the supplied ssn field to the next sequential one
function ssnJump( field_root, source_field_id, target_field_id )
{
	var target_field = document.getElementById( field_root + "_" + target_field_id );
	var field_value = document.getElementById( field_root + "_" + source_field_id ).value;
	var field_length = field_value.length;

	if ( source_field_id == 'first' )
	{
    	if ( field_length == 3 && target_field.value == "" )
    	{
    		target_field.select();
    	}
	}
	
	if ( source_field_id == 'second' )
	{
    	if ( field_length == 2 && target_field.value == "" )
    	{
    		target_field.select();
    	}
	}
}

// mypage referral 
function toggleReferral( fromWhom )
{
    
    if ( fromWhom == 'image')
    {
        effectFadeAppear( 'referral_image', 'referral_invite' );
        document.getElementById( 'referral_thanks' ).style.display = 'none';
	}
    
    else if ( fromWhom == 'cancel')
    {
        effectFadeAppear( 'referral_invite', 'referral_image' );
        document.getElementById( 'referral_thanks' ).style.display = 'none';
    }	    
    
    else if ( fromWhom == 'sent')
    {
        effectFadeAppear( 'referral_invite', 'referral_image' );
        document.getElementById( 'referral_thanks' ).style.display = 'block';
		document.getElementById( 'invite_response' ).innerHTML = 'Invite sent successfully!';
	}
}

//mypage referral, version 2
function toggleMyReferral( fromWhom, is_success )
{
    if ( fromWhom == 'mainprofile_panel_sent')
	{
		// Empty the queue, cancel all pending effects (referral_thanks may still be waiting to disappear)
		var queue = Effect.Queues.get('mainprofile panel invite');
		queue.each( function (e) { e.cancel(); } );
		
    	//The following effect is equivlent to adding $("referral_thanks").setOpacity(0.0); to the Queue (hides referral_thanks if it hadn't disappeared yet)
    	new Effect.Opacity( 'referral_thanks', {duration: 0, from: 0.0, to: 0.0, queue: {position:"end", scope: 'mainprofile panel invite'} });

    	new Effect.BlindUp( 'referral_invite', {duration: 2, queue: {position:"end", scope: 'mainprofile panel invite'} });
    	if (!G_has_invited)
    	{
    		new Effect.BlindUp( 'myreferralpanelstatsnone', {duration: 1, queue: {position:"with-last", scope: 'mainprofile panel invite'} });
    	}
	}

    else if ( fromWhom == 'mainprofile_panel_response')
    {
		if (is_success)
		{
			if (!G_has_invited)
			{
				G_has_invited = true;
				new Effect.BlindDown( 'myreferralpanelstats', {delay: 0.5, duration: 1, queue: {position:"end", scope: 'mainprofile panel invite'} });
			}
			
			new Effect.Opacity( 'myreferral_invites_sent', {delay: 1, duration: 1, from: 1.0, to: 0.0, afterFinish: referralCountIncrement, queue: {position:"end", scope: 'mainprofile panel invite'} });
			new Effect.Opacity( 'myreferral_invites_sent', {delay: 0, duration: 1, from: 0.0, to: 1.0, queue: {position:"end", scope: 'mainprofile panel invite'} });
			new Effect.Opacity( 'referral_thanks', {delay: 0, duration: 1, from: 0.0, to: 1.0, queue: {position: "with-last", scope: 'mainprofile panel invite'} });
		}
		else
		{
			new Effect.Opacity( 'referral_thanks', {delay: 1, duration: 1, from: 0.0, to: 1.0, queue: {position: "end", scope: 'mainprofile panel invite'} });
			if (!G_has_invited)
			{
				new Effect.BlindDown( 'myreferralpanelstatsnone', {delay: 1, duration: 1, queue: {position:"with-last", scope: 'mainprofile panel invite'} });
			}
			
		}
		new Effect.BlindDown( 'referral_invite', {delay: 1, duration: 2, queue: {position:"end", scope: 'mainprofile panel invite'} });
		new Effect.Opacity( 'referral_thanks', {delay: 5, duration: 2, from: 1.0, to: 0.0, queue: {position:"end", scope: 'mainprofile panel invite'} });
	}
}

function referralCountIncrement(e)
{
    	var referral_count = parseInt($('myreferral_invites_sent').innerHTML);
    	referral_count++;
    	$('myreferral_invites_sent').innerHTML = "" + referral_count;
    	
    	//Clear the e-mail address
    	$('invitation_email').value = '';
}

function petExpand( check_id, parent_array_index, form_obj )
{
	pet_check = document.getElementById( "check_group_" + check_id );

	if( pet_check.checked == true )
	{
		document.getElementById( "pet_group_" + check_id ).style.display = "block";
	}
	else
	{
		// Un-check all subcategories
		if (form_obj && window.G_types_of_pets_hierarchy_tree && G_types_of_pets_hierarchy_tree[parent_array_index])
		{
			var success = true;
			
			for (var i = 0; i < G_types_of_pets_hierarchy_tree[parent_array_index].length; i++)
			{
				var elem = form_obj.elements['a_types_of_pets_id[' + G_types_of_pets_hierarchy_tree[parent_array_index][i] + ']'];
				if (elem)
				{
					elem.checked = false;
				}
				else
				{
					success = false;
				}
			}

			// Hide all subcategories, if we successfully un-checked them
			if (success)
			{
				document.getElementById( "pet_group_" + check_id ).style.display = "none";
			}
		}
	}
}

function patricide()
{
	var parent = this.parentNode;
	while( parent.firstChild )
	{
		parent.removeChild( parent.firstChild );
	}
	parent.parentNode.removeChild( parent );
}

function premeditate()
{
	var spans = document.getElementsByTagName( "span" );

	for( var i = 0; i < spans.length; i++ )
	{
		if( spans[ i ].className == "patricide" )
		{
			spans[ i ].onclick = patricide;
		}
	}
	
	/* now handled server-side
	
	var inputs = document.getElementsByTagName( "input" );

	for( var i = 0; i < inputs.length; i++ )
	{
		if( inputs[ i ].id == "a_rate_amount_min" || inputs[ i ].id == "a_rate_amount_max" )
		{
			inputs[ i ].onclick = checkRange;
		}
	}
	
	*/
}

function checkRange()
{
	var parentDiv	= this.parentNode;
	var a_inputs	= parentDiv.getElementsByTagName( "input" );
	
	for( var i = 0; i < a_inputs.length; i++ )
	{
		if( a_inputs[i].id == "a_rate_amount_min" )
		{
			var rate_min = a_inputs[i];
		}
		
		if( a_inputs[i].id == "a_rate_amount_max" )
		{
			var rate_max = a_inputs[i];
		}
	}
		
	if( parseInt( rate_min.value ) > parseInt( rate_max.value ) && rate_min.value != "" && rate_max.value != "" )
	{
		alert( "Your maximum rate must be greater than or equal to your minimum rate.  Erroneous rate combos will not be saved." );
		return false;
	}
}

function tabSelect( selected_li_id )
{
	//Get the name of the id prefix for this tabber's LIs and DIVs
	var prefix_key_length = selected_li_id.indexOf('_li');
	var prefix_key = selected_li_id.substr(0 , prefix_key_length);

	//Update the tabs and folders
	for ( var i=0;  li_elem = document.getElementById(prefix_key+"_li"+i);  i++ )
	{
		var li_class = '', folder_class = '';
		if (li_elem.id == selected_li_id) //Select this tab
		{
			li_class = 'selected';
			folder_class = 'tabber_folder';
		}
		else //Deselect this tab
		{
			li_class = '';
			folder_class = 'hidden';
		}
		
		li_elem.className = li_class;
		document.getElementById(prefix_key+"_folder"+i).className = folder_class;
	}
}

// tabGetActiveTabIndex( prefix_key ) - Returns the active tab .... 0, 1, 2, etc
// If it cannot be found, -1 is returned.
function tabGetActiveTabIndex( prefix_key )
{
	for ( var i=0;  li_elem = document.getElementById(prefix_key+"_li"+i);  i++ )
	{
		li_class = li_elem.className;
		if (li_class == 'selected')
		{
			return i;
		}
	}
	
	//It was not found
	return -1;
}

function sidebar_click( o_active_a_tag )
{
	var div_float_clear = null;
	var li_active = o_active_a_tag.ancestors()[0];
	var active_id = o_active_a_tag.readAttribute( 'href' );

	li_active.ancestors().each( function( elem_iterator )
	{
		if ( elem_iterator.hasClassName( 'float_clear') )
		{
			div_float_clear = elem_iterator;
			throw $break; // Pretend this is a "break"
		}
	} );

	if ( !div_float_clear || !active_id )
	{
		// We couldn't find the sidebar container.  Abort!
		return;
	}

	// Drop the preceding # from the href and grab the rest
	active_id = active_id.substr(1);

	// Use Prototype to iterate over the LIs and highlight the active one
	div_float_clear.select( '.auto_sidebar ul li' ).each( function( li_iterator )
	{
		is_active = ( li_iterator == li_active );

		li_iterator.removeClassName( 'current' );
		if ( is_active )
		{
			li_iterator.addClassName( 'current' );
		}
	} );

	// Use Prototype to iterate over the DIVs and reveal the active one
	div_float_clear.select( '.auto_sidebar_page' ).each( function( div_iterator )
	{
		if ( div_iterator.identify() == active_id )
		{
			div_iterator.removeClassName( 'hidden' );
		}
		else if ( !div_iterator.hasClassName( 'hidden' ) )
		{
			div_iterator.addClassName( 'hidden' );
		}
	} );
}

function getContainerNeighbor( o_action_object, container_class, neighbor_style )
{
	o_action_object = Element.extend( o_action_object );

	var o_container = null;
	var o_neighbor = null;

	// Use PrototypeJS to find the container
	o_action_object.ancestors().each( function( elem_iterator )
	{
		if ( elem_iterator.hasClassName( container_class ) )
		{
			o_container = elem_iterator;
			throw $break; // Pretend this is a "break"
		}
	} );

	if ( ! o_container )
	{
		return null; // Trouble... Abort
	}

	// Use PrototypeJS to find a child of the container
	o_container.select( neighbor_style ).each( function( elem_iterator )
	{
		o_neighbor = elem_iterator;
		throw $break; // Pretend this is a "break"
	} );
	
	return o_neighbor;
}

function clearIMGClass( o_img )
{
	if ( o_img.className )
	{
		o_img.className = "";
	}
}

function setErrorImage( o_img, alt_src )
{
	o_img.onerror = null; // Clear the event
	o_img.src = alt_src;
}

function setUserPhotoErrorImage( o_img, is_current_user, use_large_image )
{
	var photo_missing_url = '/images/avatar/photo-missing-small.jpg';
	var photo_loading_url = '/images/avatar/photo-missing-small.jpg';

	if ( use_large_image )
	{
		photo_missing_url = '/images/avatar/photo-missing-large.jpg';
		photo_loading_url = '/images/avatar/photo-missing-large.jpg';
	}

	if ( is_current_user )
	{
		setErrorImage( o_img, photo_loading_url );
	}
	else
	{
		setErrorImage( o_img, photo_missing_url );
	}
}

function constrainImage( o_img, max_width, max_height )
{	
	if ( o_img.width && o_img.height)
	{
		var original_width = o_img.width,
			original_height = o_img.height;

		if ( original_width <= max_width && original_height <= max_height )
		{
			return; // The image is small enough... do nothing
		}

		var new_height, new_width;

		if ( original_width > original_height )
		{
			new_width = max_width;
			reduction = max_width / original_width;
			new_height = Math.round ( original_height * reduction );
		}
		else
		{
			new_height = max_height;
			reduction = max_height / original_height;
			new_width = Math.round ( original_width * reduction );
		}
		
		if ( new_height )
		{
			o_img.width = new_width;
			o_img.height = new_height;
		}
		
	}
}

function displayLightbox( boxID, width )
{
	var lightBox	= document.getElementById( "lightbox_" + boxID );
	var boxBg		= document.getElementById( "lightbox_bg" );
	var docDivs		= document.getElementsByTagName( "div" );
	var ytContainer	= document.getElementById( "youtube_container" );
	var ytHTML		= document.getElementById( "youtube_html" );
	var body		= document.getElementsByTagName('body');

	for( var i = 0; i < docDivs.length; i++ )
	{
		if( docDivs[ i ].id.indexOf( "lightbox_" ) >= 0 )
		{
			if( docDivs[ i ] != lightBox )
			{
				docDivs[ i ].className = "lightbox_off";
			}
		}
	}

	var newMargin				= ( 830 - width ) / 2;
	lightBox.className			= "lightbox_on lightbox_select";
	lightBox.style.width		= width + "px";
	lightBox.style.marginLeft 	= newMargin.toFixed( 0 ) + "px";
	boxBg.className				= "lightbox_bg_on";
	
	body[0].className = "lightbox_no_select";
	
	if( boxID == "video" && ytContainer )
	{
		ytContainer.innerHTML = ytHTML.innerHTML;
		setTimeout( "document.getElementById( 'lightbox_bg' ).style.zIndex = 999", 500 ); // Fix insane youtube/firefox bug
	}

	// Scroll to the box in pre-IE-7
	if( !window.XMLHttpRequest )
	{
		scroll( 0, 200 );
	}

}

function clearLightbox()
{
	var docDivs		= document.getElementsByTagName( "div" );
	var boxBg		= document.getElementById( "lightbox_bg" );
	var ytContainer	= document.getElementById( "youtube_container" );
	var body		= document.getElementsByTagName('body');

	for( var i = 0; i < docDivs.length; i++ )
	{
		if( docDivs[ i ].id.indexOf( "lightbox_" ) >= 0 )
		{
			docDivs[ i ].className = "lightbox_off lightbox_no_select";
		}
	}
	
	boxBg.className = "lightbox_bg_off";

	if( ytContainer )
	{
		ytContainer.innerHTML = "";
	}
	
	body[0].className = "";
	

}

function navExpand( expand_name, headerObj )
{
	if( expand_name )
	{
		var listItems = document.getElementsByTagName( "li" );
	
		for( var i = 0; i < listItems.length; i++ )
		{
			if( listItems[ i ].id.indexOf( expand_name ) >= 0 )
			{
				if( listItems[ i ].style.display == "none" )
				{
					listItems[ i ].style.display = "block";
					headerObj.style.color = "#333333";
				}
				else
				{
					listItems[ i ].style.display = "none";
					headerObj.style.color = "#37639E";
				}
			}
		}
	}
}

function hoverOpaque( element )
{
	element = Element.extend( element );

	if ( element.hasClassName( "translucent" ) )
	{
		element.removeClassName( "translucent" );
		element.addClassName( "opaque" );
	}
	else
	{
		element.removeClassName( "opaque" );
		element.addClassName( "translucent" );
	}
}

function getParentForm( input_elem )
{
	//Bubble up until we find the form that contains the input that raised the event
	o_input_elem = Element.extend( input_elem );

	if ( o_input_elem.tagName == 'FORM' )
	{
		return o_input_elem;
	}

	o_form_elem = null;
	o_input_elem.ancestors().each( function( elem_iterator )
	{
		if ( elem_iterator.tagName == 'FORM' )
		{
			//we found the form!
			o_form_elem = elem_iterator;
			throw $break; // Pretend this is a "break"
		}
	} );

	return o_form_elem;
}

function submitPagination( input_elem )
{
	if ( getHasFormBeenSubmitted() ) { return; }

	o_form_elem = getParentForm( input_elem );
	if ( ! o_form_elem ) { return; }

	var last_page = 1;
	o_form_elem.select('.lastpagenum').each( function( elem_iterator )
	{
		last_page = parseInt( elem_iterator.innerHTML );
		throw $break; // Pretend this is a "break"
	} );

	o_page_elem = Element.extend( o_form_elem.elements['page'] );
	var go_page = parseInt( o_page_elem.value );
	if ( isNaN( go_page ) )
	{
		o_page_elem.addClassName( 'bad_field' );
		o_page_elem.focus();
		alert( "Please enter a valid page number." );
		o_page_elem.removeClassName( 'bad_field' );
		return;
	}
	else if ( go_page > last_page )
	{
		o_page_elem.value = last_page;
	}

	o_form_elem.submit();
	setHasFormBeenSubmitted();
}

function submitSort( input_elem )
{
	if ( getHasFormBeenSubmitted() ) { return; }

	o_input_elem = Element.extend( input_elem );

	if ( $F( o_input_elem.identify() ) > 0 )
	{
		o_form_elem = getParentForm( o_input_elem );
		if ( o_form_elem )
		{
			o_form_elem.submit();
			setHasFormBeenSubmitted();
		}
	}
}

function scrollProfileThumbs( direction )
{
	var thumbContainer	= document.getElementById( 'photo_thumbs_row' );
	var maxScroll		= parseInt( document.getElementById( 'thumb_scroll_max' ).value );
	var curPosition		= parseInt( document.getElementById( 'thumb_scroll_cur' ).value );
	var leftArrow		= document.getElementById( 'photo_scroll_left' );
	var rightArrow		= document.getElementById( 'photo_scroll_right' );
	
	if( direction == "right" )
	{
		thumbContainer.style.top = ( curPosition - 64 ) + "px";
		document.getElementById( 'thumb_scroll_cur' ).value = curPosition - 64;
	}
	else if( direction == "left" )
	{
		thumbContainer.style.top = ( curPosition + 64 ) + "px";
		document.getElementById( 'thumb_scroll_cur' ).value = curPosition + 64;
	}

	curPosition = parseInt( document.getElementById( 'thumb_scroll_cur' ).value );

	if( curPosition + maxScroll == 0 )
	{
		rightArrow.className = "photo_scroll_right_dim";
		rightArrow.onclick = "";
	}
	else
	{
		rightArrow.className = "photo_scroll_right";
		rightArrow.onclick = function() { scrollProfileThumbs( 'right' ); };
	}

	if( curPosition == 0 )
	{
		leftArrow.className = "photo_scroll_left_dim";
		leftArrow.onclick = "";
	}
	else
	{
		leftArrow.className = "photo_scroll_left";
		leftArrow.onclick = function() { scrollProfileThumbs( 'left' ); };
	}

}

function swapProfilePhoto()
{
	var photoID			= this.id.substring( 6 );
	var photoContainer	= document.getElementById( 'photo_container' );
	var thumbContainer	= document.getElementById( 'photo_thumbs_row' );
	var aPhotos			= photoContainer.getElementsByTagName( 'img' );
	var aThumbs			= thumbContainer.getElementsByTagName( 'div' );
	
	for( var i = 0; i < aPhotos.length; i++ )
	{
		if( aPhotos[ i ].id == "photo_" + photoID )
		{
			aPhotos[ i ].className = "visible_inline";
		}
		else
		{
			aPhotos[ i ].className = "hidden";
		}
	}
	
	for( var i = 0; i < aThumbs.length; i++ )
	{
		if( aThumbs[ i ].id == "thumb_" + photoID )
		{
			aThumbs[ i ].className = "photo_thumb_container_current";
			aThumbs[ i ].onclick = "";
		}
		else
		{
			aThumbs[ i ].className = "photo_thumb_container";
			aThumbs[ i ].onclick = swapProfilePhoto;
		}
	}

}

function getContainerNeighbor( o_action_object, container_class, neighbor_style )
{
	return getContainerNeighbors( o_action_object, container_class, neighbor_style, true )
}

function getContainerNeighbors( o_action_object, container_class, neighbor_style, return_first_only )
{
	o_action_object = Element.extend( o_action_object );

	var o_container = null;

	// Use PrototypeJS to find the container
	o_action_object.ancestors().each( function( elem_iterator )
	{
		if ( elem_iterator.hasClassName( container_class ) )
		{
			o_container = elem_iterator;
			throw $break; // Pretend this is a "break"
		}
	} );

	if ( ! o_container )
	{
		return null; // Trouble... Abort
	}

	// Use PrototypeJS to find a child of the container
	o_neighbor_collection = o_container.select( neighbor_style );

	if ( !return_first_only )
	{
		return o_neighbor_collection;
	}

	var o_neighbor = null;
	o_neighbor_collection.each( function( elem_iterator )
	{
		o_neighbor = elem_iterator;
		throw $break; // Pretend this is a "break"
	} );
	
	return o_neighbor;
}



function addLanguage()
{
	var selectContainer	= document.getElementById( 'language_select_container' );
	var selectSource	= document.getElementById( 'language_select_source' );
	var selectAdd		= document.getElementById( 'language_select_add' );

	var newSelect		= document.createElement( 'div' );
	newSelect.className = "language_row";

	newSelect.innerHTML = selectSource.innerHTML;
	selectContainer.insertBefore( newSelect, selectAdd );
}

function cloneSelect( input_elem )
{
	o_cloneable_select_row = getContainerNeighbor( input_elem, 'clona_caseta_select', '.clon_rand_select' );
	
	if ( ! o_cloneable_select_row ) { return; } //Couldn't find the cloneable prototype
 
	new_cloned_select_row = new Element( 'div', { 'class': 'clon_rand_select' } );
	new_cloned_select_row.update( '<br />'+o_cloneable_select_row.innerHTML + '<br /><a href="#" onclick="javascript: removeSelect(this); return false;" class="action">Sterge</a><br />' );

	// Cycle through all visible cloned select rows, and add this one after the last one
	o_last_cloned_select_row = null;
	o_cloned_select_row_collection = getContainerNeighbors( input_elem, 'clona_caseta_select', '.clon_rand_select' );
	o_cloned_select_row_collection.each( function( elem_iterator )
	{
			o_last_cloned_select_row = elem_iterator;
	} );
	if ( ! o_last_cloned_select_row ) { return; } //Couldn't find the last cloned select row

	o_last_cloned_select_row.insert( { after: new_cloned_select_row } );
}

function removeSelect(o) {  
  clon_rand_select = o.parentNode
  clon_rand_select.parentNode.removeChild(clon_rand_select)
  //alert(o.parentNode.parentNode.removeChild(o.parentNode))
}