// Global variables
var pathToImage = "./";
var ajaxURL = "Remote.aspx";  // URL that handles Ajax calls


//-----------------------------------------------------------------
// CollapsiblePanel
//-----------------------------------------------------------------

function toggleElement(id){
	
	if(document.getElementById){
		var link = document.getElementById('link_'+id)
		var element = document.getElementById(id);
		if(element){
			if(element.style.display == 'none')
			{
			 	element.style.display ='block';
			}
			else
			{
				element.style.display ='none'
			}			
		}
		if(link){
			if(element.style.display == 'none')
			{				
				link.innerHTML = ' [+ Expand +] '
			}
			else
			{
				link.innerHTML= ' [- Collapse -] '
			}			
		}
	}
}

function toggleElementPic(id, imgid/*, onFirstOpen*/){
	var theImage = document.getElementById(imgid);
	
	if(document.getElementById){
		var element = document.getElementById(id);
		if(element)
		{
			if(element.style.display == 'none')
			{
				element.style.display = 'block';
				theImage.src = pathToImage + "Collapse.gif";
				
//				if (!element.loadedOnce && onFirstOpen)
//				{
//				    element.loadedOnce = true;
//				    onFirstOpen();
//				}
			}
			else
			{
				element.style.display = 'none';
				theImage.src = pathToImage + "Expand.gif";
				
//				element.loadedOnce = true;
			}
		}
	}
}

function toggleBlock(id){
	
	if(document.getElementById){
		var element = document.getElementById(id);
		if(element){
			if(element.style.display == 'none')
			{
			 	element.style.display ='block';
			 	return true;
			}
			else
			{
				element.style.display ='none';
				return false;
			}			
		}
	}
}

function toggleElementPicRegions(idClosed, idOpened, imgid, expandImageSrc, collapseImageSrc){
	var theImage = document.getElementById(imgid);
	
	var openedElement = document.getElementById(idOpened);
	var closedElement = document.getElementById(idClosed);
	if(openedElement && closedElement)
	{
		if(openedElement.style.display == 'none')
		{
			openedElement.style.display = 'block';
			closedElement.style.display = 'none';
			theImage.src = collapseImageSrc;	
			return true;		
		}
		else
		{
			openedElement.style.display = 'none';
			closedElement.style.display = 'block';
			theImage.src = expandImageSrc;		
			return false;	
		}
		
	}
}

function setImage(imageId, newImageSrc)
{
	if(document.getElementById)
	{
	    var theImage = document.getElementById(imageId);
	    if (theImage)
	    {
	        theImage.src = newImageSrc;
	    }
	}
}

function showRegion(id)
{
	var element = document.getElementById(id);
	if (element)
	{
	    element.style.display = 'block';
	}
}

function showSetRegion(id, innerHtml)
{
	var element = document.getElementById(id);
	if (element)
	{
	    element.style.display = 'block';
	    element.innerHTML = innerHtml;
	}
}

function hideRegion(id)
{
	var element = document.getElementById(id);
	if (element)
	{
	    element.style.display = 'none';
	}
}

function SetCheck(id, value)
{
	var element = document.getElementById(id);
	if (element)
    {
        element.checked = value;
    }
}

//-----------------------------------------------------------------
// RatingControl
//-----------------------------------------------------------------

function ratingMouseOver(imageId, selectedId, newImageSrc)
{
	if(document.getElementById && imageId != selectedId)
	{
	    var theImage = document.getElementById(imageId);
	    theImage.src = pathToImage + "Controls/" + newImageSrc;
	}
}

function ratingMouseOut(imageId, selectedId, newImageSrc)
{
	if(document.getElementById && imageId != selectedId)
	{
	    var theImage = document.getElementById(imageId);
	    theImage.src = pathToImage + "Controls/" + newImageSrc;
	}
}

function ratingClick(imageId, selectedId, newImageSrc, nodeId, keywordIds, rating, oldRating)
{
    if (imageId == selectedId)
    {
        return; // already selected, do nothing
    }
        
    // change image
    if(document.getElementById)
    {
	    var theImage = document.getElementById(imageId);
	    theImage.src = pathToImage + "Controls/" + newImageSrc;        
    }
    
    // make AJAX call
    //xmlhttpGetURL("/Ajax/Rate.aspx?nid=" + nodeId + "&keywordList=" + keywordIds + "&r=" + rating + "&or=" + oldRating, ratingCallback, false, false); // debugging
    xmlhttpGetURL("/Ajax/Rate.aspx?nid=" + nodeId + "&keywordList=" + keywordIds + "&r=" + rating + "&or=" + oldRating, false, false, false);
}

function ratingCallback(xmlHttpReq, userdata1, userdata2)
{
    alert("Rating result:\n" + xmlHttpReq.responseText)    
}

//-----------------------------------------------------------------
// CollapsiblePanel
//-----------------------------------------------------------------

function getCheckedAttribValues(parentId, attribName)
{
    var parent = document.getElementById(parentId);
    return getCheckedAttribValuesEl(parent ,attribName);
}

function getCheckedAttribValuesEl(parent, attribName)
{
    var vals = "";
	var children = parent.childNodes;
    for(var i = 0; i < children.length; i++)
	{
	    var el = children[i];
	    if (el == null || el.getAttribute == null)
	        continue;
	    
	    var val = el.getAttribute(attribName);
	    if (val != null && typeof el.checked == "boolean")
	    {
	        if (el.checked == true)
	        {
	            vals = vals + val + ";";
	        }	    
	    }
	    else
	        vals = vals + getCheckedAttribValuesEl(el, attribName);
    }
    return vals;
}

function setCheckedWithAttribValues(parentId, attribName, value)
{
    setCheckedWithAttribValuesEl(document.getElementById(parentId), attribName, value);
}

function setCheckedWithAttribValuesEl(parent, attribName, value)
{
	var children = parent.childNodes;
	
    for(var i = 0; i < children.length; i++)
	{
	    var el = children[i];
	    if (el == null || el.getAttribute == null)
	        continue;
	      
	    var val = el.getAttribute(attribName);
	    if (val != null && typeof el.checked == "boolean")
	        el.checked = value;
	    else
	        setCheckedWithAttribValuesEl(el, attribName, value);
    }
}

function toggleCheckbox(checkboxId)
{
	if(document.getElementById)
	{
	    var ck = document.getElementById(checkboxId);
	    if (ck != null)
	    {
	        ck.checked = !ck.checked;
	    }
	    else
	    {
	        alert("Not found (ToggleCheckbox): " + checkboxId);
	    }
	}    
}

//-----------------------------------------------------------------
// Basic AJAX support
//-----------------------------------------------------------------

function xmlhttpGetURL(strURL, responseCallback, userdata1, userdata2) 
{
    var xmlHttpReq = false;
    
    if (window.ActiveXObject) 
    {
        // IE
      	try 
      	{
        	xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
      	} 
      	catch(e) 
      	{
        	try 
        	{
          		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        	} 
        	catch(e) 
        	{
          		xmlHttpReq = false;
        	}
		}

    }
    else if (window.XMLHttpRequest) 
    {
        // Mozilla/Safari
        xmlHttpReq = new XMLHttpRequest();
        xmlHttpReq.overrideMimeType('text/xml');
    }
     
    
    if (xmlHttpReq)
    {
        //xmlHttpReq.open('POST', strURL, true);
        //xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHttpReq.open('GET', strURL, true);

        xmlHttpReq.onreadystatechange = function() 
        {
            if (xmlHttpReq.readyState == 4) 
            {
                if (xmlHttpReq.status == 200)
                {
                    // success, execute callback if specified
                    if (responseCallback)
                    {
                        responseCallback(xmlHttpReq, userdata1, userdata2);
                    }
                }
                else
                {
                    // failure!
                    alert("There was a problem retrieving the XML data (" + strURL + "):\n" + xmlHttpReq.statusText)
                }
            }
        }
        xmlHttpReq.send("");
    }
}

function fillInnerHtml(xmlHttpReq, replaceControlId, userdata2)
{
    var theControl = document.getElementById(replaceControlId);
    if (theControl)
    {
        theControl.innerHTML = xmlHttpReq.responseText;
        //alert(replaceControlId + " FillInnerHtml: " + theControl.innerHTML);
    }
    else
    {
        alert("Not found (fillInnerHtml): " + replaceControlId);
    }
}

function xmlhttpGetSetInnerHtml(strURL, replaceControlId) 
{
    var theControl = document.getElementById(replaceControlId);
    if (theControl)
    {    
        xmlhttpGetURL(strURL, fillInnerHtml, replaceControlId, false);
    }
    else
    {
        //For debugging
        //alert("Not found (xmlhttpGetSetInnerHtml): " + replaceControlId);
    }
}

//-----------------------------------------------------------------
// AQI
//-----------------------------------------------------------------


// Parameter-based Generic Ajax function version from Greg that uses a global variable with the URL 
// -- Bad, but easier than changing AQI
function xmlhttpGet(params, responseCallback, userdata1, userdata2)	
{
	// This code below may look like a big comment block, but it's not...
	var xmlHttpReq = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try
	{
		xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e2)
		{
			xmlHttpReq = false;
		}
	}
	@end @*/

	if(!xmlHttpReq && typeof XMLHttpRequest != "undefined")
	{
		xmlHttpReq = new XMLHttpRequest();
	}
	
	if(xmlHttpReq)
	{
		if(xmlHttpReq.overrideMimeType) xmlHttpReq.overrideMimeType("text/xml");
		xmlHttpReq.open("POST", ajaxURL, true);
		xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttpReq.setRequestHeader("Content-Length", params.length);
		xmlHttpReq.setRequestHeader("Cache-Control", "no-cache");
		xmlHttpReq.setRequestHeader("Connection", "close");
		
		//xmlHttpReq.open("GET", strURL, true);

		xmlHttpReq.onreadystatechange =	function() 
		{
			if(xmlHttpReq.readyState == 4)	
			{
				// Sometimes interrupted requests may cause problems
				try
				{
					if(xmlHttpReq.status == 200)
					{
						if(responseCallback)
						{
							responseCallback(xmlHttpReq, userdata1,	userdata2);
						}
					}
					else
					{
						throw("Unsuccessful Ajax call (code "+xmlHttpReq.status+").  Status: "+xmlHttpReq.statusText);
					}
				}
				catch(error)
				{
					// Comment this line out for release builds
					alert("Ajax exception caught.  Parameters: "+params.replace(/&/g,", ")+"  Message: "+error);
				}
			}
		}
		xmlHttpReq.send(params);
	}
}

// Open/close a collapsible panel.  If the content is not loaded in the panel yet, load it up.
function toggleRegion(id, control, args, type, change)
{
	// Don't do anything if the region is already loading

	
	if(change)
	{
		var	img = document.getElementById(id + "Image");
		var	region = document.getElementById(id + "Region");
		if(region.style.display == "none")
		{
			region.style.display = type;
			img.src = "../Images/collapse.gif";
			// Fun stuff
			//region.style.opacity = 0.2;
			//var t = setTimeout("fadein('"+id+"Region', 0.2)", 25);
		}
		else
		{
			region.style.display = "none";
			img.src = "../Images/expand.gif";
		}
	}
		
	if(document.getElementById(id + "Region") && document.getElementById(id + "Region").innerHTML.match(id + "Loading"))
	{
		var params = "op=Collapse&control="+control+"&target="+id+"&arglist="+args;		
		xmlhttpGet(params, fillRegion, id, false);
	}
}