//set the onload function to initNav
window.onload = initNav;

function printPage() {
	print(document);
	}
	
function selectThis(obj,state){
	if (obj.className=="selected") return false;
	
	if(state){
		obj.oldClass=obj.className;
		//check this isn't the "home" div
		if (obj.className !="home" && obj.className!="home selected"){
			obj.className="mouseOver";
		} else {
			obj.className = "home mouseOver";	
		}
		
	} else {
		obj.className=obj.oldClass;
	}
	}
function clickThis(obj){
window.location.href=obj.firstChild.href;
}

function initNav(){

nav = document.getElementById('divNav');

arrLinkDivs = nav.getElementsByTagName('p');
	for (n=0; n < arrLinkDivs.length; n++){
	
		//if (arrLinkDivs[n].className=="subNavItem" || arrLinkDivs[n].className=="subNavItem lastNav"){
		
			//add the on mouseover /out functionality
			arrLinkDivs[n].onmouseover = function(){selectThis(this,true)};
			arrLinkDivs[n].onmouseout = function(){selectThis(this,false)};
			
			//get the a tag href and make the onclick event for this div
			aArr = arrLinkDivs[n].getElementsByTagName("a");
			arrLinkDivs[n].href = aArr[0].href;
			arrLinkDivs[n].onclick = function(){ window.location = this.href; }
			
		//}
		
	}
	

  
  var nav_links = document.getElementById('divNav').getElementsByTagName('a');
  var selected = location.pathname +  location.search;

  for (var i = 0; i < nav_links.length; i++) {
    var link = nav_links[i].pathname + nav_links[i].search;
	
    if (link.substring(0, 1) != '/') link = '/' + link; // fiddle IE's view of the link
    if (link == selected) {
		nav_links[i].parentNode.setAttribute("class", 'selected');
		nav_links[i].parentNode.className="selected";
	}
  }

	
	
}

function submitContact(szForm){
	
	objForm =  document.getElementById(szForm);
	
	//if there's nothing found bail out
	if (!objForm) return;
	
	//set up var for overall validity
	isValid = true;
	
	//set up array for error messages
	arrErr  = new Array();
	
	//check for presence of something in all fields
	if (objForm.title.value == "Other" && objForm.other.value==""){
		//arrErr.push("Please select a value for 'other' title");
		objForm.other.className = "missingField";
		isValid=false;
	} else {
		objForm.other.className = null;	
	}
	if (objForm.firstname.value =="") {
		//arrErr.push("Please enter your first name");
		objForm.firstname.className = "missingField";
		isValid=false;
	} else {
		objForm.firstname.className = null;	
	}
	if (objForm.surname.value =="") {
		//arrErr.push("Please enter your surname");
		objForm.surname.className = "missingField";
		isValid=false;
	} else {
		objForm.surname.className = null;	
	}
	
	// Added by MH Ticket 1886
	if (objForm.address1.value =="") {
		isValid=false;
		objForm.address1.className = "missingField";
		isValid=false;
	} else {
		objForm.address1.className = null;	
	}
	
	if (objForm.address2.value =="") {
		isValid=false;
		objForm.address2.className = "missingField";
		isValid=false;
	} else {
		objForm.address2.className = null;	
	}
	
	if (objForm.postcode.value =="") {
		isValid=false;
		objForm.postcode.className = "missingField";
		isValid=false;
	} else {
		objForm.postcode.className = null;	
	}
	
	if (objForm.areaofinterest.value =="") {
		isValid=false;
		objForm.areaofinterest.className = "missingField";
		isValid=false;
	} else {
		objForm.areaofinterest.className = null;	
	}
	// End of Ticket 1886
	
	if (objForm.email.value =="") {
		isValid=false;
		objForm.email.className = "missingField";
		isValid=false;
	} else {
		objForm.email.className = null;	
	}
	
	if(objForm.subject) {
//		if (objForm.subject.value =="") {  -------------- updated to stop strings with http: in - Marc Lucas 08-08-07 ----------
		var str = objForm.subject.value;
		var reg = new RegExp("http:");
		if ((objForm.subject.value =="") || (reg.test(str))) {
			isValid=false;
			objForm.subject.className = "missingField";
			isValid=false;
		} else {
			objForm.subject.className = null;	
		}
	}
	
	if(objForm.enquiry) {
		
//		if (objForm.enquiry.value =="") { -------------- updated to stop strings with http: in - Marc Lucas 08-08-07 ----------
		var str = objForm.enquiry.value;
		var reg = new RegExp("http:");
		if ((objForm.enquiry.value =="") || (reg.test(str))) {
			isValid=false;
			objForm.enquiry.className = "missingField";
			isValid=false;
		} else {
			objForm.enquiry.className = null;	
		}
	}
	
	var proj = false;
	for(var i=0; i<objForm.elements.length; i++) {
		var e = objForm.elements[i];
		if(e.name == "Sub[]"
		   		&& e.checked) {
			proj = true;
			break;
		}
	}
	
	if(objForm.MKPC) {
		if ( proj == false && objForm.MKPC.checked == false && objForm.MKPSC.checked == false && objForm.CMKPB.checked == false && objForm.NEA.checked == false && objForm.WEA.checked == false && objForm.WEASG.checked == false && objForm.EEA.checked == false && objForm.EEASG.checked == false && objForm.UDAPMG.checked == false && objForm.CMK.checked == false && objForm.MKE.checked == false && objForm.MKW.checked == false) {
			isValid=false
		}
	}
		
		
	//now additionally check for validty of email address
	if (isValidEmail(objForm.email.value,true)){
		validEmail = true;
	} else {
		validEmail = false;
	}	
	
	
	//if isValid is false or it's an invalid email address, we'll alert a box
	//else we'll submit the form
	if (isValid  && validEmail){
		
		//submit the form
		objForm.submit();
	} else {
		if (!isValid && !validEmail) { //missing fields and invalid email address
		alertMsg = "Some required fields are blank,\nAnd the email address you have supplied appears to be invalid\n\nPlease check and retry";
		} else if (!isValid) {
			
			alertMsg = "Some required fields are blank\n\nPlease check and retry";
		} else if (!validEmail){
			alertMsg = "The email address you have supplied appears to be invalid\n\nPlease check and retry";
		}
		
	alert (	alertMsg );
	}
	
}

function deleteCheck() {
	if(!confirm("Are you sure you wish to delete this item?")) { return false; }
}

function getWinSize(){
   var iWidth = 0, iHeight = 0;

   if (document.documentElement && document.documentElement.clientHeight){
       iWidth = parseInt(window.innerWidth,10);
       iHeight = parseInt(window.innerHeight,10);
   }
   else if (document.body){
       iWidth = parseInt(document.body.offsetWidth,10);
       iHeight = parseInt(document.body.offsetHeight,10);
   }

   return {width:iWidth, height:iHeight};
}
function resize_id(obj) {                           
    var oContent = new getObj(obj);
    var oWinSize = getWinSize();
    if ((oWinSize.height - parseInt(oContent.obj.offsetTop,10))>0)
        oContent.style.height = (oWinSize.height - parseInt(oContent.obj.offsetTop,10) - 34);
}