//
// Code moved to ad-1.1.js and fixed.
//

// Current Page Reference
// copyright Stephen Chapman, 1st Jan 2005
// you may copy this function but please keep the copyright notice with it
function getURL() {
    var uri = new Object();
    uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
    uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
    uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
    uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
    pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
    pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
    uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
    uri.file = uri.page;
    if (uri.ext != '') uri.file += '.' + uri.ext;
    if (uri.file == '') uri.page = 'index';
    uri.args = location.search.substr(1).split("?");
    return uri;
}

// adding search functionality to JS array
Array.prototype.findItem = function(searchEl) {
    for (i=0; i<this.length; i++) {
       if (this[i]===searchEl) { return true; }
    }
    return false;
}

// epals JS namespaces
var epals = {};
var epalsfunc = {};
var epalsVar = {};

var isUserParamsDone = false;

// default params
epals.DEFAULT_ROLE = 'teacher';
epals.DEFAULT_REGION = 'us';
epals.DEFAULT_APP = 'home';
epals.DEFAULT_AGE = 0;

// user params
var usrrole = epals.DEFAULT_ROLE;
var usrregion = epals.DEFAULT_REGION;
var usrage = epals.DEFAULT_AGE;

epalsfunc.randInt = function() { return  (Math.random() + "") * 1000000000000000000; }

epalsVar.adKwd = '';
// function to validate user params, it updates a flag to allow to 
// check it have called already
epalsfunc.validateUserParams = function() {
    if (isUserParamsDone) { return; }

    var validRoles = ['teacher','student','parent'];
    var validRegion = ['us','row'];
    var validAgeGroup = ['1','2','3','4','5','6','7','8'];
		var kw = [];
    if (validRoles.findItem(usrrole)) {
        kw.push('role='+usrrole);
        // alert('added role kw:' + kw[0]);
    } else {
        // alert('role not found');
    }
    // alert ('user role:' + usrrole);
    if (validRegion.findItem(usrregion)) {
        kw.push('country='+usrregion);
        //alert('added country kw:' + usrregion);
    } else {
        // alert('region not found');
    }
    // validate age
    if (validAgeGroup.findItem(usrage)) {
        kw.push('age='+usrage);
    }
    if (kw.length > 0) {
        epalsVar.adKwd = kw.join(';') + ';';
    } else {
        epalsVar.adKwd = '';
    }
    // alert('user kwd:' + epalsVar.adKwd);
    isUserParamsDone = true;
}

var epalsApp = '';
epalsfunc.appFromUrl = function () {
    var result = epals.DEFAULT_APP;
    var urlAppMap = {
        'activities' : 'activities',
        'askepals' : 'ask',
        'blog' : 'blog',
        'tools/forums' : 'forums',
        'mail' : 'mail',
        'webmail2 | home' : 'mailhome',
        'esearch | search'  : 'search',
        'translation' : 'translation'
    }
    var url = getURL();
    var path = url.path;
    if (path.match(/^activities/)){
        result = 'activities';
    } else if (/^askepals/.test(path) ){
         result = 'ask';
    } else if (path.match(/^tools\/forum/)){
         result = 'forums';
    } else if (/^webmail2/.test(path) ){
         result = 'mail';
    } else if (/home/.test(path) ){
         result = 'mailhome';
    } else if (/^esearch/.test(path) || /^search/.test(path)) {
         result = 'search';
    } else if (/^translation/.test(path) ){
         result = 'translation';
    }

    return result;
}

epalsfunc.validateAdParams = function(size) {
    var validApps = ['home','activities','forums','ask','mailhome','mail','blog','search','translation'];
    var validSize = ['728x90','300x250'];
    if (epalsApp.length == 0 || (!validApps.findItem(epalsApp))) {
        epalsApp = epalsfunc.appFromUrl();
    }
    return true;
}

var baseAdUrl = 'http://ad.doubleclick.net/adj/site240.tmus/';
epalsVar.tileCount = 0;
var ord = epalsfunc.randInt();
epalsfunc.showAd = function (width,height,tileNumber,position) {
    if (! isUserParamsDone) { epalsfunc.validateUserParams(); }
    var size = width + 'x' + height;
    if (! epalsfunc.validateAdParams(size)) {
        alert('Ad problem');
        return;
    }
		if ((tileNumber+0) < 1) {
		    tileCount=1; 
    } else {
        tileCount=tileNumber;
    }
    //alert ('tile count:' + tileCount);
    // epalsVar.tileCount++;
		
    var baseUrl = baseAdUrl + epalsApp;
    var richAd = '<scr'+'ipt language="Javascript1.1"' + 'src="' + baseUrl +
                 ';dcopt=ist;abr=!webtv;'+epalsVar.adKwd+'tile=' + epalsVar.tileCount +';pos=' + position + ';sz=' + size + ';ord=' + ord + '?"></scr'+'ipt>';
    // alert('rich add:' + richAd);
    document.write(richAd);

    <!--
    if ((!document.images && navigator.userAgent.indexOf('Mozilla/2.') >= 0) || 
         navigator.userAgent.indexOf("WebTV") >= 0) {
        document.write('<a href="http://ad.doubleclick.net/jump/site240.tmus/' + epalsApp + 
                       ';'+epalsVar.adKwd+'tile=' + epalsVar.tileCount +';pos=' + position + ';sz=' + size + ';ord=' +
                       ord + '?" target="_blank">');
        document.write('<img src="http://ad.doubleclick.net/ad/site240.tmus/' + epalsApp +''+epalsVar.adKwd+
                       ';tile=' + epalsVar.tileCount +
                       ';pos=' + position + ';sz=' + size + ';ord=' + ord + '?" width="728" height="90" border="0" alt=""></a>');
    }
    //-->
}

