// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// - - - - - - ( event listener ) - - - - - - - -
// by Scott Andrew - http://scottandrew.com
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false); 
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}
	else
	{
		return false;
	}
}

// run all the onload scripts
function runScripts()
{
	runLSPrep();
}

window.onload = runScripts;

// Freight Calc Stuff
var rid = '';
var tid = '';
var pid = '';

$(document).ready(function(){
	$("#region").bind("change",function(){
		rid = $('#region').querydd();

		if (rid != ''){
			$.get('freight.php?rid=' + rid,function(txt){
				$("#tcontainer").html('<select name="town_id" id="town" size="1">'+txt+"</select>");
				$("#town").tBind();
			});
			$("#fp").addStyle("#fp","display: none").html('Please select nearest Centre above').fadeIn("slow");
		} else {
			$("#town").hide();
			$("#fp").addStyle("#fp","display: none").html('Please select closest Region').fadeIn("slow");
		}
	});

	$("#town").tBind();

	$("#fs").hide();

	$("input[@id='pid']").each(function() {
		pid = this.value;
	});
});

$.fn.tBind = function() {
	return this.each(function(){
		$("#town").bind("change",function(){
			tid = $('#town').querydd();
			if (tid != ''){
				$("#fp").addStyle("#fp","display: none").freight();
			} else {
				$("#fp").addStyle("#fp","display: none").html('Please select nearest Centre above').fadeIn("slow");
			}
		});
	});
};

$.fn.freight = function() {
	return this.each(function(){
		$(this).load('freight.php?rid='+rid+'&tid='+tid+'&pid='+pid).fadeIn("slow");
	});
};

$.fn.querydd = function() {
	var res = '';
	this.find('option').each(function(){
		if(this.selected && this.value != ''){
			res = this.value;
		}
	});

	return res;
};

// Quick Fix
$.fn.addStyle = function(a,b) {
	var d = document;
	var s = d.createElementNS ?
		d.createElementNS("http://www.w3.org/1999/xhtml", "style") :
		d.createElement("style");

	if ( !document.all || navigator.userAgent.indexOf('Opera') > -1 )
		s.appendChild(d.createTextNode(a+" {"+b+"}"));

	$("head").append(s);

	/*@cc_on @*/
	/*@if (@_win32)
		var ss = d.styleSheets;
		if ( ss && ss.length && ss[ss.length - 1].addRule )
			ss[ss.length - 1].addRule( a, b );
	/*@end @*/

	return this;
};
