function createCookie(name,value,days)
{
	if (days)
	{
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires='; expires='+date.toGMTString();
	}
	else var expires='';
	document.cookie=name+'='+value+expires+'; path=/';
}

function readCookie(name)
{
	var nameEQ=name + '=';
	var ca=document.cookie.split(';');
	for(var looper=0; looper < ca.length; looper++)
	{
		var current=ca[looper];
		while (current.charAt(0) == ' ') current=current.substring(1,current.length);
		if (current.indexOf(nameEQ) == 0) return current.substring(nameEQ.length,current.length);
	}
	return null;
}

function changeSource()
{
        // Point the iframe to a selected page
	if (readCookie('expand') > 0)
	{
		if (document.getElementById('chatframe').src == 'about:blank')
		{
			try{
				// Should change the src of the iframe
				document.getElementById('chatframe').src='/supportchat/';
			}catch (exception) {
				// Alternative method
				frames['chatframe'].location.href='/supportchat/';
			}
		}
	}
}

function resize(size)
{
        // Check if the first way to resize will work
	if (document.getElementById && !(document.all))
	{
                // Change the frame, header, and image
		document.getElementById('chatframe').style.height=size;
                if (size > 0)
                {
                        document.getElementById('chatheader').style.height=15;
                        document.getElementById('chatimage').src = 'http://www.icesoft.ca/images/chat_button_arrow_down.gif';
                }
                else
                {
                        document.getElementById('chatheader').style.height=40;
                        document.getElementById('chatimage').src = 'http://www.icesoft.ca/images/chat_button_arrow_right.gif';
                }
	}
        // Otherwise use the second way to resize
	else if (document.all)
	{
                // Change the frame, header, and image
		document.all.chatframe.style.height=size;
                if (size > 0)
                {
                        document.all.chatheader.style.height=15;
                        document.all.chatimage.src = 'http://www.icesoft.ca/images/chat_button_arrow_down.gif';
                }
                else
                {
                        document.all.chatheader.style.height=40;
                        document.all.chatimage.src = 'http://www.icesoft.ca/images/chat_button_arrow_right.gif';
                }
	}
}

function switchSize()
{
	// Get the new height, set it to the CSS style height, and store the cookie
	chatframe = document.getElementById('chatframe');
	frameSize=150-readCookie('expand');
	
	// Perform a valid method to resize the iframe
        resize(frameSize);
	
	// Store the size in a cookie and change the iframe source
	createCookie('expand', frameSize, 0);
        changeSource();
}

function initializeFrame()
{
	chatCookie=readCookie('expand');
	
        // Set the default size to minimized if no cookie is defined
        if (chatCookie == null)
        {
                if (document.getElementById && !(document.all))
                        document.getElementById('chatframe').style.height=0;
                else if (document.all)
                        document.all.chatframe.style.height=0;
		createCookie('expand', 0, 0);
        }
        // Reload the previous cookie and use it's size value
        else
        {
                resize(chatCookie);
                if (chatCookie > 0)
                        changeSource();
        }
}
