loadgraphic = '<img src="http://comments.sltdb.com/images/popindicator.gif" alt="load indicator">';

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);
}

function $w(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 resetnav() {
	var items = document.getElementById('com_options').getElementsByTagName('a');
	for(i=0;i<items.length;i++) {
		items[i].className = 'com_item';
	}
}
function sltload(action, item, uid) {
	
	resetnav();

	$w('com_nav_'+item).className = 'com_itemactive';

	$w('com_content_area').innerHTML = '<img id="com_loading" src="http://comments.sltdb.com/images/load.gif" alt="loading" />';

	var ajx = new ajax();
	ajx.open('get','includes/ajax.php?action=load&item=' + action + '&uid=' + uid + '&random=' + Math.random(99999999), true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			$w('com_content_area').innerHTML = ajx.responseText;
		}
	}
	ajx.send(null);
	
}
function update(action, uid) {
	
	switch(action) {
	
		case "profile":
		
			var opts = Array();
			opts[0] = $w('gender').value;
			opts[1] = $w('location').value;
			opts[2] = $w('occupation').value;
			opts[3] = $w('password').value;
			opts[4] = $w('verify').value;
			opts[5] = $w('interests').value;
			opts[6] = $w('email').value;
			
			var ajx = new ajax();
			
			if(opts[3]!=""&&opts[3]==opts[4]) {
				ajx.open('get','includes/ajax.php?action=update&item=profile&gender=' + opts[0] + '&location=' + opts[1] + '&occupation=' + opts[2] + '&userid=' + uid + '&interests=' + opts[5] + '&email=' + opts[6] + '&password=' + opts[3] + '&random=' + Math.random(9999999999), true);
			} else if(opts[3]=="") {
				ajx.open('get','includes/ajax.php?action=update&item=profile&gender=' + opts[0] + '&location=' + opts[1] + '&occupation=' + opts[2] + '&userid=' + uid + '&interests=' + opts[5] + '&email=' + opts[6] + '&random=' + Math.random(9999999999), true);
			} else {
				alert("Passwords do not match.");	
			}
			
			popi('Saving Settings...',loadgraphic);
			
			ajx.onreadystatechange = function() {
				if(ajx.readyState == 4) {
					
					popi('Settings Saved','The changes made to your profile have been saved.', 1);
					
				}
			
			}
			ajx.send(null);
		
		break;
	
	}
	
}

function pop(head, content, noexpand) {
	$w('com_pop_header').innerHTML = head;
	$w('com_pop_content').innerHTML = content;
	if(!noexpand)
		overlay();	
}

function overlay() {
	$w('com_box').style.top = '150px';
	$('#com_box').fadeIn('slow');
	$('#com_overlay').fadeIn('fast');
}

function closeoverlay() {
	$.fn.colorbox.close();
}

function checkLogin(which,data1,data2,iframe) {
	var ajx = new ajax();
	ajx.open('get','includes/ajax.php?action=checklogin&random=' + Math.random(99999999), true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			response = ajx.responseText;
			switch(which) {
				case "report":
					report(response,data1);
					break;
				case "postcomment":
					postComment(response,data1,data2,iframe);
					break;
				case "reply":
					reply(response,data1,data2);
					break;
			}
		}
	}
	ajx.send(null);	
}

function promote(id,articleid) {
	popi('Promote?','Are you sure you want to promote this comment?<br><br><a href="javascript:submitPromote(' + id + ')" class="com_button com_nofloat autosize">Yes. Promote it!</a>&nbsp;<a href="javascript:closeoverlay()"class="com_button com_nofloat autosize">No wait!</a>');
}
function submitPromote(id) {
	$w('post_iframe').src = 'http://comments.sltdb.com/includes/ajax.php?action=promote&id=' + id + '&random=' + Math.random(99999999);
	popi('Comment Promoted','The comment has been promoted.<br><br><a class="com_button com_nofloat com_autosize" href="javascript:closeoverlay()">Close</a>');
}


function report(commentid,articleid) {
		popi('Report Abuse','Why are you reporting this comment?<div style="color:red; font-size:11px; height:12px;" id="msg"></div><p class="com_content" style="padding-top:15px;"><form name="abuse" method="post" target="post_iframe" action="http://comments.sltdb.com/includes/ajax.php?action=reportpost"><input type="hidden" name="articleid" value="' + articleid + '"><input type="hidden" name="commentid" value="' + commentid + '"><textarea style="width:350px; height:50px;" id="report_reason" name="report_reason"></textarea></p><p class="content" style="padding-top:15px;"><input type="button" class="com_button com_nofloat" value="Report this comment" onclick="submitReport(' + commentid + ')"></form></p>');
}

function submitReport(id) {
	reason = $w('report_reason').value;
	if(reason == '') {
		alert("You must enter a reason");
	} else {
		$w('post_iframe').src = 'http://comments.sltdb.com/includes/ajax.php?action=reportpost&id=' + id + '&reason=' + reason + '&random=' + Math.random(99999999);
		popi('Comment Reported','Your abuse report has been submitted.<br><br><a class="com_button com_nofloat com_autosize" href="javascript:closeoverlay()">Close</a>');
	}
}

function remove(id,aid) {
	popi('Are you sure?','Are you sure you want to permanantly delete this post from the website?<br><br><a class="com_button com_nofloat com_autosize" href="javascript:removePost(' + id + ');">Yes</a> <a class="com_button com_nofloat com_autosize" href="javascript:closeoverlay()">No</a>');
}
function removeignore(id) {
	
	var ajx = new ajax();
	
	ajx.open('get','/includes/ajax.php?action=removeignore&id=' + id + '&random=' + Math.random(9999999), true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			//alert(ajx.responseText);
		}
	}
	ajx.send(null);	
	
	$w('user_'+id).style.display = "none";
}
function removecomment() {
	$w('post_iframe').src = 'http://comments.sltdb.com/includes/ajax.php?action=removepost&aid=' + aid + '&id=' + id;
	popi('Thanks!','Comment has been removed.<br><br><a class="com_button com_nofloat com_autosize" href="javascript:closeoverlay()">Close</a>');
}
function admindelete(id) {
	popi('Are you sure?','Hey admin! Are you sure you want to permanantly delete this post from the website?<br><br><a class="com_button com_nofloat autosize" href="javascript:removePost(' + id + ',1)">Yes</a> <a class="com_button com_nofloat com_autosize" href="javascript:closeoverlay()">No</a>');
}
function removePost(id,admin) {

	var url = '';
	
	if(admin == 1) {
		if(prmp = prompt('Please provide a reason for this comment to be removed')) {
			url = 'http://comments.sltdb.com/includes/ajax.php?action=adminremovepost&id=' + id + '&reason=' + prmp + '&random=' + Math.random(99999999);
		}
	} else {
		url = 'http://comments.sltdb.com/includes/ajax.php?action=removepost&id=' + id + '&random=' + Math.random(99999999);
	}
	
	$w('post_iframe').src = url;

	popi('Thanks!','Comment has been removed.<br><br><a class="com_button com_nofloat com_autosize" href="javascript:document.location.reload();">Close</a>');
}

function reply(loggedin,id,replytoid,iframe) {
	
	var replytocontents = $w('com_' + replytoid).innerHTML;
	var replytouser = $w('usr_' + replytoid).innerHTML;
	
	if(loggedin == true) {
		popn('Reply to comment',"<div align='left' style='font-size:12px; color:#000;'><b>" + replytouser + " says:</b><br><br>" + replytocontents + "<br><br></div><div class='com_commentForm' id='com_commentForm'><form name='commentform' method='post' action='http://comments.sltdb.com/includes/ajax.php?action=submitcomment&refurl=" + encodeURIComponent(document.location.href) + "&iframe=" + iframe + "' target='post_iframe' id='post_reply_form'><textarea name='com_commentText' id='com_commentText'></textarea><br><br><input type='button' onclick='replycomment()' class='com_button com_nofloat com_autosize' value='post reply'><input type='hidden' id='com_storyid' name='com_storyid' value='" + id + "'><input type='hidden' id='com_replytoid' name='com_replytoid' value='" + replytoid + "'></form></div>");
	} else {
		login("You must be logged in to comment.");		 
	}
}

function login(msg) {
	loginmsg = '';
	if(msg != "") {
		loginmsg += "<h3>" + msg + "</h3><br><br>";
	}	
	loginmsg += "<table><tr><td align='right'>Username:</td><td><input type='text' name='l_user' id='l_user'></td></tr><tr><td align='right'>Password:</td><td><input type='password' id='l_password'></td></tr><tr><td></td><td><a href='javascript:submitlogin(\"overlay\")' class='com_button'>login</a><a class='com_button' href='register.php'>click here to register</a></td></tr></table>";
	
	popi('Login',loginmsg);	
}

function postComment(loggedin,id,iframe) {
	if(loggedin == "true") {
		popi('Post a Comment',"<div class='com_commentForm' id='com_commentForm'><form name='commentform' method='post' action='includes/ajax.php?action=submitcomment&refurl=" + encodeURIComponent(document.location.href) + "&iframe=" + iframe + "'><textarea name='com_commentText' id='com_commentText'></textarea><br><br><input type='submit' class='com_button com_nofloat com_autosize' value='post comment'><input type='hidden' id='com_storyid' name='com_storyid' value='" + id + "'><input type='hidden' id='com_replytoid' name='com_replytoid' value=''></form></div>");
	} else {
		login("You must be logged in to comment.");		
	}
}
function replycomment() {
	if(document.getElementById("com_commentText").value == '') {
		alert("Enter a comment.");	
	} else {
		document.getElementById('post_reply_form').submit();
		//closeoverlay();
		//document.location.reload();
	}
}
function submitComment(which) {	
	
	if(which == "overlay") {
		commenttext = $w("com_commentText").value;
		storyid = $w("com_storyid").value;
		replytoid = $w("com_replytoid").value;
		//storytitle = $w("storytitle").value;
		if(commenttext == '') {
			alert("Enter a comment");
			$w("com_commentText").focus();
		} else {
			
			document.getElementById("com_commentForm").innerHTML = "<img src='http://comments.sltdb.com/images/load.gif'>";
			
			var ajx = new ajax();
			
			var parameters = 'ct=' + commenttext + '&id=' + storyid + '&replytoid=' + replytoid;
			
			ajx.open('post','/includes/ajax.php?action=submitcomment&random=' + Math.random(9999999), true);
			ajx.onreadystatechange = function() {
				if(ajx.readyState == 4) {
					//alert(ajx.responseText);
					posted = "<br><br>Your comment was successfully posted and will appear momentarily.<br><br>";
					if(which == 'overlay') {
						posted += "<a class='com_button com_nofloat com_autosize' href='javascript:closeoverlay()'>close</a>&nbsp;<a class='com_button com_nofloat com_autosize' href=''>return to story</a><br><br>";
					} else {
						posted += "<a class='com_button' href='javascript:closeCommentBox();' style='margin-left:150px;'>Close</a><a class='com_button' href='http://comments.sltdb.com/comments.php?id=" + storyid + "'>View All Comments</a>";
					}
					$w("com_commentForm").innerHTML = posted;
				}
			}
			ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajx.send(parameters);		
		}
	} else {
		document.getElementById('comment_form').submit();
		
		//setTimeout(window.location.href = window.location.href,2000);
		
		posted = "<br><br>Your comment was successfully posted and will appear momentarily.<br><br>";
		posted += "<a class='com_button' href='javascript:closeCommentBox();' style='margin-left:150px;'>Close</a><a class='com_button' href=''>View All Comments</a>";
		$w("com_commentForm").innerHTML = posted;
	}
}

function updateAvatar(uid) {
	pop('Upload Avatar','<iframe src="/upload.php" width="300" height="100" frameborder="0"></iframe>');	
}

function commentstate() {
	if(readCookie('hidecomments') == '1') {
		hideComments();
	}
}

function hideComments() {
	if($w('com_display').style.display == "block") {
		createCookie('hidecomments','1','365');
		$w('com_display').style.display = 'none';
		$w('comment_hide').innerHTML = 'Show Comments';
	} else {
		eraseCookie('hidecomments');
		$w('com_display').style.display = 'block';
		$w('comment_hide').innerHTML = 'Hide Comments';
	}
}

function pc(action, item, container) {

	$w(container).innerHTML = loadgraphic;

	var ajx = new ajax();
	ajx.open('get','http://comments.sltdb.com/includes/ajax.php?action=' + action + '&item=' + item + '&random=' + Math.random(9999999), true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			$w(container).innerHTML = ajx.responseText;	
		}
	}
	ajx.send(null);

}


function addComment() {
	$('#com_commentForm').slideDown('slow');	
}

function login(onsite) {
	$.fn.colorbox({
		href:"http://comments.sltdb.com/login.php?onsite=" + onsite,
		width:500,
		height:350,
		iframe:true
	});	
}

function register(onsite) {
	$.fn.colorbox({
		href:"http://www.tribtalk.com/register.php",
		width:850,
		height:750,
		iframe:true
	});	
}

function edit(article, onsite) {
	$.fn.colorbox({
		href:"http://comments.sltdb.com/includes/ajax.php?action=edit&cid=" + article + "&onsite=" + onsite,
		width:850,
		height:375,
		iframe:true
	});
}

function clearCommentBox() {
	$w('com_commentForm').innerHTML = '<textarea id="com_commentText" name="com_commentText" style="width:428px; height:100px;"></textarea><a class="com_button com_buttonf" href="javascript:submitComment()">Submit Comment</a><a class="com_button com_buttonf" href="javascript:closeCommentBox();">Cancel</a>';
}

function closeCommentBox() {
	$('#com_commentForm').slideUp('slow');
	setTimeout(clearCommentBox,1000);
	//$w('commentForm').innerHTML = '<textarea id="commentText" name="commentText" style="width:428px; height:100px;"></textarea><a class="button right" href="javascript:submitComment()">Submit Comment</a><a class="button right" href="javascript:closeCommentBox();">Cancel</a>';
}

function popi(head, content) {
	$.fn.colorbox({
		html:"<div class='com_popibox'><div align='center'><div class='com_popihead'><h1>" + head + "</h1></div><div class='com_popicontent'>" + content + "</div></div>",
		width: 500,
		opacity:.35
	});
}

function popn(head, content) {
	$.fn.colorbox({
		html:"<div class='com_popibox'><div align='center'><div class='com_popihead'><h1>" + head + "</h1></div><div class='com_popicontent'>" + content + "</div></div>",
		width: 500,
		opacity:.35
	});
}


function submitlogin(which) {
	u = $w("com_l_user").value;
	p = $w("com_l_password").value;
	
	if(u == '' || p == '') {
		alert("You must enter you username and password to login");	
	} else {
		
		$w('com_pop_content').innerHTML = loadgraphic;
		
		var ajx = new ajax();
		ajx.open('get','includes/ajax.php?action=login&u=' + u + '&p=' + p + '&random=' + Math.random(9999999), true);
		ajx.onreadystatechange = function() {
			if(ajx.readyState == 4) {
				//alert(ajx.responseText);
				response = ajx.responseText.split('|');
				if(response[0] == 'good') {
					$w('com_pop_content').innerHTML = '<br><br>Login Successful<br><br><br><br>';
					$w('com_uiBox').innerHTML = '<div class="com_left com_loggedinmsg">Welcome ' + response[1] + '.</div><a class="com_button" href="account.php">Account</a><a class="com_button" href="javascript:logout();">Logout</a>';
				} else if(response[1] == 'banned') {
					$w('com_pop_content').innerHTML = "<h2 style='color:red;'>This account has been banned. Have a nice day.</h2>";
				} else {
					$w('com_pop_content').innerHTML = "<div style='color:red;'>Login Incorrect</div><h3>You must be logged in to comment.</h3><table><tr><td align='right'>Username:</td><td><input type='text' name='com_l_user' id='com_l_user'></td></tr><tr><td align='right'>Password:</td><td><input type='password' id='com_l_password'></td></tr><tr><td></td><td><a href='javascript:submitlogin(\"overlay\")' class='com_button'>login</a><a class='com_button' href=''>click here to register</a></td></tr></table>";
				}
			}
		}
		ajx.send(null);	
	}
}
function logout(onsite) {
	$.fn.colorbox({
		href:"http://comments.sltdb.com/logout.php?onsite=" +onsite,
		width:500,
		height:350,
		iframe:true
	});	
}

function settings() {
	$.fn.colorbox({
		href:"http://comments.sltdb.com/settings.php?refurl=" + document.location.href,
		width:650,
		height:660,
		iframe:true
	});	
}


function regUserNameCheck() {
	
	username = document.registration.username.value;
	
	var ajx = new ajax();
	ajx.open('get','includes/ajax.php?action=regusernamecheck&username=' + username + '&random=' + Math.random(9999999), true);
	ajx.onreadystatechange = function() {
		if(ajx.readyState == 4) {
			response = ajx.responseText;
			if(response > 0) {
				$w("com_usernameMsg").innerHTML = "Username is already in use";
				document.registration.usernamecheck.value = 'X';
			} else if(response == 0) {
				$w("com_usernameMsg").innerHTML = "";
				document.registration.usernamecheck.value = '';
			}
		}
	}
	ajx.send(null);	
}
function regCheck() {
	$broke = 0;
	if(document.registration.username.value == '') {
		$w("com_usernameMsg").innerHTML = "Username missing";
		$broke++;
	} else {
		regUserNameCheck();
		if(document.registration.usernamecheck.value == 'X') {
			$w("com_usernameMsg").innerHTML = "Username is already in use";
			$broke++;
		}
	}
	if(document.registration.password.value == '') {
		$w("com_passwordMsg").innerHTML = "Password missing";
		$broke++;
	}
	if(document.registration.month.selectedIndex == '0' || document.registration.day.selectedIndex == '0' || document.registration.year.selectedIndex == '0') {
		$w("com_dobMsg").innerHTML = "Date of Birth invalid";
		$broke++;
	}
	if(document.registration.email.value == '') {
		$w("com_emailMsg").innerHTML = "Email missing";
		$broke++;
	}
	if(document.registration.zip.value == '') {
		$w("com_zipMsg").innerHTML = "Zip Code missing";
		$broke++;
	}
	if($broke > 0) {
		return false;	
	} else {
		return true;	
	}
}

function change_parent_url(url)
{
	document.location=url;
}

function ignore(uid) {
	if(confirm("Are you sure you want to ignore all comments by this user?") == true) {
		$w('post_iframe').src = 'http://comments.sltdb.com/includes/ajax.php?action=ignore&id=' + uid + '&random=' + Math.random(99999999);
		popi('User Ignored','This user has been placed on your ignore list.<a href="javascript:closeoverlay();">Close</a>');	
	}
}

function vote(commentid, direction, onsite) {
	
	var url = 'http://comments.sltdb.com/includes/ajax.php?action=vote&id=' + commentid + '&thumbs=' + direction + '&onsite=' + onsite + '&random=' + Math.random(99999999);
	$w('post_iframe').src = url;
	
}

function votesuccess(comment, direction) {
	
	var ttl;
	if(direction == 1) {
		ttl = $w('up_' + comment).innerHTML;
		$w('up_' + comment).innerHTML = '+' + (parseInt(ttl)+1);
	} else {
		ttl = $w('down_' + comment).innerHTML;
		$w('down_' + comment).innerHTML = parseInt(ttl)-1;
	}
	
}

function votefail() {
	alert("You have already voted on that comment.");
}

function loginwarn() {
	alert("You must be logged in to do that.");
}