function submitForm(val) {
	var critString = document.getElementById("critString").value;
	critString = trim(critString);

	if (critString.length != 0) {
		if (val == "1") {
			window.location="Results.php?critString=" + critString;
		} else {
			if (document.getElementById("addans").value.length != 0) {
				window.location="AddFeed.php?critString=" + escape(critString) + "&addans=" + document.getElementById("addans").value;
			}
		}
	}
}

function submitNTForm() {
	var critString = document.getElementById("critString").value;
	critString = trim(critString);

	if (critString.length != 0) {
		window.location="Results.php?critString=" + critString;
	}
}

function trim(val) {
	try {
		return val.replace(/^\s+|\s+$/gm,'');
	} catch (e) {
		return val;
	}
}

function addForm() {
	var formElem = document.createElement("form");
	formElem.id = "addFeedForm";
	formElem.setAttribute("method", "post");
	formElem.setAttribute("action", "http://www.feedbase.net/AddFeed.php");
	var divElem1 = document.createElement("div");
	divElem1.setAttribute("style", "text-align: center;");
	divElem1.innerHTML = "Spam/rubbish feeds will <strong>NOT</strong> be approved, so don't waste your time!<br/>Supply a valid feed URL (Must include protocol e.g. http://):<div style='height: 10px;'></div><input type='text' id='critString' class='inputbox' value='' name='critString' /><div style='height: 10px;'></div><img src='Image.php' alt='Captcha'/><br/><input type='text' id='addans' class='inputbox' name='addans' /><div style='height: 10px;'></div>URL of page that contains a <a href='http://en.wikipedia.org/wiki/Backlink'>backlink</a> to http://www.feedbase.net/<br/>The backlink on your page should take this form: - <strong>&lt;a href=\"http://www.feedbase.net/\"&gt;Feedbase&lt;/a&gt;</strong><br/>If the backlink is removed, the feed will be deleted from the database.<br/>The backlink <strong>must</strong> be on the same domain as feed items.<div style='height: 10px;'></div><input type='text' id='backlink' class='inputbox' name='backlink' /><div style='height: 20px;'></div><input type='button' value='Add' class='inputbutton' onclick='submitAddFeedForm()'/>";
	formElem.appendChild(divElem1);
	document.getElementById("formTD").appendChild(formElem);
}

function submitAddFeedForm() {
	if (document.getElementById("critString").value.length == 0) {
		alert("Please supply a feed URL");
	} else if (!isUrl(document.getElementById("critString").value)) {
		alert("The feed URL is invalid");
	} else if (document.getElementById("addans").value.length == 0) {
		alert("Please supply the result of the Captcha sum");
	} else if (document.getElementById("backlink").value.length == 0) {
		alert("Please supply the URL of a page that contains a backlink to http://www.feedbase.net/");
	} else if (!isUrl(document.getElementById("backlink").value)) {
		alert("The backlink URL is invalid");
	} else if (document.getElementById("backlink").value.toLowerCase() == document.getElementById("critString").value.toLowerCase()) {
		alert("The URL of your backlink page can not be the same as your feed URL");
	} else if (document.getElementById("backlink").value.toLowerCase().indexOf("feedbase.net") != -1 || document.getElementById("backlink").value.toLowerCase().indexOf("wizzcomputers.com") != -1 || document.getElementById("backlink").value.toLowerCase().indexOf("wizzrss.com") != -1 || document.getElementById("backlink").value.toLowerCase().indexOf("blat.co.za") != -1) {
		alert("Don't try to be smart!");
	} else {
		document.getElementById("addFeedForm").submit();
	}
}

function isUrl(s) {
	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}