// script to setup confirmation box on homepage, to check for accidental clicking on 'new application' button
// SAD, 16/05/07
// edited to stop links on nav responding during application process
// SAD, 29/07/08
// added popup for the action plans on planning_esd, quality_provision, environ_esd and develop_coll
// AGM, 19/03/10
// added minimse function to maximise/minimise divs in /admin/editable/contacts.php
// due to the page being ridiculousely long
// AGM, 10/07/10
function minimise(elem) {
	var e = document.getElementById(elem);
	if(e.style.height==22+'px'){
		e.style.height='auto';
	} else {
		e.style.height=22+'px';
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function stripLink() {
	if (!document.getElementsByTagName) {
		return false;
	}
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		if (links[i].getAttribute("class") == "toConfirm") {
			var dest = links[i].getAttribute("href");
			links[i].onclick = function() {
				confirmation(dest);
				return false;
			}
		}
	}
}

function confirmation(addr) {
	if (confirm("Are you sure you want to start a new application? All data entered will be lost.")){
		window.location = addr + "?new=1";
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function stopLinks() {
	if (!document.getElementsByTagName) {
		return false;
	}
	var links = getElementsByClass('inactive',document,'*');
	for (var i = 0; i < links.length; i++) {
		links[i].onclick = function() {
			return false;
		}
	}
}


function appWarnings() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById('appNav')) return false;
	var nav = document.getElementById('appNav');
	var links = nav.getElementsByTagName('a');
	for (var i = 0; i < links.length; i++) {
		links[i].onclick = function() {
			// IE has problems with getAttribute
			if (!this.getAttribute("class")) {
				var dest = this.getAttribute('href', 2);
				var className = this.getAttribute('className');
			} else {
				var dest = this.getAttribute("href");
				var className = this.getAttribute("class");				
			}
			/*
			alert(dest);
			*/
			var classes = className.split(" ");
			switch (classes[0]) {
				case "leave":
					if (confirm("Are you sure that you want to navigate away from the application?\nAll data entered will be lost.")) {
						window.location = dest;
					}
					break;
				case "restart":
					if (confirm("Are you sure that you wish to return to the start of the application?\nAll data entered will be lost.")) {
						window.location = dest;
					}
					break;
				case "inactive":
					alert("In order to preserve entered data, please navigate using the buttons at the bottom of the page.");
					break;
				default:
					//alert("hi");
					window.location = dest;
					break;
			}
			return false;
		}
	}
}



function popUp(URL, editable) {
	if (editable=='edit'){
		editActionPlanPopup = window.open(URL, 'editPlans', 'toolbar=0,scrollbars=1,lcation=0,statusbar=0,menubar=0,resizable=1,width=800,height=500,left = 312,top = 134');
	} else {
		actionPlanPopup = window.open(URL, 'plans', 'toolbar=0,scrollbars=1,lcation=0,statusbar=0,menubar=0,resizable=1,width=400,height=500,left = 312,top = 134');
		if (actionPlanPopup.opener == null) {
			actionPlanPopup.opener = self;	
		}
	}
}



addLoadEvent(stripLink);
addLoadEvent(appWarnings);
//addLoadEvent(stopLinks);

