/*
 * Function called in ONLOAD for every page to eliminate 
 * use of http://globalgiving.com style URLs, e.g., without
 * a  www hostname prefix.
 */

function forcewww() {
   var url = window.location.href;
   // dev servers do not have .org in host name
   dotcom = url.indexOf(".org");
   if (dotcom > 0 && dotcom < 20) {
      www = url.indexOf("www");
      if (www < 0 ) {
         // Get current site address
         stendndx = url.indexOf("://")+3;
         ststartndx = url.indexOf(".org");
         site = url.substring(ststartndx,stendndx)
         // Get site location, if different than the index page
         uslondx = url.indexOf(".org")+4;
         userlocation = url.substring(uslondx);
         // Redirect users to www site, replacing page in history
         newurl = "http://www." + site + ".org" + userlocation;
         window.location.replace(newurl);
      }
   }
}

// This function lets us grab data out of the URL
function getParam(parmname) { 
   url = window.location.href;
   qndx = url.indexOf("?"); 
   if(qndx < 0) return null; 
   parmndx = url.indexOf(parmname,qndx+1); 
   if(parmndx < 0) return null; 
   eqndx = url.indexOf("=",parmndx); 
   if(eqndx < 0) return null; 
   termndx = url.indexOf("&", eqndx); 
   if( termndx < 0 ) { 
     termndx = url.indexOf("#",eqndx); 
     if(termndx < 0) { 
       return unescape(url.substring(eqndx+1)); 
     } 
   } 
   return unescape(url.substring(eqndx + 1,termndx)); 
 } 

// Function to write links to the photo page
function photoLink(id,cpt,width,height) {
  settings="toolbar=no,location=no,directories=no,"+"status=no,menubar=no,scrollbars=yes,"+"resizable=no,width="+width+",height="+height;
  url = "/photo/photo.php?imgid=" + id + "&cpt=" + cpt;
  newwin=window.open(url,"Win1",settings);
  newwin.focus();
}

// Function to write links to the photo page
function slideshow(id) {
  settings="toolbar=no,location=no,directories=no,"+"status=no,menubar=no,scrollbars=yes,"+"resizable=no,width=700,height=640";
  url = "/gallery2/main.php?g2_itemId=" + id;
  newwin=window.open(url,"Win1",settings);
  newwin.focus();
}


// Function to write links to the photo page
function photoLink2(src,title,desc,width,height) {
  src = escape(src);
  title = escape(title);
  desc = escape(desc);
  settings="toolbar=no,location=no,directories=no,"+"status=no,menubar=no,scrollbars=yes,"+"resizable=no,width="+width+",height="+height;
  url = "/photo/photo.html?img=" + src + "&title=" + title + "&desc=" + desc;
  newwin=window.open(url,"Win3",settings);
  newwin.focus();
}

// Function to write links to the photo page - ENGLISH version
function photoLinkEn(id,cpt,width,height) {
  settings="toolbar=no,location=no,directories=no,"+"status=no,menubar=no,scrollbars=yes,"+"resizable=no,width="+width+",height="+height;
  url = "/en/photo/photo.php?imgid=" + id + "&cpt=" + cpt;
  newwin=window.open(url,"Win1",settings);
  newwin.focus();
}

// Function to write links to the photo page - ENGLISH version
function photoLinkEn2(src,title,desc,width,height) {
  src = escape(src);
  title = escape(title);
  desc = escape(desc);
  settings="toolbar=no,location=no,directories=no,"+"status=no,menubar=no,scrollbars=yes,"+"resizable=no,width="+width+",height="+height;
  url = "/en/photo/photo.html?img=" + src + "&title=" + title + "&desc=" + desc;
  newwin=window.open(url,"Win3",settings);
  newwin.focus();
}

// Function to pop up page
function popUp(page,width,height) {
  settings="toolbar=no,location=no,directories=no,"+"status=no,menubar=no,scrollbars=yes,"+"resizable=no,width="+width+",height="+height;
  newwin=window.open(page,"Win2",settings);
  newwin.focus();
}

// Function to write tournassoud.org email address
function makeEmailTourn(name) {
  document.write('<a href=\"mailto:' + name + '@tournassoud.org\">' + name + '@tournassoud.org</a>')
}


// Function for iframe height    
function getElement(aID) {
    return (document.getElementById) ?
    document.getElementById(aID) : document.all[aID];
}

function getIFrameDocument(aID) { 
    var rv = null; 
    var frame=getElement(aID);
    // if contentDocument exists, W3C compliant (e.g. Mozilla) 
    if (frame.contentDocument)
        rv = frame.contentDocument;
    else // bad IE  ;)
        rv = document.frames[aID].document;
    return rv;
}

function adjustMyFrameHeight(myFrame) {
    var frame = getElement(myFrame);
    var frameDoc = getIFrameDocument(myFrame);
    frame.height = frameDoc.body.offsetHeight;
}
