function deletetemplate() { 
if (confirm("Are you sure you want to remove this item?")) 
return true; 
else 
return false; 

}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
 } 
 
 function isNumber(x)
 {
	//var y = x * 1;
	if (isNan(x)) return false;
	return true;
 }
 
function isPosNumber(x)
 {
	//var y = x * 1;
	if (isNaN(x)) return false;
	if (x <=0) return false;
	return true;
 }
 
 
function isPosInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false;
   if (y <= 0) return false;
   return x==y && x.toString()==y.toString(); 
 } 


function Left(str, n)
{
   if (n <= 0)
         return "";
   else if (n > String(str).length)
         return str;
   else
         return String(str).substring(0,n);
}


function checkEmail(strEmail) 
{
	var filter = /^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i;
	if (!filter.test(strEmail)) 
	{
		return false;
	}
	else
	{
		return true;
	}
}

function checkNumeric(objName,minval,maxval,period)
{
var checkOK = "0123456789" + period;
var checkStr = objName;
var allValid = true;
var decPoints = 0;
var allNum = "";

for (i = 0;  i < checkStr.value.length;  i++)
{
	ch = checkStr.value.charAt(i);
	for (j = 0;  j < checkOK.length;  j++)
	if (ch == checkOK.charAt(j))
	break;
	if (j == checkOK.length)
	{
		allValid = false;
		break;
	}
	if (ch != ",")
		allNum += ch;
}
if (!allValid)
{	
	return (false);
}// set the minimum and maximum
var chkVal = allNum;
var prsVal = parseInt(allNum);
if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
{
	return (false);
}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function setDisabled(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.disabled = newValue;
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].disabled = newValue;
	}
}



function isradio(oField)
{
	if (typeof(oField[0]) !=  "undefined")
	{
		if (oField[0].type=="radio")
			return true;
	}
	return false;
}

function checkform(strFormMessage, frmName)
{
	var bGoingon = true;
	var rgroup = 0;
	var rgroupon = 0;
	var nItems = document.forms[frmName].elements.length;
	for (x=0; x < nItems; x++)
	{
		oField = document.forms[frmName].elements[x];
		strField =  document.forms[frmName].elements[x].name;
		if (Left(strField,2)=="r_")
		{
			strVal = "";
			if (typeof(oField) == "undefined")
				continue;
		
			// if it's a radio button look through the array, otherwise get the value
			if (isradio(oField))
			{
				for (var i=0; i< oField.length; i++) 
				{
					if (oField[i].checked)
					{
						strVal = oField[i].value;
					}
				}
			}	
			else
				strVal = trim(document.forms[frmName].elements[strField].value);
			oLabel = document.getElementById("l" + strField);
			thisOK = true;
			errMessage = ""
			if (Left(strField,3)=="r_e")
			{
				if (strVal.length < 1)
				{
					thisOK = false;
				}
				else if (!checkEmail(strVal))
				{
					thisOK = false;
				}
			}
			else if (Left(strField,3)=="r_i")
			{
				if (strVal.length < 1)
				{
					thisOK = false;
				}
			
				else if (!isInt(strVal))
				{
					thisOK = false;
				}
			}
			else if (Left(strField,3)=="r_g")
			{
				rgroup = 1;
				if (document.forms[frmName].elements[strField].checked)
				{
					rgroupon = 1;
				}
			}
			else if (Left(strField,3)=="r_I")
			{
				if (strVal.length < 1)
				{
					thisOK = false;
				}

				else if (!isPosInt(strVal))
				{
					thisOK = false;
				}
			}

			else if (Left(strField,3)=="r_B")
			{
				if (!document.forms[frmName].elements[strField].checked)
				{
					thisOK = false;
				}
			}
			
			else if (Left(strField,3)=="r_n")
			{
				if (strVal.length < 1)
				{
					thisOK = false;
				}

				else if (!isNumber(strVal))
				{
					thisOK = false;
				}
			}
			else if (Left(strField,3)=="r_N")
			{
				if (strVal.length < 1)
				{
					thisOK = false;
				}

				else if (!isPosNumber(strVal))
				{
					thisOK = false;	
			
				}
			}
			else if (strField == "r_Xnusnumber")
			{
				if (strVal.length < 1 && document.forms[frmName].elements["bStudent"].checked)
				{
					thisOK = false;
				}
			
			
			}

			else if (Left(strField,3)=="r_s")
			{
				if (strVal.length < 1)
				{
					thisOK = false;
				}
			}
			if (!thisOK)
			{
				bGoingon = false;
				if (oLabel)
					oLabel.style.color="red";
			}
			else
			{
				if (oLabel)
					oLabel.style.color="#595959";
			}

		}
	}
	if (rgroup)
	{
		if (!rgroupon)
		{
			document.getElementById("rgroup").style.borderColor = "red";
			bGoingon = false;
		}
		else
		{
			
			document.getElementById("rgroup").style.borderColor = "white";
		}
	}
	if (bGoingon)
	{
		document.getElementById(strFormMessage).style.color = "#333432";
		document.getElementById(strFormMessage).innerHTML = "Submitting details";
		return true;
	}
	else
	{
		document.getElementById(strFormMessage).style.color = "red";
		document.getElementById(strFormMessage).innerHTML = " Please complete required fields";
		return false;
	}

}

function goodpass(checkStr, minlength, maxlength)
{
	var checkOK = "0123456789abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var foundit = true;
	if (checkStr.length < minlength)
		return false;
	else if (checkStr.length > maxlength)
		return false;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		foundit = false;
		for (j = 0;  j < checkOK.length;  j++)
		{
			if (ch == checkOK.charAt(j))
			{
				foundit = true;
				break;
			}
		}	
			
		if (!foundit)
			return false
	}
	return true;
}


/* Postcode software */

	function xmlhttpPost(strURL) 
	{
		var xmlHttpReq = false;
		var thisform = this;
		// Checks if browser is Mozilla or Safari
		if (window.XMLHttpRequest){thisform .xmlHttpReq = new XMLHttpRequest();}
		// Checks if browser is Internet Explorer
		else if (window.ActiveXObject){thisform .xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}
		
		thisform .xmlHttpReq.open('POST', strURL, true);
		thisform .xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		thisform .xmlHttpReq.onreadystatechange = function() 
		{
			if (thisform .xmlHttpReq.readyState == 4) {updatepage(thisform .xmlHttpReq.responseText);}
		}
		thisform .xmlHttpReq.send(getquerystring());
	}
	
	function getquerystring() 
	{
	    var postcode= document.getElementById('postcode').value;
	    qstr = 'postcode=' + escape(postcode);  // NOTE: no '?' before querystring
	    return qstr;
	}
	
	function updatepage(str)
	{
	    document.getElementById("result").innerHTML = str;
	}




