function openLink(params) {
    window.open("http://banners.macam.ac.il/hit.aspx?"+params,"","");
}


/* =========================================================
	In the <param> FlashVars qs is passed to the query string
	of the link opened by the SWF file
	paramsArray will be split on the | character
	The variables are as follows:
		locationId
		bannerId
		bannerDimensions
		rank
		userScreen
		userColors
========================================================= */
var userScreen	= screen.width + "x" + screen.height;
var userColors	= screen.colorDepth;
function writeSwfBanner(locId,bannerId,w,h,rank){
	var returnString = "";
	returnString += "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"" + w + "\" height=\"" + h + "\" viewastext>\n";
	returnString += "<param name=\"movie\" value=\"http://banners.macam.ac.il/bannerServer.aspx?bannerId=" + bannerId + "&bannerDimensions=" + w + "x" + h + "\" />\n";
	returnString += "<param name=\"FlashVars\" value=\"qs=paramsArray=" + locId + "|" + bannerId + "|" + w + "x" + h + "|" + rank + "|" + userScreen + "|" + userColors + "\">\n";
	returnString += "<param name=\"quality\" value=\"high\" />\n";
	returnString += "<param name=\"wmode\" value=\"transparent\" />\n";
	returnString += "<param name=\"AllowScriptAccess\" value=\"always\" />\n";
	// The following line is for Mozilla browsers but does not work at this stage due to the use of an activeX (IE) object to parse the XML file
	//returnString += "<embed src=\"http://banners.macam.ac.il/bannerServer.aspx?bannerId=" + bannerId + "&bannerDimensions=" + w + "x" + h + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"" + w + "\" height=\"" + h + "\" FlashVars=\"qs=paramsArray=" + locId + "|" + bannerId + "|" + w + "x" + h + "|" + rank + "|" + userScreen + "|" + userColors + "\" />\n";
	returnString += "</object>\n";
	return returnString;
}

function displayBanners(xmlFile,locationId,dateToday,displayMax,widthArray,bannerSeparator,bannersBeginning,bannersEnding){
	var returnString = "";
	var xmlBanners = new ActiveXObject("Microsoft.XMLDOM");
	xmlBanners.async = false;
	xmlBanners.load(xmlFile);
	var theLocationNodes = xmlBanners.documentElement.selectNodes("loc");
	var bannersArray = new Array();
	/* =============================
	The Banners Array consists of the following fields
	[bannerIndex] [0=id,1=fixed,2=[typeIndex]] [typeIndex] [0=m,1=w,2=h]
	==============================*/
	
	if(theLocationNodes.length > 0){
		for(var locCounter=0;locCounter < theLocationNodes.length;locCounter++){
			if(theLocationNodes[locCounter].getAttribute("id")==locationId){
				
				// Fill an array with available banners
				// Eliminating those whose date has expired
				var theBannerNodes = theLocationNodes[locCounter].selectNodes("bnr");
				if(theBannerNodes.length > 0){
					for(var b=0;b < theBannerNodes.length;b++){
						var dateArray = new Array();
						dateArray = theBannerNodes[b].getAttribute("ex").split("/");
						var bannerExpiresDate = new Date();
						bannerExpiresDate.setMonth(dateArray[1]-1,dateArray[0]);
						bannerExpiresDate.setYear(dateArray[2]);
						bannerExpiresDate.setHours(0,0,0,0);
						if(bannerExpiresDate.getTime() > dateToday.getTime()){ // Not expired
							bannersArray[bannersArray.length] = new Array(); // Banner
							bannersArray[bannersArray.length-1][0] = theBannerNodes[b].getAttribute("id");
							bannersArray[bannersArray.length-1][1] = theBannerNodes[b].getAttribute("fixed");
							bannersArray[bannersArray.length-1][2] = new Array(); // Types Array
							var theTypeNodes = theBannerNodes[b].selectNodes("type");
							if(theTypeNodes.length > 0){
								for(var t=0;t < theTypeNodes.length;t++){
									bannersArray[bannersArray.length-1][2][t] = new Array(); // Type
									bannersArray[bannersArray.length-1][2][t][0] = theTypeNodes[t].getAttribute("m");
									bannersArray[bannersArray.length-1][2][t][1] = theTypeNodes[t].getAttribute("w");
									bannersArray[bannersArray.length-1][2][t][2] = theTypeNodes[t].getAttribute("h");
								}
							}
						}
					}
				}

				// Delete banners without types (no file)
				for(var i=0;i<bannersArray.length;i++){
					if(bannersArray[i][2].length == 0){ // No file
						bannersArray.splice(i,1) // Deletes this banner from the Array
						i -= 1; // Adjust the index
					}
				}

				var displayCount		= 0;
				var widthArrayCount 	= 0;

				// Display fixed banners
				for(var i=0;i<bannersArray.length;i++){
					if(bannersArray.length==1){
						widthArrayCount = displayMax-1; // Use last banner width - also triggers display of bannersEnding
					}
					if(displayCount<displayMax && bannersArray[i][1]=="1"){
						var typesArrayLength = bannersArray[i][2].length;
						for(var j=0;j < typesArrayLength;j++){
							if(bannersArray[i][2][j][0]=="application/x-shockwave-flash"  && bannersArray[i][2][j][1] == widthArray[widthArrayCount]){
								if(displayCount==0){
									returnString += bannersBeginning; // Beginning
								}
								returnString += writeSwfBanner(locationId,bannersArray[i][0],bannersArray[i][2][j][1],bannersArray[i][2][j][2],displayCount+1);
								returnString += bannerSeparator;
								if(widthArrayCount == displayMax-1){
									returnString += bannersEnding; // End
								}
								displayCount 		+= 1;
								widthArrayCount	+= 1;
								bannersArray.splice(i,1) // Deletes this banner from the Array
								i -= 1; // Adjust the index
								j = typesArrayLength; // Exit the loop
							}
						}
					}
				}

				// Display the rest of the banners
				// If necessary choose them randomly
				for(var i=displayCount;i<displayMax;i++){
					if(bannersArray.length>0){
						if(bannersArray.length==1){
							var randBanner = 0;
							widthArrayCount = displayMax-1; // Use last banner width - also triggers display of bannersEnding
						}else{
							var randBanner = Math.floor(Math.random()*bannersArray.length);
						}
						var typesArrayLength = bannersArray[randBanner][2].length;
						for(var j=0;j < typesArrayLength;j++){
							if(bannersArray[randBanner][2][j][0]=="application/x-shockwave-flash"  && bannersArray[randBanner][2][j][1] == widthArray[widthArrayCount]){
								if(displayCount==0){
									returnString += bannersBeginning; // Beginning
								}
								returnString += writeSwfBanner(locationId,bannersArray[randBanner][0],bannersArray[randBanner][2][j][1],bannersArray[randBanner][2][j][2],displayCount+1);
								returnString += bannerSeparator;
								if(widthArrayCount == displayMax-1){
									returnString += bannersEnding; // End
								}
								displayCount 		+= 1;
								widthArrayCount	+= 1;
								bannersArray.splice(randBanner,1) // Deletes this banner from the Array
								j = typesArrayLength; // Exit the loop
							}
						}
					}
				}
			}
		}
	}
	return returnString;
}


