// The field field_name cannot be empty
var msg1 = "The field '";
var msg1a = "' is empty.";

// The field field_name must be a number
var msg2 = "The field '";
var msg2a = "' must be a number.";

// The field field_name must be a valid email :REMOVE
var msg3 = "The field '";
var msg3a = "' must be a valid email.";

// field_name has invalid value.
var msg4 = " has an invalid value.";

// Cannot contain illegal characters or white space
var msg5 = "There are illegal characters and/or whitespaces in your 'Username'.";

// The field field_name cannot have whitespace
var msg6 = "The field '";
var msg6a = "' cannot have any whitespaces.";

// The passwords do not match
var msg7 = "The passwords do not match.";

// Invalid birthday
var msg8 = "Your birthday is invalid.";

// Term of use not check
var msg9 = "Hang on!  Please agree to the \"Terms of Use\" to continue!";


function validatePassword(password, confirmed_password){
  if(password != confirmed_password){
    alert(msg7);
    return false;
  }
  return true;
}

function findElement(array, element){
  for(var i=0; i<array.length; i++){
    if (array[i] == element)
      return i;
  }
  return -1;
}

function validateDate(month, day){
  shortMonth = new Array(4,6,9,11);
  longMonth = new Array(1,3,5,7,8,10,12);

  //it's a shortmonth
  if(findElement(shortMonth, month)>=0){
    if(day > 30){
      alert(msg8);
      return false;
    }
  }else if(findElement(longMonth, month)>=0){ //long month
    if(day > 31){
      alert(msg8);
      return false;
    }
  }
  else{ //feburary
    if(day > 29){
      alert(msg8);
      return false;
    }
  }
  return true;
}

function validateTermOfUse(term_of_use_checkbox){
  if(!term_of_use_checkbox.checked){
    alert(msg9);
    return false;
  }
  return true;
}



/********************
 *
 *   Input class
 *
 ********************/
function Input(obj){
  if(arguments.length>0){
    this.init(obj);
  }
}

Input.prototype.init = function(obj){
  this.obj = obj;
};

Input.prototype.isNotEmpty = function(){
  if (this.obj.isEmpty == null || this.obj.isEmpty.toLowerCase() == "true"){
    return true;
  }
  //check to see if the value is empty
  if (this.obj.value == ""){
    alert(msg1 + this.obj.field_name + msg1a);
    return false;
  }
  return true;
};

Input.prototype.trimString = function(string) {
  string = string.replace( /^\s+/g, "" ); // strip leading
  return string.replace( /\s+$/g, "" );   // strip trailing
};

//trim front and end
Input.prototype.trimFE = function(){
  this.obj.value = this.obj.value.replace( /^\s+/g, "" );
  this.obj.value = this.obj.value.replace( /\s+$/g, "" );
};

Input.prototype.hasSpace = function(){
  if(this.obj.value.indexOf(" ")>=0){
    alert(msg6 + this.obj.field_name + msg6a);
    return false;
  }
  return true;
};

Input.prototype.isInt = function(){
  //var value = this.trimString(this.obj.value);
  var value = this.obj.value;
  for (var i = 0; i < value.length; i++) {
    if (value.charAt(i) < "0" || value.charAt(i) > "9") {
      alert(msg2 + this.obj.field_name + msg2a);
      return false;
    }
  }
  return true;
};

Input.prototype.validate = function(){
  var isValid = true;
  this.trimFE();
  while(1){
    if(!(isValid = this.isNotEmpty())) break;
    break;
  }
  return isValid;
};

/********************
 *
 *   InputTextNoSpace class inheritance
 *
 ********************/
InputTextNoSpace.prototype = new Input();
InputTextNoSpace.prototype.constructor = InputTextNoSpace;
InputTextNoSpace.superclass = Input.prototype;

// InputTextNoSpace constructor
function InputTextNoSpace(obj){
  if (arguments.length>0)
    this.init(obj);
}

InputTextNoSpace.prototype.init = function(obj){
  InputTextNoSpace.superclass.init(obj);
};

//check for illegal characters
InputTextNoSpace.prototype.goodChar = function(){
  badChar = new Array("$","\"","<",">","(",")",";"," ");
  for(var i=0; i<badChar.length; i++){
    if(this.obj.value.indexOf(badChar[i])>=0){
      alert(msg5);
      return false;
    }
  }
  return true;
};

InputTextNoSpace.prototype.validate = function(){
  var isValid = true;
  while(1){
    if(!(isValid = InputTextNoSpace.superclass.validate())) break;
    if((isValid = this.goodChar())) break;
    break;
  }
  return isValid;
};

/********************
 *
 *   InputText class inheritance
 *
 ********************/
InputText.prototype = new Input();
InputText.prototype.constructor = InputText;
InputText.superclass = Input.prototype;

// InputText constructor
function InputText(obj){
  if (arguments.length>0)
    this.init(obj);
}

InputText.prototype.init = function(obj){
  InputText.superclass.init(obj);
};

//check for illegal characters
InputText.prototype.goodChar = function(){
  badChar = new Array("$","\"","<",">","(",")",";");
  for(var i=0; i<badChar.length; i++){
    if(this.obj.value.indexOf(badChar[i])>=0){
      alert(msg5);
      return false;
    }
  }
  return true;
};

InputText.prototype.minLength = function(){
  if(this.obj.minLength != null){
    if (this.obj.value.length < this.obj.minLength){
      alert("Please use your real last name!"); //this.obj.field_name
      return false;
    }
  }
  return true;
};

InputText.prototype.validate = function(){
  var isValid = true;
  while(1){
    if(!(isValid = InputText.superclass.validate())) break;
    if(!(isValid = this.goodChar())) break;
    if((isValid = this.minLength())) break;
    break;
  }
  return isValid;
};

/********************
 *
 *   InputEmail class inheritance
 *
 ********************/
InputEmail.prototype = new InputText();
InputEmail.prototype.constructor = InputEmail;
InputEmail.superclass = InputText.prototype;

// InputEmail constructor
function InputEmail(obj){
  if (arguments.length>0)
    this.init(obj);
}

InputEmail.prototype.init = function(obj){
  InputEmail.superclass.init(obj);
};

InputEmail.prototype.hasAt = function(){
  if(this.obj.value.indexOf("@")<0){
    alert("Please use a valid email address!");//msg3 + this.obj.field_name + msg3a
    return false;
  }
  return true;
};

InputEmail.prototype.validate = function(){
  var isValid = true;
  while(1){
    if(!(isValid = InputEmail.superclass.validate())) break;  //inherite the validate method
    if(!(isValid = this.hasAt())) break;
    if(!(isValid = InputEmail.superclass.hasSpace())) break;
    break;
  }
  return isValid;
};

/********************
 *
 *   InputNumber class inheritance
 *
 ********************/
InputNumber.prototype = new Input();
InputNumber.prototype.constructor = InputNumber;
InputNumber.superclass = Input.prototype;

// InputNumber constructor
function InputNumber(obj){
  if (arguments.length>0)
    this.init(obj);
}

InputNumber.prototype.init = function(obj){
  InputNumber.superclass.init(obj);
};

InputNumber.prototype.gleMin = function(){
  if(this.obj.min != null){
    if(parseInt(this.obj.value, 10) >= parseInt(this.obj.min, 10))
      return true;
    else{
      alert(this.obj.field_name + msg4);
      return false;
    }
  }
  return true;
};

InputNumber.prototype.lleMax = function(){
  if(this.obj.max != null){
    if(parseInt(this.obj.value, 10) <= parseInt(this.obj.max, 10))
      return true;
    else{
      alert(this.obj.field_name + msg4);
      return false;
    }
  }
  return true;
};

InputNumber.prototype.validate = function(){
  
  var isValid = true;
  while(1){
    if(!(isValid = InputNumber.superclass.validate())) break;
    if(!(isValid = InputNumber.superclass.isInt())) break;
    if(!(isValid = this.gleMin())) break;
    if(!(isValid = this.lleMax())) break;
    break;
  }
  return isValid;
};

/********************
 *
 *   InputPhone class inheritance
 *
 ********************/
InputPhone.prototype = new Input();
InputPhone.prototype.constructor = InputPhone;
InputPhone.superclass = Input.prototype;

// InputNumber constructor
function InputPhone(obj){
  if (arguments.length>0)
    this.init(obj);
}

InputPhone.prototype.init = function(obj){
  InputPhone.superclass.init(obj);
};

//string.replace( /^\s+/g, "" ); 
InputPhone.prototype.isNumber = function(){
  this.obj.value = this.obj.value.replace(/\s+/g,""); //remove space
  this.obj.value = this.obj.value.replace(/-+/g,"");  //remove hyphen
  return InputPhone.superclass.isInt(); 
};

InputPhone.prototype.validate = function(){
  var isValid = true;
  while(1){
    if(!(isValid = InputPhone.superclass.validate())) break;
    if(!(isValid = this.isNumber())) break;
    break;
  }
  return isValid;
};

/********************
 *
 *   InputFactory class
 *
 ********************/
function InputFactory(obj){
  this.obj = obj;
}

InputFactory.prototype.getInput = function(){
  var value;

  //doing this so that field_type is not a required parameter
  if (this.obj.field_type == null)
    value = "default";
  else
    value = this.obj.field_type.toLowerCase();

  switch (value) {
    case "text_no_space":
      return new InputTextNoSpace(this.obj);
    case "text":
      return new InputText(this.obj);
    case "email":
      return new InputEmail(this.obj);
    case "number":
      return new InputNumber(this.obj);
    case "phone":
      return new InputPhone(this.obj);
    default:
      return new Input(this.obj);
  }
};
