/*
*
* This file was auto-generated by Validar Lead Import.
*
* Copyright (c) 2009, Validar Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Validar Incorporated nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY VALIDAR INCORPORATED 'AS-IS' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL VALIDAR INCORPORATED BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

var ValidarcomMarketerOffer = {
	// This constant represents the unspecified value for all enumerations
	ENUM_UNSPEC_VALUE : -1,
	
	// The maximum number of characters
	TEXT_MAX_LEN : 4000,
	
	// Used with property EventVolume
	EventVolumeType : {
		Ten : 0,
		TwentyFive : 1,
		Fifty : 2,
		OneHundred : 3,
		MoreThanOneHundred : 4
	},
	
	// Public properties
	FirstName : null,
	LastName : null,
	Company : null,
	Email : null,
	Phone : null,
	WhatEvents : {
		Email : false,
		Mobile : false,
		Web : false,
		Print : false,
		TradeShow : false,
		CorporateEvents : false
	},
	EventVolume : -1,
	WhatMarketingAutomation : {
		EmailMarketing : false,
		LeadScoring : false,
		LandingPage : false,
		SMSMarketing : false
	},
	
	// SourceId is a tracking property that is used to indicate the source of the lead
	SourceId : null,
	
	// ResultKey is a tracking property that is used to indicate the unique identifier of the result
	ResultKey : null,
	
	// MergeKey is a tracking property that is used to merge the result with contact or demographic data
	MergeKey : null,
	
	// ResultDateTime ResultDateTime is the collection date and time of the result
	ResultDateTime : null,
	
	// AlwaysAdd is a property that, when true, will create a new result even when the value for ResultKey matches an existing result, otherwise the matching result will be updated
	AlwaysAdd : true,
	
	// Public control properties
	_Timeout : 300000, // Timeout in milliseconds, default 5 min
	_SubmitResult : false, // Set to the result of the lead submission, true or false
	_SubmitMessage : null, // Set to the message returned from the lead submission
	
	_Reset : function() {
		// Public properties
		this.FirstName = null;
		this.LastName = null;
		this.Company = null;
		this.Email = null;
		this.Phone = null;
		this.WhatEvents_Email = null;
		this.WhatEvents_Mobile = null;
		this.WhatEvents_Web = null;
		this.WhatEvents_Print = null;
		this.WhatEvents_TradeShow = null;
		this.WhatEvents_CorporateEvents = null;
		this.EventVolume = this.ENUM_UNSPEC_VALUE;
		this.WhatMarketingAutomation_EmailMarketing = null;
		this.WhatMarketingAutomation_LeadScoring = null;
		this.WhatMarketingAutomation_LandingPage = null;
		this.WhatMarketingAutomation_SMSMarketing = null;
		this.SourceId = null;
		this.ResultKey = null;
		this.MergeKey = null;
		this.ResultDateTime = null;
		this.AlwaysAdd = true;
		this._SubmitResult = false;
		this._SubmitMessage = null;
		
		// Internal properties
		this.__onCompCb = null;
		this.__onTimeCb = null;
		this.__firstReq = true;
		this.__curProp = 0;
		this.__curOffset = 0;
		this.__subKey = null;
		this.__timer = null;
		this.__scriptElt = null;
	},
	
	// Call this method to submit the lead
	// c - function to call when the submission process completes
	// t - function to call if the submission process times out
	_SubmitLead : function(c, t) {
		this.__onCompCb = c;
		this.__onTimeCb = t;
		if (this.__validate()) {
			this.__doSubmit();
		} else {
			this._SubmitResult = false;
			if (this.__onCompCb != null) this.__onCompCb();
		}
	},
	
	// !!! DO NOT USE OR MODIFY !!!
	// Internal properties and functions
	// !!! DO NOT USE OR MODIFY !!!
	__onCompCb : null,
	__onTimeCb : null,
	
	__firstReq : true,
	__curProp : 0,
	__maxProp : 21,
	__curOffset : 0,
	__subKey : null,
	
	__timer : null,
	__scriptElt : null,
	
	__getIdPairs : function() {
		if (this.__firstReq) {
			this.__firstReq = false;
			return "_JO=ValidarcomMarketerOffer&_OG=af71b830-d00e-4579-a2b0-77a450a1d8f9&_LSG=46adf3ad-2085-4ce5-a14b-f11d3095e271";
		}
		else return "_JO=ValidarcomMarketerOffer&_SK=" + this.__subKey;
	},
	
	__getCurPropKey : function() {
		switch(this.__curProp) {
			case 0: return "_DI";
			case 1: return "_RK";
			case 2: return "_MK";
			case 3: return "_RD";
			case 4: return "_AA";
			case 5: return "FirstName";
			case 6: return "LastName";
			case 7: return "Company";
			case 8: return "Email";
			case 9: return "Phone";
			case 10: return "WhatEvents_Email";
			case 11: return "WhatEvents_Mobile";
			case 12: return "WhatEvents_Web";
			case 13: return "WhatEvents_Print";
			case 14: return "WhatEvents_TradeShow";
			case 15: return "WhatEvents_CorporateEvents";
			case 16:
				switch (this.EventVolume) {
					case this.EventVolumeType.Ten: return "EventVolume_Ten";
					case this.EventVolumeType.TwentyFive: return "EventVolume_TwentyFive";
					case this.EventVolumeType.Fifty: return "EventVolume_Fifty";
					case this.EventVolumeType.OneHundred: return "EventVolume_OneHundred";
					case this.EventVolumeType.MoreThanOneHundred: return "EventVolume_MoreThanOneHundred";
				}
				return null;
			case 17: return "WhatMarketingAutomation_EmailMarketing";
			case 18: return "WhatMarketingAutomation_LeadScoring";
			case 19: return "WhatMarketingAutomation_LandingPage";
			case 20: return "WhatMarketingAutomation_SMSMarketing";
		}
	},
	
	__getCurPropValue : function() {
		switch(this.__curProp) {
			case 0: return this.SourceId;
			case 1: return this.ResultKey;
			case 2: return this.MergeKey;
			case 3: return this.ResultDateTime;
			case 4: return (this.AlwaysAdd ? "1" : "0");
			case 5: return this.FirstName;
			case 6: return this.LastName;
			case 7: return this.Company;
			case 8: return this.Email;
			case 9: return this.Phone;
			case 10: return (this.WhatEvents.Email ? "1" : null);
			case 11: return (this.WhatEvents.Mobile ? "1" : null);
			case 12: return (this.WhatEvents.Web ? "1" : null);
			case 13: return (this.WhatEvents.Print ? "1" : null);
			case 14: return (this.WhatEvents.TradeShow ? "1" : null);
			case 15: return (this.WhatEvents.CorporateEvents ? "1" : null);
			case 16: return (this.EventVolume == this.ENUM_UNSPEC_VALUE ? null : "1");
			case 17: return (this.WhatMarketingAutomation.EmailMarketing ? "1" : null);
			case 18: return (this.WhatMarketingAutomation.LeadScoring ? "1" : null);
			case 19: return (this.WhatMarketingAutomation.LandingPage ? "1" : null);
			case 20: return (this.WhatMarketingAutomation.SMSMarketing ? "1" : null);
		}
	},
	
	__getCurPropType : function() {
		switch(this.__curProp) {
			case 16:
				return 0;
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
			case 17:
			case 18:
			case 19:
			case 20:
				return 1;
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
				return 2;
		}
	},
	
	__setTextLenMessage : function (member) {
		this._SubmitMessage = "The member " + member + " exceeds the maximum length of " + this.TEXT_MAX_LEN + " characters.";
	},
	
	__validate : function() {
		// Check length of member FirstName
		if ((this.FirstName != null) && (this.FirstName.length > this.TEXT_MAX_LEN))
		{
			this.__setTextLenMessage("FirstName");
			return false;
		}
		
		// Check length of member LastName
		if ((this.LastName != null) && (this.LastName.length > this.TEXT_MAX_LEN))
		{
			this.__setTextLenMessage("LastName");
			return false;
		}
		
		// Check length of member Company
		if ((this.Company != null) && (this.Company.length > this.TEXT_MAX_LEN))
		{
			this.__setTextLenMessage("Company");
			return false;
		}
		
		// Check length of member Email
		if ((this.Email != null) && (this.Email.length > this.TEXT_MAX_LEN))
		{
			this.__setTextLenMessage("Email");
			return false;
		}
		
		// Check length of member Phone
		if ((this.Phone != null) && (this.Phone.length > this.TEXT_MAX_LEN))
		{
			this.__setTextLenMessage("Phone");
			return false;
		}
		
		return true;
	},
	
	__cleanScriptElt : function() {
		if (this.__scriptElt != null) {
			document.getElementsByTagName("head")[0].removeChild(this.__scriptElt);
			this.__scriptElt = null;
		}
	},
	
	__cleanTimer : function() {
		if (this.__timer != null) clearTimeout(this.__timer);
		this.__timer = null;
	},
	
	__submitTimeout : function() {
		this.__cleanScriptElt();
		this.__cleanTimer();
		if (this.__onTimeCb != null) this.__onTimeCb();
	},
	
	__submitComplete : function(res, msg) {
		this.__cleanScriptElt();
		this.__cleanTimer();
		this._SubmitResult = res;
		this._SubmitMessage = msg;
		if (this.__onCompCb != null) this.__onCompCb();
	},
	
	__encodeValue : function(s) {
		if (s) {
			s = s.replace(/\%/g, '%25');
			s = s.replace(/\&/g, '%26');
			s = s.replace(/\</g, '%3C');
			s = s.replace(/\>/g, '%3E');
		}
		return s;
	},
	
	__doSubmit : function() {
		// Start query string with identifiers
		var q = this.__getIdPairs();
		
		var anyData = false;
		// Loop appending property pairs until we reach max query string length of 2048
		while (this.__curProp < this.__maxProp) {
			var val = this.__encodeValue(this.__getCurPropValue());
			if (val != null) {
				anyData = true;
				var type = this.__getCurPropType();
				var key = this.__getCurPropKey();
				if (key == null) throw "Failed to get the key for property " + this.__curProp + ".";
				var procVal;
				if ((type == 2) && (this.__curOffset > 0)) procVal = val.substr(this.__curOffset);
				else procVal = val;
				var enc = escape(procVal);
				var len = q.length + key.length + enc.length + 2; // Include & and =
				if (len > 2048) {
					if (type != 2) break; // If po or pm, done
					else {
						var rem = (2048 - q.length - key.length - 2); // Get available room
						if (rem < 133) break; // Skip append if not much room
						var bloat = enc.length/procVal.length;
						var chkLen = Math.floor(rem/bloat); // Best guess at available pre-encoded count
						var newVal = val.substr(this.__curOffset, chkLen); // Get chunk
						var newEnc = escape(newVal);
						while (newEnc.length > rem) {
							chkLen = Math.floor(chkLen * (2/3)); // Exponential reduction in chunk size, assume nominal bloat
							newVal = val.substr(this.__curOffset, chkLen);
							newEnc = escape(newVal);
						}
						q += "&" + key + "=" + newEnc;
						this.__curOffset += chkLen;
						if (this.__curOffset >= val.length) {
							this.__curOffset = 0;
							this.__curProp++;
						}
						break;
					}
				} else {
					q += "&" + key + "=" + enc;
					this.__curProp++;
				}
			} else {
				this.__curProp++;
			}
		}
		
		if (anyData) {
			if (this.__curProp == this.__maxProp) {
				if (q.length <= 2043) q += "&_C=1"; // We're done, tell server
			}
			
			this.__scriptElt = document.createElement("script");
			this.__scriptElt.type = "text/javascript";
			this.__scriptElt.src = "https://anysource.validar.com/WebServices/V2/Core/JSONSubmitResult.aspx?" + q;
			
			if (this.__onTimeCb != null) {
				this.__timer = window.setTimeout("ValidarcomMarketerOffer.__submitTimeout()", this._Timeout);
			}
			
			document.getElementsByTagName("head")[0].appendChild(this.__scriptElt);
		} else {
			this._SubmitResult = true;
			this._SubmitMessage = "There was no data to submit";
			if (this.__onCompCb != null) this.__onCompCb();
		}
	},
	
	__submitCallback : function(sk, cnt) {
		this.__subKey = sk;
		this.__cleanScriptElt();
		this.__cleanTimer();
		this.__doSubmit();
	}
}
