
var modalPanels = [];
var registrationForm;

function getPanel(id)
{
    var content;
    var title;
    var properties;
    switch (id)
    {
        case "applicationLicense":
            content = document.getElementById("applicationLicense").innerHTML;
            title = "ContractPal Open Public License"
            properties = {"width":"500px"};
            break;
        case "applicationCost":
            content = document.getElementById("applicationCost").innerHTML;
            title = "Cost Title"
            properties = {"width":"500px"};
            break;
        case "whitePaperRegistration":
            content = document.getElementById("whitePaperRegistration").innerHTML;
            title = "Register"
            properties = {"width":"500px"};
            break;
        default:
            alert("something incorrect was passed in.");
    }
    var panel = getModalPanel ("modal1", title, content+"<br/><div style=\"width:100%; text-align:center;\"><input type=\"button\" value=\"Close\" id=\"closeModal1\"/></div>", properties);
    panel.show();
}

function getModalPanel (id, title, content, properties)
{
    var panel;
    // loop thru and see if id is there
    if (modalPanels.length > 0)
    {
        for (var i=0; i<modalPanels.length; i++)
        {
            if (modalPanels[i].id == id)
            {
                panel = modalPanels[i];
                break;
            }
        }
    }

    // if not there - create
    if (panel==null)
    {
        var oPanel = new YAHOO.widget.Panel(id, {
            fixedcenter         : true,
            constraintoviewport : true,
            underlay            : "none",
            close               : true,
            visible             : false,
            draggable           : true,
            modal               : true} );
        panel = oPanel
        modalPanels[modalPanels.length] = panel;
    }

    panel.setHeader("<div class=\"tl\"></div><span>"+title+"</span><div class=\"tr\"></div>");
    if (typeof content == "object")
    {
        panel.setBody(content.getText());
    }
    else if (typeof content == "string")
    {
        panel.setBody(content);
    }
    else if (typeof content == "xml")
    {
        panel.setBody(content.toString());
    }

    panel.setFooter("<div class=\"bl\"></div><span></span><div class=\"br\"></div></div>");
    panel.render();

    // set the properties -- properties allowed: width, height, ...
    for (property in properties)
    {
        panel.cfg.setProperty(property, properties[property]);
    }

    if (document.getElementById("closeModal1"))
    {
        YAHOO.util.Event.addListener("closeModal1","click",hidePanel,panel);
    }
    return panel;
}

function hidePanel(e, obj)
{
    var id = typeof obj=="string"?obj:obj.id;

    for (var i=0; i<modalPanels.length; i++)
    {
        if (modalPanels[i].id == id)
        {
            modalPanels[i].hide();
        }
    }
}

function sendFormWithAction(id, action)
{
    var f = document.getElementById(id);
    f.action = f.action+"?action="+action;
    f.submit();
}

function getWhitePaper(id)
{
    document.getElementById("whitepaperId").value = id;

    //if (getCookie("ok4cpwp") == "true")  {
        document.location="whitepaper.html?id=" + id;
    //} else {
        //var request = new AjaxRequest("getFragment.do", "whitepaperhandler");
        //request.send();
    //}
}

function whitepaperhandler(response) {
    properties = {"width":"500px"};
    
    var panel = getModalPanel ("modal1", "Register", response.getText()+"<br/><div style=\"width:100%; text-align:center;\"><input type=\"button\" value=\"Close\" id=\"closeModal1\"/></div>", properties);
    panel.show();
}

function saveContactInformation(f)
{
    if (!validateForm(f))
    {
        alert("Your form has errors associated with it.\n\nPlease correct the errors and submit again.");
        return;
    }
    else
    {
        f.submit();
    }
}

function saveContactInformationWithCookie(f)
{
    if (!validateForm(f))
    {
        alert("Your form has errors associated with it.\n\nPlease correct the errors and submit again.");
        return;
    }
    else
    {
        var whitepaperId = document.getElementById("whitepaperId").value;
        var webURL = document.getElementById("webURL").value;
        document.getElementById('retURL').value=""+webURL+"whitepaper.html?id="+whitepaperId;
        
        // set cookie
        /*  86400 is the number of seconds in 24 hours.
            If the timeout is changed here it will also 
            need to be changed in the workflow.
        */
        setCookie("ok4cpwp","true",86400,"/","","");
        
        registrationForm = f;
        var request = new AjaxRequest("setRegCookie.do", "submitRegForm");
        request.send();
    }
}

function submitRegForm()
{
    registrationForm.submit();
}


function setCookie( name, value, expires, path, domain, secure )
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );
    
    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24, for
    seconds, delete * 60 * 60 * 24
    */
    if ( expires )
    {
        // expires = expires * 1000 * 60 * 60 * 24;
        expires = expires * 1000;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function launchVideo(vname, vtitle, width, height)
{
    var settings = "width=630,height=550";
    if (width!=null && height!=null)
    {
        settings = "width="+width+",height="+height;
    }

    var mynewwindow = window.open("http://files.contractpal.com/flash/"+vname, vtitle, "resizable=no,toolbar=no,location=no,"+settings);
    mynewwindow.focus();
}

function windowOpen(url, title, params)
{
    var newwindow = window.open(url, title, params);
    newwindow.focus();
}

	function openTab(lyr) {
		document.getElementById(lyr).style.display='block';
		}
	function closeTab(lyr) { 
	/* there has got to be an easier way to do this */
		document.getElementById(lyr).style.display='none';

	}

