if (/^www.tap4help.com/.exec(document.domain) && typeof(Recaptcha) == "undefined") {
	document.write(unescape("%3Cscript src='http://api.recaptcha.net/js/recaptcha_ajax.js' type='text/javascript'%3E%3C/script%3E"));
}
var Tap4Help = {
  application: null,
  apikey: "756b563010dec7e5ed0f94c8defd5795",
  displayfaq: function(target,application) {
	var element = document.getElementById(target);
	if (!element) {
		return;
	}

  	if (arguments.length > 1 && Tap4Help.application == null) {
		Tap4Help.application = application;
	}

	var script = document.createElement('script');
	script.src = document.location.protocol+'//www.tap4help.com/service/'+Tap4Help.apikey+'/applications/'+Tap4Help.application+'?_callback=Tap4Help.embedFaqResponse("'+target+'",';
	document.getElementsByTagName('head')[0].appendChild(script);
  },
 create: function(target,application) {
	var element = document.getElementById(target);
	if (!element) {
		return;
	}

	Tap4Help.application = application;

	element.innerHTML = "<form name='t4hquesform'><div class='tap4help' id='questionform'><h3><a name='question'>Have a Question?</a></h3><label for='firstname'>First Name:</label><input type='text' id='firstname' name='firstname' /><br /><label for='lastname'>Last Name:</label><input type='text' id='lastname' name='lastname' /><br /><label for='email'>Email Address:</label><input type='text' id='email' name='email' /><br /><label for='question'>Question:</label><textarea id='question' name='question' rows='15'></textarea><br /><br /><label id='recaptchalabel'>Please verify these words:</label><div id='recaptchablock'></div><br /><label for='askbutton'></label><input type='button' name='askbutton' id='askbutton' value='Ask Question' onclick='Tap4Help.ask();'/></div></form>";
	
	if (typeof(Recaptcha) != "undefined" && Recaptcha.create) {
		Recaptcha.create("6LcsdgQAAAAAALr5KWYVEmU3xWL8omlg_9YgfNSu","recaptchablock", {});
	} else {
		document.getElementById('recaptchalabel').style.display = 'none';
	}
  },
  ask: function () {
	if (/^www.tap4help.com/.exec(document.domain) && typeof(application) != "undefined") {
		Tap4Help.application = application['_id'];
	}
	if (!Tap4Help.application) {
		alert('Unknown error');
		return;
	}
	firstname = document.forms.t4hquesform.firstname.value;
	lastname = document.forms.t4hquesform.lastname.value;
	email = document.forms.t4hquesform.email.value;
	question = document.forms.t4hquesform.question.value;
	if (document.forms.t4hquesform.firstname.value == "") {
		alert("First Name is required");
		document.forms.t4hquesform.firstname.activate();
		return;
	}
	if (document.forms.t4hquesform.lastname.value == "") {
		alert("Last Name is required");
		document.forms.t4hquesform.lastname.activate();
		return;
	}
	if (document.forms.t4hquesform.email.value == "") {
		alert("Email address is required");
		document.forms.t4hquesform.email.activate();
		return;
	}
	if (document.forms.t4hquesform.question.value == "") {
		alert("Question is required");
		document.forms.t4hquesform.question.activate();
		return;
	}
	document.forms.t4hquesform.askbutton.disabled = true;
/*
	if ($F('recaptcha_response_field').strip() == "" || $F('recaptcha_response_field').strip.split(' ').length != 2) {
		alert("Please verify the two words requested");
		$('recaptcha_response_field').activate();
		return;
	}
	*/
	var params = "firstname=" + escape(firstname);
	params += "&lastname=" + escape(lastname);
	params += "&email=" + escape(email);
	params += "&question=" + escape(question);

	if (typeof(Recaptcha) != "undefined" && Recaptcha.create) {
		challenge = document.forms.t4hquesform.recaptcha_challenge_field.value;
		response = document.forms.t4hquesform.recaptcha_response_field.value;
		params += "&recaptcha_response_field=" + escape(response);
		params += "&recaptcha_challenge_field=" + escape(challenge);
	}

	if (/^www.tap4help.com/.exec(document.domain)) {
		var url = document.location.protocol+'//www.tap4help.com/service/'+Tap4Help.apikey+'/applications/'+Tap4Help.application+'/supportrequest';
		var request = $.ajax(
			{
				url: url,
				type:'post',
				data: params,
				success: function (transport) { Tap4Help.supportRequestResponse(transport) },
				error: function (transport) { Tap4Help.failedSupportRequest(transport) },
				dataType: 'json'
			}
		);
	} else {
		var script = document.createElement('script');
		script.src = document.location.protocol+'//www.tap4help.com/service/'+Tap4Help.apikey+'/applications/'+Tap4Help.application+'/esupportrequest?_method=POST&encodedparams='+Tap4Help.encode(params);
		document.getElementsByTagName('head')[0].appendChild(script);

	}
   },
   _utf8_encode : function (string) {
   string = string.replace(/\r\n/g,"\n");
   var utftext = "";
   
   for (var n = 0; n < string.length; n++) {
   
   var c = string.charCodeAt(n);
   
   if (c < 128) {
   utftext += String.fromCharCode(c);
   }
   else if((c > 127) && (c < 2048)) {
   utftext += String.fromCharCode((c >> 6) | 192);
   utftext += String.fromCharCode((c & 63) | 128);
   }
   else {
   utftext += String.fromCharCode((c >> 12) | 224);
   utftext += String.fromCharCode(((c >> 6) & 63) | 128);
   utftext += String.fromCharCode((c & 63) | 128);
   }
   
   }
   
   return utftext;
   },

   _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
   encode : function (input) {
   var output = "";
   var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
   var i = 0;
   
   input = Tap4Help._utf8_encode(input);
   
   while (i < input.length) {
   
   chr1 = input.charCodeAt(i++);
   chr2 = input.charCodeAt(i++);
   chr3 = input.charCodeAt(i++);
   
   enc1 = chr1 >> 2;
   enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
   enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
   enc4 = chr3 & 63;
   
   if (isNaN(chr2)) {
   enc3 = enc4 = 64;
   } else if (isNaN(chr3)) {
   enc4 = 64;
   }
   
   output = output +
   this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
   this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
   
   }
   
   return output;
   },
   embedResponse: function(data) {
	alert("Support request successfully created. You should receive a confirmation email shortly.");
	document.forms.t4hquesform.askbutton.disabled = false;
	document.forms.t4hquesform.reset();

	if (typeof(Recaptcha) != "undefined") {
		Recaptcha.reload();
	}
   },
   supportRequestResponse: function(transport) {
	document.forms.t4hquesform.askbutton.disabled = false;
	document.forms.t4hquesform.reset();
	if (typeof(Recaptcha) != "undefined") {
		Recaptcha.reload();
	}
	if (typeof(this.onComplete) != "undefined") {
		this.onComplete();
	} else {
		alert("Support request successfully created. You should receive a confirmation email shortly.");
	}
   },
   failedSupportRequest: function(transport) {
	alert('Error: ' + transport.responseText);
	document.forms.t4hquesform.askbutton.disabled = false;
	Recaptcha.reload();
   },
   faqResponse: function(transport,element) {
   	var app = transport.responseText.evalJSON(true);
	Tap4Help.embedFaqResponse(app);
   },
   embedFaqResponse: function(target,app) {
   	var element = document.getElementById(target);
	var html = "";
	if (!app || !app.faqs || !app.faqs.length || app.faqs.length < 1) {
		html = "No frequently asked questions.";
		element.innerHTML = html;
		return;
	}

	for (idx = 0; idx < app.faqs.length; idx++) {
		html += '<div class="t4hfaqitem">';
		html += '<div class="t4hfaqquestion">';
		html += '<b>Q: ' + app.faqs[idx].question + '</b>';
		html += '</div>';
		html += '<div class="t4hfaqanswer">';
		html += '<b>A:</b>' + app.faqs[idx].answer;
		html += '</div>';
		html += '</div>';
	}
	element.innerHTML = html;
   },
   failedRequest: function(transport) {
   alert(transport.responseText);
	element.innerHTML = "Error loading FAQ";
   }
}

