function GetCookieData()
{
	//reading and splitting the whole cookie
	var whole_cookie = unescape(document.cookie);
	var each_cookie = whole_cookie.split(";");
	
	for (i = 0; i < each_cookie.length; i++)
	{
	
		if (each_cookie[i].indexOf("mbedirectory") > -1)
		{
			var name_cookie_data = each_cookie[i];
		}
	
	}
	
	if (name_cookie_data == "undefined" || name_cookie_data == null)
		return null;

	var entname = name_cookie_data.split("=");
	var params = entname[1].split("-");

	return params;
}

function EmailTo_Change()
{
	if (dojo.byId('EmailTo').value > 0)
	{
		dojo.byId('EmailToLoading').style.display = "normal";
	    dojo.xhrGet
        ({
            url: "http://centre.mbe.com.au/business/xmlhttp",
            handleAs: "json",
        	content:
        	{
        	    a : 'busent',
        	    businessid : dojo.byId('EmailTo').value
        	},

            timeout: 30000, // Time in milliseconds

            // The LOAD function will be called on a successful response.
            handle: function (response, ioArgs)
            {
        		var result = response;

		        dojo.byId('EmailTo').innerHTML = "<select id=\"EmailTo\" name=\"EmailTo\">" + result + "</select>";
				dojo.byId('EmailToLoading').style.display = "none";
			},

            // The ERROR function will be called in an error case.
            error: function(response, ioArgs)
            {
                alert("An error occurred, please refresh the page and try again");
                return response;
            }
        });
	}
}


function Client_Load()
{
	params = GetCookieData();
	
	var intBusinessID;
	if (params != null)
	{
		if (!(dojo.byId('EmailTo') == null))
		{
			dojo.byId('EmailTo').value = params[1];
		}
		intBusinessID = params[0];
	}
	else
	{
		if (!(dojo.byId('EmailTo') == null))
		{
			dojo.byId('EmailTo').value = 'contact@mbe.com.au';
		}
		intBusinessID = 0;
	}
	if (!(dojo.byId('BusinessID') == null))
	{
		dojo.byId('BusinessID').value = intBusinessID;
	}

	if (dojo.byId('panSelectedStore') != null)
	{
		dojo.byId('panSelectedStore').src = 'http://centre.mbe.com.au/business/viewsummary/BusinessID/' + intBusinessID;
	}
}

dojo.connect(dojo.byId('client_form_name'), 'onload', 'Client_Load');
