/**
/* forms.js
/*
/* @copyright: 2007 by Thomas M. Stambaugh & Zeetix, LLC (http://www.zeetix.com)
/* All rights reserved.
/*
/* The contents of this file may not be copied, duplicated, or used without the
/* written consent of Zeetix, LLC.
**/

/**
 * The Abstract ancestor of the Form family
 */
function AbstractFormData(){
}

AbstractFormData.prototype.handleInvalidField_ = function (aFieldName){
    this._invalidFieldNames.push(aFieldName);
}

AbstractFormData.prototype.doFields = function() {
    return this.subclassResponsibility();
}

AbstractFormData.prototype.fields = function() {
    return this.doFields();
}

AbstractFormData.prototype.doFormName = function() {
    return this.subclassResponsibility();
}

AbstractFormData.prototype.formName = function() {
    return this.doFormName();
}

AbstractFormData.prototype.doInitialize = function() {
}

AbstractFormData.prototype.initialize = function() {
    this._timeStamp = null;
    this._invalidFieldNames = new Array(0);
    this._timeStamp = Date();
    var aFieldsObject = this.fields();
    for (var aFieldName in aFieldsObject) {
        var aFormElement = document.forms[this.formName()][aFieldName];
        //If the field is required, then insist that it present and non-null.
        if (aFieldsObject[aFieldName] && (!aFormElement || aFormElement.value == '')) {
            this.handleInvalidField_(aFieldName);
            }
        else {
            this[aFieldName] = aFormElement.value;
            }
        };
    this.doInitialize();
}

AbstractFormData.prototype.doFinalize = function() {
}

/**
 * Fix an apparent core leak, the _invalidFieldNames array is
 * apparently not getting garbage collected.
 */
AbstractFormData.prototype.finalize = function() {
    this.doFinalize();
    for (var aFieldName in this) {
        this[aFieldName] = null;
        }
}

/**
 * The "Followup" Form
 */
function FollowupFormData(){}
FollowupFormData._fields = {
            'salutation': false,
            'firstName': true,
            'lastName': true,
            'streetAddress1': true,
            'streetAddress2': false,
            'townCity': true,
            'state': true,
            'postalCode': true,
            'country': true,
            'primaryPhone':true,
            'alternatePhone':false,
            'cellPhone':false,
            'faxPhone':false,
            'email':true,
            'newsletterCheckbox':false};
FollowupFormData._formName = 'followupForm';

FollowupFormData.prototype = new AbstractFormData();
FollowupFormData.prototype.constructor = FollowupFormData;

FollowupFormData.create = function(){
    var answer = new FollowupFormData();
    answer.initialize();
    return answer;
}

FollowupFormData.onSubmitHandler = function() {
    var aFormData = FollowupFormData.create();
    var answer = null;
    if (aFormData._invalidFieldNames.length != 0) {
        //Complain and block submission
        aFormData.highlightInvalidFields();
        aFormData.complain_('Please complete the fields marked in red and resubmit.');
        answer = false;
        }
    else {
        answer = true;
        }
    aFormData.finalize();
    aFormData = null;
    return answer;
}

/**
 * Turn the labels of the invalid fields red.
 */
FollowupFormData.prototype.highlightInvalidFields = function () {
    for (var i=0; i<this._invalidFieldNames.length; i++) {
        var anInvalidFieldName = this._invalidFieldNames[i];
        var anElementId = anInvalidFieldName + 'Label';
        document.getElementById(anElementId).style.color = "#ff0000";
    }
}

/**
 * Turn on the complaint
 */
FollowupFormData.prototype.complain_ = function (aComplaint) {
    var aComplaintElement = document.getElementById('complaint');
    aComplaintElement.innerHTML=aComplaint;
    aComplaintElement.style.visibility = 'visible';
}

FollowupFormData.prototype.doFields = function () {
    answer = FollowupFormData._fields;
    return answer;
}

FollowupFormData.prototype.doFormName = function() {
    return FollowupFormData._formName;
}

/**
 * The FormApplication class
 */
FormApplication = function() {
}
/**
 * Why is this causing the status bar to stay busy in firefox?
 */
FormApplication.prototype.loadVPage = function(aJSONString, aVPageDocument, aLocation, aBoolean) {
    if (!isLoaded()) {
        //Don't try to load until I have a map.
        //I'm not trying to handle _nsxl...
        window._pending = [];
        for (var i = 0; i < arguments.length; ++i) {
            window._pending.push(arguments[i]);}}
    else {
        // This handles the load event.
        //What should I do with aBoolean?
        //Perhaps...
        //loadJSON.apply(this, arguments)
        this.loadJSON(aJSONString, aVPageDocument, aLocation, aBoolean);
    }
}

/**
 * Collect and parse anJSONString (provided by the vp iframe).
 */
FormApplication.prototype.loadJSON = function(aJSONString, aDocument, aLocation, aBoolean) {
    this.vpageDoc=aDocument;
    this.vpageUrl=aLocation;
    var aThing = JSON.parse(aJSONString);
    if (!aThing || !aThing.wasSuccessful) {
        this.complain_('Whoops, something bad happened. Please try again later');
        }
    else {
        this.confirm();
        }
    return;
}

FormApplication.prototype.confirm = function() {
    var aFormDiv = document.getElementById('followupForm');
    var aConfirmationDiv = document.getElementById('confirmationDiv');
    aFormDiv.style.display = 'none';
    aConfirmationDiv.style.display = 'inline';
    var aTrackingString = aFormDiv.form_sku.value.split(" ").join("_");
    urchinTracker('/form/confirm/' + aTrackingString);
}

/**
 * Turn on the complaint
 */
FormApplication.prototype.complain_ = function (aComplaint) {
    var aComplaintElement = document.getElementById('complaint');
    aComplaintElement.innerHTML=aComplaint;
    aComplaintElement.style.visibility = 'visible';
}


/**
 * Global/functions
 */

/**
 * Generate and write to the current document a script tag
 * that imports a script from aScriptUrl
 */
function importScript_(aScriptUrl) {
    var answer = '<' + 'script src="' + aScriptUrl + '"' +' type="text/javascript"><' + '/script>';
    document.write(answer);
}
