function trackerImage()
{
  document.getElementById('pldiv').innerHTML = plimg;
}

function searchValidate()
{
  var return_boolean = false;
  var str = document.getElementById("search_text").value.replace(/ /g, "");
  if( (str.length < 2) || (str == 'word(s)or"quotedphrase"') )
  {
    alert("Your search query must be at least 2 characters");
    document.getElementById("search_text").focus();
  }
  else
  {
    return_boolean = true;
  }

  return return_boolean;
}

// Validate domain name
function validate_domain_name(txtBoxId) {
  var domainNamesTextBox = document.getElementById(txtBoxId);
  if (domainNamesTextBox != null) {
    if (domainNamesTextBox.value.length <= 0) {
      alert('Please enter a domain name.');
      return false;
    }
    else {
      var regExInvalidChars = /[^a-zA-Z0-9-\s.]+/;
      var isInvalid = regExInvalidChars.test(domainNamesTextBox.value);
      if (isInvalid) {
        alert('Invalid character in domain. Only letters, numbers or hyphens are allowed.');
        return false;
      }
    }
  }
  return true;
}

// Popup functions
function blogPopin(url, w, h)
{
	// Get position to place popin
	var posTop = '150px';
	var posLeft = '0px';
	if( window.innerWidth != null )
		posLeft = parseInt((window.innerWidth - parseInt(w)) / 2) + 'px';
	else if( document.documentElement && document.documentElement.clientWidth )
		posLeft = parseInt((document.documentElement.clientWidth - parseInt(w)) / 2) + 'px';
	else if( document.body != null )
		posLeft = parseInt((document.body.clientWidth - parseInt(w)) / 2) + 'px';

	var ifr = document.getElementById('blogPopin');
	if( !ifr && document.createElement )
	{
		ifr = document.createElement('iframe');
		if( ifr )
		{
			ifr.src = url;
			ifr.id = 'blogPopin';
			ifr.frameBorder = '0';
			ifr.style.border = '1px solid black';
			ifr.style.width = w;
			ifr.style.height = h;
			ifr.style.overflow = 'hidden';
			ifr.style.display = 'block';
			ifr.style.position = 'absolute';
			ifr.style.top = posTop;
			ifr.style.left = posLeft;
			document.body.appendChild(ifr);
		}
		var ifr = document.getElementById('blogPopin');
	}
	
	// IE6 does not support create element
	if( navigator.appVersion.match('MSIE 6') || !document.createElement )
	{
		if( ifr )
			ifr.style.display = 'none';
		ifr = null;
	}

	if( ifr )
	{
		ifr.style.display = 'block';
		ifr.src = url;
	}

	if( !ifr )
	{
		url = url.replace('&i=1', '&i=0');
		if ( !window.open(url, 'submail', 'width=448,height=264') )
		{
			alert ('Please disable your popup blocker to complete this action.');
		}
	}
}

function closeBlogPopin()
{
	document.getElementById('blogPopin').style.display='none';
}

function popup_blogwindow(url, parms, windowname, w, h)    {
  var width = w;
  if (width > screen.availWidth) width = screen.availWidth;
  var height = h;
  if (height > screen.availHeight) height = screen.availHeight;
  var left = parseInt((screen.availWidth/2) - (width/2));
  var top = parseInt((screen.availHeight/2) - (height/2));
  var window_features =        "height=" + height +        ",width=" + width +
      ",left=" + left +
      ",top=" + top +
      ",resizable=0" +
      ",screenX=" + left +
      ",screenY=" + top;
  var poploc = url;
  if (parms) poploc += "?" + parms;
  var wp = window.open(poploc, windowname, window_features);
  if (!wp) {
      alert("Your browser's popup-blocker is preventing this window from being displayed");
      return;
  }
  wp.focus();
}


function subscribePopinNew(addr, type)
{
	if ( addr == 'Enter your email address here' )
	addr = '';
	if ( !type )
		type = 'emailaddr';
	addr = escape(addr);

	if( type == 'subconf' || type == 'unsub' || type == 'comment_unsubscribe' )
		blogPopin('/submail.php?' + type + '=' + addr + '&i=1', '448px', '200px');
	else
		popup_blogwindow('/submail.php', type + '=' + addr, 'subPopin', 448, 360);
}

function unsubscribeCommentPopupNew(addr, entry_id)
{
	addr = escape(addr);
	if( entry_id > 0 )
	{
		popup_blogwindow('/submail.php?comment_unsubscribe=1&email=' + addr + '&i=1', 'subPopin', 448, 360);
	}
}

function subPopinOnLoad() {
	var subconf;
	if ( subconf = window.location.href.match(/subconf=(.*)/) ) {
		subscribePopinNew(unescape(subconf[1]), 'subconf');
	}
	else if ( subconf = window.location.href.match(/unsub=(.*)/) ) {
		subscribePopinNew(unescape(subconf[1]), 'unsub');
	}
	else if( subconf = window.location.href.match(/comment_unsubscribe=(.*)/) ) {
		subscribePopinNew(unescape(subconf[1]), 'comment_unsubscribe');
	}
	else if( subconf = window.location.href.match(/rss=1/) ) {
		var rand = Math.floor(Math.random()*10000000);

		// Nothing to do with subscriptions. But log when they come over from the rss feed.
		ajaxFunction('/ajax_rss_track.php', 'entry_id=' + current_entry_id + "&r=" + rand, 'GET'); 
	}
}


/**
 * Cookie functions from javascript
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + value +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return '';
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end)).replace(/\+/g, " ");
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return unescape(pair[1]).replace(/\+/g, " ");
    }
  } 
  return null;
}

function text_article_tracking() {
	ajaxFunction('/dbq_articletrack.php', "id=" + current_entry_id + "&type=T", 'POST');
}
