//script for finding broken links
var _broken = new Array();
var _page_name = document.location.href;
var _url = _page_name.split('/');
var _realurl='http://' + _url[2];
var a=0;
var req;
var _asset = !_asset_id ? 'no asset id found' : _asset_id;
var _user = !_user_id ? 'no user found' : _user_id;
/*for(a=1; a< 3; a++)
{
	_realurl+=_url[a] + '/';
}*/

_realurl+='/apps/broken/email/';
//alert(_realurl);
function search_links()
{
	//grab ref to body
	//alert(_realurl);
	var _body = document.getElementsByTagName('body')[0];
	var _a = _body.getElementsByTagName('a');
	if(_a.length > 0)
	{
		//alert(_a.length);
		var i=0;
		var _count=0;
		var _regX = new RegExp(_page_name,"ig");
		for(i=0; i<_a.length; i++)
		{
			var ref = _a[i].href;
			var aref = ref.replace(_regX,'');
			//alert(aref);
			if(aref.substr(0,3) == '?a=')
			{
				_broken[_count] = escape(aref);
				_count++;
			}
		}
		if(_broken.length > 0)
		{
			report_links();
		}	
	}
}
function report_links()
{
	
	//make the url to do the ajax call to
	_page_name = escape(_page_name);
	_realurl +='?input=' + _page_name + '~' + _broken.join('~') + '~' +  _asset + '~' +  _user;
	//alert(_realurl);
	quickRequest(_realurl,'GET',_do,_do);
}
function _do()
{
	//we dont really want to do anything
}

/**
	  *used to call the server side code
	  *_url to call
	  *_meth defaults to GET
	  *_do the function to use on good return
	  *_dam the function to catch bad results
	  *Uses the dealwithit function to make the request
	  */
	function quickRequest(_url,_meth,_do,_dam)
	{
		//alert('quickRequest called with url ' + __url);
		var meth = _meth;
		// branch for native XMLHttpRequest object
		//alert(_url);
		if (window.XMLHttpRequest)
		{
			req = new XMLHttpRequest();
			req.onreadystatechange = function () { dealWithIt(_do,_dam) };
			req.open(meth, _url, true); //
			req.send(null);
		// branch for IE/Windows ActiveX version
		}
		else if (window.ActiveXObject)
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req)
			{
				req.onreadystatechange = function () { dealWithIt(_do,_dam) };
				req.open(meth, _url, true);
				req.send();
			}
		}
	}
	
	/**
	//used by request to catch a response
	//_do is the function to pass the response to
	//_dam is the error trap
	//req is a global var(or should be)
	//ie is global
	*/
	function dealWithIt(_do,_dam)
	{
		//alert('deal with it called with value ' + req.readyState);
		if (req.readyState == 4) 
		{
			//alert('deal with it called with value ' + req.status);
			if (req.status == 200)
			{
	
				var jsondata = eval('(' + req.responseText + ')');
				_do(jsondata);
			} 
			else 
			{
				var info="Ajax Error : " + req.status;
				_dam(info);
			}
		}
	}
setTimeout(search_links,2000);