/**
 * Open a small popup browser window.
 * If link is not a URL it will be converted to once using the current URL.
 * @link = the page name or URL to open the window with.
 * @w = optional window width
 * @h = optional window height
 */
function popup(link, name, w, h)
{
  // set the size
	if (!w) w = 640;
	if (!h) h = 480;

  // default name
  if (!name) name = 'popup';
	
  // insure a proper URL
  if (link.indexOf('http://') < 0) {
	 // add page to base
	 link = getBase() + link;
	}
	
	this.window.open(link, name, 'scrollbars=1,menubar=0,personalbar=0,resizable=1,status=1,toolbar=0,width='+w+',height='+h+',dependent=1');
}

/**
 * Removes any query string or anchor from the URL.
 */
function getBase()
{
  // remove filename if it is present
  path = document.location.pathname;
  idx = path.lastIndexOf("/");
  if (idx < (path.length - 1)) {
    // remove file name
    path = path.substring(0, ++idx);
  }

  return document.location.protocol + "//" + document.location.hostname + path;
}


/**
 * Checks the limit of the supplied field.
 * REQUIREMENTS:
 * 	- attempts to write to an element of name: field.name+'_count'
 */
function chrLimit(field,limit)
{
	try
	{
		var lbl = document.getElementById(field.name+'_count');
		var color = (field.value.length <= limit) ? 'green' : 'red';
		lbl.innerHTML = "<font color='"+color+"'><b>("+field.value.length+" of "+limit+")</b></font>";
	}
	catch(e)
	{
		alert("Missing dynamic element: "+field.name+"_count");
	}
}

function doAjax(url)
{
	// assert valid URL
	if (url.indexOf('http') < 0) url = getBaseURL()+url;
	
	// create new JS element
	var js = document.createElement('SCRIPT');
	js.type = 'text/javascript';
	js.language = 'javascript';
	js.src = url;
	
	// append JS element (therefore executing the 'AJAX' call)
	try
	{
		var ajax = document.getElementById('ajax_tag');
		ajax.appendChild(js);
	}
	catch(e)
	{
		alert("Missing Ajax element 'ajax_tag': "+e.message);
	}
}

function getBaseURL()
{
	var url = ''+document.location.href;
	var xend = url.lastIndexOf("/") + 1;
	return url.substring(0, xend);
}

// checks to see if the enter key was pressed, 
// if so, calls the callback function
// if the callback function returns false, then this function sets the keyCode to null
function checkForEnter(event, callback)
{
	if (event.keyCode == 13) {
		if (!eval(callback+'();')) event.keyCode = null;
	}
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

// find a document element by id
function getElement(id)
{
  return document.getElementById(id);
}

var E_ITEM = 1;
var E_COMMENT = 2;
var E_USER = 3;
function vote(id, like, itemType)
{
	if (itemType == undefined) itemType = E_ITEM;
	doAjax('ajax_vote.php?id='+id+'&like='+like+'&type='+itemType);
}

// id == item id
// fav == 0 | 1, for favorite state
function favorite(id, fav)
{
	doAjax('ajax_favorite.php?id='+id+'&fav='+fav);
}
