var box;
var diffX;
var diffY;
function startMove(e,boxid) {
	var w = window.document;
	if(document.all) {
		w.attachEvent("onmousemove", moveBox);
		w.attachEvent("onmouseup", stopMove);
	} else {
		w.onmousemove = moveBox;
		w.onmouseup = stopMove;
	}
	box = w.getElementById(boxid);
	diffX = e.clientX - box.offsetLeft;
	diffY = e.clientY - box.offsetTop;
}

function moveBox(e) {
	box.style.left = e.clientX - diffX + "px";
	box.style.top = e.clientY - diffY + "px";
}

function stopMove(e) {
	var w = window.document;
	if(document.all) {
		w.detachEvent("onmousemove", moveBox);
		w.detachEvent("onmouseup", stopMove);
	} else {
		w.onmousemove = null;
		w.onmouseup = null;
	}
}

function getRealTop(el) {
	var yPos = el.offsetTop;
	var tempEl = el.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

function getRealLeft(el) {
	var xPos = el.offsetLeft;
	var tempEl = el.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}


function showSynopsis(e,text,close) {
	var d = window.document;
	var b = d.getElementById('detailbox');
	var t = d.getElementById('detailtext');
	if(close) {
		b.style.display = 'none';
	}
	else if(text != '') {
		t.innerHTML = decode64(text);
		var newLeft = getRealLeft(e) + e.offsetWidth * 1.3;
		var newTop  = getRealTop(e) - e.offsetHeight;
		var maxWidth = document.body.offsetWidth;

		if(newLeft + b.offsetWidth > maxWidth)
			newLeft = getRealLeft(e) - (b.offsetWidth - maxWidth);
		b.style.left = newLeft + "px";
		b.style.top  = newTop + "px";

		b.style.display = 'block';
	}
}


function register(id,t,d,l,h,c,p,nf) {
   if(!p) {
//      window.location.href = id;
		window.open(id,'partnerlink');
	}
   else {
		var url = window.location.href.toString();
		if(!url.match(/^https:/))
	           url = url.replace(/http:/,'https:');
                if(id.substr(0,1) == 'H') //Interpreting Coaching Report trainings
   		   url = url.replace(/coachingreport.php/,'registerForm.php');
                else if(id.substr(0,2) == 'LC')  //For learning partners, no payment needed
		   url = url.replace(/learningpartnerTraining.php/,'registerForm_noPayment.php');
                else
		   url = url.replace(/certificationTraining.php/,'registerForm.php');

                //the page for learning partners inside US
                if(url.match(/learningpartner/))
                   url = url.replace(/learningpartnerTraining.php/,'registerForm.php');

//		var w = window.open('','courses','toolbar=yes,scrollbars=yes,resizable=yes,width=750,height=500');
//		var f = createForm('signup','post','register_form.php','courses');
		var f = createForm('signup','post',url,'');
		createInput('signup','id','hidden',id);
		createInput('signup','topic','hidden',t);
		createInput('signup','date','hidden',d);
		createInput('signup','location','hidden',l);
		createInput('signup','host','hidden',h);
		createInput('signup','credits','hidden',c);
		createInput('signup','price','hidden',p);
		createInput('signup','nofee','hidden',nf);
		submitForm('signup');
	}
}

function createForm(name,method,action,target) {
	var e = window.document.createElement('form');
	e.method = method;
	e.action = action;
	e.target = target;
	e.name = name;
	window.document.body.appendChild(e);
	return true;
}

function createInput(form,name,type,value) {
	var n = window.document.createElement('input');
	n.type = type;
	n.name = name;
	n.value = value;

	var f = findForm(form);
	f.appendChild(n);
	return true;
}

function submitForm(name) {
	var f = findForm(name);
	f.submit();
	return true;
}

// IE won't allow access to form by stringkey when its dynamically created
function findForm(name) {
	var i = 0;
	var f = window.document.forms;
	var l = f.length;
	do {
		if(f[i].name == name)
			return f[i];
	} while (++i < l);
	alert('Error - no form by name ['+name+'] in document');
	return false;
}

// this function is designed to change the background color
// to a red when its id is passed in via argument. Any number
// of id's can be passed in.
function errorFields() {
	var e = null;
	var w = window.document;
	var len1 = arguments.length;
	var len2 = w.forms.length;
	for (var i=0; i<len1; i++) {
		for(var n=0; n<len2; n++) {
			e = w.forms[n].elements[arguments[i]];
			if(e) {
				if(e.name)
					e.style.backgroundColor = '#ffaaaa';
				else {
					for(v=0; v<e.length; v++)
						e[v].parentNode.style.backgroundColor = '#ffaaaa';
				}
			}
		}
	}
}

