var dialogCallback = null;
var expectInput = false;

function grayOut(vis, zindex) {
  // Pass true to gray out screen, false to ungray
  if (zindex == null) {
  	zindex = 1000;
  }
  var opacity = 60;
  var opaque = (opacity / 100);
  var bgcolor = '#000020';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
//        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.position='fixed';
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
        tnode.style.right='0px';
		tnode.style.bottom='0px';
		tnode.style.textAlign='center';
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;
    dark.style.display='block';
  } else {
     dark.style.display='none';
  }
}

// build/show the dialog box, populate the data and call the fadeDialog function //
// type can be 'warning', 'success', 'prompt', or 'error'
function showDialog(title,message,type)
{
	grayOut(true);
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);
    dialogclose.setAttribute('onclick','closeDialog()');
    dialogclose.onclick = closeDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialog.style.visibility = "visible";
	dialog.style.display = "block";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = posLeft();
  var top = posTop();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  dialog.timer = setInterval("fadeDialog(1)", 5);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + 10;
  } else {
    value = dialog.alpha - 10;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}

function fadeIn(divName) {
	var theDiv = document.getElementById(divName);
	theDiv.alpha = 0;
	theDiv.style.opacity = 0;
	theDiv.style.filter = 'alpha(opacity=0)';
	theDiv.style.display = 'block';
	fadeInDiv(divName);
}
function fadeInDiv(divName) {
	var theDiv = document.getElementById(divName);
	var value = theDiv.alpha + 5;
	theDiv.alpha = value;
	theDiv.style.opacity = (value / 100);
	theDiv.style.filter = 'alpha(opacity='+value+')';
	if (value < 99) {
		setTimeout('fadeInDiv("'+divName+'")', 50);
	}
}
function closeDialog()
{
	document.getElementById("dialog").style.display = 'none';
	document.getElementById("darkenScreenObject").style.display = 'none';
	grayOut(false);
}

function clickConfirm()
{
	document.getElementById("dialog").style.display = 'none';
	document.getElementById("darkenScreenObject").style.display = 'none';
	grayOut(false);
	if (expectInput)
		dialogCallback(document.getElementById("dialogInputBox").value);
	else
		dialogCallback(true);
}

function clickCancel()
{
	document.getElementById("dialog").style.display = 'none';
	document.getElementById("darkenScreenObject").style.display = 'none';
	grayOut(false);
	if (expectInput)
		dialogCallback(null);
	else
		dialogCallback(false);
}

// build/show the dialog box, populate the data and call the fadeDialog function //
// type can be 'warning', 'success', 'prompt', or 'error'
function showConfirm(title,message,type,callbackfunc)
{
	expectInput = false;
	dialogCallback = callbackfunc;
	grayOut(true);
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogconfirm;
  var dialogcancel;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','closeDialog()');
    dialogclose.onclick = closeDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialog.style.visibility = "visible";
	dialog.style.display = "block";
  }
  if(!document.getElementById('dialog-confirm')) {
	dialogconfirm = document.createElement('div');
	dialogconfirm.id = 'dialog-confirm';
	dialogconfirm.innerHTML = '<button class="dialog-button" onclick="clickConfirm()">OK</button>';
	dialogcancel = document.createElement('div');
	dialogcancel.id = 'dialog-cancel';
	dialogcancel.innerHTML = '<button class="dialog-button" onclick="clickCancel()">Cancel</button>';
  }
  else {
	dialogconfirm = document.getElementById('dialog-confirm');
	dialogcancel = document.getElementById('dialog-cancel');  	
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = posLeft();
  var top = posTop();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message+'<div id="dialog-confirm">'+dialogconfirm.innerHTML+'</div><div id="dialog-cancel">'+dialogcancel.innerHTML+'</div>';
  dialog.timer = setInterval("fadeDialog(1)", 5);
}

// build/show the dialog box, populate the data and call the fadeDialog function //
// type can be 'warning', 'success', 'prompt', or 'error'
function showPrompt(title,message,type,callbackfunc)
{
	expectInput = true;
	dialogCallback = callbackfunc;
	grayOut(true);
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogconfirm;
  var dialogcancel;
  var dialoginput;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);;
    dialogclose.setAttribute('onclick','closeDialog()');
    dialogclose.onclick = closeDialog;
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialog.style.visibility = "visible";
	dialog.style.display = "block";
  }
  if(!document.getElementById('dialog-confirm')) {
	dialogconfirm = document.createElement('div');
	dialogconfirm.id = 'dialog-confirm';
	dialogconfirm.innerHTML = '<button class="dialog-button" onclick="clickConfirm()">OK</button>';
	dialogcancel = document.createElement('div');
	dialogcancel.id = 'dialog-cancel';
	dialogcancel.innerHTML = '<button class="dialog-button" onclick="clickCancel()">Cancel</button>';
  }
  else {
	dialogconfirm = document.getElementById('dialog-confirm');
	dialogcancel = document.getElementById('dialog-cancel');  	
  }
  if (!document.getElementById('dialog-input')) {
	dialoginput = document.createElement('div');
	dialoginput.id = 'dialog-input';
	dialoginput.innerHTML = '<form onsubmit="clickConfirm()"><input type="text" id="dialogInputBox" size="50" maxlength="200" /></form>';
  }
  else {
	dialoginput = document.getElementById('dialog-input');
  	document.getElementById("dialogInputBox").value = "";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  var width = pageWidth();
  var height = pageHeight();
  var left = posLeft();
  var top = posTop();
  var dialogwidth = dialog.offsetWidth;
  var dialogheight = dialog.offsetHeight;
  var topposition = top + (height / 3) - (dialogheight / 2);
  var leftposition = left + (width / 2) - (dialogwidth / 2);
  dialog.style.top = topposition + "px";
  dialog.style.left = leftposition + "px";
  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message+'<div id="dialog-confirm">'+dialogconfirm.innerHTML+'</div><div id="dialog-cancel">'+dialogcancel.innerHTML+'</div><div id="dialog-input">'+dialoginput.innerHTML+'</div>';
  dialog.timer = setInterval("fadeDialog(1)", 5);
}

function getHTTPObject()
{
	var xmlHttp;
	try
   	{
   		// Firefox, Opera 8.0+, Safari
   		xmlHttp=new XMLHttpRequest();
   	}
	catch (e)
   	{
  		// Internet Explorer
   		try
   		{
   			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   		}
   		catch (e)
   		{
   			try
   			{
   				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
   			}
   			catch (e)
   			{
   				alert("Your browser does not support AJAX!");
   				return false;
   			}
   		}
   	}	
			
	return xmlHttp;
}

function pageWidth()
{
	return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}
function pageHeight() 
{
	return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}
function posLeft() 
{
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
}
function posTop() 
{
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}

