
function mybHome (){
	this.bookingRef = '';
	this.surname = '';
	this.error = '';
	this.originalSurname = '';
	this.containsForeignCharacters = false;
}

mybHome.prototype.anyErrors=function(){
	return this.error;
}

mybHome.prototype.doesSurnameHasDiacritics=function(){
	return this.containsForeignCharacters;
}

mybHome.prototype.changedSurname=function(){
	return this.surname;
}

mybHome.prototype.validateMYBLogin=function(bookingRef, surname){
	this.bookingRef = bookingRef;
	this.surname = surname;
	this.originalSurname = surname;
	this.error = '';
	
	if(this.bookingRef.indexOf('Booking') > -1){
		this.bookingRef = '';
	}
	this.bookingRef = replaceAll(this.bookingRef, "\'", " ");
	this.bookingRef = replaceAll(this.bookingRef, "-", " ");
	
	this.surname = replaceAll(this.surname, "\'", " ");
	this.surname = replaceAll(this.surname, "-", " ");
	
	if(this.surname != null && this.surname.length > 0 && this.bookingRef != null && this.bookingRef.length > 0){
		this.surname = trim(this.surname);
		if(this.surname.length == 6
			&& isAlphanumeric(this.surname) 
			&& isAlphaWithSpace(this.bookingRef)){
			if(!(isAlphabet(this.surname) && isAlphabet(this.bookingRef) && this.bookingRef.length == 6)){
				var swap = this.surname;
				this.surname = this.bookingRef;
				this.bookingRef = swap;
			}
		}
	}
	
	
	
	
	if(this.bookingRef == null || this.bookingRef.length < 6 || !isAlphanumeric(this.bookingRef)){
		this.error = "bookingref is invalid";
	}
	
	if(this.surname == null || this.surname.length < 1 || this.surname.toLowerCase() == 'last name'){
		if(this.error.length > 0){
			this.error = this.error + ", surname null";			
		} else{
			this.error = "surname null";		
		}
	} else {
			this.surname = replaceDiacritics(surname);
			if(this.surname != this.originalSurname){
				this.containsForeignCharacters = true;
			}
			if(isAlphaWithSpace(this.surname)){
				this.surname = replaceUpperCase(this.surname);
			} else {
				if(this.error.length > 0){
					this.error = this.error + ", surname invalid";			
				} else{
					this.error = "surname invalid";		
				}
			}
	}
}


function isAlphabet(checkString) {
        var regExp = /^[A-Za-z]$/;
        if(checkString != null && checkString != "")
        {
          for(var i = 0; i < checkString.length; i++)
          { 
            if (!checkString.charAt(i).match(regExp))
            {
              return false;
            }
          }
        }
        else
        {
          return false;
        }
        return true;
}

function isAlphanumeric(checkString) {
        var regExp = /^[A-Za-z0-9]$/;
        if(checkString != null && checkString != "")
        {
          for(var i = 0; i < checkString.length; i++)
          { 
            if (!checkString.charAt(i).match(regExp))
            {
              return false;
            }
          }
        }
        else
        {
          return false;
        }
        return true;
}

function isAlphaWithSpace(checkString) {
        var regExp = /^[A-Za-z\s]$/;
        if(checkString != null && checkString != "")
        {
          for(var i = 0; i < checkString.length; i++)
          { 
            if (!checkString.charAt(i).match(regExp))
            {
				return false;
            }
          }
        }
        else
        {
          return false;
        }
        return true;
}

function isUpperCase(checkString) {
        var regExp = /^[A-Z\s]$/;
        if(checkString != null && checkString != "")
        {
          for(var i = 0; i < checkString.length; i++)
          { 
            if (!checkString.charAt(i).match(regExp))
            {
				return false;
            }
          }
        }
        else
        {
          return false;
        }
        return true;
}



function replaceAll(orgString, stringToFind,stringToReplace){
    
    var index = orgString.indexOf(stringToFind);
        while(index != -1){
            orgString = orgString.replace(stringToFind,stringToReplace);
            index = orgString.indexOf(stringToFind);
        }
        
        return orgString;
}

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
	
}

function replaceUpperCase( value ) {
	if(isUpperCase(value)){
		return  value;
	} else{
		var regExpCaps = /^[A-Z]$/;
		var isSpace = false;
		var finalString = value.charAt(0);
		if(value != null && value.length > 0 && !isUpperCase(value)){
		  for(var i = 1; i < value.length; i++){ 
			if (value.charAt(i).match(regExpCaps) && !isSpace)
			{
				finalString = finalString + " " + value.charAt(i);
			} else{
				finalString = finalString + value.charAt(i);
				
				if (value.charAt(i) == " ") {
					isSpace = true;
				} else {
					isSpace = false;
				}			
			}
			  }
		}
		return finalString;
	}
}

function replaceDiacritics(surname)
{
	var surname;
	var regExp = /^[A-Za-z\s]*$/;
	if(surname.match(regExp)){
		return surname;
	} else{
		var diacritics =[
		/[\300-\303]/g, /[\304]/g, /[\305-\306]/g, /[\340-\343]/g, /[\344]/g, /[\345-\346]/g,// A, Ae, A, a, ae, a
		/[\310-\313]/g, /[\350-\353]/g, // E, e
		/[\314-\317]/g, /[\354-\357]/g, // I, i
		/[\322-\325]/g, /[\326]/g, /[\362-\365]/g, /[\366]/g, // O, Oe, o, oe
		/[\331-\333]/g, /[\334]/g, /[\371-\373]/g, /[\374]/g,  // U, Ue, u, ue
		/[\321]/g, /[\361]/g, // N, n
		/[\307]/g, /[\347]/g, // C, c
		/[\335]/g, /[\375]/g, /[\377]/g,// Y, y, y
		/[\330]/g, /[\370]/g, // C, c
		];

		var chars = ['A','AE','A','a','ae','a','E','e','I','i','O','OE','o','oe','U','UE','u','ue','N','n','C','c','Y','y','y','O','o'];

		for (var i = 0; i < diacritics.length; i++)
		{
			surname = surname.replace(diacritics[i],chars[i]);
		}
		return surname;
	}
}