// Get a query-string parameter value
function getParam(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );  
	if(results == null)
		return "";
	else
		return results[1];}

// Limit the number of characters in a textarea. Example use:
// <textarea onkeypress="javascript:handleOnKeypress(this, 3500);" onchange="javascript:handleOnChange(this, 3500);"></textarea>
function handleOnKeypress(obj, maxlength) {
	if ( ( maxlength > 0 ) && ( obj.value.length >= maxlength ) ) {
		window.event.returnValue = false;
	}
}

function handleOnChange(obj, maxlength) {
	if ( ( maxlength > 0 ) && ( obj.value.length > maxlength ) ) {
		obj.value = obj.value.substr( 0, maxlength );
	}
}

// MouseOver events for the GridViews
var _oldColor;

function SetNewColor(source)
{
    _oldColor = source.style.backgroundColor;
    source.style.backgroundColor = '#ffe703';
}

function SetOldColor(source)
{
    source.style.backgroundColor = _oldColor;
}	

function PopUp(strURL, intW, intH, newWindow)
{
    strWindowFeatures = "";
    if (intW > 0 && intH > 0)
    {
        strWindowFeatures +=  "width=" + intW + ", ";
	    strWindowFeatures += ("height=" + intH + ", ");    	
	    strWindowFeatures += ("left=" + (screen.availWidth-intW)/2 + ", ");
	    strWindowFeatures += ("top=" + (screen.availHeight-intH)/2 + ", ");
    }
	strWindowFeatures += "menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes";
	wndPopup = window.open(strURL, (newWindow ? "_blank" : "GISIS_PopUp"), strWindowFeatures);
}
           
// Set the control in ref to state. State can be either 'visible' or 'hidden'
function ShowHide(ref, state) { 
    if (document.getElementById) { // DOM3 = IE5, NS6 
        document.getElementById(ref).style.visibility = state; 
    } 
    else if (document.layers) { // Netscape 4 
        document.layers[ref].visibility = state;
    } 
    else { // IE 4 
        eval("document.all." + ref + ".style.visibility = '" + state + "'");
    } 
}

var showHideContentExpImg = new Image();
var showHideContentColImg = new Image();
showHideContentExpImg.src = webroot + "Shared/Images/Expand.gif";
showHideContentExpImg.alt = "Expand";
showHideContentColImg.src = webroot + "Shared/Images/Collapse.gif";
showHideContentColImg.alt = "Collapse";

function showHideContent(id)
{
	var oContent = document.getElementById("content_" + id);	
	var oImage = document.getElementById("image_" + id);	

	if (!oContent) 
		return;	

	if (oContent.style.display == "")
	{
		oContent.style.display = "none";
		if (oImage)
		{
			oImage.src = showHideContentExpImg.src;
			oImage.alt = "Expand";
		}
	}
	else
	{
		oContent.style.display = "";
		if (oImage)
		{
			oImage.src = webroot + "Shared/Images/Collapse.gif";
			oImage.alt = "Collapse";
		}
	}	
}

// Overrides are triggered when the page is loaded.
function PageLoad(){}

// Overrides are triggered when the page is unloaded.
function PageUnload(){}

// Overrides are triggered just before page is unloaded.
function PageBeforeUnload(){}

function ScrollToAnchor()
{
	if (scrollToAnchor != "")
		location.hash = scrollToAnchor;
}

// This function calls the add or update method on the search control if needed.
// Add the following to the Search button OnClientClick="CompleteSearch('NAME OF SQLWHEREBUILDER CONTROL')"
function CompleteSearch(control)
{
    // get the SQLWhereBuilder Object
    var sqlwb = SQLWB_GetSQLWBObject(control);
    
    // get the current values
    var values = sqlwb.GetCurrentValues();
    
    // get the current field                
    var field = sqlwb.GetCurrentField();
    
    // get the current operator
    var operator = sqlwb.GetCurrentOperator();
    
    // return if there is not at least one value
    if (values.length < 1) return;
    
    // get the first value
    var value = values[0];
    
    // if editing then call update
    if (sqlwb.editIndex != -1)
    {
        SQLWB_UpdateButton_Click(control);
    }
    else
    {
        // ignore if default value
        if ((value.value != "") && (value.value != -1) && (value.value != "dd/MM/yyyy")) 
        {
            // check if value already exists in the conditions collection              
            for (var i=0; i<sqlwb.conditions.length; i++)
            {            
                // dont add if already in the conditions collection 
                if (field.id == sqlwb.conditions[i].field.id &&
                    operator.id == sqlwb.conditions[i].operator.id &&
                    value.value == sqlwb.conditions[i].values[0].value) return;
            }
            
            // now add
            SQLWB_AddButton_Click(control);
        }
    }
}

// cancel 'enter' keypresses
function CheckEnter(e)
{
	if (!e) var e = window.event;
	
	if(e.keyCode == 13 && e.srcElement.type != "textarea")
		return false;
	return true;
}

function GetWindowHeight()
{
	if (document.all)
	{
		return document.documentElement.clientHeight;
	}
	else
	{
		return window.innerHeight;
	}
}

function GetWindowWidth()
{
	if (document.all)
	{
		return document.documentElement.clientWidth;
	}
	else
	{
		return window.innerWidth;
	}
}

// Overrides to restore auto saved data
function AutoSaveRestore(){}

// When page is unloaded, this will be checked.
function AutoSavePageExit(){}

// Displays confirmation box and cancels event
function doConfirm(e, message) {
    if (!e) e = windows.event;
    if (e.stopPropagation) { e.stopPropagation(); } //For 'Good' browsers
    else { e.cancelBubble = true; } //For IE
    return confirm(message);
}