var spinner='
';
//var logout_copy='
You are currently signed in, please sign out to register for a new account.
';
var site='rcw';
//form for sign in pop up
var sign_in_copy='';
var sign_up_copy='';
var request_password_copy='';
if (document.images)
{
// preload_image = new Image(18,15);
// preload_image.src="http://util.realclearpolitics.com/comments/images/spinner_email.gif";
}
//user vars
//hash for user
var realclear_key = readCookie('realclear_key');
//user id
var realclear_user = readCookie('realclear_user');
//user vars -- username, email etc.
var realclear_vars = readCookie('realclear_vars');
$(document).ready(function(){
auth();
});
//function to see if someone is signed in
function auth(){
var auth_content = '';
//if signed in cookie exists, display accordingly
if(readCookie('realclear_key')){
auth_content = auth_in(); //username|signout menu
$('.auth').html(auth_content);
}else{
auth_content = auth_out(); //signed out menu -> signup|signin
$('.auth').html(auth_content);
}
//assigns the sign in button to raise pop up
sign_in_pop();
//assigns the sign up button to raise pop up
sign_up_pop();
//assign sign out click
sign_out_click();
}
function auth_out(){
var content = '';
//CHECK IF DISQUS COMMENTS MODULE IS ON THE PAGE
//if(document.getElementById("disqus_thread")){
if($("disqus_thread").length>0){
content += '';
}
content += 'Sign up | Sign in';
return content;
}
function auth_in(){
return ''+realclearVars(2)+'\'s account | Sign out';
}
//assigns the sign in button(s) action
function sign_in(){
$('.sign_in').unbind();
$('.sign_in').click(function() {
//var c=$(this).parent('.sign_in_form');
//alert(c.innerHTML);
var user_email = $('.sign_in_form #sign_in_user_email').val();
var user_password = $('.sign_in_form #sign_in_user_password').val();
var zelda = $('.sign_in_form #zelda').val();
clearLocalCookies(1,1);
verified = true;
errorString = '';
if(validEmail(user_email)==false || user_email==''){
verified=false;
errorString += '*Email address not valid.
';
}
if(user_password==''){
verified=false;
errorString += '*Please fill in password field.
';
}
if(verified==true){
$.ajax({
url: "https://util.realclearpolitics.com/comments/process/post_to_login.php?jsoncallback=?",
type: "POST",
crossDomain: true,
data: {
zelda: zelda,
email: user_email,
password: user_password
},
success: function(data) {
//if there is a php error (we check with js first, sometimes an error might get to php we double check)
if(data.status==1){
setLocalCookies(data);
jQuery(document).trigger('close.facebox');
}else{
$('#errorDisplay').html("There was an error validating credentials. Please try again.");
}
}
});
}else{
$('#errorDisplay').html(errorString);
}
});
}
//function assigns the sign_in_drop class links to open up the pop up
function sign_in_pop(){
$('.sign_in_pop').unbind();
$('.sign_in_pop').click(function() {
jQuery.facebox(sign_in_copy);
//inside the pop up there is a sign in button, this instantiates that
sign_in();
sign_up_pop();
forgot_pass_pop();
});
}
//function assigns the sign_in_drop class links to open up the pop up
function sign_up_pop(){
$('.sign_up_pop').unbind();
$('.sign_up_pop').click(function() {
jQuery.facebox(sign_up_copy);
//inside the pop up there is a sign in button, this instantiates that
sign_up();
sign_in_pop();
});
}
//LOADS FACEBOX USER ACCOUNT PROFILE
function loadUserAccount(user_id,type){
if(user_id!=''){
$.getJSON("https://util.realclearpolitics.com/comments/process/loadAccount.php?jsoncallback=?",{
user_id: user_id,
type: type
}, function(data) {
if(data.status==0){
jQuery.facebox('Error!
'+data.msg+'');
}else if(data.status==1){
jQuery.facebox('Account
'+data.msg+'');
$('#facebox .sign_in_form').width('400px');
}else{
alert('uh oh!');
}
});
}
}
//SETS CLICK FUNCTIONALITY FOR USER-PROFILE REST PASSWORD BUTTON
function changePassword_pop(){
verified = true;
errorString = '';
var user_id = $('.sign_in_form #user_id').val();
var old_password = $('.sign_in_form #old_password').val();
var new_password = $('.sign_in_form #sign_in_password').val();
var new_password_re = $('.sign_in_form #sign_in_repassword').val();
var zelda = $('.sign_in_form #zelda').val();
if(old_password=='' || new_password=='' || new_password_re==''){
verified=false;
errorString += '*Please fill in all fields.
';
}
if(new_password!=new_password_re){
verified=false;
errorString += '*New passwords do not match.
';
}
if(user_id==''){
verified=false;
errorString += '*User id not present.
';
}
if(verified==true){
$('#errorDisplay').html('');
if(zelda==''){
$.ajax({
url: "https://util.realclearpolitics.com/comments/process/post_to_changePassword.php",
type: "POST",
crossDomain: true,
data: {
user_id: user_id,
old_password: old_password,
new_password: new_password
},
success: function(data) {
if(data.status==0){
$('#errorDisplay').html(data.msg);
}else if(data.status==1){
$('#errorDisplay').html(data.msg);
$('.sign_in_form #old_password').val('');
$('.sign_in_form #sign_in_password').val('');
$('.sign_in_form #sign_in_repassword').val('');
}else{
alert('uh oh!');
}
}
});
}else{
alert('uh oh!');
}
}else{
$('#errorDisplay').html(errorString);
}
}
//function sets new email address
function changeEmail_pop() {
var user_id = $("input#user_id").val();
var new_email = $("input#new_email").val();
var password = $("input#sign_in_password").val();
var zelda = $("input#zelda").val();
$.ajax({
url: "https://util.realclearpolitics.com/comments/process/post_to_changeEmail.php",
type: "POST",
data: {
user_id: user_id,
sign_in_password: password,
new_email: new_email
},
success: function(data) {
if(data.status==0){
$('#errorDisplay').html(data.msg);
}else if(data.status==1){
jQuery.facebox('Congratulations!
'+data.msg+'');
}else{
alert('uh oh!');
}
}
});
}
//function assigns the sign_in_drop class links to open up the pop up
function forgot_pass_pop(){
$('.sign_in_fp').unbind();
$('.sign_in_fp').click(function() {
jQuery.facebox(request_password_copy);
sign_up_pop();
sign_in_pop();
forgot_pass();
});
}
function forgot_pass(){
$('.request_password_submit').unbind();
$('.request_password_submit').click(function() {
var user_email = $('.sign_in_form #sign_in_user_email').val();
var zelda = $('.sign_in_form #zelda').val();
if(!validEmail(user_email) && user_email==''){
$('#errorDisplay').html('*Email address not valid.
');
}else{
$('#errorDisplay').html('');
if(zelda==''){
$.getJSON("https://util.realclearpolitics.com/comments/process/request_password.php?jsoncallback=?",{
zelda: zelda,
user_email: user_email
}, function(data) {
if(data.status==0){
$('#errorDisplay').html(data.error);
}else if(data.status==1){
jQuery.facebox('Congratulations!
'+data.success+'');
}else{
alert('uh oh!');
}
});
}
}
});
}
function sign_up(){
$('.sign_up').unbind();
$('.sign_up').click(function() {
confirmSignUp();
});
//CHECK EMAIL MAKE SURE NOT ALREADY BEING USED
$('.sign_in_form input[name=sign_in_user_email]').blur(function() {
var email = $(this).val();
$.getJSON("https://util.realclearpolitics.com/comments/process/check_email.php?jsoncallback=?",{
zelda: "",
user_email: email
}, function(data) {
if(data.status==0){
$('.sign_in_form input[name=sign_in_user_email]').addClass("fill");
$('.sign_in_form #email_status').html('*'+data.error);
}else{
$('.sign_in_form input[name=sign_in_user_email]').removeClass("fill");
$('.sign_in_form #email_status').html('*is available');
}
$('.sign_in_form #email_status').show();
});
});
$('.sign_in_form input[name=user_displayname]').blur(function() {
var username = $(this).val();
$.getJSON("https://util.realclearpolitics.com/comments/process/check_username.php?jsoncallback=?",{
zelda: "",
user_name: username
}, function(data) {
if(data.status==0){
$('.sign_in_form input[name=user_displayname]').addClass("fill");
$('.sign_in_form #username_status').html('*'+data.error);
}else{
$('.sign_in_form input[name=user_displayname]').removeClass("fill");
$('.sign_in_form #username_status').html('*is available');
}
$('.sign_in_form #username_status').show();
});
});
}
function sign_out_click(){
$('.sign_out').unbind();
$('.sign_out').click(function() {
sign_out();
});
}
function sign_out(){
clearLocalCookies(1,2);
$('.auth').html(auth_out());
//IF DISQUS COMMENTS MODULE -- HIDE POST FORM
if(document.getElementById("disqus_thread")){
$('#dsq-content #dsq-new-post').hide();
}
//assigns the sign in button to raise pop up
sign_in_pop();
//assigns the sign up button to raise pop up
sign_up_pop();
}
function confirmSignUp() {
dataVerified = verifyData('.required');
if(dataVerified){
$('button.sign_up').after('
');
var c=$('.sign_in_form');
var zelda=c.find('input[name=zelda]').val();
var site = getDomain();
var user_email = c.find('input[name=sign_in_user_email]').val();
var display_name = c.find('input[name=user_displayname]').val();
var user_pass = c.find('input[name=sign_in_password]').val();
var user_pass_again = c.find('input[name=sign_in_repassword]').val();
var user_title = c.find('select[name=user_title]').val();
//var user_age = c.find('select[name=user_age]').val();
var user_birthday = c.find('input[name=user_birthday]').val();
var g='';
if($("input[name='user_gender']:checked").val()){
g=$("input[name='user_gender']:checked").val();
}
var ageo=0;
if($("input[name='user_age_over']:checked").val()){
ageo=$("input[name='user_age_over']:checked").val();
}
if(ageo!=1){
$('#errorDisplay').html('*You must be over 13yrs old to sign up.
');
return;
}
if(isNaN(user_birthday) && user_birthday!=''){
$('#errorDisplay').html('*Age birthday is not a valid number.
');
return;
}
if(zelda==''){
var current_loc = window.location.pathname;
$.ajax({
url: "https://util.realclearpolitics.com/comments/process/post_to_register.php",
type: "POST",
data: {
zelda: zelda,
user_email: user_email,
site: site,
user_password: user_pass,
user_password_again: user_pass_again,
user_displayname: display_name,
user_title: user_title,
user_gender: g,
user_age: user_birthday,
redirect_to: 'http://'+location.host+current_loc
},
success: function(data) {
if(data.status==0){
jQuery.facebox('Uh Oh!
'+data.error+'');
sign_in_pop();
sign_up_pop();
}else if(data.status==1){
jQuery.facebox('Congratulations!
'+data.success+'');
}else{
alert('uh oh!');
}
$('.think').html('');
}
});
}
}
}
var cookieSetCounter = 0;
function countCookieSets(){
cookieSetCounter++;
if(cookieSetCounter==6){
location.reload(true);
}
}
function clearLocalCookies(level,type){
eraseCookie('realclear_vars');
eraseCookie('realclear_user');
eraseCookie('realclear_key');
$.getJSON("http://www1.realclearpolitics.com/dev/user_sign_out.php?jsoncallback=?", { level: level }, function(data) {
if(type=='2'){
countCookieSets();
}
});
$.getJSON("http://dyn.realclearpolitics.com/dev/user_sign_out.php?jsoncallback=?", { level: level }, function(data) {
if(type=='2'){
countCookieSets();
}
});
$.getJSON("http://www.realclearworld.com/dev/user_sign_out.php?jsoncallback=?", { level: level }, function(data) {
if(type=='2'){
countCookieSets();
}
});
$.getJSON("http://www1.realclearmarkets.com/dev/user_sign_out.php?jsoncallback=?", { level: level }, function(data) {
if(type=='2'){
countCookieSets();
}
});
$.getJSON("http://www1.realclearsports.com/dev/user_sign_out.php?jsoncallback=?", { level: level }, function(data) {
if(type=='2'){
countCookieSets();
}
});
$.getJSON("http://www.realclearscience.com/dev/user_sign_out.php?jsoncallback=?", { level: level }, function(data) {
if(type=='2'){
countCookieSets();
}
});
$.getJSON("http://www.realcleardefense.com/dev/user_sign_out.php?jsoncallback=?", { level: level }, function(data) {
if(type=='2'){
countCookieSets();
}
});
}
function setLocalCookies(data){
//alert('creating cookie');
eraseCookie('realclear_vars');
eraseCookie('realclear_user');
eraseCookie('realclear_key');
createCookie('realclear_vars',data.realclear_vars,365);
createCookie('realclear_user',data.realclear_user,365);
createCookie('realclear_key',data.realclear_key,365);
$.getJSON("http://www1.realclearpolitics.com/dev/user_sign_in.php?jsoncallback=?", { realclear_vars: data.realclear_vars, realclear_user: data.realclear_user, realclear_key: data.realclear_key }, function(data) {
//alert(data.realclear_vars+' '+data.realclear_user+' '+data.realclear_key);
countCookieSets();
});
$.getJSON("http://dyn.realclearpolitics.com/dev/user_sign_in.php?jsoncallback=?", { realclear_vars: data.realclear_vars, realclear_user: data.realclear_user, realclear_key: data.realclear_key }, function(data) {
countCookieSets();
});
$.getJSON("http://www.realclearworld.com/dev/user_sign_in.php?jsoncallback=?", { realclear_vars: data.realclear_vars, realclear_user: data.realclear_user, realclear_key: data.realclear_key }, function(data) {
//alert(data.status+' '+data.site+' '+data.level);
countCookieSets();
});
$.getJSON("http://www1.realclearmarkets.com/dev/user_sign_in.php?jsoncallback=?", { realclear_vars: data.realclear_vars, realclear_user: data.realclear_user, realclear_key: data.realclear_key }, function(data) {
//alert(data.status+' '+data.site+' '+data.level);
countCookieSets();
});
$.getJSON("http://www1.realclearsports.com/dev/user_sign_in.php?jsoncallback=?", { realclear_vars: data.realclear_vars, realclear_user: data.realclear_user, realclear_key: data.realclear_key }, function(data) {
//alert(data.status+' '+data.site+' '+data.level);
countCookieSets();
});
$.getJSON("http://www.realclearscience.com/dev/user_sign_in.php?jsoncallback=?", { realclear_vars: data.realclear_vars, realclear_user: data.realclear_user, realclear_key: data.realclear_key }, function(data) {
//alert(data.status+' '+data.site+' '+data.level);
countCookieSets();
});
$.getJSON("http://www.realcleardefense.com/dev/user_sign_in.php?jsoncallback=?", { realclear_vars: data.realclear_vars, realclear_user: data.realclear_user, realclear_key: data.realclear_key }, function(data) {
//alert(data.status+' '+data.site+' '+data.level);
countCookieSets();
});
auth();
}
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=."+getDomain()+"";
}
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);
}
function getDomain(){
var path = window.location.host;
if(path.substr(0,3)=='www'){
var per=path.indexOf('.')
per=per+1;
path=path.substr(per);
}
return path;
}
//function to parse realclear_vars cookie
function realclearVars(rcVar){
var rcVars = new Array("status", "user_id", "user_name", "user_email", "user_title", "user_gender", "user_age");
var realclear_vars = readCookie('realclear_vars');
var rcv = '';
if(realclear_vars){
rcv=realclear_vars.replace(/%7C/g, '|');
rcv=rcv.split('|');
rcv=rcv[parseInt(rcVar)];
}else{
return -1;
}
return rcv;
}
function verifyData(toVerify){
var verified = true;
var errorString = '';
var c = $('.sign_in_form');
var email = c.find('input[name=sign_in_user_email]').val();
var password = c.find('input[name=sign_in_password]').val();
var password_again = c.find('input[name=sign_in_repassword]').val();
$(''+toVerify+'').each(function(){
if(this.value==""){
$(this).addClass("fill");
verified=false;
}else{
$(this).removeClass("fill");
}
});
//alert(verified+'none empty');
if(!validEmail(email) && c.find('input[name=sign_in_user_email]').val()){
verified=false;
c.find('input[name=sign_in_user_email]').addClass("fill");
errorString += '*Email address not valid.
';
}
if(password!=c.find('input[name=sign_in_repassword]').val()){
verified=false;
c.find('input[name=sign_in_repassword]').addClass("fill");
errorString += '*Passwords do not match.
';
}
if(verified==false){
errorString += '*Highlighted fields are invalid or missing.
';
$('#errorDisplay').html(errorString);
}else{
$('#errorDisplay').html('');
}
return verified;
}
function validEmail(email){
var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailReg.test(email);
}