﻿
var CTIsPlayback = true;
	try {
		if (!parent || !parent.WebPlayer) // if there is no WebPlayer or if the above frame is not accessible
			throw false;
} catch (e) { CTIsPlayback = false; }

CT.AjaxForm = function () {

	var formID = null;
	var formInputs = [];
	var validateInputs = [];
	var formTypeName = null;
	var callbackDomElement = null;
	var loaderElement = null;
	var formContainer = null;
	var mailSubject = null;
	var confirmationMsg = null;
	var showConfirm = null;

	YUE.on(window, "load", function () {
		CT.AjaxForm.preInit();
	});

	return {

		formSubmitted: new YAHOO.util.CustomEvent("formSubmitted", this),

		preInit: function () {
			/*YUE.on("submitButton", "click", function(e) {
			if (typeof (e.preventDefault) == "function") e.preventDefault();
			CT.AjaxForm.submit();
			return false;
			});*/
		},

		init: function (oConfig) {
			formID = oConfig.formClientID;
			formTypeName = oConfig.formType;
			callbackDomElement = YUD.get(oConfig.callbackDomEl);
			loaderElement = YUD.get(oConfig.loaderEl);
			formContainer = YUD.get(oConfig.formContainerEl);
			mailSubject = oConfig.MailSubject;
			confirmationMsg  = oConfig.confirmationMsg;
			showConfirm = oConfig.showConfirm;

			YUE.on(formID, "submit", function (e) {
				if (typeof (e.preventDefault) == "function") e.preventDefault();
				CT.AjaxForm.submit();

				if (typeof ClickTaleExec == "function" && !CTIsPlayback) {
					ClickTaleExec("CT.AjaxForm.submit();");
				}

				return false;
			});

			for (var index = 0; index < oConfig.fieldsNamesArray.length; index++) {
				formInputs[index] = YUD.get(oConfig.fieldsNamesArray[index]);
				validateInputs.push(oConfig.fieldsNamesArray[index]);
			}
		},

		submit: function () {
			//calling the asp.net validation method, all validation will occur using asp.net
			if (!CTIsPlayback && typeof Page_ClientValidate != "undefined" && Page_ClientValidate() === false) // not playback and failed validation
				return false;
			
			//buliding the Request Url with params.
			var Request = BASE + "MailingHandler.ashx?mailType=" + formTypeName + "&xml=true";
			var PostParams = "";
			for (var index = 0; index < formInputs.length; index++) {
				PostParams += ((index == 0) ? "" : "&") + formInputs[index].name.substring(formInputs[index].name.lastIndexOf("$") + 1) + "=" + escape(formInputs[index].value);
			}
			if (typeof mailSubject != "undefined" && mailSubject != null) {
				PostParams += "&MailSubject=" + mailSubject;
			}
			if (typeof confirmationMsg != "undefined" && confirmationMsg != null) {
				PostParams += "&ConfirmationMsg=" + confirmationMsg;
			}

			if(typeof ClickTaleGetUID == "function")
			{
				PostParams += "&WRUID=" + ClickTaleGetUID() + "&SID=" + ClickTaleGetSID() + "&PID=" + ClickTaleGetPID();
			}

			loaderElement.style.display = "block";
			YAHOO.util.Connect.asyncRequest('POST', Request, this.callback, PostParams);

		},

		onSubmited: function (func) {
			this.formSubmitted.subscribe(func, this);
		},

		callback: {
			success: function (o) {
				if (o.status == 200) {
					loaderElement.style.display = "none";
					formContainer.style.display = "none";
					callbackDomElement.style.display = "block";
					var res = this.grabPosXML(o, "confirmation");
					var div = document.createElement("div");
					div.innerHTML = res;
					if(showConfirm){
						callbackDomElement.appendChild(div);
					}

					CT.AjaxForm.formSubmitted.fire();

					if (this.grabPosXML(o, "status") == "True")
						ClickTaleHelper.tag("form_submit_success");
					else
						ClickTaleHelper.tag("form_submit_fail");
				}
			},
			failure: function (o) {
				loaderElement.style.display = "none";
				callbackDomElement.style.display = "block";
				var strong = document.createElement("strong");
				strong.appendChild(document.createTextNode("Sorry, there seems to be a problem sending the form."));
				//remove all child elements
				while (callbackDomElement.firstChild) {
					callbackDomElement.removeChild(callbackDomElement.firstChild);
				}
				callbackDomElement.appendChild(strong);
				ClickTaleHelper.tag("form_submit_fail");
			},
			grabPosXML: function (response, tagName) {
				return response.responseXML.documentElement.getElementsByTagName(tagName)[0].childNodes[0].nodeValue;
			}
		}


	};
} ();
