// JavaScript Document
function searchText(fnc) {
	var searchForm = document.getElementById("mainSearchForm");
	switch(fnc) {
	case '1':
	if(document.getElementById("mainSearchForm").value == "New Scout Search...") {
	searchForm.style.color = "#99CC99";
	searchForm.value = "";
	document.getElementById("mainSearchAjax").style.display = "none";
	}
	break;
	case '2':
	if(document.getElementById("mainSearchAjax").style.display == "none") {
	searchForm.style.color = "#99CC99";
	searchForm.value = "New Scout Search...";
	document.getElementById("mainSearchAjax").style.display = "none";
	}
	break;
	}
}

function noCache(uri){return uri.concat(/\?/.test(uri)?"&":"?","scoutSearch-AjaxForceRefresh=",(new Date).getTime(),".",Math.random()*1234567)}

function suggestions() {
var curr_sea = document.getElementById("mainSearchForm").value;
document.getElementById("mainSearchHolder").value = curr_sea;
var searchRequest = "";  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		searchRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			searchRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				searchRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser does not support on page refreshing");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	searchRequest.onreadystatechange = function(){
		switch(searchRequest.readyState) {
			case 4:
			if(searchRequest.responseText != "") {
			document.getElementById("mainSearchAjax").style.display = "block";
			document.getElementById("mainSearchAjax").innerHTML = searchRequest.responseText;
			}
			if(searchRequest.responseText == "") {
			document.getElementById("mainSearchAjax").style.display = "none";
			}
			if(document.getElementById("mainSearchForm").value == "") {
			document.getElementById("mainSearchAjax").style.display = "none";
			}
			break;
		}
		
	}
	searchRequest.open("GET", noCache("_server/ajax-SearchSuggestions.php?terms="+curr_sea), true);
	searchRequest.send(null); 
}

function ajax(page, divid){
	var ajaxRequest = "";  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser does not support on page refreshing");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		switch(ajaxRequest.readyState) {
			case 4:
			document.getElementById(divid).innerHTML = ajaxRequest.responseText;
			break;
		}
		
	}
	ajaxRequest.open("GET", noCache(page), true);
	ajaxRequest.send(null); 
}

function scoutSearch() {
var keywords = document.getElementById("mainSearchForm").value;
if(keywords == "New Scout Search...") {
alert("You must type in a valid search term(s)")	
}else{
document.getElementById("mainSearchForm").value = keywords;
alert("Once this functionality is complete, this will redirect you to:\n\n http://www.scoutsearch.org.uk/Search-Results/"+keywords+"/")
}
}

function fillSearch(formText) {
	window.location=noCache("http://www.scoutsearch.org.uk/RedirEngine.php?vxns="+formText);
}

function submitSite(step_id) {
	var url_format = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	var new_site = document.getElementById("submitSiteForm").value;
	if(url_format.test(new_site)) {
	document.getElementById("submitSiteUrlMem").value = new_site;
	document.getElementById("submitUrlAjax").innerHTML = "<p style='text-align:center'><img src='_assets/dynamicScripting/scoutSearchLoading.gif' /><p style='font-size: 20px; margin: 20px; text-align: center; color: #99CC99'>Verifying Site '"+ new_site + "'</p>";
		//Ajax Steps Here
		var verifySiteAjax = "";  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		verifySiteAjax = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			verifySiteAjax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				verifySiteAjax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser does not support on page refreshing");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	verifySiteAjax.onreadystatechange = function(){
		switch(verifySiteAjax.readyState) {
			case 4:
			if(verifySiteAjax.responseText == "NOT-VALID") {
			window.location="http://www.scoutsearch.org.uk/New-Addition-Reject/Summary/";
			}
			if(verifySiteAjax.responseText == "VALID") {
				document.getElementById("submitUrlAjax").innerHTML = "<p style='text-align:center'><img src='_assets/dynamicScripting/scoutSearchLoading.gif' /><p style='font-size: 20px; margin: 20px; text-align: center; color: #99CC99'>Site successfully verified<br />Indexing '"+ new_site + "'</p>";	
				ajax("_server/ajax-SubmitIndex.php?isJs=true","submitUrlAjax");
			}
			break;
		}
		
	}
	verifySiteAjax.open("GET", noCache("_server/ajax-SubmitVerify.php?isJs=true&addrToSubmit="+new_site), true);
	verifySiteAjax.send(null);
	
		//End Ajax Steps
	}else{
	alert("Invalid URL Entered")	
	}
}

function cancelSubmission() {
if(confirm("Are you sure you want to cancel this submission?")) {
window.location="http://www.scoutsearch.org.uk/Search/Start-Search/";	
}
}

function sendSubmission() {
var sTitle = escape(document.getElementById('sTitle').value);
var SiteKW = escape(document.getElementById('SiteKW').value);
var SiteDes = escape(document.getElementById('SiteDes').value);
var website_content = escape(document.getElementById('website_content').value);
var SiteID = escape(document.getElementById('SiteID').value);
var cofo = escape(document.getElementById('cofo').value);
var scat = escape(document.getElementById('scat').value);

document.getElementById("submitUrlAjax").innerHTML = "<p style='text-align:center'><img src='_assets/dynamicScripting/scoutSearchLoading.gif' /><p style='font-size: 20px; margin: 20px; text-align: center; color: #99CC99'>Finalising Site Submission</p>";
var searchSubmission = "";  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		searchSubmission = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			searchSubmission = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				searchSubmission = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser does not support on page refreshing");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	searchSubmission.onreadystatechange = function(){
		switch(searchSubmission.readyState) {
			case 4:
			document.getElementById("submitUrlAjax").innerHTML = searchSubmission.responseText;
			break;
		}
		
	}
	var parameters="sTitle="+sTitle.replace("&","&amp;")+"&SiteKW="+SiteKW.replace("&","&amp;")+"&SiteDes="+SiteDes.replace("&","&amp;")+"&SiteID="+SiteID.replace("&","&amp;")+"&website_content="+website_content.replace("&","&amp;")+"&cofo="+cofo+"&scat="+scat+"&isJs=true";

	searchSubmission.open("POST", noCache("_server/ajax-SubmitSite.php"), true);
	searchSubmission.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	searchSubmission.setRequestHeader("Content-length","parameters.length");
	searchSubmission.setRequestHeader("Connection","close");
	searchSubmission.send(parameters); 
}

