/*
- - ( FILE INFO ) - - - - - - - - - - - - - - - - - - - - - - - - 
 Name:           general.js
 Title:          General scripts run on all pages
 Author:         Colin Mc Mahon [Protomatter Web Solutions]
                 www.protomatter.co.uk
 Version:        1.0
 Updated:        08/03/2007
- - - - - - - -  - - - - - - - - - - - - - - - - - - - - - - - - - 
*/
var Utilities = {
	goUrl : function(url) {
		if (url === 'back') {
			history.go(-1);
			return false;
		} else {
			document.location = url;
		}
	},
	ConfirmSubmit : function() {
		var str = "Are you sure you wish to continue?";
		if (arguments[1] != null) str = arguments[1] + "\n" + str;
		var agree = window.confirm(str);
		if (agree) {
			if (arguments[0] != null) {
				this.goUrl(arguments[0]);
			} else {
				return true;
			}
		} else {
			return false;
		}
	},
	OpenImage : function(in_img) {
		Utilities.OpenWindow('/assets/images/images.asp?img=' + in_img, "ImgWindow", 200, 200, 0);
		return false;
	},
	OpenWindow : function(in_url, in_win_id, in_width, in_height, in_scroll_bars) {
		if (in_width =="" || in_width == null) in_width = 486;
		if (in_height == "" || in_height == null) in_height = 500;
		var features ='directories=0,location=0,menubar=0,scrollbars=' + in_scroll_bars + ',status=0,toolbar=0,resizable=1,width=' + in_width + ',height=' + in_height +	',screenX=15,screenY=15,top=15,left=15';
		in_url = in_url.replace(/\s/,'%20');
		var wind=window.open (in_url, in_win_id, features);
		wind.focus();
	}
};

/* --------------------------------------------------
   Script checks for required class form elements and
   blocks form submission if blank
   Copyright (C) Colin Mc Mahon, Protomatter Web
   Solutions 2007
   -------------------------------------------------- */
var Checkform = {
	init : function(in_text) {
		var aForms = document.getElementsByTagName('form');
		for (var i=0; i<aForms.length; i++) {
			var aRequired = $(aForms[i]).getElementsByClassName('required');
			aForms[i].aRequired = aRequired;
			addEvent(aForms[i], 'submit', function(evt){
				var ok = true;
				for (var k=0; k<this.aRequired.length; k++) {
					var val = this.aRequired[k].value;
					if(val == '' || val == in_text || val == '-1') {
						ok = false;
						var pdiv = $(this.aRequired[k]).getParentByTagName('div');
						$(pdiv).addClass('field_error');
					} else {
						var pdiv = $(this.aRequired[k]).getParentByTagName('div');
						$(pdiv).removeClass('field_error');
					}
				}
				if(ok==false) {
					alert('Please complete all required fields!');
					evt.preventDefault();
				};
			});
		}
	}
}

/* --------------------------------------------------
   Script highlights form errors post form submission
   given a list of field ids
   Copyright (C) Colin Mc Mahon, Protomatter Web
   Solutions 2007
   -------------------------------------------------- */
var HighlightFields = {
	init : function (in_fields, in_cmts) {
		addEvent(window,'DOMContentLoaded',function(){
			var fldAr = in_fields;
			var cmtAr = in_cmts;
			for (var i=0; i<fldAr.length; i++) {
				var tmpElem = fldAr[i];
				if (tmpElem.indexOf('file_')!=-1 || tmpElem.indexOf('img_')!=-1) {
					tmpElem = tmpElem.split("__");
					tmpElem = tmpElem[1];
				}
				var parentDiv = $(tmpElem).getParentByTagName('div');
				$(parentDiv).addClass('field_error');
				$(parentDiv).addContent('<p class="field_comment"><span>' + cmtAr[i] + '</span></p>');
			}
		});
	}
};

/* --------------------------------------------------
   Script simulates 'position: fixed;' on absolute
   elements by moving them up and down with window
   scroll
   Copyright (C) Colin Mc Mahon, Protomatter Web
   Solutions 2007
   -------------------------------------------------- */
var FixedElements = {
	init : function(offset) {
		var fixed_els = document.getElementsByClassName('fixed');
		for (var i=0; i<fixed_els.length; i++) {
			 var s;
			// scrolling offset calculation via www.quirksmode.org 
			if (self.pageYOffset) {
				s = self.pageYOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	
				s = document.documentElement.scrollTop;
			} else if (document.body) {
				s = document.body.scrollTop;
			}
			fixed_els[i].style.top= s + offset + "px";
		}
	}
}

var ClearFeedback = {
	init : function() {
		var fback = $('system_message')
		if(fback) {
			setTimeout(function(){
				fback.remove();
			}, 5000);
		}
	}
}

/* --------------------------------------------------
   General page initialisation
   -------------------------------------------------- */
g_LoadEvents = function() {
	Checkform.init();
	ClearFeedback.init();
}
g_ScrollEvents = function() {
	FixedElements.init(20);
}
addEvent(window, 'load', g_LoadEvents);
//addEvent(window, 'scroll', g_ScrollEvents);