//STATEMACHINE
function STATEMACHINE()
{
	this.currentState = null;	
	this.initialize = STATEMACHINE_initialize;
	this.on_next = STATEMACHINE_on_next;
	this.on_back = STATEMACHINE_on_back;
	this.update_controls = STATEMACHINE_update_controls;
}

function STATEMACHINE_initialize()
{
	this.currentState = new EMAIL;
	this.currentState.display();
}	

function STATEMACHINE_update_controls()
{
	if(this.currentState.update_controls != null)
	{
		this.currentState.update_controls();
	}
}

function STATEMACHINE_on_next()
{
	if(this.currentState.validate())
	{
		this.currentState.hide();
		var nextState = this.currentState.next();
		if(nextState != null)
		{
			nextState.display();
		}
		this.currentState = nextState;
		this.update_controls();
	}
}

function STATEMACHINE_on_back()
{
	this.currentState.hide();
	var nextState = this.currentState.back();
	if(nextState != null)
	{
		nextState.display();
	}
	this.currentState = nextState;		
	this.update_controls();
}


//STATEOBJECT
function STATEOBJECT()
{
	this.window = null;
	this.next = null;
	this.back = null;
	this.validate = null;
	this.display = STATEOBJECT_display;
	this.hide = STATEOBJECT_hide;
	this.update_controls = null;
}

function STATEOBJECT_display()
{
	if(this.window != null)
	{
		this.window.style.display = "block";
	}
}

function STATEOBJECT_hide()
{
	if(this.window != null)
	{
		this.window.style.display = "none";
	}
}


// GLobals
var g_nc_subscriber = false;
var g_sp_subscriber = false;


//SURVEY
function SURVEY()
{
	this.validate = SURVEY_validate;
	this.next = SURVEY_next;
	this.window = document.getElementById('survey_window');	
	this.back = SURVEY_back;
	this.update_controls = SURVEY_update_controls;
}
SURVEY.prototype = new STATEOBJECT;


function SURVEY_back() {
    g_nc_subscriber = false;
    g_sp_subscriber = false;
	return new EMAIL;
}


function SURVEY_update_controls()
{
	var notCustomer = !document.getElementById('customerYesRadio').checked;
	document.getElementById('cardCheck').disabled = notCustomer;
	document.getElementById('softwareCheck').disabled = notCustomer;
	document.getElementById('otherCheck').disabled = notCustomer;
	document.getElementById('survey').style.color = notCustomer ? "gray" : "black";

	document.getElementById('bothNewslettersCheckbox').checked = false;

	if (g_sp_subscriber && g_nc_subscriber || (!g_sp_subscriber && !g_nc_subscriber))
        document.getElementById('bothNewslettersCheckbox').checked = true;
	else if (g_nc_subscriber)
	    document.getElementById('neurologicalNewslettersCheckbox').checked = true;
    else if (g_sp_subscriber)
	    document.getElementById('evidenceBasedNewslettersCheckbox').checked = true;

	if (g_nc_subscriber || g_sp_subscriber) 
    {
        document.getElementById('customerYesRadio').disabled = true;
        document.getElementById('customerNoRadio').disabled = true;
	}
}


function SURVEY_validate() {
    var valid_newsletter = false;

    var newsletter_validator = new VALIDATE();
    newsletter_validator.add_required_check_box_group([document.getElementById('evidenceBasedNewslettersCheckbox'), document.getElementById('neurologicalNewslettersCheckbox'),
        document.getElementById('bothNewslettersCheckbox')], document.getElementById('subscription_error'), "Please check at least one newsletter option.");

    valid_newsletter = newsletter_validator.validate();

    if (g_nc_subscriber || g_sp_subscriber) {
        return valid_newsletter;
    }

	var validator = new VALIDATE();
	
	validator.add_required_check_box_group([document.getElementById('customerYesRadio'), document.getElementById('customerNoRadio')],
		document.getElementById('surveyError2'),
		"Please answer yes or no.");

	var valid = validator.validate();
		
	if(document.getElementById('customerYesRadio').checked)
	{	
		var surveyValidator = new VALIDATE();
		    surveyValidator.add_required_check_box_group([document.getElementById('cardCheck'), document.getElementById('softwareCheck'), document.getElementById('otherCheck')],
			document.getElementById('surveyError1'),
			"Please check at least one item.");
			
		valid = surveyValidator.validate() ? valid : false;
	}
	else
	{
		document.getElementById('surveyError1').innerHTML = "";
	}

    return (valid && valid_newsletter);
}

function SURVEY_next() 
{

    if (g_nc_subscriber || g_sp_subscriber) 
    {
        // The customer already exists as a subscriber so just submit
        // without further questioning.
        document.getElementById('mainform').submit();
    }
	else if(document.getElementById('alreadyReceiveMailings').value == "1" ||
		document.getElementById('customerYesRadio').checked)
	{
		//this isn't unsetting their physical mailings subscription it is telling
		//the code to not insert the address fields into the db
		document.getElementById('receiveMailingsChkBox').checked = false;
		return new PROFESSION;
	}
	else
	{
		return new ADDRESS();
	}
}


//EMAIL
function EMAIL() 
{
    g_sp_subscriber = false;
    g_nc_subscriber = false;
    
    this.validate = EMAIL_validate;
	this.next = EMAIL_next;
	this.window = document.getElementById('email_window');
	
}
EMAIL.prototype = new STATEOBJECT();

function EMAIL_validate()
{
	var validator = new VALIDATE();
	validator.add_required_text_field(document.getElementById('emailTxtBox'),
		document.getElementById('emailError'),
		"Please supply a valid email.");
	validator.add_regular_expression_field(document.getElementById('emailTxtBox'),
		document.getElementById('emailError'),
		"Please supply a valid email.",
		/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
	
	
		
	var valid1 = validator.validate();
	
	
	
	var validator2 = new VALIDATE();
	validator2.add_required_check_box_group([document.getElementById('termsChkBox')], 
		document.getElementById('terms_error'),
		"Please agree to the terms and conditions");	
	
	var valid2 = validator2.validate();

	if(!valid1)
	{
		scroll(0, 0);
	}
	
	return valid1 && valid2;
	
}

function EMAIL_next() 
{
    g_sp_subscriber = false;
    g_nc_subscriber = false;

	var email = document.getElementById('emailTxtBox').value;
	var page = "/freeproducts/newsletter/CheckEmailExists.aspx";
	var response = post_page(page, "email=" + email);
	
	var exp = /<physical_mailing>(.*)<\/physical_mailing>/i;
    var result = exp.exec(response);
    if(result)
    {
        var mail = result[1];
        if(mail)
        {
			document.getElementById('alreadyReceiveMailings').value = "1";
        }
    }


    var ncregex = /<nc>(.+)<\/nc>/i;
    var ncresult = ncregex.exec(response);
    if (ncresult) {
        g_nc_subscriber = true;
    }

    var spregex = /<sp>(.+)<\/sp>/i;
    var spresult = spregex.exec(response);
    if (spresult) {
        g_sp_subscriber = true;
    }
    
    var exp2 = /<profession>(.*)<\/profession>/i;
    var result2 = exp2.exec(response);
    if(result2)
    {
		var profession_id = result2[1];
		document.getElementById('professionList_' + profession_id).checked = true;
    }
    
    return new SURVEY;
	
}



//ADDRESS
function ADDRESS()
{
	this.window = document.getElementById('address_window');
	this.validate = ADDRESS_validate;
	this.next = ADDRESS_next;
	this.back = ADDRESS_back;
	this.set_provide_address = ADDRESS_set_provide_address;
	this.update_controls = ADDRESS_update_controls;
}
ADDRESS.prototype = new STATEOBJECT;
	
function ADDRESS_validate()
{
	var valid = true;
	
	if(document.getElementById('receiveMailingsChkBox').checked)
	{		
		var validator = new VALIDATE();
		validator.add_required_text_field(document.getElementById('firstNameTxtBox'),
			firstNameError,
			"Required Field");
		validator.add_required_text_field(document.getElementById('lastNameTxtBox'),
			lastNameError,
			"Required Field");
		validator.add_required_text_field(document.getElementById('line1TxtBox'),
			line1Error,
			"Required Field");
		validator.add_required_text_field(document.getElementById('cityTxtBox'),
			cityError,
			"Required Field");
		validator.add_required_text_field(document.getElementById('stateTxtBox'),
			stateError,
			"Required Field");
		validator.add_required_text_field(document.getElementById('zipTxtBox'),
			zipError,
			"Required Field");

		valid = validator.validate();
	}
		
	return valid;
}

function ADDRESS_set_provide_address(setToTrue)
{
	document.getElementById('receiveMailingsChkBox').checked = setToTrue;
	this.update_controls();
}

function ADDRESS_back()
{
	return new SURVEY;
}

function ADDRESS_next()
{
	var receive = document.getElementById('receiveMailingsChkBox').checked;
	if(receive)
	{
		var newState = new PROFESSION;
		return newState;
	}
	else
	{
		return new COUPON;
	}
}

function ADDRESS_update_controls()
{
	var disabled = !document.getElementById('receiveMailingsChkBox').checked;
	var color = disabled ? "gray" : "black";
	
	document.getElementById('firstNameTxtBox').disabled = disabled;
	document.getElementById('firstNameTxtBox').style.color = color;
	document.getElementById('firstNameLbl').style.color = color;
	
	document.getElementById('lastNameTxtBox').disabled = disabled;
	document.getElementById('lastNameTxtBox').style.color = color;
	document.getElementById('lastNameLbl').style.color = color;
	
	document.getElementById('companyTxtBox').disabled = disabled;
	document.getElementById('companyTxtBox').style.color = color;
	document.getElementById('companyLbl').style.color = color;
	
	document.getElementById('line1TxtBox').disabled = disabled;
	document.getElementById('line1TxtBox').style.color = color;
	document.getElementById('line1Lbl').style.color = color;
	
	document.getElementById('line2TxtBox').disabled = disabled;
	document.getElementById('line2TxtBox').style.color = color;
	document.getElementById('line2Lbl').style.color = color;
	
	document.getElementById('cityTxtBox').disabled = disabled;
	document.getElementById('cityTxtBox').style.color = color;
	document.getElementById('cityLbl').style.color = color;
	
	document.getElementById('stateTxtBox').disabled = disabled;
	document.getElementById('stateTxtBox').style.color = color;
	document.getElementById('stateLbl').style.color = color;
	
	document.getElementById('zipTxtBox').disabled = disabled;
	document.getElementById('zipTxtBox').style.color = color;
	document.getElementById('zipLbl').style.color = color;
	
	//country
	document.getElementById('countryDropDown').disabled = disabled;
	document.getElementById('countryLbl').style.color = color;
}	


//COUPON
function COUPON()
{
	this.window = document.getElementById('coupon_window');
	this.next = COUPON_next;
	this.back = COUPON_back;
	this.validate = COUPON_validate;
}
COUPON.prototype = new STATEOBJECT;

function COUPON_next()
{
	if(document.getElementById('yesCoupon').checked)
	{
		document.getElementById('couponHidden').value = 1;
	
		var address = new ADDRESS;
		address.set_provide_address(true);
		return address;
	}
	else
	{
		return new PROFESSION;
	}
}	

function COUPON_back()
{
	return new ADDRESS;
}

function COUPON_validate()
{
	return true;
}


//PROFESSION
function PROFESSION()
{
	this.window = document.getElementById('profession_window');
	this.validate = PROFESSION_validate;
	this.back = PROFESSION_back;
	this.next = PROFESSION_next;
}
PROFESSION.prototype = new STATEOBJECT;

function PROFESSION_validate()
{
	var valid = true;
	
	var args = new Object();
	args.IsValid = false;
	
	//the profession form already has validation code using asp.net validation, but since I want this validated
	//when they hit next instead of submit I'll just call it here.
	profession_validate(null, args);
	
	if(!args.IsValid)
	{
		document.getElementById('profession_error').innerHTML = "Please select one.";
	}
	else
	{
		document.getElementById('profession_error').innerHTML = "";
	}
	
	return args.IsValid;
}

function PROFESSION_back()
{
	if(document.getElementById('alreadyReceiveMailings').value == "1" ||
		document.getElementById('customerYesRadio').checked)
	{
		return new SURVEY;
	}
	else
	{
		return new ADDRESS;
	}
}

function PROFESSION_next()
{
	document.getElementById('mainform').submit();
}
