var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}


function regValidate(newsletter, formname, fieldnames, fieldtypes, validatetypes)
{
	//var isnews = false;
	//var a_newsletter = newsletter.split(",");
	//for (var x=0; x<a_newsletter.length; x++)
	//{
	//	if(eval("document." + formname + ".pdf_" + a_newsletter[x] + ".checked") || eval("document." + formname + ".doc_" + a_newsletter[x] + ".checked"))
	//	{
	//		isnews = true;
	//	}
	//}

	// next line as subs are not mandatory at present

	var isnews = true;

	oFormEl = document.getElementById("mail_ermsg");

	if(! isnews)
	{
		oFormEl.style.display = "block";
	}else{
		oFormEl.style.display = "none";
	}

	var fcheck = formValidate(formname, fieldnames, fieldtypes, validatetypes)

	if(isnews && fcheck)
	{
		return true;
	}else{
		return false;
	}
}

function formValidate(formname, fieldnames, fieldtypes, validatetypes)
{

	var array_fieldnames = fieldnames.split(",");
	var array_fieldtypes = fieldtypes.split(",");
	var array_validatetypes = validatetypes.split(",");
	var isvalid = 1;
	var elvalid = 1;
	var numcbox = 0;

	for (var x=0; x<array_fieldtypes.length; x++)
	{

		elvalid = 1;

		var array_cbox = array_fieldnames[x].split("_");
		if (array_cbox.length > 2)
		{
			oFormEl = document.getElementById("ermsg_" + array_cbox[0] + "_" + array_cbox[1]);
		}else{
			oFormEl = document.getElementById("ermsg_" + array_fieldnames[x]);
		}
		oFormEl.style.display = "none";

		if (array_fieldtypes[x] == "radio" && array_validatetypes[x] == "required")
		{
			if (! valRadio(eval("document." + formname + "." + array_fieldnames[x])))
			{
				isvalid = 0;
				elvalid = 0;
				oFormEl.style.display = "block";
			}
		}

		if (array_fieldtypes[x] == "select" && (array_validatetypes[x] == "required" || array_validatetypes[x] == "all|required"))
		{
			if (! valSelectBox(array_fieldnames[x],formname))
			{
				isvalid = 0;
				elvalid = 0;
				oFormEl.style.display = "block";
			}

		}

		if (array_fieldtypes[x] == "checkbox" && array_validatetypes[x] == "required")
		{
			var array_cbox = array_fieldnames[x].split("_");

			var namecbox = "";
			if (array_cbox.length > 2)
			{
				namecbox = array_cbox[0] + "_" + array_cbox[1];
				numcbox = array_cbox[2];
			}else{
				namecbox = array_cbox[0];
				numcbox = array_cbox[1];
			}

			var oneischecked = 0;

			for (var cb=0; cb<numcbox; cb++){

				if (eval("document." + formname + "." + namecbox + "_" + cb + ".checked"))
				{
						oneischecked = 1;
				}
			}

			if(oneischecked == 0)
			{
				isvalid = 0;
				elvalid = 0;
				oFormEl.style.display = "block";
			}

		}

		if (array_fieldtypes[x] == "text" || array_fieldtypes[x] == "textarea" || array_fieldtypes[x] == "password")
		{

			var array_validation = array_validatetypes[x].split("|");

			for (var i=0; i<array_validation.length; i++){


				if(array_validation[i] == "required")
				{
					if (eval("document." + formname + "." + array_fieldnames[x] + ".value.length") == 0)
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i] == "email" && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valEmail(eval("document." + formname + "." + array_fieldnames[x] + ".value")))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if((array_validation[i] == "alpha" || array_validation[i] == "alphanumeric" || array_validation[i] == "numeric") && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valAlphaNumeric(eval("document." + formname + "." + array_fieldnames[x] + ".value"),array_fieldnames[x],eval("document." + formname + "." + array_fieldnames[x] + ".value.length"),array_validation[i]))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i] == "date" && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! isDate(eval("document." + formname + "." + array_fieldnames[x] + ".value")))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i].indexOf("length>") > -1 && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valLength(eval("document." + formname + "." + array_fieldnames[x] + ".value.length"),array_validation[i].substring(array_validation[i].indexOf(">")+1,array_validation[i].length),'gt'))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i].indexOf("length<") > -1 && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valLength(eval("document." + formname + "." + array_fieldnames[x] + ".value.length"),array_validation[i].substring(array_validation[i].indexOf("<")+1,array_validation[i].length),'lt'))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}


			}

			// check if field match required name with _confirm

			if(array_fieldnames[x].indexOf("_confirm") > -1)
			{
				if (eval("document." + formname + "." + array_fieldnames[x] + ".value") != eval("document." + formname + "." + array_fieldnames[x].substring(0,array_fieldnames[x].indexOf("_confirm")) + ".value"))
				{
					isvalid = 0;
					elvalid = 0;
					var oConfirm = document.getElementById(oFormEl.id);
					oConfirm.style.display = "block";
				}
			}


		}

		if (elvalid == 0)
		{

			if (array_fieldtypes[x] == "radio")
			{
				for (var i=0; i<eval("document." + formname + "." + array_fieldnames[x]).length; i++){
					eval("document." + formname + "." + array_fieldnames[x])[i].className = "invalid_input";
				}
			}else if (array_fieldtypes[x] == "checkbox"){
				for (cb=0; cb<numcbox; cb++){

					eval("document." + formname + "." + namecbox + "_" + cb + ".className = 'invalid_input'");
				}
			}else{
				eval("document." + formname + "." + array_fieldnames[x] + ".className = 'invalid_input'");
			}
		}else{
			if (array_fieldtypes[x] == "radio")
			{
				for (var i=0; i<eval("document." + formname + "." + array_fieldnames[x]).length; i++){
					eval("document." + formname + "." + array_fieldnames[x])[i].className = "valid_input";
				}

			}else if (array_fieldtypes[x] == "checkbox"){
				for (cb=0; cb<numcbox; cb++){

					eval("document." + formname + "." + namecbox + "_" + cb + ".className = 'valid_input'");
				}
			}else{
				eval("document." + formname + "." + array_fieldnames[x] + ".className = 'valid_input'");
			}
		}


	}

	if(isvalid == 0)
	{
		eval("document." + formname + "." + array_fieldnames[0]).focus();
		oFormEl = document.getElementById("form_ermsg");
		oFormEl.style.display = "block";
		return false;
	}else{
		eval("document." + formname + ".verified.value = '1'");
		return true;
	}

}

function valEmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){

	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }

	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 if (str.indexOf("'")!=-1){
	    return false
	 }

	 return true;
}

function valRadio(formField)
{
	var result = "";
	// if undefined then there is only one validate it and return
	//if(formField.length == undefined)
	//{
	//	if(formField.checked)
	//	{
	//		result = true;
	//	}else{
	//		result = false;
	//	}
//
	//}else{

		result = true;
		var radioSelected;
		for (var i=0; i<formField.length; i++){
			if (formField[i].checked) {
			radioSelected = formField[i].value
			}
		}

		if (!radioSelected){
			result = false;
		}
	//}
	return result;

}

function valSelectBox(fieldName,formname) {

	var result = true;
	selected   = document.forms[formname][fieldName].selectedIndex;
	if( selected < 2 ) {
		var result = false;
	}

	return result;
}

function valLength(strLength, limit, operation)
{

	var isvalid = 1;
	// CR 10 jul 2004
	//if(strLength == undefined)
	//{
	//	return false;
	//}

	if(operation == "gt")
	{
		if(strLength <= limit)
		{
			isvalid = 0;
		}
	}
	if(operation == "lt")
	{
		if(strLength >= limit)
		{
			isvalid = 0;
		}
	}

	if(isvalid == 0){
		return false;
	}else{
		return true;
	}

}

function valAlphaNumeric(fieldValue, fieldName, fieldLength, valType)
{

	var validChars = "";
	var invalid = 0;

	if (valType == "alphanumeric" || valType == "numeric" || valType == "alphanumeric_")
	{
		validChars += "0123456789";
	}
	if (valType == "alphanumeric" || valType == "alpha" || valType == "alphanumeric_" || valType == "alpha_")
	{
		validChars += "abcdefghijklmnopqrstuvwxyz";
		validChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	}
	if (valType == "alphanumeric_" || valType == "alpha_")
	{
		validChars += "_";
	}
	for(var i=0;i<fieldLength;i++) {

		if ( validChars.indexOf(fieldValue.charAt(i)) == -1 )
		{
			invalid = 1;
		}
	}
	// CR 10 jul 2004
	//if (invalid == 1 || fieldLength == 0 || fieldLength == undefined)
	if (invalid == 1 || fieldLength == 0)
	{
		return false;
	}else{
		return true;
	}

}

function isDate(dtStr){

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}

	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}

	return true
}

function isTime(str)
{
	if (str.indexOf(":") == -1) return false;

	var bits = str.split(":");

	if (bits.length > 2) return false;

	if (!isInteger(bits[0])) return false;
	if ( (bits[0] < 0) || (bits[0] > 23) ) return false;
	if (!isInteger(bits[1])) return false;
	if ( (bits[1] < 0) || (bits[1] > 59) ) return false;
	return true;
}

function validateEvent()
{
	var valid = true;

	var msg = "";

	if (document.events.contact.value == "")
	{
		valid = false;
		ermsg_contact.style.display = "block";
		//msg += "Please enter your name\n";
	}else{
		ermsg_contact.style.display = "none";
	}

	if (!valEmail(document.events.email.value))
	{
		valid = false;
		ermsg_email.style.display = "block";
	}else{
		ermsg_email.style.display = "none";
	}

	if (document.events.phone.value == "")
	{
		valid = false;
		ermsg_phone.style.display = "block";
		//msg += "Please enter your name\n";
	}else{
		ermsg_phone.style.display = "none";
	}

	if (document.events.category.value == "-1")
	{
		valid = false;
		ermsg_category.style.display = "block";
	}else{
		ermsg_category.style.display = "none";
	}


	if (document.events.subject.value == "")
	{
		valid = false;
		ermsg_subject.style.display = "block";
	}else{
		ermsg_subject.style.display = "none";
	}

	if (document.events.description.value == "")
	{
		valid = false;
		ermsg_description.style.display = "block";
	}else{
		ermsg_description.style.display = "none";
	}

	ermsg_startdate.style.display = "none";
	ermsg_enddate.style.display = "none";
	if (!isDate(document.events.startdate.value))
	{
		valid = false;
		ermsg_startdate.style.display = "block";
	}

	if ( (!document.events.allday.checked) && (!isTime(document.events.starttime.value)) )
	{
		valid = false;
		ermsg_startdate.style.display = "block";
	}

	if ( (!document.events.allday.checked) && (!isDate(document.events.enddate.value)) )
	{
		valid = false;
		ermsg_enddate.style.display = "block";
	}

	if ( (!document.events.allday.checked) && (!isTime(document.events.endtime.value)) )
	{
		valid = false;
		ermsg_enddate.style.display = "block";
	}

	if (valid)
	{
		var sdbits = document.events.startdate.value.split("/");
		var sd = new Date(Date.parse(sdbits[1]+"/"+sdbits[0]+"/"+sdbits[2] + " " + document.events.starttime.value));
		edbits = document.events.enddate.value.split("/");
		var ed = new Date(Date.parse(edbits[1]+"/"+edbits[0]+"/"+edbits[2] + " " + document.events.endtime.value));

		if (ed < sd)
		{
			alert("The end date you have added is before the start date");
			document.events.enddate.value = document.events.startdate.value;
			valid = false;
		}else{
			document.events.startdate.value = sd.getFullYear() + "-" + (sd.getMonth()+1) + "-" + sd.getDate();
			document.events.enddate.value = ed.getFullYear() + "-" + (ed.getMonth()+1) + "-" + ed.getDate();
		}

		if (document.events.allday.checked)
		{
			document.events.starttime.value = "00:00";
			document.events.endtime.value = "00:00";
			document.events.enddate.value = document.events.startdate.value;
		}

	}

	//if (!valid) alert(msg)

	if (valid) document.events.verified.value = '1';

	return valid;
	//return false;
}

function jump(){}

function clearSearchMask(fieldname)
{
	var field = document.getElementById(fieldname);
	if (field != null) field.value = "";
}

var searchtype = "site";
function checkSearch(search)
{
	if (searchtype == "web")
	{
		search.action = "http://www.google.co.uk/search";
		search.target = "_blank";
	}else{
		search.removeAttribute("target");
		search.action = "/search.jsp";
	}
}

function clearSearch(field)
{
	if (field.value == "Site Search")
	{
		field.value = "";
	}
}

function checkSearchReset(field)
{
	if (field.value == "")
	{
		field.value = "Site Search";
	}
}


var isFlashPresent = 0;
var FlashVersion = 0;
var isIE = "false";

if (navigator.plugins && navigator.plugins.length)
{
	var isNavigator = navigator.plugins["Shockwave Flash"];
	if (isNavigator)
	{
		isFlashPresent = 2;
		if (isNavigator.description)
		{
			var navDesc = isNavigator.description;
			FlashVersion = navDesc.charAt(navDesc.indexOf('.')-1);
		}
	}else{
		isFlashPresent = 1;
	}
}else if (navigator.mimeTypes && navigator.mimeTypes.length){
	var isNavigator = navigator.mimeTypes['application/x-shockwave-flash'];
	if (isNavigator && isNavigator.enabledPlugin)
	{
		isFlashPresent = 2;
	}else{
		isFlashPresent = 1;
	}
}else{
	isIE = "true";
}

if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('Opera') == -1))
{
	document.writeln(' <script language="VBScript"> ');
	document.writeln(' on error resume next ');
	document.writeln(' If isIE = "true" Then ');
	document.writeln(' For i = 2 to 7 ');
	document.writeln(' If Not(IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & i))) Then ');
	document.writeln(' Else ');
	document.writeln(' isFlashPresent = 2 ');
	document.writeln(' FlashVersion = i ');
	document.writeln(' End If ');
	document.writeln(' Next ');
	document.writeln(' End If ');
	document.writeln(' If isFlashPresent = 0 Then ');
	document.writeln(' isFlashPresent = 1 ');
	document.writeln(' End If ');
	document.writeln(' <\/script> ');
}

function opendemo(filename)
{
	demo = window.open("/_static/"+filename,"_demo", "width=1014,height=620,left=0,top=0");
}

function bannercycler()
{
	var i = Math.round(1*Math.random())

	if (i == 0)
		document.write('<a href="http://oas-eu.247realmedia.com/5c/insider/1111427377/x02/TrinityMirror/165522/icplogo_web_banner.gif/35313965363537623432326633653730" target="_blank"><img src="/images/banners/icplogo_web_banner.gif" border="0"/></a>');
	else
		document.write('<a href="http://oas-eu.247realmedia.com/5c/insider/1304889751/x01/TrinityMirror/emphub-c-328136-ros/Employment_Hub_banner3.gif/35313965363537623432326633653730" target="_blank"><img src="/images/banners/Employment_Hub_banner3.gif" border="0"/></a>');

}

function toggleBranch(oImg)
{
	var oBranch = oImg.parentNode;
	var oChildren = oBranch.nextSibling;

	if (oChildren.style.display == "none")
	{
		oChildren.style.display = "block";
		oImg.src = "/images/site/o.gif";
	}else{
		oChildren.style.display = "none";
		oImg.src = "/images/site/c.gif";
	}
}

function arrow_on(link)
{
	if (document.getElementById) link.firstChild.src = "/images/site/arrow_on.gif";
}

function arrow_off(link)
{
	if (document.getElementById) link.firstChild.src = "/images/site/arrow_off.gif";
}

function init()
{
	if (document.getElementById)
	{
		var myWidth = 0;
		var myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' )
		{
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		document.getElementById("main").style.height = myHeight - 160;
		document.getElementById("tocdiv").style.height = myHeight - 160;
	}
}

function toggleBtn(link,  bg)
{
	if (document.getElementById && link.parentNode.className != "current")
	{
		link.parentNode.style.backgroundImage = "url('/images/site/"+bg+".gif')";
	}
}
