// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.


////////////////////////////////////////////////
// Ajax activity indicator
////////////////////////////////////////////////

Ajax.Responders.register({
        onCreate: function() {
                if($('notification') && Ajax.activeRequestCount> 0)
                        Effect.Appear('notification',{duration: 0.25, queue: 'front'});
        },
        onComplete: function() {
                if($('notification') && Ajax.activeRequestCount == 0)
                        Effect.Fade('notification',{duration: 0.25, queue: 'end'});
        }
});



////////////////////////////////////////////////
// Journal Subscription functions
////////////////////////////////////////////////


function activateJournal() {
    if ($('Journalform')) {
		$('JournalEmail').style.color = '#CC3300';
		$('JournalEmail').value = 'Enter your email address...';	
		$('JournalEmail').onfocus = focusEmail;

		$('JournalEmail').blur();
		$('JournalSubmit').value = 'Notify me!';
		$('Journalform').onsubmit = function() { 
        	doJournal();return false; 
        	};

        is_sending = false;

    	}
	}


function doJournal() {
    if (is_sending) return false; 
    JournalEmail = $F('JournalEmail');
    mode = $F('__mode');
    blog_id = $F('blog_id');
    redirect = $F('_redirect');
	
    if (JournalEmail == '' || JournalEmail == 'Enter your email address...') { $('Journalresults').innerHTML = "Please enter your email address"; return false; }
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(JournalEmail)) {

	    is_searching = true;
    	JournalSubmit.value = 'Sending...';
    	JournalSubmit.disabled = true;
    
	    pars = 'email=' + escape(JournalEmail) + '&__mode=' + escape(mode) + '&_redirect=' + escape(redirect) + '&blog_id=' + escape(blog_id);
    	var myAjax = new Ajax.Request('/mt/mt-add-notify.cgi', 
          {method: 'post', parameters: pars, onComplete:doJournalResponse});

	} else { 
		$('Journalresults').innerHTML = "<p>Please enter a valid email address"; return false; 
		}

}


function doJournalResponse(response) {
	$('Journalresults').innerHTML = response.responseText;
	resetJournalForm();
	}

function thankyou() {
	$('JournalformDiv').innerHTML = "Thank you. A confirmation email has been sent! Want to <a href='#' onClick='activateJournal();'>enter another email address?</a>";
	}

function resetJournalForm() {
    JournalSubmit.value = 'Notify me!';
    JournalSubmit.disabled = false;
    is_sending = false;
    activateJournal();
	}



function focusEmail() {
    if ($F('JournalEmail') == 'Enter your email address...') { $('JournalEmail').style.color = '#333'; $('JournalEmail').value = ''; }
	}



////////////////////////////////////////////////
// Journal Subscription functions II
////////////////////////////////////////////////


function activateJournal2() {
    if ($('Journalform2')) {
	$('JournalEmail2').style.color = '#CC3300';
	$('JournalEmail2').value = 'Enter your email address...';	
	$('JournalSubmit2').value = 'Remind me!';

	$('JournalEmail2').onfocus = focusEmail2;
	$('Journalform2').onsubmit = function() { doJournal2(); return false; };
        is_sending2 = false;

    	}
	}


function doJournal2() {
    if (is_sending2) return false; 
    JournalEmail2 = $F('JournalEmail2');
    mode2 = $F('__mode2');
    blog_id2 = $F('blog_id2');
    redirect2 = $F('_redirect2');
	
    if (JournalEmail2 == '' || JournalEmail2 == 'Enter your email address...') { $('Journalresults2').innerHTML = "<p>Please enter your email address"; return false; }
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(JournalEmail2)) {
	
    	is_searching2 = true;
    	JournalSubmit2.value = 'Sending...';
    	JournalSubmit2.disabled = true;
    
	    pars = 'email=' + escape(JournalEmail2) + '&__mode=' + escape(mode2) + '&_redirect=' + escape(redirect2) + '&blog_id=' + escape(blog_id2);
   		var myAjax = new Ajax.Request('/mt/mt-add-notify.cgi', 
          {method: 'post', parameters: pars, onComplete:doJournalResponse2});
	
	} else { 
		$('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false; 
		}

	}


function doJournalResponse2(response) {
	$('Journalresults2').innerHTML = '<p>' + response.responseText;
	resetJournalForm2();
	}

function thankyou2() {
	$('JournalformDiv2').innerHTML = "Thank you. A confirmation email has been sent! Want to <a href='#' onClick='activateJournal();'>enter another email address?</a>";
	}

function resetJournalForm2() {
    JournalSubmit2.value = 'Notify me!';
    JournalSubmit2.disabled = false;
    is_sending2 = false;
    activateJournal2();
	}



function focusEmail2() {
    if ($F('JournalEmail2') == 'Enter your email address...') { $('JournalEmail2').style.color = '#333'; $('JournalEmail2').value = ''; }
	}



////////////////////////////////////////////////
// Email Validation
////////////////////////////////////////////////

function echeck2(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   $('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   $('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   $('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   $('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   $('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   $('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   $('Journalresults2').innerHTML = "<p>Please enter a valid email address"; return false;
		 }

 		 return true;					
	}
	
	
	

////////////////////////////////////////////////
// MT Basic JavaScript
////////////////////////////////////////////////



function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (f.author != undefined)
       setCookie('mtcmtauth', f.author.value, now, '/', '', '');
    if (f.email != undefined)
       setCookie('mtcmtmail', f.email.value, now, '/', '', '');
    if (f.url != undefined)
       setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', '');
    deleteCookie('mtcmthome', '/', '');
    deleteCookie('mtcmtauth', '/', '');
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function hideDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'block';
}

var commenter_name;

function individualArchivesOnLoad(commenter_name) {


 // comments are allowed but not required
    if (commenter_name) {
        hideDocumentElement('name-email');
    } else {
        showDocumentElement('name-email');
    }



    if (document.comments_form) {
        if (!commenter_name && (document.comments_form.email != undefined) &&
            (mtcmtmail = getCookie("mtcmtmail")))
            document.comments_form.email.value = mtcmtmail;
        if (!commenter_name && (document.comments_form.author != undefined) &&
            (mtcmtauth = getCookie("mtcmtauth")))
            document.comments_form.author.value = mtcmtauth;
        if (document.comments_form.url != undefined && 
            (mtcmthome = getCookie("mtcmthome")))
            document.comments_form.url.value = mtcmthome;
        if (document.comments_form["bakecookie"]) {
            if (mtcmtauth || mtcmthome) {
                document.comments_form.bakecookie.checked = true;
            } else {
                document.comments_form.bakecookie.checked = false;
            }
        }
    }
}

function writeTypeKeyGreeting(commenter_name, entry_id) {
    if (commenter_name) {
        document.write('<p>Thanks for signing in, ' + commenter_name +
          '. Now you can comment. '+
          '(<a href="http://www.drudgeradioarchives.com/mt/mt-comments.cgi?__mode=handle_sign_in&amp;static=1&amp;logout=1&entry_id=' + entry_id + '">sign out</a>)</p>');
    } else {
        document.write('<p>If you have a TypeKey identity, you can '+
          '<a href="https://www.typekey.com/t/typekey/login?&amp;need_email=1&amp;lang=en_US&amp;t=q22BjbnDhohfDbv9cM8p&amp;v=1.1&amp;_return=http://www.drudgeradioarchives.com/mt/mt-comments.cgi%3f__mode=handle_sign_in%26static=1%26entry_id=' + entry_id + '">sign in</a> '+
          'to use it here.</p>');

    }

}

if ('www.drudgeradioarchives.com' != 'www.drudgeradioarchives.com') {
    document.write('<script src="http://www.drudgeradioarchives.com/mt/mt-comments.cgi?__mode=cmtr_name_js"></script>');
} else {
    commenter_name = getCookie('commenter_name');
}




Event.observe(window, 'load', activateJournal, false);
Event.observe(window, 'load', activateJournal2, false);
