	//global function for calling ids
function id(value){return document.getElementById(value);}
function focusElement(caller){if(id(caller))id(caller).focus()}

function listen(evnt, elem, func){
	if (elem.addEventListener) elem.addEventListener(evnt,func,false);
	else if (elem.attachEvent) {var r = elem.attachEvent("on"+evnt, func); return r;}
}

function imageLoader(){
		//if client's browser supports images
	if (document.images){
			//image folder
		imageFolder = "/static_media/images/";
		imageName = new Array();
			//list of images to preload
		imageName[0] = "input-button_down_bg.png";
		imageName[1] = "help_div_top.jpg";
		imageName[2] = "help_div_bottom.png";
		imageName[3] = "help_div_pointer_top.png";
		imageName[4] = "help_div_L.png";
		imageName[5] = "help_div_R.png";
		imageName[6] = "help_div_TL.png";
		imageName[7] = "help_div_TR.png";
		imageName[8] = "help_div_BL.png";
		imageName[9] = "help_div_BR.png";
		imageName[10] = "help_div_pointer_BR.png";
		imageName[11] = "help_div_pointer_bottom.png";
		for (i=0; i<imageName.length; i++){
				//downloads the images
			preloadImage = new Image();
			preloadImage.src = imageFolder + imageName[i];
		}
	}
}

/* Buttons */

var buttonPressed = false;

function changeButtonBg(caller){
	buttonPressed = true;
	caller.className = "bRev";
	listen("mouseup", document, buttonUp);
	listen("mouseover", caller, function(e){mouseOverButton(caller);});
}

function resetButtonBg(caller){
	caller.className = "buttonMain";
}

function mouseOverButton(caller){
	if(!buttonPressed) caller.className = "buttonMain";
	else caller.className = "bRev";
}

function buttonUp(e){
	buttonPressed = false;
}

/* Buttons end */

/* Help Box */

function showHelp(caller){
	hideHelp();
	var diffTop = 24;
	var diffLeft = 0;
	divId = id("helpDiv");
	divLeft = findObject(divId)[0];
	divTop = findObject(divId)[1];
	callerLeft = findObject(caller)[0];
	callerTop = findObject(caller)[1];
	if ((divId.offsetHeight + callerTop) > getDocHeight()){
		diffTop = divId.offsetHeight - 12 - caller.offsetHeight;
		id("helpDivPointerTop").style.display = "none";
		id("helpDivPointerBR").style.display = "none";
		id("helpDivPointerBottom").style.display = "block";
	}
	if ((divId.offsetWidth + callerLeft + caller.offsetWidth) > getDocWidth()){
		diffLeft = divId.offsetWidth - 45;
		diffTop = divId.offsetHeight + 25;
		id("helpDivPointerTop").style.display = "none";
		id("helpDivPointerBottom").style.display = "none";
		id("helpDivPointerBR").style.display = "block";
	}
	divId.style.left = callerLeft - divLeft + caller.offsetWidth - diffLeft + "px";
	divId.style.top = callerTop + Math.round(caller.offsetHeight / 2) - divTop - diffTop + "px";
	divId.style.visibility = "visible";
}

function hideHelp(clearContent){
	divId = id("helpDiv");
	divId.style.visibility = "hidden";
	id("helpDivPointerBottom").style.display = "none";
	id("helpDivPointerBR").style.display = "none";
	id("helpDivPointerTop").style.display = "block";
	divId.style.left = divId.style.top = 0;
	if (clearContent == "yes"){clearHelp();}
}

function clearHelp(){
	id("helpDivContent").innerHTML = "";
}
	
	//template for elements which have a help text defined
function loadHelpText(caller, helpText){
	id("helpDivContent").innerHTML = helpText;
	showHelp(caller);
}

/* Terms of Use pop-up */

var tou;

function openTermsOfUse(caller){
	var tou = window.open('/terms_sign_up', 'termsSignup', 'width=300, height=200, scrollbars=1');
	var sl = (document.body.scrollLeft)? sl = document.body.scrollLeft : sl = document.documentElement.scrollLeft;
	var st = (document.body.scrollTop)? st = document.body.scrollTop : st = document.documentElement.scrollTop;
	var winX = (document.all)?window.screenLeft:window.screenX;
	var winY = (document.all)?window.screenTop:window.screenY;
	var maxX = screen.width;
	var maxY = screen.height;
	
	var lp = findObject(caller)[0] - sl + winX;
	var tp = findObject(caller)[1] - st - 220 + winY;
	
	if (lp + 350 > maxX) lp = maxX - 350;
	if (tp + 250 > maxY) tp = maxY - 250;
	
	if (lp < 1) lp = 1;
	if (tp < 1) tp = 1;
	
	if (tou){
		tou.moveTo(lp, tp);
		if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) tou.resizeTo(300, 200);
	}
}

/* Terms of Use end */

function getDocHeight(){
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

function getDocWidth(){
	var D = document;
	return Math.max(
		Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
		Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
		Math.max(D.body.clientWidth, D.documentElement.clientWidth)
	);
}

function findObject(obj){
	var posLeft = posTop = 0;
	if (obj.offsetParent) {
		do {
			posLeft += obj.offsetLeft;
			posTop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
	}
	return [posLeft,posTop];
}

/* Help Box end */


/* ajax */

	//ajax request variable
var request;
	//define the request variable based on the client's broswer
function ajaxLoad(){
	try {
			// Firefox, Opera, Safari
		request = new XMLHttpRequest();
	} 
	catch (e) {
			// Internet Explorer
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
		} 
			catch (e) {
				return false;
			}
		}
	}
}

	// value = name of the script requested
	// render = id of element where the response appears
	// caller(optional) = id of element to hide
function ajax(value, render, caller){
	url = "/members/ajax/" + value + "/";
	request.open("GET",url,true);
	request.setRequestHeader("X_REQUESTED_WITH", "XMLHttpRequest");
	request.send(null);
	request.onreadystatechange=function(){
	if(request.readyState==4){
			if (caller){id(caller).style.display = "none";}
			id(render).innerHTML = request.responseText;
		}
	}
}

function ajaxHide(hide,show){
	id(hide).innerHTML = "";
	id(show).style.display = "block";
}

/* ajax end */