// functions.js

// opens a page in current window
// used by a few
function change_Location(url)
{
	window.location=url
}

function reload_image()
{
	document.getElementById('captcha').src = "includes/securimage_show.php?" + Math.random();
	document.getElementById('captcha_code').focus();
}

function close_warning()
{
	document.getElementById("warning").style.visibility='hidden';
}

// this is a universal replacment for show_application()
// pass the id for what should be shown
function show_form_container(container)
{
	//var theform=document.getElementById("app_form");
	//theform.style.visibility = "visible";
	var theobject=document.getElementById( container )
	// show the form
	theobject.style.display = "block";
}

// inspired by http://www.irt.org/script/40.htm
function newexit(file)
{
	top.opener.window.location.href = file;
	// close the window
	window.close()
}

function OpenBrWindow(theURL,winName,features)
{
	mediumWin = window.open(theURL,winName,features);
	mediumWin.focus();
}

// opens a help topic in a new window
function open_help(link, width, height )
{
	var myWindow = window.open(link,"HelpWindow",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width='+width+',height='+height);
	myWindow.focus();
}

// provides hints for field content length
function hintLength(field_id, maxlength)
{
	//alert("field_id value:"+field_id,"");
	var thefield=document.getElementById( field_id )
	//alert("thefield value:"+thefield,"");
	var thelen = thefield.value.length;
	//alert("thelen value:"+thelen,"");
	var txtlen = maxlength-thelen;
	//alert("txtlen value:"+txtlen,"");
	var thehint=document.getElementById("hint_"+field_id);
	//alert("thehint value:"+thehint,"");
	thehint.innerHTML=txtlen+" characters left ( "+maxlength+" max )";
	// make hint red when exceeding maxlength
	if ( thelen > maxlength )
	{
		document.getElementById("hint_"+field_id).style.color='red';
		document.getElementById("hint_"+field_id).style.fontSize='large';
	}
	else
	{
		//document.getElementById("hint_"+field_id).style.color='black';
		//document.getElementById("hint_"+field_id).style.color='red';
		document.getElementById("hint_"+field_id).style.color='red';
		document.getElementById("hint_"+field_id).style.fontSize='small';
	}
}

// provides hints for field content MIN and MAX lengths
// New version to be used for recommended service, appointment and reminder reason and possibly others.
function NewHintLength(field_id, minlength, maxlength)
{
	//alert("field_id value:"+field_id,"");
	var thefield=document.getElementById( field_id )
	//alert("thefield value:"+thefield,"");
	var thelen = thefield.value.length;
	//alert("thelen value:"+thelen,"");
	var thehint=document.getElementById("hint_"+field_id);
	//alert("thehint value:"+thehint,"");

	// make hint red when less then minlength
	if ( thelen < minlength && minlength != "0" )
	{
		thehint.innerHTML="Length too short ( "+minlength+" characters min )"
		document.getElementById("hint_"+field_id).style.color='red';
		document.getElementById("hint_"+field_id).style.fontWeight='bold';
	}
	else if ( thelen > maxlength && maxlength != "0" )
	{
		var excess = thelen-maxlength;
		thehint.innerHTML="Length "+excess+" too long ( "+maxlength+" max )";
		document.getElementById("hint_"+field_id).style.color='red';
		document.getElementById("hint_"+field_id).style.fontWeight='bold';
	}
	else if ( maxlength != "0" )
	{
		var txtlen = maxlength-thelen;
		//thehint.innerHTML="OK"
		thehint.innerHTML=txtlen+" characters left ( "+maxlength+" max )";
		document.getElementById("hint_"+field_id).style.color='black';
		document.getElementById("hint_"+field_id).style.fontWeight='normal';
	}
	else
	{
		thehint.innerHTML="OK"
		//thehint.innerHTML="<b style=\"color: green\">OK</b>"
		document.getElementById("hint_"+field_id).style.color='green';
		document.getElementById("hint_"+field_id).style.fontWeight='bold';
	}
}



function showtip(current, whichDiv)
{
	if(document.getElementById)
	{
		elm=document.getElementById( whichDiv );
		elml=current;  
		elm.style.height=elml.style.height;
		// next two line use a drop declaration for left and top
		// elm.style.top=parseInt(elml.offsetTop+elml.offsetHeight);
		//       elm.style.left=parseInt(elml.offsetLeft+elml.offsetWidth+10);
		elm.style.visibility = "visible";
		// next 2 line cause javascript console error message DL
		// elm.style.pixelTop = event.clientY + 10;
		// elm.style.pixelLeft = event.clientX + 10;
	}
}

function hidetip(whichDiv)
{
	if (document.layers)
	{
		elm2 = eval("document." + whichDiv );
		elm2.visibility="hidden";
	}
	else
	{
		if(document.getElementById)
		{
			elm.style.visibility="hidden";
		}
	}
}
