/*	*** ADS : JavaScript Ad Rotation ***	
	Last Modified   : August 15, 2002 by Jim McGinley	
	Documented code with samples available (comments removed to decrease load time)
*/

function Ads() {	
	var images = new Array;	
	var totalchance = 0;	
	var chancefilled = false;	
	
	this.add = function add(pimg, pa, pchance) {		
		var index = images.length;		
		var chance = 0;				
		
		images[index] = new Object;		
		images[index].img = pimg; images[index].a = pa;				
		
		if ((pchance) && (pchance != "")) {			
			if (isNaN(parseInt(pchance))) {				
				alert("ERROR:From JAVASCRIPT ADS:\npchance needs to be a number."); return;			
			} else {				
				chance = parseInt(pchance);			
			}		
		}		
		if (((chancefilled) && (chance == 0)) || 		    
			((!chancefilled) && (chance != 0) && (index != 0))			
			) {									
			alert("ERROR:From JAVASCRIPT ADS:\nImages added to Ads must always have the chance argument filled out, or never have the chance argument filled out. You only have some filled out."); return;		
		} else if (chance > 0) {			
				chancefilled = true;			
				images[index].chance = chance;			
				totalchance += images[index].chance;		
			}	
		}	
		this.html = function html() {		
			var index = -1;		
			var anchor = false;		
			var return_html = "";			
			var sumchance = 0;				
			
			if (images.length==0) { alert("ERROR:From JAVASCRIPT ADS:\nNo images have been added to Ads.\nCan't use ads.html() until at least 1 image has been added using ads.add()."); return; }		
			
			if (totalchance == 0) {			
				// not weighted. 10 images yields 0 - 9			
				image =  images[Math.floor(Math.random() * images.length)];		
			} else {			
				// weighted			
				chance = Math.floor(Math.random() * totalchance);			
				for (index=0; index < images.length; index++) {				
					sumchance += images[index].chance;				
					if (chance < sumchance) {image = images[index]; break;}			
					}		
			}				
			if ((image.a) && (image.a != "")) {anchor = true;}			
			return_html += "\n";		
			if (anchor) {return_html += image.a;}			
				return_html += image.img;		
			if (anchor) {return_html += '</A>';}		
			return_html += "\n";				
			return return_html;	
			}
		}
var ads = new Ads;