/*These are the functions for conditional ad functionality. The first call goes to the function
choosead(size). */


//------------------------------------------------------------------------
function parseQueryString (str) {              //this function supports findComponent()
  str = str ? str : location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();
  if (query) {
    var fields = query.split('&');
    for (var f = 0; f < fields.length; f++) {
      var field = fields[f].split('=');
      args[unescape(field[0].replace(/\+/g, ' '))] = 
unescape(field[1].replace(/\+/g, ' '));
    }
  }
  return args;
}
//-----------------------------------------------------------------------
function findComponent(component) {                      //this function returns the URL component
 var args = parseQueryString (); 
 var URLcomponent = false; 
 for (var arg in args) { 
// document.write(arg + ': ' + args[arg] + '<br />');   this writes out all the URL variables  
  if (arg==component) {                                 //gets component from URL 
 
   URLcomponent = args[arg]; 
  } 
 }
 return (URLcomponent);
}
//-----------------------------------------------------------------
function ispartner(i) {                  //this function returns "true" if i is a partner, false otherwise

if (typeof adtree.ads.ad[i].partner == 'string') {

    if (adtree.ads.ad[i].partner == 'all' || adtree.ads.ad[i].partner == partner) {
        debug+="<br>Ad number " + i + "'s partner is " + adtree.ads.ad[i].partner + ",which matches " + partner;
        return (true);
    }
    else { 
      debug+="<br>Ad number " + i + "'s partner is " + adtree.ads.ad[i].partner + ",which does not match " + partner;
      return(false); 
    } 
   
}  
else if (typeof adtree.ads.ad[i].partner == 'object') {
          for (j=0; j<adtree.ads.ad[i].partner.length; j++) {

                if (adtree.ads.ad[i].partner[j] == 'all' || adtree.ads.ad[i].partner[j] == partner) {
                debug+="<br>Ad number " + i + "'s MULTIPLE partner is " + adtree.ads.ad[i].partner[j] + ",which matches " + partner; 
                    return (true);
                }
          }
debug+="<br>Ad number " + i + "'s partner is " + adtree.ads.ad[i].partner + ",which does not match " + partner;
return (false);
}

}
//-----------------------------------------------------------------
function rightsection(i) {            //this function returns 'true' if i matches the_section variable

if (typeof adtree.ads.ad[i].section == 'string') {

    if (adtree.ads.ad[i].section == 'all' || adtree.ads.ad[i].section == the_section) {
        debug+="<br>Ad number " + i + "'s section is " + adtree.ads.ad[i].section + ",which matches " + the_section;
        return (true);
    }
    else { 
      debug+="<br>Ad number " + i + "'s section is " + adtree.ads.ad[i].section + ",which does not match " + the_section;
      return(false); 
    } 
   
}  
else if (typeof adtree.ads.ad[i].section == 'object') {
          for (j=0; j<adtree.ads.ad[i].section.length; j++) {

                if (adtree.ads.ad[i].section[j] == 'all' || adtree.ads.ad[i].section[j] == the_section) {
              debug+="<br>Ad number " + i + "'s MULTIPLE section is " + adtree.ads.ad[i].section[j] + ",which matches " + the_section; 
                    return (true);
                }
          }
debug+="<br>Ad number " + i + "'s section is " + adtree.ads.ad[i].section + ",which does not match " + the_section;
return (false);
}

}
//-----------------------------------------------------------------
function notexpired(i) {              //this function returns true if ad is valid, false if it's expired
var test2 = " ";
//idate[0] is the month, idate[1] the day, idate[2] the year


var d = new Date();
var m = d.getMonth()+1;

//this is the current date in the format mm/dd/yyyy
var currentdate = m + "\/" + d.getDate() + "\/" + d.getFullYear();

//this is from the XML file
var idate = adtree.ads.ad[i].expiration.split("\/");

if (idate[2] > d.getFullYear()) { return(true); }               //case for valid year
else if (idate[2] < d.getFullYear()) { return(false); }         //case for expired year

else if (idate[2] == d.getFullYear()) {                         //case for same year

    if (idate[0] > m ) { return(true); }                        //case for valid month
    else if (idate[0] < m ) { return(false); }                  //case for expired month
    else if (idate[0] == m ) {                                  //case for same month

               if (idate[1] >= d.getDate() ) { return(true); }  //case for valid day
               
               else { return(false); }                          //case for expired day
   
    }
}

}
//-----------------------------------------------------------------
function choosead(sizer) {                         //this ad chooses a random ad from the valid ones
  debug+="<br>" + "I'm inside! And sizer = " + sizer;
  var ranNum;
  var adnumber = -1;
for (i=0; i<adtree.ads.ad.length; i++) {         //find out how many ads are the right size 
    debug+="<br>I'm inside the for loop"
	if (adtree.ads.ad[i].size == sizer && rightsection(i) && adtree.ads.ad[i].mark != 1 && notexpired(i) && ispartner(i)) {
          adnumber+=parseInt(adtree.ads.ad[i].weight);
          debug+="<br>adnumber = " + adnumber;
     }
}
ranNum= Math.floor(Math.random()*(adnumber+1));                      //generate a number between 0 and total ads
var debugvalue = adnumber+1;
//--------this is to test the randomness-----------
var zeeble;
for (i=0; i<20; i++) {
zeeble = Math.floor(Math.random()*(adnumber+1));
randomlist += zeeble + ", ";

}
randomlist +="<br>";
//------------------------------------------------
debug+="<br>Ok, there are " + debugvalue + " ads. Random number between 0 and " + adnumber + " is: " + ranNum;
  
var tempcount = -1;
for (i=0; i<adtree.ads.ad.length;i++) {       //go through all ads  
     if (adtree.ads.ad[i].size == sizer && adtree.ads.ad[i].mark != 1 && notexpired(i) && ispartner(i) && rightsection(i))        //if they're right size and not marked
     { tempcount+= parseInt(adtree.ads.ad[i].weight);
     if (tempcount > ranNum) {                                            //go until you find the right one
          debug+="<br>tempcount=" + tempcount + " and ranNum =" + ranNum;
          debug+="<br>" + adtree.ads.ad[i].image + " is the ad I'll use.";
          adtree.ads.ad[i].mark = 1;                                        //mark it so it isn't used again
          return(i);                                                      //then use it
     }
     else if (tempcount == ranNum) {
          debug+="<br>tempcount=" + tempcount + "ranNum =" + ranNum;
          debug+="<br>" + adtree.ads.ad[i].image + " set";
          adtree.ads.ad[i].mark = 1;                                        //mark it so it isn't used again
          return(i);                                                      //then use it
     }                                       
     else {
        debug+="<br>" + "tempcount = " + tempcount + ". Haven't found it.";    //otherwise keep going
        
        debug+=" Next tempcount is " + tempcount;
         }
     }
}
  
}
//---------------------------------------------------------------------------------
