var cgiUrl = siteUrl + 'cgi-bin/';
var reqSignup = 0;
var srvUrl;
var reqObj;
var spObj;

function OutputResponse(voteId) {
var retQuote
var i=0
	if (reqObj.readyState == 4) {
	    retQuote = reqObj.responseText;
		
		if(reqSignup) {
			if(retQuote.indexOf("#sup#") != -1) {
				document.location = siteUrl + 'signIn.html';
				return;
			}
		}
		if(retQuote.indexOf("upAlert") != -1) {
			eval(retQuote);
			return;
		}
		spObj.innerHTML  = retQuote;
	}
}

function GetData(s_srvURL, doSignup) {
	reqSignup = doSignup;

 	// Create Object
 	reqObj = createObject();
	srvUrl = s_srvURL;

	reqObj.open("GET", srvUrl, true);
	reqObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	reqObj.onreadystatechange = OutputResponse;
	// Send to server.
	reqObj.send(null);
	
}

function createObject() {
	var xmlObj = false;

	if (typeof XMLHttpRequest != 'undefined') {
		try {
			xmlObj = new XMLHttpRequest();
		} catch (e) {
			xmlObj = false;
		}
	}
	if (!xmlObj) {
		try {
			xmlObj = new ActiveXObject ("Msxml2.XMLHTTP")
		}
		catch (e) {
			try {
				xmlObj = new ActiveXObject ("Microsoft.XMLHTTP")
			}
			catch (E) {
				xmlObj = false;
				alert ('Some interactive functionality will not be available in this browser');
			}
  		}
	}
	return xmlObj;
}
function showUrl(sUrl, divId, signUp) {
	var srvURL = siteUrl + sUrl;
	spObj = document.getElementById(divId);
	GetData(srvURL, signUp);
}

function voteUp(vId, hashId) {
	var srvURL = cgiUrl + "rkMain.cgi?actn=voteNews&vid="; 
	srvURL = srvURL + escape(vId);
	srvURL = srvURL + '&hs=';
	srvURL = srvURL + escape(hashId);
	spObj = document.getElementById('vt'+vId);

	GetData(srvURL, 1);
}

function voteDown(vId, hashId) {
	var srvURL = cgiUrl + "rkMain.cgi?actn=voteDownNews&vid="; 
	srvURL = srvURL + escape(vId);
	srvURL = srvURL + '&hs=';
	srvURL = srvURL + escape(hashId);
	spObj = document.getElementById('vt'+vId);
	GetData(srvURL, 1);
}

function setFav(vId, hashId) {
	var srvURL = cgiUrl + "rkMain.cgi?actn=setFav&vid="; 
	srvURL = srvURL + escape(vId);
	srvURL = srvURL + '&hs=';
	srvURL = srvURL + escape(hashId);
	spObj = document.getElementById('vf'+vId);

	GetData(srvURL, 1);
}

function voterPage(pageId, vId, showAll) {
	var srvURL = cgiUrl + "rkMain.cgi?actn=voterPage&vid="; 
	srvURL = srvURL + escape(vId);

	srvURL = srvURL + '&idx=';
	srvURL = srvURL + escape(pageId);

	srvURL = srvURL + '&sall=';
	srvURL = srvURL + escape(showAll);

	spObj = document.getElementById('vtrBoxId');

	GetData(srvURL, 0);
}

function addSource(userNick) {
	var agree=confirm("Are you sure you want to add " + userNick + " as a source?");
	if (!agree)
		return;

	var srvURL = cgiUrl + "rkAcct.cgi?actn=addSrc&uname="; 
	srvURL = srvURL + escape(userNick) + "&url=" + document.location;

	document.location = srvURL;
}

function rmSource(userNick) {
	var agree=confirm("Are you sure you want to remove " + userNick + " as a source?");
	if (!agree)
		return;

	var srvURL = cgiUrl + "rkAcct.cgi?actn=rmSrc&uname="; 
	srvURL = srvURL + escape(userNick) + "&url=" + document.location;
	document.location = srvURL;
}

function fvSubmit(vId) {

	formObj = eval('document.forms.fn' + vId ); 

	sTitle = formObj.title.value;
	sDescr = formObj.descr.value;

	var srvURL = cgiUrl + "rkSubmit.cgi?actn=updNews&title=" + encodeURIComponent(sTitle) 
		+ "&descr=" + encodeURIComponent(sDescr) 
		+ "&vid=" + vId;

	spObj = document.getElementById('fe' + vId);
	GetData(srvURL, 1);

	fTxt = document.getElementById('ft' + vId);
	fTxt.innerHTML = sTitle;

	fTxt = document.getElementById('fd' + vId);
	fTxt.innerHTML = sDescr;

	fTxt = document.getElementById('fdf' + vId);
	fTxt.innerHTML = encodeURIComponent(sDescr);

        fTxt = document.getElementById('fsc' + vId);
	if(sDescr.length>83)
		fTxt.innerHTML = sDescr.substring(0,78) + '...';
	else
		fTxt.innerHTML = sDescr;
	swFavUpd(vId);
}

function fvDelete(vId) {
	var agree=confirm("Are you sure you want to remove this favorite forever?");
	if (!agree)
		return;

	fvUnboldId(vId);
	//formObj = eval('document.forms.fn' + vId ); 
	var srvURL = cgiUrl + "rkSubmit.cgi?actn=rmMyNews&vid=" + vId;
	spObj = document.getElementById('vfb' + vId);
	GetData(srvURL, 1);
}

function wtDiv(divId, iVal) {

	wBox = 'wId' + divId;
	tBox = 'tId' + divId;

	wDiv = document.getElementById(wBox);
	tDiv = document.getElementById(tBox);
	nId  = document.getElementById('nId' + divId);
	
	if(iVal==1) {
		wDiv.style.display = "block";
		tDiv.style.display = "none";
		nId.focus();
		nId.select();
	}
	else {
		wDiv.style.display = "none";
		tDiv.style.display = "block";
	}
}

function wEdit(wId, wVal) {

	spObj = document.getElementById('tId' + wId);

	if(wVal>100) {
		spObj.innerHTML  = '100';
		spObj = document.getElementById('wId' + wId);
		spObj.innerHTML  = '100';
		wtDiv(wId, 0);
		return;
	}

	var srvURL = cgiUrl + "rkAcct.cgi?actn=updWgt&fid="; 
	srvURL = srvURL + escape(wId) + "&wgt=" + escape(wVal);

	wtDiv(wId, 0);
	GetData(srvURL, 1);
	return;
}

function chkAval(sNick) {

	if(sNick=='')
		return false;

	var srvURL = cgiUrl + "rkSignup.cgi?actn=chkAval&nick="; 
	srvURL = srvURL + escape(sNick);

	spObj = document.getElementById('chkAval');
	GetData(srvURL, 0);
}

function chkUNameAval(sNick) {
	if(sNick=='')
		return false;
	var srvURL = cgiUrl + "rkGroups.cgi?actn=chkAval&uname=";
	srvURL = srvURL + encodeURIComponent(sNick);
	spObj = document.getElementById('chkAval');
	GetData(srvURL, 0);
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function setVisible(iPath) {
	grayOut(true);
        w = (f_clientWidth() - 350)/2;
        h = ((f_clientHeight()  - 350)/2) + f_scrollTop();

	newObj = document.getElementById('imgpop');

	if(!newObj)
		newObj = document.createElement("div");

	newObj.id = "imgpop";
	var myBody = document.getElementsByTagName("body")[0];
	myBody.appendChild(newObj);

	newObj.style.left = w + 'px' ;
	newObj.style.top = h + 'px' ;

	newObj.style.width = '350px';
	//newObj.style.height = '350px';
	newObj.style.borderColor = '#000';
	newObj.style.borderWidth = "1px";
	newObj.style.borderStyle = "solid";
	newObj.style.backgroundColor = '#ddd';
	newObj.style.position = "absolute";
	newObj.style.padding = "15px";
	newObj.style.zIndex = "60";

	newObj.innerHTML =
		'<p><img src="/np/' + iPath + '/photo.jpg" style="border:1px solid #222;"/><p>' +
		'<a href="javascript:void(0);" onClick="rmLayer(\'imgpop\');">Close</a>';

	newObj.style.visibility = 'visible';
	newObj.style.display = 'block';
}

function rmLayer(divId) {
	oldObj = document.getElementById(divId);
	if(oldObj) {
		oldObj.style.display = 'none';
		delete oldObj;
	}
	grayOut(false);
}

function grayOut(vis, options) {
// Pass true to gray out screen, false to ungray
// options are optional.  This is a JSON object with the following (optional) properties
// opacity:0-100         // Lower number = less grayout higher = more of a blackout
// zindex: #             // HTML elements with a higher zindex appear on top of the gray out
// bgcolor: (#xxxxxx)    // Standard RGB Hex color code
// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
// in any order.  Pass only the properties you need to set.
var options = options || {};
var zindex = options.zindex || 50;
var opacity = options.opacity || 70;
var opaque = (opacity / 100);
var bgcolor = options.bgcolor || '#000000';
var dark=document.getElementById('darkenScreenObject');
if (!dark) {
	// The dark layer doesn't exist, it's never been created.  So we'll
	// create it here and apply some basic styles.
	// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
	var tbody = document.getElementsByTagName("body")[0];
	var tnode = document.createElement('div');           // Create the layer.
	tnode.style.position='absolute';                 // Position absolutely
	tnode.style.top='0px';                           // In the top
	tnode.style.left='0px';                          // Left corner of the page
	tnode.style.overflow='hidden';                   // Try to avoid making scroll bars
	tnode.style.display='none';                      // Start out Hidden
	tnode.id='darkenScreenObject';                   // Name it so we can find it later
	tbody.appendChild(tnode);                            // Add it to the web page
	dark=document.getElementById('darkenScreenObject');  // Get the object.
 }
 if (vis) {
	// Calculate the page width and height
	if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
		var pageWidth = document.body.scrollWidth+'px';
		var pageHeight = document.body.scrollHeight+'px';
	} else if( document.body.offsetWidth ) {
		var pageWidth = document.body.offsetWidth+'px';
		var pageHeight = document.body.offsetHeight+'px';
	} else {
		var pageWidth='100%';
		var pageHeight='100%';
	}
	//set the shader to cover the entire page and make it visible.
	dark.style.opacity=opaque;
	dark.style.MozOpacity=opaque;
	dark.style.filter='alpha(opacity='+opacity+')';
	dark.style.zIndex=zindex;
	dark.style.backgroundColor=bgcolor;
	dark.style.width= pageWidth;
	dark.style.height= pageHeight;
	dark.style.display='block';
} else {
	dark.style.display='none';
}
}

