$(document).ready(initPage);

function initPage()
{
	toggleFieldsByMemberType();
	toggleServiceProviderOther();
	toggleRegionOther();
	toggleStreetStateOther();
	togglePostalStateOther();
	initialiseAddress();
}

$(function()
{
    /*
    * Save (create or update) member
    */
    $("#submitMember").click(function()
    {
        if ($("#sameAsAddress").attr("checked"))
        {
    		// copy field content from street address to ensure we have the latest data
    		$('#member\\.postalAddress\\.address1').val($('#member\\.streetAddress\\.address1').val());
    		$('#member\\.postalAddress\\.suburb').val($('#member\\.streetAddress\\.suburb').val());
    		$('#member\\.postalAddress\\.postcode').val($('#member\\.streetAddress\\.postcode').val());
    		$('#member\\.postalAddress\\.state').val($('#member\\.streetAddress\\.state').val());
    		$('#member\\.postalAddress\\.stateOther').val($('#member\\.streetAddress\\.stateOther').val());
    		$('#member\\.postalAddress\\.country').val($('#member\\.streetAddress\\.country').val());

			// enable postal fields so it will send
			$('#member\\.postalAddress\\.address1').removeAttr("disabled");
			$('#member\\.postalAddress\\.suburb').removeAttr("disabled");
			$('#member\\.postalAddress\\.postcode').removeAttr("disabled");
			$('#member\\.postalAddress\\.state').removeAttr("disabled");
    		$('#member\\.postalAddress\\.stateOther').removeAttr("disabled");
    		$('#member\\.postalAddress\\.country').removeAttr("disabled");
        }

        $("#entityForm").submit();
        
        return false;
    });
    
    
    /*
     * change membershipType selection
     */
    $(".membershipType").click(function()
    {
    	toggleFieldsByMemberType();
    });
    
    /*
     * change technologyType selection
     */
    $("#member\\.technologyType").change(function()
    {
    	toggleTechnologyTypeOther();
    });
    
    /*
     * change industry selection
     */
    $("#member\\.industries").change(function()
    {
    	toggleIndustryOther();
    });

    /*
     * change service provider selection
     */
    $("#member\\.serviceProviders").change(function()
    {
    	toggleServiceProviderOther();
    });
    
    /*
     * check international box
     */
    $("#international").click(function()
    {
    	toggleRegionOther();
    });
    
    /*
     * change state in street selection
     */
    $("#member\\.streetAddress\\.country").change(function()
    {
    	toggleStreetStateOther();
    });
    
    /*
     * change state in postal selection
     */
    $("#member\\.postalAddress\\.country").change(function()
    {
    	togglePostalStateOther();
    });
    
    /*
     * same as above for postal address is clicked
     */
    $("#sameAsAddress").click(function()
	{
		togglePostalAddress();
	});
    
});

function toggleFieldsByMemberType()
{
	var networkSelected = false;
	var supportingSelected = false;
	var optionSelected = $(".membershipType:checked").val();
	if (optionSelected == undefined)
	{
		optionSelected = $("#ignoredMembershipTypeId").val();
		if (optionSelected == undefined)
		{
			optionSelected = '';
		}
	}
	
	if (optionSelected == member_type_network_id)
	{
		networkSelected = true;
	}
	else if (optionSelected == member_type_support_id)
	{
		supportingSelected = true;
	}

    if (networkSelected)
    {
        $('#accreditationNumberDiv').show();
    }
    else
    {
        $('#accreditationNumberDiv').hide();
    }
}

function toggleServiceProviderOther()
{
	var otherSelected = false;
	$('#member\\.serviceProviders option:selected').each(function(i)
	{
		// TODO should change this to use the option value (reference data id)
		if ($(this).text() == "Other")
		{
			otherSelected = true;
		}
	});  
	
    if (otherSelected)
    {
        $('#otherServiceProviderDiv').show();
    }
    else
    {
        $('#otherServiceProviderDiv').hide();
    }
}

function toggleRegionOther()
{
	if ($("#international").attr("checked"))
	{
        $('#otherRegionDiv').show();
	}
	else
	{
        $('#otherRegionDiv').hide();
        $('#member\\.otherRegion').val('');
	}
}

function toggleStreetStateOther()
{
	var australiaSelected = false;
	if ($("#member\\.streetAddress\\.country option:selected").text() == "Australia")
	{
		australiaSelected = true;
	}

    if (australiaSelected)
    {
        $('#streetStateDiv').show();
        $('#streetOtherStateDiv').hide();
        $("#member\\.streetAddress\\.stateOther").val('');
    }
    else
    {
        $('#streetStateDiv').hide();
        $("#member\\.streetAddress\\.state").val('');
        $('#streetOtherStateDiv').show();
    }
}

function togglePostalStateOther()
{
	var australiaSelected = false;
	if ($("#member\\.postalAddress\\.country option:selected").text() == "Australia")
	{
		australiaSelected = true;
	}

    if (australiaSelected)
    {
        $('#postalStateDiv').show();
        $('#postalOtherStateDiv').hide();
        $("#member\\.postalAddress\\.stateOther").val('');
    }
    else
    {
        $('#postalStateDiv').hide();
        $("#member\\.postalAddress\\.state").val('');
        $('#postalOtherStateDiv').show();
    }
}

function initialiseAddress()
{
	var sAddress1 = $('#member\\.streetAddress\\.address1').val();
	var sSuburb = $('#member\\.streetAddress\\.suburb').val();
	var sPostcode = $('#member\\.streetAddress\\.postcode').val();
	var sState = $('#member\\.streetAddress\\.state').val();
	var sStateOther = $('#member\\.streetAddress\\.stateOther').val();
	var sCountry = $('#member\\.streetAddress\\.country').val();
	
	var pAddress1 = $('#member\\.postalAddress\\.address1').val();
	var pSuburb = $('#member\\.postalAddress\\.suburb').val();
	var pPostcode = $('#member\\.postalAddress\\.postcode').val();
	var pState = $('#member\\.postalAddress\\.state').val();
	var pStateOther = $('#member\\.postalAddress\\.stateOther').val();
	var pCountry = $('#member\\.postalAddress\\.country').val();

	if (sAddress1 != '' && sSuburb != '' && sPostcode != '' && (sState != '' || pStateOther != '') && sCountry != '' && 
			sAddress1 == pAddress1 && sSuburb == pSuburb && sPostcode == pPostcode && sState == pState && sStateOther == pStateOther && sCountry == pCountry)
	{
		$("#sameAsAddress").attr("checked", "checked");
		togglePostalAddress();
	}
}

function togglePostalAddress()
{
	if ($("#sameAsAddress").attr("checked"))
	{
		// copy field content from street address
		$('#member\\.postalAddress\\.address1').val($('#member\\.streetAddress\\.address1').val());
		$('#member\\.postalAddress\\.suburb').val($('#member\\.streetAddress\\.suburb').val());
		$('#member\\.postalAddress\\.postcode').val($('#member\\.streetAddress\\.postcode').val());
		$('#member\\.postalAddress\\.state').val($('#member\\.streetAddress\\.state').val());
		$('#member\\.postalAddress\\.stateOther').val($('#member\\.streetAddress\\.stateOther').val());
		$('#member\\.postalAddress\\.country').val($('#member\\.streetAddress\\.country').val());
		
		// disable fields
		$('#member\\.postalAddress\\.address1').attr("disabled", true);
		$('#member\\.postalAddress\\.suburb').attr("disabled", true);
		$('#member\\.postalAddress\\.postcode').attr("disabled", true);
		$('#member\\.postalAddress\\.state').attr("disabled", true);
		$('#member\\.postalAddress\\.stateOther').attr("disabled", true);
		$('#member\\.postalAddress\\.country').attr("disabled", true);
	}
	else
	{
		// enable fields
		$('#member\\.postalAddress\\.address1').removeAttr("disabled");
		$('#member\\.postalAddress\\.suburb').removeAttr("disabled");
		$('#member\\.postalAddress\\.postcode').removeAttr("disabled");
		$('#member\\.postalAddress\\.state').removeAttr("disabled");
		$('#member\\.postalAddress\\.stateOther').removeAttr("disabled");
		$('#member\\.postalAddress\\.country').removeAttr("disabled");
	}
	
	// ensure the postal state other is checked each time the postal address is checked
	togglePostalStateOther();
}

