//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================

// debug func to show props of obj
function  printProps(obj, objName) {
  var output = "" ;
  for (var prop in obj) {
    output += objName + "." + prop + " = " + obj[prop] + "\n" ;
  }
  return output ;
}

//=======================================================================================================
// Functions used by the update user info
//=======================================================================================================

function init_tab()
{
	current_tab = document.getElementById('account_tab');
    showTab(document.getElementById('tabgroup').childNodes[0], 'account_tab');

}

function showTab(link, tab)
{
	//var current_tab;
    var links = link.parentNode.childNodes;

    // Reset all tabs
    for(var i = 0; i < links.length; i++)
    {
        links[i].className = 'tab_link';
    }

    link.className = 'tab_link tab_link_active';

    if(link.previousSibling)
    {
        var prev = link.previousSibling;
        prev.className = 'tab_link tab_link_prev';
    }

    links[0].className += ' tab_link_first'; 

	current_tab.style.display	= 'none';
	current_tab			= document.getElementById(tab);
	current_tab.style.display	= 'block';

	//document.getElementById('currentTab').value = tab;
}

//=======================================================================================================
// Functions used by the update user info to hide / show hidden rows
//=======================================================================================================

// Function to Add a social network hidden row
function add_social_network_row()
{
	if(current_row_value >= 10)
	{
		alert('Limit Exceeded');
		return;
	}

	document.getElementById('row_'+current_row_value).style.display = '';
	current_row_value++;
}

// Function to Add a rss hidden row
function add_rss_row()
{
	if(current_rss_row_value >= 10 )
	{
		alert('Limit Exceeded');
		return;
	}

	document.getElementById('rss_row_'+current_rss_row_value).style.display = '';
	current_rss_row_value++;
}


// func to show / hide obj
function expand(param)
{
	if( param )
	{
		param.style.display=(param.style.display=="none")?"block":"none";
	}
}

function expand_by_id(id)
{
	expand(document.getElementById(id));
}

//=======================================================================================================
//
//=======================================================================================================

function advanced_search()
{
	expand_by_id('advanced_search');
	expand_by_id('advanced_search_link');
	expand_by_id('basic_search_link');
}




//=======================================================================================================
// Homepage Animation stuff
//=======================================================================================================
var currentAnimationItem = new Array();
currentAnimationItem[1] = 2;
currentAnimationItem[2] = 2;
currentAnimationItem[3] = 2;

function rotateHomeAnimation( q )
{
	lastItem = animationItems[q][ currentAnimationItem[q] ];
	
	//	Get the next image
	currentAnimationItem[q]++;
	
	animationCount = eval('animationCount_'+ q );

	//	Reset if we've hit the end
	if( currentAnimationItem[q] > animationCount )
	{
		currentAnimationItem[q] = 1;
	}

	showItem	 = animationItems[q][ currentAnimationItem[q] ];

	// image url
	var showItem_IMG;
	showItem_IMG = showItem[0];

	//	Has the box been rendered yet? Place the new img 
	var animation_id	= 'animation-image' + q;
	var image_box_id	= 'image_box_' + q;
	if (document.getElementById( animation_id ))
	{
		setTimeout( "changeOpacity( '"+ animation_id  + "', 10 );", 40 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 9 );",  80 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 8 );",  120 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 7 );",  160 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 6 );",  200 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 5 );",  240 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 4 );",  280 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 3 );",  320 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 2 );",  360 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 1);",   400 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 0);",   440 );
		
		
		//setTimeout( "document.getElementById( '"+ animation_id  + "' ).innerHTML = '<a href="+showItem_URL+"><img src="+showItem_IMG+"></a>';", 450 );
		//setTimeout( "document.getElementById( '"+ image_box_id  + "' ).src = showItem_IMG;",450);
		document.getElementById( image_box_id  ).src = showItem_IMG;
	
		setTimeout( "changeOpacity( '"+ animation_id  + "', 1 );",  460 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 2 );",  500 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 3 );",  540 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 4 );",  580 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 5 );",  620 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 6 );",  660 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 7 );",  700 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 8 );",  740 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 9 );",  780 );
		setTimeout( "changeOpacity( '"+ animation_id  + "', 10);",  820 );
		
	}

	//	Set the slide to change
		setTimeout( "rotateHomeAnimation("+q+")", 6700 );

}
function changeOpacity( id, opacity )
{
	document.getElementById( id ).style.opacity = opacity/10;
	document.getElementById( id ).style.filter = 'alpha(opacity=' +  (opacity*10) + ')';	
}

function applicationSubmitted()
{
	document.getElementById( 'applicationsubmitted' ).value = '1';

}

// if the checkbox has no value then we are adding a review else we are deleting
function updateAssignApplication( applicant_id,reviewer_id)
{
//---------------------------------------------------------------------------------------------------------
//	init variables
//---------------------------------------------------------------------------------------------------------
		review_id = '';

//---------------------------------------------------------------------------------------------------------
//	get variables
//---------------------------------------------------------------------------------------------------------
	field_obj		= applicant_id +'_'+ reviewer_id;
	status_id		= 'status_' +applicant_id ;
	checkbox_field	= 'checkbox_' + field_obj ;

	checkbox_obj	= document.getElementById( checkbox_field );
	
//---------------------------------------------------------------------------------------------------------
// get the review id
//---------------------------------------------------------------------------------------------------------
	if( checkbox_obj )
	{
		review_id = checkbox_obj.value;
	}

//---------------------------------------------------------------------------------------------------------
// confirm user wants to delete the review
//---------------------------------------------------------------------------------------------------------
	if( review_id )
	{
		var continue_delete = false;
		var continue_delete = window.confirm('Are you sure you want to delete this review?');
		if( !continue_delete )
		{
			return continue_delete;
		}
	}

//---------------------------------------------------------------------------------------------------------
// setup te objNumber
//---------------------------------------------------------------------------------------------------------
	objNumber = 0;
	//	Have we used the div object before?
	for( i=1; i<= HTTPObjects.length; i++ )
	{
		if (HTTPObjects[i] == field_obj)
		{
			objNumber = i
		}
	}

	//	Not found?
	if(!objNumber)
	{
		objNumber				= HTTPObjects.length;
		HTTPObjects[objNumber]	= field_obj;
	}

	//	Generate the div object pointer
	//alert(status_id);
	var status_obj = eval( 'document.getElementById("' + status_id + '");' );
	status_obj.innerHTML = '';
	url = '/extranet/assign_application_ajax.php?applicant_id='+applicant_id+'&reviewer_id='+reviewer_id+'&review_id='+review_id;

	eval( 'http' + objNumber + ' = initHTTPObject();' );
	eval( 'if (!http' + objNumber + '){window.location=\'/site/compatibility.php\'}' );
	eval( 'http' + objNumber + '.open("GET", url, true );');
	eval( 'http' + objNumber + '.onreadystatechange = function() { if (http' + objNumber + '.readyState == 4) {updateComplete(http' + objNumber + '.responseText, status_obj,checkbox_obj);}};');
	//eval( 'http' + objNumber + '.onreadystatechange = function() { if (http' + objNumber + '.readyState == 4) {status_obj.innerHTML = http' + objNumber + '.responseText;}};');
	//eval( 'http' + objNumber + '.onreadystatechange = function() { if (http' + objNumber + '.readyState == 4) {}};');
	eval( 'http' + objNumber + '.send(null);');

}

//*********************************************************************************************************
//		Function:	updateComplete()
//---------------------------------------------------------------------------------------------------------
//	What it does:	updates status box + 
//					if adding a review, updates form checkbox value with the id of the new review 
//					if deleting a review, updates form checkbox value with 0
//*********************************************************************************************************
function updateComplete(response_string,status_obj,checkbox_obj)
{
	var response_obj = eval('(' + response_string + ')'); 
	
	status_obj.innerHTML = response_obj.status;
	if( checkbox_obj)
	{
		if( response_obj.inserted_article_id )
		{
			checkbox_obj.value= response_obj.inserted_article_id;
		}
		else
		{
			checkbox_obj.value = ''; 
		}
	}

}
/*
function getTabs()
{
	var tabs = new Array('account_tabz','basic_info_tab','section1_tab','education_work_tab','section2_tab','section3_tab','section4_tab','references_tab','resume_tab','rate_tab','reviewer_notes_tab' );
	return tabs;
}*/
function aplicationPrintableView()
{
	$('#tabs .update_tab').css({'display':'block'});
	//alert(current_tab.id);
	/*var tabs = getTabs();
	
	for ( key in tabs )
	{
		if( current_tab.id != tabs[key])
		{

			var tabObj = document.getElementById( tabs[key] );
			if( tabObj )
			{
				tabObj.style.display = "block";
			}
		}
	}*/
	resetPrintTabLinks();
	setupPrintHeaders( 'none');
}

function aplicationTabView()
{
	$('#tabs .update_tab').css({'display':'none'});
	$('#tabs #'+current_tab.id).css({'display':'block'});

	/*var tabs = getTabs();

	for ( key in tabs )
	{
		if( current_tab.id != tabs[key])
		{

			var tabObj = document.getElementById( tabs[key] );
			if( tabObj )
			{
				tabObj.style.display = "none";
			}
		}
	}*/

	resetPrintTabLinks();
	setupPrintHeaders( 'inline');

}

function resetPrintTabLinks()
{
	expand_by_id('anchor_tab_view');
	expand_by_id('anchor_printable_view');
}

function setupPrintHeaders(div_display )
{
	var header_display = '';

	// if div_display = none, then the print header display style should be block 
	if( div_display == 'none' )
	{
		header_display = 'block';
	}
	else
	{
		header_display = 'block';
	}

//---------------------------------------------------------------------------------------------------------
//	show / hide tab titles 
//---------------------------------------------------------------------------------------------------------
	var titles = document.getElementsByName('tab_title');
	for ( key in titles )
	{
		var tabObj = titles[key]  ; // document.getElementById( titles[key] );
		if( tabObj.style )
		{
			tabObj.style.display = div_display;
		}
	}

//---------------------------------------------------------------------------------------------------------
//	show / hide print display headers 
//---------------------------------------------------------------------------------------------------------
	var titles = document.getElementsByName('print_view_tab_title');
	for ( key in titles )
	{
		var tabObj = titles[key]  ; // document.getElementById( titles[key] );
		if( tabObj.id )
		{
			tabObj.style.display = header_display;
		}
	}

}

function toggle_item( id)
{
	var div_id = 'div_activity_full_description_'+id;
	var link_id = 'link_activity_full_description_'+id;
	$('#'+div_id).css('display','block');
	$('#'+link_id).addClass('activities_link_active');
}