
//***************************************************************************
//*
//*		(c) Copyright 2009 Alta Systems (NI) Ltd
//*		All reproduction and reuse prohibited without prior written
//*		permission of the copyright owner.
//*
//*
//***************************************************************************

var moz = window.Event ? true : false;

var m_dialog;


//// ---------------------------------------------------------------------
//// Return false if the enter key has been hit, true otherwise.
//// Used in onKeypress event in form text fields to prevent form
//// submission when Enter is pressed.
//// ---------------------------------------------------------------------
//function noenter(e) 
//{
//
//	// Only interested in trapping keypresses
//	if (e.type != "keypress")
//		return true;
//
//	// Get the key code
//	if (moz) // Mozilla etc.
//		var whichCode = e.which
//	else // Internet Explorer 4.0x
//		var whichCode = e.keyCode
//
//	// If enter hit then return false to prevent form submit
//	if (whichCode==13)
//		return false;
//	else
//		return true;
//
//}



// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
function display_dialog( p_type, p_title, p_body, p_action, p_fadein )   
{   
	display_dialog_all_action( p_type, p_title, p_body, p_action, "", p_fadein );
}

function display_dialog_all_action( p_type, p_title, p_body, p_ok_action, p_cancel_action, p_fadein )   
{   

	// If already looking at a dialog don't start another one
	if ( m_dialog > 0 )
		return;

	// Hide the selects due to bleed-through problem
	hide_selects();

	var h_width = 400;
	var h_height = 300;

	// Scroll position of the window
	var h_sP = getScrollPos();

	// Set position for the dialog
	var h_left = document.body.clientWidth/2 - (h_width/2);
	var h_top = document.body.clientHeight/2 - (h_height/2) - 40;
	var h_top = h_sP.y + 150; //document.body.clientHeight/2 - (h_height/2) - 40;

//	alert( h_left ) ;
//	alert( h_top ) ;

	var d = document.getElementById('dialogContainer');
	var t = document.getElementById('dialogHeaderInner');
	var b = document.getElementById('dialogBody');
	var c = document.getElementById('curtain');

	// ------------------------------------------------------------------------
	// Display requested buttons - will need to enable passed in script if 
	// dialog button requires an action.
	// ------------------------------------------------------------------------

	p_type = p_type.toLowerCase();
	
	// Only Yes and Ok buttons can have actions - and both shouldn't appear on same dialog. 
	// If this becomes restrictive can revisit.
	prepare_dialog_button ( 'dialogBtnOk',  'ok', p_type, p_ok_action );
	prepare_dialog_button ( 'dialogBtnYes', 'yes', p_type, p_ok_action );
	prepare_dialog_button ( 'dialogBtnNo',  'no', p_type, p_cancel_action );
	prepare_dialog_button ( 'dialogBtnClose',  'close', p_type, p_cancel_action );
	prepare_dialog_button ( 'dialogBtnCancel', 'cancel', p_type, p_cancel_action );


	// ------------------------------------------------------------------------
	// Set the title text
	// ------------------------------------------------------------------------
	if (t)
	{
		p_title = p_title.replace(/-sq-/g, '\'');
		p_title = p_title.replace(/-dq-/g, '"');
		t.innerHTML = p_title;
	}

	// ------------------------------------------------------------------------
	// Set the body text
	// ------------------------------------------------------------------------
	if (b)
	{
		p_body = p_body.replace(/-sq-/g, '\'');
		p_body = p_body.replace(/-dq-/g, '"');
		b.innerHTML = p_body;
	}

	// ------------------------------------------------------------------------
	// Display the curtain
	// ------------------------------------------------------------------------
	if (c)
	{
		c.style.display = "block";
	}


	// ------------------------------------------------------------------------
	// Display the dialog itself
	// ------------------------------------------------------------------------
	if (d)
	{
		d.style.width = h_width;
		d.style.top = h_top;
		d.style.left = h_left;
		d.style.display = "inline";

		// We're in a dialog situation
		m_dialog = 1;

		// Fade in or direct display? Could use a speed setting rather than a flag for more flexibility?
		if(p_fadein == 0)
		{
			d.style.filter = 'alpha(opacity=100)';
			d.style.MozOpacity = 1.0;
			d.style.opacity = 1.0;
		}
		else
		{
			// Fade the dialog in...
			RequestDialogFadeIn(d);
		}
	}

	// Set focus on default button - either OK or Yes
	// Tried this in the prepare button proc but IE didn't like it. May be something to do with display being NONE until this point.
	if(p_type.indexOf("ok") >= 0)
		document.getElementById('dialogBtnOk').focus();
	if(p_type.indexOf("yes") >= 0)
		document.getElementById('dialogBtnYes').focus();

}  

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
function display_curtain()
{
	var c = document.getElementById('curtain');

	if (c)
	{
		c.style.display = "block";
	}

}

// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
function hide_curtain()
{
	var c = document.getElementById('curtain');

	if (c)
	{
		c.style.display = "none";
	}

}

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------

function prepare_dialog_button ( p_btn_div, p_btn_type, p_requested_buttons, p_action )	
{

	var h_btn = document.getElementById(p_btn_div);

	if(h_btn)
	{
		// Is the current button in the string of requested buttons for the dialog?
		if(p_requested_buttons.indexOf(p_btn_type) >= 0)
		{
			h_btn.style.display = "inline";

			if(p_action != '')
			{
				// Quotes within quotes within quotes - it's a pain.
				p_action = p_action.replace(/-sq-/g, '\'');
				p_action = p_action.replace(/-dq-/g, '\"');
				h_btn.onclick = function (evt) {eval(p_action);}
			}

		}
		else
		{
			h_btn.style.display = "none";
		}

	}

}

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------

function hide_selects () 
{
	var selects, i; 

	selects = document.getElementsByTagName('select'); 
	for(i=0;i<selects.length;i++) 
	{
		selects[i].style.visibility='hidden'; 
	}
}

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------

function show_selects () 
{
	var selects, i; 

	selects = document.getElementsByTagName('select'); 
	for(i=0;i<selects.length;i++) 
	{
		selects[i].style.visibility='visible'; 
	}
}

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------

function cancel_dialog( p_dialog_div )   
{   
	var d = document.getElementById(p_dialog_div);
	var c = document.getElementById('curtain');
	
	// We're out of a dialog situation
	m_dialog = 0;
	m_dialog_fade_triggered = false;

	c.style.display = "none";

	RequestDialogFadeOut(d);

	// Show the selects again
	show_selects();

}  

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


// ---------------------------------------------------------------------
// ---------------------------------------------------------------------


// Use globals for recursive fade procs to save passing params around - cheat-tastic

var m_dialog_fadeblock;
var m_dialog_fade_level;
var m_dialog_fade_target;
var m_dialog_fade_triggered = false;

function RequestDialogFadeIn( p_this ) 
{
	// If fade already triggered this time round then don't run again since that will cause a flicker effect
	if (m_dialog_fade_triggered)
	{
		return;
	}

	m_dialog_fadeblock = p_this;
	m_dialog_fade_level = 0;
	dialogFadeIn();

	m_dialog_fade_triggered = true;
}

function dialogFadeIn()
{
	// If we'ver faded to our target opacity then we're done
	if(m_dialog_fade_level >= 100)
		return;

	// Reduce fade level
	m_dialog_fade_level+=10;

	// Set styles
	m_dialog_fadeblock.style.filter = 'alpha(opacity='+m_dialog_fade_level+')';
	m_dialog_fadeblock.style.MozOpacity = m_dialog_fade_level/100;
	m_dialog_fadeblock.style.opacity = m_dialog_fade_level/100;

	// And recurse into function again
	var myfader=setTimeout('dialogFadeIn()',50);

}

function RequestDialogFadeOut( p_this ) 
{
	m_dialog_fadeblock = p_this;
	m_dialog_fade_level = 100;
	m_dialog_fade_target = 0;
	dialogFadeOut();
}

function dialogFadeOut()
{
	// If we'ver faded to our target opacity then we're done
	if(m_dialog_fade_level <= m_dialog_fade_target)
	{
		// Finally, turn the dialog off completely - have to do it here due to timeouts
		m_dialog_fadeblock.style.display = "none";

		return;
	}

	// Reduce fade level
	m_dialog_fade_level-=10;

	// Set styles
	m_dialog_fadeblock.style.filter = 'alpha(opacity='+m_dialog_fade_level+')';
	m_dialog_fadeblock.style.MozOpacity = m_dialog_fade_level/100;
	m_dialog_fadeblock.style.opacity = m_dialog_fade_level/100;

	// And recurse into function again
	var myfader=setTimeout('dialogFadeOut()',1);

}

function getScrollPos(){
    if (window.pageYOffset){
        return {y:window.pageYOffset, x:window.pageXOffset};
    }
    if(document.documentElement && document.documentElement.scrollTop){
        return {y:document.documentElement.scrollTop, x:document.documentElement.scrollLeft};
    }
    if(document.body){
        return {y:document.body.scrollTop, x:document.body.scrollLeft};
    }
    return {x:0, y:0};
}

