// JavaScript Document
var objLogEmail, objLogPwd, hasTyped;
var objSearchBox, hasSearched;

function initLogin() {
	//The login form:
	objLogEmail = getObject("log_email");
	objLogPwd   = getObject("log_pwd");
	hasTyped = false;
	
	//piggy-back on this to init the search form:
	initSearch();
}

function activateLoginForm() {
	if (hasTyped == false) {
		objLogEmail.style.color = "#000000";
		objLogEmail.value = "";
		objLogPwd.style.color = "#000000";
		objLogPwd.value = "";
		hasTyped = true;
	}
}

function initSearch() {
	objSearchBox = getObject("q");
	hasSearched = false;
}

function activateSearchForm() {
	if (hasSearched == false) {
		objSearchBox.style.color = "#000000";
		objSearchBox.value = "";
		hasSearched = true;
	}
}

//Handles the database-friendly calendar date format (e.g., yyyy-mm-dd) and returns as string for the Dojo calendar date as JavaScript Date object
function ConvertYMD(date,divider) {
	var aryDate = date.split(divider);
	if (aryDate.length != 3) { return ""; }
	return new Date(aryDate[0], aryDate[1]-1, aryDate[2]);
}

function textCounterUpdate(fieldName, maxlimit) {
	var field, txt_field;
	field = getObject( fieldName );
	txt_field = getObject(fieldName+'_char_count');
	txt_field.innerText = maxlimit - field.value.length;
	txt_field.innerHTML = maxlimit - field.value.length;
	if (field.value.length >= maxlimit) { field.value = field.value.substring(0, maxlimit); }
}

function HighLightWords ( objName, objDef, task ) {
	var oSpan = getObject(objName);
	var oDef = getObject(objDef);
	if (oDef == null || oSpan == null) { return; }
	var maxwidth = document.body.clientWidth;
	
	var newLeft, newTop;
	if (task == "over") {
		newLeft = getObjectLeft(oSpan);
		if (maxwidth - newLeft < 200) { newLeft = maxwidth - 210; } //The "definition" div width is 200.
		newTop = getObjectTop(oSpan) + 20;
		oSpan.style.backgroundColor = "#FFFFCC";
		oDef.style.visibility = "visible";
		oDef.style.display = "block";
		oDef.style.left = newLeft + "px";
		oDef.style.top = newTop + "px";
	} else {
		oSpan.style.backgroundColor = "transparent";
		oDef.style.visibility = "hidden";
		oDef.style.display = "none";
		oDef.style.left = "0px";
		oDef.style.top = "0px";
	}
	return;
}

function getObjectLeft ( obj ) {
	var curleft = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	return curleft;
}

function getObjectTop ( obj ) {
	var curtop = 0;
	if (obj.offsetParent) {
		do {
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return curtop;
}

/* Silly "highlight box" effects for showing off DASC initiatives/web sites */
function HighlightBox(obj) {
	obj.style.backgroundColor = "#CFCFCF";
	obj.style.border = "2px solid gray";
}
function UnHighlightBox(obj) {
	obj.style.backgroundColor = "#FFFFFF";
	obj.style.border = "2px solid #CFCFCF";
}

function getObject( sObj ) {
	var oElement;
	if ( document.getElementById ) {
		oElement = document.getElementById( sObj );
	} else if ( document.all ) {
		oElement = document.all.item( sObj );
	} else {
		oElement = null;
	}
	return oElement;
}

