/*
GetXmlHttpObject
getajax
closelightbox
closelightboxImage
cleantxtforajax
setdatequick
setdateyr
setdate
timecheck
isDate
makeyear
setzero
getSelectedRadio
getSelectedRadioValue
dropdownval
dropdowntext
ddlookup
showhide
showhidediv
toggleBox
swapdivs
switchdiv
layervisibility
tickall
tickallgroup
selectedCount - counts checkboxes of this class
toggleCheckboxes - better tickall function
deeplinkcheck
escapeurl
testlink
imagepickerhover
convertdimensions
changebeltimage
scorePassword
sortcoder
sortcodeshift
RadioSelectorCSS
ColourSwap
ValidateEmail
MultiChoiceActions
highlightRows - changes row colour based on checkbox
setCookie
agechange
applyCollapsing
updateCollapsing
*/
function GetXmlHttpObject() {
	var xmlHttp=null; try { xmlHttp=new XMLHttpRequest(); } catch (e) { try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp;
}

function getajax(div, ajaxpage, ajtype, var1, var2, var3, waitforresponse, lightboxname, uselightbox, finishlightbox, lightboxwidth, postform, enhance, addQuerystring, p1, p2) {
	xmlHttp=GetXmlHttpObject();
	asynchronous 	= true; 
	if(waitforresponse=="y") asynchronous = false; 
	lightboxname	= (lightboxname==null)?"":lightboxname;
	uselightbox		= (uselightbox==null)?false:uselightbox;
	finishlightbox	= (finishlightbox==null)?false:finishlightbox;
	postform			= (postform==null)?"":postform;
	addQuerystring	= (addQuerystring==null)?"":addQuerystring;
	//if(p1=="derren") alert(addQuerystring);
	myQS				= "ajax";
	if(var3=="usevar2") { myQS = var2; var2 = ""; var3 = ""; }
	var ajaxstring = myQS + "="+div+"|"+ajtype+"|"+var1+"~"+var2+"~"+var3+"|"+new Date().getTime();

	if(postform!="") { 
		var formdata = $('#' + postform).serialize(); 
		ajaxdelivery = "POST"; 
		ajaxpage = ajaxpage + "?" + ajaxstring; 
		ajaxstring = formdata; 
		if(addQuerystring.length>1) ajaxpage = ajaxpage + "&x9y" + addQuerystring;
		} 
	else { ajaxdelivery = "GET"; ajaxstring = ajaxpage + "?" + ajaxstring; 
		if(addQuerystring.length>1) ajaxstring = ajaxstring + "&x9y" + addQuerystring;
	}
	if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } 
	if(asynchronous && ajaxdelivery=="GET") {
		xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4 && div!="") { document.getElementById(div).innerHTML=xmlHttp.responseText } }
	}
	if(ajaxdelivery=="GET") { 
		xmlHttp.open(ajaxdelivery, ajaxstring, asynchronous); xmlHttp.send(null); 
	}
	else { 
		xmlHttp.open(ajaxdelivery, ajaxpage, asynchronous); 
		xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlHttp.send(ajaxstring);
		if(asynchronous) {
			xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4 && div!="") { document.getElementById(div).innerHTML=xmlHttp.responseText } }
		} else { returnedtext = xmlHttp.responseText;  }
	}
	if(uselightbox) { 
		openLightBox(lightboxname, lightboxwidth);
	}
	if(finishlightbox) { closeLightBox(lightboxname, 50);  }
	if(!asynchronous && div!="") document.getElementById(div).innerHTML=xmlHttp.responseText;
	if(enhance=="y" && div!="") $("#" + div).enhanceWithin();
} 
function closeLightBox(lightboxname, setDelay, newimagename, newimageid) {
	setDelay			= (setDelay==null)?10:setDelay;
	newimagename 	= (newimagename==null)?"":newimagename;
	newimageid		= (newimageid==null)?"":newimageid;
	if(newimageid!="") $("#" + newimageid).attr("src",newimagename+"?x="+Math.random());
	$('#'+lightboxname).delay(setDelay).fadeOut(); $('#'+lightboxname+'back').delay(setDelay).fadeOut();
}

function openLightBox(lightboxname, lightboxwidth) {
	$('#'+lightboxname+'back').fadeIn();
	$('#'+lightboxname).fadeIn(); 
	if(lightboxname=="lightbox"&&typeof(document.getElementById(lightboxname+"defaultwidth"))!="undefined") { 
		if(parseInt(document.getElementById(lightboxname+"defaultwidth").value)>0) { // -1 is mobile
		lightboxwidth=(lightboxwidth==null)?parseInt(document.getElementById(lightboxname+"defaultwidth").value):lightboxwidth;
		jQuery("#lightbox").css({"margin-left":parseInt((lightboxwidth*-1)/2) + "px","width":lightboxwidth+"px"});
		}
	} 
}
function cleantxtforajax(str, crlf, commats, hashes) {
	str = str.replace(/&/gi, "^amp^"); str = str.replace(/'/gi, "^sq^"); if(crlf==null) crlf = false; if(crlf) str = str.replace(/\n/gi, "^crlf^");
	if(commats==null) commats = false; if(commats) str = str.replace(/@/gi, "^comat^"); if(hashes==null) hashes = false; if(hashes) str = str.replace(/#/gi, "^hsh^"); return str;
}
function setdatequick(frm, datefld, initial) {
	var datefield = document.getElementById(datefld); var datefield1 = document.getElementById(datefld + "1");
	var datefieldlast = document.getElementById(datefld+"last"); var datefieldlast1 = document.getElementById(datefld + "last1");
	var datestr = datefield1.value; if(datestr=="") { datefield.value=''; return; }
	if((datestr.length>5)&&(datestr.indexOf("/")==-1)&&(!isNaN(datestr))) { datestr = datestr.substr(0,2)+"/"+datestr.substr(2,2)+"/"+datestr.substr(4); }
	datestr = datestr.replace("-","/");datestr = datestr.replace("-","/");datefield1.value = datestr;
	var x = datestr.split("/"); if(x.length < 2) {alert("Please enter dates in dd/mm/yyyy or ddmmyyyy format"); datefield.value = datefieldlast.value; datefield1.value = datefieldlast1.value; return;}
	var thisday = x[0]; var thismon = x[1]; var thisyea = x[2];
	if(thisday.length==1) thisday = "0" + thisday; if(thismon.length==1) thismon = "0" + thismon;
	// irrespective of the rest of the date values, update the year to a 4 figure year
	if ((thisyea<=100)&&(isNaN(parseFloat(thisyea))==false)) { if(thisyea>=31) thisyea=(1900 + parseFloat(thisyea)); else thisyea=(2000 + parseFloat(thisyea)); }
	if (isNaN(parseInt(thisday))||thismon==0||isNaN(parseFloat(thisyea))) { datefield.value=''; datefield1.value=''; }
	else{ if (thisday.length==1) thisday=("0" + thisday);
		if ((isDate(""+thisyea+thismon+thisday+"")==false)||thisyea<1900||thisyea>2080||thismon>12) {
			alert(" "+thisday+"/"+thismon+"/"+thisyea+" is not a valid date"); datefield.value = datefieldlast.value; datefield1.value = datefieldlast1.value;
			}
		else  {var thisDate = new Date(); 
			if((datefld=="empDob")&&((thisDate.getFullYear()-thisyea)<15)) alert("Sorry, but that would make the age less than 16 - please try again");
			else {
					if(typeof(cutoffchk)=="undefined") cutoffchk = false;
						datefield.value = (""+thisyea+thismon+thisday+""); datefield1.value = (""+thisday+"/"+thismon+"/"+thisyea+""); 
						datefieldlast.value = datefield.value; datefieldlast1.value = datefield1.value; 
				}
			}
		}
}
// Allows the month to be a hidden or text field for year only choices
function setdateyr(datefld) {
	var thisobj = document.getElementById(frm); var thisday = document.getElementById(datefld + "1").value; var thismon = document.getElementById(datefld+"2").value;
	if(thismon.length==1) thismon=("0" + thismon); var thisyea = document.getElementById(datefld + "3").value; 
  	if((thisyea<=100)&&(isNaN(parseInt(thisyea))==false)) { if(thisyea>=31) thisyea=(1900 + parseInt(thisyea)); else thisyea=(2000 + parseInt(thisyea));
		 document.getElementById(datefld + "3").value=thisyea; }
	if (isNaN(parseInt(thisday))||thismon==0||isNaN(parseInt(thisyea))) document.getElementById(datefld).value='';
	else { if (thisday.length==1) thisday=("0" + thisday); if (isDate(""+thisyea+thismon+thisday+"")==false) { alert(" "+thisday+"/"+thismon+"/"+thisyea+" is not a valid date"); eval(thisobj+"."+datefld+".value=''");} else {var thisDate = new Date(); if((datefld=="dob")&&((thisDate.getFullYear()-thisyea)<15)) {alert("Sorry, but that would make the age less than 16 - please try again");} else {document.getElementById(datefld).value=(""+thisyea+thismon+thisday+"");} } }
}
// create a SQL date from 2 text boxes + select
function setdate(frm,datefld) {
	var thisobj = ("document." + frm); var thisday = document.getElementById(datefld + "1").value;
	var thismon = document.getElementById(datefld + "2").options[document.getElementById(datefld + "2").selectedIndex].value;
	var thisyea = document.getElementById(datefld + "3").value; if(thisday=="" || thismon=="" || thisyea=="") {document.getElementById(datefld).value="";}
	else { if(thisday.length==1) thisday = "0" + thisday; if(thismon.length==1) thismon = "0" + thismon;
		if ((thisyea<=100)&&(isNaN(parseInt(thisyea))==false))
			{ if(thisyea>=31) thisyea=(1900 + parseInt(thisyea)); else thisyea=(2000 + parseInt(thisyea)); document.getElementById(datefld + "3").value=thisyea; }
		if (isDate(""+thisyea+thismon+thisday+"")==false) { alert(" "+thisday+"/"+thismon+"/"+thisyea+" is not a valid date"); document.getElementById(datefld).value=""; } else { var thisDate = new Date(); if((datefld=="empDob")&&((thisDate.getFullYear()-thisyea)<15)) alert("Sorry, but that would make the age less than 16 - please try again"); else document.getElementById(datefld).value=(""+thisyea+thismon+thisday+""); }
	}
}
function timecheck(thisfld, timetype) { thistime = parseInt(setzero(thisfld.value)); if(thistime==0 || isNaN(thistime)) { thistime = "00" }
	else { if(timetype=='h') maxval = 23; else maxval = 59; if(thistime<0 || thistime >maxval) thistime=0; 
	if(parseInt(thistime) < 10) thistime = "0" + parseInt(thistime); } thisfld.value=thistime;
}
function isDate(dateStr) { month = dateStr.substring(4,6); day = dateStr.substring(6,8); year = dateStr.substring(0,4);
    if (day < 1 || day > 31) { return false;} if ((month==4 || month==6 || month==9 || month==11) && day>=31) {return false;}
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if (day > 29 || (day==29 && !isleap)) {return false;}
    }
    return true; // date is valid
}
function makeyear(thisyea) { if ((thisyea<=100)&&(isNaN(parseInt(thisyea))==false)) { if(thisyea>=31) thisyea=(1900 + parseInt(thisyea));
		else thisyea=(2000 + parseInt(thisyea)); } return thisyea;
}
function setzero(sz_num) { if(sz_num=='') return "0.00"; sz_num = "" + sz_num; sz_num = sz_num.replace(",",""); if(isNaN(sz_num)) sz_num = "0";
	sz_num = parseFloat(sz_num); sz_num = Math.round(sz_num*100)/100; sz_num = "" + sz_num; var sz_dotpos = sz_num.indexOf('.');
	if (sz_dotpos < 0) sz_num+=".00"; if (sz_dotpos==(sz_num.length-1)) sz_num+="00"; if (sz_dotpos==(sz_num.length-2)) sz_num+="0"; return sz_num ;
}
function getSelectedRadio(buttonGroup) { // returns the array number of the selected radio button or -1 if no button is selected
	if (buttonGroup[0]) { for (var i=0; i<buttonGroup.length; i++) if (buttonGroup[i].checked) return i } 
	else {if (buttonGroup.checked) return 0;} return -1; }

function getSelectedRadioValue(buttonGroup) { // returns the value of the selected radio button or "" if no button is selected
	var i = getSelectedRadio(buttonGroup);
	if (i == -1) return ""; else {if (buttonGroup[i]) return buttonGroup[i].value; else return buttonGroup.value; }}

function dropdownval(itemid) { return document.getElementById(itemid).options[document.getElementById(itemid).selectedIndex].value; }
function dropdowntext(itemid) { return document.getElementById(itemid).options[document.getElementById(itemid).selectedIndex].text; }
function ddlookup(selectid, fieldid) { document.getElementById(fieldid).value = document.getElementById(selectid).options[document.getElementById(selectid).selectedIndex].value; }

function tickall(chkval, chkname, chknum) { var items = parseInt(document.getElementById(chknum).value); for(i=1; i<=items; i++) { document.getElementById(chkname+i).checked=chkval; } }
function tickallgroup(chkval, chkname, chknum, chkid, chkidval) { // this version only ticks ones that also match the value of the chkid field as well
	var items = parseInt(document.getElementById(chknum).value);
	for(i=1; i<=items; i++) { if(document.getElementById(chkid+i).value==chkidval) { document.getElementById(chkname+i).checked=chkval;} }
}
function selectedCount(selectedItem) { return $("input." + selectedItem + ":checked").length; }
function toggleCheckboxes(selectedName, changeObject) { $("."+selectedName).prop("checked", $(changeObject).prop("checked")); }

function deeplinkcheck(ev,deepfield) {
	if(!ev=="") { ev=ev.trim(); var reg=new RegExp("[^abcdefghijklmnopqrstuvwxyz0123456789 \\-]", "ig"); var reg2=new RegExp("[ ]", "ig"); ev = ev.replace(reg, "");
		ev = ev.replace(reg2, "-"); ev = ev.replace("--", "-"); document.getElementById(deepfield).value = ev.toLowerCase() + "/"; }
}
function escapeurl(url) { return url.replace(/\//g,"\/"); /* fixes forward slashes in links to allow them to be passes as javascript */ }
function testlink(thisval) { 
	if(thisval!=""&&thisval!="http://" && thisval.indexOf(".")>-1) { if(thisval.indexOf("http")==-1) thisval = "http://" + thisval; window.open(thisval,"linktest");
	} else alert("Sorry, but you have not entered a valid address to test");
}
function imagepickerhover(imagefield, imgpath, imgpathfull, imgval) { 
	// imgpathfull should be absolutepath
	document.getElementById(imagefield+'newimage').src = imgpath+imgval; 
	$("#" + imagefield + "selected").val(imgpathfull+imgval);
	$("#" + imagefield + "selectedsrc").val(imgval);
	$("#" + imagefield + "Instructions").show();
	//document.getElementById(imagefield+'selectedsrc').value = imgpathfull+imgval;
}
function imagepickerclick(imagefield, useFullPath) {
	var ImageSource = $("#" + imagefield + "selected").val();
	var myImg = $("#" + imagefield + "selectedsrc").val();
	if(useFullPath) myImg = ImageSource;
	$("#" + imagefield).val(myImg); 
	document.getElementById(imagefield+'-img').src = ImageSource; 
	document.getElementById(imagefield+'-url').innerHTML = ImageSource; // document.getElementById(imagefield + 'absolutepath').value + document.getElementById(imagefield + 'selected').value;
	showhide(imagefield+'div');
}
function imagePickerClear(imagefield, shim) {
	$("#" + imagefield + "-img").attr("src",shim); $("#" + imagefield).val(""); $("#" + imagefield + "-url").html(""); $("#" + imagefield + "div").hide();
}
function convertdimensions(endval, startval, stem, endfield) {
	if(endval=="m") { var val1 = parseFloat(document.getElementById(stem+"1").value); var val2 = parseFloat(document.getElementById(stem+"2").value);
		if(val1+val2<1) return; document.getElementById(endfield).value = setzero((((val1 * 12) + val2) * 25.4)/1000);
	}
	if(endval=="ft") { var val1 = parseFloat(document.getElementById(stem).value); if(val1<1) return; val1 = (val1 * 1000)/25.4; var inches = val1%12; 
		var feet = parseInt((val1 - inches)/12); document.getElementById(endfield+"1").value = feet; document.getElementById(endfield+"2").value = setzero(inches);
	}
	if(endval=="kg") { var val1 = parseFloat(document.getElementById(stem+"1").value); if(val1==isNaN) val1=0;
		var val2 = parseFloat(document.getElementById(stem+"2").value); if(val2==isNaN) val2=0;
		if(val1+val2<1) return; document.getElementById(endfield).value = setzero((((val1 * 14) + val2) * 453.59)/1000);
	}
	if(endval=="st") { var val1 = parseFloat(document.getElementById(stem).value); if(val1<0.1) return; val1 = ((val1 * 1000)/453.59); var lbs = val1%14; 
		var stone = parseInt((val1 - lbs)/14); document.getElementById(endfield+"1").value = stone; document.getElementById(endfield+"2").value = parseInt(lbs);
	}
	if(endval=="lb") { var val1 = parseFloat(document.getElementById(stem).value); if(val1<0.1) return; val1 = ((val1 * 1000)/453.59); var lbs = val1; 
		document.getElementById(endfield+"2").value = parseInt(lbs);
	}
}
function changebeltimage(selectfield, imageholder, fullpath) { var gurn = parseInt(dropdownval(selectfield)); var gradeimage = "/images/shim.gif"; if(gurn>0) gradeimage = "https://b.blackculm.com/" + document.getElementById("gurnimg"+gurn).value; document.getElementById(imageholder).src = gradeimage;
}
function flagchange(whichcell,resultval) { $("#"+whichcell).attr("class","resultcell"+resultval); }

function scorePassword(pass, scorefield, scoreresult) {
	var score = 0; if (!pass) return score;
	var letters = new Object(); // award every unique letter until 5 repetitions
	for (var i=0; i<pass.length; i++) { letters[pass[i]] = (letters[pass[i]] || 0) + 1; score += 5.0 / letters[pass[i]]; }
	// bonus points for mixing it up
	var variations = { digits: /\d/.test(pass), lower: /[a-z]/.test(pass), upper: /[A-Z]/.test(pass), nonWords: /\W/.test(pass), }
	variationCount = 0; for (var check in variations) { variationCount += (variations[check] == true) ? 1 : 0; } score += (variationCount - 1) * 10;
	if(!scorefield=="") document.getElementById(scorefield).value=score;
	if(!scoreresult=="") { var scoredesc = "<span style=\"color:#C00\">Too weak</span>";
		if (score >= 30) scoredesc =  "<span style=\"color:#F90\">Weak, but getting better</span>";
		if (score >= 60) scoredesc =  "<span style=\"color:#9C3\">Good enough</span>";
		if (score > 80) scoredesc =  "<span style=\"color:#9C3\">Strong</span>";
		document.getElementById(scoreresult).innerHTML=scoredesc;
	}
	return parseInt(score);
}
function sortcoder(event){var char=event.which||event.keyCode;if(char==8||char==9||char==16||char==37||char==38||char==39||char==40||char==90) return;if(!(char>=48&&char<=57)){event.preventDefault();return false;}}
function sortcodeshift(event,thisfield,whichid,whichfield,nextfield) {var char=event.which||event.keyCode;if(char==8||char==9||char==16||char==37||char==38||char==39||char==40||char==90) return;if(thisfield.value.length==2&&char>=48&&char<=57){if(whichid==2) document.getElementById(nextfield).focus(); else document.getElementById(whichfield+(whichid+1)).focus();}}

function getMask(whichType) {
	if(whichType=="numbers") 	return "0123456789";
	if(whichType=="tel") 		return "0123456789 +()";
	if(whichType=="currency") 	return "0123456789.-";
	if(whichType=="sortcode") 	return "0123456789-";
	if(whichType=="email") 		return "0123456789-@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._;";
	if(whichType=="url") 		return "0123456789-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:/"; // top level only
	if(whichType=="postcode") 	return "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if(whichType=="taxcode") 	return "0123456789BCDKLMNRSTWX";
	if(whichType=="date") 		return "0123456789/";
	if(whichType=="sharecode") return "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if(whichType=="hex")			return "#0123456789abcdefABCDEF";
}
function inputMask(e,mask) {
	var char = e.key; // e.which || e.keyCode; alert(e.key); 
	var ctrl = e.ctrlKey ? e.ctrlKey : ((char === 17) ? true : false);
	if (ctrl && (e.key == "v" || e.key == "c")) return true; //leaves illegal characters in it
	if(char==8||char==9||char==16||char==37||char==38||char==39||char==40||char==90) return;
	pattern = getMask(mask);
	if(pattern.indexOf(char)<0) { e.preventDefault(); return false; }
}

function toTitleCase(str) { return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1);}); }
function _calculateAge(birthday) { // birthday is a date
	var ageDifMs = Date.now() - birthday.getTime();
	var ageDate = new Date(ageDifMs); // miliseconds from epoch
	return Math.abs(ageDate.getUTCFullYear() - 1970);
}
function showage(dobfield, showin, prefix, suffix) {
	var dateStr = document.getElementById(dobfield).value;
	suffix	= (suffix==null)?"":suffix;
	if(dateStr=="") { return ""; } 
	else {
		var birthday = new Date(dateStr.substring(0,4)+"-"+dateStr.substring(4,6)+"-"+dateStr.substring(6,8));
		var myAge = _calculateAge(birthday);
		document.getElementById(showin).innerHTML = prefix + myAge + suffix;
	}
}
function getAgeAtDate(dateString, dateStringCompare) {
	if(dateString.length==8) {
		var dateIn = new Date(dateString.slice(0,4),dateString.slice(4,6),dateString.slice(6,8));
		var dateComparison = new Date(dateStringCompare.slice(0,4),dateStringCompare.slice(4,6),dateStringCompare.slice(6,8));
		for(age=0;dateIn<=dateComparison;age++) dateIn.setFullYear(dateIn.getFullYear()+1)
		return(age-1); // always remove a year as we've gone past the date
	} else return 0;
}
function niformat(ninum) { 	
	ninum = ninum.toUpperCase(); ninum = ninum.replace(/ /gi, ""); ninum = ninum.replace(/-/gi, "");
	if (!RegexCheck("NI", ninum) && ninum!="") { alert("Sorry, but " + ninum + " is not a valid National Insurance Number. Please try entering it again. " +
	"It's always two very specific letters, six numbers and then a final letter (A-D). " + 
	"If you can't do it, leave the field blank but this may affect how you get paid."); }
	return ninum;
 }
function RegexCheck(CheckType, CheckValue) {
	if(CheckType=="NI") {
		const regex = /([ACEHJLMOPRSWXY][A-CEGHJ-NPR-TW-Z]|B[A-CEHJ-NPR-TW-Z]|G[ACEGHJ-NPR-TW-Z]|[KT][A-CEGHJ-MPR-TW-Z]|N[A-CEGHJL-NPR-SW-Z]|Z[A-CEGHJ-NPR-TW-Y])[0-9]{6}[A-D]/gm;
		return regex.test(CheckValue);
	}
	return regex.test(CheckValue);
}

function formatTel(fieldname) { // sets 5 space rest format for telephone numbers
	var tel = document.getElementById(fieldname).value; tel = tel.replace(/\+44/gi, "0"); tel = tel.replace(/ /gi, ""); 
	if(tel.length>5) { tel = tel.substr(0,5) + " " + tel.substr(5,tel.length-5); }
	document.getElementById(fieldname).value = tel;
}
function RadioSelectorCSS(whichClass, whatIsSelected) {
	$('label').removeClass(whichClass); $("#" + whatIsSelected).addClass(whichClass);
}
function ColorSwap(fieldID, swapField) {
	if("color" == $("#" + fieldID).attr("type")){ document.getElementById(fieldID).type = "text"; $("#" + swapField).html("Colour Picker");
	} else { document.getElementById(fieldID).type = "color"; $("#" + swapField).html("Enter Text"); }
}

function ValidateEmail(email) {
	//const regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/gm;
	const regex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,10}$/g; // 
	return regex.test(email);
}

function MultiChoiceActions(fieldname, action) {
	if(action=="toggle") {
		var isShowing = $("#" + fieldname + "Chooser").is(":visible"); $("#" + fieldname + "dropDownShield").hide(); $(".multiChooser").hide(); 
		if(!isShowing) { $("#" + fieldname + "Chooser").show();$("#" + fieldname + "dropDownShield").show(); }
	}
	if(action=="hide") { $(".dropDownShield").hide(); $(".multiChooser").hide(); }
	if(action=="selecting") { if($(this).prop("checked")==true) $("#" + fieldname + "label").attr("class","isSelected"); else $("#" + fieldname + "label").attr("class",""); }
}
function highlightRows(baseID, baseClass, checkID, i, allRows) {
	if(allRows) { var studentCount = parseInt($("#studentCount").val()); 
		for(i=1;i<=studentCount;i++) $("#"+baseID+i).attr("class",$("#"+checkID+i).prop("checked")?"lightSelected":"");
	} else { $("#"+baseID+i).attr("class",$("#"+checkID+i).prop("checked")?"lightSelected":""); }
}

function setCookie(name,value,days) {
	var expires = "";
	if (days) {
		 var date = new Date();
		 date.setTime(date.getTime() + (days*24*60*60*1000));
		 expires = "; expires=" + date.toUTCString();
	}
	document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
function applyCollapsing(whichDiv) {
	document.getElementById(whichDiv).addEventListener("click", function() {
		this.classList.toggle("open");
		var content = this.nextElementSibling;
		if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; }
	});
}
function updateCollapsing() {
	var coll = document.getElementsByClassName("collapsing"); var i;
	for (i = 0; i < coll.length; i++) {
	coll[i].addEventListener("click", function() {
		this.classList.toggle("open");
		var content = this.nextElementSibling;
		if (content.style.display === "block") { content.style.display = "none"; } else { content.style.display = "block"; }
	});
	}
}

/* old */ 
function showhide(id){obj=document.getElementById(id);if(obj.style.display=="none")obj.style.display="";else obj.style.display="none";} 
function showhidediv(id,istate){obj=document.getElementById(id);if(istate=="show"||(obj.style.display=="none"&&istate=="swap"))obj.style.display="block";else obj.style.display="none";} 
function toggleBox(szDivID,iState){if(document.layers)if(document.layers[szDivID].visibility=="show"||iState==0)document.layers[szDivID].visibility="hide";else document.layers[szDivID].visibility="show"; else if(document.getElementById){var obj=document.getElementById(szDivID);if(obj.style.visibility=="visible"||iState==0)obj.style.visibility="hidden"; else obj.style.visibility="visible";} else if(document.all) if(document.all[szDivID].style.visibility=="visible"||iState==0)document.all[szDivID].style.visibility="hidden"; else document.all[szDivID].style.visibility="visible";showhide(szDivID)}
function swapdivs(divstem,divno,total){showhidediv(divstem+divno,"show");for(i=1;i<=total;i++){if(i!=divno)showhidediv(divstem+i,"");}}
function switchdiv(block,imgstem,fp){if(fp==undefined)fp="";thisimg=document.getElementById(block+'img');if(imgstem==undefined)imgstem="";if(layervisibility(block)){showhidediv(block,'hide');thisimg.src=fp+'images/icons/plus'+imgstem+'.gif';}else{showhidediv(block,'show');thisimg.src=fp+'images/icons/minus'+imgstem+'.gif';}}
function layervisibility(layerid){if(document.layers)	return(document.layers[layerid].visibility=="show");else if(document.getElementById){return(document.getElementById(layerid).style.visibility=="visible"||document.getElementById(layerid).style.display!="none")} else if(document.all)return(document.all[layerid].style.visibility=="visible");}