﻿var displayeditem;

Ext.onReady(function() {
	Ext.QuickTips.init();

	var buttonregister = Ext.get("submit_button");
	var buttonregistered1 = Ext.get("signIn1");
	var buttonregistered2 = Ext.get("signIn2");
	var buttonregistered3 = Ext.get("signIn3");
	var buttonregistered4 = Ext.get("signIn4");
var btnReturnone= Ext.get("btnReturn1");

	var buttonregistereddownload = Ext.get("btnRegisteredDownload");
	var buttonReturn = Ext.get("btnReturn");

	buttonregistereddownload.on('click', checkreturnregistration);
btnReturnone.on('click', displayDefaultContent);
	buttonReturn.on('click', displayDefaultContent);

	buttonregistered1.on('click', displayRegisteredForm);
	buttonregistered2.on('click', displayRegisteredForm);
	buttonregistered3.on('click', displayRegisteredForm);
	buttonregistered4.on('click', displayRegisteredForm);

	buttonregister.on('click', register)

	initRegisteredField();
	var isAlreadyRegistered = checkregistration();

	if (!isAlreadyRegistered) {
		displayDefaultContent();
	};

	buildForm();

});

function initRegisteredField() {

	registrationemail = new Ext.form.TextField({
		hideLabel : true,
		emptyText : 'Email',
		labelSeparator : '',
		name : 'RegisteredEmail',
		allowBlank : false,
		applyTo : 'RegisteredEmail'

	})
}

function buildForm() {

	RefCombo = new Ext.form.ComboBox({
		typeAhead : true,
		triggerAction : 'all',
		transform : 'Referred',
		hiddenName : 'Referred',
		listWidth : 135,
		width : 135
	});

	var statestore = new Ext.data.SimpleStore({
		fields : ['abbr', 'state', 'nick'],
		data : Ext.data.states
			// from states.js
	});

	var industrystore = new Ext.data.SimpleStore({
		fields : ['name'],
		data : Ext.data.industries

	});

	var statescombo = new Ext.form.ComboBox({
		store : statestore,
		name : 'State',
		forceSelection : true,
		hiddenName : 'State',
		width : 68,
		listWidth : 135,
		hideLabel : true,
		displayField : 'state',
		typeAhead : true,
		mode : 'local',
		triggerAction : 'all',
		emptyText : 'State'
	});

	var industrycombo = new Ext.form.ComboBox({
		store : industrystore,
		name : 'Industry',
		forceSelection : true,
		hiddenName : 'Industry',
		labelSeparator : '',
		hideLabel : true,
		width : 126,
		displayField : 'name',
		typeAhead : true,
		mode : 'local',
		triggerAction : 'all',
		emptyText : 'Industry'
	});

	registrationform = new Ext.FormPanel({
		border : false,
		defaults : {
			bodyStyle : 'background-color: #e5e5e5'
		},
		width : 353,
		items : [{
			width : 353,
			defaults : {
				bodyStyle : 'background-color: #e5e5e5'
			},
			layout : "column",
			border : false,
			items : [{
				width : 180,
				layout : 'form',
				border : false,
				items : [{
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'Email*',
					name : 'Email',
					allowBlank : false
				}, {
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'First Name*',
					labelSeparator : '',
					name : 'FirstName',
					allowBlank : false

				}, {
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'Last Name*',
					labelSeparator : '',
					name : 'LastName',
					allowBlank : false

				}, {
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'Company*',
					labelSeparator : '',
					name : 'Company',
					allowBlank : false

				}, industrycombo

				]
			}, {
				defaults : {
					bodyStyle : 'background-color: #e5e5e5'
				},
				width : 170,
				layout : 'form',
				border : false,
				items : [{
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'Address',
					labelSeparator : '',
					name : 'Address1'
				}, {
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'Address 2',
					labelSeparator : '',
					name : 'Address2'

				}, {
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'City',
					labelSeparator : '',
					name : 'City'

				}, {
					defaults : {
						bodyStyle : 'background-color: #e5e5e5'
					},
					layout : "column",
					border : false,
					items : [{
						width : 70,
						border : false,
						items : statescombo
					}, {
						style : 'margin-top:1px',
						width : 55,
						hideLabel : true,
						xtype : 'textfield',
						emptyText : 'Zip',
						name : 'Zip'

					}]
				}

				, {
					style : 'margin-top:2px',
					hideLabel : true,
					xtype : 'textfield',
					emptyText : 'Phone',
					labelSeparator : '',
					name : 'Phone'

				}

				]
			}]
		}]
	});

	registrationform.render('FormHolder');
}

function displayRegisteredForm() {

	swapDisplay(Ext.get('BoxRegistered'))
}

function displayDefaultContent() {

	swapDisplay(Ext.get('BoxForm'));
}

function register() {

	if (!registrationform.form.isValid()) {
		return;
	}

	var answeredquestions = getAnsweredQuestions();

	registrationform.form.items.each(function(item) {
		if (item.el.getValue() == item.emptyText) {
			item.el.dom.value = '';
		}
	});

	var record = registrationform.form.getValues();

	record.CampaignID = 1;
	record.ReferralID = RefCombo.getValue();

	registrationform.form.items.each(function(item) {
		if (item.el.getValue() == '' && item.emptyText) {
			item.el.dom.value = item.emptyText;
		}
	});

	registrationform.form.el.mask("Registering...", 'x-mask-loading');

	Ext.Ajax.request({
		url : 'controllerhandler.ashx',
		method:'POST',
		success : function(response, options) {
			swapDisplay(Ext.get('BoxThankYou'));
			registrationform.form.el.unmask();
		},
		failure : function() {
			registrationform.form.el.unmask();
			ajaxErrorHandler("Error Registering");
		},

		params : {
			method : 'addprospect',
			prospect : Ext.encode(record),
			questions : Ext.encode(answeredquestions)
		}
	});

}

function ajaxErrorHandler(error) {

	alert(error);

}

function getAnsweredQuestions() {

	var questions = new Array();

	if (Ext.get('Question1').dom.checked) {
		var question1 = new Object();
		question1.QuestionID = 1
		question1.Results = "yes";
		questions.push(question1);
	}

	if (Ext.get('Question2').dom.checked) {
		var question2 = new Object();

		question2.QuestionID = 2
		question2.Results = "validate";
		questions.push(question2);
	}

	return questions;

}

function authenticatedownload(emailaddress) {

	Ext.Ajax.request({
		url : 'controllerhandler.ashx',
		success : function(response, options) {
			var content = response.responseText;

			if (content != "") {
				if (content.indexOf('error', 0) > -1) {
					registrationemail.setValue("");
					ajaxErrorHandler("Email address not found. Try again or complete registration form.");
					return;
				}

				swapDisplay(Ext.get('BoxDownload'), Ext.get('DownloadHolder'),
						content);
			} else {

				alert('there was an error processing your request')
				swapDisplay(Ext.get('BoxForm'));
			}
		},
		failure : function(response, options) {
			ajaxErrorHandler("Invalid Registration");
			swapDisplay(Ext.get('BoxForm'));
		},
		params : {
			method : 'authenticateregistration',
			emailaddress : emailaddress,
			campaignid : 1
		}
	});

}

function validateemail(emailaddress, id) {

	Ext.Ajax.request({
		url : 'controllerhandler.ashx',
		success : function(response, options) {
			var content = response.responseText;

			if (content != "") {
				if (content == "True") {
					authenticatedownload(emailaddress);
				} else {
					ajaxErrorHandler("Invalid Email Address");
					swapDisplay(Ext.get('BoxForm'));
				}
				// swapDisplay(Ext.get('BoxDownload'),content);
			} else {
				ajaxErrorHandler("Invalid Email Address");
				swapDisplay(Ext.get('BoxForm'));
			}
		},
		failure : function(response, options) {
			ajaxErrorHandler("Error Validating Email");
			swapDisplay(Ext.get('BoxForm'));
		},
		params : {
			method : 'validateemail',
			emailaddress : emailaddress,
			id : id
		}
	});

}

function checkregistration() {

	var href = window.location.href;
	var emailaddress = "";
	var id = "";

	if (href.indexOf('?') > 0 || href.indexOf('!') > 0) {

		var qs = (href.indexOf('?') > 0)
				? href.split('?')[1]
				: href.split('!')[1];
		var ps = Ext.urlDecode(qs);
		emailaddress = ps['emailaddress'];
		id = ps['id'];

	}

	if (emailaddress != "" && id != "") {
		validateemail(emailaddress, id);
		return true;
	} else {
		return false;
	}
}

function checkreturnregistration() {

	var emailaddress;

	if (registrationemail.getValue() != registrationemail.emptyText) {
		emailaddress = registrationemail.getValue();
		authenticatedownload(emailaddress);
	}

}

function swapDisplay(ItemToDisplay, contentholder, content) {

	if (displayeditem) {

		displayeditem.replaceClass('visibleitem', 'hiddenitem')

	}

	displayeditem = ItemToDisplay;
	displayeditem.replaceClass('hiddenitem', 'visibleitem');

	if (content) {

		contentholder.dom.innerHTML = content;

	}

	ItemToDisplay.show(true);

}

