//data retreval functions

var siteMode = 1;
if(siteMode == 1){
	var linksurl = "http://www.businessroute.co.uk/";
	var scripturl = "http://www.businessroute.co.uk/includes/remote_calls/ajax_files/";
	var imagesurl = "http://www.businessroute.co.uk/images/";
	var photosurl = "http://www.businessroute.co.uk/photos/";	
}
else{
	var linksurl = "http://localhost/businessroute.co.uk/";
	var scripturl = "http://localhost/businessroute.co.uk/includes/remote_calls/ajax_files/";
	var imagesurl = "http://localhost/businessroute.co.uk/images/";
	var photosurl = "http://localhost/businessroute.co.uk/photos/";	
}

function check_search_profit_loss ( profit_min ) {

	var profitOption = document.getElementById ( "profit_option" );
	var lossOption = document.getElementById ( "loss_option" );
	var theDiv = document.getElementById ( "profit_select_div" );

	if ( lossOption.checked ) {
	
		//load select box with minus values
		var theValue = 2;

	}
	else {
	
		//load select box without minus values
		var theValue = 1;

	}
	
	new Ajax.Updater(theDiv, scripturl + 'load_form_data.php', { method: 'POST', parameters: {value: theValue, profit_min: profit_min},
			onSuccess: function(transport){
				removeLoader("contentLoader");
			}
	});
	
}
	

function load_interesting_businesses() {

	var theDiv = document.getElementById("interested_holder");
	addLoader("interested_holder", "contentLoader");
	
	new Ajax.Updater(theDiv, scripturl + 'get-sellers.php', { method: 'POST', 
			onSuccess: function(transport){
				removeLoader("contentLoader");
			}
	});
	
}

function load_interesting_clients() {

	var theDiv = document.getElementById("interested_holder");
	addLoader("interested_holder", "contentLoader");
	
	new Ajax.Updater(theDiv, scripturl + 'get-buyers.php', { method: 'POST', 
			onSuccess: function(transport){
				removeLoader("contentLoader");
			}
	});
	
}

function addLoader(theDiv, loaderName){
	
	theDiv = document.getElementById(theDiv);
	
	theDiv.style.display = 'block';
	theDiv.innerHTML = "";
	
	tempImage = document.createElement("img");
	tempImage.src = imagesurl + "/icons/loading.gif";
	tempImage.alt = "Content loading, please wait";
	tempImage.id = loaderName;;
	tempImage.name = loaderName;

	theDiv.appendChild(tempImage);
	
}

function removeLoader(loaderName){

	document.removeChild(document.getElementById(loaderName));
	
}

function clear_help_block(div_id, input_id){

	theDiv = document.getElementById(div_id);
	divToRemove = document.getElementById("help_block_" + input_id);
	
	theDiv.className = "form_help_block";
	if(divToRemove && theDiv) theDiv.removeChild(divToRemove);

}

function suggest_usernames(theValue){

	theDiv = document.getElementById("username_suggest_div");
	
	new Ajax.Updater(theDiv, scripturl + 'suggest_usernames.php', { method: 'POST', parameters: {value: theValue},
			onSuccess: function(transport){
				removeLoader("contentLoader");
			}
	});
	
	
}

function check_form_value(div_id, form_id, input_id){
	
	var theDiv = document.getElementById(div_id);
	var theValue = document.getElementById(form_id).value;
	
	theDiv = document.getElementById(div_id);
	if(!document.getElementById("help_block_" + input_id) && theValue != ""){

		var extraval1 = "";
		
		switch(input_id){
			case 3: extraval1 = document.getElementById("signup_type")[document.getElementById("signup_type").selectedIndex].value; break;
			case 4: extraval1 = document.getElementById("password").value; break;
		}

		new Ajax.Request(scripturl + 'check_form_input.php', { method: 'POST', parameters: {value: theValue, inputid: input_id, extra_val1: extraval1},
			onSuccess: function(transport){	
				var json  = eval('(' + transport.responseText + ')');

				var valid = json[0]['valid'];
				var suggest_usernames = json[0]['suggest_usernames'];

				switch(valid){				
					case true: image = "tick"; alt_text = "This input is correct"; break;
					case false: image = "cross"; alt_text = "This input is incorrect"; break;
					default: image = "form-star"; alt_text = ""; break;
				}
				
				theImage = document.getElementById(div_id + "_image");
				theImage.src = imagesurl + "/icons/" + image + ".gif";
				theImage.alt = alt_text;
				
				if(!valid){
					//there were errors oupput them to the relevant div

					tempDiv = document.createElement("div");	
					tempDiv.id = "help_block_" + input_id;
					tempDiv.className = "help_block";

					for(var x = 1; x < json.length; x++){
						tempDiv.innerHTML = tempDiv.innerHTML + "<strong>" + json[x]['error'] + "</strong><br />";
					}

					theDiv.appendChild(tempDiv);
					theDiv.className = "form_help_block_error";
					
				}
				else clear_help_block(div_id, input_id);
	
			}
		});
		
	}
	else if ( theValue == "" ) {
	
		theImage = document.getElementById(div_id + "_image");
		theImage.src = imagesurl + "/icons/form-star.gif";
		theImage.alt = "please fill in this field";
		
	}
	
}

function load_help_block(div_id, input_id){

	if(!document.getElementById("help_block_" + input_id)){

		new Ajax.Request(scripturl + 'loadformhelp.php', { method: 'POST', parameters: {inputid: input_id, divid: div_id},
			onSuccess: function(transport){	
				var json  = eval('(' + transport.responseText + ')');

				help_text = json[0]['text'];
				help_title = json[0]['title'];
				divid2 = json[0]['divid2'];

				if(div_id == divid2){
				
					theDiv = document.getElementById(divid2);
				
					tempDiv = document.createElement("div");
					tempDiv.id = "help_block_" + input_id;
					tempDiv.clasName = "help_block";
					tempDiv.innerHTML = "<strong>" + help_title + "</strong>" + help_text;

					theDiv.appendChild(tempDiv);
					theDiv.className = "form_help_block_active";
					
				}
				
			}
		});
		
	}
	
}

function loadTowns(){
	
	theDiv = document.getElementById("town_holder");
	theSelect = document.getElementById("search_county");
	county_id = theSelect[theSelect.selectedIndex].value;

	if(county_id != ""){
	
		addLoader("town_holder", "contentLoader");
		
		new Ajax.Updater(theDiv, scripturl + 'loadtowns.php', { method: 'POST', parameters: {countyid: county_id},
			onSuccess: function(transport){
				removeLoader("contentLoader");
			}
		});
		
	}
	else theDiv.innerHTML = "";
	
}

function loadCounties(){

	theDiv = document.getElementById("area_holder");
	theSelect = document.getElementById("search_country");
	country_id = theSelect[theSelect.selectedIndex].value;
	
	theDiv.innerHTML = "";
	theTown = document.getElementById("town_holder");
	if(theTown != null) theTown.innerHTML = "";
	
	theArea = document.getElementById("area_holder");
	if(theArea != null) theArea.innerHTML = "";
	
	theCounty = document.getElementById("county_holder");
	if(theCounty != null) theCounty.innerHTML = "";

	if(country_id != ""){
	
		addLoader("area_holder", "contentLoader");

		new Ajax.Updater(theDiv, scripturl + 'loadcounties.php', { method: 'POST', parameters: {countryid: country_id, loadtype: 1},
			onSuccess: function(transport){
				removeLoader("contentLoader");
			}
		});
		
	}
	else{

	}
	
}

function loadCountiesFromArea(){

	theSelect = document.getElementById("search_area");
	areaid = theSelect[theSelect.selectedIndex].value;
	theDiv = document.getElementById("county_holder");
	
	if(areaid != 0){
	
		addLoader("county_holder", "contentLoader");

		new Ajax.Updater(theDiv, scripturl + 'loadcounties.php', { method: 'POST', parameters: {area_id: areaid, loadtype: 2},
			onSuccess: function(transport){
				removeLoader("contentLoader");
			}
		});
		
	}
	else{
		theDiv.innerHTML = "";
		
		theTown = document.getElementById("town_holder");
		if(theTown != null) theTown.innerHTML = "";
		
		theArea = document.getElementById("area_holder");
		if(theArea != null) theArea.innerHTML = "";
		
		theCounty = document.getElementById("county_holder");
		if(theCounty != null) theCounty.innerHTML = "";
	}
	
}

function loadSubCategories(){

	addLoader("sub_category_holder", "contentLoader");
	theDiv = document.getElementById("sub_category_holder");
	theSelect = document.getElementById("search_sector");
	cat_id = theSelect[theSelect.selectedIndex].value;
	
	new Ajax.Updater(theDiv, scripturl + 'loadsubcategories.php', { method: 'POST', parameters: {catid: cat_id},
		onSuccess: function(transport){
			removeLoader("contentLoader");
		}
	});
	
}

function loadFormHelp(help_id, div_id){
	
	addLoader(div_id, "contentLoader");
	theDiv = document.getElementById(div_id);
	
	new Ajax.Updater(theDiv, scripturl + 'loadformhelp.php', { method: 'POST', parameters: {helpid: help_id},
		onSuccess: function(transport){
			removeLoader("contentLoader");
		}
	});
	
}
	
function loadEmoticons(divName){
	theDiv = document.getElementById(divName);
	new Ajax.Updater(theDiv, scripturl + 'loademoticons.php')		
}

function startDraftSaver(){
	new PeriodicalExecuter(saveDraft, 30); 
}

function saveDraft(){

	var messageSubject = document.getElementById("subject").value;
	var messageContent = document.getElementById("post").value;
	var messageTo = document.getElementById("messageto").value;
	var messageref = document.getElementById("messageref").value;

	new Ajax.Request(scripturl + 'savemessagedraft.php', { method: 'POST', parameters: {id: messageref, subject: messageSubject, message: messageContent, to: messageTo},
			onSuccess: function(transport){	
				var json  = eval('(' + transport.responseText + ')');
				
				theCount = json[0]['draftCount'];
				if(parseInt(theCount) > 0){
					theHolder = document.getElementById('dc_holder');
					theHolder.innerHTML = "(" + theCount + ")";
				}

			}
	});
	
}
