function Utils() {
  throw new Error("Utils is a static class. You can't create instances of it.");
}

Utils.HOST_NAME_PRODUCTION  = "hostNameProduction";
Utils.HOST_NAME_STAGE  	  	= "hostNameStage";
Utils.HOST_NAME_BUILD		= "hostNameBuild";
Utils.HOST_NAME_LOCAL		= "hostNameLocal";
Utils.HOST_NAME_UNKNOWN	  	= "hostNameUnknown";

Utils.SUFFIX_TEMPLATE		= "Template";
Utils.SUFFIX_INNER			= "Inner";

Utils.getHostName = function() {
	var loc = document.location;
	var hostName = loc.hostname;
		
	if (hostName == "localhost" || hostName == "127.0.0.1") {
		return Utils.HOST_NAME_LOCAL;
	} else if (hostName.indexOf("build") != -1) {
		return Utils.HOST_NAME_BUILD;
	} else if (hostName.indexOf("stage") != -1) {
		return Utils.HOST_NAME_STAGE;
	} else if (hostName.indexOf("imshopping") != -1) {
		return Utils.HOST_NAME_PRODUCTION;
	} else {
		return Utils.HOST_NAME_UNKNOWN;
	}	
};

Utils.isEnter = function(e) {
	var keynum;
	var keychar;
	var numcheck;
	if(window.event) { // IE
		keynum = e.keyCode;
	}
	else if(e.which) { // Netscape/Firefox/Opera
		keynum = e.which;
	}
	if (keynum == 13) {
		return true;
	} else {
		return false;
	} 
};

Utils.isIE = function() {
  return (navigator.appName == "Microsoft Internet Explorer");
};

Utils.processTemplateId = function(templateId, container, indexId){
	var template = $("#" + templateId + Utils.SUFFIX_TEMPLATE).html();
	container.append(template);
	var innerId = templateId + Utils.SUFFIX_INNER;
	$("#" + innerId).attr("id", innerId + indexId);
	Utils.showElement(container);
};

Utils.processTemplateIds = function(templateId, containerId, indexId){
	var template = $("#" + templateId + Utils.SUFFIX_TEMPLATE).html();
	$("#" + containerId).append(template);
	$("#" + containerId).find("." + templateId + Utils.SUFFIX_INNER + ":last *[id]").attr("id", function () {
		 return $(this).attr("id");
	}).each(function () {
		 $(this).attr("id", this.id + indexId);
	  });
	$("#" + containerId).show();
};


Utils.validateEmail = function(email){
	//The Official Standard: RFC 2822
	var re = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i;
	if(re.test(email)) {
    	return true; 
    } else { 
	    return false;
	} 
};

Utils.googleAnalyticsTrack = function(url) {
	Utils.log("googleAnalyticsTrack:"+url);
	if (typeof pageTracker != "undefined" && null != pageTracker) {
		pageTracker._trackPageview(url);
		gTestFramework.trackAction(window.location.href, url);
	}
};

Utils.trimString = function(str) {
	if(typeof str != "undefined" && null != str ) {
		return str.replace(/^[ \t]+|[ \t]+$/g,"");
	} else {
		return "";
	}
};

Utils.getJsonObject = function(object) {
	if (object == null){
		return null;
	}
	return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(object.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + object + ')');
};

Utils.checkIsMicrositePage = function() {
	if (typeof MICROSITE_URL_PREFIX != "undefined" && null != MICROSITE_URL_PREFIX && "" != MICROSITE_URL_PREFIX) {
		return true;
	} else {
		return false;
	}
};

Utils.getEventTarget = function(e) {
    var targ = (e.currentTarget)? e.currentTarget : e.srcElement;
    if (targ.nodeType == 3) { // Safari
		targ = targ.parentNode;
    }
    return targ;
};

Utils.addEventListener = function (inElement, inEventType, inCallback, inCaptures) {
  if (inElement.addEventListener) {
    // for DOM Level 2 browsers, like Firefox
    inElement.addEventListener(inEventType, inCallback, inCaptures);
  } else {
    if (inElement.attachEvent) {
      // for Internet Explorer
      inElement.attachEvent("on"+inEventType, inCallback, inCaptures);
    }
  } 
};

Utils.endsWith = function(string, match){
	return ((string.substring(string.length - match.length) == match) ? true : false);
};

Utils.startsWith = function(string, match){
	return ((string.substring(0, match.length) == match) ? true : false);
};

Utils.makeCuttedStr = function (str, cuttedLength) {
	if (str == null) {
		return "";
	}	
	var resultStr = "";
	if (str.length >= cuttedLength + 3) {
		resultStr = str.substr(0,cuttedLength) + "...";
	} else {
		resultStr = str;
	}
	return resultStr;
};

Utils.validateHTML = function(str) {
	if (/<\/?[a-z][a-z0-9]*[^<>]*>/i.test(str)) {
		return true;
	} else {
		return false;
	}
};

Utils.validateUrl = function(url) {
	var regexp = /^https?:\/\/(\d+\.\d+\.\d+\.\d+|([a-zA-Z][\w-]*\w\.)+([a-zA-Z][\w-]{0,2})\w|localhost)(:[1-9][0-9]*)?(\/([\w\-.\/:%+@&=]+[\w\-.\/?:%+@&=]*)?)?(#(.*))?$/i;
	if(regexp.test(url)) {
    	return true; 
    } else {
	    return false;
	} 
};

Utils.htmlDenc = function(str){
	if (!str){
		return null;
	}
	str = str.replace(/&quot;/g,"\"");
	str = str.replace(/&amp;/g,"&");
	str = str.replace(/&amp/g,"&");
	str = str.replace(/&lt;/g,"<");
	str = str.replace(/&gt;/g,">");
	str = str.replace(/&nbsp;/g," ");
	str = str.replace(/&#39;/g,"\'");
	return str;
}

Utils.makeIdSuitable = function(name) {
	if (!name){
		return null;
	}
	//TODO ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). (http://www.w3.org/TR/html4/types.html)
	return name.replace(/ /g,"_").replace(/&/g, "_and_");
}

Utils.htmlConvert = function(str){
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	return str;
}

Utils.hideElement = function (element){
	element.css("display", "none");
}

Utils.showElement = function (element){
	element.css("display", "block");
}

Utils.formatDoubleToString = function BRS(value, separator) { // Str a String
  	value = String(value);
	var L = value.length;
  	var P = value.indexOf(".");
  	if (P < 0) {
  		return value + separator + "00"; // was integer
	}	
  	if (P == L - 2) {
		return value.replace(".", separator) + "0"; // was tenths
	}	
  	if (P == L - 3) {
		return value.replace(".", separator); // was hundredths
	}	
  	if (value.charAt(0) == "-") {
		return "-" + BRS(value.substring(1)); // NEG
	}	
  	var Q = value.substring(P+3); // tail
  	if (Q < "5") {
		return value.substring(0, P + 3);
	}	
  	if (Q > "5") {
		return BRS(String(0.0125 + +value.substring(0, P + 3)));
	}	
  	// past here, a Bankers' decision is needed
  	Q = (value.charCodeAt(P+2) % 2)? +0.001 : -0.001;
	return (BRS(String(Q + +value)).replace(".", separator));
}
	
Utils.isString = function (inValue) {
		return ((typeof inValue) == "string");
};

Utils.isNumber = function (inValue) {
		return (((typeof inValue) == "number") && isFinite(inValue));
};

Utils.isNumeric = function (inValue) {
  var isNumber = Utils.isNumber(inValue);
  if (isNumber) {
    return true;
  }
  var isNumeric = Utils.isString(inValue) && Utils.isNumber(parseInt(inValue));
  return isNumeric;
};
	
Utils.getUrlParam  = function( name ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ){
		return "";
	} else {
		return results[1];
	}
};
	
Utils.showInPopUpWindow = function(link, popupWidth, popupHeight) {
	var width = popupWidth;
	var height = popupHeight;
	
	if (null == width) {
		width = 1000;
	}
	if (null == height) {
		height = 600;
	}
	
	window.open(link, "_blank", "width=" + width + ", height=" + height + ", status=1, toolbar=1, location=1, menubar=1, directories=1, resizable=1, scrollbars=1");
	return false;
};

Utils.log = function(msg){
	if (window.console) {
		console.log(msg);
	}	
};

Utils.scaleImage = function(box, img) {
	// Grab the image's dimensions
	var imgH = img.attr("height");
	var imgW = img.attr("width");
	
	// Find which dimension is scaled the most
	var scaleH = box.attr("height") / img.attr("height");
	var scaleW = box.attr("width") / img.attr("width");
	
	// Scale the image
	if (scaleH < scaleW) {
		img.attr("height", box.attr("height"));
		img.attr("width", Math.round(imgW * scaleH));
	} else {
		img.attr("width", box.attr("width"));
		img.attr("height", Math.round(imgH * scaleW));
	}
};

Utils.renderLazyImages = function() {
	$(".lazyImage").each(function(){
    	var self = $(this);
    	var defaultUrl = "/images/shopNote/defaultImage.gif";
		var realSrc = self.attr("realSrc");
		if(realSrc){
    		self.attr("src", realSrc);
    	} else if (realSrc == "") {
    		self.attr("src", defaultUrl);
    	}
    });
};

Utils.isElementPresent = function(elementList, element) {
	var result = false;
	for (var i=0; i<elementList.length; i++) {
		if (elementList[i] == element) {
			result = true;
			break;
		}  
	} return result;
};

Utils.isInteger = function(s) {
    var i;
    if (s != null && s.length > 0){
	    for (i = 0; i < s.length; i++){
	    	var c = s.charAt(i);
	    	if (!Utils.isDigit(c)) {
				return false;
			}
	    }
	    return true;
    }
    return false;
};

Utils.isDigit = function(c){
	return ((c >= "0") && (c <= "9"));
};


