
// JavaScript includes for YNP
// ------------ Some configuration variables ------------ //
var ajax_path = 'php/ajax/';
var templates_path = 'templates/';

// TinyMCE vars
var waMode = false;
var wpMode = false;
var eaMode = false;
var epMode = false;
var caMode1 = false;
var cpMode1 = false;
var caMode2 = false;
var cpMode2 = false;
var caMode3 = false;
var cpMode3 = false;

Ajax.currentRequests = {};

Ajax.Responders.register({
	onCreate: function(request) {
		if (request.options.onlyLatestOfClass && Ajax.currentRequests[request.options.onlyLatestOfClass]) {
			// if a request of this class is already in progress, attempt to abort it before launching this new request
			try { Ajax.currentRequests[request.options.onlyLatestOfClass].transport.abort(); } catch(e) {}
		}
		// keep note of this request object so we can cancel it if superceded
		Ajax.currentRequests[request.options.onlyLatestOfClass] = request;
	},
	onComplete: function(request) {
		if (request.options.onlyLatestOfClass) {
			// remove the request from our cache once completed so it can be garbage collected
			Ajax.currentRequests[request.options.onlyLatestOfClass] = null;
		}
	}
});

// This function can find the position of a DOM element
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// This function returns the width en height of the browser
function getWinDimensions() {
	var winHeight, winWidth, dimensions, browserHeight;
	dimensions = getScrollDimensions();
	winHeight = dimensions[0];
	winWidth = dimensions[1];
	dimensions = getBrowserDimensions();
	browserHeight = dimensions[0];
	if(winHeight < browserHeight)
		winHeight = browserHeight;
	return [winHeight,winWidth];
}

// The following two 'tiny' functions create a tinyMCE for comments and for write stuff
function initTinyComments() {
	tinyMCE.init({
		mode : "none",
		height: '100px',
		width: '370px',
		plugins : "emotions",
		language: 'en',
		theme_advanced_buttons1: "emoticons",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: "",
		theme_advanced_buttons1_add : "emotions"
	});
}

function initTinyNormal() {
	tinyMCE.init({
		mode : "none",
		language: "en",
		height: '225px',
		width: '400px',
		plugins: "emotions, media, youtube",
		theme_advanced_buttons1: "bold, italic, underline, strikethrough, separator, justifyleft, justifycenter, justifyright, separator, fontsizeselect, separator",
		theme_advanced_buttons1_add : "emotions, media, youtube",
		theme_advanced_buttons2: "bullist, numlist, separator, undo, redo, separator, link, unlink, image, separator, hr, charmap, separator, help",
		theme_advanced_buttons3: ""
	});
}

function ChangeVisibilityForFlash(Visibility) {
	// Object tag
	var ToHide = document.getElementsByTagName('OBJECT');
	for(var i=0; i < ToHide.length; i++) {
		ToHide[i].style.visibility = Visibility;
    }
	// Embed tag
	var ToHide = document.getElementsByTagName('EMBED');
	for(var i=0; i < ToHide.length; i++) {
		ToHide[i].style.visibility = Visibility;
    }
}

// This function fades the mask out en hides it
function hideMask() {
	var popup = $('popup');
	var popupcontent = $('popupcontent');
	var popuptitlebar = $('popuptitlebar');
	var mask = $('mask');
	
	tinyMCE.idCounter=0;
	// TinyMCE meuk
	if(waMode) {
		waMode = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'wa');
	} else if(wpMode) {
		wpMode = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'wp');
	} else if(eaMode) {
		eaMode = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'ea');
	} else if(epMode) {
		epMode = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'ep');
	} else if(caMode1) {
		caMode1 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeNew');
	} else if(cpMode1) {
		cpMode1 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeNew');
	} else if(caMode2) {
		caMode2 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeReply');
	} else if(cpMode2) {
		cpMode2 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeReply');
	} else if(caMode3) {
		caMode3 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'editComment'+previousEditId);
	} else if(cpMode3) {
		cpMode3 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'editComment'+previousEditId);
	}
	
	initTinyNormal();

	popup.style.display = 'none';
	new Effect.Parallel(
		[ new Effect.Opacity('mask', { from: 0.6, to: 0.0 }) ],
		{ duration: 0.5, 
		  afterFinish: function() {
			mask.style.display = 'none';
			popuptitlebar.innerHTML = '';
			popupcontent.innerHTML = '';
			ChangeVisibilityForFlash('visible');
		  }
		}
	);
}

// This function creates a mask and popup
function createMask(content, param1) {
	var popuptitlebar = $('popuptitlebar');
	var popupcontent = $('popupcontent');
	var popup = $('popup');
	var mask = $('mask');
	
	ChangeVisibilityForFlash('hidden');
	var dimensions = getBrowserDimensions();
	
	wait = false;
	
	switch(content) {
		case 'writeArticle':
			var titlebarTxt = 'Write an Article';
			var contentTxt = '<form id="articleForm" name="articleForm" method="post" action="?add/Article" enctype="multipart/form-data" onsubmit="return submitForm();">		<table class="popup">			<tr><td class="formtitle">				Title:			</td></tr>			<tr><td class="formcontent"><input type="text" class="text" maxlength="50" name="title" />			<tr><td class="formtitle">				Description:			</td></tr>			<tr><td class="formcontent">				<textarea name="description" rows="6" cols="50" id="wa"></textarea>			</td></tr>			<tr><td class="formtitle">				Add an image (2 MegaBytes max!):			</td></tr>			<tr><td>				<input type="file" name="image" />			</td></tr>			<tr><td>&nbsp;</td></tr>			<tr><td class="formcontent">				<button type="button" name="submitbutton" onclick="javascript:submitForm(document.forms[__FORMNAME__])">Add Article</button>				&nbsp;<input type="reset" name="resetbutton" value="Reset fields" />			</td></tr>		</table>	</form>';
			contentTxt = contentTxt.replace("__FORMNAME__", "\'articleForm\'");
			popuptitlebar.innerHTML = titlebarTxt;
			popupcontent.innerHTML = contentTxt;
			waMode = true;
			tinyMCE.execCommand("mceAddControl", false, 'wa');
			break;
		case 'writeParticle':
			var wPTemplate = new Template('<input type="hidden" name="click_id" value="#{click_id}" />');
			var cid = {click_id: param1};
			var titlebarTxt = 'Write a Comment';
			var contentTxt = '<form id="particleForm" method="post" action="?add/Particle" enctype="multipart/form-data">		<table class="popup">			<tr><td class="formtitle">				Title:			</td></tr>			<tr><td class="formcontent"><input type="text" class="text" maxlength="50" name="title" />			<tr><td class="formtitle">				Description:			</td></tr>			<tr><td class="formcontent">				<textarea rows="6" cols="35" id="wp"></textarea>			</td></tr>			<tr><td class="formtitle">				Add an image (2 MegaBytes max!):			</td></tr>			<tr><td>				<input type="file" name="image" />			</td></tr>			<tr><td>&nbsp;</td></tr>			<tr><td class="formcontent">				<button type="button" name="submitbutton" onclick="javascript:submitForm(document.forms[__FORMNAME__])">Add Comment</button>				&nbsp;<input type="reset" name="resetbutton" value="Reset fields" />			</td></tr>		</table>	</form>';
			contentTxt = contentTxt.replace("__FORMNAME__", "\'particleForm\'");
			popuptitlebar.innerHTML = titlebarTxt;
			popupcontent.innerHTML = contentTxt;
			$('particleForm').innerHTML += (wPTemplate.evaluate(cid));
			wpMode = true;
			tinyMCE.execCommand("mceAddControl", false, 'wp');
			break;
		case 'editParticle':
			wait = true;
			new Ajax.Request(templates_path+'popup/editParticle.php?id='+param1, {
				method: 'post',
				onComplete: function() {
					tinyMCE.execCommand("mceAddControl", false, 'ep');
					new Effect.Parallel(
			[ new Effect.Fade('mask', { from: 0, to: 0.6 }) ],
			{ duration: 0.7,
			  afterFinish: function() {
				popup.style.display = 'block';
				if(navigator.userAgent.indexOf("Mozilla") != -1 && navigator.userAgent.indexOf("MSIE") == -1) {
					var left = ((dimensions[1] / 2) - (popup.offsetWidth / 2));
					if(left < 0)
						left = 0;
					popup.style.left = left + 'px';
					
					var top = ((dimensions[0] / 2) - (popup.offsetHeight / 2));
					if(top < 0)
						top = 0;
					popup.style.top =  top + 'px';
				} else if(navigator.userAgent.indexOf("MSIE") && navigator.appVersion.indexOf('MSIE 7.0') != -1) {
					// IE7
					var left = ((dimensions[1] / 2) - (popup.offsetWidth / 2));
					if(left < 0)
						left = 0;
					popup.style.left = left + 'px';
					
					var top = ((windowHeight() / 2) - (popup.offsetHeight / 2));
					if(top < 0)
						top = 0;
					popup.style.top =  top + 'px';
				}
			  }
			}
		);
				}
			});
			epMode = true;
			break;
		case 'editArticle':
			wait = true;
			new Ajax.Request(templates_path+'popup/editArticle.php?id='+param1, {
				method: 'post',
				onComplete: function() {
					tinyMCE.execCommand("mceAddControl", false, 'ea');
					new Effect.Parallel(
			[ new Effect.Fade('mask', { from: 0, to: 0.6 }) ],
			{ duration: 0.7,
			  afterFinish: function() {
				popup.style.display = 'block';
				if(navigator.userAgent.indexOf("Mozilla") != -1 && navigator.userAgent.indexOf("MSIE") == -1) {
					var left = ((dimensions[1] / 2) - (popup.offsetWidth / 2));
					if(left < 0)
						left = 0;
					popup.style.left = left + 'px';
					
					var top = ((dimensions[0] / 2) - (popup.offsetHeight / 2));
					if(top < 0)
						top = 0;
					popup.style.top =  top + 'px';
				} else if(navigator.userAgent.indexOf("MSIE") && navigator.appVersion.indexOf('MSIE 7.0') != -1) {
					// IE7
					var left = ((dimensions[1] / 2) - (popup.offsetWidth / 2));
					if(left < 0)
						left = 0;
					popup.style.left = left + 'px';
					
					var top = ((windowHeight() / 2) - (popup.offsetHeight / 2));
					if(top < 0)
						top = 0;
					popup.style.top =  top + 'px';
				}
			  }
			}
		);
				}
			});
			eaMode = true;
			break;
		case 'rssPopup':
			var titlebarTxt = '<img style="margin-top: 2px;" src="images/rss.png" alt="RSS" /> Add RSS feed';
			var contentTxt = '<p><b>Add the feed of this blog to MY NEWS</b></p>	<p><b>Step 1:</b> Choose the feed you want to use:</p>	<p><select id="rss_type" onChange="javascript:changeOption();">		<optgroup>			<option value="1">Articles only</option>			<option value="2" selected="selected">Articles and Comments</option>			<option value="3">Comments only</option>		</optgroup>	</select></p>	<p><b>Step 2:</b> Make a choice what you would like to do:</p>	<p>- Add it to My News by pressing the "Add" button: <button type="button" onclick="popUp();">Add</button></p>	<p>- Copy the URL of this feed for usage in other sites:</p>	<input value="http://www.ccc.yournewspage.com/rss/.xml" style="width: 300px;" type="text" id="copy_txt" onclick="javascript:selectAll();" /> <button type="button" onclick="javascript:copy(document.getElementById(__FORMNAME__));">Copy to clipboard</button>';
			contentTxt = contentTxt.replace("__FORMNAME__", "\'copy_txt\'");
			popuptitlebar.innerHTML = titlebarTxt;
			popupcontent.innerHTML = contentTxt;
			break;
	}
	
	if(wait == false) {
		new Effect.Parallel(
			[ new Effect.Fade('mask', { from: 0, to: 0.6 }) ],
			{ duration: 0.7,
			  afterFinish: function() {
				popup.style.display = 'block';
				if(navigator.userAgent.indexOf("Mozilla") != -1 && navigator.userAgent.indexOf("MSIE") == -1) {
					var left = ((dimensions[1] / 2) - (popup.offsetWidth / 2));
					if(left < 0)
						left = 0;
					popup.style.left = left + 'px';
					
					var top = ((dimensions[0] / 2) - (popup.offsetHeight / 2));
					if(top < 0)
						top = 0;
					popup.style.top =  top + 'px';
				} else if(navigator.userAgent.indexOf("MSIE") && navigator.appVersion.indexOf('MSIE 7.0') != -1) {
					// IE7
					var left = ((dimensions[1] / 2) - (popup.offsetWidth / 2));
					if(left < 0)
						left = 0;
					popup.style.left = left + 'px';
					
					var top = ((windowHeight() / 2) - (popup.offsetHeight / 2));
					if(top < 0)
						top = 0;
					popup.style.top =  top + 'px';
				}
			  }
			}
		);
	}
	mask.style.display = 'block';
}

// ------------ Article and Particle actions ------------ //

// This function is a wrapper to create a popup for writing an Article
function writeArticle() {
	createMask('writeArticle');
}

// This function removes an Article and, if exists, a Particle
function deleteArticle(cid, id, g) {
	if($('empty'+id)) {
		// No Particle has been written
		var answer = confirm("Are you sure you want to delete\nthis Article with all it's replies?");
		if (answer) {
			// Delete the Article
			new Ajax.Request(ajax_path+'deleteArticle.php?cid='+cid+'&id='+id+'&p=n&g='+g, {
				method: 'get'
			});
		}
	} else {
		// A Particle exists
		var answer = confirm("A matching Particle exists, are you sure\nyou want to delete both the Article and Particle?");
		if (answer) {
			// Delete the Article and Particle
			new Ajax.Request(ajax_path+'deleteArticle.php?cid='+cid+'&id='+id+'&p=y&g='+g, {
				method: 'get'
			});
		}
	}
}

// This function shares an Article
function shareArticle(cid, id) {
	new Ajax.Request(ajax_path+'setPublicArticle.php?cid='+cid+'&id='+id+'&action=share', {
		method: 'get'
	});
}

// This function unshares an Article
function unshareArticle(cid, id) {
	if(!$('empty'+id)) {
		alert('You will have to delete the Particle before unsharing this Article');
	} else {
		new Ajax.Request(ajax_path+'setPublicArticle.php?cid='+cid+'&id='+id+'&action=unshare', {
			method: 'get'
		});
	}
}

function editArticle(id) {
	createMask('editArticle', id);
}

function copyArticle(cid, id) {
	var answer = confirm("Would you like to share this article immediately?");
	var share = 0;
	if (answer)
		share = 1;
	
	new Ajax.Request(ajax_path+'copyArticle.php?cid='+cid+'&id='+id+'&share='+share, {
			method: 'get'
		});
}

// This function is a wrapper to create a popup for writing a Particle
function writeParticle(cid) {
	createMask('writeParticle', cid);
}

// This function is a wrapper to create a popup for editing a Particle
function editParticle(id) {
	createMask('editParticle', id);
}

// This function removes an Article and, if exists, a Particle
function deleteParticle(pid, id) {
	var answer = confirm("Are you sure you want to delete\nthis Particle with all it's replies?");
	if (answer) {
		// Delete the Article and Particle
		new Ajax.Request(ajax_path+'deleteParticle.php?pid='+pid+'&id='+id, {
			method: 'get'
		});
	}
}

// -------------------------

// ------------ Comments ------------ //

function comments_a(id) {
	new Ajax.Request(templates_path+'popup/comments.php?id='+id+'&type=articles', {
		method: 'get'
	});
	createMask('comments_a', id);
}

function comments_p(id) {
	new Ajax.Request(templates_path+'popup/comments.php?id='+id+'&type=particles', {
		method: 'get'
	});
	createMask('comments_p', id);
}

// This is one big function for:
//	1. Writing new comments
//	2. Writing replies on other comments
//	3. Editing a comment
//	All defined in the 'action' var
previousEditId = previousReplyId = -1;
function expandComment(action, topic_id, type, comment_id) {
	if(caMode1) {
		caMode1 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeNew');
	} else if(cpMode1) {
		cpMode1 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeNew');
	} else if(caMode2) {
		caMode2 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeReply');
	} else if(cpMode2) {
		cpMode2 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'writeReply');
	} else if(caMode3) {
		caMode3 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'editcomment'+previousEditId);
	} else if(cpMode3) {
		cpMode3 = false;
		tinyMCE.execCommand("mceRemoveControl", false, 'editcomment'+previousEditId);
	}
	
	if(type == 'comments_a') {
		type = 'Comment_a';
		var tmce = 'ca';
	} else {
		type = 'Comment_p';
		var tmce = 'cp';
	}
	
	if(previousReplyId != -1) {
		if($('newreply'+previousReplyId))
			$('newreply'+previousReplyId).innerHTML = '';
	}
	previousReplyId = comment_id;
	
	if(previousEditId != -1) {
		if($('comment'+previousEditId) && previousBody)
			$('comment'+previousEditId).innerHTML = previousBody;
	}
	previousEditId = comment_id;
	
	$('newcomment').innerHTML = '';
	$('newcomment').style.height = 0 + 'px';
	
	if(!topic_id) {
		alert('The topic_id is missing');
	} else {
		switch(action) {
			case 'writeNew':
				$('newcomment').innerHTML = '<form id="writeNewForm" method="post" action="?add/'+type+'"><input type="hidden" name="topic_id" value="'+topic_id+'" /><table><tr><td class="commentbody">Your comment:</td><td><textarea id="writeNew" name="body" rows="3" cols="30"></textarea></td></tr><tr><td colspan="2"><button type="button" onclick="javascript:submitComment(document.forms[\'writeNewForm\']);">Add</button>&nbsp;<input type="reset" value="Clear" /></td></tr></form>';
				$('newcomment').style.height = 130 + 'px';
				if(tmce == 'ca')
					caMode1 = true;
				else
					cpMode1 = true;
				initTinyComments();
				tinyMCE.execCommand("mceAddControl", false, 'writeNew');
				break;
			case 'writeReply':
				$('newreply'+comment_id).innerHTML = '<form id="writeReplyForm" method="post" action="?add/'+type+'"><input type="hidden" name="topic_id" value="'+topic_id+'" /><input type="hidden" name="parent_id" value="'+comment_id+'" /><table><tr><td class="commentbody">Your comment:</td><td><textarea id="writeReply" name="body" rows="3" cols="30"></textarea></td></tr><tr><td colspan="2"><button type="button" onclick="javascript:submitComment(document.forms[\'writeReplyForm\']);">Add</button>&nbsp;<input type="reset" value="Clear" /></td></tr></form>';
				if(tmce == 'ca')
					caMode2 = true;
				else
					cpMode2 = true;
				initTinyComments();
				tinyMCE.execCommand("mceAddControl", false, 'writeReply');
				break;
			case 'editComment':
				previousBody = $('commentBody'+comment_id).innerHTML;
				$('commentBody'+comment_id).innerHTML = '<form id="editCommentForm" method="post" action="?edit/'+type+'"><input type="hidden" name="topic_id" value="'+topic_id+'" /><input id="cid" type="hidden" name="cid" value="'+comment_id+'" /><textarea name="body" id="editcomment'+comment_id+'" rows="3" cols="30"></textarea><br /><button type="button" onclick="javascript:submitComment(document.forms[\'editCommentForm\']);">Edit</button>&nbsp;<input type="reset" value="Reset" /></td></tr></form>';
				if(tmce == 'ca')
					caMode3 = true;
				else
					cpMode3 = true;
				initTinyComments();
				new Ajax.Request(ajax_path+'fillCommentBody.php?id='+comment_id+'&topic_id='+topic_id+'&type='+type, {
					method: 'get',
					onComplete: function() {
						tinyMCE.execCommand("mceAddControl", false, 'editcomment'+comment_id);
					}
				});
				break;
		}
	}
}

// This function removes a comment
function deleteComment(cid, ctype) {
	var answer = confirm("Are you sure you want to delete this comment?");
	if (answer) {
		new Ajax.Request(ajax_path+'deleteComment.php', {
			method: 'post',
			parameters: { id: cid, type: ctype }
		});
	}
}

// This function posts a comment
function submitComment(form) {
	if(form.id == 'writeNewForm') {
		var inst = tinyMCE.getInstanceById('writeNew');
	} else if(form.id == 'writeReplyForm') {
		var inst = tinyMCE.getInstanceById('writeReply');
	} else if(form.id == 'editCommentForm') {
		var cid = $('cid').value;
		var inst = tinyMCE.getInstanceById('editcomment'+cid);
	}
	var body = inst.getHTML();

	var bodyInput = document.createElement('input');
	bodyInput.value = body;
	bodyInput.name = 'body';
	bodyInput.type = 'hidden';
	form.appendChild(bodyInput);
	
	form.submit();
}

// This function disables the submitbutton when submitting a form
function submitForm(form) {
	if(form.id == 'particleForm') {
		var title = form.title;
		if(title.value == "") {
			alert('Please enter a title');
			return;
		}
		var inst = tinyMCE.getInstanceById('wp');
		var body = inst.getHTML();
		var bodyInput = document.createElement('input');
		bodyInput.value = body;
		bodyInput.name = 'body';
		bodyInput.type = 'hidden';
		form.appendChild(bodyInput);
	} else if(form.id == 'editParticleForm') {
		var title = form.title;
		if(title.value == "") {
			alert('Please enter a title');
			return;
		}
		var inst = tinyMCE.getInstanceById('ep');
		var body = inst.getHTML();
		var bodyInput = document.createElement('input');
		bodyInput.value = body;
		bodyInput.name = 'body';
		bodyInput.type = 'hidden';
		form.appendChild(bodyInput);
	} else if(form.id == 'articleForm') {
		var title = form.title;
		if(title.value == "") {
			alert('Please enter a title');
			return;
		}
		var inst = tinyMCE.getInstanceById('wa');
		var body = inst.getHTML();
		if(body == "") {
			alert('Please enter a description');
			return;
		}
		//$('wa').innerHTML = body;
		
		
		var bodyInput = document.createElement('input');
		bodyInput.value = body;
		bodyInput.name = 'body';
		bodyInput.type = 'hidden';
		form.appendChild(bodyInput);
		
	} else if(form.id == 'editArticleForm') {
		var title = form.title;
		if(title.value == "") {
			alert('Please enter a title');
			return;
		}
		var inst = tinyMCE.getInstanceById('ea');
		var body = inst.getHTML();
		var bodyInput = document.createElement('input');
		bodyInput.value = body;
		bodyInput.name = 'body';
		bodyInput.type = 'hidden';
		form.appendChild(bodyInput);
	}
	form.submitbutton.disabled = true;
	form.resetbutton.disabled = true;
	form.submit();
	return true; 
}

function escapeStr(str) {
	str = str.replace(/"/g,"\\\"");
	str = str.replace(/'/g,"\\'");
	return str;
}

// This function checkes if the enter button is pressed in a form
function checkEnter(event) {
	var key;
	if(window.event) // IE
		key = window.event.keyCode;
	else // FF
		key = event.which;
	
	if(key == 13)
		login();
	else
		return;
}

// This function tries to login a user via AJaX
function login() {
	new Ajax.Request(ajax_path+'login.php', {
		method: 'post',
		parameters: { username: $('username').value, password: $('password').value, remember: $('remember').value }
	});
}

// This function is used to submit the form for editProfile
function editProfile() {
	new Ajax.Request(ajax_path+'editProfile.php', {
		method: 'post',
		parameters: { password: $('password').value, firstname: $('firstname').value, lastname: $('lastname').value, email: $('email').value, birthdate: $('birthdate').value, city: $('city').value, country: $('country').value, likes: $('likes').value, music: $('music').value, website: $('website').value }
	});
}

// This function loads the form to register
function pushyOption(option) {
	if(option == 1) {
		$('loginerror').innerHTML = '';
		$('registerform').innerHTML = '<table class="pushylogin"><tr><td colspan="2">Please enter your Pushy username and password:</td></tr><tr><td>Username:</td><td><input id="username" type="text" name="username" /></td></tr><tr><td>Password:</td><td><input id="password" type="password" name="password" /></td></tr><tr><td>&nbsp;</td><td><button type="button" onclick="javascript:pushyLogin();">Login</button></td></tr></table>';
	} else {
		$('loginerror').innerHTML = '';
		$('registerform').innerHTML = '<table class="pushylogin"><tr><td colspan="2">Please enter your new Pushy/YourNewsPage username and password and your new YourNewsPage blog name:</td></tr><tr><td class="error" colspan="2" id="error"></td></tr><tr><td>Email:</td><td><input id="username" type="text" /></td></tr><tr><td>Password:</td><td><input id="password" type="password" /></td></tr><tr><td>Verify password:</td><td><input id="password2" type="password" /></td></tr><tr><td>Blogname:</td><td><input id="blogname" type="text" name="blogname" /></td></tr><tr><td>&nbsp;</td><td><button type="button" onclick="javascript:pushyRegister();">Register</button></td></tr></table>';
	}
}

function pushyRegister() {
	new Ajax.Request(ajax_path+'pushyRegister.php', {
		method: 'post',
		parameters: { email: $('username').value, password: $('password').value, password2: $('password2').value, blogname: $('blogname').value }
	});
}

function pushyLogin() {
	new Ajax.Request(ajax_path+'pushyLogin.php', {
		method: 'post',
		parameters: { username: $('username').value, password: $('password').value }
	});
}

function checkBlogname() {
	new Ajax.Request(ajax_path+'checkBlogname.php', {
		method: 'post',
		parameters: { blogname: $('blogname').value }
	});
}

var myTimeout;

function filterBlogs(type) {
	if(myTimeout)
		clearTimeout(myTimeout);
	if(type == 'name') {
		if($('searchName').value != '' && $('searchName').value.length > 1) {
			myTimeout = setTimeout("search('name', $('searchName').value);", 500);
		}
	} else {
		if($('searchBlog').value != '' && $('searchBlog').value.length > 1) {
			myTimeout = setTimeout("search('blog', $('searchBlog').value);", 500);
		}
	}
}

function search(type, value) {
	if(type == 'name') {
		$('overviewPage').innerHTML = '<br /><br /><br /><center><img src="../../images/loading.gif" alt="Loading..." /></center><br /><br /><br />';
		new Ajax.Request(ajax_path+'filterBlogs.php', {
			method: 'post',
			parameters: { q: value, type: 'name' },
			onlyLatestOfClass: 'filter'
		});
	} else {
		$('overviewPage').innerHTML = '<br /><br /><br /><center><img src="../../images/loading.gif" alt="Loading..." /></center><br /><br /><br />';
		new Ajax.Request(ajax_path+'filterBlogs.php', {
			method: 'post',
			parameters: { q: value, type: 'blog' },
			onlyLatestOfClass: 'filter'
		});
	}
}

function rssPopup() {
	createMask('rssPopup');
}

function copy(inElement) {
  if (inElement.createTextRange) {
    var range = inElement.createTextRange();
    if (range)
      range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

function selectAll(id) {
	if(!id)
		id = 'copy_txt';
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function changeOption() {
	var value = document.getElementById('rss_type').value;
	if(value == 1)
		var rss = 'http://www.ccc.yournewspage.com/rss/_articles.xml';
	else if(value == 2)
		var rss = 'http://www.ccc.yournewspage.com/rss/.xml';
	else if(value == 3)
		var rss = 'http://www.ccc.yournewspage.com/rss/_comments.xml';
	var copy_txt = document.getElementById('copy_txt').value = rss;
}

function popUp() {
	var value = document.getElementById('rss_type').value;
	
	if(value == 1) {
		var rss = 'http://www.ccc.yournewspage.com/rss/_articles.xml';
		var name = 'Articles';
	} else if(value == 2) {
		var rss = 'http://www.ccc.yournewspage.com/rss/.xml';
		var name = 'Articles and Comments';
	} else if(value == 3) {
		var rss = 'http://www.ccc.yournewspage.com/rss/_comments.xml';
		var name = 'Comments';
	}
	
	URL = 'http://infocaster.net/projects/configtool/add.php?sid=&sName= - '+name+' - ClimateChangeCollege.org&sKeyword='+rss+'&sDomain=ccc';
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=320,height=150,left = 430,top = 295')");
}

function positionPopup() {
  var myWidth = 0, myHeight = 0, myleft = 0, mytop = 0, popup;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if(jQuery("#newPopup2")) {
	popup = jQuery("#newPopup2");
	myleft = ((myWidth / 2) - (popup.width() / 2)) + 'px';
	mytop = ((myHeight / 2) - (popup.height() / 2)) + 'px';
	popup.css("left",myleft);
	popup.css("top",mytop);
	popup.css("display",'block');
  }
  if(jQuery("#newPopup4")) {
	popup = jQuery("#newPopup4");
	myleft = ((myWidth / 2) - (popup.width() / 2)) + 'px';
	mytop = ((myHeight / 2) - (popup.height() / 2)) + 'px';
	popup.css("left",myleft);
	popup.css("top",mytop);
	popup.css("display",'block');
  }
}

function showWriteHelp() {
	var myWidth = 0, myHeight = 0, myleft = 0, mytop = 0, popup;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	if(jQuery("#newPopup3")) {
		popup = jQuery("#newPopup3");
		myleft = ((myWidth / 2) - (popup.width() / 2)) + 'px';
		mytop = ((myHeight / 2) - (popup.height() / 2)) + 'px';
		popup.css("left",myleft);
		popup.css("top",mytop);
		popup.css("display",'block');
	}
}

function showGoogleHelp() {
	var myWidth = 0, myHeight = 0, myleft = 0, mytop = 0, popup;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	if(jQuery("#newPopup")) {
		popup = jQuery("#newPopup");
		myleft = ((myWidth / 2) - (popup.width() / 2)) + 'px';
		mytop = ((myHeight / 2) - (popup.height() / 2)) + 'px';
		popup.css("left",myleft);
		popup.css("top",mytop);
		popup.css("display",'block');
	}
}

function hidePop() {
	jQuery('#newPopup').css('display','none');
}

function hidePop2(topic) {
	if(topic == 'blog') { number = 2; dontshow = document.popupform2.dontshow; }
	else if(topic == 'write') { number = 3; dontshow = document.popupform3.dontshow; }
	else if(topic == 'blogs') { number = 4; dontshow = document.popupform4.dontshow; }
	
	if(dontshow.checked) {
		jQuery.ajax({
			url: '/php/dontshow.php?topic='+topic
		});
	}
	
	jQuery('#newPopup'+number).css('display','none');
}

