//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PEBBLEPAD URL BASED ON 404 SEARCH CRITERIA

//Function based on http://mattwhite.me/11tmr.nsf/D6Plinks/MWHE-695L9Z
//Edited by T.Teichmann 03/2010

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){	  
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
	
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  
  //get last string before file extension 
  pgStr = strReturn.slice(strReturn.lastIndexOf("/")+1);
  if (pgStr.indexOf(".") == -1) {
	var pg = pgStr;
  } else {
	var pg = pgStr.slice(0, pgStr.indexOf("."));  
  }  
  document.getElementById("requrl").innerHTML = "<b>Requested URL:</b> " + unescape(strReturn);
   //post 404 error to log file
  post404Data('404datalog.asp', unescape(strReturn));
  //find possible matches based on the fragments of the URL
  find404URL(pg, 'reqMatch');
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PEBBLEPAD URL BASED ON 404 SEARCH CRITERIA

var xmlObj = "";
var searchStr = "";
var searchAction = "";

function find404URL(str, action) {
	searchAction = action;
	searchStr = str;
	
	if (searchAction == "reqMatch") {
		if (str == "" || str == null) {
			return "No matches could be found.";
		} else {
			loadXMLObj();
		}
	} else {
		if (str == "" || str == null) {
			document.getElementById("404searchresult").innerHTML = "Please enter a search criteria.";
		} else {
			loadXMLObj();
		}	
	}
	
}

function loadXMLObj() {		
	if (window.XMLHttpRequest) {
        xmlObj = new XMLHttpRequest();
        xmlObj.onreadystatechange = manageReqChange;
        xmlObj.open("GET", "siteindex.xml", true);
        xmlObj.send(null);
	} else if (window.ActiveXObject) {
		xmlObj = new ActiveXObject("Microsoft.XMLDOM");
		xmlObj.onreadystatechange = function () {
			if (xmlObj.readyState == 4) {
				processURLRequest();
			}
		};
 	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
}

function manageReqChange() {
	if (xmlObj.readyState == 4) {
        if (xmlObj.status == 200) {
			processURLRequest();
        } else {
           alert("There was a problem retrieving the XML data:\n" + xmlObj.statusText);
        }
    }
}

function processURLRequest()	{
	var xml = xmlObj.responseXML;
	var nodeCnt = xml.getElementsByTagName("url").length;
	var itemNode = xml.getElementsByTagName("url");
	var i = 0;
	var searchResult = "";
	var subsearchResult = "";
	var resultStr = "";
	var cntr = 0;
	var search_cntr = 0;
	var subsearch_cntr = 0;
	var startTag = "<span style='color:#990000;'>";
	var endTag = "</span>";
	var introA = "Found ";
	var introB = " item(s) matching your search criteria:";
	var introC = " link(s) matching your search criteria:"
	var subintroB = " page(s) containing your search criteria:";
	searchStr = searchStr.toLowerCase();
	
	for (i = 0; i < nodeCnt; i++) {
		urlStr = itemNode[i].getElementsByTagName("loc")[0].childNodes[0].nodeValue;
		descStr = itemNode[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
		descStr = descStr.toLowerCase();
		
		if (urlStr.indexOf (searchStr) != -1) {
			searchResult += "<span style=\"margin-left:10px;\"><a href=\"" + urlStr + "\">" + highlight(urlStr, searchStr, startTag, endTag) + "</a></span><br />";
			cntr ++;
			search_cntr ++;
		}
		
		if (descStr.indexOf (searchStr) != -1) {
			subsearchResult += "<span style=\"margin-left:10px;\"><a href=\"" + urlStr + "\">" + urlStr + "</a></span><br />";
			cntr ++;
			subsearch_cntr ++;
		}		
	}

	if (cntr > 0) {
		if (search_cntr > 0 || subsearch_cntr > 0) {
			if (searchAction == "reqMatch") {
				resultStr += "<br /><b>" + introA + cntr + " link(s) similar to the one you were looking for:" + "</b><br /><br />";
			} else {
				resultStr += "<b>" + introA + cntr + introB + "</b><br /><br />";
			}			
		}
		if (search_cntr > 0) {
			resultStr += introA + search_cntr + introC + "<br />" + searchResult + "<br />";
		}
		if (subsearch_cntr > 0) {
			resultStr += introA + subsearch_cntr + subintroB + "<br />" + subsearchResult + "<br />";
		}
	} else {
		resultStr = "Sorry, nothing found.";
	}
		
	if (searchAction == "reqMatch") {
		if (searchResult == "" || searchResult == null) {
			document.getElementById("reqsuggestions").innerHTML = "";
		} else {
			document.getElementById("reqsuggestions").innerHTML = resultStr;
			document.findPPURL.searchString.value = "";
		}
	} else {
		document.getElementById("404searchresult").innerHTML = resultStr;
		document.findPPURL.searchString.value = "";
	}
	
	findInstallURL(searchStr);
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PEBBLEPAD INSTALL URL BASED ON 404 SEARCH CRITERIA

var xmlInstObj = "";
var searchInstStr = "";

function findInstallURL(uristr) {
	searchInstStr = uristr;
	
	if (uristr == "" || uristr == null) {
		searchInstStr = "No installs could be found.";
	} else {
		loadXMLInstObj();
	}
	
}

function loadXMLInstObj() {
	if (window.XMLHttpRequest) {
        xmlInstObj = new XMLHttpRequest();
        xmlInstObj.onreadystatechange = manageInstallReqChange;
        xmlInstObj.open("GET", "servicedata/xml/pporgs.xml", true);
        xmlInstObj.send(null);
	} else if (window.ActiveXObject) {
		xmlInstObj = new ActiveXObject("Microsoft.XMLDOM");
		xmlInstObj.onreadystatechange = function () {
			if (xmlInstObj.readyState == 4) {
				processInstallURLRequest();
			}
		};
 	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
}

function manageInstallReqChange() {
	if (xmlInstObj.readyState == 4) {
        if (xmlInstObj.status == 200) {
			processInstallURLRequest();
        } else {
           alert("There was a problem retrieving the XML data:\n" + xmlInstObj.statusText);
        }
    }
}

function processInstallURLRequest()	{	
	var xml = xmlInstObj.responseXML;
	var nodeCnt = xml.getElementsByTagName("organisation").length;
	var itemNode = xml.getElementsByTagName("organisation");
	var i = 0;
	var searchResult = "";
	var subsearchResult = "";
	var searchInstallStr = "";
	var cntr = 0;
	var search_cntr = 0;
	var subsearch_cntr = 0;
	var startTag = "<span style='color:#990000;'>";
	var endTag = "</span>";
	var introA = "Found ";
	var introB = " install(s) matching your search criteria:";
	var introC = " link(s) matching your search criteria:"
	var subintroB = " install(s) containing your search criteria:";
	searchInstStr = searchInstStr.toLowerCase();
	
	for (i = 0; i < nodeCnt; i++) {
		insturlStr = itemNode[i].getElementsByTagName("installurl")[0].childNodes[0].nodeValue;
		instnameStr = itemNode[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
		instnameStr = instnameStr.toLowerCase();
		
		if (insturlStr.indexOf (searchInstStr) != -1) {
			searchResult += "<span style=\"margin-left:10px;\"><a href=\"" + insturlStr + "\">" + highlight(insturlStr, searchInstStr, startTag, endTag) + "</a></span><br />";
			cntr ++;
			search_cntr ++;
		}
		
		if (instnameStr.indexOf (searchInstStr) != -1) {
			subsearchResult += "<span style=\"margin-left:10px;\"><a href=\"" + insturlStr + "\">" + insturlStr + "</a></span><br />";
			cntr ++;
			subsearch_cntr ++;
		}		
	}	
	
	if (cntr > 0) {
		if (search_cntr > 0 || subsearch_cntr > 0) {
			searchInstallStr += "<b>" + introA + cntr + introB + "</b><br /><br />";
		}
		if (search_cntr > 0) {
			searchInstallStr += introA + search_cntr + introC + "<br />" + searchResult + "<br />";
		}
		if (subsearch_cntr > 0) {
			searchInstallStr += introA + subsearch_cntr + subintroB + "<br />" + subsearchResult + "<br />";
		}
		document.getElementById("404searchinstallresult").innerHTML = searchInstallStr;
		document.findPPURL.searchString.value = "";
	}
	
	
}



function highlight(string,text,before,after) {
	string = string.toLowerCase();
	text = text.toLowerCase();	
	var strLength = string.length;
	var txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return string;			
	var i = string.toLowerCase().indexOf(text);
	if ((!i) && (text != string.toLowerCase().substring(0,txtLength))) return string;
	if (i == -1) return string;
	var newstr = string.substring(0,i) + before + string.substring(i,i+txtLength) + after;
	if (i+txtLength < strLength)
		newstr += highlight(string.substring(i+txtLength,strLength),text,before,after);
	return newstr;
}
