﻿var focusPanel = 0;
var tarkenton = false;
var affiliate = false;

$(document).ready(function () {
	ConfigureAccordion();
	var prm = Sys.WebForms.PageRequestManager.getInstance();
	prm.add_endRequest(EndRequest);
});

function getQuerystring(key, default_) {
	if (default_ == null) default_ = "";
	key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if (qs == null)
		return default_;
	else
		return qs[1];
}


function EndRequest(sender, args) {
	ConfigureAccordion();
	SetCurrentPassword();
}

function ConfigureAccordion() {

	var accordion = $("#MainContainer").accordion({
		autoHeight: false,
		header: "div.accordion-header",
		changestart: function (event, ui) {
				ui.newHeader.addClass('accordion-header-selected');
				ui.oldHeader.removeClass('accordion-header-selected');
		},
		change: function (event, ui) {
			if ($(ui.newContent[ui.newContent.length - 1]).parent().css("overflow") == "scroll") {
				$(ui.newContent[ui.newContent.length - 1]).parent().css("overflow", "visible")
			}
		}
	});

	var type = getQuerystring('type');
	if (type == "register") {
		accordion.accordion("activate", 1);
	}
	
	//accordion.accordion("activate", focusPanel);

	if (tarkenton == true) {
		$('#divTarkenton').css({ "display": "block" });

		$('.accordion-header').each(function(i, item) {
			if (item.innerHTML.contains("I am an existing customer"))
				item.innerHTML = "<b>I have an account with InfoGroup Marketing Portal</b>";
			else
				item.innerHTML = "<b>Register to gain access to the InfoGroup Marketing Portal</b>";
		});
	}

	if (affiliate == true) {
		$('.accordion-content-panel').each(function(i, item) {
			item.style.width = "870px";
		});
	}
}

function VerifyEmail() {
	var plcAlreadyReg = document.getElementById(GetClientId("pnlAlreadyRegistered"));
	var emailFilter = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	var vTxtEmail = document.getElementById(GetClientId("txtEmailAddress"));
	var email = vTxtEmail.value;
	var divInvalidEmailAddress = document.getElementById("divInvalidEmailAddress");
	if (vTxtEmail.value.length > 0) {
		if (!emailFilter.test(vTxtEmail.value)) {
			divInvalidEmailAddress.style.display = "block";
			return false;
		}
		else {
			divInvalidEmailAddress.style.display = "none";
			PageMethods.ValidateUserID(email, ValidateUserIDCallback);
			return true;
		}
	}
	else {
		divInvalidEmailAddress.style.display = "none";
		plcAlreadyReg.style.display = "none"
	}
}

function ValidateUserIDCallback(results) {
	var plcAlreadyReg = document.getElementById(GetClientId("pnlAlreadyRegistered"));
	var txtAddress = document.getElementById(GetClientId("txtAddress"));
	var txtCity = document.getElementById(GetClientId("txtCity"));
	var txtCompany = document.getElementById(GetClientId("txtCompany"));
	var txtFirstName = document.getElementById(GetClientId("txtFirstName"));
	var txtLastName = document.getElementById(GetClientId("txtLastName"));
	var txtListName = document.getElementById(GetClientId("txtListName"));
	var txtPhone = document.getElementById(GetClientId("txtPhone"));
	var txtChoosePassword = document.getElementById(GetClientId("txtChoosePassword"));
	var txtConfirmPassword = document.getElementById(GetClientId("txtConfirmPassword"));
	var txtZIP = document.getElementById(GetClientId("txtZIP"));
	var ddlCountry = document.getElementById(GetClientId("ddlCountry"));
	var ddlHowDidYouHear = document.getElementById(GetClientId("ddlHowDidYouHear"));
	var ddlState = document.getElementById(GetClientId("ddlState"));
	var txtState = document.getElementById(GetClientId("txtState"));
	var btnRegister = document.getElementById(GetClientId("btnRegister"));
	var ckTerms = document.getElementById(GetClientId("ckTerms"));

	if (plcAlreadyReg == null)
		return;

	if (results == "1") // 1 = existing user
	{
		plcAlreadyReg.style.display = "";

		//Disable input controls
		txtAddress.disabled = true;
		txtCity.disabled = true;
		txtCompany.disabled = true;
		txtFirstName.disabled = true;
		txtLastName.disabled = true;
		if (txtListName != null)
			txtListName.disabled = true;
		txtPhone.disabled = true;
		txtChoosePassword.disabled = true;
		txtConfirmPassword.disabled = true;
		txtZIP.disabled = true;
		ddlCountry.disabled = true;
		btnRegister.disabled = true;
		ckTerms.disabled = true;

		//Gray out input controls
		txtAddress.style.backgroundColor = "#E5E5E5";
		txtCity.style.backgroundColor = "#E5E5E5";
		txtCompany.style.backgroundColor = "#E5E5E5";
		txtFirstName.style.backgroundColor = "#E5E5E5";
		txtLastName.style.backgroundColor = "#E5E5E5";
		if (txtListName != null)
			txtListName.style.backgroundColor = "#E5E5E5";
		txtPhone.style.backgroundColor = "#E5E5E5";
		txtChoosePassword.style.backgroundColor = "#E5E5E5";
		txtConfirmPassword.style.backgroundColor = "#E5E5E5";
		txtZIP.style.backgroundColor = "#E5E5E5";
		ddlCountry.style.backgroundColor = "#E5E5E5";

		if (ddlHowDidYouHear != null) {
			ddlHowDidYouHear.disabled = true;
			ddlHowDidYouHear.style.backgroundColor = "#E5E5E5";
		}

		if (ddlState != null) {
			ddlState.disabled = true;
			ddlState.style.backgroundColor = "#E5E5E5";
		}

		if (txtState != null) {
			txtState.disabled = true;
			txtState.style.backgroundColor = "#E5E5E5";
		}
	}
	else {
		plcAlreadyReg.style.display = "none";

		//Enable input controls
		txtAddress.disabled = false;
		txtCity.disabled = false;
		txtCompany.disabled = false;
		txtFirstName.disabled = false;
		txtLastName.disabled = false;
		if (txtListName != null)
			txtListName.disabled = false;
		txtPhone.disabled = false;
		txtChoosePassword.disabled = false;
		txtConfirmPassword.disabled = false;
		txtZIP.disabled = false;
		ddlCountry.disabled = false;
		btnRegister.disabled = false;
		ckTerms.disabled = false;

		//Un-Gray input controls
		txtAddress.style.border = "#999999 1px inset";
		txtCity.style.border = "#999999 1px inset";
		txtCompany.style.border = "#999999 1px inset";
		txtFirstName.style.border = "#999999 1px inset";
		txtLastName.style.border = "#999999 1px inset";
		if (txtListName != null)
			txtListName.style.border = "#999999 1px inset";
		txtPhone.style.border = "#999999 1px inset";
		txtChoosePassword.style.border = "#999999 1px inset";
		txtConfirmPassword.style.border = "#999999 1px inset";
		txtZIP.style.border = "#999999 1px inset";
		ddlCountry.style.border = "#999999 1px inset";

		txtAddress.style.backgroundColor = "#FFFFFF";
		txtCity.style.backgroundColor = "#FFFFFF";
		txtCompany.style.backgroundColor = "#FFFFFF";
		txtFirstName.style.backgroundColor = "#FFFFFF";
		txtLastName.style.backgroundColor = "#FFFFFF";
		if (txtListName != null)
			txtListName.style.backgroundColor = "#FFFFFF";
		txtPhone.style.backgroundColor = "#FFFFFF";
		txtChoosePassword.style.backgroundColor = "#FFFFFF";
		txtConfirmPassword.style.backgroundColor = "#FFFFFF";
		txtZIP.style.backgroundColor = "#FFFFFF";
		ddlCountry.style.backgroundColor = "#FFFFFF";

		if (ddlHowDidYouHear != null) {
			ddlHowDidYouHear.disabled = false;
			ddlHowDidYouHear.style.border = "#999999 1px inset";
			ddlHowDidYouHear.style.backgroundColor = "#FFFFFF";
		}

		if (ddlState != null) {
			ddlState.disabled = false;
			ddlState.style.border = "#999999 1px inset";
			ddlState.style.backgroundColor = "#FFFFFF";
		}

		if (txtState != null) {
			txtState.disabled = false;
			txtState.style.border = "#999999 1px inset";
			txtState.style.backgroundColor = "#FFFFFF";
		}

		if (txtListName != null) {
			txtListName.disabled = false;
			txtListName.style.border = "#999999 1px inset";
			txtListName.style.backgroundColor = "#FFFFFF";
		}
	}
}

function DefaultFocus() {
	var txtEmailAddress = document.getElementById(GetClientId("txtEmailAddress"));
	if (txtEmailAddress != null)
		txtEmailAddress.focus();
}

function DisableSignIn() {
	var btnSignIn = document.getElementById(GetClientId("btnSignIn"));
	if (btnSignIn != null)
		btnSignIn.disabled = true;
}

function EnableSignIn() {
	var btnSignIn = document.getElementById(GetClientId("btnSignIn"));
	if (btnSignIn != null)
		btnSignIn.disabled = false;
}

function SignOnClick() {	
	var txtUserName = jQuery('#' + GetClientId("txtUserName"));
	var txtPassword = jQuery('#' + GetClientId("txtPassword"));	
	if (typeof txtUserName != 'undefined' && typeof txtPassword != 'undefined') {
		jQuery('.CurrentUserName').val(txtUserName.val());
		jQuery('#' + GetClientId('hdnCurrentPassword')).val(txtPassword.val());
	}
}

function SetCurrentPassword() {
	jQuery('.CurrentPassword').val(jQuery('#' + GetClientId('hdnCurrentPassword')).val());
}
        
