
// Adds event to window.onload without overwriting currently 
// assigned onload functions.

function addLoadEvent(func) {    
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } 
  else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


// set target for external links (rel="external")

function external() { 
  if(!document.getElementsByTagName)return; 
  var anchors = document.getElementsByTagName("a"); 
  for(var i=0;i<anchors.length;i++) { 
    var anchor = anchors[i]; 
    if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")anchor.target = "_blank";
    else if(anchor.getAttribute("href") && anchor.getAttribute("type") == "external")anchor.target = "_blank";
  } 
} 

addLoadEvent(external);

// ------------- print page popup -------------

function printpage(title, date, url) { 
  if(document.getElementById!= null) { 
    var divid = "print-area";
    var html = '<?'+'xml version="1.0" encoding="utf-8"?>\n';
    html += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n\n';
    html += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n<head>\n';
    html += '<title>' + title + '</title>\n';
        html += '<link rel="stylesheet" type="text/css" href="http://columbiavalleynews.com/wp-content/themes/columbiavalleynews/common/stylesheets/template/columbiavalleynews_template.css" />\n';
        html += '<link rel="stylesheet" type="text/css" href="http://columbiavalleynews.com/wp-content/themes/columbiavalleynews/common/stylesheets/content/columbiavalleynews_content.css" />\n';
        html += '<link rel="stylesheet" type="text/css" href="http://columbiavalleynews.com/wp-content/themes/columbiavalleynews/common/stylesheets/content/columbiavalleynews_directory.css" />\n';
        html += '<link rel="stylesheet" type="text/css" href="http://columbiavalleynews.com/wp-content/themes/columbiavalleynews/common/stylesheets/content/columbiavalleynews_print.css" />\n';
    html += '</he' + 'ad>\n<body>\n';
    var printPageElem = document.getElementById(divid); 
    if(printPageElem != null) {
      html += '\n\n';
      if(title != '' && date != '' && url != '') {
        html += '<div style="font-size:11px;letter-spacing:1px;margin-left:15px;margin-bottom:15px;">';
        if(title != '')html += title + " - ";
        if(date != '')html += date;
        if(url != '')html += "<br/>" + url;
        html += '</div>\n\n';
      }
      html += printPageElem.innerHTML; 
    }
    else return;
    html += '\n\n</bo' + 'dy>\n</ht' + 'ml>'; 
    var printWin = window.open("","printFriendly","toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=no,status=no,width=540,height=600");
    printWin.document.open(); 
    printWin.document.write(html); 
    printWin.document.close(); 
    printWin.print(); 
  } 
} 

// AJAX get ad by zone code

function ajaxGetFile(divID, url) {
  ran = new Date();
  if(url.indexOf('?') == -1)var sep = "?";
  else var sep = "&amp;";
  url = url + sep + ran.getTime();
  var ajaxRequest;  // The variable that makes Ajax possible!
  try {
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer Browsers
    try {
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        // Something went wrong
        return false;
      }
    }
  }
  // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function() {
    if(ajaxRequest.readyState == 4) {
      if(ajaxRequest.status == 200) {
        var ajaxDisplay = document.getElementById(divID);
        document.getElementById(divID).innerHTML = ajaxRequest.responseText;
        // eval any Javascript
        var x = document.getElementById(divID).getElementsByTagName("script");
        for(var i=0;i<x.length;i++) {
          eval(x[i].text);
        }
      }
    }
  }
  ajaxRequest.open("GET", url, true);
  ajaxRequest.send(null);
}
