// JavaScript Document

var myadscript_ad_client = (Math.round((Math.random()*90000000)+1));

var myadscript_server_path = 'http://www.myadscript.com/ars/';



var ads_url = escape(parent.document.URL);

var myadscript_cblink = parseHex(myadscript_border);

var myadscript_ad_inframe = (top.frames.length);



var myadscript_ad_whsplit = myadscript_format.indexOf('x');

var myadscript_ad_width   = myadscript_format.substring(0,myadscript_ad_whsplit);

var myadscript_ad_height  = myadscript_format.substring(myadscript_ad_whsplit+1);



var browser=navigator.appName;

var b_version=navigator.appVersion;

if (browser=="Netscape") {

	myadscript_ad_height = parseInt(myadscript_ad_height);

}else {

	myadscript_ad_height = parseInt(myadscript_ad_height)+8;

}



document.write('<div id="agg_' + myadscript_ad_client + '" style="width:'+ myadscript_ad_width +'px; height:'+ myadscript_ad_height +'px;"></div>');



/* ----- main function ------ */

window.onload=function(){



	var p = document.documentElement;

  	var keyword = '';

  	var matchs = new Array;



  	if (p.innerHTML != undefined) {

  		var content = p.innerHTML;

		content = content.replace(/<\S[^<>]*>/gi,'');	

		matchs = content.match(/maskw:([^<>.]*)/i);

    	if (matchs != null) {

	  		keyword = matchs[1].replace(/&/g,'_').replace(/\n/g,'').replace(/\u2033/g,'"').replace(/\u2019/g,"'");

		} else {

			// extract all words

			//alert(countWords(content));

			content = content.replace(/\n/gi,' ');		

			content = content.replace(/\t/gi,' ');		

			content = content.replace(/[0-9]/g,' ');
			
			content = content.replace(/&/g,'_');	

			content = content.replace(/  /gi,' ');		

			keyword = calculateWords(content);

		}

  	}

	myadscript_kw = keyword;

	document.getElementById('agg_'+ myadscript_ad_client).innerHTML = '<iframe frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" topmargin=\"0\" bottommargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" scrolling=\"no\" id=\"myadscript_'+myadscript_ad_client+'_iframe\" name=\"myadscript_'+myadscript_ad_client+'_iframe\" src=\"'+ myadscript_server_path + 'generate.php?pdc_id='+myadscript_pdc_id+'&format='+myadscript_format+'&border='+myadscript_border+'&bg='+myadscript_bg+'&link='+myadscript_link+'&text='+myadscript_text+'&cblink='+myadscript_cblink+'&kw='+myadscript_kw+'&inframe='+myadscript_ad_inframe+'" width=\"'+myadscript_ad_width+'\" height=\"'+myadscript_ad_height+'\"></iframe>';	

	setCookie( 'our_affilliates_url_params', myadscript_pdc_id+'[--1--]'+myadscript_format+'[--1--]'+myadscript_border+'[--1--]'+myadscript_bg+'[--1--]'+myadscript_link+'[--1--]'+myadscript_text+'[--1--]'+myadscript_cblink+'[--1--]'+myadscript_kw+'[--1--]'+myadscript_ad_inframe );

}



function setCookie(c_name,value,expiredays){

	var exdate=new Date();

	exdate.setDate(exdate.getDate()+expiredays);

	document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

}



function getCookie(c_name) {

	if (document.cookie.length>0){

		c_start=document.cookie.indexOf(c_name + "=");

		if (c_start!=-1){ 

			c_start=c_start + c_name.length+1; 

			c_end=document.cookie.indexOf(";",c_start);

			if (c_end==-1) c_end=document.cookie.length;

			return unescape(document.cookie.substring(c_start,c_end));

		} 

	}

	return "";

}



function checkCookie() {

	username=getCookie('username');

	if (username!=null && username!="") {

		alert('Welcome again '+username+'!');

	}else {

		username=prompt('Please enter your name:',"");

		if (username!=null && username!="") {

			setCookie('username',username,365);

		} 

	}

}



/* ----- helper functions ------ */

function parseHex (valHex) {

	

	// RGB

	var red = parseInt ( valHex.substring (1,3),16);

	var grn = parseInt ( valHex.substring (3,5),16);

	var blu = parseInt ( valHex.substring (5,7),16);

	var red = parseInt ( valHex.substring (0,2),16);

	var grn = parseInt ( valHex.substring (2,4),16);

	var blu = parseInt ( valHex.substring (4,6),16);

	

	

	// CMY

	var cya = Math.abs ( Math.ceil ( red / 256 * 100 ) - 100);

	var maj = Math.abs ( Math.ceil ( grn / 256 * 100 ) - 100);

	var yel = Math.abs ( Math.ceil ( blu / 256 * 100 ) - 100);

	

	var cmy = cya+maj+yel;

	var cblink = (cmy>100) ? 'w' : 'b';

	

	return cblink;

}



/* ----- helper functions ------ */

var stopWords = new Array();

stopWords = [

  	'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 

    'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 

    'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 

    'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 

    'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 

    'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 

    'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 

    'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 

    'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 

    'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 

    'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 

    'than', 'too', 'very', 'get', 'according', 'this', 'these', 'yes', 'no', 'yeah', 'nope', 

	'can', 'will', 'every', 'everything', 'any', 'anything', 'going to', 'cannot', 'can\'t', 

	'ago', 'just', 'com', 'find', 'search', 'go', 'via', 'elsewhere', 'links', 'us', 'rants', 

	'week', 'day', 'month', 'year', 'weeks', 'days', 'months', 'years', 'comments', 'trackbacks', 

	'tags', 'nbsp', 'well', 'make',



	'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december', 

	'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec', 

	'sunday', 'monday', 'tuesday', 'wednsday', 'thursday', 'friday', 'saturday',

	'sunday', 'monday', 'tue', 'wed', 'thu', 'fri', 'sat',

];





function inArray(value, array) {

	for (var i=0; i<array.length; i++) {

		if (array[i] === value) {

			return true;

		}

	}

}



function cleanWord(word) {

	rv = word.toLowerCase();

	rv = rv.replace(",","");

	rv = rv.replace(".","");

	return rv;

}



function calculateWords(text) {

	var words = text.split(" ");

	

	for (i=0;i<words.length;i++) {

		words[i] = cleanWord(words[i]);

	}

	

	var wordsFrequencies = new Array();

	var uniqueWordCount = 0;



	// Count unique words and place into array

	for (c=0;c<words.length;c++) {

		alreadyCounted=false;

		currentWord = words[c];



		if (currentWord.length > 1 && !inArray(currentWord, stopWords)) {

		

			// make sure it's a word we haven't already counted

			for (e=0;e<wordsFrequencies.length;e++) {

				if (currentWord==wordsFrequencies[e][0]){

					alreadyCounted = true;

				}

			}



			if (!alreadyCounted) {

				// add to array

				wordsFrequencies[uniqueWordCount] = new Array(2);

				wordsFrequencies[uniqueWordCount][0] = currentWord;

				wordsFrequencies[uniqueWordCount][1] = 0;



				// count total instances

				for (d=0;d<words.length;d++) {

					if (words[d]==wordsFrequencies[uniqueWordCount][0]){

						wordsFrequencies[uniqueWordCount][1]++;

					}

				}



				uniqueWordCount++;



			}



		}



	}



	// Sort array

	var sorted = new Array();

	var maxFreq = 0;

	var sortCounter = 0;



	// find the highest word frequency

	for (c=0;c<wordsFrequencies.length;c++) {

		maxFreq = (wordsFrequencies[c][1]>maxFreq) ? wordsFrequencies[c][1] : maxFreq;

	}



	// count down through possible frequencies, adding words to new array if their frequency matches.

	for (c=maxFreq;c>=1;c--) {

		for (d=0;d<wordsFrequencies.length;d++) {

			word = wordsFrequencies[d][0];

			freq = wordsFrequencies[d][1];

			if (freq==c) {

				sorted[sortCounter] = new Array(2);

				sorted[sortCounter][0] = word;

				sorted[sortCounter][1] = freq;

				sortCounter++;

			}

		}

	}





	// create HTML list output

	var reportLength = (sorted.length>15) ? 15 : sorted.length;

	var strReport = '';

	for (c=0;c<reportLength;c++) {

		//strReport += sorted[c][0] + ": " + sorted[c][1] + "<br />";

		strReport += sorted[c][0] + " ";

	}

	return strReport;

	//document.getElementById("report").innerHTML = strReport;

}