/*
 * COMMON JAVASCRIPT
 *
 * Subject to copyright.
 *
 * Web Development - LOOKsystems Limited
 * mailto:info@looksystems.ltd.uk
 * http://www.looksystems.ltd.uk
 *
 */

// POPUP SUPPORT

var popups = new Object;
var popups_child;

function popups_initialise() {
	if (!document.links.length) return;
	var a = 0;
	while (a < document.links.length) {
		var id = document.links[a].id;
		if (!popups[id]) id = document.links[a].id;
		if (!popups[id] && id.substring(0, 5) == 'popup') id = 'popup';
		if (popups[id] != null || id == 'popup') {
			var href = document.links[a].href;
			document.links[a].href = "javascript:popup('"+id+"','"+href+"')";
			document.links[a].target = "_self";
		}
		a++;
	}
}

function popup(id, url) {
	var name, features;
	if (popups != null) features = popups[id];
	if (features != null) {
		name = features[0];
		var x = features[1];
		var y = features[2];
		var w = features[3];
		var h = features[4];
		if (x < 0) x = (window.screen.availWidth - w) / 2;
		else if (window.screenX != null) x += window.screenX;
		if (y < 0) y = (window.screen.availHeight - h) / 2;
		else if (window.screenY != null) y += window.screenY;
		features = features[5];
		if (features != "") features += ",";
		features += "width="+w+",height="+h;
		features += ",screenX="+x+",screenY="+y;
		features += ",left="+x+",top="+y;
	}
	if (popups_child != null) {
		if (popups_child.close != null) popups_child.close();
		else name = '';
	}
	popups_child = window.open(url, name, features);
	if (popups_child != null && popups_child.focus != null) popups_child.focus();
}

/*
 * Submit Once - insipred on lightbox
 * Lightbox JS: Fullsize Image Overlays 
 * by Lokesh Dhakar - http://www.huddletogether.com
 *
 * For more information on this script, visit:
 * http://huddletogether.com/projects/lightbox/
 */

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function init_submitonce(objForm) {
	if (objForm != undefined) objForm.onsubmit = function() { return submitonce(this); }
	else {
		for (var f = 0; f < document.forms.length; ++f) {
			document.forms[f].onsubmit = function() { return submitonce(this); }
		}
	}
}

var showoverlay = true;
function submitonce(objForm) {

	if (!objForm) return;
	if (showoverlay) {
		var arrayPageSize = getPageSize();
		var objOverlay = document.getElementById('overlay');
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		objOverlay.style.display = 'block';
		var objImg = objOverlay.firstChild;
		objImg.style.top = (arrayPageSize[3] / 2) + 'px';
	}
	for (objElement in objForm.elements) {
		objElement.disabled = true;
	}
	return true;

}

// ACCESSIBLE FORMS

var waiForms = new Object;
function waiform(objForm) {
	if (!typeof(objForm) != 'object') objForm = document.getElementById(objForm);
	if (!objForm || !objForm.id || !waiForms[objForm.id]) return;
	objForm.onsubmit = function() { return waiform_onsubmit(this); }
	for (var e = 0; e < objForm.elements.length; ++e) {
		var element = objForm.elements[e];
		if (element.type != "text" && element.type != "textarea") continue;
		if (!waiForms[objForm.id][element.id]) continue;
		element.onfocus = function() { return waiform_onfocus(this); }
		element.onblur = function() { return waiform_onblur(this); }
		if (element.value == '') element.value = waiForms[objForm.id][element.id];
	}
}

function waiform_onfocus(objInput) {
	if (!objInput) return;
	var objForm = objInput.form;
	if (!objForm || !waiForms[objForm.id]) return;
	if (objInput.value == waiForms[objForm.id][objInput.id]) objInput.value = '';
}

function waiform_onblur(objInput) {
	if (!objInput) return;
	var objForm = objInput.form;
	if (!objForm || !waiForms[objForm.id]) return;
	if (objInput.value == '') objInput.value = waiForms[objForm.id][objInput.id];
}

function waiform_onsubmit(objForm) {
	if (!objForm || !waiForms[objForm.id]) return;
	for (var e = 0; e < objForm.elements.length; ++e) {
		var element = objForm.elements[e];
		if (element.type != "text" && element.type != "textarea") continue;
		if (!waiForms[objForm.id][element.id]) continue;
		if (element.value == waiForms[objForm.id][element.id]) element.value = '';
	}
	return true;
}

// DOCUMENT FOCUS

function sf() {
	var e = 0;
	if (!document.forms.length) return;
	var myform = document.forms[0];
	while (e < myform.elements.length) {
		if (myform.elements[e].type == "text" || myform.elements[e].type == "textarea") {
			myform.elements[e].focus();
			break;
		}
		e++;
	}
}

// DOCUMENT INITIALISE

function init(so) {
	if (so) init_submitonce();
	popups_initialise();
	sf();
}
