function AJAX(serverURL, action, keyword, maxresults, orderby, callBack) {
	this.serverURL = serverURL;
	this.callBack = callBack;
	this.action = action;
	this.keyword = keyword;
	this.maxresults = maxresults;
	this.orderby = orderby;
	this.htmlHead = document.getElementsByTagName('HEAD').item(0);
}

AJAX.prototype = {

	counter: 0,

	arguments: [],

	stringify: function (v) {
		var a = [];

		function e(s) {
			a[a.length] = s;
		}

		function g(x) {
			var c, i, l, v;

			switch (typeof x) {
			case 'object':
				if (x) {
					if (x instanceof Array) {
						e('[');
						l = a.length;
						for (i = 0; i < x.length; i += 1) {
							v = x[i];
							if (typeof v != 'undefined' &&
									typeof v != 'function') {
								if (l < a.length) {
									e(',');
								}
								g(v);
							}
						}
						e(']');
						return;
					} else if (typeof x.valueOf == 'function') {
						e('{');
						l = a.length;
						for (i in x) {
							v = x[i];
							if (typeof v != 'undefined' &&
									typeof v != 'function' &&
									(!v || typeof v != 'object' ||
										typeof v.valueOf == 'function')) {
								if (l < a.length) {
									e(',');
								}
								g(i);
								e(':');
								g(v);
							}
						}
						return e('}');
					}
				}
				e('null');
				return;
			case 'number':
				e(isFinite(x) ? +x : 'null');
				return;
			case 'string':
				l = x.length;
				e('"');
				for (i = 0; i < l; i += 1) {
					c = x.charAt(i);
					if (c >= ' ') {
						if (c == '\\' || c == '"') {
							e('\\');
						}
						e(c);
					} else {
						switch (c) {
						case '\b':
							e('\\b');
							break;
						case '\f':
							e('\\f');
							break;
						case '\n':
							e('\\n');
							break;
						case '\r':
							e('\\r');
							break;
						case '\t':
							e('\\t');
							break;
						default:
							c = c.charCodeAt();
							e('\\u00' + Math.floor(c / 16).toString(16) +
								(c % 16).toString(16));
						}
					}
				}
				e('"');
				return;
			case 'boolean':
				e(String(x));
				return;
			default:
				e('null');
				return;
			}
		}
		g(v);
		return a.join('');
	},

	doCall: function(method, arguments) {
		var url = this.serverURL + '?action=' + encodeURI(this.action) + '&keyword=' + encodeURI(this.keyword) + '&maxresults=' + encodeURI(this.maxresults) + '&orderby=' + encodeURI(this.orderby) + '&callback=' + encodeURI(this.callBack);

		var script = document.createElement('SCRIPT');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('charset', 'utf-8');
		script.setAttribute('src', url);
		script.setAttribute('id', '_ajax_' + this.counter);
		this.htmlHead.appendChild(script);
	}
}

function getData(data) {
	document.getElementById("wQAContainer").innerHTML = data.html;
}

function GetQuestionsList(keyword, maxresults, orderby, action) {
	if (keyword == undefined) keyword = 'health insurance';
	if (maxresults == undefined) maxresults = '2';
	if (orderby == undefined) orderby = 'newest'; // newest, toprated, closetime
	if (action == undefined) action = 'getquestionslist';
	ajax = new AJAX('http://www.healthinsurancefinders.com/api/AnswersWidgetAPI.aspx', action, keyword, maxresults, orderby, 'getData');
	ajax.doCall('', []);
}

var _wQAMessageCounter = "messageCounter"; 
var _wQAQuestion = "question"; 
var _wQATypeHere="Type here..."; 
var _wQAEmpty="";

function messageCounter(textarea, tagId) { 
	try { 
		if (typeof textarea == 'string') { 
			textarea = document.getElementById(textarea); 
		} 
		var text = textarea.value; 
		var textlength = text.length; 
		var max = '75'; //var max = textarea.getAttribute('maxlength');
		if (text.length > max) { 
			textarea.value = text.substring(0, max); 
			alert('Please keep the question text under ' + max + ' characters!'); 
			textlength = max; return false; 
		} 
		document.getElementById(tagId).innerHTML = textlength + '/'; 
		return true; 
	} catch (e) { } 
}
