
var woAlertsTimeout = null;
function showAlert() {
	if(document.getElementById) {
		alert('showAlert was called.');
		if(! document.getElementById('woAlertsDiv')) {
			var alertDiv = document.createElement('div');
			alertDiv.setAttribute('id', 'woAlertsDiv');
			document.getElementsByTagName("body")[0].appendChild(alertDiv);
		}
		if(! document.getElementById('woAlertsDivContent')) {
			var dText = '';
			dText = '<div id="woAlertsDivContent" class="woAlertsDivContent" onmouseover="//CancelFade();SetFullOpacity(\'woAlertsDiv\',\'woAlertsDivContent\')" style="position:absolute;bottom:-260px;right:5px;border:1px solid black;background:#89D027 url(/app/images/alertHeaderBg.gif) repeat-x top left;color:#FFFFFF;font-weight:bold;width:350px;height:260px;">' +
					'<div style="width:100%;height:16px;text-align:right;padding:3px;"><a href="#" onclick="hideMe(\'woAlertsDiv\');return false;" style="color:#FFFFFF;font-weight:bold;">Close</a>&nbsp;&nbsp;</div>' + 
					'<iframe id="alertsIframe" name="alertsIframe" frameborder="0" width="100%" height="230px" src="/app/default.asp?W=ShowAlert&P=0&X=1"></iframe></div>';
			document.getElementById('woAlertsDiv').insertAdjacentHTML('afterBegin', dText);
		}
		else {
			//alert('ShowAlert was called.');
			if(document.getElementById('woAlertsDiv').style.visibility == 'hidden') {
				document.getElementById('woAlertsDiv').style.visibility = 'visible';
				document.getElementById('woAlertsDiv').style.display = 'block';
				document.getElementById('woAlertsDivContent').style.bottom = '-260px';
				document.getElementById('alertsIframe').src = '/app/default.asp?W=ShowAlert&P=0&X=1';
			}
		}
		showAlertDivContent();
		//woAlertsTimeout = setTimeout("fadeOut('woAlertsDivContent', 'woAlertsDiv')", 20000);
	}
}

function showAlertDivContent() {
	var valBottom = document.getElementById('woAlertsDivContent').style.bottom.replace('px','');
	if(eval(valBottom) < 0) {
		document.getElementById('woAlertsDivContent').style.bottom = eval(valBottom) + 5 + 'px';
		setTimeout('showAlertDivContent()', 15);
	}
}
function checkForAlerts() {
	var url = '/app/default.asp?W=GetUserAlerts&X=1';
	var groupName = 'group1';
	AjaxRequest.get(
		{
			'url':url
			,'timeout':5000
			,'onSuccess':function(req){ 
				returnVal = req.responseText;
				//alert(returnVal);
				if(returnVal == 'yes') {
					showAlert();
				}
			}
			,'onError':function(){
				returnVal = "Error connecting to the web service.";
			}
			,'groupName':groupName
		}
	);
}
//setInterval("showAlert()", 60000); //Check for alerts every minute.
//checkForAlerts();
if(location.href.indexOf('Z') == -1 && location.href.indexOf('X') == -1 && location.href.indexOf('/app/') != -1 && location.href.indexOf('ShowAlert') == -1) {
	setTimeout("checkForAlerts()", 30000); // wait 30 seconds before the first call
	setInterval("checkForAlerts()", 300000); // Check for alerts every 5 minutes
}

/*		
function doNotShowAlertAgain(activeAlertID) {
	var url = '/app/default.asp?W=DoNotShowAlert&P=' + activeAlertID + '&X=1';
	if(confirm('Are you sure you wish to hide this alert?\nYou will not be notified about it again.')) {
		location.href = url;
	}
}
*/

function goToNextAlert(nextIndex, counter) {
	document.getElementById('FormDirection').value = 'next';
	document.getElementById('ShowAlertForm').submit();
}
function goToPreviousAlert(nextIndex, counter) {
	document.getElementById('ShowAlertForm').action = '/app/default.asp?W=ShowAlert&P=' + nextIndex + '&force=1&X=1&ShowText=test';
	document.getElementById('FormDirection').value = 'previous';
	document.getElementById('ShowAlertForm').submit();
}
function doNotShowAlertAgain(viewHistoryID, checkedYN) {
	var url = '/app/default.asp?W=UpdateAlertViewHistory&P=' + viewHistoryID + '&P=' + checkedYN + '&X=1';
	//alert(url);
	GetAJAXResult('', url, 'group1');
}
function goToWorkOrder(id) {
	document.getElementById('GoToWorkOrder').value = id;
	document.getElementById('ShowAlertForm').submit();
}

function selectAllAlerts(checkedYN) {
	var alertsTable = document.getElementById('alertsTable');
	var alertsTableCBs = alertsTable.getElementsByTagName('input');
	var i;
	if(checkedYN) {
		for(i in alertsTableCBs) {
			if(/cbAlert/.test(alertsTableCBs[i].id)) {
				alertsTableCBs[i].checked = 1;
			}
		}
	}
	else {
		for(i in alertsTableCBs) {
			if(/cbAlert/.test(alertsTableCBs[i].id)) {
				alertsTableCBs[i].checked = 0;
			}
		}
	}
}

function hideOrUnhideCheckedAlerts(hideYN) {
	var alertHistoryID = '';
	var alertsTable = document.getElementById('alertsTable');
	var alertsTableCBs = alertsTable.getElementsByTagName('input');
	var i;
	for(i in alertsTableCBs) {
		if(/cbAlert/.test(alertsTableCBs[i].id)) {
			if(alertsTableCBs[i].checked == 1) {
				if(alertHistoryID == '') {
					alertHistoryID = alertsTableCBs[i].id.replace('cbAlert', '');
				}
				else {
					alertHistoryID += ',' + alertsTableCBs[i].id.replace('cbAlert', '');
				}
			};
		}
	}
	doNotShowAlertAgain(alertHistoryID, hideYN);
	location.href = location.href;
}

