// Site Registration - SLT - 1.0 , March 24th, 2008

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=/";
}

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);
}

/*

	Cookie Logic -
	
	site_registration_mediaone_inital - Set when user visits the site the first time, saved for 7 days
	site_registration_mediaone_inital_temp - Set when user visits the site the first time, deleted with browser closes
	
	site_registration_mediaone_pageviews - incrementing number
	
	if site_registration_mediaone_inital exists and site_registration_mediaone_inital_temp does not, this signifies the second visit
	
	once the user has registered, or closed the box, site_registration_mediaone_inital_temp will be set to 7 days, or 1 year respectively
	

*/

document.write('<style>');
document.write('#regoverlay{');
document.write('		position: absolute;');
document.write('		top: 0;');
document.write('		left: 0;');
document.write('		z-index: 90;');
document.write('		width: 99.9%;');
document.write('		height:100%;');
document.write('		background-color: #000;');
document.write('		filter:alpha(opacity=80);');
document.write('		-moz-opacity: 0.8;');
document.write('		opacity: 0.8;');
document.write('	}');
document.write('	');
document.write('	');
document.write('	#regpopup {');
document.write('	');
document.write('		position:absolute;');
document.write('		width:100%;');
document.write('		height:500px;');
document.write('		top:0px;');
document.write('		left:0px;');
document.write('		padding-top:25px;');
document.write('		z-index: 100;');
document.write('		display:none;');
document.write('	}');
document.write('	#regclosebutton {');
document.write('		position:absolute;');
document.write('		top:5px;');
document.write('		left:5px;');
document.write('		width:24px;');
document.write('		height:21px;');
document.write('		cursor:pointer;');
document.write('	}');
document.write('	');
document.write('	#regsub {');
document.write('	');
document.write('		border:6px solid #000000;');
document.write('		width:500px;');
document.write('		height:390px;');
document.write('		background-color:#FFFFFF;');
document.write('		padding:10px;');
document.write('		font-family:Arial, Helvetica, sans-serif;');
document.write('		font-size:14px;');
document.write('		position:relative;');
document.write('		color:#000000;');
document.write('	}');
document.write('	');
document.write('	#regsub input {');
document.write('		padding:3px;');
document.write('	}');
document.write('    #registration_frame {');
document.write('	visibility:hidden;');
document.write('	}');
document.write('');
document.write('</style>');
document.write('<div id="regpopup" align="center">');
document.write('<div id="regsub">');
document.write('<table width="500" border="0" cellspacing="4" cellpadding="2" style="padding-top:10px;">');
document.write('<tr>');			   
document.write('<td colspan="3">');
document.write('<div align="center" style="font-weight:bold; padding-bottom:5px;">');
document.write('<img src="http://reg.mediaoneutah.com/TribLogo.gif"><br><br>To keep the Salt Lake Tribune free, please help us by providing more information about you.');
document.write('</div>');
document.write('<div align="center" style="padding-bottom:15px;">');
document.write('Please supply the following information, you must be 16 or older:');
document.write('</div>');
document.write('</td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td width="33%"><div align="right">I am a: </div></td>');
document.write('<td width="15%"><div align="left">');
document.write('<input name="male" type="radio" value="male" id="male" onclick="$(\'female\').checked = false;" checked/> Male</div></td>');
document.write('<td width="52%"><div align="left"><input type="radio" id="female" name="female" value="female" onclick="$(\'male\').checked = false;" /> Female</div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td><div align="right">Born in: </div></td>');
document.write('<td colspan="2"><div align="left"><input type="text" id="birthYear" name="birthYear" value="Year" onClick="this.value=\'\';"  /></div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td><div align="right">And live in: </div></td>');
document.write('<td colspan="2"><div align="left"><input type="text" id="zipcode" name="zipcode" value="Zip Code" onClick="this.value=\'\';" /></div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td><div align="right">Email Address: </div></td>');
document.write('<td colspan="2"><div align="left"><input type="text" id="email" name="email" /></div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td><div align="right"><input type="checkbox" id="alerts" checked></div></td>');
document.write('<td colspan="2"><div align="left">Please send me news alerts and updates to the email address included above.</div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td colspan="2"><div align="right" style="padding-top:15px;"></div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td colspan="3"><div align="right">');
document.write('<input type="submit" value="Register Now" onClick="submitreg()">');
document.write('&nbsp;&nbsp;&nbsp;');
document.write('<input type="button" value="No Thanks" onClick="closeregbox()">');
document.write('</div></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td colspan="3"><div align="center" style="font-size:12px; color:#000000; padding-top:8px;"><a href="javascript:whatinfo();" style="color:#000000;">What will you do with my information?</a></div></td>');
document.write('</tr>');
document.write('</table>');
document.write('</div>');
document.write('</div>');
document.write('<iframe width="1" height="1" border="0" id="registration_frame"></iframe>');
document.write('<div id="regoverlay" style="display:none;"></div>');


var x = readCookie('site_registration_mediaone_inital');

if (x) { // They have been to the site at least once
	
	var y = readCookie('site_registration_mediaone_inital_temp');

	if(y) { // This is their first visit, or the cookie has been set for registration or opt out
	
		
		
	} else { // This is their 2nd visit, we need to wait 3 page views and pop a registration box
	
		var z = readCookie('site_registration_mediaone_pageviews');
		
		if(z) {
			
			var a = readCookie('site_registration_mediaone_pageviews');
			
			if(a == "3") { // 3 page views in
			
				regbox();
				document.getElementById("registration_frame").src = "http://reg.mediaoneutah.com/sltreg.php?action=view";
			
			} else {
				
				createCookie("site_registration_mediaone_pageviews",parseInt(a)+1,7);
				
			}
			
		} else {
		
			createCookie("site_registration_mediaone_pageviews",1,7);
		
		}
	
	}
	
} else {
	createCookie('site_registration_mediaone_inital','1',7);
	createCookie('site_registration_mediaone_inital_temp','1',0);
}


function regbox() {
	//document.getElementById("regoverlay").style.display = "block";
	document.getElementById("regpopup").style.display = "block";
}

function whatinfo() {
	alert("What will you do with my information and why do you need it?\n\nWe are an advertising-supported site. To better serve our advertisers and their customers, we are supplying our advertisers with aggregated gender, age and ZIP code information of our readers. We provide this aggregated information so advertisers can target their advertising more effectively. Nothing in the data identifies you personally to the advertiser, and we will never disclose your email address as we deploy all advertising to ensure your privacy and negate spam / inappropriate content.");
}

function submitreg() {
	
	if(checkform() == true) {
		
		if(document.getElementById("male").checked == true) {
			var sex = "male";	
		} else {
			var sex = "female";			
		}
		
		if(document.getElementById("alerts").checked == true) {
			var alertsopt = "1";	
		} else {
			var alertsopt = "0";			
		}
		
		document.getElementById("registration_frame").src = "http://reg.mediaoneutah.com/sltreg.php?action=accepted&email=" + document.getElementById("email").value  + "&zipcode=" + document.getElementById("zipcode").value + "&birthyear=" + document.getElementById("birthYear").value + "&sex=" + sex + "&alerts=" + alertsopt;
		createCookie('site_registration_mediaone_pageviews','1',365);												
		createCookie('site_registration_mediaone_inital_temp','1',365);	
		document.getElementById("regoverlay").style.display = "none";
		document.getElementById("regpopup").style.display = "none";		
		alert("Thank you for participating!");
		
	}
	
}

function closeregbox() {
	document.getElementById("registration_frame").src = "http://reg.mediaoneutah.com/sltreg.php?action=rejected";	
	document.getElementById("regoverlay").style.display = "none";
	document.getElementById("regpopup").style.display = "none";
	createCookie('site_registration_mediaone_pageviews','1',7);
	createCookie('site_registration_mediaone_inital_temp','1',7);
}

function $(id) {
	return document.getElementById(id);
}

function ajax() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

function checkform() {

	var regerror = "";
	var regerrors = 0;

	if(document.getElementById("birthYear").value == "" || document.getElementById("birthYear").value == "Year" || document.getElementById("birthYear").value.length < 4) {
		regerror += "- Please select your birth year\n";	
		regerrors++;
	}
	if(document.getElementById("zipcode").value == "" || document.getElementById("zipcode").value == "Zip Code" || document.getElementById("zipcode").value.length < 5) {
		regerror += "- Please select your zip code\n";	
		regerrors++;
	}		
	if(document.getElementById("email").value == "" || document.getElementById("email").value.length < 3) {
		regerror += "- Please select your email address\n";	
		regerrors++;
	}

	if(parseInt(document.getElementById("birthYear").value) > 1992) {
		regerror += "- You must be 16 or older to register\n";	
		regerrors++;
	}	
	
	if(regerrors > 0) {
		alert(regerror);
		return false;
	
	} else {
		return true;
	}
	
}
