// JavaScript Document

var addthis_config = {
	ui_offset_left: -118
	}

function runGlobalSearch(){
	
	var criteria = document.searchForm.searchbox.value;
	var trimCriteria = criteria.replace(/^\s+|\s+$/g, '');
	if((trimCriteria == "")||(trimCriteria == "Search product name, keyword or code..."))
	{
		alert("Please enter a product name, keyword or code to search");
		return false;
	}
	else
	{
		return true;
	}
}

function runEmailSignUp(){
	var missing = "";
	var emailAddress = document.emailSignUp.email.value;
	var firstName = document.emailSignUp.firstname.value;
	var lastName = document.emailSignUp.lastname.value;
	var trimEmailAddress = emailAddress.replace(/^\s+|\s+$/g, '');
	var trimfirstName = firstName.replace(/^\s+|\s+$/g, '');
	var trimlastName = lastName.replace(/^\s+|\s+$/g, '');
	
	if((trimfirstName == "")||(trimfirstName == "First Name")) { missing += "\nFirst Name "; }
	if((trimlastName == "")||(trimlastName == "Last Name")) { missing += "\nLast Name "; }
	if((trimEmailAddress == "")||(trimEmailAddress == "Email Address")) { missing += "\nEmail Address"; }
	var message = "Please fill in the following to sign up :\n" + missing;
	if(missing != "") { alert(message); return false; }
	else if(eMailcheck(trimEmailAddress) == "invalid") { alert("Please enter a valid email address"); return false; }
	else { document.emailSignUp.emailSubmit.disabled = true; return true; }
}


function eMailcheck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1) { return "invalid"; }
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) { return "invalid"; }
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) { return "invalid"; }
		if (str.indexOf(at,(lat+1))!=-1) { return "invalid"; }
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { return "invalid"; }
		if (str.indexOf(dot,(lat+2))==-1) { return "invalid"; }		
		if (str.indexOf(" ")!=-1) { return "invalid"; }
 		return true;				
}

var RecentlyViewedItems = {
	
	items : [],
	
	cookieValue : "",
	
	readCookie : function(qty)	{
		var cookieValue = "";
		var recentItems = [];
		var ck = document.cookie;
		
		if(ck.indexOf("SCRITEMS") != -1)	{
			ck = ck.split("SCRITEMS")[1].substring(1);
			var items = ck.split("SCRECENTLY");
			for(var a = 1; a <= qty && a < items.length; a++)	{
				var atts = items[a].split("/////");
				var newItem = { id : unescape(atts[0]), name : unescape(atts[1]), url : unescape(atts[2]), thumbnail : unescape(atts[3]), price : unescape(atts[4]) };
				recentItems.push(newItem);
				
				cookieValue += "SCRECENTLY" + atts[0] + "/////" + atts[1] + "/////" + atts[2] + "/////" + atts[3] + "/////" + atts[4];
			}
		}
		else	{
			return;
		}
		
		this.items = recentItems;
		this.cookieValue = cookieValue;
	},
	
	Create : function(id, name, url, thumbnail, price)	{
		
		var its = this.items;
		var i = its.length;
		while(i--)	{
			if(id == its[i].id)	{
				return false;
			}
		}
		
		this.readCookie(5);
		document.cookie = "SCRITEMS=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
		
		var newItem = { id : id, name : name, url : url, thumbnail : thumbnail, price : price };
		this.items.unshift(newItem);
		
		var expires = new Date();
		expires.setTime( expires.getTime() + (60*24*60*60*1000) );
		expires = expires.toGMTString();
		var c = "SCRITEMS=SCRECENTLY" + escape(id) + "/////" + escape(name) + "/////" + escape(url) + "/////" + escape(thumbnail) + "/////" + escape(price) + this.cookieValue + "SCRITEMS" + ";path=/;expires=" + expires;
		document.cookie = c;
	},
	
	Read : function(target, template)	{
		target = document.getElementById(target);
		this.readCookie(6);
		
		var its = this.items;
		var j = its.length;
		if(j == 0)	{
			target.style.display = "none";
			return;
		}
		
		for(var a = 0; a < j; a++)	{
			var itemHTML = template;
			itemHTML = itemHTML.replace(/<internalid>/gi, its[a].id);
			itemHTML = itemHTML.replace(/<storedisplayname>/gi, its[a].name);
			itemHTML = itemHTML.replace(/<storeurl>/gi, its[a].url);
			itemHTML = itemHTML.replace(/<storedisplaythumbnail>/gi, its[a].thumbnail);
			itemHTML = itemHTML.replace(/<salesprice>/gi, its[a].price);
			target.innerHTML += itemHTML;
			document.getElementById('recentlyViewedItems').style.display = "block";
		}
	}
}