var hover_array = new Array();
var moving_div_id = '';
var tempX = 0;
var tempY = 0;
var first_time = true;
var timerid = null;

function getElement(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else if (document.layers) {
 		return document.layers[id];
	}
}

function start_move_div(id) {
	var arr = hover_array[id];
	if (arr[1] == 1 || arr[1] == 2) {
		arr[1] = 0;
		moving_div_id = id;
		tp = arr[0].style.top.split("p");
		arr[2] = tempY - tp[0];
		tp = arr[0].style.left.split("p");
		arr[3] = tempX - tp[0];
		timerid = setTimeout('moving_div_id="";arr[1] = 1;',1000);
	} else {
		clearTimeout(timerid);
		timerid = null;
		arr[1] = 1;
		moving_div_id = '';
	}
}



function move_div(e) {
	if (document.all) {
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {
		tempX = e.pageX;
		tempY = e.pageY;
	}

	if (moving_div_id != '') {
		if (getElement(moving_div_id)) {
			arr = hover_array[moving_div_id];
			arr[0].style.top = (tempY - arr[2]) + 'px';
			arr[0].style.left = (tempX - arr[3]) + 'px';
		} else {
			moving_div_id = '';
		}
	}
}

function makePop_special(topPos,leftPos,id,content,width,height) {
	if (!nopop) {
		if (!getElement(id + '_blanker')) {
			var div = document.createElement('div');
			var div_start = "<div name=\""+id+"_blanker\" id=\""+id+"_blanker\" style=\"filter:alpha(opacity=70);-moz-opacity:.7;opacity:.7; background-color: black; z-index: 2; padding: 0px; border: 0px; position: absolute; top:0px; left: 0px; width: 100%; height: " + document.body.offsetHeight + "px\">";
			div.innerHTML = div_start + "</div>";
			document.body.appendChild(div.firstChild);
			hover_array[id + '_blanker'] = new Array(getElement(id),1,0,0);
		}

		if (!getElement(id)) {
			offset = 0;
			if (window.pageYOffset) {
				offset = window.pageYOffset;
			} else {
				offset = document.body.scrollTop;
			}
			if (offset == 0 && document.documentElement.scrollTop > 0) {
				offset = document.documentElement.scrollTop;
			}
			var topP = 10 + offset; //(screen.availHeight / 3) + (screen.height - screen.availHeight) - (height / 2) + 
			var leftP = (screen.availWidth / 2) - (width / 2);
			var div = document.createElement('div');
			var div_start = "<div id=\""+id+"\" style=\"background-color: white; z-index: 3; padding: 0px; border: 0px; position: absolute; top: " + topP + "px; left: " + leftP + "px;\">";
			div_start += '<table style="border: 1px solid;"><tr><td style="background: #1c71ff; " align="right" id="'+id+'_inner" name="'+id+'_inner" height="22px"><a href="javascript: ;" onclick=" killoff_special(\''+id+'\',true);" class="x"><img src="/images/x.png" border="0" width="22px" height="22px" /></a></td></tr><tr><td><div style="overflow: auto; width: ' + width + 'px; id="' + id + '_text" name="' + id + '_text">';
			div.innerHTML = div_start + content +  "</div></td></tr></table></div>";
			document.body.appendChild(div.firstChild);

			title_id = id+'_inner';
			title = getElement(title_id);
			title.onmousedown = function() {start_move_div(id);};
			title.onmouseup = function() {start_move_div(id);};

			hover_array[id] = new Array(getElement(id),1,0,0);
		}
	}
}

function makePop(topPos,leftPos,id,content,width,height) {
	var div = document.createElement('div');
	var div_start = "<div id=\""+id+"\" style=\"background-color: white; z-index: 2; padding: 0px; border: 2px solid blank; position: absolute; top: " + topPos + "px; left: " + leftPos + "px;\">";
	div_start += '<table style="border: 1px solid;"><tr><td style="background: #1c71ff; " align="right" id="'+id+'_inner" name="'+id+'_inner" height="22px"><a href="javascript: ;" onclick="killoff(\''+id+'\',true);" class="x"><img src="/images/x.png" border="0" width="22px" height="22px" /></a></td></tr><tr><td><div style="overflow: auto; width: ' + width + 'px; height: ' + height + 'px;" id="' + id + '_text" name="' + id + '_text">';
	div.innerHTML = div_start + content + "</div></td></tr></table></div>";
	if (!getElement(id)) {
		document.body.appendChild(div.firstChild);

		title_id = id+'_inner';
		title = getElement(title_id);
		title.onmousedown = function() {start_move_div(id);};
		title.onmouseup = function() {start_move_div(id);};

		hover_array[id] = new Array(getElement(id),1,0,0);
	}
}

function isactive(id) {
	var tmp = hover_array[id];
	tmp[1] = 2;
}

function preview(id,content,topPos,leftPos,width,height) {
	var width = (width == null) ? 660 : width ;
	var height = (height == null) ? 400 : height ;
	var topPos = (topPos == null) ? (document.body.clientHeight / 2) - (height / 2) : topPos ;
	var leftPos = (leftPos == null) ? (document.body.clientWidth / 2) - (width / 2) : leftPos ;

	makePop(topPos,leftPos,id,content,width,height);
}


function killoff_special(id) {
        
        document.getElementById(id).innerHTML = '';
	document.body.removeChild(getElement(id));
	hover_array[id] = null;
	if (moving_div_id == id) {
		moving_div_id = '';
	}
	document.body.removeChild(getElement(id+'_blanker'));
}

function killoff(id,other) {
	var tmp = hover_array[id];
	if (tmp[1] == 0 || other == true) {
		document.body.removeChild(getElement(id));
		title_id = id+'_inner';
		title = getElement(title_id);
		if (title) {
			title.onmousedown = function() {};
			title.onmouseup = function() {};
		}
		hover_array[id] = null;
		if (moving_div_id == id) {
			moving_div_id = '';
		}
	}
}


function allow_popups() {
document.onmousemove = move_div;
}
setTimeout('activate_popups()',200);

function activate_popups() {
	}

function focus_text(elm) {
	if (elm.value == '<optional>') {
		elm.value = '';
	}
}

function unfocus_text(elm) {
	if (elm.value == '') {
		elm.value = '<optional>';
	}
}

var nopop = false;

function toggle_exitpop(num,max) {
	var pp = 3;
	for (i=1;i <= max; i++) {
		if (getElement('reason' + i)) {
			if (i == num) {
				getElement('reason' + num).checked = true;
				getElement('reason' + num + '_inner').style.display = '';
			} else {
				getElement('reason' + i + '_inner').style.display = 'none';
			}
		}
	}
	if (pp == num) {
		getElement('pp').style.display = '';
	} else {
		getElement('pp').style.display = 'none';
	}
}