YAHOO.namespace("login.container");
YAHOO.namespace("comments.container");
YAHOO.namespace("fwd.container");
YAHOO.namespace("fwd2.container");
YAHOO.namespace("favorites");

		function init() {
			// Instantiate a Panel from markup
			YAHOO.login.container.login = new YAHOO.widget.Panel("login", { width:"320px", visible:false, draggable:false, constraintoviewport:true } );
			YAHOO.login.container.login.render();
			YAHOO.comments.container.comments = new YAHOO.widget.Panel("comments", { width:"580px", visible:false, draggable:false, constraintoviewport:true } );
			YAHOO.comments.container.comments.render();
			YAHOO.fwd.container.fwd = new YAHOO.widget.Panel("fwd", { width:"360px", visible:false, draggable:false, constraintoviewport:true } );
			YAHOO.fwd.container.fwd.render();
			YAHOO.fwd2.container.fwd = new YAHOO.widget.Panel("fwd2", { width:"360px", visible:false, draggable:false, constraintoviewport:true } );
			YAHOO.fwd2.container.fwd.render();
		}

YAHOO.util.Event.addListener(window, "load", init);

function do_search(e)
{
	if (window.event)
	{
		if(e.keyCode == 13)
			document.getElementById('search1').click();
	}
	else if (e.which)
	{
		if(e.which == 13)
			document.getElementById('search1').click();
	}
}

function do_search2(e)
{
	if (window.event)
	{
		if(e.keyCode == 13)
			document.getElementById('search2').click();
	}
	else if (e.which)
	{
		if(e.which == 13)
			document.getElementById('search2').click();
	}
}

function sorter(sortee)
{
	document.search_form.sort.value = sortee;
	document.search_form.submit.click();
}
function sndReq(vote,id_num,ip_num,type) {
	if (vote >= 1 && vote <= 5)
	{
	var theUL = document.getElementById('unit_ul_'+id_num);
	theUL.innerHTML = '<div class="loading"></div>';
	
	var url = 'libs/rpc.php';
	var pars = 'vote='+vote+'&url_id='+id_num+'&id='+ip_num+'&type='+type;
	var answer = 'Null';
	//alert(pars);
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			postBody: pars, 
			onComplete: showvResponse
		});
	}
}

function showvResponse(originalRequest)
{
	//put returned XML in the textarea
	var answer = originalRequest.responseText;
	//alert(answer);
	var answer = answer.split("|");
	document.getElementById('unit_long_'+answer[0]).innerHTML = answer[1];
}

function url_click(url_id) {
	
	var url = '../libs/url_click.php';
	var pars = 'url_id=' + url_id;
	var answer = 'Null';
	//alert(pars);
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showResponse
	});

}

function showResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("<br>");
	//alert(answer);
	//window.location = window.location.href;
}

function do_login() {
	
	var url = '../libs/login.php';
	var email = document.getElementById('email').value;
	var password = document.getElementById('password').value;
	var username = document.getElementById('username').value;
	var user_status = document.login_form.user_status;
	
	for(var i = 0; i < 2; i++) {
		if(user_status[i].checked) {
			var status_value = user_status[i].value;
		}
	}
		
	var pars = 'email=' + email + '&password=' + password + '&username=' + username + '&user_status=' + status_value;
	var answer = 'Null';
	//alert(pars);
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showLResponse
	});

}

function showLResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("|");
	//alert(answer);
	if(word[0] == 1)
	{
		document.getElementById('login_response').innerHTML = '<font style=" color: #00FF00; ">' + word[1] + '</font>';
		window.location = "";
	}
	else
	{
		document.getElementById('login_response').innerHTML = '<font style=" color: #FF0000; ">' + word[1] + '</font>';
		document.getElementById('password').value = '';
	}
}

function do_logout() {
	
	var url = '../libs/logout.php';
		
	var pars = '';
	var answer = 'Null';
	//alert(pars);
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showLOResponse
	});

}

function showLOResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("|");
	window.location = "";
}

function flag(url_id,id) {
	
	var url = '../libs/flag.php';
	var pars = 'url_id=' + url_id + '&id=' + id;
	var answer = 'Null';
	var ans = confirm('Are you sure you want to flag this site?');
	if(ans == 1)
	{
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showFResponse
	});
	
	}
}

function showFResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("<br>");
	//alert(answer);
	//window.location = "http://" + word[4];
}

function show_login()
{
	YAHOO.login.container.login.show();
	document.getElementById('email').focus();
}

function do_click(e)
{
	if (window.event)
	{
		if(e.keyCode == 13)
			do_login();
	}
	else if (e.which)
	{
		if(e.which == 13)
			do_login();
	}
}

function search_sort(opt)
{
	window.location = window.location.href + "&sort=" + opt.value;
}

function comm(url_id, type)
{
	document.getElementById('url_id').value = url_id;
	document.getElementById('type').value = type;
	YAHOO.comments.container.comments.cfg.setProperty("context", ["comm" + type + "_" + url_id, "tl", "tl"]);
	myDataSource.sendRequest("&type=" + type + "&url_id=" + url_id,
            myDataTable.onDataReturnInitializeTable, myDataTable);
	myDataTable.refreshView();
	YAHOO.comments.container.comments.show();
	if(document.getElementById('comment_txt').disabled == false)
		document.getElementById('comment_txt').focus();
}

function sendCom() {
	var comm = document.getElementById('comment_txt').value;
	var url_id = document.getElementById('url_id').value;
	var type = document.getElementById('type').value;
	var url = '../libs/process.php';
	var pars = '&url_id=' + url_id + '&type=' + type + "&action=c&comment=" + comm;
	var answer = 'Null';
	//alert(pars);
	document.getElementById('comment_txt').value = '';
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showCommResponse
	});

}

function showCommResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("|");
	//alert(answer);
	//document.getElementById('bd').innerHTML = word[1];//alert('Thank You!');
	//window.location = "http://" + word[4];
	//YAHOO.comments.container.comments.show();
	myDataSource.sendRequest("&type=" + word[3] + "&url_id=" + word[1],
            myDataTable.onDataReturnInitializeTable, myDataTable);
	myDataTable.refreshView();
	document.getElementById('comm' + word[3] + '_cnt_id_' + word[1]).innerHTML = '[' + word[2] + ']';
}

function do_sendCom(e)
{
	if (window.event)
	{
		if(e.keyCode == 13)
			document.getElementById('submit_comments').click();
	}
	else if (e.which)
	{
		if(e.which == 13)
			document.getElementById('submit_comments').click();
	}
}

function fav(url_id, type) {

	var url = '../libs/process.php';
	var pars = 'url_id=' + url_id + '&type=' + type + "&action=f";
	var answer = 'Null';
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showFavResponse
	});

}

function showFavResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("|");
	//alert(answer);
	document.getElementById('fav' + word[1]).innerHTML = '<img src="/images/favorites_small_x.png" width="22" height="20" align="absbottom" alt="Add to Favorites"> ';
	document.getElementById('fav' + word[3] + '_cnt_id_' + word[1]).innerHTML = '[' + word[2] + ']';
}

function do_email(e)
{
	if (window.event)
	{
		if(e.keyCode == 13)
			document.getElementById('submit_comments').click();
	}
	else if (e.which)
	{
		if(e.which == 13)
			document.getElementById('submit_comments').click();
	}
}

function sendFwd() {
	if(document.getElementById('to_name').value != '' && document.getElementById('to_email').value != '')
	{
	var email_txt = document.getElementById('email_txt').value;
	var to_email = document.getElementById('to_email').value;
	var to_name = document.getElementById('to_name').value;
	var url_id = document.getElementById('url_id').value;
	var type = document.getElementById('type').value;
	var url = '../libs/process.php';
	var pars = 'url_id=' + url_id + '&type=' + type + "&action=e&email_txt=" + email_txt + "&to_name=" + to_name + "&to_email=" + to_email;
	var answer = 'Null';
	//alert(pars);
	document.getElementById('comment_txt').value = '';
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showFwdResponse
	});
	
	}
	else
	{
		document.getElementById('mail_response').innerHTML = '<font style=" color: #FF0000; ">Please Fill in Name and Email!</font>';
	}

}

function fwd(url_id, type)
{
	document.getElementById('url_id').value = url_id;
	document.getElementById('type').value = type;
	YAHOO.fwd.container.fwd.cfg.setProperty("context", ["comm" + type + "_" + url_id, "tl", "tr"]);
	YAHOO.fwd.container.fwd.show();
	document.getElementById('to_name').focus();
}

function showFwdResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("|");
	//alert(answer);
	if(word[0] == 1)
	{
		document.getElementById('mail_response').innerHTML = '<font style=" color: #00FF00; ">Mail Sent</font>';
		document.getElementById('email' + word[3] + '_cnt_id_' + word[1]).innerHTML = '[' + word[2] + ']';
		document.getElementById('to_email').value = '';
		document.getElementById('to_name').value = '';
	}
	else
	{
		document.getElementById('mail_response').innerHTML = word[0] + '<font style=" color: #FF0000; ">Error</font>';
	}
}

function showusername()
{
		document.getElementById('username_section').style.display = "block";
}

function hideusername()
{
		document.getElementById('username_section').style.display = "none";
}

function remove_fav(fav_id, type) {
	var url = '../libs/process.php';
	var pars = 'action=r&fav_id=' + fav_id + '&type=' + type;
	var answer = 'Null';
	//alert(pars);
	
	var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		postBody: pars, 
		onComplete: showRemResponse
	});

}

function showRemResponse(originalRequest)
{
	//put returned XML in the textarea
	answer = originalRequest.responseText;
	var word = answer.split("|");
	//alert(answer);
	myDataSource.sendRequest("results=100",
            myDataTable.onDataReturnInitializeTable, myDataTable);
	myDataTable.refreshView();
}