/* ------------------------------------------------------------ 
   Copyright (c) 2003 ManyFutures, Inc. All rights reserved.
   ------------------------------------------------------------ 
   FILE: globalgiving.js
   DESCRIPTION: Site wide javascript functions for globalgiving.
   We try to place all (most) javascript into one file that will
   be downloaded once and cached for all pages.
   ------------------------------------------------------------ */

/* ********************* GENERAL ** GENERAL ***************** */
/* ********************* GENERAL ** GENERAL ***************** */
/* ********************* GENERAL ** GENERAL ***************** */

/*
 * 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;
   // staging-test servers do not have .com in host name
   dotcom = url.indexOf(".com");
   if (dotcom > 0 && dotcom < 20) {
      www = url.indexOf("www");
      if (www < 0 ) {
         // Get current site address
         stendndx = url.indexOf("://")+3;
         ststartndx = url.indexOf(".com");
         site = url.substring(ststartndx,stendndx)
         // Get site location, if different than the index page
         uslondx = url.indexOf(".com")+4;
         userlocation = url.substring(uslondx);
         // Redirect users to www site, replacing page in history
         newurl = "http://www." + site + ".com" + userlocation;
         window.location.replace(newurl);
      }
   }
}

/*
 * Create Hope Give Button
 */

function giveCH(projid) {

// No Parent Opened Us
var CHUrl1 = "https://www.givingstation.com?cid=7230&glgvid="; 
// CH Parent Opened Us
var CHUrl2 = "https://www.givingstation.com/pledge_/pledge_begin_process.cfm?reglgvid=";

  if( top.opener != null && top.location  != location ) {
 // && null != top.opener.location.href.match(/givingstation/)
   top.opener.location = CHUrl2 + projid;
   top.close();
   return;
  }
  top.location = CHUrl1 + projid;
}

/* ********************* EMAIL ** EMAIL ***************** */
/* ********************* EMAIL ** EMAIL ***************** */
/* ********************* EMAIL ** EMAIL ***************** */

/*
 * Functions used to mask email addresses throughout website.
 * Variations on subject line, body or email address style
 */

function mkaddr5(addr,subj,body,label) {
  document.write('<a class="menutab" href=\"mailto:' + addr + '@' + site 
	+ '?subject=' + subj + '&body=' + body + '\">' 
	+ label + '</a>');
}

function mkaddr4(addr,subj,body,label) {
  site = "globalgiving.com";
  document.write('<a href=\"mailto:' + addr + '@' + site 
	+ '?subject=' + subj + '&body=' + body + '\">' 
	+ label + '</a>');
}

function mkaddr2(addr,label) {
  site = "globalgiving.com";
  document.write('<a href=\"mailto:' + addr + '@' + site 
        + '\">' + label + '</a>');
}

function mkaddr(addr) {
  site = "globalgiving.com";
  mkaddr2(addr,addr + '@' + site);
}

function protectaddr(addr,domain,label) {
	document.write('<a href=\"mailto:' + addr + '@' + domain 
		+ '\">' + label + '</a>');
}
 

function emailafriend(body,url,lbl) {
    document.write('<a href="mailto:?body='+body+'  Please visit our webpage at http://www.globalgiving.com/'+url+'">'+lbl+'</a>');
}
1
/* ********************* POP UPS * POP UPS ***************** */
/* ********************* POP UPS * POP UPS ***************** */
/* ********************* POP UPS * POP UPS ***************** */

/*
 * Functions that call up a pop-up window.  Settings vary for new window.
 */

// Simple browser window without features
function popup(url, name, width, height) {
  settings=
  "toolbar=no,location=no,directories=no,"+
  "status=no,menubar=no,scrollbars=no,"+
  "resizable=no,width="+width+",height="+height;
		  
  MyNewWindow=window.open(url,name,settings);
}

// Simple browser window with only a scrollbar
function popup2(url, name, width, height) {
  settings=
  "toolbar=no,location=no,directories=no,"+
  "status=no,menubar=no,scrollbars=yes,"+
  "resizable=no,width="+width+",height="+height;
		  
  MyNewWindow=window.open(url,name,settings);
 }

function howitworks(url, name) {
	settings="toolbar=no,location=no,directories=no,"+
	"status=yes,menubar=no,scrollbars=yes,"+
	"resizable=yes,width=520,height=600";
	HowitWorks=window.open(url,name,settings);
}

/* ********************* DONATIONS ** DONATIONS ***************** */
/* ********************* DONATIONS ** DONATIONS ***************** */
/* ********************* DONATIONS ** DONATIONS ***************** */

/*
 * The following are several functions used in the donation pages. 
 * They range from taking data out of the URL to display on page to
 * creating hyperlinks to giftany functions and comments for verisign.
 */

// This function lets us grab data out of the URL
function getUrlParm(url,parmname) { 
   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)); 
 } 

// This function makes the hyperlink to the gift any feature
function makehyperlink(lbl) {
    var url = window.location.href
    var projid = getUrlParm(url,"pid");
    var projpath = getUrlParm(url,"path");
    var projtitle = getUrlParm(url,"ptitle");
    var resturl = "/dy/gifts/gifta.html?projid="+projid+"&projpath="+projpath+"&projtitle="+projtitle;
    document.write('<a href="'+resturl+'">'+lbl+'</a>')
}

// Test to make sure entered donation amount is valid
function is_a_num(number) {
    var tester = number * 1;
    // Check to make sure field is not blank
    if ((number == null) || (number.length == 0)){
	alert("Please enter a donation amount");
        return false;
    }
    // Check to make sure number is not negative or 0
    if (number < 1){
	alert("Please enter a positive donation amount");
        return false;
    }
    // Check to make sure number is indeed a number
    if (isNaN(tester) == true){
	alert("Please enter a valid donation amount");
        return false;
    }
    return true;
}

// Create comment field for verisign and submit verisign form
function descvalue() {
    var url = window.location.href
    // Gathering variables needed for comment field
    var projid = getUrlParm(url,"pid");
    var projtitle = getUrlParm(url,"ptitle");
    var donation = document.amt.AMOUNT.value;
    // Converting data from checkboxes into variables
    if(document.amt.ANON.checked){
       var anonymous = "a";
    } else {
      var anonymous = "p";
    }
    if(document.amt.NEWSL.checked){
      var newsletter = "r";
    } else {
      var newsletter = "u";
    }
    // Creating the verisign comment and description field
    document.amt.DESCRIPTION.value = (unescape(projtitle+" ("+projid+")"));
    document.amt.COMMENT1.value = ("d:-1:"+projid+":"+anonymous+":"+newsletter+":"+donation);
    // Submiting form if donation amount is a valid number
    if ( is_a_num(donation) == true ) {
       document.amt.submit();
    }
}

/* ********************* GIFTS GIFTS GIFTS ***************** */
/* ********************* GIFTS GIFTS GIFTS ***************** */
/* ********************* GIFTS GIFTS GIFTS ***************** */
/* ********************* GIFTS GIFTS GIFTS ***************** */

/*
 * The following functions let the user preview the information entered
 * for the gift cards.  there are two functions now as there are two cards
 */

// Gift Any Preview
function preview() {
  var projtitle = document.gift.projtitle.value;
  var projid = document.gift.projid.value;
  var projpath = document.gift.projpath.value;
  var recipient = document.gift.recip_fname.value;
  var message = document.gift.recip_msg.value;
  if (message.length > 200) {
     alert("The personalized message is above 200 characters, please reduce it.");
     var msgerror = "toolong";
  };
  var sender = document.gift.sender_fname.value;
  var sendlast = document.gift.sender_lname.value;		  
  if ((msgerror == null) || (msgerror.length == 0)) {
       popup("/gifts/giftprev.html?ptitle="+projtitle+"&pid="+projid+
	     "&path="+projpath+
             "&recname="+recipient+"&recmsg="+message+"&sender="+sender+
             "&lastname="+sendlast, 'Win1', 600, 430); return false;
  }
}

// Gift Package Preview
function preview2() {
  var message = document.gift.recip_msg.value;
  if (message.length > 200){
     alert("The personalized message is above 200 characters, please reduce it.");
     var msgerror = "toolong";
  };
  var sendfirst = document.gift.sender_fname.value;
  var sendlast = document.gift.sender_lname.value;
  var sender = sendfirst + " " + sendlast
  var pid = document.gift.projid.value;
  var path = document.gift.projpath.value;
  var text = document.gift.cardtext.value;
  cardtext = text.replace(/__/, sender);
  if ((msgerror == null) || (msgerror.length == 0)) {
       popup("/gifts/giftpkgprev.html?cardtext="+cardtext+"&pid="+pid+
	     "&path="+path+
             "&recmsg="+message+"&sender="+sendfirst+"&lastname="+sendlast, 
             'Win1', 600, 400); return false;
  };
}

/* ********************* URL MAKER ***************** */
/* ********************* URL MAKER ***************** */
/* ********************* URL MAKER ***************** */

/*  
 * Make the url to run htdig search on project partners for aboutds page
 */

function htdigurl(ngoname) {
document.write('<a href="/cgi-bin/htsearch?config=htdig&amp;restrict=&amp;exclude=&amp;method=and&amp;format=long&amp;sort=score&amp;words='+ngoname+'">View projects</a>')
    }

/*  
 * Make the url for the give now button to link to the donate now page with
 * the project title and project id included in the url.
 */

function giveurl(pid,ptitle,path) {
    document.write('<a href="/donate.html?ptitle='+ptitle+'&pid='+pid+'&path='+path+'"><img alt="Give Now" border="0" src="/cb/hp/img/givenow.gif"></a>')
	}

/*  
 * Make the url for the give now button to link to the donate now page with
 * the project title and project id included in the url.  This is for cobrands
 * Where the donate page is in the image directory
 */

function giveurl2(pid,ptitle) {
    document.write('<a href="img/donate.html?ptitle='+ptitle+'&pid='+pid+'"><img alt="Give Now" border="0" src="/cb/hp/img/givenow.gif"></a>')
	}

/* ********************* Country info pages ***************** */
/* ********************* Country info pages ***************** */
/* ********************* Country info pages ***************** */


/*  
 * Makes sure graph bar width is always at least 1 on country stat pages
 */

function ctrywid(wid) {
    if ( wid < 2 ) {
	width = 2;
    }
	}

/*  
 * Navigate between various country info pages
 */

function gotoctry(page){
    selectedctry = document.ctry_nav.recip_ctryid.selectedIndex;
    ctryid = document.ctry_nav.recip_ctryid.options[selectedctry].value;
    ctryurl = '/ctry/ctryi' + page + ctryid + '.html'
    // alert("this is the url:   " + ctryurl)
    location.href = ctryurl;
}

/*  
 * Return to index or hall of fame index pages
 */

function indxlink(url){
    opener.location = url;
    window.close()
}
