﻿var domain = 'equipment.golf.com';
var path = '';
var $j = jQuery;
jQuery(document).ready(function() {
	$j('input.nav-search-btn').click(function(){
		window.location="http://search.golf.com/site/results.html?Ntt=" + $j('.search-text').val();
		return false;
	});
	$j('.hover').hover(
		function() {
			$j(this).attr('src', $j(this).attr('src').replace('.jpg', '_hover.jpg').replace('.gif', '_hover.gif'));
		},
		function() {
			$j(this).attr('src', $j(this).attr('src').replace('_hover.jpg', '.jpg').replace('_hover.gif', '.gif'));
		}
	);
	$j('#Searchtb').focus(function(){
		if ($j(this).val()=="Search the Equipment Finder.")
		{
			$j(this).val('').css('color', '#000');
		}
	}).blur(function(){
		if ($j(this).val()=='')
		{
			$j(this).val("Search the Equipment Finder.").css('color', '#666');
		}
	}).keydown(function(e) {
		if(e.keyCode == 13) {
			doSearch();
			return false;
		}
	});
	$j('.searchSubmit').click(doSearch);
	
	$j('#Next').click(function(){MoveCompare(true);});
	$j('#Previous').click(function(){MoveCompare(false);});
	comCnt = $j('#CItems tr:eq(0) td').length;
	
	$j('.compCheck').change(addComp);
	
	$j('.thumb img').click(function(){
		$j('.prodImg img').attr('src', $j(this).attr('src').replace('/thumb/', '/large/'));
	});
});

function doSearch()
{
	window.location=path + "/search.aspx?term=" + $j('#Searchtb').val();
}
function HideColumn(table, index)
{
	$j(table + ' tr').each(function(){
		$j(this).children("td:eq("+index+")").hide();
	});
}
function ShowColumn(table, index)
{
	$j(table + ' tr').each(function(){
		$j(this).children("td:eq("+index+")").show();
	});
}
var comCol = 4;
var comCnt;
function MoveCompare(right)
{
	if (right)
	{
		if (comCol < comCnt)
		{
			HideColumn('#CItems', comCol-4);
			ShowColumn('#CItems', comCol);
			comCol++;
			$j('#Previous').show();
		}
		if (comCol >= comCnt)
		{
			$j('#Next').hide();
		}
	}
	else
	{
		if (comCol > 4)
		{
			comCol--;
			ShowColumn('#CItems', comCol-4);
			HideColumn('#CItems', comCol);
			$j('#Next').show();
		}
		if (comCol <= 4)
		{
			$j('#Previous').hide();
		}
	}
}

var list = "";
function addComp()
{
	values = this.id.split("-");
	if (this.checked)
	{
		eraseCookie("compareLast");
		createCookie("compareLast", values[1], 1);
		list = readCookie("compare-"+values[1]);
		if (list==null) list="";
		list = list + "," + values[0];
		eraseCookie("compare-"+values[1]);
		createCookie("compare-"+values[1], list, 1);
	}
	else
	{
		list = readCookie("compare-"+values[1]);
		if (list!=null)
		{
			prods = list.split(',')
			list = "";
			for (var x=0; x<prods.length; x++)
			{
				if (prods[x]!=values[0])
				{
					list = list + "," + prods[x];
				}
			}
			list = list.replace(',,', ',');
			if (list==',') list=null;
			eraseCookie("compare-"+values[1]);
			createCookie("compare-"+values[1], list, 1);
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; domain="+domain+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*Ratings&Review Code*/
jQuery(document).ready(function() {
	$j('#SubmitReview').click(SubmitReview);
	$j('#PreviewReview').click(PreviewReview);
	$j('#EditReview').click(function(){$j('#ReviewPreview').hide(); $j('#ReviewForm').show();});
	setReviewActions();
});
function setReviewActions()
{
	$j('.helpful').click(helpful);
	$j('.nothelpful').click(helpful);
	$j('.showAll').click(showAll);
	$j('.report').click(report);
}
function PreviewReview()
{
	if (ValidateReview())
	{
	
		$j.post('http://' + domain + path + '/callback/review.ashx',
				{action:"clean", "content":$j('#Review').val()}, function(data){
			//$j('#ReviewForm').hide();
			$j('#ReviewPreview .ratingbar div').css('width', $j('input[name=star1]:checked').val()*20 + '%');
			$j('#ReviewPreview .title').html($j('#Title').val());
			$j('#ReviewPreview .author em').html($j('#Name').val());
			var currentTime = new Date();
			var month = currentTime.getMonth() + 1;
			var day = currentTime.getDate();
			var year = currentTime.getFullYear();
			$j('#ReviewPreview .author span').html(month + '/' + day + '/' + year);
			$j('#ReviewPreview p').html(data.replace(/[\r\n]/g, '<br />'));
			$j('#ReviewConfirm span').html($j('#Email').val());
			$j('#ReviewPreview').show();
			$j('#ReviewForm').hide();
			$j('#h3AddReview').html('Preview Your Review');
		});
	}
	else
	{
		$j('#ReviewPreview').hide();
		$j('#ReviewForm').show();
		$j('#h3AddReview').html('Add Your Review');
	}
}
function SubmitReview()
{
	if (ValidateReview())
	{
		var rating = $j('input[name=star1]:checked').val();
		var title = $j('#Title').val();
		var name = $j('#Name').val();
		var email = $j('#Email').val();
		var review = $j('#Review').val();
		var productid = $j('#ProductID').val();
		$j.post('http://' + domain + path + '/callback/review.ashx',
				{action:"new", "rating":rating, "title":title,"name":name,"email":email,"review":review,"productid":productid},
				function(data){
					if (data=='')
					{
						$j('#ReviewConfirm').show();
						$j('#ReviewPreview').hide();
						$j('#ReviewForm').hide();
						$j('#h3AddReview').html('Confirm Your Email');
						$j('#AReview').hide();
					}
					else
					{
						if (data == 'duplicate')
						{
							$j('#ReviewConfirm').show().html("You have already reviewed this product.  "+$j('#ReviewConfirm').html());
							$j('#ReviewPreview').hide();
							$j('#ReviewForm').hide();
							$j('#h3AddReview').html('Confirm Your Email');
							$j('#AReview').hide();
						}
						else
						{
							alert(data);
						}
					}
				});
	}
}
function ValidateReview()
{
	var rating = $j('input[name=star1]:checked').val();
	$j('#Title').val(removeUrls(stripHTML($j('#Title').val())));
	var title = $j('#Title').val();
	$j('#Name').val(removeUrls(stripHTML($j('#Name').val())));
	var name = $j('#Name').val();
	var email = $j('#Email').val();
	$j('#Review').val(removeUrls(stripHTML($j('#Review').val())));
	var review = $j('#Review').val();
	var productid = $j('#ProductID').val();
	var valid = true;
	if (rating == null)
	{
		$j('#RatingError').css('visibility', 'visible');
		valid = false;
	}
	else
	{
		$j('#RatingError').css('visibility', 'hidden');
	}
	if (title == '')
	{
		$j('#TitleError').css('visibility', 'visible');
		valid = false;
	}
	else
	{
		$j('#TitleError').css('visibility', 'hidden');
	}
	if (name == '')
	{
		$j('#NameError').css('visibility', 'visible');
		valid = false;
	}
	else
	{
		$j('#NameError').css('visibility', 'hidden');
	}
	if (email == '' || !validateEmail(email))
	{
		$j('#EmailError').css('visibility', 'visible');
		valid = false;
	}
	else
	{
		$j('#EmailError').css('visibility', 'hidden');
	}
	if (review == '')
	{
		$j('#ReviewError').css('visibility', 'visible');
		valid = false;
	}
	else
	{
		$j('#ReviewError').css('visibility', 'hidden');
	}
	return valid;
}
function validateEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
}
function stripHTML(oldString) {
	return oldString.replace(/<\S[^><]*>/g, "");
}

function removeUrls(input)
{
	return input.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '')
	.replace(/([^\/])(www[\S]+(\b|$))/gim, '')
	.replace(/([^\/])([\S]+\.com(\b|$))/gim, '');
} 

function helpful()
{
	if (this.id.indexOf('Not')<0)
	{
		var h = 1;
	}
	else
	{
		var h = 0;
	}
	var ReviewID = this.id.split('-')[1];
	$j.post('http://' + domain + path + '/callback/review.ashx',
				{action:"helpful", "rating":h,"reviewid":ReviewID});
	this.parentNode.innerHTML = 'Thank You';
}

function showAll()
{
	//$j('#ReviewDisplay').css('background-color', '#CCCCCC');
	$j('#ReviewDisplay').load('http://' + domain + path + '/callback/AllReviews.aspx?ProductID='+this.id + ' #ReviewDisplay', function(){
			setReviewActions();
			//$j('#ReviewDisplay').css('background-color', '#FFFFFF');
		});
}
function reviewgetCallback(data)
{
	$j('#ReviewDisplay').html(data);
};

function report()
{
	var txt = '<h3>Report this content as inappropriate</h3>If you find this content inappropriate and think it should be removed from our site, let us know by entering your email and clicking the button below.<br /><strong>Your email:</strong><br /><span class="note">(Required for confirmation only)</span><br /><input type="text" id="ReportEmail" name="ReportEmail" value="" />';
	$j.prompt(txt,{
		callback: mycallbackform,
		buttons: { Report: this.id.split('-')[1] }
	});
}
function mycallbackform(v,m,f)
{
	if (v)
	{
		$j.post('http://' + domain + path + '/callback/review.ashx',
			{action:"report", "email":f.ReportEmail,"reviewid":v}, function(data){});
		$j('#Report-' + v).html('(Successfully Reported)').css('color', '#000000');
	}
}