// declare cookie variable
var _oActiveForm = readCookie("home_form_quickquote");

// declare quick quote form variables 
var _iFormVisible = 0;
var _iFormDisplayPosition = 0;


if (_oActiveForm)
{
    var i = parseInt(getCookieValue("home_form_quickquote"));
    if (i)
    {
        DisplayQuickQuoteForm("quickquote_form_headercontainer","quickquoteheader_form_open",
                                  "quickquote_form_container","quickquote_form_visible",
                                  "home_form_quickquote");
    }
    else
    {
        HideQuickQuoteForm("quickquote_form_headercontainer","quickquoteheader_form_closed",
                           "quickquote_form_container","quickquote_form_hidden",
                           "home_form_quickquote");   
    }
}

function QuickQuoteFormHandler()
{
    var oDiv = document.getElementById("quickquote_form_header");
    if (oDiv)
    {
        if (oDiv.attachEvent) // is IE
        {
            oDiv.attachEvent("onclick", QuickQuoteFormControl);
            oDiv.attachEvent("onmouseover", QuickQuoteTitleReset); 
            oDiv.attachEvent("onmouseout", QuickQuoteTitleReset); 
        }
        else if (oDiv.addEventListener) // is Mozilla, Firefox, Safari, Opera, etc.
        {
            oDiv.addEventListener("click", QuickQuoteFormControl, false);
            oDiv.addEventListener("mouseover", QuickQuoteTitleReset, false);
            oDiv.addEventListener("mouseout", QuickQuoteTitleReset, false);
        }
    }
}

function QuickQuoteFormControl()
{
    if (!_iFormDisplayPosition)
    {
        if (!_iFormVisible)
        {
            DisplayQuickQuoteForm("quickquote_form_headercontainer","quickquoteheader_form_open",
                                  "quickquote_form_container","quickquote_form_visible",
                                  "home_form_quickquote");
        }
        else
        {
            HideQuickQuoteForm("quickquote_form_headercontainer","quickquoteheader_form_closed",
                               "quickquote_form_container","quickquote_form_hidden",
                               "home_form_quickquote");   
        }            
    }
    else
    {
        if (!_iFormVisible)
        {
            DisplayQuickQuoteForm("quickquote_form_headercontainer","quickquoteheader_form_open",
                                  "quickquote_form_container","quickquote_form_visible",
                                  "home_form_quickquote");
        }
        else
        {
            HideQuickQuoteForm("quickquote_form_headercontainer","quickquoteheader_form_closed",
                               "quickquote_form_container","quickquote_form_hidden",
                               "home_form_quickquote");   
        }            
    }
}
    
function QuickQuoteTitleReset()
{
    var oHeader = document.getElementById("quickquote_form_header");
    
    if (!_iFormVisible)
    {
        oHeader.setAttribute("title", "Click here to open form");
    }
    else
    {
       oHeader.setAttribute("title", "Click here to close form");
    }
}                                   
    
function DisplayQuickQuoteForm(headerId,headerClass,formId,formClass,formCookieName)
{
    if (!_iFormDisplayPosition) // Form in default|inline position
    {
        var oHeaderContainer = document.getElementById(headerId);
        oHeaderContainer.className = headerClass;
            
        var oForm = document.getElementById(formId);
        oForm.className = formClass;
        _iFormVisible = 1;
        
        setCookie(formCookieName,1,"","");
    }
    else  // Form in center page position
    {
        var oForm = document.getElementById("dvQuickQuoteForm");
        oForm.style.visibility = "visible";
        _iFormVisible = 1;
    }
}
    
function HideQuickQuoteForm(headerId,headerClass,formId,formClass,formCookieName)
{
    if (!_iFormDisplayPosition) // Form in default|inline position
    {
        var oHeaderContainer = document.getElementById(headerId);
        oHeaderContainer.className = headerClass;
            
        var oForm = document.getElementById(formId);
        oForm.className = formClass;
        _iFormVisible = 0;
        
        if(getCookieValue(formCookieName) != undefined)
        {
            setCookie(formCookieName,0,"",""); 
        }
    }
    else  // Form in center page position
    {
        var oForm = document.getElementById("dvQuickQuoteForm");
        oForm.style.visibility = "hidden";
        _iFormVisible = 0;
    }
}

function setCookie(cookieName,cookieValue,cookiePath,cookieExpires)
{
    cookieValue = escape(cookieValue);
    if (cookieExpires == "")
    {
        var nowDate = new Date();
        nowDate.setTime(nowDate.getTime()+(1*24*60*60*1000));
        cookieExpires = nowDate.toGMTString();
    }
    if (cookiePath == "")
    {
        cookiePath = "/";
    }
    document.cookie = cookieName + "=" + cookieValue + ";expires=" + cookieExpires + ";path=" + cookiePath + ";";
}

function createCookie(name,value,days)
{
	if (days)
	{
		value = escape(value);
		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 i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}                     
	return null;
}

function getCookieValue(cookieName)
{
    var cookieValue = document.cookie;
    var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
    if (cookieStartsAt == -1)
    {
        cookieStartsAt = cookieValue.indexOf(cookieName + "=");
    }
    if (cookieStartsAt == -1)
    {
        cookieValue = null;
    }
    else
    {
        cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
        var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
        if (cookieEndsAt == -1)
        {
            cookieEndsAt = cookieValue.length;
        }
        cookieValue = unescape(cookieValue.substring(cookieStartsAt,cookieEndsAt));
    }
    return cookieValue;
}