// Crew Scheduler js utilities

//070524 open a screen in separate window, using std win size
var wins;
function navigateTo(sURL,sName,sFeatures) {
	// if screen size is < spec, use screen max
	var wid = screen.width - 28;	// 28,80 are padding values for a 1024x768 screen 
	var ht = screen.height - 80;	
	// if screen is large enough, use spec size
	if (wid >= 996) wid = 996;
	if (ht >= 688) ht = 688;	
	if (sFeatures == null || sFeatures == '') 
		sFeatures = 'width=' + wid + ',height=' + ht + ',resizable=yes,scrollbars=yes,status=yes,left=0,top=0';					
	if (wins == null)
		wins = new Array();
	if ((wins[sName] == null) || (wins[sName].closed))
		wins[sName] = window.open(sURL,sName,sFeatures);
	wins[sName].focus();
}


// open a child screen; put timestamp in url to avoid caching:
var winChild = null;
function openChildScreen(url) {
	url = url + '&tstamp=' + new Date().getTime();
	winChild = window.open(url,'winChild','width=996,height=688,resizable=yes,scrollbars=yes,status=yes');
	winChild.focus();
}

// handle the wait screen, usually StatusWindow.aspx;
var winWait = null;
function openWaitScreen(url) {
	winWait = window.open(url,'winWait','width=450,height=50,resizable=no,scrollbars=no,status=no');
	winWait.focus();
}
function closeWaitScreen() {
    if(winWait) {
        winWait.close();
    }
}

// trap the button event fired by [Enter] key on textbox; force postback on btnName.Click()
function postbackOnEnterKey(btnName) {
	if (event.keyCode == 13) {
		// cancel the default submit
		event.returnValue = false;
		event.cancel = true;
		// postback via button click
	    if(btnName == null || btnName == 'undefined') {
            btnName = 'btnRefresh'; // default 
        }
		__doPostBack(btnName,'Click'); 		
		return false; 	
	}
}




// older stuff below here:

// trim leading/trailing whitespace
function trim(s) { return s.replace(/^\s+/,'').replace(/\s+$/,''); }  

// waitMsgShow and waitMsgHide should probably be replaced by calls to StatusWindow.aspx:
// Show control, styled as "please wait" panel
function waitMsgShow(in_labCtrl, in_msg, in_wid, in_ht, in_paddingTop) {
    var ctrl = document.getElementById(in_labCtrl);
    if(ctrl) {
        // defaults
        if(in_wid == null) in_wid = 300;
        if(in_ht  == null) in_ht  = 100;
        if(in_paddingTop  == null) in_paddingTop  = in_ht / 3;  // default top pad: 1/3 ht
        // control dims, padding
        ctrl.style.paddingTop = in_paddingTop;
        ctrl.style.width   = in_wid;
        ctrl.style.height  = in_ht;
	  	// msg text
	    if(in_msg == null) in_msg = "Processing... Please wait...";
	    // add some top-space
	    ctrl.innerHTML = in_msg;
	    // styles
	    ctrl.style.backgroundColor = "#FFFF99";
	    ctrl.style.border     = "gray 2px outset";
        ctrl.style.fontWeight = "bold";
	    ctrl.style.zIndex     = "9999";         // note: dropdownlist controls ignore zindex and always show on top.
	    ctrl.style.position   = "absolute";
	    ctrl.style.textAlign  = "center";
	    ctrl.style.verticaAllign = "middle";
        // msg panel dims
        var oW = parseInt(ctrl.style.width);
        var oH = parseInt(ctrl.style.height);
        // window dims
		var wW = parseInt(document.body.clientWidth);  
		var wH = parseInt(document.body.clientHeight);
		// set msg panel at screen center, accounting for any scroll
		var oL = (wW / 2 + document.body.scrollLeft) - (oW / 2);		
		var oT = (wH / 2 + document.body.scrollTop)  - (oH / 2);		
		// sanity check on neg coords					
		if(oL < 0) oL = 0;
		if(oT < 0) oT = 0;
        // 
		ctrl.style.left = oL;
		ctrl.style.top = oT;
		ctrl.style.visibility = "visible";
	}
}
// hide it
function waitMsgHide(in_ctrl) {
    var ctrl = document.getElementById(in_ctrl);
    if(ctrl) {
	    ctrl.style.visibility = "hidden";
	}
}


