// Trim whitespace off the beginning and end of a string.
function trim(str) {
    return (str.replace(/^\s+|\s+$/g, ''));
}

// Load an ajax procedure asynchronously.
function asyncLoad(dest, cb) {
    xmlhttp = (window.XMLHttpRequest ?
	       new XMLHttpRequest()  :
	       new ActiveXObject("Microsoft.XMLHTTP"));
    xmlhttp.onreadystatechange = cb;
    xmlhttp.open("GET", dest, true);
    xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    xmlhttp.send(null);
}

// Load an ajax procedure synchronously.
function syncLoad(dest) {
    xmlhttp = (window.XMLHttpRequest ?
	       new XMLHttpRequest()  :
	       new ActiveXObject("Microsoft.XMLHTTP"));
    xmlhttp.open("GET", dest, false);
    xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    xmlhttp.send(null);
    return xmlhttp.responseText;
}

var voteurl      = 'http://' + _domain + '/ajax/news/vote.php';
var imagebase    = 'http://ofrecord.com/images/';
var voteupimg    = 'vote-up.gif';
var votedupimg   = 'voted-up.gif';
var votedownimg  = 'vote-down.gif';
var voteddownimg = 'voted-down.gif';
var voteupalt    = 'vote up';
var votedupalt   = 'voted up';
var votedownalt  = 'vote down';
var voteddownalt = 'voted down';

// Use ajax to update the vote count of a specified article on an internal page.
function i_incvote(articleid, user, votes) {
//    return sync_i_incvote(articleid, user, votes);
    return async_i_incvote(articleid, user, votes);
}
// Use ajax to update the vote count of a specified article on an external page.
function e_incvote(articleid, user, votes) {
//    return sync_e_incvote(articleid, user, votes);
    return async_e_incvote(articleid, user, votes);
}

// Synchronous version of the function which updates the
// vote count of a specified article on an internal page.
function sync_i_incvote(articleid, user, votes) {
    try {
	element = document.getElementById('v' + articleid);
	if (element == undefined) {
	    return (true);
	}
	if (articleid < 0 || votes == 0) {
	    return (true);
	}
	url = voteurl + '?t=i&i=' + articleid + '&u=' + user + '&n=' + votes;
	response = trim(syncLoad(url));
	if (response == '') {
	    // do nothing
	}
	else {
	    sresponse = response.substring(0, 5);
	    if (sresponse == 'ERROR') {
		// do nothing
	    }
	    else {
		// The response is the updated vote count.
		n = 0;
		try {
		    n = parseInt(response);
		}
		catch (ee) {
		    n = 0;
		}
		element.innerHTML = (n > 0 ? '+' : '') + response;
	    }
	}
    }
    catch (ex) {}
    return (true);
}

// Asynchronous version of the function which updates the
// vote count of a specified article on an internal page.
function async_i_incvote(articleid, user, votes) {
    try {
	element = document.getElementById('v' + articleid);
	if (element == undefined) {
	    return (true);
	}
	if (articleid < 0 || votes == 0) {
	    return (true);
	}
	url = voteurl + '?t=i&i=' + articleid + '&u=' + user + '&n=' + votes;
	cb = function() {
	    try {
		if (xmlhttp.readyState == 4) {
		    response = trim(xmlhttp.responseText);
		    if (response == '') {
			// do nothing
		    }
		    else {
			sresponse = response.substring(0, 5);
			if (sresponse == 'ERROR') {
			    // do nothing
			}
			else {
			    // The response is the updated vote count.
			    n = 0;
			    try {
				n = parseInt(response);
			    }
			    catch (ee) {
				n = 0;
			    }
			    element.innerHTML = (n > 0 ? '+' : '') + response;
			}
		    }
		}
	    }
	    catch (exc) {}
	}
	asyncLoad(url, cb)
    }
    catch (ex) {}
    return (true);
}

// Synchronous version of the function which updates the
// vote count of a specified article on an external page.
function sync_e_incvote(articleid, user, votes) {
    try {
	vuelement = document.getElementById('vu' + articleid);
	vdelement = document.getElementById('vd' + articleid);
	if (vuelement == undefined || vdelement == undefined) {
	    return (true);
	}
	if (articleid < 0 || votes == 0) {
	    return (true);
	}
	url = voteurl + '?t=i&i=' + articleid + '&u=' + user + '&n=' + votes;
	response = trim(syncLoad(url));
	if (response == '') {
	    // do nothing
	}
	else {
	    sresponse = response.substring(0, 5);
	    if (sresponse == 'ERROR') {
		// do nothing
	    }
	    else {
		// The response is the updated vote count and
		// the last vote for the given user, separated
		// by a vertical bar.
		items = response.split('|', 2);
		if (items.length < 2) {
		    // do nothing
		}
		else {
		    try {
			count = parseInt(items[0]);
		    }
		    catch (ee) {
			count = 0;
		    }
		    try {
			latest = parseInt(items[1]);
		    }
		    catch (ee) {
			latest = 0;
		    }
		    var vuimg = '';
		    var vdimg = '';
		    var vualt = '';
		    var vdalt = '';
		    if (latest == 0) {
			vuimg = voteupimg;
			vualt = voteupalt;
			vdimg = votedownimg;
			vdalt = votedownalt;
		    }
		    else if (latest > 0) {
			vuimg = votedupimg;
			vualt = votedupalt;
			vdimg = votedownimg;
			vdalt = votedownalt;
		    }
		    else {
			vuimg = voteupimg;
			vualt = voteupalt;
			vdimg = voteddownimg;
			vdalt = voteddownalt;
		    }
		    vuelement.src = imagebase + vuimg;
		    vuelement.alt = vualt;
		    vdelement.src = imagebase + vdimg;
		    vdelement.alt = vdalt;
		    if (vcelement != undefined) {
			vcelement.innerHTML =
			    (count > 0 ? '+' : '') + count;
		    }
		}
	    }
	}
    }
    catch (ex) {}
    return (true);
}

// Asynchronous version of the function which updates the
// vote count of a specified article on an external page.
function async_e_incvote(articleid, user, votes) {
    try {
	vcelement = document.getElementById('vc' + articleid);
	vuelement = document.getElementById('vu' + articleid);
	vdelement = document.getElementById('vd' + articleid);
	if (vuelement == undefined || vdelement == undefined) {
	    return (true);
	}
	if (articleid < 0 || votes == 0) {
	    return (true);
	}
	url = voteurl + '?t=e&i=' + articleid + '&u=' + user + '&n=' + votes;
	cb = function() {
	    try {
		if (xmlhttp.readyState == 4) {
		    response = trim(xmlhttp.responseText);
		    if (response == '') {
			// do nothing
		    }
		    else {
			sresponse = response.substring(0, 5);
			if (sresponse == 'ERROR') {
			    // do nothing
			}
			else {
			    // The response is the updated vote count and
			    // the last vote for the given user, separated
			    // by a vertical bar.
			    items = response.split('|', 2);
			    if (items.length < 2) {
				// do nothing
			    }
			    else {
				try {
				    count = parseInt(items[0]);
				}
				catch (ee) {
				    count = 0;
				}
				try {
				    latest = parseInt(items[1]);
				}
				catch (ee) {
				    latest = 0;
				}
				var vuimg = '';
				var vdimg = '';
				var vualt = '';
				var vdalt = '';
				if (latest == 0) {
				    vuimg = voteupimg;
				    vualt = voteupalt;
				    vdimg = votedownimg;
				    vdalt = votedownalt;
				}
				else if (latest > 0) {
				    vuimg = votedupimg;
				    vualt = votedupalt;
				    vdimg = votedownimg;
				    vdalt = votedownalt;
				}
				else {
				    vuimg = voteupimg;
				    vualt = voteupalt;
				    vdimg = voteddownimg;
				    vdalt = voteddownalt;
				}
				vuelement.src = imagebase + vuimg;
				vuelement.alt = vualt;
				vdelement.src = imagebase + vdimg;
				vdelement.alt = vdalt;
				if (vcelement != undefined) {
				    vcelement.innerHTML =
					(count > 0 ? '+' : '') + count;
				}
			    }
			}
		    }
		}
	    }
	    catch (exc) {}
	}
	asyncLoad(url, cb)
    }
    catch (ex) {}
    return (true);
}

function getobjbyid(oid) {
  if (document.getElementById) {
    return document.getElementById(oid);
  }
  else if (document.layers) {
    return eval("document." + oid);
  }
  else if (document.all) {
    return eval("document.all." + oid);
  }
  else {
    return eval("document." + oid);
  }
}

var browserName  = navigator.appName;

function titlecount(thisobj, idbase, maxtitle, maxtotal) {
  title_count_name     = idbase + "-title-count";
  title_total_name     = idbase + "-title-total";
  subtitle_value_name  = idbase + "-subtitle";
  subtitle_count_name  = idbase + "-subtitle-count";
  subtitle_total_name  = idbase + "-subtitle-total";
  title_count_obj      = getobjbyid(title_count_name);
  title_total_obj      = getobjbyid(title_total_name);
  subtitle_value_obj   = getobjbyid(subtitle_value_name);
  subtitle_count_obj   = getobjbyid(subtitle_count_name);
  subtitle_total_obj   = getobjbyid(subtitle_total_name);
  title_text           = thisobj.value;
  title_text_length    = title_text.length;
  subtitle_text        = subtitle_value_obj.value;
  subtitle_text_length = subtitle_text.length;
  pos_class = 'positive';
  neg_class = 'negative';
  if (title_text_length > maxtitle) {
    title_class = neg_class;
    title_count = maxtitle - title_text_length;
    maxsubtitle = maxtotal - maxtitle;
  }
  else {
    title_class = pos_class;
    title_count = title_text_length;
    maxsubtitle = maxtotal - title_text_length;
  }
  if (subtitle_text_length > maxsubtitle) {
    subtitle_class = neg_class;
    subtitle_count = maxsubtitle - subtitle_text_length;
  }
  else {
    subtitle_class = pos_class;
    subtitle_count = subtitle_text_length;
  }
  if (title_count_obj) {
    title_count_obj.innerHTML = '' + title_count;
    title_count_obj.className = title_class;
  }
  if (subtitle_count_obj) {
    subtitle_count_obj.innerHTML = '' + subtitle_count;
    subtitle_count_obj.className = subtitle_class;
  }
  if (subtitle_total_obj) {
    subtitle_total_obj.innerHTML = '' + maxsubtitle;
    subtitle_total_obj.className = pos_class;
  }
  return true;
}
function subtitlecount(thisobj, idbase, maxtitle, maxtotal) {
  subtitle_count_name  = idbase + "-subtitle-count";
  subtitle_total_name  = idbase + "-subtitle-total";
  title_value_name     = idbase + "-title";
  subtitle_count_obj   = getobjbyid(subtitle_count_name);
  subtitle_total_obj   = getobjbyid(subtitle_total_name);
  title_value_obj      = getobjbyid(title_value_name);
  subtitle_text        = thisobj.value;
  subtitle_text_length = subtitle_text.length;
  title_text           = title_value_obj.value;
  title_text_length    = title_text.length;
  pos_class = 'positive';
  neg_class = 'negative';
  if (title_text_length > maxtitle) {
    maxsubtitle = maxtotal - maxtitle;
  }
  else {
    maxsubtitle = maxtotal - title_text_length;
  }
  subtitle_total = maxsubtitle - subtitle_text_length;
  if (subtitle_text_length > maxsubtitle) {
    subtitle_class = neg_class;
    subtitle_count = maxsubtitle - subtitle_text_length;
  }
  else {
    subtitle_class = pos_class;
    subtitle_count = subtitle_text_length;
  }
  if (subtitle_count_obj) {
    subtitle_count_obj.innerHTML = '' + subtitle_count;
    subtitle_count_obj.className = subtitle_class;
  }
  if (subtitle_total_obj) {
    subtitle_total_obj.innerHTML = '' + maxsubtitle;
    subtitle_total_obj.className = pos_class;
  }
  return true;
}
function fieldcount(thisobj, maxcount) {
  field_id          = thisobj.id;
  field_text        = thisobj.value;
  field_text_length = field_text.length;
  field_count_name  = field_id + "-count";
  field_total_name  = field_id + "-total";
  field_count_obj   = getobjbyid(field_count_name);
  field_total_obj   = getobjbyid(field_total_name);
  pos_class = 'positive';
  neg_class = 'negative';
  if (field_text_length > maxcount) {
    field_class = neg_class;
    field_count = maxcount - field_text_length;
  }
  else {
    field_class = pos_class;
    field_count = field_text_length;
  }
  if (field_count_obj) {
    field_count_obj.innerHTML = '' + field_count;
    field_count_obj.className = field_class;
  }
  if (field_total_obj) {
    field_total_obj.innerHTML = '' + maxcount;
    field_total_obj.className = pos_class;
  }
  return true;
}
var _sc = null;
var _li = null;
function removeTimedMessage() {
  try {
    if (_li != null) {
      _li.className = 'visible';
      _li.innerHTML = _logininfo;
    }
    if (_sc != null) {
      _sc.className = 'invisible';
      _sc.innerHTML = '';
    }
  }
  catch (e) {}
  try {
    _timedmsg = '';
  }
  catch (e) {}
  return (true);
}      
var _timer = null;
function disableTimer() {
  try {
    if (_timer != null) {
      clearTimeout(_timer);
      _timer = null;
    }
  }
  catch (e) {}
}
function runAfter(delaySeconds, code) {
  try {
    disableTimer();
    _li = getobjbyid('login-information');
    _sc = getobjbyid('system-confirmation');
    if (_timedmsg == null || _timedmsg == '') {
	liclass = 'visible';
	scclass = 'invisible';
    }
    else {
	liclass = 'invisible';
	scclass = 'visible';
    }
    if (_li != null) {
      _li.className = liclass;
      _li.innerHTML = ((_timedmsg == null || _timedmsg == '') ? _logininfo : '');
    }
    if (_sc != null) {
      _sc.className = scclass;
      _sc.innerHTML = (_timedmsg == null ? '' : _timedmsg);
    }
    if (code != null && delaySeconds > 0) {
      _timer = setTimeout(code, delaySeconds * 1000);
    }
  }
  catch (e) {}
  return (true);
}
