var popin_onload_function = '';
var trialUrl = '/trial';
var browserW = 0;
var browserH = 0;
var frameid = 'popinwin';
var win_posx = 0;
var win_posy = 0;
var popin_onclose_function = '';
var curr_message_win_id = '';
var closebarid = 'popin_closebar';
var uri_info;

var IE = document.all ? true : false;
if (!IE) { document.captureEvents(Event.MOUSEMOVE); }

// Temporary variables to hold mouse x-y pos.s
var mouseX = 0;
var mouseY = 0;

// Browser detection
var xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xIE6,xNN4,xUA=navigator.userAgent.toLowerCase();
if (window.opera) {
  	var i_opera=xUA.indexOf('opera');
  	if (i_opera != -1) {
    		var v=parseInt(xUA.charAt(i_opera + 6));
    		xOp7Up=v>=7;
    		xOp6Dn=v<7;
  	}
} else if (navigator.vendor!='KDE' && document.all && xUA.indexOf('msie')!=-1) {
  	xIE4Up=parseFloat(navigator.appVersion) >= 4;
  	xIE4=xUA.indexOf('msie 4') != -1;
  	xIE5=xUA.indexOf('msie 5') != -1;
  	xIE6=xUA.indexOf('msie 6') != -1;
} else if (document.layers) {
	xNN4=true;
}
var xMac = xUA.indexOf('mac')!=-1;

function createPopInWindow(newid) {
	var ifwin = document.getElementById(newid);
	// Check to see if the element exists
	if (ifwin) { return; }
	// Create the new element
	var newwin = document.createElement('iframe');
	newwin.id = newid;
	newwin.className = 'popinwindow';
	newwin.style.display = 'none';
	newwin.frameBorder = '0';
	newwin.marginHeight = '0';
	newwin.marginWidth = '0';
	newwin.scrolling = 'auto';
	newwin.style.position = 'absolute';
	newwin.style.zIndex = 10000;
	newwin.style.border = '1px solid #000';
	newwin.style.borderCollapse = 'collapse';
	newwin.style.margin = '0';
	newwin.style.padding = '0';
	// Append the element to the body
	var mybody = document.getElementsByTagName("body")[0];
	mybody.appendChild(newwin);
}

function createCloseBar(w,x,y,inner) {
	var closebar = document.getElementById(closebarid);
	// Check to see if the element exists
	if (!closebar) {
		// Create the new element
		closebar = document.createElement('div');
		closebar.id = closebarid;
		closebar.className = 'popinwindow_closebar';
		closebar.style.display = 'none';
		closebar.style.position = 'absolute';
		closebar.style.zIndex = '10000';
		closebar.style.margin = '0';
		closebar.style.border = '1px solid #000';
		closebar.style.textAlign = 'right';
		// Append the element to the body
		var mybody = document.getElementsByTagName("body")[0];
		mybody.appendChild(closebar);
	}

	var height = 20;
	var extra = 5;
	if (IE) { w += 2; }
	closebar.style.width = w + 'px';
	closebar.style.height = height + 'px';
	closebar.style.top = (y - height - extra) + 'px';
	closebar.style.left = x + 'px';
	if (inner) {
		closebar.innerHTML = inner;
	} else {
		closebar.innerHTML = '<a href="#" onClick="return hidePopIn();">close</a>';
	}
	closebar.style.display = 'block';
}

function pingChild(child_id) {
	var c = document.getElementById(child_id);
	if (!c) { return false; }
	try { return c.document.acceptPingFromParent(); }
	catch (err) { return false; }
}

function showFrame(frame,url,w,h,x,y) {
	var uparts = url.split('?');
	var dimensions = 'framew=' + w + '&frameh=' + h;
	var qs = (uparts.length > 1) ? uparts[1] + '&' + dimensions : dimensions;
	frame.src = uparts[0] + '?' + qs;
	if (popin_onload_function !== '') { frame.onload = popin_onload_function; }
	frame.style.height = h + 'px';
	frame.style.width = w + 'px';
	frame.style.left = (x > -1) ? x + 'px' : '0px';
	frame.style.top = (y > -1) ? y + 'px' : '0px';
	frame.style.display = 'block';
	if (!pingChild(frameid)) { createCloseBar(w,x,y); }
	return false;
}

function xDef() {
  	for(var m=0; m<arguments.length; ++m) {
  		if(typeof(arguments[m])==='undefined') { return false; }
  	}
  	return true;
}

function xNum() {
  	for (var i=0; i<arguments.length; ++i) {
  		if(isNaN(arguments[i]) || typeof(arguments[i])!='number') { return false; }
  	}
  	return true;
}

function xGetElementById(e) {
  	if (typeof(e)!='string') { return e; }
  	if (document.getElementById) {
  		e = document.getElementById(e);
  	} else if (document.all) {
  		e = document.all[e];
  	} else {
  		e = null;
  	}
  	return e;
}

function xScrollLeft(e, bWin) {
  	var offset=0;
  	if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    		var w = window;
    		if (bWin && e) { w = e; }
    		if(w.document.documentElement && w.document.documentElement.scrollLeft) {
    			offset=w.document.documentElement.scrollLeft;
    		} else if(w.document.body && xDef(w.document.body.scrollLeft)) {
    			offset=w.document.body.scrollLeft;
  		}
  	} else {
    		e = xGetElementById(e);
    		if (e && xNum(e.scrollLeft)) { offset = e.scrollLeft; }
  	}
  	return offset;
}

function xScrollTop(e, bWin) {
  	var offset=0;
  	if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    		var w = window;
    		if (bWin && e) { w = e; }
    		if(w.document.documentElement && w.document.documentElement.scrollTop) {
    			offset=w.document.documentElement.scrollTop;
    		} else if(w.document.body && xDef(w.document.body.scrollTop)) {
    			offset=w.document.body.scrollTop;
    		}
  	} else {
    		e = xGetElementById(e);
    		if (e && xNum(e.scrollTop)) { offset = e.scrollTop; }
  	}
  	return offset;
}

function xClientHeight() {
  	var h=0;
  	if (xOp6Dn) {
  		h = window.innerHeight;
  	} else if (document.compatMode == 'CSS1Compat' && !window.opera && document.documentElement && document.documentElement.clientHeight) {
    		h = document.documentElement.clientHeight;
  	} else if (document.body && document.body.clientHeight) {
    		h = document.body.clientHeight;
  	} else if (xDef(window.innerWidth,window.innerHeight,document.width)) {
    		h = window.innerHeight;
     		if (document.width>window.innerWidth) { h -= 16; }
  	}
  	return h;
}

function xClientWidth() {
  	var w=0;
  	if (xOp6Dn) {
  		w = window.innerWidth;
  	} else if (document.compatMode == 'CSS1Compat' && !window.opera && document.documentElement && document.documentElement.clientWidth) {
  		w = document.documentElement.clientWidth;
  	} else if (document.body && document.body.clientWidth) {
    		w = document.body.clientWidth;
  	} else if (xDef(window.innerWidth,window.innerHeight,document.height)) {
    		w = window.innerWidth;
    		if (document.height>window.innerHeight) { w -= 16; }
  	}
  	return w;
}

function setBrowserDimensions() {
	if (document.body) {
		browserW = xClientWidth();
		browserH = xClientHeight();
		return true;
	}
	return false;
}

function setCoordinates(w,h) {
	if (setBrowserDimensions()) {
		win_posx = (browserW < (mouseX + w)) ? mouseX-(Math.floor(w * (3/4))) : mouseX-(Math.floor(w/4));
		if (browserH < (mouseY + h)) {
			win_posy = mouseY-(Math.floor(h * (3/4)));
		} else {
			win_posy = mouseY-(Math.floor(h/4));
		}
	} else {
		win_posx = mouseX-(Math.floor(w/4));
		win_posy = mouseY-(Math.floor(h/4));
	}
	if (win_posx < 0) { win_posx = mouseX; }
	if (win_posy < 0) { win_posy = mouseY; }
}

function showPopIn(url,w,h) {
	createPopInWindow(frameid);
	var myframe = document.getElementById(frameid);
	if (!myframe) { return false; }
	setCoordinates(w,h);
	return showFrame(myframe,url,w,h,win_posx,win_posy);
}

function popupLivePerson(type) {
	// remove the ability to access live person
	document.location = '/members/support/transfer.php?p_next_page=live.php';
	return false;
}

function popupWin(url, name, w, h, return_pointer) {
	var prefs = 'left=50,top=5,toolbar=No,location=No,scrollbars=Yes,status=No,resizable=Yes,fullscreen=No';
	var win = window.open(url,name,'width='+w+',height='+h+','+prefs);
	if (win) { win.focus(); }
	
	if (return_pointer) {
		return win;
	}
	
	return false;
}

function openChat() {
	document.location = '/members/support/transfer.php?p_next_page=chat.php';
	return false;
}

function openHelp() {
	document.location = '/members/support/transfer.php?p_next_page=std_alp.php';
	return false;
}

function openAsk() {
	document.location = '/members/support/transfer.php?p_next_page=ask.php';
	return false;
}

function showLoginWin() {
	return showPopIn('/members/login.php?type=popin',350,270);
}

function popupChat() {
	return popupLivePerson('base');
}

function popupSignupChat() {
	var url = 'http://livechat.boldchat.com/aid/6243414089742659024/bc.chat?url=' + escape(document.location.href);
	return popupWin(url,'boldchat',640,480);
}

function statusWindow ( status ) {
	return popupWin('/members/order/status_definitions.php#'+status, 'statuses', 400, 400);
}

function setTrialUrl(url) {
	if (url !== '') { trialUrl = url; }
}

function getTrialUrl() {
	return trialUrl;
}

function registerPopInOnCloseFunction(fun) {
	popin_onclose_function = fun;
}

function showFixedPosPopIn(url,w,h,x,y) {
	createPopInWindow(frameid);
	var myframe = document.getElementById(frameid);
	if (!myframe) { return false; }
	return showFrame(myframe,url,w,h,x,y);
}

function getSizeFromStyle(st) {
	var s = st.split('px');
	return s[0];
}

function hideMessageWin() {
	if (curr_message_win_id !== '') {
		var owin = document.getElementById(curr_message_win_id);
		if (owin) { owin.style.display = 'none'; }
		curr_message_win_id = '';
		var cwin = document.getElementById('hide_message_win');
		if (cwin) { cwin.style.display = 'none'; }
	}
	return false;
}

function showDivWin(did, w, h, x, y) {
	hideMessageWin();
	var mwin = document.getElementById(did);
	if (mwin) {
		var cwin = document.getElementById('hide_message_win');
		if (cwin) {
			cwin.style.display = 'block';
			cwin.style.left = (x + w - 47) + 'px';
			cwin.style.top = (y + 2) + 'px';
		}
		mwin.style.display = 'block';
		mwin.style.left = x + 'px';
		mwin.style.top = y + 'px';
		mwin.style.width = w + 'px';
		mwin.style.height = h + 'px';
		curr_message_win_id = did;
	}
	return false;
}

function showMessageWin(sid, w, h) {
	setCoordinates(w,h);
	return showDivWin(sid, w, h, win_posx, win_posy);
}

function showFixedPosMessageWin(sid, w, h, x, y) {
	return showDivWin(sid, w, h, x, y);
}

function registerPopInOnLoadFunction(fun) {
	popin_onload_function = fun;
}

function hideCloseBar() {
	var cb = document.getElementById(closebarid);
	if (cb) { cb.style.display = 'none'; }
}

function testAccess() {
	// Run this function from a child window to test
	// access between the windows.
	// If the child can access this function, we need
	// to hide the closebar that we may have created
	hideCloseBar();
	return true;
}

function getUrlInfo() {
	var uri = new Object();
	uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
	uri.dom = uri.dir;
	if (uri.dom.substr(0,7) == 'http:\/\/') { uri.dom = uri.dom.substr(7); }
	uri.path = '';
	var pos = uri.dom.indexOf('\/');
	if (pos > -1) {
		uri.path = uri.dom.substr(pos+1);
		uri.dom = uri.dom.substr(0,pos);
	}
	uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
	pos = uri.page.indexOf('?');
	if (pos > -1) { uri.page = uri.page.substring(0, pos); }
	pos = uri.page.indexOf('#');
	if (pos > -1) { uri.page = uri.page.substring(0, pos); }
	uri.ext = '';
	pos = uri.page.indexOf('.');
	if (pos > -1) {
		uri.ext = uri.page.substring(pos+1);
		uri.page = uri.page.substr(0,pos);
	}
	uri.file = uri.page;
	if (uri.ext !== '') { uri.file += '.' + uri.ext; }
	if (uri.file === '') { uri.page = 'index'; }
	uri.args = location.search.substr(1).split("?");
	return uri;
}

function hidePopIn() {
	var myframe = document.getElementById(frameid);

	if (myframe) {
		myframe.style.display = 'none';
		if (uri_info === undefined) { uri_info = getUrlInfo(); }
		var prefix = (uri_info.dom.indexOf('doba.com') > -1) ? 'https://' + uri_info.dom : 'https://www.doba.com';
		myframe.src = prefix + '/blank.htm';
	}
	if (popin_onclose_function !== '') { popin_onclose_function.call(); }
	// hide the close bar if it exists
	hideCloseBar();
	return false;
}

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
  	if (IE) { // grab the x-y pos.s if browser is IE
  		var mybody = document.getElementsByTagName("body")[0];
    		mouseX = event.clientX + xScrollLeft();
    		mouseY = event.clientY + xScrollTop();
  	} else {  // grab the x-y pos.s if browser is NS
    		mouseX = e.pageX;
    		mouseY = e.pageY;
  	}
  	// catch possible negative values in NS4
  	if (mouseX < 0) { mouseX = 0; }
  	if (mouseY < 0){ mouseY = 0; }
  	return true;
}

var w3c = (document.getElementById) ? true : false;
var ie = (document.all) ? true : false;
var N = -1;

function getScrolledHorizontally(){
	//how much the page has scrolled horizontally
	var x;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
	}
	
	//alert("scrolled horizontally:" + x);
	return x;
}

function getInnerWindowWidth(){
	//inner dimensions of the window
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
	}
	
	//alert("inner width:" + x);
	return x;
}

function getPageWidth(){
	//width of the total page
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		x = document.body.scrollWidth;
	}
	else // Explorer Mac;
	     //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		x = document.body.offsetWidth;
	}
	
	//alert("page width:" + x);
	return x;
}

function getMiddleWidth(){
	
	var scrolledDistance = getScrolledHorizontally();
			
	var windowWidth = getInnerWindowWidth();

	var documentWidth = getPageWidth();
	
	if(documentWidth > windowWidth){
		return scrolledDistance + (windowWidth / 2)		
	}else{
		return scrolledDistance + (documentWidth / 2)	
	}
}

function getScrolledVertically(){
	//how much the page has scrolled vertically
	var y;
	if (self.pageYOffset) // all except Explorer
	{
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		y = document.body.scrollTop;
	}
	
	//alert("scrolled vertically:" + y);
	return y;
}

function getInnerWindowHeight(){
	//inner dimensions of the window
	var y;
	if (self.innerHeight) // all except Explorer
	{
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		y = document.body.clientHeight;
	}
	
	//alert("inner height:" + y);
	return y;
}

function getPageHeight(){
	//height of the total page
	var y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		y = document.body.scrollHeight;
	}
	else // Explorer Mac;
	     //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		y = document.body.offsetHeight;
	}
	
	//alert("page height:" + y);
	return y;
}

function getMiddleHeight(not_scrolled){
	//how much the page has scrolled vertically
	var scrolledDistance = getScrolledVertically();
			
	//inner dimensions of the window
	var windowHeight = getInnerWindowHeight();

	//height of the total page
	var documentHeight = getPageHeight();
	
	var h = 0;
	if(documentHeight > windowHeight){
		h = windowHeight / 2;		
	}else{
		h = documentHeight / 2;	
	}
	
	return (not_scrolled) ? h : scrolledDistance + h;
}

var message_box_displaying_id = '';
var message_box_popin_id = 'message_box_popin';
var message_box_popin_main_id = 'message_box_popin_main';
var message_box_popin_content_id = 'message_box_popin_content';
var message_box_timeout = false;
var message_box_class = 'norm';

function showMessageBoxPopin( content_container_id, dont_use_timeout, box_class ) {
	if (!content_container_id || content_container_id == '') {
		return false;
	}
	
	if (box_class) {
		message_box_class = box_class;
	}
	
	createMessageBoxPopin();
	
	var content = document.getElementById(content_container_id);
	var box = document.getElementById(message_box_popin_id);
	var main = document.getElementById(message_box_popin_main_id);
	var dest = document.getElementById(message_box_popin_content_id);
	
	if (content && box && dest && main) {
		dest.innerHTML = content.innerHTML;
		box.style.display = 'none';
		message_box_displaying_id = content_container_id;
		if (message_box_timeout) {
			clearTimeout(message_box_timeout);
		}
		if (dont_use_timeout) {
			doMessageBoxDisplay();
		} else {
			message_box_timeout = setTimeout('doMessageBoxDisplay()', 500);
		}
	}
	
	return false;
}

function doMessageBoxDisplay() {
	var box = document.getElementById(message_box_popin_id);
	
	if (box) {
		box.style.top = (mouseY * 1 + 10) + 'px';
		box.style.left = (mouseX * 1 + 25) + 'px';
		box.style.display = 'block';
	}
	
	message_box_timeout = false;
}

function doMessageBoxHide() {
	var box = document.getElementById(message_box_popin_id);
	
	if (box) {
		box.style.display = 'none';
		var content = document.getElementById(message_box_popin_content_id);
		if (content) {
			content.innerHTML = '';
		}
		message_box_displaying_id = '';
	}
	
	message_box_timeout = false;
}

function hideMessageBoxPopin() {
	if (message_box_displaying_id != '') {
		if (message_box_timeout) {
			clearTimeout(message_box_timeout);
		}
		message_box_timeout = setTimeout('doMessageBoxHide()', 1000);
	}
	
	
	return false;
}

function createMessageBoxPopin() {
	var box = document.getElementById(message_box_popin_id);
	
	if (!box) {
		box = document.createElement('div');
		box.id = message_box_popin_id;
		box.className = message_box_class;
		var e = document.createElement('div');
		e.id = message_box_popin_main_id;
		var c = document.createElement('div');
		c.className = 'box_image_top';
		e.appendChild(c);
		var d = document.createElement('div');
		d.className = 'box_image_bottom';
		e.appendChild(d);
		var f = document.createElement('div');
		f.id = message_box_popin_content_id;
		e.appendChild(f);
		box.appendChild(e);
		
		var mybody = document.getElementsByTagName("body")[0];
		mybody.appendChild(box);
		
		return true;
	} else if (box.className != message_box_class) {
		box.className = message_box_class;
	}
	
	return false;
}

var light_box_displaying_id = '';
function showLightBox( content_container_id, w, h ){
	if (!content_container_id || content_container_id == '') {
		return false;
	}
	
	var content = document.getElementById(content_container_id);
	if (!content) {
		return false;
	}
	
	createLightBoxBarrier();

	var myMsg = document.getElementById('lightbox_msg');
	var myBg = document.getElementById('lightbox_bg');

	//get center of scrolled screen from get_center.js
	var middleHeight = getMiddleHeight((!xIE6));

	//get middle of scrolled screen from get_center.js
	var middleWidth = getMiddleWidth();

	//middle height of the page - half the height
	myBg.style.top = (middleHeight - (h / 2)) + "px";

	//middle height of the page - half the width
	myBg.style.left = (middleWidth - (w / 2)) + "px";

	//go to the top of the background then middle of background minus half of the progress
	myMsg.style.top = ((middleHeight - (h / 2)) + 25) + "px";

	//go to the left of the background then middle of background minus half of the progress
	myMsg.style.left = ((middleWidth - (w / 2)) + 25) + "px";
	
	var wrapper = document.createElement('div');
	wrapper.id = 'lightbox_msg_wrapper';
	wrapper.innerHTML = content.innerHTML + createLightBoxCloseLink();
	
	myMsg.innerHTML = '';
	myMsg.appendChild(wrapper);
	
	myMsg.style.display = 'block';
	myBg.style.display = 'block';
	myMsg.style.width = (w - 50) + 'px';
	myBg.style.width = w + 'px';
	myMsg.style.height = (h - 50) + 'px';
	myBg.style.height = h + 'px';
	
	return false;
}

function createLightBoxCloseLink() {
	return '<a href="#" id="lightbox_close_link" onClick="return hideLightBox()">close</a>';
}

function hideLightBox() {
	var myMsg = document.getElementById('lightbox_msg');
	var myBg = document.getElementById('lightbox_bg');
	var bar = document.getElementById('blockade');
	
	var myParent = bar.parentNode;
	myParent.removeChild(bar);
	
	myMsg.style.display = 'none';
	myBg.style.display = 'none';
	
	return false;
}

// This function will build the progress bar box and background elements
// and append them onto the end of the body content
function buildLightBoxElements() {
	try {
		var myMsg = document.createElement('p');
		myMsg.id = 'lightbox_msg';
		
		var myBg = document.createElement('p');
		myBg.id = 'lightbox_bg';
		
		// Append the element to the body
		var mybody = document.getElementsByTagName("body")[0];
		mybody.appendChild(myMsg);
		mybody.appendChild(myBg);
	}
	catch (err) {
		//displayErrorMsg("buildPBarElements()",err);
	}
}

function createLightBoxBarrier() {
	// Create the new element
	var newwin = document.createElement('div');
	newwin.id = 'blockade';
	newwin.style.width = getPageWidth() + "px";
	var page_height = getPageHeight(); // + 40;
	var page_inner_height = getInnerWindowHeight(); // + 40;
	if(page_height < page_inner_height){
		newwin.style.height = page_inner_height + "px";	
	}else{
		newwin.style.height = page_height + "px";	
	}

	// Append the element to the body
	var mybody = document.getElementsByTagName("body")[0];
	mybody.appendChild(newwin);
}

document.onmousemove = getMouseXY;

// get info about the current url
uri_info = getUrlInfo();