/* BAS JS */

function $(element) {
  return document.getElementById(element);
}

var BAS = {	
	version: 1.0,
	debug: false,
	showVersion: function() {
		var msg = "BAS Javascript Framework: Version " + this.version;
		if(this.debug) {
			msg = msg + "\nDebug Mode Active. Verbose messages will be displayed.";
		} else {
			msg = msg + "\nProduction mode active.";
		}
		alert(msg);
	}	
};

/* BAS.XMLHttp Connection Class */
/* Based on : */
/* Simple AJAX Code-Kit (SACK) v1.6.1 */
/* 2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence,
   see documentation or authors website for more details */
   
BAS.XMLHttp = function() {
	
	this.xmlhttp = false;
	
	//Errors
	this.error = true;
	this.errorMsg = "Not initialized";
	
	//Method
	this.method = "GET";
	this.queryStringSeparator = "?";
	this.argumentSeparator = "&";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;
	this.element = null;
	this.elementObj = null;
	this.requestFile = "";
	this.vars = new Object();
	this.responseStatus = new Array(2);	
	
	//Callback parameters	
	this.completionParams = new Array(0);
	
	//Create XMLHttpRequest Object
	this.createRequestObject = function() { 
		try {
			//Should support IE7+,Firefox 1.0.7+, Safari,Opera
			this.xmlhttp = new XMLHttpRequest();
		} catch(e) {
			//Legacy IE6 and below
			var xmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
											'MSXML2.XMLHTTP.5.0',
											'MSXML2.XMLHTTP.4.0',
											'MSXML2.XMLHTTP.3.0',
											'MSXML2.XMLHTTP',
											'Microsoft.XMLHTTP');
											
			for(var i=0; i< xmlHttpVersions.length && !this.xmlhttp; i++) {
				try {
					this.xmlhttp = new ActiveXObject(xmlHttpVersions[i]);
				} catch(e2) {}
			}
		}// end catch
		
		//Return request object
		if(!this.xmlhttp) {
			this.xmlhttp = false;
			this.error = true;
			this.errorMsg = "Could not create XMLHttpRequest object";
		} else {
			this.error = false;
			this.errorMsg = "";
		}
	};
	
	//Initialization routines
	this.resetData = function() {
		this.method = "GET";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = "";
		this.vars = new Object();
		this.responseStatus = new Array(2);
		this.completionParams = new Array(0);
  	};

	this.resetFunctions = function() {
  		this.onLoading = this.catchBlank;
  		this.onLoaded = this.catchBlank;
  		this.onInteractive = this.catchBlank;
  		this.onCompletion = this.catchBlank;
  		this.onError = this.catchBlank;
		this.onFail = this.catchBlank;
	};
	
	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};	
	
	this.catchBlank = function() {		
	};	
	
	this.setURL = function(url) {
		this.requestFile = url
	};
	
	//Parameter passing
	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};
	
	this.addCompletionParam = function(value) {
		this.completionParams.push(value);	
	};
	
	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	};
	
	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;	
					if(self.xmlhttp.readyState == 0 || self.xmlhttp.readyState == 4) {
						this.xmlhttp.open(this.method, totalurlstring, true);
					}
				} else {
					if(self.xmlhttp.readyState == 0 || self.xmlhttp.readyState == 4) {
						this.xmlhttp.open(this.method, this.requestFile, true);
						try {
							this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
						} catch (e) { }
					}
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {								
								if(self.completionParams.length <= 0)
									self.onCompletion();
								else if(self.completionParams.length == 1)
									self.onCompletion(self.completionParams[0]);
								else if(self.completionParams.length == 2)
									self.onCompletion(self.completionParams[0],self.completionParams[1]);
								else if(self.completionParams.length == 3)
									self.onCompletion(self.completionParams[0],self.completionParams[1],self.completionParams[2]);	
								else if(self.completionParams.length == 4) {								
									self.onCompletion(self.completionParams[0],self.completionParams[1],self.completionParams[2],self.completionParams[3]);										
								}
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};	
	
	//Error Handling
	this.isError = function() {
		if(this.error)
			return true;
		return false;
	};
	
	this.showError = function() {
		if(this.error)
			alert(this.errorMsg);
		else
			alert("There is no error");
	};
	
	//Initialize component
	this.reset();
	this.createRequestObject();
};

BAS.Utils = {
	cElementNone: 0,
	cElementBlock: 1,
	cElementInline: 2,
	windowClose: function() {
		window.close();
	},
	hidediv: function(id) {
		//safe function to hide an element with a specified id
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(id).style.display = 'none';
		}
		else {
			if (document.layers) { // Netscape 4
				document.id.display = 'none';
			}
			else { // IE 4
				document.all.id.style.display = 'none';
			}
		}
	},
	showdiv: function(id,elementType) {

		//safe function to show an element with a specified id
		var element_display = 'block';
		if(elementType == this.cElementInline) {
			element_display = 'inline';
	  	}

		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(id).style.display = element_display;
		}
		else {
			if (document.layers) { // Netscape 4
				document.id.display = element_display;
			}
			else { // IE 4
				document.all.id.style.display = element_display;
			}
		}
	},
	togglediv: function(id) { 
		if (document.getElementById) {
		    if(document.getElementById(id).style.display == 'none'){
		      document.getElementById(id).style.display = 'block';
		    }else{
		      document.getElementById(id).style.display = 'none';
		    }
		} else { //Compatibility
			if (document.layers) { // Netscape 4
				if(document.id.display == 'none') {
					document.id.display = 'block';
				} else {
					document.id.display = 'none';
				}
			}
			else { // IE 4
				if(document.all.id.style.display == 'none') { 
					document.all.id.style.display = 'none';
				} else {
					document.all.id.style.display = 'none';
				}
			}		
		}
	},	
	masktwo: function(str,textbox,loc,l,delim,chg,delim2,e) {
		var locs = loc.split(',');
	
		if(str.length > l) {
			str = str.substring(0,l);
		}
	
		for (var i = 0; i <= locs.length; i++) {
			for (var k = 0; k <= str.length; k++) {
				if ( k > chg) {
					if (k == locs[i]) {
						if (str.substring(k, k+1) != delim2){
							if (e.keyCode != 8){ //backspace
								str = str.substring(0,k) + delim2 + str.substring(k,str.length);
							}
						}
					}
				} else {
					if (k == locs[i]) {
						if (str.substring(k, k+1) != delim){
							if (e.keyCode != 8){ //backspace
								str = str.substring(0,k) + delim + str.substring(k,str.length);
							}
						}
					}			
				}
			} 
		}
		textbox.value = str;
	},
	mask: function(str,textbox,loc,l,delim,e) {
		var locs = loc.split(',');
		
		if(str.length > l) {
			str = str.substring(0,l);
		}
		
		for (var i = 0; i <= locs.length; i++) {
			for (var k = 0; k <= str.length; k++) {
				if (k == locs[i]) {
					if (str.substring(k, k+1) != delim){
						if (e.keyCode != 8){ //backspace
							str = str.substring(0,k) + delim + str.substring(k,str.length);
						}
					}
				}
			} 
		}
		textbox.value = str;
	}	
};

