var lb_active;
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function getPopupPageSize()
{			
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getUpperLeftCorner()
{
	var new_x_coord=0;
	var new_y_coord=0;
	if(document.all)
	{
		new_x_coord=window.pageXOffset;
		new_y_coord=window.pageYOffset;
	}
	else
	{
		new_x_coord=document.body.scrollLeft;
		new_y_coord=document.body.scrollTop;
	}
	
	arrayUpperLeft = new Array(new_x_coord,new_y_coord);
	return arrayUpperLeft;
}

function setFSLBCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function getFSLBCookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

var final_url="";

function updateURLFSLB(donationValue)
{
	if(donationValue!='' && donationValue>0)
	{
		final_url=form_url+"&set.Value="+(donationValue*100);
		document.getElementById('donate_link').href=final_url;
		return true;
	}
	return false;
}
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   if(!updateURLFSLB(myfield.value))
   {
	alert('Please Enter A Donation Amount');
   }
   else
   {
	window.location.href=final_url.toString();
   }
   return false;
   }
else
   return true;
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


	  var popup_overlayurl='http://www.nomoredrugwar.org/sites/all/themes/drugwar/images/black-70.png';


	  var popup_cookie_enabled=true;
	  var popup_cookie_name="NMDWLightbox";
	  var popup_cookie_duration=30;
	  var popup_cookie_value="yes";

      var popup_code='<div id="lbframe"><a href="#" onclick="closeFSLB(); return false;"><img src="http://www.nomoredrugwar.org/sites/all/themes/drugwar/images/lb_close_btn.png" border="0" style="position: absolute; right: 0px; top: 0px; z-index: 100;"/></a><div id="lbcontent"><div id="lbleft"><h2>Help bring an end to the war on drugs.</h2><p>By making a tax-deductible gift today, you\'ll be supporting Drug Policy Alliance\'s work nationwide to roll back the excesses of the drug war and to implement new drug policies based on science, compassion, health and human rights.</p><div id="askstring"><form action="#" method="post" onsubmit="validate(); return false;" name="theform"><input type="radio" value="3622" name="donationValue" id="level120" onclick="donationLevel=this.value;"> <label for="level120">$120</label><br/><input type="radio" value="3625" name="donationValue" id="level60" onclick="donationLevel=this.value;"> <label for="level60">$60</label><br/><input type="radio" value="3623" name="donationValue" id="level35" onclick="donationLevel=this.value;"> <label for="level35">$35</label><br/><input type="radio" value="3624" name="donationValue" id="levelmonthly" onclick="donationLevel=this.value;"> <label for="levelmonthly">Enter a Monthly Amount</label><br/><input type="text" value="" name="monthlyval" id="monthlyval" class="textinput" onfocus="document.getElementById(\'levelmonthly\').checked=true; donationLevel=document.getElementById(\'levelmonthly\').value;" onblur="customValue=this.value"/><br/><input type="radio" value="3621" name="donationValue" id="levelother" onclick="donationLevel=this.value;"> <label for="levelother">Enter an Amount</label><br/><input type="text" value="" name="onetimeval" id="onetimeval" class="textinput" onfocus="document.getElementById(\'levelother\').checked=true; donationLevel=document.getElementById(\'levelother\').value;" onblur="customValue=this.value;" /><br/><br/><input type="image" src="http://www.nomoredrugwar.org/sites/all/themes/drugwar/images/lb_donate_btn.png" width="150" height="49" class="textinput"/></form></div></div><div id="lbright"><img src="http://www.nomoredrugwar.org/sites/all/themes/drugwar/images/nomoredrugwarlogo.png" width="206" height="241" alt="No More Drug War" style="width: 206px; margin: 60px 10px;" /></div></div></div>';
	  
	  function gupFSLB( name )
		{
		  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		  var regexS = "[\\?&]"+name+"=([^&#]*)";
		  var regex = new RegExp( regexS );
		  var results = regex.exec( window.location.href );
		  if( results == null )
			return "";
		  else
		  return results[1];
		}
		
		var closeDisabled=false;
		var popup_height=450;
		var popup_width=628;
		var popup_bordercolor='#000000';
		var popup_bgTitle='#71B8FF';
		var popup_timeout=0;
		var popup_disable_scrollbar=false;
		var myWidth=0;
		var myHeight=0;
		var seen_once=false;
		var opened=false;
		var is_in=false;
		var egwind=null;
		var popup_overlay=null;
		var popup_x=100;
		var popup_y=100;
		var nrp=-1;
		var nrp_show=1;
		var autoclose=null;
		var popup_size_array=null;
		var popup_location_array=null;
		
		
		function initFSLB()
		{
			if(!lb_active) return;
			if(typeof(window.innerWidth)=='number')
			{
				myWidth=window.innerWidth;
				myHeight=window.innerHeight;
			}
			else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
			{
				myWidth=document.documentElement.clientWidth;
				myHeight=document.documentElement.clientHeight;
			}
			else if(document.body && (document.body.clientWidth || document.body.clientHeight))
			{
				myWidth=document.body.clientWidth;
				myHeight=document.body.clientHeight;
			}
			popup_x=myWidth/2-popup_width/2;
			popup_y=myHeight/2-popup_height/2+document.body.scrollTop;
			
			if(popup_x<0) popup_x=0;
			if(popup_y<0) popup_y=0;
			
			var y=document.createElement('div');
			y.setAttribute('id','popup_overlay');
			document.body.appendChild(y);
			var x=document.createElement('div');
			x.setAttribute('id','egwind');
			y.appendChild(x);
			x.style.position='absolute';
			x.style.top='-1000px';
			x.style.left='-1000px';
			//x.style.background='#fdf9f3 url(img/bg_middle.jpg) repeat-y';
			x.style.border='none';//'1px solid '+popup_bordercolor;
			x.style.width=popup_width+'px';
			//x.style.height=popup_height+'px';
			x.style.display='block';
			var xtext="";
			xtext+=popup_code;
			x.innerHTML=xtext;
			egwind=x;
			popup_overlay=document.getElementById('popup_overlay');
			launchFSLB();
		}
		
		
	
		function launchFSLB()
		{
			if(popup_cookie_enabled)
			{
				if(getFSLBCookie(popup_cookie_name)==popup_cookie_value)
				{
					return false;
				}
				setFSLBCookie(popup_cookie_name,popup_cookie_value,popup_cookie_duration, "/", "nomoredrugwar.org", 0);
			}
			if(popup_disable_scrollbar && document.all)
			{
				document.documentElement.style.overflow='hidden';
			}
			else if(popup_disable_scrollbar)
			{
				document.body.style.overflow='hidden';
			}
			opened=true;
			seen_once=true;
			scroll(0,0);
			var x=egwind;
			x.style.top=popup_y+'px';
			x.style.left=popup_x+'px';
			x.style.display='block';
			var y=popup_overlay;
			y.style.position='absolute';
			y.style.top='0px';
			y.style.left='0px';
			
			popup_size_array=getPopupPageSize();
			y.style.width='100%';//document.body.scrollWidth+'px';//popup_size_array[0]+'px';
			y.style.height=popup_size_array[1]+'px';
			y.style.zIndex='999';
			y.style.display='block';
			/*if (BrowserDetect.browser=="Explorer")
			{
				y.filters[0].opacity = "50";
				x.filters[0].opacity = "100";
			}
			else
			{
				y.style.opacity = ".5";
				x.style.opacity = "1";
			}
			y.style.backgroundColor="#000000";*/
			y.style.background="url('"+popup_overlayurl+"')";
			if(popup_timeout>0){autoclose=setTimeout("closeFSLB()",popup_timeout)}return false
		};

		function closeFSLB()
		{
			if(closeDisabled==true) return;
			opened=false;
			egwind.style.display='none';
			popup_overlay.style.display='none';
			if(popup_disable_scrollbar && document.all)
			{
				document.documentElement.style.overflow='auto';
			}
			else if(popup_disable_scrollbar)
			{
				document.body.style.overflow='auto';
			}
			if(autoclose)clearTimeout(autoclose);
			return false;
		};

		addLoadEvent(initFSLB);

var donationLevel="";
var customValue="";
function validate()
{
	if(document.getElementById('levelmonthly').checked)
		customValue=document.getElementById('monthlyval').value;
	if(document.getElementById('levelother').checked)
		customValue=document.getElementById('onetimeval').value;
	
	window.location.href="https://secure2.convio.net/dpa/site/Donation2?df_id=2800&2800.donation=form1&set.DonationLevel="+donationLevel+"&value="+customValue;
}
