// This function will be called in the case that the page hasn't loaded yet and the user tries to navigate.
// Originally we planned to deny navigation in this case, but returning false from this function causes the
// page to stop loading.  Now the user can potentially get an error if they navigate too soon from the
// Select Context page, but they can just go back and try again.
function AllowNavigate()
{
    return true;
}


function Navigate(validateFunction, destUrl, navExtraInfoName, navExtraInfoValue) 
{
    var valid = true;
    var maxLength = 10000;
 
    destUrl += ""; // to force it to be a string.

    if (validateFunction != null) {
        valid = validateFunction(destUrl);
    }
    
    if (valid) {
        var frm = document.getElementById('theForm');
        if (frm == null || frm.action == null || frm.action.length == 0) {
			// check if we can get the game plan id.
            var hiddenId = document.getElementById('entityId');
            var loc = destUrl;
            var sep = "?";
            
            if (loc.indexOf('?') > 0) {
                sep = "&";
            } else {
                if (hiddenId != null) {
                    loc += "?entityId=" + hiddenId.value;
                    sep = "&";
                }
            }
            if (navExtraInfoName != null) {
                loc += sep + navExtraInfoName + '=' + navExtraInfoValue
            }
            window.location = loc;
        } else {
        	document.getElementById('destUrl').value = destUrl;
            if (navExtraInfoName != null) {
                document.getElementById('navExtraInfoName').value = navExtraInfoName;
                document.getElementById('navExtraInfoValue').value = navExtraInfoValue;
            }
            TruncateTextAreasThatExceedMaxLength(maxLength);
            frm.submit();
        }
    }

}

function Save(validateFunction)
{
    var destUrl = window.location;
    
    if(destUrl.toString().indexOf('isSave') > 0) {
        Navigate(validateFunction, destUrl, null, null);
    }
    else {
       Navigate(validateFunction, destUrl, 'isSave', 1);
    }
}

function examplesPopUp(URL) {
	var attributes = "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1";
        var width = 600;
        var height = 400;
	launchWindow(URL, width, height, attributes);
}

function documentPopUp(URL) {
	var attributes = "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1";
        var width = 700;
        var height = 500;	
        launchWindow(URL, width, height, attributes);
}

function pagePopUp(URL)
{
	var attributes = "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1";
        var width = 700;
        var height = 500;	
        launchWindow(URL, width, height, attributes);
}

function prepPlanPrintPopUp(URL) {
	var attributes = "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1";
        var width = 700;
        var height = 500;
	launchWindow(URL, width, height, attributes);
}

function launchStandardPopUp(URL, width, height)
{
	var attributes = "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1";
	launchWindow(URL, width, height, attributes);
}

function instructionsPopUp(URL)
{
    launchStandardPopUp(URL, 500, 350);
}
function longInstructionsPopUp(URL)
{
    launchStandardPopUp(URL, 700, 500);
}
function launchWindow(URL, width, height, attributes) {
    var winl = (screen.width - width) / 2;
    var wint = (screen.height - height) / 2;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', '' + attributes + ',width=' + width + ',height=' + height + ',left = ' + winl + ',top = ' + wint + '');");
}


function TruncateTextAreasThatExceedMaxLength(maxLength) {
    
    var aTextAreas = document.body.getElementsByTagName("TEXTAREA");

    if (aTextAreas != null) {
        for(var i=0; i < aTextAreas.length; i++) {
            if((aTextAreas[i].value != null) && (aTextAreas[i].value.length > maxLength)) {
                aTextAreas[i].value = aTextAreas[i].value.substr(0, maxLength);
            }    
        }
    }

}