// Copyright Otto de Voogd 2006-2009 - www.7is7.com/otto
// All rights reserved.
// Original URL: http://www.7is7.com/software/firefox/opensearch.html

// Generate code for an OpenSearch plugin

// Cleanup User input field
function cleanupUserInput(text) {
	text=text.replace(/^ */,"");
	text=text.replace(/ *$/,"");
	text=text.replace(/  */g," ");
	return text;
}

function generatePlugin(input) {

	if(document.getElementById('instructions')) {
		document.getElementById('instructions').style.display='block';
	}

	// Cleanup user input variables
	var vSiteName=cleanupUserInput(input.SiteName.value);
	var vDescription=vSiteName+' Search';
	var vSiteUrl=cleanupUserInput(input.SiteUrl.value);
	var vDomain=vSiteUrl.match(/^[^\/]*:\/\/[^\/]+\.[^\/]+/);
	var vSearchBoxCode=input.SearchBoxCode.value;
	var vInputEncoding=cleanupUserInput(input.InputEncoding.value);
	var vHostUrl=cleanupUserInput(input.HostUrl.value);
	if (vHostUrl.match(/^[^\/]*:\/\/[^\/]+\.[^\/]+/)) {
		if (!vHostUrl.match(/^[^\/]*:\/\/[^\/]+\.[^\/]+$/)) {
			vHostUrl=vHostUrl.replace(/[^\/]*$/,'');
		} else {
			vHostUrl+='/';
		}
	} else {
		vHostUrl=vDomain+'/';
	}
	var vXmlFile=vHostUrl+cleanupUserInput(input.XmlFile.value);
	var vImage=vHostUrl+cleanupUserInput(input.Image.value);


	// Extract data from SearchBoxCode
	var eAction=vSearchBoxCode.match(/form[^>]*action=[^=>]*/)[0].replace(/form.*action=/,"").replace(/ [^ ]*$/,"").replace(/(^['"]|['"]$)/g,'');
	if (eAction.match(/^\//)) {
		eAction=vDomain+eAction;
	}
	var eMethod='get';
	if (vSearchBoxCode.match(/form[^>]*method=/)) {
		eMethod=vSearchBoxCode.match(/form[^>]*method=[^=>]*/)[0].replace(/form.*method=/,"").replace(/ [^ ]*$/,"").replace(/(^['"]|['"]$)/g,'');
	}
	var eTemplate=eAction+'?';
	var eInput=vSearchBoxCode.match(/input[^>]*name=[^>]*/g);
	for (var i=0;i<eInput.length;i++) {
			var name=eInput[i].match(/name=[^=\/>]*/)[0].replace(/name=/,"").replace(/ [^ ]*$/,"").replace(/(^['"]|['"]$)/g,'');
			var value='';
			if (eInput[i].match(/value=/)) {
				value=eInput[i].match(/value=[^=\/>]*/)[0].replace(/value=/,"").replace(/ [^ ]*$/,"").replace(/(^['"]|['"]$)/g,'');
			}
			if (!eInput[i].match(/type=.?(password|checkbox|radio|submit|reset|file|hidden|image|button)/)) { value='{searchTerms}'; }
			eTemplate+=name+'='+value+'&amp;';
	}
	eTemplate+='sourceid=opensearch';
	//alert(eTemplate);

	// Generate the different fields

	input.pluginname.value = vXmlFile;

	// Not implemented (yet)
	var genImage64='';
	if (typeof(vImage64)!="undefined") {
		genImage64 = '<Image width="64" height="64" type="image/png">'+vImage64+'</Image>\n'
	}

	// Create the OpenSearch plugin code
	var plugincode
	= '<?xml version="1.0" encoding="UTF-8"?>\n'
	+ '<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"'
	+ ' xmlns:moz="http://www.mozilla.org/2006/browser/search/">\n'
	+ '<!-- Generated by http://www.7is7.com/software/firefox/opensearch.html -->\n'
	+ '<ShortName>'+vSiteName+'</ShortName>\n'
	+ '<Description>'+vDescription+'</Description>\n'
	+ '<Url type="text/html" method="'+eMethod+'" template="'+eTemplate+'"/>\n'
	+ '<Image width="16" height="16" type="image/x-icon">'+vImage+'</Image>\n'
	+ genImage64
	+ '<InputEncoding>'+vInputEncoding+'</InputEncoding>\n'
	+ '<moz:SearchForm>'+vSiteUrl+'</moz:SearchForm>\n'
	+ '<moz:UpdateUrl>'+vXmlFile+'</moz:UpdateUrl>\n'
	+ '<moz:IconUpdateUrl>'+vImage+'</moz:IconUpdateUrl>\n'
	+ '<moz:UpdateInterval>7</moz:UpdateInterval>\n'
	+ '</OpenSearchDescription>\n';

	input.plugincode.value=plugincode;

	input.pagecode.value
	= "<a href=\"javascript:if(typeof(window.external)=='object'){try{window.external.AddSearchProvider('"+vXmlFile+"');}catch(e){alert('Your%20browser%20does%20not%20support%20OpenSearch%21');}};void(0);\">Install "+vSiteName+" Search Plugin</a>";

	input.headcode.value
	= '<link rel="search" type="application/opensearchdescription+xml" title="'+vSiteName+'" href="'+vXmlFile+'">';


if (
	document.getElementById('oTarget1') &&
	document.getElementById('oTarget1').firstChild
) {
	document.getElementById('oTarget1').firstChild.nodeValue=vXmlFile;
}

alert('The code for the search plugin and search box has been generated.\nFollow the instructions at the bottom of the page.');

}

// TIAF!
