// javascript library for likey.com
// Aras Angelo@likey

function check_e(e,user) {
var evt = e || window.event;


    if (evt.keyCode == 13) {
	t_render(user);
	}
	
return true;
}

function toggle(tab) {
var mytab = 'info'+tab;
var content = document.getElementById(mytab).innerHTML;
document.getElementById('infotab').innerHTML = content;

	for (i=1; i<=4; i++) {
	var othertab = 'tab'+i;
	
		if (othertab != tab) {
		document.getElementById(othertab).className='';
		}
		else {
		document.getElementById(othertab).className='selected';
		}
		
	}
	
}

function illuminate(tag) {
var mye = document.getElementById(tag);

	if (mye.style.display == 'none') {
	mye.style.display = 'block';
	}
	else {
	mye.style.display = 'none';
	}
	
return true;
}


function a_render(likey,sess) {

	if (likey == "none") {
	return false;
	}
	
var xmlHttpReq = false;
var self = this;
	
	if (window.XMLHttpRequest) {
	self.xmlHttpReq = new XMLHttpRequest();
	}
   	
	else if (window.ActiveXObject) {
	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}


self.xmlHttpReq.open('POST', 'score.php', true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

document.getElementById('mainarea').innerHTML = 'Please wait, Likey is pulling content that you may like<br><img src="images/wait.gif">';
document.getElementById('additional').innerHTML = '<img src="images/indicator_white.gif"> Loading...';
document.getElementById('mysites').innerHTML = '<img src="images/indicator_white.gif"> Loading...<br>';
	
	self.xmlHttpReq.onreadystatechange = function() {
	
		if (self.xmlHttpReq.readyState == 4) {
		var topics = self.xmlHttpReq.responseText.split('||||');
		document.getElementById('mainarea').innerHTML = topics[0];
		
		
		document.getElementById('mysites').innerHTML = topics[3];
		
		
			if (topics[2] != "") {
			document.getElementById('additional').innerHTML = topics[2]+' additional articles found.<br>(<a href="#" onClick="illuminate(\'show_additional\')">show me</a>)<br><div id="show_additional" style="display: none;">'+topics[1]+'</div>';
			}
			else {
			document.getElementById('additional').innerHTML = 'No additional topics to show';
			}
		}

	}

qstr = 'likey=' + escape(likey)+'&sess_key='+ escape(sess);
self.xmlHttpReq.send(qstr);
}


function t_render(sess) {

var topic = document.getElementById('newtopic').value;

	if (topic == "") {
	return false;
	}
	
var xmlHttpReq = false;
var self = this;
	
	if (window.XMLHttpRequest) {
	self.xmlHttpReq = new XMLHttpRequest();
	}
   	
	else if (window.ActiveXObject) {
	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}


self.xmlHttpReq.open('POST', 'score.php', true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
document.getElementById('mainarea').innerHTML = '<img src="images/wait3.gif> Likey will now bring you articles for your new topic';
document.getElementById('additional').innerHTML = '<img src="images/indicator_white.gif"> Loading...';

var topic_area = document.getElementById('mytopics').innerHTML;
document.getElementById('mytopics').innerHTML = 'rearranging your keywords...<br>';
	
	self.xmlHttpReq.onreadystatechange = function() {
	
		if (self.xmlHttpReq.readyState == 4) {
		var topics = self.xmlHttpReq.responseText.split('||||');
		
			if (topics[0] == "") {
			topics[0] = 'Likey was unable to find new articles for this keyword at the moment.<br>However it is saved to your topics and Likey will continue searching for it.';
			}
			
		document.getElementById('mainarea').innerHTML = topics[0];
		
			if (topics[2] != "") {
			document.getElementById('additional').innerHTML = topics[2]+' additional articles found.<br>(<a href="javascript:void(0)" onClick="illuminate(\'show_additional\')">show me</a>)<br><div id="show_additional" style="display: none;">'+topics[1]+'</div>';
			}
			else {
			document.getElementById('additional').innerHTML = 'No additional topics to show';
			}
			
		document.getElementById('mytopics').innerHTML = topics[4];
		//illuminate('add_topic');
		
		}

	}

qstr = 'likey=' + escape(topic)+'&sess_key='+ escape(sess) + '&cache=force';
self.xmlHttpReq.send(qstr);
}



function likey(what,who,where,how,via,mode) {

	if (typeof mode == 'undefined' ) mode = 'default';

	if (how == "0") {
	var element = 'item'+where;
	document.getElementById(element).style.backgroundColor = 'darkgray';
	document.getElementById(element).innerHTML = 'This article is clipped. Thanks for voting! We will try not to show similar articles that you won\'t like.';
	setTimeout("remove_div('"+element+"')",2000);
	}
	
	else if (how == "1") {
	var element = 'article_title'+where;
	var backbox = 'abox'+where;
	var layer = document.getElementById(element);
	var str = layer.innerHTML;
	
		if (str.indexOf('<img src="images/mark_set.png">') == '-1') {
		layer.innerHTML = '<img src="images/mark_set.png"> '+str;
		document.getElementById(backbox).style.backgroundColor = '#CFF3CB';
		// increment vote.
		var counter = 'z_'+what+'_count';
		var nextvalue = parseInt(document.getElementById(counter).innerHTML) + 1;
		document.getElementById(counter).innerHTML = nextvalue;		
		}
		
	}
	
	else if (how == "2") {
	var element = 'item'+where;
	document.getElementById(element).style.backgroundColor = 'darkgray';
	document.getElementById(element).innerHTML = 'This article is marked as read. You can reach this article from the Articles I Read menu in the future.';
	setTimeout("remove_div('"+element+"')",2000);
	}

var xmlHttpReq = false;
var self = this;
	
	if (window.XMLHttpRequest) {
	self.xmlHttpReq = new XMLHttpRequest();
	}
   	
	else if (window.ActiveXObject) {
	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}


self.xmlHttpReq.open('POST', 'messenger.php', true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	self.xmlHttpReq.onreadystatechange = function() {
	
		if (self.xmlHttpReq.readyState == 4) {
		//alert(self.xmlHttpReq.responseText);
		return true;
		}
	
	}


qstr = 'mode=likey&what=' + escape(what)+'&who='+ escape(who) + '&how='+escape(how)+'&via='+escape(via)+'&mode2='+escape(mode);


self.xmlHttpReq.send(qstr);

	
}


function urlscan(sid,type) {

var xmlHttpReq1 = false;
var self1 = this;
var element = document.getElementById('siteprogress');
	
	if (window.XMLHttpRequest) {
	self1.xmlHttpReq = new XMLHttpRequest();
	}
   	
	else if (window.ActiveXObject) {
	self1.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}


self1.xmlHttpReq.open('POST', 'messenger.php', true);
self1.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	if (type == "home") {
	element.innerHTML = '<img src="images/orangewait.gif"><br>Please hold... Likey is searching the homepage for this web site...';
	}
	if (type == "deep") {
	element.innerHTML = '<img src="images/orangewait.gif"><br>Please hold... Likey is searching more of this web site...';
	}
	
	self1.xmlHttpReq.onreadystatechange = function() {
		
		if (self1.xmlHttpReq.readyState == 4) {
		// do nothing
		}
	
	}
	
var siteurl = document.getElementById('siteurl').value;
qstr = 'mode=urlscan&siteurl=' + escape(siteurl)+'&sid='+escape(sid)+'&type='+escape(type);
self1.xmlHttpReq.send(qstr);
}

var donot = false;


function progress_meter(sid,runtime) {



	var pelement = document.getElementById('siteprogress');

if (donot != true) {
	
	if (runtime != 1) {
	var responseArray = ['Likey is still searching this web site...','Did you know that Likey will refine your results based on your votes?','If you found what you are looking for, you can stop... we are still searching...'];
	var now = new Date();
	var seed= now.getSeconds();
	var rand= Math.round(Math.random() * seed) % 3;
	var rand2= Math.round(Math.random() * seed) % 3;
	var msg = responseArray[rand];

		if (rand2 == '2') {
		pelement.innerHTML = '<img src="images/orangewait.gif"><br>'+msg+'<br><a href="javascript: void(0)" onClick="stop()">Click here</a> to stop searching';
		}
		
	}
	

var xmlHttpReq = false;
var self = this;
var element = document.getElementById('siteprogress');
	
	if (window.XMLHttpRequest) {
	self.xmlHttpReq = new XMLHttpRequest();
	}
   	
	else if (window.ActiveXObject) {
	self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}


self.xmlHttpReq.open('POST', 'messenger.php', true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

var element = document.getElementById('feeds');

	self.xmlHttpReq.onreadystatechange = function() {
		
		if (self.xmlHttpReq.readyState == 4) {
		var split = self.xmlHttpReq.responseText.split('--ENDLINE');
		
			if (split[1] == '--') {
			donot = true;
			var stop = '1';
			pelement.innerHTML = '<b>Search Completed</b>';
			}
			
		element.innerHTML = split[0];
		}
	
	}
	
qstr = 'mode=txtscan&sid='+escape(sid);
self.xmlHttpReq.send(qstr);



	if (donot == true) {
	pelement.innerHTML = '<b>Search Completed</b>';
	}
	else {
	setTimeout("progress_meter("+sid+",0)",3000);
	}
	
}
else {
pelement.innerHTML = '<b>Search Completed</b>';
}
}

function stop() {
donot = true;
}

function start() {
donot = false;
}


function importlist(qtype) {

	if (typeof qtype == "undefined") {
    qtype = "default";
    }

var xmlHttpReq = false;
var self = this;
	
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
   	
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', 'import.php', true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {

var element;

	if (qtype == "search") {
	element = document.getElementById("importbox2");
	
	}
	else {
	element = document.getElementById("importbox");
	}
	
	element.innerHTML = '<img src="images/indicator_white.gif"> <b>One moment please...</b>';

if (self.xmlHttpReq.readyState == 4) {

element.style.display = 'block';

	if (self.xmlHttpReq.responseText == "") {
	element.innerHTML = 'We are unable to load your contact list, make sure you provided the correct credentials';
	}
	
	else {
	element.innerHTML = 'Following friends were found from your contact list. Select friends to send Likey invitation.<br><br>	<p align="left" style="height: 200px; width: 500px; overflow: scroll; border: 1px solid black; background-color: white; padding: 10px;">'+self.xmlHttpReq.responseText+'</p> <input type="submit" value="Invite">';
	}
}

}

var form = document.forms['import'];
var mail = form.mail.value;
var user = form.user.value;
var pass = form.pass.value;
qstr = 'mail='+escape(mail)+'&user='+escape(user)+'&pass='+escape(pass);
self.xmlHttpReq.send(qstr);
return false;

}


function remove_div(element) {
var r1 = document.getElementById(element);
r1.removeAttribute('style');
r1.removeAttribute('innerHTML');
r1.removeChild(r1.childNodes[0]);
r1.removeAttribute('id');
return true;
}


function reason(article,sess,layer) {

var xmlHttpReq1 = false;
var self1 = this;
var element = document.getElementById('reason'+layer);
	
	if (window.XMLHttpRequest) {
	self1.xmlHttpReq = new XMLHttpRequest();
	}
   	
	else if (window.ActiveXObject) {
	self1.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}


self1.xmlHttpReq.open('POST', 'messenger.php', true);
self1.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
element.innerHTML = 'One moment...';

	
	self1.xmlHttpReq.onreadystatechange = function() {
		
		if (self1.xmlHttpReq.readyState == 4) {
		element.innerHTML = self1.xmlHttpReq.responseText;
		}
	
	}
	
qstr = 'mode=reason&article=' + escape(article)+'&sess='+escape(sess);
self1.xmlHttpReq.send(qstr);
}
