var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA	-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (typeof String.prototype.trim !== 'function') {
	String.prototype.trim = function () {
		return this.replace(/^\s+|\s+$/g, '');
	}
}
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 isNumberKey(evt)
{
	 var charCode = (evt.which) ? evt.which : evt.keyCode
	 if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	 return true;
}
//check for valid numeric strings
function IsNumeric(strString)
{
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
	  {
			 blnResult = false;
	  }
   }
   return blnResult;
}
function IsValidEmailAddress(address)
{
	return emailRegex.test(address);
}
function ValidSearch() {
	var searchVal = $('#strSearch').val();
	if (searchVal == "SEARCH" || searchVal.length < 4) {
		alert("Please make sure your search query is more than 3 characters");
		return false;
	}
	return true;
}
function CheckValue(id, checkForNumber)
{
	var el = $('#' + id);
	//ignore element if nt found
	if(el == undefined || el.length == 0) return false;
	
	var error = false;

	if(el.val() == "" || (el.attr('tagName') == "SELECT" && el.val() == 0) || (checkForNumber != undefined && isNaN(el.val())))
	{
		error = true;
	}
	
	ErrorState(error, el);
}
function ErrorState(error, el)
{
	if(error)
	{
		//red border
		el.css("border","2px solid #FF3300");
		
		//add "required" to span
		var text = el.parent().find(" > span.required");
		if(text.length > 0 && text.html().trim() == "*")
		{
			text.append("<span class='required'> required</span>");
		}
	}
	else
	{
		//clear error styles
		el.css("border","");
		el.parent().find(" > span.required").html("*");
	}
}
function makeTabs(el, container)
{
	//ignore selected tab clicks
	if(el.hasClass("selected")) return;
	
	var selectedAnchor = $(container + " a.selected");
	
	$(selectedAnchor.attr("href")).hide();
	
	selectedAnchor.removeClass("selected");
	
	el.addClass("selected");
	$(el.attr("href")).show();		
}
	
$(function () {
	//for search box
	$("#strSearch").bind("blur",function(){
		if($(this).val() == "")
		{
			$(this).val("SEARCH");
		}
	});
	$("#strSearch").bind("focus",function(){
		if($(this).val() == "SEARCH")
		{
			$(this).val("");
		}
	});
	
	//add js calls so we can style no JS in CSS
	$("body").removeClass("NOJS").addClass("JS");
	
	//hide all tabs
	$("div.tab").hide();
	
	$("#showroom-tabs a[href^='#']").bind("click dblclick", function(e){
		e.preventDefault();
		makeTabs($(this),"#showroom-tabs");
	});
	
	$("#specification-tabs a[href^='#']").bind("click dblclick", function(e){
		e.preventDefault();
		makeTabs($(this),"#specification-tabs");
	});
	
	//show selected tabs
	$($("#showroom-tabs a.selected").attr("href")).show();
	$($("#specification-tabs a.selected").attr("href")).show();
});
