function setClickedColour (currentClassName,originalClassName,clickedClassName)
{
    if (currentClassName == clickedClassName)
    {
        return originalClassName;
    }
    else
    {
        return clickedClassName;
    }
}


function setHoverColour (currentClassName,originalClassName,clickedClassName,hoverClassName)
{
    if (currentClassName == clickedClassName)
    {
        return clickedClassName;
    }
    else
    {
        return hoverClassName;
    }
}
function setExitColour (currentClassName,originalClassName,clickedClassName)
{
    if (currentClassName == clickedClassName)
    {
        return clickedClassName;
    }
    else
    {
        return originalClassName;
    }
}

function findPosition( cell) {
  if( typeof( cell.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; cell; cell= cell.offsetParent ) {
      posX += cell.offsetLeft;
      posY += cell.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ cell.x, cell.y ];
  }
}
function popupPosition(cellId){
	//request data for centering
	var pos = findPosition(cellId);
	$('#popup').corner();
	$("#popup").css({
		"position": "absolute",
		"top": pos[1],
		"left": pos[0],
		"filter":"alpha(opacity=80)"
});
}


// initialize cache array
var urlCache = new Array();

function buildQueryString()
{
    // init some variables
    var queryString = "";

    // loop through the form elements
    for (var i = 0; i < document.formats_form.elements.length; i++)
    {
	// current form element
	var thisElement = document.formats_form.elements[i];

	// if this is an edition checkbox, append to editions string if checked
	if ((thisElement.name == "editions") && (thisElement.checked == true))
	{
	    queryString = queryString + "editions[]=" + document.formats_form.elements[i].value + "&";
	}
	// if this is the text search box and something has been entered, append to post vars for query
	else if ((thisElement.name == "strFormat") && (thisElement.value.length > 0))
	{
	    queryString = queryString + "str=" + thisElement.value + "&";
	}
	else if (thisElement.name == "server_edition")
	{
	    queryString = queryString + "editions[]=" + thisElement.value + "&" + "product_type=SERVER" + "&";
	}
	else if (thisElement.name == "platforms" && (thisElement.checked == true))
	{
	   queryString = queryString + "platforms[]=" + thisElement.value + "&";
	}
    }

    // if types specified, append to query string
    if (document.formats_form.formatTypeALL.checked == false)
    {
	if (document.formats_form.formatTypeVECTOR.checked == true)
	{
	    queryString = queryString + "type[]=VECTOR&type[]=VECTOR_RASTER&";
	}
	if (document.formats_form.formatTypeRASTER.checked == true)
	{
	    if (document.formats_form.formatTypeVECTOR.checked == true)
	    {
	    	queryString = queryString + "type[]=RASTER&";
	    }
	    else
	    {
	    	queryString = queryString + "type[]=RASTER&type[]=VECTOR_RASTER&";
	    }
	}
	if (document.formats_form.formatTypeNON_SPATIAL.checked == true)
	{
	    queryString = queryString + "type[]=NON_SPATIAL&";
	}
	if(document.formats_form.formatType3D.checked == true)
	{
		queryString = queryString + "type[]=3D&";
	}    
    }

    if (document.formats_form.tableByEdition.checked == true)
    {
       queryString  = queryString + "compareBy=EDITION&";
    }
    else if (document.formats_form.tableByPlatform.checked == true)
    {
       queryString  = queryString + "compareBy=PLATFORM&";
    }	
	//document.write(queryString);
    return queryString;
}

function refreshTable(platform)
{
    // replace format_table div with animated loader graphic
    document.getElementById('format_table').innerHTML = "<p style=\"text-align: center\"><br><br><br><img src=\"/products/desktop/formats/ajax-loader.gif\" border=\"0\"></p>";

    // first, create the query string
    var queryString = buildQueryString();

    // check if the query string is cached, otherwise send request to format-table
    if (typeof(urlCache[queryString]) != 'undefined')
    {
	//document.getElementById('format_table').innerHTML = "<p class=\"success_box\">CACHED</p>" + urlCache[queryString];
	// delay 1 second (so the loader displays), then return the cached html
	document.getElementById('format_table').innerHTML = urlCache[queryString];
    }
    else
    {
	// init output
        var outputHTML = "";

	function successHandler(o)
	{
		// store the output
		outputHTML = o.responseText;
		
		// send the output to the format table div
		document.getElementById('format_table').innerHTML = outputHTML;

		// store output in cache array
		urlCache[queryString] = outputHTML;
	}

        // send the ajax request
        //var connectionObject = YAHOO.util.Connect.asyncRequest('POST','/dev/20090129_formats/format-table.php?platform=' + platform, {success:successHandler}, queryString);
        var connectionObject = YAHOO.util.Connect.asyncRequest('POST','/products/desktop/formats/format-table.php?platform=' + platform, {success:successHandler}, queryString);
    }
}

// if all is checked, check all boxes
function formatTypeCheckAll()
{
    if (document.formats_form.formatTypeALL.checked == true)
    {
	document.formats_form.formatTypeVECTOR.checked = false;
	document.formats_form.formatTypeRASTER.checked = false;
	document.formats_form.formatTypeNON_SPATIAL.checked = false;
	document.formats_form.formatType3D.checked = false;
    }
    else
    {
	document.formats_form.formatTypeVECTOR.checked = true;
    }
}

// if no boxes checked, default to all
function formatTypeUncheckAll()
{
    if ((document.formats_form.formatTypeVECTOR.checked == true) || (document.formats_form.formatTypeRASTER.checked == true) || (document.formats_form.formatTypeNON_SPATIAL.checked == true) || (document.formats_form.formatType3D.checked == true))
    {
	document.formats_form.formatTypeALL.checked = false;
    }
    else
    {
	document.formats_form.formatTypeALL.checked = true;
    }
}

function formatEditionCheckAll(currentCheckbox)
{
    // init check all
    var checkAll = true;
	    
    if (currentCheckbox.name == "editionsAll")
    {
	if (currentCheckbox.checked == true)
	{
	    for (i = 0; i < document.formats_form.editions.length; i++)
	    {
		document.formats_form.editions[i].checked = false;
	    }
	}
	else
	{
	    document.formats_form.editions[0].checked = true;
	    checkAll = false;
	}
    }
    else
    {
	for (i = 0; i < document.formats_form.editions.length; i++)
	{
	    if (document.formats_form.editions[i].checked == true)
	    {
		checkAll = false;
	    }
	}
    }
    
    // check whether all should be checked or unchecked
    if (checkAll)
    {
        document.formats_form.editionsAll.checked = true;
    }
    else
    {
        document.formats_form.editionsAll.checked = false;
    }
}

function formatPlatformCheckAll(currentCheckbox)
{
   var checkAll = true;
    if (currentCheckbox.name == "allPlatforms")
    {
	if (currentCheckbox.checked == true)
	{
	    for (i = 0; i < document.formats_form.platforms.length; i++)
	    {
		document.formats_form.platforms[i].checked = false;
	    }
	}
	else
	{
	    document.formats_form.platforms[0].checked = true;
	    checkAll = false;
	}
    }
    else
    {
	for (i = 0; i < document.formats_form.platforms.length; i++)
	{
	    if (document.formats_form.platforms[i].checked == true)
	    {
		checkAll = false;
	    }
	}
    }
    
    // check whether all should be checked or unchecked
    if (checkAll)
    {
        document.formats_form.allPlatforms.checked = true;
    }
    else
    {
        document.formats_form.allPlatforms.checked = false;    
    }


}

// show the popup info on a clicked format table cell
function showEditionPanel (cellId,editionId,formatId)
{

	loadFormatEditionPanelContent(editionId,formatId);
	popupPosition(cellId);
	$('#popup').slideDown('fast');
}


function showPlatformPanel (cellId,editionId,formatId)
{	
	loadFormatPlatformPanelContent(editionId,formatId);
	popupPosition(cellId);
	$('#popup').slideDown('fast');
}

function hideEditionPanel ()
{
	$('#popup').hide('fast');
}


function hidePlatformPanel ()
{
	$('#popup').hide('fast');
}



function loadFormatEditionPanelContent(editionId,formatId)
{


    // first, create the query string
    var queryString = "edition_id=" + editionId + "&format_id=" + formatId;

    // check if the query string is cached, otherwise send request to format-table
    if (typeof(urlCache[queryString]) != 'undefined')
    {
	//document.getElementById('format_table').innerHTML = "<p class=\"success_box\">CACHED</p>" + urlCache[queryString];
	// delay 1 second (so the loader displays), then return the cached html
	document.getElementById('popup').innerHTML = urlCache[queryString];
    }
    else
    {
	// init output
        var outputHTML = "";

	function successHandler(o)
	{
		// store the output
		outputHTML = o.responseText;
		
		// send the output to the format table div
		document.getElementById('popup').innerHTML = outputHTML;

		// store output in cache array
		urlCache[queryString] = outputHTML;
	}

        // send the ajax request
        var connectionObject = YAHOO.util.Connect.asyncRequest('POST','/products/desktop/formats/formateditions-details.php', {success:successHandler}, queryString);
    }
}

function loadFormatPlatformPanelContent(editionId,formatId)
{

    // first, create the query string
    var queryString = "edition_id=" + editionId + "&format_id=" + formatId;

    // check if the query string is cached, otherwise send request to format-table
    if (typeof(urlCache[queryString]) != 'undefined')
    {
	//document.getElementById('format_table').innerHTML = "<p class=\"success_box\">CACHED</p>" + urlCache[queryString];
	// delay 1 second (so the loader displays), then return the cached html
	document.getElementById('popup').innerHTML = urlCache[queryString];
    }
    else
    {
	// init output
        var outputHTML = "";

	function successHandler(o)
	{
		// store the output
		outputHTML = o.responseText;
		
		// send the output to the format table div
		document.getElementById('popup').innerHTML = outputHTML;

		// store output in cache array
		urlCache[queryString] = outputHTML;
	}

        // send the ajax request
        var connectionObject = YAHOO.util.Connect.asyncRequest('POST','/products/desktop/formats/formatplatforms-details.php', {success:successHandler}, queryString);
    }
}



// Check/uncheck "Compare by" checkboxes
function compareByCheck(currentCheckbox)
{
	if(currentCheckbox.name == 'tableByEdition')
	{
	   if(currentCheckbox.checked == true)
	   {
             document.formats_form.tableByPlatform.checked = false;
		$('#platform_edition_checkboxes').hide();
		$('#format_edition_checkboxes').show();
   	   }
	   else
          {
             document.formats_form.tableByPlatform.checked = true;
	      $('#platform_edition_checkboxes').show();
             $('#format_edition_checkboxes').hide();
          }

	}	
	else
	{
	   if(currentCheckbox.checked == true)
	   {	
             document.formats_form.tableByEdition.checked = false;

	$('#platform_edition_checkboxes').show();
		$('#format_edition_checkboxes').hide();
   	   }
	   else 
          {
             document.formats_form.tableByEdition.checked = true;
		$('#platform_edition_checkboxes').hide();
             $('#format_edition_checkboxes').show();
          }
	}	
}

function hideSelection()
{ 
	if(document.formats_form.tableByPlatform.checked == true)
	{
 		$('#platform_edition_checkboxes').show();
       	$('#format_edition_checkboxes').hide();
	}
	else
	{
		$('#platform_edition_checkboxes').hide();
		$('#format_edition_checkboxes').show();
	}

}
