function validateURL() {
  var site = document.getElementById("urlinput").value;
  var RegExp = /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/;
  if(!RegExp.test(site)) {
    var message = "Please enter your URL in one of the following formats:" +
    "<br /><ul><li>siteamount.com</li>" +
    "<li>www.siteamount.com</li>" +
    "<li>http://siteamount.com</li>" +
    "<li>http://www.siteamount.com</li></ul>" +
    "<span class='smalltext'><strong>Note:</strong> Results will vary based on the inclusion of 'www'</span>";
    showDialog('Invalid URL',message)
    return false;
  } else {
    return true;
  }
}

var sources = new Array();
sources[0] = "image";
sources[1] = "title";
sources[2] = "description";
sources[3] = "pagerank";
sources[4] = "alexa";
sources[5] = "compete";
sources[6] = "dmoz";
sources[7] = "yahoo_dir";
sources[8] = "wikipedia";
sources[9] = "google_ind";
sources[10] = "yahoo_ind";
sources[11] = "msn_ind";
sources[12] = "google_back";
sources[13] = "yahoo_back";
sources[14] = "ask_back";
sources[15] = "delicious";
sources[16] = "digg";
sources[17] = "technorati";
sources[18] = "xhtml_errors";
sources[19] = "css_errors";
sources[20] = "rss_errors";
sources[21] = "age";
sources[22] = "ip";
sources[23] = "location";
sources[24] = "speed";

var functionLength;

function processRequest(site) {
  document.getElementById('submitbtn').src = 'http://static.siteamount.com/img/go_wait.gif';
  document.getElementById('submitbtn').disabled = true;
  functionLength = sources.length + 1;
  for (i = 0; i < sources.length; i++) {
    sendRequest(site, "/functions/" + sources[i] + ".php", sources[i])
  }
}

var requestArray = new Array();
var responseArray = new Array();

function sendRequest(site,url,dest) {
  var arrayPos = -1;
  for (var i=0; i < requestArray.length; i++) {
    if (requestArray[i].available == 1) {
      arrayPos = i;
      break;
    }
  }
  if (arrayPos == -1) {
    arrayPos = requestArray.length;
    requestArray[arrayPos] = new newRequest(1);
  }
  if (requestArray[arrayPos].xmlhttp) {
    requestArray[arrayPos].available = 0;
    requestArray[arrayPos].xmlhttp.open("GET",url,true);
    requestArray[arrayPos].xmlhttp.onreadystatechange = function() {
      if (typeof(requestArray[arrayPos]) != 'undefined' && requestArray[arrayPos].available == 0 && requestArray[arrayPos].xmlhttp.readyState == 4) {
        var responseString;
        if (requestArray[arrayPos].xmlhttp.status == 200 || requestArray[arrayPos].xmlhttp.status == 304) {
          var AJAXresponse = requestArray[arrayPos].xmlhttp.responseText;
          functionLength = functionLength - 1;  
          document.getElementById(dest).innerHTML = AJAXresponse.replace(/\\/g,"");
          if (functionLength == 1) {
            processResponse(site);
            document.getElementById('submitbtn').src = 'http://www.siteamount.com/img/go.gif';
            document.getElementById('submitbtn').disabled = false;
          }
        } else {
          document.getElementById(dest).innerHTML = 'Error';
          functionLength = functionLength - 1;
          if (functionLength == 1) {
            processResponse(site);
            document.getElementById('submitbtn').style.visibility = 'visible';
          }
        }
        requestArray[arrayPos].available = 1;
      }
    }
    if (window.XMLHttpRequest) {
      requestArray[arrayPos].xmlhttp.send(null);
    } else if (window.ActiveXObject) {
      requestArray[arrayPos].xmlhttp.send();
    }
  }
}

function newRequest(available) {
  this.available = available;
  this.xmlhttp = false;
  if (window.XMLHttpRequest) {
    this.xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function processResponse(url) {
document.getElementById('submitbtn').innerHTML = '<img id="submitimg" src="http://static.siteamount.com/img/go.gif" width="45" height="28" alt="Analyze URL" />';
// sendRequest(url, "/php/database_entry.php", "site_value")
}

var offsetfromcursorX = -7;
var offsetfromcursorY = 23;
var offsetdivfrompointerX = 13;
var offsetdivfrompointerY = 13;
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var enabletip = false;

document.write('<div id="theToolTip"></div>');
document.write('<img id="ToolTipPointer" src="http://static.siteamount.com/img/tooltip_icon.gif">');

if (ie||ns6) {
  var tipobj = document.all? document.all["theToolTip"] : document.getElementById? document.getElementById("theToolTip") : "";
}

var pointerobj = document.all? document.all["ToolTipPointer"] : document.getElementById? document.getElementById("ToolTipPointer") : "";

function ietruebody() {
  return (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
}

function tooltip(value) {
  tipobj.innerHTML = value;
  enabletip = true;
  return false;
}

function positiontip(e){
  if (enabletip) {
    var nondefaultpos = false;
    var curX = (ns6)? e.pageX : event.clientX + ietruebody().scrollLeft;
    var curY = (ns6)? e.pageY : event.clientY + ietruebody().scrollTop;
    var winwidth = ie && !window.opera? ietruebody().clientWidth : window.innerWidth - 20;
    var winheight = ie && !window.opera? ietruebody().clientHeight : window.innerHeight - 20;
    var rightedge = ie && !window.opera? winwidth - event.clientX - offsetfromcursorX : winwidth - e.clientX - offsetfromcursorX;
    var bottomedge = ie&&!window.opera? winheight - event.clientY - offsetfromcursorY : winheight - e.clientY - offsetfromcursorY;
    var leftedge = (offsetfromcursorX < 0)? offsetfromcursorX*(-1) : -1000;
    if (rightedge < tipobj.offsetWidth){
      tipobj.style.left = curX - tipobj.offsetWidth + "px";
      nondefaultpos = true;
    } else if (curX < leftedge) {
      tipobj.style.left = "5px";
    } else {
      tipobj.style.left = curX + offsetfromcursorX - offsetdivfrompointerX + "px";
      pointerobj.style.left = curX + offsetfromcursorX + "px";
    }
    if (bottomedge < tipobj.offsetHeight) {
      tipobj.style.top = curY - tipobj.offsetHeight - offsetfromcursorY + "px";
      nondefaultpos = true;
    } else {
      tipobj.style.top = curY + offsetfromcursorY + offsetdivfrompointerY + "px";
      pointerobj.style.top = curY + offsetfromcursorY + "px";
    }
    tipobj.style.visibility = "visible";
    if (!nondefaultpos) {
      pointerobj.style.visibility = "visible";
    } else {
      pointerobj.style.visibility = "hidden";
    }
  }
}

function hidetooltip() {
  enabletip = false;
  tipobj.style.visibility = "hidden";
  pointerobj.style.visibility = "hidden";
  tipobj.style.left = "-1000px";
  tipobj.style.backgroundColor = '';
  tipobj.style.width = '';
}

document.onmousemove = positiontip;

// Message Alert Script //

var TIMER = 5;
var SPEED = 10;
var WRAPPER = 'wrapper';

function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function showDialog(title,message) {
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;
    dialog.style.opacity = .00;
    dialog.style.filter = 'alpha(opacity=0)';
    dialog.alpha = 0;
    dialogmask.style.opacity = .75;
    dialogmask.style.filter = 'alpha(opacity=75)';
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  var width = pageWidth();
  var height = pageHeight();
  var left = leftPosition();
  var top = topPosition();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogtitle.innerHTML = title;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';
  dialog.timer = setInterval("fadeDialog(1)", TIMER);
}

function hideDialog() {
  var dialog = document.getElementById('dialog');
  if(!dialog.timer) {
    dialog.timer = setInterval("fadeDialog(0)", TIMER);
  }
}

function fadeDialog(flag) {
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}

function slideContent(t,i){
	var c,l,x,d,s;
	c=t.parentNode.getElementsByTagName('div');
	l=c.length;
	for(x=0;x<l;x++){
		c[x].className='sidelink';
	}
	t.className='sidelink linksel';
	d=document.getElementById('results-'+i);
	s=d.parentNode;
	clearInterval(s.timer);
	s.target=d.offsetTop;
	s.style.top=s.style.top||'0px';
	s.current=s.style.top.replace('px','');
	if(Math.abs(s.current)!=s.target){
		s.section=i;
		s.direction=Math.abs(s.current)>s.target?1:-1;
		s.style.opacity=.4;
		s.style.filter='alpha(opacity=40)';
		s.timer=setInterval(function(){slideAnimate(s)},3);
	}
}

function slideAnimate(s) {
	var v,c,t,d;
	v=30;
	c=Math.abs(s.current);
	t=Math.abs(s.target);
	d=s.direction;
	if((t-c<=v&&d==-1)||(c-t<=v&&d==1)){
		s.style.top=(s.target*-1)+'px';
		s.style.opacity=1;
		s.style.filter='alpha(opacity=100)';
		clearInterval(s.timer);
	}else{
		var pos=(d==1)?parseInt(s.current)+v:s.current-v;
		s.current=pos;
		s.style.top=pos+'px';
	}
}

var curHeight = 0;
var curPos = 0;
var newPos = 0;
var mouseStatus = 'up';

function setPos(e){
  mouseStatus = 'down';
  curPos = e.clientY;
  var tempHeight = document.getElementById('results').offsetHeight;
  curHeight = parseInt(tempHeight);
}

function getPos(e){
  if(mouseStatus == 'down'){
    curevent = typeof event == 'undefined' ? e:event;
    newPos = curevent.clientY;
    var pxMove = parseInt(newPos - curPos);
    var newHeight = parseInt(curHeight + pxMove);
    newHeight = newHeight < 5 ? 5 : newHeight;
    document.getElementById('results').style.height = newHeight + 'px';
  }
}

document.body.addEventListener('mousemove', getPos, false);
document.body.addEventListener('mouseup', function(){mouseStatus='up'}, false);