var m_gCallback = null;

function visitorInfo_GetStandardFormValues( callback, reqPath ) {
    internal_requestStandardFormValues( callback, reqPath );
}

function visitorInfo_StandardFormValues() {

    // User information
    this.prefixId = '';
    this.firstName = '';
    this.lastName = '';
    this.title = '';
    this.company = '';
    this.address1 = '';
    this.address2 = '';
    this.city = '';
    this.stateId = '';
    this.countryId = '';
    this.zip = '';
    this.phone = '';
    this.email = '';

    // Email updates
    this.emailUpdates = '';

    // Lead questions
    this.businessId = '';
    this.primaryFieldId = '';
    this.marketId = '';

    // Offer center data
    this.offerStatus = '';
}

function internal_requestStandardFormValues( callback, reqPath ) {

    m_gCallback = callback;

    var url = 'http://' + location.host + reqPath;

    var parameters = "";
    var siteID = cda_getSiteID();
    if( siteID ) {
        parameters = "siteID=" + siteID;
    }

    userInfoRequest = new Ajax.Request(
    url,
    {
        method: 'get',
        parameters: parameters,
        requestHeaders: ['If-Modified-Since', 'Sat, 29 Oct 1994 19:43:31 GMT'],
        asynchronous: true,
        onComplete: onCompleteProcessing,
        onFailure: onFailureProcessing,
        onException: onExceptionProcessing
    } );
}

function onFailureProcessing() {
    if ( m_gCallback != null ) {
        m_gCallback( null );
    }
    m_gCallback = null;
}

function onExceptionProcessing() {
    if ( m_gCallback != null ) {
        m_gCallback( null );
    }
    m_gCallback = null;
}

function getXMLElementValue( n, name ) {
    var tag = n.getElementsByTagName( name );
    if ( tag == null ||
         tag.length == 0 ||
         tag[0].childNodes == null ||
         tag[0].childNodes.length == 0 ||
         tag[0].childNodes[0].nodeValue == null  ) return '';
    return tag[0].childNodes[0].nodeValue;
}

function getXMLAttributeValue( n, name, attr ) {
    var tag = n.getElementsByTagName( name );
    var attrVal = '';
    if ( tag != null && tag.length != 0 ) {
        attrVal = tag[0].getAttribute( attr );
    }
    return (attrVal == null) ? '' : attrVal;
}

function isEmptyResponse( xml ) {
    if ( xml == null ) return true;
    var root = xml.getElementsByTagName( "user_data" );
    if ( root == null || root.length == 0 ) return true;
    return ( isNoElements( root ) && isNoAttributes( root ) );
}

function isNoElements( root ) {
    return (root[0].childNodes == null ||
            root[0].childNodes.length == 0 );
}

function isNoAttributes( root ) {
    return ( root[0].attributes == null ||
             root[0].attributes.length == 0 );
}

function onCompleteProcessing( request ) {

    var userFormValues = null;

    if ( request != null &&
         request.responseXML != null &&
         ! isEmptyResponse( request.responseXML )) {

        var xml = request.responseXML;
        userFormValues = new visitorInfo_StandardFormValues();

        userFormValues.prefixId         = getXMLElementValue( xml, 'prefix_id' );
        userFormValues.firstName        = getXMLElementValue( xml, 'first_name' );
        userFormValues.lastName         = getXMLElementValue( xml, 'last_name' );
        userFormValues.full_name        = getXMLElementValue( xml, 'full_name' );
        userFormValues.title            = getXMLElementValue( xml, 'title' );
        userFormValues.company          = getXMLElementValue( xml, 'company' );
        userFormValues.address1         = getXMLElementValue( xml, 'address1' );
        userFormValues.address2         = getXMLElementValue( xml, 'address2' );
        userFormValues.city             = getXMLElementValue( xml, 'city' );
        userFormValues.stateId          = getXMLElementValue( xml, 'state_id' );
        userFormValues.countryId        = getXMLElementValue( xml, 'country_id' );
        userFormValues.zip              = getXMLElementValue( xml, 'zip' );
        userFormValues.phone            = getXMLElementValue( xml, 'phone' );
        userFormValues.email            = getXMLElementValue( xml, 'email' );

        // Email updates
        userFormValues.emailUpdates     = getXMLElementValue( xml, 'email_updates' );

        // Lead questions
        userFormValues.businessId       = getXMLElementValue( xml, 'business_id' );
        userFormValues.primaryFieldId   = getXMLElementValue( xml, 'primary_field_id' );
        userFormValues.marketId         = getXMLElementValue( xml, 'market_id' );

        // Offer center data
        userFormValues.offerStatus      = getXMLAttributeValue( xml, 'user_data', 'offer-status' );
    }

    if ( m_gCallback != null ) {
        m_gCallback( userFormValues );
    }
    m_gCallback = null;
}
