function trim(s) {
	return s.replace(/^\s+|\s+$/g,"");
}

function getEstoreApplicationContextRoot() {
	return '/';
}

$.tools.validator.fn("[type=phone]", function(input, value) {
	var re = /^(\d{10}|(\d{3}-\d{3}-\d{4})|(\d{1}-\d{3}-\d{3}-\d{4}))$/; 
	return !value || re.test(value);
});

$.tools.validator.fn("[data-equals]", function(input) {
	var name = input.attr("data-equals"),
		 field = this.getInputs().filter("[id=" + name + "]"); 
	return input.val() == field.val() ? true : false; 
});

//adds an effect called "wall" to the validator
$.tools.validator.addEffect("wall", function(errors, event) {
	var wall = $(this.getConf().container).fadeIn();
	wall.find("li").remove();
	$.each(errors, function(index, error) {
		wall.append(
			"<li>" +error.messages[0]+ "</li>"
		);		
	});	
}, function(inputs){
	
});

function changeToImageUnavailable(img,root) {
	img.onerror=null;
	img.src=root + '/images/imgunavl.gif';
}

function changeToProductPreview(img, prdImg, root) {
	img.onerror = function() {
		img.src=root + '/images/imgunavl.gif';
		img.width=100;
	};
	img.src=prdImg;
}

function pageSelected(pageNumber)
{
	refreshPagingUrl(pageNumber);
}

function refreshPagingUrl(pageNumber)
{
	var url = new String(window.location);
	
	var pageSize = $('select#pageSizeDropDown').val();
	var sortAttribute = $('select#sortDropDown').val();
	
	var pageNoText = 'pageNo=' + pageNumber;
	var pageSizeText = 'pageSize=' + pageSize;
	var sortAttribute = 'sortAttribute=' + sortAttribute;
	
	var flag = url.search(/pageNo/);
	if(flag!=-1)
	{
		url = url.replace(/pageNo=([0-9])*/,pageNoText);
	}else
	{
		url = url + "&pageNo="+pageNumber;
	}
	flag = url.search(/pageSize/);
	if(flag!=-1)
	{
		url = url.replace(/pageSize=([0-9])*/,pageSizeText);
	}else
	{
		url = url + "&pageSize="+pageSize;
	}
	flag = url.search(/sortAttribute/);
	if(flag!=-1)
	{
		url = url.replace(/sortAttribute=(.)*/,sortAttribute);
	}else
	{
		url = url + "&sortAttribute="+sortAttribute;
	}
	var viewType = getViewType(window.location.href);
	if ( viewType != null && viewType != "" ) {
		if ( url.indexOf("viewType") > -1 ) {
			url = url.replace(/viewType=(.)*/, viewType);
		} else {
			url += ("&viewType="+viewType);
		}
	}
	window.location = url;
}

function pageSizeChanged()
{
	refreshPagingUrl(1);
}

function sortChanged()
{
	refreshPagingUrl(1);
}

function changeViewType(viewType) {
	
	var pageNum = getParameterValue(window.location.href, "pageNo");
	if ( pageNum == "" ) {
		pageNum = 1;
	}
	$("#viewTypes").attr("class", viewType);
	refreshPagingUrl(pageNum);
}
function getViewType(url) {
	
	var viewType = $("#viewTypes").attr("class");
	if ( viewType == null || viewType.length == 0 ) {
		viewType = getParameterValue(url, "viewType");
	}
	return viewType;
}

function getParameterValue(url, name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( url );
  if( results == null )
    return "";

  return results[1];
}

//Quick View
this.imagePreview = function(){	
	
	xOffset = 10;
	yOffset = 30;
	
	var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
			
	if (jQuery.browser.msie) {
		if(parseInt(jQuery.browser.version) == 7) {
			viewportWidth -= 3;
		}
	}
	
	$("a.preview").hover(function(e){
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? "<br/>" + this.t : "";
			var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
			var offsetX = $(this).offset().left + $(this).width() + 400;
		
			$("body").append("<p id='preview'><img src='"+ this.name +"' alt='Image preview' />"+ c +"</p>");
			$("#preview").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px").fadeIn("fast");
			
			if(offsetX > viewportWidth) {
				//ie6offset = $.browser.msie && $.browser.version.substr(0,1)<7 ? 30 : 0;
				$("#preview").css("left", (e.pageX - 450) + "px");
			}
		},
		function(){
			this.title = this.t;	
			$("#preview").remove();
		}
	);	
	$("a.preview").mousemove(function(e){
		var offsetX = $(this).offset().left + $(this).width() + 400;
		var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
		$("#preview").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
		
		if(offsetX > viewportWidth) {
			$("#preview").css("left", (e.pageX - 450) + "px");
		}
	});
};

function siteSearchClicked() {
	
	var defaultTxt = "Enter Keyword or Item #";
	var srchValue = $("input#searchBox").val();
	srchValue = trim(srchValue);
	if ( srchValue.length == 0 || srchValue == defaultTxt ) {
		if ( srchValue.length == 0 ) {
			$("input#searchBox").val(defaultTxt);
		}
		return false;
	}
	return true;
}

function siteSearchClickedOnError(){
	var defaultTxt = "Enter Keyword or Item #";
	var srchValue = $("input#searchBox1").val();
	srchValue = trim(srchValue);
	if ( srchValue.length == 0 || srchValue == defaultTxt ) {
		if ( srchValue.length == 0 ) {
			$("input#searchBox1").val(defaultTxt);
		}
		return false;
	}
	return true;
}

$(document).ready(function(){

	imagePreview();

	$('.toolTip').hover(
			function() {
			this.tip = this.title;
			$(this).append(
				'<div class="toolTipWrapper">'
					+'<div class="toolTipTop"></div>'
					+'<div class="toolTipMid">'
						+this.tip
					+'</div>'
					+'<div class="toolTipBtm"></div>'
				+'</div>'
			);
			this.title = "";
			this.width = $(this).width();
			$(this).find('.toolTipWrapper').css({left:this.width-22});
			$('.toolTipWrapper').fadeIn(300);
		},
		function() {
			$('.toolTipWrapper').fadeOut(100);
			$(this).children().remove();
			this.title = this.tip;
		}
	);

	$("#siteSearchForm").submit(function() {
		return siteSearchClicked();
	});
	
	$("#siteSearchForm1").submit(function() {
		return siteSearchClickedOnError();
	});
	
	
	
});

function getUrlForKeyCodeRedirection(templateUrl,navcount,productId,skuId,secure)
{
	var protocol;
	if(secure=='true'){
		protocol = "https:";
	}else{
		protocol = window.location.protocol; 
	}
	var url =  protocol + '//'+ window.location.host  + templateUrl + '?navAction=jump'
	url = url + '&navCount=' + navcount +
	      '&productId=' + productId +
		  '&skuId=' + skuId;
	  return url;
}

function getUrlForPOCRedirection(url)
{
	var url = window.location.protocol + '//'+ window.location.host  + url;
    return url;
}

function checkForRedirect(code)
{
	if(code == null || code == '')
		return false;
	if(code.length == 8)
	{
		var str = code.substring(4,7);
		var url = 'http://www.orderpens.com/' + str + '/MMILogin.asp?PctNo='+code+'&utm_source=internal&utm_medium=popup&utm_content='+code+'&utm_campaign=estorecart';
		window.open(url);
		return true;
	}
	
	return false;
}

function generateUrlToDoBackGroundDesignSearch(searchText)
{
	var	url = getEstoreApplicationContextRoot() + 'site/search/_backGroundDesignSearch.jsp';
	url = url + '?searchText=' + searchText;
	return url;
}

