if (!window.AJS) {

        alert("Please load the AJS.js library first before loading Common.js!");
}


var Common = {

	// ajax

	ajax: {

		queryString: "",
		callBack: function() {},
		errBack: function() {},
		firstParam: true,
		running: false,


		start: function () {

			if (this.running) {
				return false;
			}

			this.running = true;
			return true;
		},

		finish: function () {
	
			if (! this.running) {
				return false;
			}

			this.running = false;
			this._reset();
		},

		setParam: function ( k, v) {

			if (this.firstParam) {
				this.queryString += "?" + k + "=" + v;
				this.firstParam = false;
			} else {
				this.queryString += "&" + k + "=" + v;
			}
		},

		setCallBack: function ( cb ) {
	
			this.callBack = cb;
		},

		setErrBack: function ( cb ) {

			this.errBack = cb;
		},

		get: function (url) {

			if (this.queryString.length > 0) 
			  url += this.queryString;

		        var d = AJS.getRequest(url, null, "GET")
        		d.addCallback(this.callBack)
        		d.addErrback(this.errBack)
	        	d.sendReq()
		},

		includeform: function (fid) {

			var elms = AJS.$bytc('input', 'ajax', $(fid))

			for (var i=0; i < elms.length; i++)
			  this.setParam(elms[i].name,elms[i].value);

		},

		_reset: function () {

			this.queryString = "";
			this.firstParam = true;
			this.callBack = function() {};
			this.errBack = function() {};
		},

		simple: {

			load: function (id, url) {

				// just loads a URL into a DIV or other element..

				new Effect.Fade( id, {duration: .5, afterFinish: function() {

					$(id).innerHTML = "<br><br><br><center><img src = 'v2/images/progress.gif'></center>";
					new Effect.Appear( id, {duration: .3});

				}});				

			        Common.ajax.start()
        			Common.ajax.setCallBack( function(res, req) {   

					// hide progress bar before we show it
					new Effect.Fade( id, {duration: .5, afterFinish: function() {

						$(id).innerHTML = res;

						new Effect.Appear( id, {duration: .3});

					}});				


				});

				Common.ajax.get(url);
				Common.ajax.finish() 
			}

		}

	},


	// Browser

	browser: {

		isLinux: function() {

			var nav = navigator.userAgent;
			if (nav.match('Linux')) {
				return true;
			} else {
				return false;
			}

		}
	},

	// Sounds

	sound: {

		// dont do any sound functions on linux


		playOnload: function(uri) { 

			if (Common.browser.isLinux()) return false;

			var MSIE=navigator.userAgent.indexOf("MSIE");        
			var NETS=navigator.userAgent.indexOf("Netscape");
			var OPER=navigator.userAgent.indexOf("Opera");
			if((MSIE>-1) || (OPER>-1)) {
			        document.write("<BGSOUND SRC=" + uri + " LOOP=0>");
			} else {
        			document.write("<EMBED SRC=" + uri + " AUTOSTART=TRUE ");
        			document.write("HIDDEN=true VOLUME=50 LOOP=FALSE>");
			}
		},

		preLoad: function(uri) { 

			if (Common.browser.isLinux()) return false;

			var MSIE=navigator.userAgent.indexOf("MSIE");        
			var NETS=navigator.userAgent.indexOf("Netscape");
			var OPER=navigator.userAgent.indexOf("Opera");
			if((MSIE>-1) || (OPER>-1)) {
			        document.write("<BGSOUND SRC=" + uri + " LOOP=0>");
			} else {
        			document.write("<EMBED SRC=" + uri + " AUTOSTART=FALSE ");
        			document.write("HIDDEN=true VOLUME=50 LOOP=FALSE>");
			}
		},
		
		play: function(uri) {

			if (Common.browser.isLinux()) return false;

			// append an iframe with our play_sound.php script
			AJS.ACN( AJS.getBody(), AJS.IFRAME({

				src: '/ajax/play_sound.php?uri=' + uri, 
				className: 'hidden'

				}, '')
			)

		}

	},



	// Windowing Toolkit

	windows: {

		url: function(title, url, w, h) {

        		if (! w ) w = 400;
        		if (! h) h = 300;

        		var win = new Window({className: 'alphacube', title: title, url: url, width: w, height:h, destroyOnClose: true, recenterAuto:true});
        		win.showCenter();
			return win;
		}

	},

	// Payments

	creditcard: {

		isValid: function (s) {
			var v = "0123456789";
			var w = "";
			for (i=0; i < s.length; i++) {
				x = s.charAt(i);
				if (v.indexOf(x,0) != -1)
				w += x;
			}
			j = w.length / 2;
			if (j < 6.5 || j > 8 || j == 7) return false;
			k = Math.floor(j);
			m = Math.ceil(j) - k;
			c = 0;
			for (i=0; i<k; i++) {
				a = w.charAt(i*2+m) * 2;
				c += a > 9 ? Math.floor(a/10 + a%10) : a;
			}
			for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
			return (c%10 == 0);
		}
	}
}

