/*************************************************************

   FILE:  /JavaScript/ads.js
   AUTH:  Thuy Le

   DATE:  July 22, 2007
   LMD :  March 12, 2009

	Functions & Classes for Ads.

**************************************************************/





/*************************************************************
 *
 *  Randomly ad from a selected list.
 *
 *************************************************************/
function getRandomAd( ads_list, num )
{
	max 	= ads_list.length - 2;
	ac	= "";

	if ( num )
	{
		ac += "<hr>";
		ad_id_list = new Array();

		if ( num == 1 )
			ac += getAd( ads_list, getElem( ads_list, max ) );
		else
		{
			for ( i=1; i<=num; i++ )
			{
				ad_id_list[i] = getElem( ads_list, max );
				if ( !validateItems( ad_id_list, ad_id_list[i] ) )
					i--;
			}

			for ( i=1; i<=num; i++ )
			{
				ac += getAd( ads_list, ad_id_list[i] );	
			}
		}
	}

	return ac;
}


/*************************************************************
 *
 *  Returns ad & link.
 *
 *************************************************************/
function getAd( ads_list, curr_ad )
{
	adLayout  = "<center id=adText>- a d v e r t i s e m e n t -<br>";
	adLayout += "<a href=\"" + ads_list[curr_ad].url + "\" target=_blank><img src=\"";
	adLayout += ads_list[curr_ad].img + "\" width=" + ads_list[curr_ad].width + " height=";
	adLayout += ads_list[curr_ad].height + " alt=\"";
	adLayout += ads_list[curr_ad].alt + "\" border=0></a></center><p>";

	return adLayout;
}




///////////////////////////////////  end new functions  ////////////////////////////////




/*********************************************************************
 *
 *  Display DEALS.
 *
 *********************************************************************/

function displayDeals( ads_list, ad_title, tag_name )
{
	max 	= ads_list.length;
//	max2 	= ads_list2.length;
	count 	= 0;
	id	= 1;

	document.write( "<a name=" + tag_name + "></a>" );
	document.write( "<center id=pgCat>" + ad_title + "</center><p>" );

	for ( i=1; i<max; i++ )
	{
		count++;
	
		document.write( "<table border=0 width=100%>" );
		document.write( "<tr><td align=left valign=top width=100% id=text>" );

		displayAd( ads_list, i );
/*
		if ( count % 2 == 0 && count+1 != max )
		{			
			if ( id == max2 ) 
				id=1;
				
			document.write( "<p><hr width=100%><p align=center>" );
			displayLogo( ads_list2, id++ );
		}
*/

		document.write( "</td></tr>" );
		document.write( "<tr><td><br>" );

		if ( count+1 != max )
		{
			document.write( "<hr width=100%>" );
		}

		document.write( "</td></tr>" );
		document.write( "</table>" );
	}
}


/*************************************************************
 *
 *  Display banner randomly.
 *
 *************************************************************/
function displayBanner( ads_list )
{
	max 		= ads_list.length;
	banner_id 	= getElem( ads_list, max );

	document.write("<p><center id=adText>- a d v e r t i s e m e n t -<br>");
	displayLogo( ads_list, banner_id );
	document.write("</center><p>");

}


/*************************************************************
 *
 *  Validate item is unique.
 *
 *************************************************************/
function validateItems( items_list, curr_item )
{
	for ( i=1; i<=curr_item-1; i++ )
	{
		for ( j=i+1; j<=curr_item; j++ )
		{
			if ( items_list[i] == items_list[j] )	
			{
				return false;
			}
		}
	}
	
	return true;
}

/*************************************************************
 *
 *  Returns a random element, excluding computer/elec ad.
 *
 *************************************************************/

function getElem( ads_list, max_items ) 
{
	// Math.random() returns number from 0..1
	// Math.round( number ) rounds number to nearest integer

	rand_num = Math.round( Math.random() * 1000 ) % max_items + 1;
	
	while ( !ads_list[rand_num].ad || ads_list[rand_num].img == "" )
	{
		rand_num = Math.round( Math.random() * 1000 ) % max_items + 1;
	}

	return rand_num;
}

/*************************************************************
 *
 *   Display table of desired number of ads.
 *
 *************************************************************/

function displaySel( ads_list, total_items ) 
{
	max_items 	= ads_list.length;
	items 		= new Array();
	i		= 1;

	items[i++] = getElem( ads_list, max_items );

	if ( total_items > 1 )
	{
		do {
			items[i++] = getElem( ads_list, max_items );

			if ( !validateItems( items, i) )
			{
				i--;
			}

		} while ( i <= total_items );
	}

	document.write( "<table border width=210><tr><th>s p o n s o r  " );

	if ( total_items > 1 )
		document.write( "s" );

	document.write( " :</th></tr>" );

	for ( i=1; i<=total_items; i++ ) 
	{
		document.write( "<tr><td align=center><br>" );
		displayLogo( ads_list, items[i] );
		document.write( "<br><br></td></tr>" );
	}

	document.write( "</table>" );
	
}


/*************************************************************
 *
 *  Display each table row of ad image.
 *
 *************************************************************/

function displayAd( ads_list, curr_ad ) 
{
	ad_layout  = "<b><a href=\"" + ads_list[curr_ad].url + "\" target=_top id=pgItem>";
	ad_layout += ads_list[curr_ad].name + "</a></b><p id=text>";
	ad_layout += ads_list[curr_ad].desc;

	document.write( ad_layout );
}

/*************************************************************
 *
 *  Displays specified category.
 *
 *************************************************************/
                                        
function displayCat( ads_list, curr_cat )
{
	max	= ads_list.length;
//	max2	= ads_list2.length;
	count	= 0;
	id	= 1;

	for ( i=1; i<max; i++ )
	{
		/*
		 *   Current category with following condition to exclude text ads:
		 *	1. Is an Ad with image link
		 *	2. Is not an Ad w/o an image link
		 */
		if ( ads_list[i].cat == curr_cat 
			&& (ads_list[i].ad && ads_list[i].img != "" || (!ads_list[i].ad && ads_list[i].img == "")) )
		{
			count++;
	
			document.write( "<table border=0 width=100%>" );
			document.write( "<tr><td align=left valign=top width=100%>" );

			displayAd( ads_list, i );

			document.write( "</td></tr>" );
			document.write( "<tr><td><br><hr width=100%></td></tr>" );
			document.write( "</table>" );

		}
	}
}

/*************************************************************
 *
 *  Display each table row of ad image/logo.
 *
 *************************************************************/

function displayLogo( ads_list, curr_ad ) 
{
	adLayout = "<a href=\"" + ads_list[curr_ad].url + "\" target=_blank><img src=\"";
	adLayout += ads_list[curr_ad].img + "\" width=" + ads_list[curr_ad].width + " height=";
	adLayout += ads_list[curr_ad].height + " alt=\"";
	adLayout += ads_list[curr_ad].alt + "\" border=0></a>";

	document.write(adLayout);
}

/*************************************************************
 *
 *   Display table of logos of Computer/Electronic related.
 *
 *************************************************************/

function displayCE( ads_list ) 
{
	max = ads_list.length;

	document.write( "<table border width=210><tr><th>s p o n s o r s :</th></tr>" );

	for ( i=1; i<max; i++ ) 
	{
		document.write( "<tr><td align=center><br>" );
		displayLogo( ads_list, i );
		document.write( "<br><br></td></tr>" );
	}

	document.write( "</table>" );
}

/*********************************************************************
 *
 *  Display Discover Card Ad.
 *
 *********************************************************************/

function displayDiscover()
{
	layout = "<p align=center id=adText>- a d v e r t i s e m e n t -<br>";
	layout += "<a href='http://www.kqzyfj.com/click-2476192-10368531' target='_blank'><img src='http://www.lduhtrp.net/image-2476192-10368531' border='0' alt='Discover Open Road - FILL UP on Cashback Bonus. 5% Cashback on gas and automotive purchases!'></a></p>";

	document.write( layout );
}



/*********************************************************************
 *
 *  Display PowWeb Ad link.
 *
 *********************************************************************/

function displayPowweb()
{
	layout = "<center><a href=\"http://www.kqzyfj.com/click-2476192-1174936\" ";
	layout += "style=\"border: none; color: #000000; text-decoration: none; ";
	layout += "font-size: 12px; font-family: Trebuchet MS, Verdana, Arial, Helvetica, sans-serif;\" ";
	layout += " target=\"_blank\">Hosted By<br>";
	layout += "<img src=\"http://naucon.net/images/logos/pwsmall_logo.gif\" ";
	layout += " alt=\"Web Hosting by PowWeb\" border=0></a></center>";

	document.write( layout );
}


/*
<center><!-- hosted by powweb -->
	<a href="http://www.powweb.com/join/TOP.bml?AffID=563704" style="border: none; color: #000000; text-decoration: none; font-size: 12px; font-family: Trebuchet MS, Verdana, Arial, Helvetica, sans-serif;" target="_blank">
		Hosted By<br>
		<img src="http://www.powweb.com/images/logos/pwsmall_logo.gif" alt="Web Hosting by PowWeb" border="0">
	</a>
</center>		
*/


////////////////////////////    C L A S S E S    ///////////////////////////////


/*********************************************************************
 *
 *  Class returns name/institution, category, (0=not ad, 1=ad),
 *  url, image location, image width, image height, image alt value,
 *  ad description, ad expiration date.
 *
 *********************************************************************/

function Deals( ad_name, ad_url, ad_desc ) 
{
	this.name	= ad_name;
	this.url	= ad_url;
	this.desc	= ad_desc;

	return this;
}


/*********************************************************************
 *
 *  Class returns name/institution, category, (0=not ad, 1=ad),
 *  url, image location, image width, image height, image alt value, 
 *  ad description.
 *
 *********************************************************************/

function Ads( ad_name, ad_cat, is_ad, img_url, img_loc, img_width, img_height, img_alt, ad_desc ) 
{
	this.name	= ad_name;
	this.cat	= ad_cat;
	this.ad		= is_ad;
	this.url	= img_url;
	this.img	= img_loc;
	this.width	= img_width;
	this.height	= img_height;
	this.alt	= img_alt;
	this.desc	= ad_desc;

	return this;
}



