/**
  The intention of this js file is to submit leads to the servlet
  configured by AutoPage.
  Any site needs to include this js after the quote if they are not able to handle the leads themselves.
  The site may also include this JS and override the methods to suit its needs, for submitting leads but taking
  some other action.
**/

//Clicky variables
var uname;
var email;
var mobile;
var model;

function LeadSubmitter ( quote )
{
    this.quote = quote ;
}

/**
  Returns the callback function to use to process the data.
  Override this method if the result needs to be processed in some other
  manner, than the one provided, or the format of the return data
  is different.
**/
LeadSubmitter.prototype.getCallback = function ()
{
    return '__leadSubmitter.handleSubmission' ;
}

/**
  Get the type of submission to be performed
**/
LeadSubmitter.prototype.getSubmissionType = function ()
{
    return 'HyundaiSignupBeans' ;
}

/**
  Returns the URL to post the send the data to
  Override this method if the quote needs to be posted to some other location
**/
LeadSubmitter.prototype.getURLToPost = function ()
{
    return TRS.ConfigMap.ComponentsBaseURL + 'submitadf' ;
}

/**
  Constructs the URL for the quote.
  TODO: Make this a loop throug the keys and values of the quote
  so that more fields can be added with no issues.
  TODO: Fix the server to be able to pick up salutation, first name and last name as seperate entities
**/
LeadSubmitter.prototype.constructQuoteURL = function ()
{
    var url = '' ;

	uname = encodeURIComponent(this.quote.firstname + ", " + this.quote.lastname);
	email = encodeURIComponent(this.quote.email) ;
	mobile = encodeURIComponent(this.quote.mobile) ;
	model = encodeURIComponent(this.quote.model) ;

    url += encodeURIComponent('brand') + '=' + encodeURIComponent(this.quote.brand) ;
    url += '&' + encodeURIComponent('model') + '=' + encodeURIComponent(this.quote.model) ;
    url += '&' + encodeURIComponent('location') + '=' + encodeURIComponent(this.quote.state + " " + this.quote.city) ;
    url += '&' + encodeURIComponent('prefix') + '=' + encodeURIComponent(this.quote.salutation ) ;
    url += '&' + encodeURIComponent('personName') + '=' + encodeURIComponent(this.quote.firstname + " " + this.quote.lastname ) ;
    if ( this.quote.landline != '-' ) // If no Landline was provided it need not be filled
        url += '&' + encodeURIComponent('landline') + '=' + encodeURIComponent(this.quote.landline) ;
    url += '&' + encodeURIComponent('email') + '=' + encodeURIComponent(this.quote.email) ;
	url += '&' + encodeURIComponent('subprovider') + '=' + encodeURIComponent(this.quote.subprovider) ;
    url += '&' + encodeURIComponent('mobile') + '=' + encodeURIComponent(this.quote.mobile) ;
    url += '&' + encodeURIComponent('preference') + '=' + encodeURIComponent(this.quote.preference) ;
    url += '&' + encodeURIComponent('comments') + '=' + encodeURIComponent(this.quote.comments) ;
    return url ;
}

/**
  Constructs the complete URL.
**/
LeadSubmitter.prototype.constructFullURL = function ()
{
    return this.getURLToPost() + '?' + this.constructQuoteURL() + '&'
        + encodeURIComponent('signuptype') + '=' + encodeURIComponent(this.getSubmissionType()) + '&'
        + encodeURIComponent('callback') + '=' + encodeURIComponent(this.getCallback())
}

LeadSubmitter.prototype.submit = function ()
{
    queryForData ( this.constructFullURL(), function() {} ) ;
}

LeadSubmitter.prototype.handleSubmission = function ( submitStatus, errormessage)
{
    //if ( submitStatus == 'OK' ) { // If the submission was successful - Commented this since the user needs to be transitioned to the Thank U page irrespective of the response of the ADF submitter
        this.handleSuccess() ;
    //} else {
    //    this.handleFailure( errormessage ) ;
    //}
}

/**
  Handler for the failure scenario.
**/
LeadSubmitter.prototype.handleFailure = function ( failureMessage )
{
    if ( failureMessage == '' ) {
        alert ( 'Could not submit your details, Please try again later' ) ;
    } else {
        alert ( failureMessage ) ;
    }
}

/**
  Handler for the successful scenario.
**/
LeadSubmitter.prototype.handleSuccess = function ()
{
  //  var url = 'india_thankyou.jsp?'
  //      + encodeURIComponent('make') + '=' + encodeURIComponent(this.quote.brand) + '&'
  //      + encodeURIComponent('model') + '=' + encodeURIComponent(this.quote.model.split(' ', 2)[1] ) + '&'
  //      + encodeURIComponent('year') + '=' + encodeURIComponent(this.quote.model.split(' ', 2)[0] ) + '&'
  //  switchURL(url);

    var url = 'india_thankyou.jsp?'
        + encodeURIComponent('make') + '=' + encodeURIComponent(this.quote.brand) + '&'
        + encodeURIComponent('model') + '=' + encodeURIComponent(this.quote.model.split(' ', 2)[1] ) + '&'
        + encodeURIComponent('year') + '=' + encodeURIComponent(this.quote.model.split(' ', 2)[0] ) + '&' 
        + encodeURIComponent('uname') + '=' + uname
	 + '&' + encodeURIComponent('email') + '=' + email
	 + '&' + encodeURIComponent('mobile') + '=' + mobile 
	switchURL(url);
}

/**
  Function to switch the URL
**/
function switchURL ( url )
{
    this.location = url ;
}

var __leadSubmitter ;

/**
  The function that the Lead submitter is expecting
**/
var handle_gaq_submit = function(quote)
{
    __leadSubmitter = new LeadSubmitter(quote);
    __leadSubmitter.submit() ;
}
