/*
- - ( 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();
		return wind;
	},
	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
		};
	}
};

function getElementsByTagNameMultiple( tag_names, parent_node ) {
	if( parent_node == undefined ) {
		parent_node = document;
	}
	var out = new Array();
	for( var i = 0; i < tag_names.length; i++ ) {
		elementsFound =
		parent_node.getElementsByTagName(tag_names[i]);
		for (var j = 0; j < elementsFound.length; j++)
			out.push( elementsFound.item(j) );
		}
	return out;
}

function getAllFormElements( parent_node )
{
    return getElementsByTagNameMultiple(['input', 'textarea', 'select', 'button'], parent_node);
}



/* --------------------------------------------------
   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) {
		$('form').each(function(){
			$(this).submit(function(evt){
				var ok = true;
				$(this).find('.required').each(function(){
					var val = this.value;
					if($(this).hasClass("markItUpEditor"))
						var pdiv = $(this).parents('div').get(2);
					else
						var pdiv = $(this).parents('div').get(0);
					
					if(val == '' || val == in_text || val == '-1') {
						ok = false;
						$(pdiv).addClass('field-error');
					} else {
						$(pdiv).removeClass('field-error');
					}
				});

				if(ok==false)
				{
					alert('Please complete all required fields!');
					evt.preventDefault();
				}
				else
				{
				    $(this).find('input[type="submit"]').each(function(){
						$(this).addClass("disabled").click(function(){
							evt.preventDefault();
						});
					});
				};
			});
		});
	}
}

var HighlightFields = {
	init : function (in_fields, in_cmts) {
		$(function(){
			var fldAr = in_fields;
			var cmtAr = in_cmts;
			var parentDiv;
			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];
				}
				parentDiv = $("#" + tmpElem).parents('div').get(0);
				$(parentDiv).addClass('field-error');
				if(cmtAr[i] != "")
					$(parentDiv).append('<p class="field-error-detail">' + cmtAr[i] + '</p>');
			}
		});
	}
};


/* --------------------------------------------------
   General page initialisation
   -------------------------------------------------- */
g_LoadEvents = function() {
	Checkform.init();
}
//Event.observe(window, 'load', g_LoadEvents);
$(document).ready(g_LoadEvents);