function YouthReachUpdates()
{
//First, let's see if this is a donation page, and if so them Update the OrphanID and associated fields
var site = location.href; 
var page1 = site.indexOf("donor.asp"); 

//Change header width and height
UpdateImage("http://www.youthreach.org/atf/cf/%7BEB087DE9-D966-4A6C-96C5-37E0AC398A13%7D/YouthReachHeader900.jpg",1000,120);
UpdateImage("https://www.kintera.org/atf/cf/%7BEB087DE9-D966-4A6C-96C5-37E0AC398A13%7D/YouthReachHeader900.jpg",1000,120);
UpdateImage("http://www.lemonaidfororphans.org/atf/cf/%7B97011A97-7DCF-4A1A-A336-5BEB4B797C37%7D/YouthReachHeader900.jpg",1000,120);

//Pre-Populate Orphan Sponsorship Form with Orphan ID and amount
if (page1 > 0)
	{ 
		if (querySt("OrphanID") > 0)
			{
				SetCookie("OrphanID",querySt("OrphanID"));
			}
// OLD	UpdateOrphanID (querySt("OrphanID"));
		UpdateOrphanID (GetCookie("OrphanID"));
	}

// Create any YouthReach Cookies for our applications (Orphan Sponsorship, SponsorMail, etc)
SetYouthReachCookies();

if (document.SponsorMail)
	{
		SponsorMail();
	}
}

function SetYouthReachCookies()
{
// Find passed variables in the URL
var OrphanID = querySt("OrphanID");
// set Cookie to last 1 hour
var d = new Date();
d.setTime(d.getTime() + (60*60*1000));

// If OrphanID is a passed variable, set the cookie for it
if (OrphanID != null)
	{
	SetCookie ("OrphanID", OrphanID, d, "/");
	}

// If at home page, set the Sponsor ID Cookie
if ( document.YRSponsor)
		{
		SetCookie ("SponsorName", document.YRSponsor.SponsorName.value, d, "/");
		SetCookie ("SponsorEmail", document.YRSponsor.SponsorEmail.value, d, "/");
		SetCookie ("SponsorID", document.YRSponsor.SponsorID.value, d, "/");
		}

// If at Orphan Detail page, set the Orphan ID Cookies
if ( document.YROrphan)
		{
		SetCookie ("OrphanName", document.YROrphan.OrphanName.value, d, "/");
		SetCookie ("OrphanID", document.YROrphan.OrphanID.value, d, "/");
		}

}

function SponsorMail()
{
// Pre-fill SponsorMail form
var url = "https://spreadsheets.google.com/embeddedform?formkey=dGtGTGdkNEViMXpHVy1FNlFUUENZNWc6MQ"
url = url + "&entry_0=" + GetCookie("OrphanID");
url = url + "&entry_1=" + GetCookie("OrphanName");
url = url + "&entry_3=" + GetCookie("SponsorName");
url = url + "&entry_4=" + GetCookie("SponsorEmail");
url = url + "&entry_5=" + GetCookie("SponsorID");

document.all.SponsorMailFrame.src = url;
}

function UpdateOrphanID(ID)
{

// Auto-populate fields in Orphan Sponsorship form
UpdateField ("SUPPORTER347281","Field5030342","text",ID);
UpdateField ("SUPPORTER347281","RecurringAmount","text",39);

}

function UpdateField(formname,fieldname,type,newvalue)
{
var arr = new Array();

// Find the correct form
for (i=0; i<document.forms.length;i++)
	{
	formObj = document.forms[i];
	if(formObj.name == formname)
		{
		// Look for the right field in that form
		for (n=0; n<formObj.elements.length;n++)
			{
			if(formObj.elements[n].name == fieldname)
			// Now update the field value 
				{
				if(type="text") 
					{
					formObj.elements[n].value = newvalue ;
					}
				if(type="checkbox")
					{
					formObj.elements[n].checked = newvalue;
					}
				}
			}
		}
	}

}

function UpdateImage(imagesrc,newwidth,newheight)
{

// Find the correct image
for (i=0; i<document.images.length;i++)
	{
	imageObj = document.images[i];
	//alert(imageObj.src);
	if(imageObj.src == imagesrc)
		{
		// Update the height and width of the image
		imageObj.height = newheight;
		imageObj.width = newwidth;
		}
	}

}


function querySt(ji) 
// Search through the URL for parameters, and return the value of a passed parameter
	{
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) 
		{
		ft = gy[i].split("=");
		if (ft[0] == ji) 
			{
			return ft[1];
			}
		}
	}
	
//************ START COOKIE LIBRARY -***************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
//************ END COOKIE LIBRARY -***************



// This will run YouthReachUpdates after the page loads (on every page within our website)
window.onload = YouthReachUpdates;

//Google Analytics javascript - added August 24, 2010
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-18150786-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

