// oTools / oTitle

var oTools = {

	toggle: function(oNode, sClassName)
	{
		oNode.className = oNode.className=='on' ? '' : 'on';
	},

	oSelect: {

		clear: function(oSelectNode)
		{
			while (oSelectNode.options.length > 1) oSelectNode.remove(1);
		},

		fill: function(oSelectNode, aList)
		{
			for (var sKey in aList) oSelectNode.add(new Option(aList[sKey], sKey), null);
		}
	},

	oValidate: {

		list: {},

		char: function(oNode, oEvent, sRegex)
		{
			var iCode	= oTools.getKey(oEvent);
			var sKey	= String.fromCharCode(iCode);
			var sPatern	= eval(sRegex);
			return  (oTools.in_array(iCode, [0,8,99,118]) ? true : sPatern.test(oNode.value+sKey));
		},

		string: function()
		{
		},

		validate: function(sFormRef)
		{
			this.list[sFormRef].obj.__validateAllFields()
		},

		set: function(sFormRef)
		{
			if (!document.forms[sFormRef]) return;
			if (this.list[sFormRef]) return;

			this.list[sFormRef] = {};
			this.list[sFormRef].enable = function() {
				var oElement = document.forms[sFormRef].sActionButton;
				oElement.style.opacity	= 1;
				oElement.disabled		= false;
			};
			this.list[sFormRef].disable = function() {
				var oElement = document.forms[sFormRef].sActionButton;
				oElement.style.opacity	= 0.33;
				oElement.disabled		= true;
			};
			this.list[sFormRef].obj = new DHTMLSuite.formValidator({
				keyValidation:			true,
				indicateWithBars:		true,
				formRef:				sFormRef,
				callbackOnFormValid:	('oTools.oValidate.list[\''+sFormRef+'\'].enable'),
				callbackOnFormInvalid:	('oTools.oValidate.list[\''+sFormRef+'\'].disable')
			});
		}
	},

	goPage: function(sPage)
	{
		window.location.href = sBaseUrl+(sPage ? sPage : '');
	},

	in_array: function (needle, haystack, argStrict)
	{
		var key = '', strict = !!argStrict; 
		if (strict) {
			for (key in haystack) if (haystack[key] === needle) return true;
		} else {
			for (key in haystack) if (haystack[key] == needle) return true;
		}
		return false;
	},

	getImageRatio: function(aArg)
	{
		if (aArg.iImgWidth > aArg.iMaxWidth || aArg.iImgHeight > aArg.iMaxHeight) {
			if (aArg.iImgWidth > aArg.iMaxWidth) {
				aArg.iImgHeight	*= aArg.iMaxWidth/aArg.iImgWidth;
				aArg.iImgWidth	= aArg.iMaxWidth;
			}
			if (aArg.iImgHeight > aArg.iMaxHeight) {
				aArg.iImgWidth	*= aArg.iMaxHeight/aArg.iImgHeight;
				aArg.iImgHeight	= aArg.iMaxHeight;
			}
		}

		return {iWidth: aArg.iImgWidth, iHeight: aArg.iImgHeight};
	},

	getAttr: function(oNode, sKey)
	{
		return oNode.attributes.getNamedItem(sKey) ? oNode.attributes.getNamedItem(sKey).value : null;
	},

	parseData: function(sDivId)
	{
		return oCall.jsonDecode(oCall.decrypt(document.getElementById(sDivId).innerHTML));
	},

	delayShow: function(sPageId, iTime)
	{
		setTimeout('oTools.show("'+sPageId+'")', iTime);
	},

	show: function(sPageId)
	{
		jQuery('#'+sPageId).css('visibility', 'visible');
	},

	createNamespace: function(sName, oValue)
	{
		var aName	= sName.split(".");
		var oParent	= window;
		var sPart	= aName[0];
		for (var i=0, iLen=aName.length-1; i<iLen; i++, sPart=aName[i]) {
			if (!oParent[sPart]) {
				oParent=oParent[sPart]={};
			} else {
				oParent=oParent[sPart];
			}
		}
		oParent[sPart] = oValue;
		return sPart;
	},

	checkErr: function(mHasErr)
	{
		if (mHasErr) {
			alert(mHasErr);
			return false;
		} else {
			return true;
		}
	},

	addEventLog: function(sColor, sMessage)
	{
		jQuery('#southContent').prepend('<div class="'+sColor+'">'+oPhp.date("F j, Y g:i a", oPhp.time())+' - '+sMessage+'</div>');
		oDhtmlSuite.pane.slide('south');
	},

	time: function()
	{
		return (new Date().getTime());
	},

	jsSubmit: function(oNode, oEvent)
	{
		var iKey = this.getKey(oEvent);

		return ((iKey == 13) ? oNode : false);
	},

	getKey: function(e)
	{
		if (window.event)
		   return window.event.keyCode;
		else if (e)
		   return e.which;
		else
		   return null;
	},

	getObject: function(oTest)
	{
		for (var sKey in oTest) {
			document.write(sKey + ' - ' + oTest[sKey] + '<br>');
		}
	},

	callOnce: function(oAjax, aArg)
	{
		var iTime = this.time();
		var aRegArg = {
			iTime: String(iTime),
			sMethod: aArg.sMethodName
		};
		var aRunArg = {
			iTime: String(iTime),
			sMethod: aArg.sMethodName,
			aArg: aArg.aArg
		};

		if (aArg.sRecordModuleId) {
			aRegArg['sRecordModuleId'] = aArg.sRecordModuleId;
			aRunArg['sRecordModuleId'] = aArg.sRecordModuleId;
		}

		return ((iTime != parseInt(oAjax.sync('regAction', aRegArg))) ? 'Crosscheck mismatch on ajax call' : oAjax.sync('runAction', aRunArg));
	}
};

var oTitle = {

	iRollSpeed: 250,
	bRoll: false,
	sStatus: '',
	sMain: '',
	sPage: '',
	sSignal: '',

	init: function(aArg)
	{
		jQuery.each(aArg, function(sKey, sVal) {
			oTitle[sKey] = sVal;
		});
		oTitle.normalize();
		oClient.aOnFocus.push('oTitle');
	},

	onFocus: function()
	{
		if (this.bRoll) this.bRoll = false;
	},

	normalize: function()
	{
		document.title = '' +
			(this.sStatus ? '('+this.sStatus+') ' : '') +
			(this.sPage ? this.sPage+' / ' : '') +
			this.sMain;
	},

	signal: function(sSignal)
	{
		this.bRoll		= true;
		this.sSignal	= '           (!) '+sSignal+' '+this.sMain;
		this.roll();
		oTools.addEventLog('red', '(!) '+sSignal);
	},

	roll: function()
	{
		if (this.bRoll) {
			this.sSignal = this.sSignal.substring(1, this.sSignal.length) + this.sSignal.substring(0, 1);
			document.title = this.sSignal;
			setTimeout('oTitle.roll()', this.iRollSpeed);
		} else {
			this.normalize();
		}
	},

	change: function(sPart, sContent)
	{
		this[sPart] = sContent;
		if (!this.bRoll) this.normalize();
	}
};
