// function to set the location of the cursor in a text field. May only work in IE? Intended for use with validation, not implemented at present.
function setSelRange(inputEl, selStart, selEnd) { 
 if (inputEl.setSelectionRange) { 
  inputEl.focus(); 
  inputEl.setSelectionRange(selStart, selEnd); 
 } else if (inputEl.createTextRange) { 
  var range = inputEl.createTextRange(); 
  range.collapse(true); 
  range.moveEnd('character', selEnd); 
  range.moveStart('character', selStart); 
  range.select(); 
 } 
}

// Restrict to letters, numbers & space.
function ValidateInput(id)
{
	var valueValidate = 0;
	var idValue = document.getElementById(id).value;
	if (idValue=="")
	{
		return;
	}
	else
	{
		var reg = /^[A-Za-z0-9 ]+$/; // entire string consists of these characters
		if (idValue.search(reg)!=-1) 
		{
			return;
		}
		else
		{
			var charPos = 0;
			var i = 0;
			while (charPos==0)
			{
				findPos = idValue.substr(0,i);
				if (findPos.search(reg)==-1) 
				{
					charPos = i;
				}
				i++;
			}
			alert("You entered an invalid character. Valid characters are a through z, 0 through 9, and 'space'");
			document.getElementById(id).value = idValue.substr(0, charPos-1) + idValue.substr(charPos, idValue.length -1);
		}
	}
}

// restrict only characters that might break code - eg by closing a tag
function ValidateInput2(id)
{
	var valueValidate = 0;
	var idValue = document.getElementById(id).value;
	if (idValue=="")
	{
		return;
	}
	else
	{
		var reg = /[<>{}"]/; // one of these characters is anywhere in the string
		if (idValue.search(reg)==-1) // note == compared to != in above function.
		{
			return;
		}
		else
		{
			var charPos = 0;
			var i = 0;
			while (charPos==0)
			{
				findPos = idValue.substr(0,i);
				if (findPos.search(reg)==-1) 
				{
					charPos = i;
				}
				i++;
			}
			alert("You entered an invalid character. < > {} and \" may not be used in this field.");
			document.getElementById(id).value = idValue.substr(0, charPos-1) + idValue.substr(charPos, idValue.length -1);
		}
	}
}

//check for valid #html code characters (#000 - #FFFFFF) and update display panel (Advanced style settings)
function ValidateColour(id)
{
	var valueValidate = 0;
	var idValue = document.getElementById(id).value;
	var idDisp = "display_" + id;
	if (idValue=="")
	{
		document.getElementById(idDisp).style.backgroundColor = "#FFFFFF"; 
		return;
	}
	else
	{
		var reg = /^[A-Fa-f0-9]+$/; // entire string consists of these characters
		if (idValue.search(reg)!=-1) 
		{} //continue
		else
		{
			var charPos = 0;
			var i = 0;
			while (charPos==0)
			{
				findPos = idValue.substr(0,i);
				if (findPos.search(reg)==-1) 
				{
					charPos = i;
				}
				i++;
			}
			alert("You entered an invalid character. Valid characters are A through F and 0 through 9.");
			document.getElementById(id).value = idValue.substr(0, charPos-1) + idValue.substr(charPos, idValue.length -1);
			return;
		}
		if (idValue.length != 3 && idValue.length != 6)
		{
			return;
		}
		else
		{
			document.getElementById(idDisp).style.backgroundColor = "#" + idValue; 
		}
	}
}
// check for valid # html css length - 0, 3 or 6 characters (Advanced style settings)
function ValidateLength(id)
{
	var idValue = document.getElementById(id).value;
	if (!(idValue.length == 0 || idValue.length == 3 || idValue.length == 6))
	{
		alert("Colour settings must be 0, 3 or 6 characters in length!");
		//document.getElementById(id).focus();
		var timer = setTimeout(function(){document.getElementById(id).focus(),50}); // to make it work in mozilla browsers, which change focus after the script has been run.
	}
}

// show/hide for info pages
function info_hide()
{
	sections = document.getElementsByTagName('div');
	for(i=0;i<sections.length;i++)
	{
		if (document.getElementById(sections[i].id).className == "info")
		{
			document.getElementById(sections[i].id).style.display = "none";
		}
	}
}

function info_show(id)
{
	info_hide();
	document.getElementById(id).style.display = "block";
}