function get_parameter(param_name)
{
	var retval = "";
	aURL = document.URL;
	parPos = aURL.indexOf('?');
	ParStr = "";
	if (parPos > -1 ) {
		ParStr = aURL.substring(parPos + 1, aURL.length);
		parPos = ParStr.indexOf(param_name + '=');
		if (parPos > -1 ) {
			parPos = parPos + param_name.length + 1;
			parEnds = ParStr.indexOf("&", parPos);
			if (parEnds < 0) 
	  			parEnds = ParStr.length;
			retval = ParStr.substring(parPos, parEnds);
		}
	}
	return retval;
}

function get_rotator_size(rotator_id)
{
	for (i = 0; i < ban_rotators_max_size; ++ i) {
		if ( ban_rotators[i][0] == rotator_id ) {
			return ban_rotators[i][1];
		}
	}
}

function get_banner_id(banner_size, a_country_code)
{
	numb_of_banners = 0;
	var banner_ids = new Array();
	
	for (i = 0; i < banner_arr_max_size; ++ i) {
		if ( banners[i][1] == banner_size ) {
			if ( a_country_code.length == 0 || banners[i][3].indexOf(a_country_code) >= 0 ) {
				banner_ids[numb_of_banners] = banners[i][0];
				numb_of_banners = numb_of_banners + 1;
			}
		}
	}
	if ( numb_of_banners == 0 ) {
		for (i = 0; i < banner_arr_max_size; ++ i) {
			if ( banners[i][1] == banner_size ) {
				if ( banners[i][3].length == 0 ) {
					banner_ids[numb_of_banners] = banners[i][0];
					numb_of_banners = numb_of_banners + 1;
				}
			}
		}
	}
	
	ban_index = Math.round( Math.random() * ( numb_of_banners - 1 ) );
	if ( ban_index > numb_of_banners - 1 )
		ban_index = numb_of_banners - 1;
	if ( banner_ids[ban_index] )
		return banner_ids[ban_index];
	else
		return '';
}

function get_banner_field(banner_id, field_number)
{
	for (i = 0; i < banner_arr_max_size; ++ i) {
		if ( banners[i][0] == banner_id ) {
			return banners[i][field_number];
		}
	}
}

function Hex_To_String(ahex_string)
{  
    // convert a hex string to a string  
    strTemp = "";  
    for (x = 0; x < ahex_string.length - 1; x = x + 2)  
    {  
		byte_s = ahex_string.charAt(x) + ahex_string.charAt(x + 1);
		number = parseInt( byte_s, 16 );
		strTemp = strTemp + String.fromCharCode(number);
    }  
	return strTemp;  
}

var real_banner_id = "";
var wait_time = 0;
function display_banner()
{
	try{
		if ( country_code == '' ) ;
	}
	catch(error){
		/*
		if ( wait_time < 3 ) {
			wait_time = wait_time + 1;
			setTimeout("display_banner()", 1000);
			return;
		}
		else
		*/
			country_code = '';
			
	}
	
	//document.write('country_code: ' + country_code + '<br>');
	//document.write('banner_id: "' + banner_id + '"<br>');
	try{
		if ( banner_id.length > 0 ) {
			size = get_rotator_size(banner_id);
			if ( !size )
				size = '0x0';
			if ( size.indexOf('0x0') >= 0 ) {
				size = 'U';
				banner_type = 'T';
			}
			else {
				banner_type = 'I';
			}
			real_banner_id = get_banner_id(size, country_code);
		}
	}
	catch(error){	}
	
	if ( real_banner_id.length == 0 )
		real_banner_id = banners[0][0];
	try{
		if ( user_id.length == 0 )
			user_id = "11111111";
	}
	catch(error){
		user_id = "11111111";
	}
	banner_type = get_banner_field(real_banner_id, 2);
	if ( banner_type == 'T' )
		banner_code = banner_codes[0];
	else
		banner_code = banner_codes[1];
		
	try{
		if ( just_preview ) {
			//user_id = '123e1a01';
			s = scripts_path + "click.php?tst_cl=1";
		}
		else
			s = mainSiteUrl + "/aft/" + user_id + '/' + real_banner_id + '.html';
	}
	catch(error){
		s = mainSiteUrl + "/aft/" + user_id + '/' + real_banner_id + '.html';
	}
	while ( banner_code.indexOf("{$targeturl}") >= 0 ) banner_code = banner_code.replace("{$targeturl}", s);
	
	title = Hex_To_String(get_banner_field(real_banner_id, 4));
	while ( banner_code.indexOf("{$title}") >= 0 ) banner_code = banner_code.replace( "{$title}", title );
	try{
		if ( just_preview )
			s = '';
		else
			s = '<img style="border:0" src="' + scripts_path + 'imp.php?a_aid=' + user_id + '&a_bid=' + real_banner_id + '" width="1" height="1" alt="" />';
	}
	catch(error){
		s = '<img style="border:0" src="' + scripts_path + 'imp.php?a_aid=' + user_id + '&a_bid=' + real_banner_id + '" width="1" height="1" alt="" />';
	}
	
	ban_size = get_banner_field(real_banner_id, 6);
	width = ban_size.substr( 0, ban_size.indexOf('x') );
	height = ban_size.substr( ban_size.indexOf('x') + 1, ban_size.length );
	
	//cntry1st = 'AU,CA,DK,GB,IE,NO,US';
	//if ( banner_type == 'I' && country_code.length > 0 && cntry1st.indexOf(country_code) >= 0 ) {
	if ( width >= 160) {
		banner_code = '<div style="width:{$width}px; text-align:right; padding-top:0px; margin-top:0px; background-color:#ffffff;">' + 
			banner_code + 
			'<br><a href="{$mainSiteUrl}/aft/{$userid}/55555555.html" title="Quality online advertising. Big traffic. Increase sales. Promote your website" style="font-size:10px; color:#000000; font-family:arial;">promote your website or blog...</a></div>';
	}
	
	while ( banner_code.indexOf("{$impression_track}") >= 0 ) banner_code = banner_code.replace("{$impression_track}", s);
	descr = Hex_To_String(get_banner_field(real_banner_id, 5));
	while ( banner_code.indexOf("{$description}") >= 0 ) banner_code = banner_code.replace("{$description}", descr );
	
	while ( banner_code.indexOf("{$image_src}") >= 0 ) banner_code = banner_code.replace( "{$image_src}", title );
	
	while ( banner_code.indexOf("{$alt}") >= 0 ) banner_code = banner_code.replace("{$alt}", descr );
	
	
	while ( banner_code.indexOf("{$width}") >= 0 ) banner_code = banner_code.replace("{$width}", width);
	while ( banner_code.indexOf("{$height}") >= 0 ) banner_code = banner_code.replace("{$height}", height);
	
	while ( banner_code.indexOf("{$userid}") >= 0 ) banner_code = banner_code.replace("{$userid}", user_id);
	while ( banner_code.indexOf("{$mainSiteUrl}") >= 0 ) banner_code = banner_code.replace("{$mainSiteUrl}", mainSiteUrl);
	
	while ( banner_code.indexOf("'") >= 0 ) banner_code = banner_code.replace("'", "\'");
	while ( banner_code.indexOf("\r") >= 0 ) banner_code = banner_code.replace("\r", '');
	while ( banner_code.indexOf("\n") >= 0 ) banner_code = banner_code.replace("\n", '');
	
	document.write(banner_code);
	//return banner_code;
	//alert(banner_code);
}

display_banner();




