var ALERT_BUTTON_TEXT = "Ok";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
function override_alert() {
	if(document.getElementById) {
		window.alert = function(txt) {
			createCustomAlert(txt);
		}
	}
}

function get_scroll_top()
{
	var scrollPos;
	if (typeof window.pageYOffset != 'undefined') {
	   scrollPos = window.pageYOffset;
	}
	else if (typeof document.compatMode != 'undefined' &&
	     document.compatMode != 'BackCompat') {
	   scrollPos = document.documentElement.scrollTop;
	}
	else if (typeof document.body != 'undefined') {
	   scrollPos = document.body.scrollTop;
	} 

	return scrollPos;
}

function update_custom_alert_top()
{
	var d = document;
	var alertObj = d.getElementById("custom_alert_alertBox");
	if (alertObj != undefined) {
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
//	if(d.all && !window.opera)

		alertObj.style.top = get_scroll_top() + "px";
		// center the alert box
		alertObj.style.left = (d.body.scrollWidth - alertObj.offsetWidth)/2 + "px";
	}
}

function createCustomAlert(txt, title, btn_txt) {
	// shortcut reference to the document object
	var d = document;
	var sWidth,sHeight;
	sWidth=document.body.offsetWidth;//浏览器工作区域内页面宽度 或使用 screen.width//屏幕的宽度
	sHeight=screen.height;//屏幕高度（垂直分辨率）

	// if the custom_alert_modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("custom_alert_modalContainer")) return;

	// create the custom_alert_modalContainer div as a child of the BODY element
	divObj = d.getElementById("custom_alert_div");
	bgObj = divObj.appendChild(d.createElement("div"));
	bgObj.id = "custom_alert_bgdiv";
	bgObj.style.position = "absolute";
	bgObj.style.top = "0";
	//bgObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
	//bgObj.style.opacity = "0.6";
	//bgObj.style.left = "0";
	bgObj.style.width = sWidth + "px";
	bgObj.style.height = sHeight + "px";
	bgObj.style.zIndex = "10000";
	
	mObj = bgObj.appendChild(d.createElement("div"));
	mObj.id = "custom_alert_modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = d.body.scrollHeight + "px";
	//alert(document.documentElement);

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	//alertObj = d.createElement("div");
	alertObj.id = "custom_alert_alertBox";

	update_custom_alert_top();
	if ($ != undefined) {
		$(window).scroll(update_custom_alert_top);
	}

	if ( title != undefined && title.length > 0 ) {
		// create an H1 element as the title bar
		h1 = alertObj.appendChild(d.createElement("h1"));
		h1.appendChild(d.createTextNode(title));
	}

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.innerHTML = txt;
	
	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "custom_alert_closeBtn";

	if (btn_txt == undefined) {
		btn_txt = '确定';
	}
	btn.appendChild(d.createTextNode(btn_txt));

	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { removeCustomAlert();return false; }

	//$("#custom_alert_alertBox").modal();
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	bgObj = document.getElementById("custom_alert_bgdiv");
	document.getElementById("custom_alert_div").removeChild(bgObj);
}
