// ERROR.JS
// Error reporting module 
//
// Dependencies: window.js, prototype.js
//
// History: 
//
//	15/10/2009		Sean Wilson     Initial Window.js implementation

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}

var UPRError = {
  _theme: 'alphacube',
  _title: 'Error',
  _height: 250,
  _width: 350,
  
  setTheme: function(s) {
    var oldVal = this._theme;
    
    if (typeof(s) == 'undefined' || s == '') {
      this._theme = 'alphacube';
    }
    else {
      this._theme = s;
    }
    return oldVal;
  },

  setTitle: function(s) {
    var oldVal = this._title;
    
    if (typeof(s) == 'undefined' || s == '') {
      this._title = 'Error';
    }
    else {
      this._title = s;
    }
    return oldVal;
  },
  
  setHeight: function(s) {
    var oldVal = this._height;
    
    if (typeof(s) == 'undefined' || s == '') {
      this._height = 250;
    }
    else {
      this._height = s;
    }
    return oldVal;
  },
  
  setWidth: function(s) {
    var oldVal = this._height;
    
    if (typeof(s) == 'undefined' || s == '') {
      this._width = 350;
    }
    else {
      this._width = s;
    }
    return oldVal;
  },
  
  Report: function (msg) { 
    var msgText;
      
    msgText = '<span class="error_text">'+msg+'</span>';

    // Add payment failure help
    var pmtCheck = "Payment card validation failed";
      
    if (msgText.toUpperCase().search(pmtCheck.toUpperCase()) > -1) {
      msgText = msgText+'<br><br><strong>Note</strong> that if paying by card the <strong>invoice address</strong> must be the address that appears on your <strong>card statement</strong>.';
    }

    // Add helpful "call me" link
    msgText = '<br>Oh dear! There has been a problem processing your request: <br><br>'+msgText+'<br><br>Please try again.  If the problems continue please <a href="/information/call-me"><strong>click here</strong></a> to request a call-back and we will be happy to help you.</div>'

    var win = new Window({ 
      className: this._theme, width: this._width, height: this._height, zIndex: 100, resizable: true, 
      title: this._title, 
      showEffect: Effect.BlindDown, 
      hideEffect: Effect.SwitchOff, 
      draggable: true, wiredDrag: true});
       
      win.getContent().innerHTML= msgText; 
      win.showCenter(true);
  },

  Elements: function (Id) {
    // Checks for an error message generated by the Unipower back-end and 
    // displays it using the standard error reporting mechanism.
    var errorText = "";

    $$(Id).each(function (e) {
      if (e) {
        var s;
      
        if (typeof(e.innerText) != "undefined") {
          // IE
  	     s = e.innerText;
        }		
        else {
          // Firefox, Opera etc.
          s = e.textContent;
        } 
      
        if (typeof(s) != "undefined") {
          errorText = errorText=="" ? s: errorText+"<br><br>"+s;
        }
        // Hide UPR error control
        e.style.visibility = 'hidden';
        e.style.display    = 'none';
      }
    });

    if (errorText && errorText != "") {
      errorText = errorText.trim();
    }
    else {
      errorText = "";
    }
        
    return errorText;    
  },

  Check: function () {
    // Checks for an error message generated by the Unipower back-end and 
    // displays it using the standard error reporting mechanism.
    var errorUserDetails = this.Elements(".UPRBRServerErrorLabel");
    var errorLogin = this.Elements(".UPRLoginErrorLabel");
    var errorReportElement = this.Elements("ErrorReport");
    var errorDetail = this.Elements("ValidationDetail");
    var errorText = "";

    // Build error report lines
    if (errorReportElement != "") {
      errorText = errorText+(errorText == ""? "": "<br><br>")+errorReportElement;
    }
    if (errorUserDetails != "") {
      if (errorUserDetails != "There are no items in the basket.") {
        errorText = errorText+(errorText == ""? "": "<br><br>")+errorUserDetails;
      }
    }
    if (errorDetail != "") {
      errorText = errorText+(errorText == ""? "": "<br><br>")+errorDetail;
    }
    if (errorLogin != "") {
      errorText = errorText+(errorText == ""? "": "<br><br>")+errorLogin;
    }

    if (errorText && errorText != "") {
      // Show our error control
      this.Report(errorText);
    }
  }
}



