var standardObjectsCreated = false;
var Seraphim = Class.create({
	Name : 'Seraphim',
  Version : '2.0',
  Author : 'Matt Balogh',
  Company: 'Seraphim Internet Solutions, LLC',
  License: 'MIT License',
  Creation : 'Thu Jun 21 08:48:45 2007',
  LastUpdate : 'Thu Jun 21 08:48:45 2007',
	initialize: function(){
		Seraphim.configObj 			= new Seraphim.config();
		Seraphim.commonObj 			= new Seraphim.common();
		Seraphim.clientInfoObj	= new Seraphim.clientInfo();

		this.error = '';
		this.URL = '';
		this.mode = 'display';
		this.currProcessHash = new Hash();	    
		this.errorPre = '&nbsp;&nbsp;&nbsp;&#183;&nbsp;';
		this.useValidate = true;
		this.asynchronous = false;
		this.method = 'post';

		this.messageDivId = 'message';
		this.timeOutId = [];
		this.timeOutMessageDivId = new Hash();

		standardObjectsCreated  = true;
	},
	init: function(){
		this.clearMessage();
	},
	setError: function(error){
		this.error = error;
	},
	getError: function(){
		return this.error;
	},
	appendError: function(aVal) {
		this.setError(this.getError()+this.getErrorPre()+aVal+'<br/>');
	},
	setMode: function(mode){
		this.mode = mode;
	},
	getMode: function(){
		return this.mode;
	},
	setURL: function(URL){
		this.URL = URL;
	},
	getURL: function(){
		return this.URL;
	},
	setCurrProcessHash: function(currProcessHash){
		this.currProcessHash = currProcessHash;
	},
	getCurrProcessHash: function(){
		return this.currProcessHash;
	},			
	setErrorPre: function(errorPre){
		this.errorPre = errorPre;
	},
	getErrorPre: function(){
		return this.errorPre;
	},
	setUseValidate: function(useValidate) {
		this.useValidate = useValidate;
	},
	getUseValidate: function() {
		return this.useValidate;
	},
	setAsynchronous: function(asynchronous) {
		this.asynchronous = asynchronous;
	},
	getAsynchronous: function() {
		return this.asynchronous;
	},
	setMethod: function(method) {
		this.method = method;
	},
	getMethod: function() {
		return this.method;
	},
	setMessageDivId: function(messageDivId){
		this.messageDivId = messageDivId;
	},
	getMessageDivId: function(){
		return this.messageDivId;
	},
	setTimeOutMessageDivId: function(timeOutId) {
		this.timeOutMessageDivId.set(timeOutId, this.getMessageDivId());
		this.setTimeOutId(timeOutId);
	},
	getTimeOutMessageDivId: function() {
		return this.timeOutMessageDivId.get(this.getTimeOutId());
	},
	setTimeOutId: function(timeOutId) {
		this.timeOutId.push(timeOutId);
	},
	getTimeOutId: function() {
		var rElement = undefined;
		if(this.timeOutId.length > 0) {
			rElement = this.timeOutId.shift();
		}
		return rElement;
	},
	__getParameters: function() {
		// Placeholder
		// Return the parameters
	},
	__callFormSetters: function() {
		// Placeholder
		// move any form values to their respective setters & getters
	},
	__validate: function() {
		// Placeholder
		// Save error to this.setError()
	},
	clearMessage: function(){
		try{
			$(this.getMessageDivId()+'_content').innerHTML = '';
			$(this.getMessageDivId()).hide();
		} catch(e) {
			// no message
		}
	},
	showMessage: function(msg, msgType, blnSlideUp){
		try {
			//$(this.getMessageDivId()+'_content').innerHTML = 'Mode: '+this.getMode()+'<br/>'+msg;
			$(this.getMessageDivId()+'_content').innerHTML = msg;
			if(msgType == 'success') {
				$(this.getMessageDivId()).className = 'success';
				$(this.getMessageDivId()+'_image').src = '/images/success.png'
			} else {
				$(this.getMessageDivId()).className = 'error';
				$(this.getMessageDivId()+'_image').src = '/images/error.png'
			}
			$(this.getMessageDivId()).show();
			if(blnSlideUp) {
				this.setTimeOutMessageDivId(setTimeout(function() { Effect.SlideUp($(this.getTimeOutMessageDivId()), {duration: 0.3});}.bind(this),20000));
			}
		} catch(e) {
			// no message div
			alert(e);
		}
	},
	ajaxRequest: function(funcHash) {
		if(this.getUseValidate()) {
			this.setError('');
			this.__validate();
		} else {
			this.setError('');
		}
		var paramHash = this.__getParameters();
		paramHash.set('rand', this.getRandVal());

		if(this.getError().blank()) {
			new Ajax.Request(this.getURL(), {
				method: this.getMethod(),
				parameters: paramHash,
				asynchronous: this.getAsynchronous(),
				onCreate: function(){
					this.getCurrProcessHash().set(this.getMode(), 'active');
					// this.__clearMessage();
					try {
						$('loading').show();
					} catch(e) {
						// no loading div
					}

					try {
						funcHash.onCreate();
					} catch(e) {
						// onCreate is not always defined
					}
				}.bind(this),
				onSuccess: function(rVal){
					this.getCurrProcessHash().set(this.getMode(), 'complete');
					try {
						funcHash.onSuccess(rVal);
					} catch(e) { }
				}.bind(this),
				onComplete: function(rVal) {
					try {
						$('loading').hide();
					} catch(e) { }

					try {
						funcHash.onComplete(rVal);
					} catch(e) { }
				}.bind(this),
				on404: function(t) {
					this.getCurrProcessHash().set(this.getMode(), 'error');
					this.showMessage("Error 404: Location not found.",'error');

					try {
						funcHash.on404(t);
					} catch(e) { }
				}.bind(this),
				onFailure: function(t) {
					this.getCurrProcessHash().set(this.getMode(), 'error');
					this.showMessage("Error "+t.status+": "+t.statusText,'error');

					try {
						funcHash.onFailure(t);
					} catch(e) { }
				}.bind(this)
			});
		} else {
			this.showMessage('<strong>Please correct the following errors:</strong><br/>'+this.getError(),'error');
		}
	},
	isValidEmail: function(email){
		return Seraphim.commonObj.isValidEmail(email);
	},
	checkParam: function(pVal, pText, pMaxLen) {
		return Seraphim.commonObj.checkParam(pVal, pText, pMaxLen);
	},
	getRandVal: function(){
		return Seraphim.commonObj.getRandVal();
	},
	clearForm: function (form){ 
		Seraphim.commonObj.clearForm(form);
	},
	loadCSS:function(cssFileName,prefixConfigPath){
		Seraphim.commonObj.loadCSS(cssFileName,prefixConfigPath);
	},
	loadJS: function(jsFileName,prefixConfigPath){
		Seraphim.commonObj.loadJS(jsFileName,prefixConfigPath);
	},
	getCookie:function(name) {
		return Seraphim.commonObj.getCookie(name);
	},
	setCookie:function(name,value,expires,path,domain,secure){
		Seraphim.commonObj.setCookie(name,value,expires,path,domain,secure);
	},
	deleteCookie:function(name, path, domain){
		Seraphim.comcommonObjmon.deleteCookie(name, path, domain);
	},
	preloadImages:function(arg){
		Seraphim.commonObj.preloadImages(arg);
	},
	getGetValue: function(getVar) {
		return Seraphim.commonObj.getGetValue(getVar);
	},
	checkParam: function(pVal, pText, pMaxLen) {
		var errMsg = Seraphim.commonObj.checkParam(pVal, pText, pMaxLen);
		if(!errMsg.blank()) {
			this.appendError(errMsg);
			return false;
		}
		return true;
	},
	getBrowser:function(){
		return Seraphim.clientInfoObj.getBroswer();
	},
	isOpera:function(){
		return Seraphim.clientInfoObj.isOpera();
	},
	isFirefox:function(){
		return Seraphim.clientInfoObj.isFirefox();
	},
	isMSIE:function(){
		return Seraphim.clientInfoObj.isMSIE();
	},
	isOldMSIE:function(){
		return Seraphim.clientInfoObj.isOldMSIE();
	},
	isSafari:function(){
		return Seraphim.clientInfoObj.isSafari();
	},
	getNavigatorVersion:function(){
		return Seraphim.clientInfoObj.getNavigatorVersion();
	},
	getBrowserWidth:function(){
		return Seraphim.clientInfoObj.getBrowserWidth();
	},
	getBrowserHeight:function(){
		return Seraphim.clientInfoObj.getBrowserHeight();
	},
	getClientWidth: function(){
		return Seraphim.clientInfoObj.getClientWidth();
	},
	getClientHeight: function(){
		return Seraphim.clientInfoObj.getClientHeight();
	},
	getClientScrollTop: function(){
		return Seraphim.clientInfoObj.getClientScrollTop();
	},
	getScrollWidth: function(){
		return Seraphim.clientInfoObj.getScrollWidth();
	},
	getScrollHeight: function(){
		return Seraphim.clientInfoObj.getScrollHeight();
	},
	getTopPos: function(el){
		return Seraphim.clientInfoObj.getTopPos(el);
	},
	getLeftPos: function(el){
		return Seraphim.clientInfoObj.getLeftPos(el);
	},
	hasScrollbarHeight: function(el){
		return Seraphim.clientInfoObj.hasScrollbarHeight(el);
	},
	hasScrollbarWidth: function(el){
		return Seraphim.clientInfoObj.hasScrollbarWidth(el);
	}
});

//* COMMON *//
Seraphim.common = Class.create({
	initialize: function(){
		this.loadedCSSFiles = [];
		this.loadedJSFiles 	= [];
		this.existsJSFiles 	= [];
		this.cssCacheStatus = true;
		this.jsCacheStatus 	= true;
	},
	isValidEmail: function(email){
		var myregexp = /^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
		var match = myregexp.exec(email);
		return (match !== null) ? true : false;
	},
	getRandVal: function(){
		var rndVal = new Date().getTime();
		return rndVal;
	},
	checkParam: function(pVal, pText, pMaxLen) {
		var errMsg = '';
		pVal = pVal + '';
		if(pVal.blank()) {
			errMsg = 'Missing '+pText;
		}
		if(pMaxLen) {
			if(pVal.length > pMaxLen) {
				errMsg = pText+' can be no longer than '+pMaxLen+' characters';
			}
		}
		return errMsg;
	},
	clearForm: function (form){ 
		for (i=0;i< form.elements.length;i++) {
			if (form.elements[i].type == "text" || form.elements[i].type == "textarea") {
				form.elements[i].value="";
			} else if (form.elements[i].type.substr(0,7) == 'select-' && form.elements[i].options[0]) {
				form.elements[i].options[0].selected = true;
			} else if (form.elements[i].type == "checkbox" || form.elements[i].type == "radio") {
				form.elements[i].checked = false;
			} else if (form.elements[i].type == "file") {
				form.elements[i].value="";
			}
		}	    
	},
	getGetValue: function(getVar) {
	  var qString = window.location.search.substring(1);
	  var vars = qString.split("&");
	  for(var i=0;i<vars.length;i++) {
	    var pair = vars[i].split("=");
	    if (pair[0] == getVar) {
	      return pair[1];
	    }
	  } 
		return '';
	},
	loadCSS:function(cssFileName,prefixConfigPath){
		if(!prefixConfigPath && prefixConfigPath!==false){
			prefixConfigPath=true;
		}
		if(!this.loadedCSSFiles[cssFileName]){
			this.loadedCSSFiles[cssFileName] = true;
			var linkTag = document.createElement('LINK');
			if(cssFileName.indexOf('?')>=0){
				cssFileName=cssFileName+'&';
			}else{
				cssFileName=cssFileName+'?';
			}
			cssFileName=cssFileName+'rand='+ Math.random();
			if(prefixConfigPath){
				linkTag.href = Seraphim.configObj.cssPath + cssFileName;
			}else{
				linkTag.href = cssFileName;
			}
			linkTag.rel = 'stylesheet';
			linkTag.media = 'screen';
			linkTag.type = 'text/css';
			document.getElementsByTagName('HEAD')[0].appendChild(linkTag);
		}
	},
	loadJS: function(jsFileName,prefixConfigPath){
		if(!prefixConfigPath && prefixConfigPath!==false){
			prefixConfigPath = true;
		}
		if(!this.loadedJSFiles[jsFileName]){
			this.loadedJSFiles[jsFileName] = true;
			if(prefixConfigPath){
				jsFileName = Seraphim.configObj.getJsPath() + jsFileName;
			}
			var scriptTag = document.createElement('SCRIPT');
			scriptTag.src 			= jsFileName;
			scriptTag.type			= "text/javascript";
			scriptTag.language	= "JavaScript1.2";
			var head = document.getElementsByTagName('HEAD')[0];
			$A(head.getElementsByTagName('SCRIPT')).each((function(script){
				if (scriptTag.src == script.src){
					this.existsJSFiles[jsFileName]=true;
				}
			}).bind(this));
			if (!this.existsJSFiles[jsFileName]){
				head.appendChild(scriptTag);
			}
		}
	},
	getCookie:function(name) {
		var start = document.cookie.indexOf(name+"=");
		var len = start+name.length+1;
		if ((!start) && (name != document.cookie.substring(0,name.length))){
			return null;
		}
		if (start == -1){
			return null;
		}
		var end = document.cookie.indexOf(";",len);
		if (end == -1){
			end = document.cookie.length;
		}
		return unescape(document.cookie.substring(len,end));
	},
	setCookie:function(name,value,expires,path,domain,secure){
		expires = expires * 60*60*24*1000;
		var today = new Date();
		var expires_date = new Date( today.getTime() + (expires) );
		var cookieString = name + "=" +escape(value) +
		( (expires) ? ";expires=" + expires_date.toGMTString():"") +
		( (path) ? ";path=" + path:"") +
		( (domain) ? ";domain=" + domain:"") +
		( (secure) ? ";secure":"");
		document.cookie = cookieString;
	},
	deleteCookie:function( name, path, domain ){
		if ( this.getCookie( name ) ){
			document.cookie = name + "=" + ( ( path ) ? ";path=" + path:"") + ( ( domain ) ? ";domain=" + domain:"" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	},
	preloadImages:function(arg){
		if (document.images){
			for (var i = 0; i < arg.length; i++){
				var imageObj 	= new Image();
				imageObj.src 	= arg[i];
				// This will fix the images not showing when loaded dynamically
				if(Seraphim.clientInfoObj.isMSIE()){
					imageObj.style.display = 'none';
					document.getElementsByTagName('HEAD')[0].appendChild(imageObj);
				}
			}
		}
	}
});

//* CONFIG *//
Seraphim.config = Class.create({
	initialize: function(){
		this.imagePath = '/images/';		// Path to images
		this.cssPath = '/include/';			// Path to css
		this.jsPath = '/js/';						// Path to images
		this.defaultCssPath = this.cssPath;
		this.defaultImagePath = this.imagePath;
		this.defaultJsPath = this.jsPath;
	},
	setCssPath:function(cssPath){
		this.cssPath = cssPath;
	},
	getCssPath:function(){
		return this.cssPath;
	},
	resetCssPath:function(){
		this.cssPath = this.defaultCssPath;
	},
	setImagePath:function(imagePath){
		this.imagePath = imagePath;
	},
	getImagePath:function(){
		return this.imagePath;
	},
	resetImagePath:function(){
		this.imagePath = this.defaultImagePath;
	},
	setJsPath:function(jsPath){
		this.jsPath = jsPath;
	},
	getJsPath:function(){
		return this.jsPath;
	},
	resetJsPath:function(){
		this.jsPath = this.defaultJsPath;
	}
});

//* CLIENTINFO *//
Seraphim.clientInfo = Class.create({
	initialize: function(){
	},
	getBrowser:function(){
		return navigator.userAgent;
	},
	isOpera:function(){
		return (this.getBrowser().toLowerCase().indexOf('opera')>=0)?true:false;
	},
	isFirefox:function(){
		return (this.getBrowser().toLowerCase().indexOf('firefox')>=0)?true:false;
	},
	isMSIE:function(){
		return (this.getBrowser().toLowerCase().indexOf('msie')>=0)?true:false;
	},
	isOldMSIE:function(){
		return (this.getBrowser().toLowerCase().match(/msie [0-6]/gi))?true:false;
	},
	isSafari:function(){
		return (this.getBrowser().toLowerCase().indexOf('safari')>=0)?true:false;
	},
	getNavigatorVersion:function(){
		return navigator.appVersion.replace(/.*?MSIE (\d\.\d).*/g,'$1')/1;
	},
	getBrowserWidth:function(){
		if(self.innerWidth)return self.innerWidth;
		return document.documentElement.offsetWidth;
	},
	getBrowserHeight:function(){
		if(self.innerHeight)return self.innerHeight;
		return document.documentElement.offsetHeight;
	},
	getClientWidth: function(){
		return Math.max(document.body.clientWidth,document.documentElement.clientWidth);
	},
	getClientHeight: function(){
		return Math.max(document.body.clientHeight,document.documentElement.clientHeight);
	},
	getClientScrollTop: function(){
		return window.pageYOffset||Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		//return Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	},
	getScrollWidth: function(){
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
   return w ? w : 0;
	},
	getScrollHeight: function(){
		return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
	},
	getTopPos: function(el){
		yPos = el.offsetTop;
		tempEl = el.offsetParent;
		while (tempEl !== null) {
			yPos += tempEl.offsetTop;
			tempEl = tempEl.offsetParent;
		}
		return yPos;
	},
	getLeftPos: function(el){
		xPos = el.offsetLeft;
		tempEl = el.offsetParent;
		while (tempEl !== null) {
			xPos += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		}
		return xPos;
	},
	hasScrollbarHeight: function(el){
		return (el.clientHeight < el.scrollHeight) ? true : false;
	},
	hasScrollbarWidth: function(el){
		return (el.clientWidth < el.scrollWidth) ? true : false;
	}
});

Seraphim.ajax = Class.create({
	initialize: function(){
		this.reqOptions = {};
		this.reqUrl = '';
		this.reqContainer = '';
	},
	setReqOptions: function(reqOptions){
		this.reqOptions = reqOptions;
	},
	getReqOptions: function(){
		return this.reqOptions;
	},
	setReqUrl: function(reqUrl){
		this.reqUrl = reqUrl;
	},
	getReqUrl: function(){
		return this.reqUrl;
	},
	setReqContainer: function(reqContainer){
		this.reqContainer = reqContainer;
	},
	getReqContainer: function(){
		return this.reqContainer;
	},
	request: function(){
		try {
			new Ajax.Request(this.getReqUrl(),this.getReqOptions());
		}catch(e){
			alert('You need to include the scriptaculous/lib/prototype.js file');
		}
	},
	periodicalUpdater: function(){
		try {
			new Ajax.PeriodicalUpdater(this.getReqContainer(),this.getReqUrl(),this.getReqOptions())
		}catch(e){
			alert('You need to include the scriptaculous/lib/prototype.js file');
		}
	},
	updater: function(){
		try {
			new Ajax.Updater(this.getReqContainer(),this.getReqUrl(),this.getReqOptions())
		}catch(e){
			alert('You need to include the scriptaculous/lib/prototype.js file');
		}
	}
});
