/*
- - ( 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);
		} 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();
	},
	setCookie : function(c_name,value,expiredays,path)
	{
		var exdate=new Date()
		exdate.setDate(exdate.getDate()+expiredays)
		document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
		((path) ? "; path=" + path : "");
	},
	getCookie : function(c_name)
	{
		if (document.cookie.length>0)
		{
			c_start=document.cookie.indexOf(c_name + "=")
			if (c_start!=-1)
			{ 
				c_start=c_start + c_name.length+1 
				c_end=document.cookie.indexOf(";",c_start)
				if (c_end==-1) c_end=document.cookie.length
				return unescape(document.cookie.substring(c_start,c_end))
			} 
		}
		return ""
	},
	GetCoords : function (e)
	{
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		return [posx, posy];
	},
	GetPath : function()
	{
		var loc = window.location;
		return {
			protocol: loc.protocol,
			server: loc.hostname,
			path: loc.pathname,
			query: loc.search,
			hash: loc.hash,
			fullurl: loc.protocol + "//" + loc.hostname + loc.pathname + loc.search + loc.hash
		};
	}
};

/* --------------------------------------------------
   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]).find('.required');
			aForms[i].aRequired = aRequired;
			$(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]).parents('div').get(0);
						$(pdiv).addClass('field_error');
					} else {
						var pdiv = $(this.aRequired[k]).parents('div').get(0);
						$(pdiv).removeClass('field_error');
					}
				}
				if(ok==false) {
					evt.preventDefault();
					alert('Please complete all required fields!');
				}
			});
		}
	}
}

/* --------------------------------------------------
   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) {
		$(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).parents('div').get(0);
				$(parentDiv).addClass('field_error');
				$(parentDiv).append('<span class="field_comment">' + cmtAr[i] + '</span>');
			}
		});
	}
};

$(function(){
	Checkform.init();
});
