function drop_down_list_prov()
{
    var prov = $('#province').val();
	//alert(prov);
	if (prov==="nocities")
	{
		$('#loading_all').hide(); // Hide the Loading...
		$('#city_drop_down').hide(); // Hide the drop down
		$('#cat_list').hide(); // Hide the categories
		$('#buttons').hide();
		$('#loading_categories').hide();
		$('#sidebar_msg').show(); // Show the Sidebar Message
	}
	else
	{
		$('#sidebar_msg').hide(); // Hide the Sidebar Message
		$('#city_drop_down').hide(); // Hide the drop down
		$('#loading_categories').hide(); // show the Loading...
		$('#cat_list').hide(); // Hide the Categories List
		$('#buttons').hide();
		$('#loading_all').show(); // Show the Loading...

		//alert("getScript\nprov: "+site_url+"js/cities/"+ prov.toLowerCase() +".js");
		$.getScript("/js/cities/"+ prov.toLowerCase() +".js", function(){

			//alert("populate start");
			populate(document.search_form.city);
			//alert("populate done");
			truncate_cities();
			//var prov=$('#province').val();
			//alert(prov);

			/* **********************************************
			 *
			 * JUST FOR ONTARIO FOR TESTING
			 *
			 *	REMOVE THIS CODE AFTERWARDS!!!! OR CITY WILL DEFAULT TO VALLEY EAST!
			 *
			 * *********************************************
			if (prov.toLowerCase()=='on')
			{
				document.search_form.city[776].selected=true;
			}
			********************************************/

			// the city is set to default 'nocities'=0 
			// but in the case of the upper few lines the categories will be from the VALLEY EAST... Cool eh?!!!
			var city = $('#city').val().toLowerCase();
			$.get("/scripts/sidebar_categories.php?c="+city+"&p="+prov, function(data){

				$("#cat_list").html(data);
				$('#loading_all').hide(); // Hide the Loading...
				$('#city_drop_down').show(); // Show the drop down
				$('#cat_list').show(); // Show the categories
				if ( data.search(/display_button=no/i)>0 )
				{
					//alert("Do Not Display Button");
					// Don't display the button
				}
				else {
					$('#buttons').show();
				}
			});

		});

	}
}

function drop_down_list_city()
{
    var prov = $('#province').val();
	var city = $('#city').val();

	$('#cat_list').hide(); // Hide the categories
	$('#buttons').hide();
	$('#loading_categories').show();
	//alert(prov);
	$.get("/scripts/sidebar_categories.php?c="+city+"&p="+prov, function(data){

		$("#cat_list").html(data);
		$('#loading_categories').hide();
		$('#cat_list').show(); // Show the categories
		if ( data.search(/display_button=no/i)>0 )
		{
			//alert("Do Not Display Button");
			// Don't display the button
		}
		else {
			$('#buttons').show();
		}
	});
}

function truncate_cities(form)
{
	var ln=16;
	var cities=document.getElementById("city");
	for (i=0;i<cities.length;i++) {
		if (cities.options[i].text.length>ln) {
			cities.options[i].text=cities.options[i].text.substr(0, ln)+"...";
		}
	}
}

function cbsel(item)
{
    fe = $('#checkbox_categories');

    if (fe.checked) { fe.checked=false; }
    else { fe.checked = true; }
	select_one_checkbox(document.search_form,item);
}


function select_one_checkbox(form,c) {
	var ln=form.checkbox_categories.length;
	var cvalue;
	with(form) {
		if (ln>1) {
			for (var i=0; i<ln; i++)
			{
				if (checkbox_categories[i].value==c) {
					checkbox_categories[i].checked=true;
					cvalue=checkbox_categories[i].value;
				}
				else {
					checkbox_categories[i].checked=false;
				}
			}
		}
		else {
			checkbox_categories.checked=true;
			cvalue=checkbox_categories.value;	
		} // end if (Ln>1)
	} // end with
	inline_results(form);
}


function submit_form(form) {
	form.submit();
}

function login_submit() {
	
	 var login_options = { 
		url:        base_url+'main/ajaxlogin', 
		type:		'post',
		dataType:	'json',
		success:       login_showResponse,  // post-submit callback
		beforeSubmit:	disable_btn('#login_button'),
		resetForm: true,
		timeout:	3000
    }; 
	$("#login_form").ajaxSubmit(login_options);
}

/* ***********************************************
 *
 *	Submit forms
 *
 *  page = string of url page to call
 *  btn	 = string of button id
 *  fromid = string of the form id
 *
 *  http://malsup.com/jquery/form/#options-object
 *
 *****************************************************/
function form_submit(page, btn, formid) {
	var display_panel='#content_panel';
	if (formid.search(/admin_search_form/)>0)
	{
		display_panel='#search_panel';
	}

	var options = { 
		url: base_url+page, 
		type: 'post',
		dataType: 'json',
		success: function (data) {
						//alert("ok... now showResponce");
						//alert("Display ...: "+display_panel);
						$(display_panel).html(data.content);
						$('#loading_content').hide();
						$(display_panel).show();
						//showResponse(data, statusText, display_panel);  // post-submit callback
						enable_btn(btn);
					},
		beforeSubmit: function() {
						//alert("beforeSubmit");
						$(display_panel).hide();
						$('#loading_content').show();
						disable_btn(btn);
					},
		timeout:	3000
    }; 
	$(formid).ajaxSubmit(options);
}

function disable_btn(btn)
{
	$(btn).attr("disabled", "disabled"); 
}

function enable_btn(btn)
{
	$(btn).removeAttr("disabled");
}

function login_doBefore()
{
	$("#login_button").attr("disabled", "disabled"); 
}
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString+'\n\nUrl: \n'+options["url"]); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
//	return false;
} 
 
// post-submit callback 
function login_showResponse(data, statusText)  { 
	if (data.header=='success')
	{
		$('#login_message_text').html(data.content);
		$('#login_results').html(data.results);
		$('#login_results').show();
		$('#login').hide();
		$('#logout').show();
		$('#login_message_button').show();
		$('#login_message').show();
		$('#tabs').tabs('select', 2);
		// set the deafult page to profile
		process('main/ajaxprofile', false);
	}
	else if (data.header=='fail')
	{
		$("#login_button").removeAttr("disabled");
		$('#login_results').html(data.content);
		$('#login_results').show();
		setTimeout("$('#login_results').hide();",3500);
	}
	//alert('status: ' + data.header + '\n\ndata: \n' + data.content); 
} 

// post-submit callback 
function showResponse(data, statusText, display_panel)  { 
	//alert('status: ' + data.header + '\n\ndata: \n' + data.content); 
	//if (data.header=='success') {
		//alert("Display ...: "+display_panel);
		$(display_panel).html(data.content);
		$('#loading_content').hide();
		$(display_panel).show();
} 

function setTab(tabid) {
	//alert("Setting tab to id:"+tabid);
	$('#tabs').tabs('select', tabid);
}

function logout() {
	var url=base_url+'main/ajaxlogout';
	$.post(	url,
			{ action: 'logout' },
			function(data)	{
				if (data.header=='success')
				{
					//alert 
					enable_btn("#login_button");
					$("#login_results").html(data.content);
					$("#login_results").show();
					setTimeout("$('#login_results').hide();",3500);
					$("#login_message").hide();
					$('#logout').hide();
					$('#login').show();
					// load the content panel
					load_content('main/welcome');
				}
			}, 
			"json"
	);
	return false;
}

function process(ajax, id, tabid) {
	$('#search_panel').hide();
	$('#content_panel').hide();
	$('#loading_content').show();
	var url=base_url+ajax;
	//alert("Business arg: "+id);
	$.post(url, { bid: id }, 
		function(pdata){
			if (pdata.header=='success')
			{
				//$("#content_panel").html('');
				$('#content_panel').html(pdata.content);
				$('#loading_content').hide();
				$('#content_panel').show();
				if(tabid!=null) { setTab(tabid); }
				/**
					NOTE: for some reason, when creating the RECAPTCHA 
					javascript within the page 'email_form.php' IT DOES NOT WORK in IE, but works in Firefox.
					So here we create the object when the contact us page is called
				**/
				/*
				if(ajax.search(/ajax_show_emailfrm/)>0) {
					setTimeout("create_recaptcha();", 100);
				}
				*/
				//alert ("data:\n"+pdata.content);
			}
			else {
				alert ("Javascript process call was NOT sucessfull\n"+pdata);
			}
		}, 
		"json"
	);
	return false;
}

function display_search() {
	$('#content_panel').hide();
	$('#search_panel').show();
	$('#lookup').focus();
}


function load_content_html(page) {
	var url=base_url+page;
	$('#content_panel').hide();
	$('#loading_content').show();
	$('#content_panel').load(	url, 
								{},
								function() {
									$('#loading_content').hide();
									$('#content_panel').show();
								}
		
							);
}

function load_content(page) {
	$('#content_panel').hide();
	$('#loading_content').show();
	var url=base_url+'main/load_view';
	//alert(url);
	$.post(url, { page: page }, 
		function(pdata){
			if (pdata.header=='success')
			{
				//$("#content_panel").html('');
				$("#content_panel").html(pdata.content);
				$('#loading_content').hide();
				$('#content_panel').show();
				//alert ("data:\n"+pdata.content);
			}
			else {
				alert ("data is not SUCCESSFULn\n"+pdata);
			}
		}, 
		"json"
	);
	return false;
}

$(document).ready(function(){
	$("#province").change(drop_down_list_prov);
	$("#city").change(drop_down_list_city);
});





