function get_campaign()
{
        if ( null != location.href.lastIndexOf('&_cid=') )
        {
                var campaign_id = location.href.lastIndexOf('&_cid=') + 6;
                campaign_id     = location.href.substring(campaign_id);
                return campaign_id;
        }
}

var custom = {'init' : custom_init};

function custom_init()
{
	custom.stats = {'defaults' : {}};
	custom.form  = {'defaults' : {}};
	custom._y    = {'defaults' : {}};
	custom._m    = {'defaults' : {}};

	custom.stats.populate = function(q)
	{
		var form    = q.id;
		var getArgs = "{ '_r' : 'stats', '_cam' : '" + $('_cam').value + "'";
		for ( var i = 0 ; i < $(form).elements.length ; i++ )
		{
			$(form).elements[i].value;

			getArgs += ", '" + $(form).elements[i].id + "' : '" + $(form).elements[i].value + "'";
		}
		getArgs += " }";

		QueryAjax(null, 'inc/stats.inc.php', getArgs, null, null, 'custom.form.callback(data);');
	}

	custom.form.callback = function(data)
	{
		eval(data);
		process_dropdown_callback(results, defaults, '_y', inputs, blocks);

		if ( null != inputs )
		{
			if ( inputs.unauthorized == true )
				window.location = "?_p=login";

			switch ( true )
			{
				case inputs.display_results :
					$('results_img').src			   = 'images/success.png';
					$('display_results').style.display = 'block';
				break;

				case inputs.display_results == false :
					$('results_img').src			   = 'images/failed.png';
					$('display_results').style.display = 'block';
				break;
			}
		}

		$('general_stats').src = inputs.general_stats;
		$('unique_stats').src  = inputs.unique_stats;
		$('sales_stats').src   = inputs.sales_stats;
	}

	custom.form.populate = function(q, first)
	{
		var cid = $('_cam').value;
		if ( null != first )
		{
			var a = location.href.lastIndexOf('_cid=');
			if ( a != -1 )
			{
				var cid = get_campaign();
			}
		}
		QueryAjax(null, 'inc/stats.inc.php', {'_r' : 'get_year', '_uid' : '13', '_cam':cid}, null, null, 'custom.form.callback(data);');
	}

	custom.form.populate(null, 1);
}

window.onload = function()
{
	if(isset('custom'))
		custom.init();
}

