/**
 * Fetches a page with url and parameters, then calls the callback function with the response as a parameter.
 */
var AjaxPageGetter = {
  handle:function(o) {
    this.callbackFunction(o.responseText);
  },
  startRequest:function(url, params, callbackFunction, method, scope) {
    method = useDefault(method, 'GET');
    this.callbackFunction = callbackFunction;
    this.scope = scope ? scope : this;
    YAHOO.util.Connect.asyncRequest(method, url, {
      success:this.handle,
      failure:this.handle,
      scope: this.scope
    }, params);
  }
};

/**
 * Posts a page with url and parameters, then calls the callback function with the response as a parameter.
 */
var AjaxPagePoster = {
  startRequest:function(url, params, callbackFunction) {
    AjaxPageGetter.startRequest(url, params, callbackFunction, 'POST');
  }
};

var AjaxJsonObjectRequest = {
  handle:function(o) {
    var json = undefined;
    try {
      json = parseJsonObject(o.responseText);
      if (this.callbackFunction) {
        if (this.scope) {
          this.callbackFunction.apply(this.scope, [json]);
        }
        else {
          this.callbackFunction(json);
        }
      }
    } catch (e) {
      if (this.failedCallbackFunction) {
        if (this.scope) {
          this.failedCallbackFunction.apply(this.scope);
        }
        else {
          this.failedCallbackFunction();
        }
      }
    }
  },
  startRequest:function(url, params, callbackFunction, failedCallbackFunction, method, scope) {
    method = useDefault(method, 'POST');
    this.callbackFunction = callbackFunction;
    this.failedCallbackFunction = failedCallbackFunction;
    this.scope = scope ? scope : this;
    YAHOO.util.Connect.asyncRequest(method, url, {
      success:this.handle,
      failure:this.failedCallbackFunction,
      scope: this.scope
    }, params);
  }
};

/**
 * Submits a form then runs the callback function with the complete response object as parameter.
 * Use response.responseText to get the body of the response.
 */
var AjaxFormSubmitter = {
  startRequest:function(form, callbackFunction, failedCallbackFunction, action) {
    if (!action) action = form.action;
    var postData = formParametersToStringFormat(form);
    AjaxUrlPostRequest.startRequest(action, postData, callbackFunction, failedCallbackFunction);
  }
};

var AjaxRegisterUserFromForm = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(form, callbackFunction) {
    var url = '/ajax.user.registeruser.do.action';
    this.callbackFunction = callbackFunction;
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, formParametersToStringFormat(form));
  }
};

var AjaxLoginUser = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(email, password, callbackFunction) {
    this.callbackFunction = callbackFunction;
    var url = '/ajax.user.loginuser.do.action';
    var parameters = new RequestParameters();
    parameters.addParameter("email", email);
    parameters.addParameter("password", password);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, parameters.toString());
  }
};

var AjaxLogoutUser = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(callbackFunction) {
    this.callbackFunction = callbackFunction;
    var url = '/ajax.user.logoutuser.do.action';
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, undefined);
  }
};

var AjaxForgotPassword = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(email, callbackFunction) {
    this.callbackFunction = callbackFunction;
    var url = checkAction('ajax.user.forgotpassword.do');
    var parameters = new RequestParameters();
    parameters.addParameter("email", email);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, parameters.toString());
  }
};

var AjaxChangePassword = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(password, newPassword, newPasswordRepeat, callbackFunction) {
    this.callbackFunction = callbackFunction;
    var url = checkAction('ajax.user.changepassword.do');
    var parameters = new RequestParameters();
    parameters.addParameter("password", password);
    parameters.addParameter("newPassword", newPassword);
    parameters.addParameter("newPasswordRepeat", newPasswordRepeat);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, parameters.toString());
  }
};

var AjaxChangePasswordForm = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(form, callbackFunction) {
    this.callbackFunction = callbackFunction;
    var url = checkAction('ajax.user.changepassword.do');
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, formParametersToStringFormat(form));
  }
};

var AjaxChangeEmailForm = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(form, callbackFunction) {
    this.callbackFunction = callbackFunction;
    var url = checkAction('ajax.user.changeemail.do');
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, formParametersToStringFormat(form));
  }
};

var AjaxCheckEmail = {
  handle:function(o) {
    this.validatedFunction.apply(this.callbackScope,
                                 [parseJsonObject(o.responseText), this.customObject]
      );
  },
  startRequest:function(email, callbackFunction, callbackScope, customObject) {
    this.callbackScope = callbackScope;
    this.validatedFunction = callbackFunction;
    this.customObject = customObject;
    var url = '/ajax.user.checkemail.do.action';
    var parameters = new RequestParameters();
    parameters.addParameter("email", email);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope:this
    }, parameters.toString());
  }
};

var AjaxVerifyEmail = {
  handle:function(o) {
    this.validatedFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(email, code, callbackFunction) {
    this.validatedFunction = callbackFunction;
    var url = '/ajax.user.verifyemail.do.action';
    var parameters = new RequestParameters();
    parameters.addParameter("email", email);
    parameters.addParameter("code", code);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope:this
    }, parameters.toString());
  }
};

var AjaxVerifyNewEmail = {
  handle:function(o) {
    this.validatedFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(email, newEmail, code, callbackFunction) {
    this.validatedFunction = callbackFunction;
    var url = '/ajax.user.verifynewemail.do.action';
    var parameters = new RequestParameters();
    parameters.addParameter("email", email);
    parameters.addParameter("newEmail", newEmail);
    parameters.addParameter("code", code);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope:this
    }, parameters.toString());
  }
};

var AjaxCheckIsPasswordCorrect = {
  handle:function(o) {
    this.validatedFunction.apply(this.callbackScope,
                                 [parseJsonObject(o.responseText), this.customObject]
      );
  },
  startRequest:function(password, callbackFunction, callbackScope, customObject) {
    this.callbackScope = callbackScope;
    this.validatedFunction = callbackFunction;
    this.customObject = customObject;
    var url = '/ajax.user.ispasswordcorrect.do.action';
    var parameters = new RequestParameters();
    parameters.addParameter("password", password);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope:this
    }, parameters.toString());
  }
};


/** AJAX classes for traveller data requests **/

/**
 * Stores a traveller, returns SUCCESS and the resulting ID if success, otherwise FAILED.
 */
var AjaxStoreTraveller = {
  startRequest:function(form, callbackFunction, failCallback) {
    var url = checkAction('ajax.user.storetraveller.do');
    var params = formParametersToStringFormat(form);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:function(o) {
        callbackFunction(parseJsonObject(o.responseText));
      },
      failure:failCallback,
      scope: this
    }, params);
  }
};

/**
 * Deletes a traveller.
 */
var AjaxDeleteTraveller = {
  handle:function(o) {
    this.callbackFunction(parseJsonObject(o.responseText));
  },
  startRequest:function(id, callbackFunction) {
    this.callbackFunction = callbackFunction;
    var url = checkAction('ajax.user.deletetraveller.do');
    var parameters = new RequestParameters();
    parameters.addParameter("id", id);
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:this.handle,
      failure:this.handle,
      scope: this
    }, parameters.toString());
  }
};

/** General function that the AJAX classes depend upon **/

function evaluateAllChildrenScripts(element) {
  var scripts = element.getElementsByTagName('script');
  for (var i = 0; i < scripts.length; i++) {
    var script = scripts[i];
    //ibelog('evaluating: ' + script.innerHTML);
    eval(script.innerHTML);
  }
  // Run onLoad function.
  if (typeof onSubContentLoad == 'function') {
    onSubContentLoad();
  }
}


/** These functions are new for the new JSON implementation. They need to be updated for IBE. **/

/**
 * Submits a form then runs the callback function with the complete response object as parameter.
 * Use response.responseText to get the body of the response.
 */
var AjaxUrlGetRequest = {
  startRequest:function(url, callbackFunction, failureCallbackFunction) {
    YAHOO.util.Connect.asyncRequest('GET', url, {
      success:function(o) {
        var response = undefined;
        var success = false;
        try {
          response = parseJsonObject(o.responseText);
          success = true;
        } catch (e) {
          ibeerror('Parsing of JSON failed: ' + exceptionToString(e));
          failureCallbackFunction();
        }
        if (success && callbackFunction && typeof callbackFunction === 'function') {
          try {
            callbackFunction(response);
          } catch (e) {
            ibeerror("Exception when running success callback: " + exceptionToString(e));
          }
        }
      },
      failure:failureCallbackFunction,
      scope: this
    }, undefined);
  }
};

/**
 * Submits a form then runs the callback function with the complete response object as parameter.
 * Use response.responseText to get the body of the response.
 */
var AjaxUrlPostRequest = {
  startRequest:function(url, postData, callbackFunction, failureCallbackFunction) {
    YAHOO.util.Connect.asyncRequest('POST', url, {
      success:function(o) {
        var response = undefined;
        var success = false;
        try {
          response = parseJsonObject(o.responseText);
          success = true;
        } catch (e) {
        }
        if (success) {
          if (callbackFunction && typeof callbackFunction === 'function') callbackFunction(response);
        } else {
          if (failureCallbackFunction && typeof failureCallbackFunction === 'function') failureCallbackFunction();
        }
      },
      failure:failureCallbackFunction,
      scope: this
    }, postData);
  }
};
