// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

function parseRegion(string){
	
	var lastIndex = string.lastIndexOf("_") + 1;
	return string.substring(lastIndex,string.length);
	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function formatTag(incstring,propid,brand,type){

	string = '';
	
	//string out the IM=
	var lastIndex = incstring.lastIndexOf("&IM=");
	if(lastIndex>-1){
		string = incstring.substring(0,lastIndex);
	}
	
	string += "&IM=LP_ALASKA1000MILES_"+brand+"_"+propid;
	
	if(type!=''){
		string += "_"+type;
	}
	
	//region
	var lastIndexRegion = incstring.lastIndexOf("_");
	if(lastIndexRegion>-1){
		region = incstring.substring(lastIndexRegion);
		string += region;
	}
	
	string = string.replace('&ratePlanName=99999','&ratePlanName=FDLY03');
	
	return string;

}

String.prototype.convertBrandURL = function(){

	var brandRef = new Array();
	
	brandRef["SI"] = "sheraton";
	brandRef["AL"] = "alofthotels";
	brandRef["EL"] = "element";
	brandRef["4P"] = "fourpoints";
	brandRef["MD"] = "lemeridien";
	brandRef["LC"] = "luxury";
	brandRef["SR"] = "stregis";
	brandRef["WH"] = "whotels";
	brandRef["WI"] = "westin";
	brandRef["GX"] = "gx";
	
	return brandRef[this];

}

function convertBrand2(name){

	var brandRef = new Array();
	
	brandRef["she"] = "Sheraton";
	brandRef["alo"] = "aloft Hotels";
	brandRef["ele"] = "Element";
	brandRef["fpt"] = "Four Points by Sheraton";
	brandRef["lem"] = "Le Méridien";
	brandRef["lux"] = "Luxury Collection";
	brandRef["str"] = "St. Regis";
	brandRef["who"] = "W Hotels";
	brandRef["wes"] = "Westin";
	brandRef["his"] = "Historic Hotels";
	
	return brandRef[name];

}

String.prototype.convertBrand = function(){

	var brandRef = new Array();
	
	brandRef["SI"] = "Sheraton";
	brandRef["AL"] = "aloft Hotels";
	brandRef["EL"] = "Element";
	brandRef["4P"] = "Four Points by Sheraton";
	brandRef["MD"] = "Le Méridien";
	brandRef["LC"] = "Luxury Collection";
	brandRef["SR"] = "St. Regis";
	brandRef["WH"] = "W Hotels";
	brandRef["WI"] = "Westin";
	brandRef["GX"] = "Historic Hotels";
	
	return brandRef[this];

}