﻿function PopupCenter(pageURL, title, w, h, toolbar, menubar) {
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    var targetWin = window.open( pageURL, title, 'toolbar=' + toolbar + ', location=no, directories=no, status=no, menubar=' + menubar + ', scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

function UserMailingListSignup(inputId, loadId, hideId, slideUpDiv, ajaxLoaderImg) {

    var uEmail = $('#' + inputId).val();    
    var legitEmail = checkEmailAddress(uEmail);
  
    if(legitEmail)
    {
        var loadingBlock = '<div style="width:24px; height:24px; margin:4px auto;"><img src="/resources/img/' + ajaxLoaderImg + '" width="24" height="24" alt="loading..." /></div>';

        $('#' + hideId).html(loadingBlock);
        $('#' + hideId).load('/app_clientservices/ajax/mailinglistsignup.aspx?e=' + uEmail);

        if (slideUpDiv) $('#' + loadId).delay(5000).slideUp();
                
    } else {
        $('#' + inputId).attr("value", "");
        $('#' + inputId).focus();
    }
}

function checkEmailAddress(eMail) 
{
    var goodEmail = eMail.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

    if (!goodEmail)
    {
        alert('Please enter a valid e-mail address.');
        return false;
    } else { return true; }
}

//cookies

function createCookie(name, value, days) {
    if(value == '' && readCookie(name) == null) value = 4;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {

    var cookies = document.cookie;

    if (cookies.indexOf(name) != -1) {
        var startpos = cookies.indexOf(name) + name.length + 1;
        var endpos = cookies.indexOf(";", startpos) - 1;
        if (endpos == -2) endpos = cookies.length;
        
        return unescape(cookies.substring(startpos, endpos));
    }
    else {
        return false; // the cookie couldn't be found! it was never set before, or it expired.
    }
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

