/*
	Collection of utility functions
*/
SG.namespace("SG.cal.util");

SG.cal.util = {
	isEmpty : function (string) {
		if ((string == null) || (string.length == 0)) {
			return true;
		}
		else { 
			return false; 
		}
	},
	
	isBoolean: function(o) {
		return typeof o === 'boolean';
	},
	
	isFunction: function(o) {
		return typeof o === 'function';
	},
	    
	isNull: function(o) {
		return o === null;
	},

	isNumber: function(o) {
		return typeof o === 'number' && isFinite(o);
	},

	isObject: function(o) {
		return (o && (typeof o === 'object' || this.isFunction(o))) || false;
	},
	    
	isString: function(o) {
		return typeof o === 'string';
	},
	    
	isUndefined: function(o) {
		return typeof o === 'undefined';
	},
	
	hasOwnProperty: function(o, prop) {
		if (Object.prototype.hasOwnProperty) {
			return o.hasOwnProperty(prop);
		}
		
		return !this.isUndefined(o[prop]) && 
				So.constructor.prototype[prop] !== o[prop];
	},

	isArray: function(o) { 
		if (o) {
			return this.isNumber(o.length) && !this.hasOwnProperty(o.length);
		}
		return false;
	},
	
	propertyRetrieval: function(id, name, callback, url) {
		if (!this.isEmpty(id) && !this.isEmpty(name) && this.isFunction(callback)) {
			var retrievalCallback = {
				success: function(oResponse) {
					callback(oResponse.responseText);
				},
				failure: function(oResponse) {
					callback('');
				}
			}
			var param = 'id=' + encodeURI(id) + '&name=' + encodeURI(name);
			var transaction = YAHOO.util.Connect.asyncRequest('GET', url+"&" + param, retrievalCallback);
		}
	},
	
	insertAfter: function(newNode, existingNode) {
		if(existingNode.nextSibling) {
			existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling);
		} else {
			existingNode.parentNode.appendChild(newNode);
		}
	},
	
	getControl: function(id) {
		var control = null;

		try {
			control = eval("SG.cal.search." + id);
		} catch (e) { }
		if (control) return control;

		try {
			control = eval("SG.cal.tree." + id + ".tree");
		} catch (e) { }
		if (control) return control;

		try {
			control = eval("SG.cal.grid." + id);
		} catch (e) { }
		if (control) return control;

		try {
			control = eval("SG.cal.filter." + id);
		} catch (e) { }
		if (control) return control;

		try {
			control = eval("SG.cal.balloon." + id);
		} catch (e) { }
		if (control) return control;
		
		return control;
	}
}

SG.namespace("SG.log",
			 "SG.cal.logging.init");
			 
SG.log = {
	logreader: null,
	
	info: function(message) {
		YAHOO.log(message, "info");
	},

	warn: function(message) {
		YAHOO.log(message, "warn");
	},

	error: function(message) {
		YAHOO.log(message, "error");
	},

	time: function(message) {
		YAHOO.log(message, "time");
	}
}

SG.cal.logging.init = function(config) {
	if (SG.cal.util.isNull(SG.log.logreader)) {
		SG.log.logreader = new YAHOO.widget.LogReader(null, config);
		if (config.collapsed) {
			SG.log.logreader.collapse();
		}
		YAHOO.widget.Logger.reset();
	}
}

SG.namespace("SG.cal.util.Connect.syncRequest");

SG.cal.util.Connect.syncRequest = function(method, uri, postData) {
	var YC = YAHOO.util.Connect;
	var o = (YC._isFileUpload)?YC.getConnectionObject(true):YC.getConnectionObject();

	if(!o){
		return null;
	}
	else{
		if(YC._isFormSubmit){
			if(YC._isFileUpload){
				YC.uploadFile(o, null, uri, postData);
				return o;
			}
			if(method.toUpperCase() == 'GET'){
				if(YC._sFormData.length !== 0){
					uri += ((uri.indexOf('?') == -1)?'?':'&') + YC._sFormData;
				}
			}
			else if(method.toUpperCase() == 'POST'){
				postData = postData?YC._sFormData + "&" + postData:YC._sFormData;
			}
		}

		o.conn.open(method, uri, false);

		if(YC._use_default_xhr_header){
			if(!YC._default_headers['X-Requested-With']){
				YC.initHeader('X-Requested-With', YC._default_xhr_header, true);
			}
		}

		if((method.toUpperCase() == 'POST' && YC._use_default_post_header) && YC._isFormSubmit === false){
			YC.initHeader('Content-Type', YC._default_post_header);
		}

		if(YC._has_default_headers || YC._has_http_headers){
			YC.setHeader(o);
		}

		o.conn.send(postData || '');

		if(YC._isFormSubmit === true){
			YC.resetFormState();
		}

		return o;
	}
}	