//preload images here
pic1= new Image(100,131); 
pic1.src="/images/epu_Planlogo130.gif"; 
pic2= new Image(50,50); 
pic2.src="/images/epu_black-70.png"; 
pic3= new Image(480,200); 
pic3.src="/images/epu_kids.jpg"; 

var timer2=null;
var hold=false;
var popup_width=625;
var popup_height=520;
var popup_bordercolor='#000000';
var popup_bgTitle='#71B8FF';
//var popup_htmlfile='popup.html';
var popup_overlayurl='/images/epu_black-70.png';
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_code='<div id="main_section" style="padding: 7px; width: 600px; font: 11px Verdana,Arial;"><div style="display: block"><div style="float: left; width: 143px;"><img src="/images/epu_Planlogo130.gif" alt="PlanUSA Logo" width="100" height="131" style="width: 100px; margin: 0px 21px"></div><div style="float: right; width: 443px;"><p style="margin-top: 0px;">Now\'s your chance to receive the latest news about issues concerning children worldwide!<br><br>Join our online community and Plan USA will send you free, regular updates about how our programs are helping children in more than 48 countries. <br><br>You will know what Plan is doing when disaster strikes, how our latest work is making a big difference, <strong>and how you can take action and make a difference in the lives of children around the world!</strong><br><br><strong>Just fill out the form to sign-up today</strong></p></div></div><div style="display: block; float: left; clear: both; width: 586px;"><div style="float: left; width: 286px; margin-left: 200px;"><p style="margin-top: 0px; margin-bottom: 2px;font-weight: bold;">Email: <input type="text" id="fields_email" name="fields_email" style="margin-left: 35px; display: inline;" maxlength="64"></p><p style="margin-top: 2px; margin-bottom: 2px;font-weight: bold;">First Name: <input type="text" id="fields_fname" name="fields_fname" maxlength="20"></p><p style="margin-top: 2px;margin-bottom: 2px; font-weight: bold;">Last Name: <input type="text" id="fields_lname" name="fields_lname" style="margin-left: 3px" maxlength="20"></p><p><input type="button" value="Sign Up" onClick="return verifyepuRequired();"></p></div></div><div style="display: block; width: 480px; margin: 0px 50px 0px 60px;"><img src="/images/epu_kids.jpg" alt="Kids" width="480" height="200" style="border: 1px solid #000000;"></div></div>';

window.onblur=function(){set_hold();};
window.onfocus=function(){timer2=setTimeout("release_hold()", 1000);};

// FUNCTIONS
function returnepuObjById( id )
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}

function verifyepuRequired() {
  if (returnepuObjById("fields_email").value == "") {
    returnepuObjById("fields_email").focus();
    alert("The Email field is required.");
    return false;
  } 
	submitepu_form();

	return true;
}

function setepuCookie( 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 getepuCookie(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;
	}
}

function submitepu_form()
{
	var page = document.getElementById('main_section');
	var createIFrame2 = function(target2) {
				var tgt2 = 'the_iframe';
				var d2 = document.createElement("DIV");
				d2.innerHTML = '<iframe id="' + tgt2 + '" name="' + tgt2 + '" src="about:blank" width=0 height=0 style="border: none"></iframe>';
				target2.appendChild(d2);
				var ifr2 = d2.firstChild;
				ifr2.onreadystatechange = ifr2.onload = function(){
					if(ifr2.readyState && ifr2.readyState != 'loaded' && ifr2.readyState != 'complete') return;
				};
				return tgt2;
	}
	var frm2 = document.createElement('form');
	frm2.method = 'post';
	frm2.name = 'icpsignup';
	frm2.action = 'http://app.icontact.com/icp/signup.php';
	frm2.encoding = 'application/x-www-form-urlencoded';

	var hid1=document.createElement('input');
	hid1.type='hidden';
	hid1.name='listid';
	hid1.value='__multi';

	frm2.appendChild(hid1);

	var hid2=document.createElement('input');
	hid2.type='hidden';
	hid2.name='lists';
	hid2.value='46170:44644:44442';

	frm2.appendChild(hid2);
	
	var hid3=document.createElement('input');
	hid3.type='hidden';
	hid3.name='specialid:44442';
	hid3.value='DH83';

	frm2.appendChild(hid3);
	
	var hid4=document.createElement('input');
	hid4.type='hidden';
	hid4.name='listid:44442';
	hid4.value='on';

	frm2.appendChild(hid4);
	
	var hid5=document.createElement('input');
	hid5.type='hidden';
	hid5.name='specialid:44644';
	hid5.value='9XHN';

	frm2.appendChild(hid5);
	
	var hid6=document.createElement('input');
	hid6.type='hidden';
	hid6.name='listid:44644';
	hid6.value='on';

	frm2.appendChild(hid6);
	
	var hid7=document.createElement('input');
	hid7.type='hidden';
	hid7.name='specialid:46170';
	hid7.value='NDXU';

	frm2.appendChild(hid7);

	var hid8=document.createElement('input');
	hid8.type='hidden';
	hid8.name='listid:46170';
	hid8.value='on';

	frm2.appendChild(hid8);
	
	var hid9=document.createElement('input');
	hid9.type='hidden';
	hid9.name='clientid';
	hid9.value='120534';

	frm2.appendChild(hid9);
	
	var hid10=document.createElement('input');
	hid10.type='hidden';
	hid10.name='formid';
	hid10.value='2681';

	frm2.appendChild(hid10);
	
	var hid11=document.createElement('input');
	hid11.type='hidden';
	hid11.name='reallistid';
	hid11.value='1';

	frm2.appendChild(hid11);
	
	var hid12=document.createElement('input');
	hid12.type='hidden';
	hid12.name='doubleopt';
	hid12.value='0';

	frm2.appendChild(hid12);
	
	var hid13=document.createElement('input');
	hid13.type='hidden';
	hid13.name='fields_salutation';
	hid13.value='Friend';

	frm2.appendChild(hid13);
	
	var hid14=document.createElement('input');
	hid14.type='hidden';
	hid14.name='fields_testgroup';
	hid14.value='lightbox';

	frm2.appendChild(hid14);
	
	var hid15=document.createElement('input');
	hid15.type='hidden';
	hid15.name='redirect';
	hid15.value='http://www.planusa.org/site/emailings_thanks.php';

	frm2.appendChild(hid15);
	
	var hid16=document.createElement('input');
	hid16.type='hidden';
	hid16.name='errorredirect';
	hid16.value='http://www.icontact.com/www/signup/error.html';

	frm2.appendChild(hid16);
	

	var emailfld=document.createElement('input');
	emailfld.type='hidden';
	emailfld.name='fields_email';
	emailfld.value=returnepuObjById('fields_email').value;

	frm2.appendChild(emailfld);
	
	var fnamefld=document.createElement('input');
	fnamefld.type='hidden';
	fnamefld.name='fields_fname';
	fnamefld.value=returnepuObjById('fields_fname').value;

	frm2.appendChild(fnamefld);
	
	var lnamefld=document.createElement('input');
	lnamefld.type='hidden';
	lnamefld.name='fields_lname';
	lnamefld.value=returnepuObjById('fields_lname').value;

	frm2.appendChild(lnamefld);
	
	if(!frm2.target) frm2.target = createIFrame2(page);
	page.appendChild(frm2);
	frm2.submit();
	
	closeepuWindow();
}


function mmove(e)
{
	if(opened)return true;
	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;
	}
	var posx=0;
	var posy=0;

	if(!e) var e=window.event;
	if(e.pageX || e.pageY)
	{
		posx=e.pageX;
		posy=e.pageY
	}
	else if(e.clientX || e.clientY)
	{
		posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
		posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;
	}
	popup_x=myWidth/2-popup_width/2;
	popup_y=myHeight/2-popup_height/2+document.body.scrollTop;

	if(posy<10 + document.body.scrollTop)
	{
		if(!is_in)nrp++;
		is_in=true;
		openepuWindow();
		return true;
	}
	else
	{
		is_in=false;
	}
	return true;
}
/*
function addepuLoadEvent(func)
{
var oldonload=window.onload;
if(typeof window.onload!='function')
{
	window.onload=func;
}
else
{
window.onload=function(){if(oldonload){oldonload()}func()}}};

addepuLoadEvent(popup_init);
*/

// see checkout.js for set_hold and release_hold
function popup_init()
{
	document.onmousemove=mmove;
	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='#ffffff';
	x.style.border='1px solid '+popup_bordercolor;
	x.style.width=popup_width+'px';
	x.style.height=popup_height+'px';
	x.style.display='block';
	var xtext="<div style='border-bottom:1px solid "+popup_bordercolor+" !important;text-align:right !important;background:"+popup_bgTitle+" !important;padding:5px !important;'><a href='#' style='font-family:verdana !important;font-weight:bold !important;font-size:13px !important;color:#000000 !important; text-decoration: none !important;' onClick='closeepuWindow(); return false;'>[X]</a></div>";
	xtext+=popup_code;
	x.innerHTML=xtext;
	egwind=x;
	popup_overlay=document.getElementById('popup_overlay');
}
	
function openepuWindow()
{
	if(opened)return false;
	if(hold)return false;
	if(nrp%nrp_show!=0){opened=false;return false}
	if(getepuCookie("ExitPopupSeen")=="yes") return false;
	if(seen_once) return false;
	setepuCookie("ExitPopupSeen","yes",365, "/", "planusa.org", 0);
	opened=true;
	seen_once=true;
//	returnepuObjById("mediabox").style.display="none";
//	returnepuObjById("filler").style.display="block";
	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';
	y.style.width=document.body.scrollWidth+'px';
	y.style.height=document.body.scrollHeight+'px';
	y.style.height='100%';
	y.style.zIndex='999';y.style.display='block';
	y.style.background="url('"+popup_overlayurl+"')";
	document.body.style.height='100%';
	if(popup_disable_scrollbar)document.body.style.overflow='hidden';
	if(popup_timeout>0){autoclose=setTimeout("closeepuWindow()",popup_timeout)}
	return false
};

function closeepuWindow()
{
	opened=false;
	egwind.style.display='none';
	popup_overlay.style.display='none';
//	returnepuObjById("filler").style.display="none";
//	returnepuObjById("mediabox").style.display="block";
	if(popup_disable_scrollbar)document.body.style.overflow='auto';
	if(autoclose)clearTimeout(autoclose);
	return false
};


