// registration cookie management
// paul caiazzo
function createCookie(name,value,days) {
        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=/; domain=.mainetoday.com";
}
function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}
function eraseCookie(name) {
        createCookie(name,"",-1);
}
var registered = readCookie('MT1');
if (registered == null) {

   	// check threshold
   	var currcount = readCookie('MT9') - 0;
	
	if (currcount == 0) {   	
   		// set counter
   		createCookie('MT9',1,31);
   		
   		//check it
   		var chkit = readCookie('MT9');
   		
   		// if (chkit == null)
   		// window.self.location='http://www.mainetoday.com/membercenter/nojscookie.html';
   	}
   	else if (currcount >= thresh) {  	   	
   		// set original page request
   		createCookie('MT8',window.location,365);
   	   	
   		//redirect
		window.self.location='http://www.mainetoday.com/membercenter/register1.html';
	}
	else {
	 	currcount += 1;
   		// update counter
   		createCookie('MT9',currcount,365);	 	
	}
}
else {
	var confirmed = readCookie('MT2');
  
  	if (confirmed == null) {
	   	// set original page request
   		createCookie('MT8',window.location,365);
   		
		// check status cookie
		var status = readCookie('MT0');
	
		if (status > 100) {
			// status is a timestamp, grab today's timestamp
			today = new Date();
			var now = Math.round(today.getTime() / 1000);
			
			/*
			// debug grace
			var shnow = today.getMonth()+'/'+today.getDate()+'/'+today.getFullYear();
			grace = new Date(status * 1000);
			var shstatus = grace.getMonth()+'/'+grace.getDate()+'/'+grace.getFullYear();
			alert('today is '+shnow+'\n'+'grace is '+shstatus);
			*/
			
			// check now vs grace period
			if(now > status) {
				var tryed = readCookie('MT7');
				if (tryed > 0) {
					// last resort, redirect to ...
					window.self.location='http://www.mainetoday.com/membercenter/pageoflastresort.html';				
				}
				else {
				
					// must confirm, redirect to ...
					alert('please confirm your registration.');
					window.self.location='http://www.mainetoday.com/membercenter/almostdone.html';
				}	
			}	
		}
// DPH 2009.02.26
//		else if (status == 1) {
//			//redirect to page 2
//			alert('please complete the registration process.');
//			window.self.location='http://www.mainetoday.com/membercenter/register2.html';	
//		}
//		else if (status == 2) {
//			//redirect to page 3
//			alert('please complete the registration process.');
//			window.self.location='http://www.mainetoday.com/membercenter/register3.html';	
//		}
//		else {
//			//no status and not confirmed, redirect to page 1
//			alert('please complete the registration process.');
//			window.self.location='http://www.mainetoday.com/membercenter/register1.html';	
//		}	
// / DPH

	}
	
	// reset all cookies for confirmed members
	createCookie('MT1',registered,365);
	createCookie('MT2',confirmed,365);
	var status = readCookie('MT0');
	if (status != null) {
		createCookie('MT0',status,365);
	}
	var fname = readCookie('MT3');
	if (fname != null) {
		createCookie('MT3',fname,365);
	}
	var zip = readCookie('MT4');
	if (zip != null) {
		createCookie('MT4',zip,365);
	}	
	var gender = readCookie('MT5');
	if (gender != null) {
		createCookie('MT5',gender,365);
	}	
	var birthy = readCookie('MT6');
	if (birthy != null) {
		createCookie('MT6',birthy,365);
	}			
}


 
