/* --------------------------------------------------------------
	GLOBAL VARIABLES
-------------------------------------------------------------- */
var tipDiv = null;
var dropTxtBox = null;
var dropBox = null;
var dropBtn = null;
var dropState = false;
var dropTimer = null;

/* --------------------------------------------------------------
	GLOBAL EVENT REGISTRATION
-------------------------------------------------------------- */


/* --------------------------------------------------------------
	PRELOAD TABS
-------------------------------------------------------------- */
var i1 = new Array();
i1[0] = new Image();
i1[0].src = "/i/new/gfx/newHeader/welcome-on.gif";
i1[1] = new Image();
i1[1].src = "/i/new/gfx/newHeader/aboutme-on.gif";
i1[2] = new Image();
i1[2].src = "/i/new/gfx/newHeader/writereview-on.gif";
i1[3] = new Image();
i1[3].src = "/i/new/gfx/newHeader/findreview-on.gif";
i1[4] = new Image();
i1[4].src = "/i/new/gfx/newHeader/invite-on.gif";
i1[5] = new Image();
i1[5].src = "/i/new/gfx/newHeader/messaging-on.gif";
i1[6] = new Image();
i1[6].src = "/i/new/gfx/newHeader/talk-on.gif";

//chain onload
function chainOnload(f) {
	var prev = window.onload;
	window.onload = function() {
		if (prev) {
			prev();
		}
		f();
	}
}

function chainOnunload(f) {
	var prev = window.onunload;
	window.onunload = function() {
		if (prev) {
			prev();
		}
		f();
	}
}

/* --------------------------------------------------------------
	Generic image swapping function
	img_id - the id attribute of the image to be swapped
	img_src - the path to the new image to be swapped in
-------------------------------------------------------------- */
function swap(imgId, imgSrc) {
	var imgObj = document.getElementById(imgId);
	imgObj.src = imgSrc;
}

/* --------------------------------------------------------------
	Function to find cursor location on click
-------------------------------------------------------------- */

function findPosition( oLink ) {

  if( oLink.offsetParent ) {

    for( posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {

      posX += oLink.offsetLeft;

      posY += oLink.offsetTop;

    }

    return [ posX, posY ];

  } else {

    posX = oLink.x;
	posY = oLink.y;
	return [ posX, posY ];

  }

}

/* --------------------------------------------------------------
	Character counting for textareas which cannot take maxlength
-------------------------------------------------------------- */
function countChars(fieldname,limit) {
	var num = fieldname.value.length;
	if (num > limit) {
		alert('We\'re sorry, you may only write up to ' + limit + ' characters in this field.');
		fieldname.value = fieldname.value.substring(0, limit-1);
	} else {
		//be idle
	}
	return;
}

/* --------------------------------------------------------------
	Sets the browser's location
-------------------------------------------------------------- */
function set_location(url)
{
	document.location.href = url;
}

/* --------------------------------------------------------------
	Functions for the location drop down
	moveDropper() - positions the dropper beneath the text field
		with which it is currently associated
	showDropper() - turns the rendering on the dropper on
		buttonTarget - the div containing the button image
		textTarget - the text box to fill with an address
	hideDropper() - turns the rendering of the dropper off
	dropperFill() - fills the text box from the global var
		dropTxtBox with the string str
-------------------------------------------------------------- */
function moveDropper(buttonTarget) {
	if (dropState) {
		switch(buttonTarget){
		case 'dropperBtn_Mast':
			var headerLeft = document.getElementById('header_find_form').offsetLeft;
			dropBox.style.left = ((dropBtn.offsetLeft+headerLeft) - 154) + "px";
			dropBox.style.top = (dropBtn.offsetTop + 21) + "px";
			break;
		case 'dropperBtn_ABS':
			dropBox.style.left = (dropBtn.offsetLeft - 154) + "px";
			dropBox.style.top = (dropBtn.offsetTop + 21) + "px";
			break;
		case 'dropperBtn_Review':
			dropBox.style.left = (dropBtn.offsetLeft - 154) + "px";
			dropBox.style.top = (dropBtn.offsetTop + 21) + "px";		
			break;
		case 'dropperBtn_SB':
			dropBox.style.left = (dropBtn.offsetLeft - 154) + "px";
			dropBox.style.top = (dropBtn.offsetTop + 21) + "px";		
			break;
		default:
			dropBox.style.left = (dropBtn.offsetLeft - 154) + "px";
			dropBox.style.top = (dropBtn.offsetTop + 34) + "px";
		}		
	}
	return;
}

function doDropper(buttonTarget, textTarget) {
	if (dropState) {
		if (dropBtn.id != buttonTarget) {
			hideDropper();
			showDropper(buttonTarget, textTarget);
		} else {
			hideDropper();
		}
	} else {
		showDropper(buttonTarget, textTarget);
	}
}

function showDropper(buttonTarget, textTarget) {
	dropTxtBox = document.getElementById(textTarget);
	dropBox = document.getElementById('dropper');
	dropBtn = document.getElementById(buttonTarget);
	dropState = true;
	moveDropper(buttonTarget);
	dropBox.style.display = 'block';
	dropBtn.src = "/i/new/gfx/dropper_up_on.gif";
	return;
}

function hideDropper() {
	dropState = false;
	clearDropperTimer();
	dropBox.style.display = 'none';
	dropBtn.src = "/i/new/gfx/dropper_down.gif";
	if (dropTxtBox) {dropTxtBox = null;}
	if (dropBox) {dropBox = null;}
	if (dropBtn) {dropBtn = null;}
	return;
}

function swapDropperIn(buttonTarget) {
	var btn = document.getElementById(buttonTarget);
	var btnStr = btn.src;
	if (btnStr.indexOf('dropper_down.gif') > 0) {
		swap(buttonTarget, '/i/new/gfx/dropper_down_on.gif');
	} else if (btnStr.indexOf('dropper_up.gif') > 0) {
		swap(buttonTarget, '/i/new/gfx/dropper_up_on.gif');
	}
}

function swapDropperOut(buttonTarget) {
	var btn = document.getElementById(buttonTarget);
	var btnStr = btn.src;
	if (btnStr.indexOf('dropper_down_on.gif') > 0) {
        swap(buttonTarget, '/i/new/gfx/dropper_down.gif');
	} else if (btnStr.indexOf('dropper_up_on.gif') > 0) {
        swap(buttonTarget, '/i/new/gfx/dropper_up.gif');
	}
}

function setDropperTimer() {
	if (dropState) {
		dropTimer = setTimeout("hideDropper()", 1500);
	}
}

function clearDropperTimer() {
	clearTimeout(dropTimer);
	dropTimer = null;
}

function dropperFill(str) {
	dropTxtBox.value = str;
	return;
}

function getDropperState() {
	return(dropState);
}

//standards compliant window target script
function externalLinks(theAnchor,windowName) {
var theName = "";
if(windowName){
theName = windowName;
}else{
theName = "_blank";
}
theAnchor.target = theName; 
}

function printSwitch(pageUrl){

var newWindow = window.open("","printerWindow","menubar=yes,toolbar=no,location=no,resizable=yes,scrollbars=yes,status=no");
pageUrl = pageUrl+'';
var re = new RegExp("=");

	if(pageUrl.match(re)){
		newWindow.location = pageUrl + '&printLayout=true';
	}else{
		newWindow.location = pageUrl + '?printLayout=true';
	}

}

function xmlhttpreq_encode_dict(dict){ 
	var result_string = "";
	var start = true;
	for (var i in dict){ 
		if (!start) {result_string += "&";}
		else start = false;
		result_string += encodeURIComponent(i) + "=" + encodeURIComponent(dict[i]);
	}
	return result_string; 
}
