﻿function PhPressReleaseManager()
{
	this.m_oAjax			= null;
	this.m_oTargetElement	= null;
	
	this.m_oYearFilter		= null;
	this.m_oMonthFilter		= null
	
	this.m_strSearchPage	= '';
	this.InitObject();
}

PhPressReleaseManager.prototype.InitObject = function()
{
	this.m_oAjax = new PhAjaxJson();
}

PhPressReleaseManager.prototype.SetTargetElement = function( target_element )
{
	this.m_oTargetElement = target_element;
}

PhPressReleaseManager.prototype.SetYearFilter = function( elm )
{
	this.m_oYearFilter = elm;
}

PhPressReleaseManager.prototype.SetMonthFilter = function( elm )
{
	this.m_oMonthFilter = elm;
}

PhPressReleaseManager.prototype.SetSearchPage = function( search_page )
{
	this.m_strSearchPage = search_page;
}

PhPressReleaseManager.prototype.Search = function()
{		
	var year			= 'year='+this.m_oYearFilter.value;
	var month			= 'month='+this.m_oMonthFilter.value;
	var query_string	= year+'&'+month;

	var me = this;
	var cb = function( obj ){me.DisplayResults( obj ); };
	
	this.m_oAjax.SendRequest( this.m_strSearchPage, query_string, null, cb );
}

PhPressReleaseManager.prototype.ClearResults = function( obj )
{
	var i = 0;
	
	for( i = this.m_oTargetElement.options.length-1; i >= 0; i-- )
	{
		if( g_oClient.GetBrowser() == PhClient.IE )
			this.m_oTargetElement.options.remove(i);
		else
			this.m_oTargetElement.removeChild(this.m_oTargetElement.childNodes[i]);
	}
}

PhPressReleaseManager.prototype.DisplayResults = function( obj )
{
	var i		= 0;
	var option	= null;
	var pr		= null;
	
	this.ClearResults();
	
	if( obj.PressReleases.length > 0 )
	{
			option = document.createElement( 'option' );
			option.value	= '0';
			option.text	= 'Please Select One';
			
			this.m_oTargetElement.options.add( option );
			
		for( i = 0; i < obj.PressReleases.length; i++ )
		{
			pr		= obj.PressReleases[i];
			option	= document.createElement( 'option' );
			
			option.value	= pr.root_path+'/'+pr.relative_path+'/'+pr.file_name;
			option.text		= pr.title;
			
			this.m_oTargetElement.options.add( option );
		}
	}
	else
	{
		option = document.createElement( 'option' );
		option.value	= '0';
		option.text	= 'No Press Releases This Month';
		
		this.m_oTargetElement.options.add( option );
	}
}

var g_oPressReleaseManager = new PhPressReleaseManager();