/*	Reg and Preference Forms Functions
	-------------------------------- */
	var isInit = true;
	/*regSetState: handles country and state select behavior*/
	function regSetState() {
		var formObj, stateSelect, selectedCountry, stateRequiredEl, zipRequiredEl, phoneTextField, mobileRadioButton_Yes, mobileRadionButton_No;
		stateRequiredEl = document.getElementById('stateMessageLayer');
		zipRequiredEl = document.getElementById('zipMessageLayer');
		phoneTextField = document.getElementById('phoneNumber');
		formObj = document.getElementById('userProfileForm');
		stateSelect = formObj.state;
		selectedCountry = formObj.country.options[formObj.country.selectedIndex].value;
		stateSelect.length = 0;
        mobileRadioButton_Yes = document.getElementById('mobile_alerts_yes');
        mobileRadioButton_No = document.getElementById('mobile_alerts_no');
        switch(selectedCountry){
            case "US":
                if(!isInit){
					toggleMobileAlerts( false );
				}
                toggleRequiredStateAndZip( false );
                for (var i = 0; i < usaList.length; i++)
                    stateSelect.options[i] = new Option(usaList[i], usaURL[i]);
                break;
            case "CA":
                toggleMobileAlerts( true );
                toggleRequiredStateAndZip( false );
                for (var i = 0; i < canadaList.length; i++) 
                  stateSelect.options[i] = new Option(canadaList[i], canadaURL[i]);
                break;
            default:
                toggleMobileAlerts( true );
                toggleRequiredStateAndZip( true );
                break;
        }
		if (selectedYear == "") formObj.yearOfBirth.value = "YYYY";  //if no year is selected, print YYYY
		else if (selectedYear != "YYYY") formObj.yearOfBirth.value = selectedYear; //in a year is selected, print it	
		if (selectedState != "") {
			for (var i = 0; i < stateSelect.length; i++)
				if (stateSelect.options[i].value == selectedState)
					stateSelect.options[i].selected = true;
		}
		isInit = false;
        function toggleMobileAlerts( isDisabled ){
            if(mobileRadioButton_Yes && mobileRadioButton_No){
				//check/uncheck the no radio button and enable/disable the yes radio button
				mobileRadioButton_No.checked = mobileRadioButton_Yes.disabled = isDisabled;
			}
			stateSelect.disabled = isDisabled;
			if(phoneTextField){
				phoneTextField.disabled = isDisabled;
                phoneTextField.style.backgroundColor = ( isDisabled ? '#e6e6dd' : '');
				if(isDisabled){
                    phoneTextField.value = '';
                    removeMobileError( phoneTextField);
                }
			}
        }
        function removeMobileError(phoneTextField){
            var fieldrow, element;
            fieldrow = phoneTextField.parentNode;
            if( fieldrow.className.match(/err/) ) fieldrow.className = fieldrow.className.replace(/err/, '');
            
            element = fieldrow.previousSibling;
            while( element ){
                if(element.nodeType == 1)break;   
                else element = element.previousSibling;
            }
            if( element.className.match(/errorMessage/) )
                element.parentNode.removeChild(element);
        }
        function toggleRequiredStateAndZip( isDisabled ){
            var displayState = ( isDisabled ? "none" : "inline" );
            stateRequiredEl.style.display = zipRequiredEl.style.display = displayState;
			stateSelect.disabled = isDisabled;
        }
	}
	function initPreferencesForm( prefs ){
		var submitBtn, prefForm;
		submitBtn = document.getElementById('preferences_submit');
		prefForm = document.getElementById('userProfileForm');
		submitBtn.onclick = function( ){
			clearYYYY(prefForm);
			validateMobileSelection( );
            if(!prefs) ccUpsell( );
            prefForm.submit();
		}
	}
	function validateMobileSelection( ){
		var formObj = document.getElementById('userProfileForm');
		if(!formObj.country.selectedIndex) return; //for prefs tabs where the hidden field is used instead
		var selectedCountry = formObj.country.options[formObj.country.selectedIndex].value;
		var mobileHiddenField = document.getElementById('mobile_hidden_field');
		if(mobileHiddenField && selectedCountry != 'US') mobileHiddenField.value = 'UNSUBSCRIBED'
	}
	function clearYYYY(form) {
		  var fyear = form.yearOfBirth.value;
		  if (fyear == "YYYY") form.yearOfBirth.value = "";
		  return true;
	}

/*	Autochecked Checkboxes
	---------------------- */
	function setupAutocheckedBoxes(){
		var checkboxEls = getElements('autochecked', 'input', null);
		for(var i=0; i<checkboxEls.length; i++)
			new AutocheckedBox(checkboxEls[i])
	}
	function AutocheckedBox(checkboxEl){
		this.checkboxEl = checkboxEl;
		this.fieldName = checkboxEl.id.replace(".checkbox", "");
		this.fieldEl = document.getElementById(this.fieldName);
		this.checkedValue = this.getValue( true );
		this.uncheckedValue = this.getValue( false );
		this.handleEvents();
	}
	AutocheckedBox.prototype.handleEvents = function( ){
		var AutocheckedBox = this;
		this.checkboxEl.onchange = function( ){ AutocheckedBox.setValue( ); }
	}
	AutocheckedBox.prototype.getValue = function(isChecked){
		var hiddenField, suffix
		suffix = ( isChecked ? '.checked' : '.unchecked');
		hiddenField = document.getElementById( this.fieldName + suffix );
		return hiddenField.value;
	}
	AutocheckedBox.prototype.setValue = function(){
		var currentValue, newValue;
		currentValue = this.fieldEl.value;
		newValue = ( currentValue == this.checkedValue ? this.uncheckedValue : this.checkedValue);
		this.fieldEl.value = newValue;
	}
    
/*	Expandable Units
	---------------- */
	function expandableUnit(args){
		this.unitEl = args.unitEl;
		this.isExpanded = this.getInitialState( ); //default is collapsed, classname of expanded asserts initial open state
		this.init( );
	}
	expandableUnit.prototype.init = function( ){
		var expandableUnit = this;
		this.knob = getElements('knob', null, this.unitEl)[0];
		this.knob.onclick = function(event){expandableUnit.switchState(event)}
	}
    expandableUnit.prototype.getInitialState = function( ){
        if(this.unitEl.className.match("expanded") ) return true;
        if(this.unitEl.className.match("collapsed") ) return false;
        this.unitEl.className += " collapsed";
        return false;
    }
	expandableUnit.prototype.switchState = function(event){
		this.isExpanded ? this.collapse( ) : this.expand( );
	}
	expandableUnit.prototype.expand = function( ){
        this.unitEl.className = this.unitEl.className.replace(/collapsed/, "expanded")
		this.isExpanded = true;
        this.knob.innerHTML = "Close";
	}
	expandableUnit.prototype.collapse = function( ){
		this.unitEl.className = this.unitEl.className.replace(/expanded/, "collapsed")
		this.isExpanded = false;
        this.knob.innerHTML = "Open";
	}
    
/*  AjaxFormField
    -------------- */
    function AjaxFormField(args){
        this.fieldEl = document.getElementById(args.fieldId);
		this.fieldContainerEl = document.getElementById(args.fieldContainerId);
		this.fieldContainerErrorClass = args.fieldContainerErrorClass;
		this.errorMessageClass = args.errorMessageClass;
        this.validationURL = args.validationURL;
        this.isInvalid = args.isInvalid;
        this.errorMessageText = args.errorMessageText;
		this.errorStateActive = false;
        this.init( );
    }
    AjaxFormField.prototype.init= function( ){
        var ajaxFormField = this;
        this.fieldEl.onblur = function( ){ajaxFormField.validate( )}
    }
    AjaxFormField.prototype.validate = function( ){
		if(!this.fieldEl.value){
			this.hideErrorState( );
			return;
		}
        var util, xhr, responseObj, ajaxFormField, fullRequestPath;
        fullRequestPath = this.validationURL + this.fieldEl.value;
        ajaxFormField = this;
        util = new ConciergeUtility( );
        xhr = util.getXHR( );
        xhr.onreadystatechange = function( ){
            if (xhr.readyState == 4 && xhr.status ==200){
                responseObj = eval('(' + xhr.responseText + ')');
                ajaxFormField.handleResponse(responseObj)
            }
			else ajaxFormField.hideErrorState( );
        }
        xhr.open("GET", fullRequestPath, true);
        xhr.send(null);
    }
    AjaxFormField.prototype.handleResponse = function( responseObj){
        var isInvalid = responseObj[this.isInvalid];
        if(isInvalid) this.showErrorState( responseObj );
		else this.hideErrorState( );
    }
    AjaxFormField.prototype.showErrorState = function( responseObj ){
		if(this.errorStateActive) return;
		var errorMsgTxt = responseObj[this.errorMessageText];
		this.fieldContainerEl.className += ' ' + this.fieldContainerErrorClass;
		this.errorMsgEl = document.createElement('P');
		this.errorMsgEl.innerHTML = errorMsgTxt;
		this.errorMsgEl.className = this.errorMessageClass;
		this.fieldContainerEl.appendChild(this.errorMsgEl);
		this.errorStateActive = true;
	}
    AjaxFormField.prototype.hideErrorState = function(){
		if(!this.errorStateActive) return;
		this.fieldContainerEl.className = this.fieldContainerEl.className.replace(this.fieldContainerErrorClass, '');
        this.fieldContainerEl.removeChild(this.errorMsgEl);
		delete this.errorMsgEl;
		this.errorStateActive = false;
	}

/*	Moved from HTMLINCLUDEs
	------------------------- */
    function gotoDreamtripSignInUrl() {
        gotoDreamtripSignInUrlUsingBase("/user/dreamtrip2008");
    }
    function gotoDreamtripSignInUrlUsingBase(baseUrl) {
        var returnto = document.userProfileForm.returnto.value;
        window.location = baseUrl + "/signin" + 
            ((returnto == "") ? "" : "?returnto=" + escape(returnto));    
    }
    function gotoSignInUrl() {
        var returnto = document.userProfileForm.returnto.value;
        window.location = "/user/signin" + 
            ((returnto == "") ? "" : "?returnto=" + escape(returnto));
    }
