function loginFormSubmit() {
	$.post("account/process.php?request=login",$("#user-login-form").serialize(),function(data) {   
		if (data=="OK") {
			// Login Successful
			window.location = "account/";
		} else if (data == "OKP") {
			$.get("account/puserprocess.php?request=L",function(data) {
				$("#loginBoxContent").html(data);
				$("#userLoginTitle").html("Welcome");
			});
		} else {
			// Error
			$("#errorBox").html(data);
		}
	});
	return false;
}

function requestAndReload(page,reloadLeft,reloadRight) {
	$.get(page,function(data) {
		var leftData = "";
		var rightData = "";
		if (reloadLeft) {
			$.get("process.php?request=L",function(data) {
				$("#leftContent").html(data);
			});
		}
		if (reloadRight) {
			$.get("process.php?request=R",function(data) {
				$("#rightContent").html(data);
			});
		}
	});

}

function requestAndReloadWithContents(page) {
	$.get(page,function(data) {
		$("#rightContent").html(data);
	});
}

function postAndReloadWithContents(page,postdata) {
	$.post(page,postdata,function(data) {
		$("#rightContent").html(data);
	});
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function checkPasswordsSame()
{
	if($("#pass1").val() != $("#pass2").val())
	{
		alert('Your passwords do not match. Please re-enter them.');
		return false;
	}
	return true;
}
function countySelected()
{
	$.get('process.php?request=findarea&county='+$('#countyselector').val(), function(data) {
		if (data != "none")
		{
			$("#areatd").html(data);
			$("#mainform").hide();
			$("#repselect").hide();
		}
	});
}

function countySelectedReg()
{
	$.get('process.php?request=findarea&nojs=1&county='+$('#countyselector').val(), function(data) {
		if (data != "none")
		{
			$("#areatd").html(data);
			$("#mainform").hide();
			$("#repselect").hide();
		}
	});
}

function areaSelected()
{
	if (!rf)
	{
		$.get('process.php?request=findrep&area='+$('#areaselector').val(), function(data) {
			if (data != "none")
			{
				$("#repselect").show();
				$("#mainform").show();
				$("#reptd").html(data);
			}
			else
			{
				alert('Sorry, there are no registered representatives for this area. You can not submit a query online.');
				$("#mainform").hide();
				$("#repselect").hide();
			}
		});
	}   
	else
	{   
		if ($("#areaselectlist").val()!="---") {
			$("#mainform").show();
		}
		else {
			$("#mainform").hide();
		}
	}
}

function updateProfilePic(filename)
{
	if (filename)
	{
		profilepic = filename;
		$("#uploadprofilepic").html('<img src="uploads/'+filename+'" />');
		$("#profilepicfield").val(filename);
	}
}

function afChanged()
{
    $('#afArea').empty();
    $.getJSON('process.php?request=towns&p1=' + $('#afCounty').val(), function (data)
    {
		var col = 0;
        $.each(data, function (i, item)
        {
			$('#afArea').append($("<option></option>").attr("value",item.area_id).text(item.area_name));
        });
    });
    
}

function rebuildAreaList()
{
	var arealist = "";
	$('#selectedAreas option').each(function(i, selected){
		arealist += $(selected).val()+",";
	});	
	$('#arealist').val(arealist);
}

function addSelectedAreas()
{
	$('#afArea :selected').each(function(i, selected){ 
		if (!$("#selectedAreas option[value='"+$(selected).val()+"']").length)
		{
			$('#selectedAreas').append($("<option></option>").attr("value",$(selected).val()).text($(selected).text()));
		}
	});	
	rebuildAreaList();
}

function addAllAreas()
{
	$('#afArea option').each(function(i, selected){ 
		if (!$("#selectedAreas option[value='"+$(selected).val()+"']").length)
		{
			$('#selectedAreas').append($("<option></option>").attr("value",$(selected).val()).text($(selected).text()));
		}
	});	
	rebuildAreaList();
}

function removeSelectedAreas()
{
	$('#selectedAreas :selected').each(function(i, selected){ 
		$("#selectedAreas option[value='"+$(selected).val()+"']").remove();
	});	
	rebuildAreaList();
}



