//
// support code for Mapping Controversies home page
//

// Dreamweaver functions
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// function checks if PNGs need to be fixed, and if so,
// calls correctPNG()
// (see pngfix.js)
function fixpng() {
  if (typeof needtofixpng != 'undefined' && needtofixpng)
    correctPNG();
}


//
// Random photos for home page
//
function write_random_home_photo_set() {
  var photosets = new Array('/styles/homephotoset1.css','/styles/homephotoset2.css','/styles/homephotoset3.css',
                            '/styles/homephotoset4.css','/styles/homephotoset5.css','/styles/homephotoset6.css','/styles/homephotoset7.css');
  var nphotoset = Math.floor(Math.random()*photosets.length);
  document.writeln("<link rel='stylesheet' href='"+photosets[nphotoset]+"' type='text/css' media='all' />");
}
//
// Random photos for site interior
//
function write_random_photo() {
  var photos = new Array('/img/lev2/photo01.jpg','/img/lev2/photo02.jpg','/img/lev2/photo03.jpg','/img/lev2/photo04.jpg',
                         '/img/lev2/photo05.jpg','/img/lev2/photo06.jpg','/img/lev2/photo07.jpg',/*'/img/lev2/photo08.jpg',*/
                         '/img/lev2/photo09.jpg','/img/lev2/photo10.jpg','/img/lev2/photo11.jpg','/img/lev2/photo12.jpg');
  var nphoto = Math.floor(Math.random()*photos.length);
  document.writeln("<div id='divphoto'><img src='"+photos[nphoto]+"' alt='' /></div>");
}


//
// validate email address
// return true if valid, false if not
//
function is_valid_email(str) {
  var regex_email = new RegExp("^[-.\\w]{2,}@([-\\w]{1,}\\.)+[\\w]{2,4}$");
  return (regex_email.test(str) && str.length <= 250);
}
//
// trim whitespace from string
//
function trim(str) {
  str = str.replace(/\s*$/, '');
  return str.replace(/^\s*/, '');
}
//
// get value of radio buttons
// pass in the radio set array, i.e. xform.rating
//
function get_radio_value(obj) {
  for (var i = 0; i < obj.length; i++) {
    if (obj[i].checked) {
      return obj[i].value;
    }
  }
  return undefined;
}
//
// set check state of radio buttons
// pass in the radio set array, i.e. xform.rating, and the value to look for
//
function check_radio_by_value(obj, val) {
  for (var i = 0; i < obj.length; i++) {
    obj[i].checked = (obj[i].value == val ? true : false);
  }
}

///
/// cookies
///
// get cookie value
//
function get_cookie(cookiename, defaultvalue) {
  var ret = defaultvalue;
  var cookies = document.cookie.split(';');
  for (i = 0; i < cookies.length; i++) {
    if (cookies[i].indexOf(cookiename) > -1) { // found in cookie
      cookieparts = cookies[i].split('=')
      if (typeof cookieparts[1] != 'undefined')
        ret = unescape(cookieparts[1]);
      break;
    }
  }
  return ret;
}
// set cookie value
//
function set_cookie(cookiename, value) {
  //var expire = new Date ();
  //expire.setTime (expire.getTime() + (10*365*24*60*60*1000)); //expires in 10 years
  //expire = '; expires='+expire.toGMTString();
  var expire = ''; // let it expire when browser closes
  var str = cookiename+'='+escape(value)+'; path=/'+expire;
  document.cookie=str;
}



//
// Pull in CSS that makes things open/close.
//
function initialize_opener_css() {
  document.writeln("<link rel='stylesheet' href='/styles/opener.css' type='text/css' media='all' />");
}


//
// Open/close content
// This function assumes the following:
// -The object calling this function is inside another element with class="open" or class="closed"
// This function looks for the first containing element with either of those classes,
// and changes it to the other class (in other words, it acts like a toggle). The rest must be done by CSS.
//
function opencontent(obj) {
  if (typeof obj.className != 'undefined') {
    if (obj.className == 'open') {
      obj.className = 'closed';
      return;
    }
    else if (obj.className == 'closed') {
      obj.className = 'open';
      return;
    }
    else if (typeof obj.parentNode != 'undefined') {
      opencontent(obj.parentNode);
    }
  }
}

//
// Open/close all
// This function opens all closed divs, or closes all open divs,
// depending on which state it is in currently
//
var allcontentopen = false;
function openallcontent() {
  // make sure browser supports method
  if(!document.getElementsByTagName)
    return;
  // get DIV nodes
  var nodes = document.getElementsByTagName("DIV");
  if (!nodes)
    return;

  // iterate through list
  for(var i = 0; i < nodes.length; i++) {
    var obj = nodes.item(i);
    if (typeof obj.className != 'undefined') {
      if (allcontentopen) {
        // close anything that is open
        if (obj.className == 'open') {
          obj.className = 'closed';
        }
      }
      else {
        // open anything that is closed
        if (obj.className == 'closed') {
          obj.className = 'open';
        }
      }
    }
  }
  allcontentopen = (allcontentopen ? false : true);
}

