﻿//=====================================================================
//  Altfield Client Side Scripts
//=====================================================================

var Categories = null;
var NumberOfCategories = 0;
var CategoriesPerPage = 6;

var Products = null;
var NumberOfProducts = 0;
var ProductsPerPage = 4;

var OnPage = 0;

//=====================================================================
//  All BUT links from the homepage and other pages links to the 
//  homepage use javascript links to focus page rank on the homepage
//=====================================================================

function ShowPage(pageName)
{
	switch(pageName) 
	{
		case "about" :
			document.location.href = 'about.aspx';
			break;
		case "locations" :
			document.location.href = 'locations.aspx';
			break;
		case "bespoke" :
			document.location.href = 'bespoke.aspx';
			break;
		case "news" :
			document.location.href = 'news.aspx';
			break;
		case "contact" :
			document.location.href = 'contact.aspx';
			break;
		case "furniture" :
			document.location.href = 'group.aspx?GroupId=100';
			break;
		case "artwork" :
			document.location.href = 'group.aspx?GroupId=200';
			break;
		case "lighting" :
			document.location.href = 'group.aspx?GroupId=300';
			break;
		case "altfieldcollection" :
			document.location.href = 'group.aspx?GroupId=400';
			break;
		case "mayaromanoff" :
			document.location.href = 'product.aspx?CategoryId=150';
			break;
		case "innovations" :
			document.location.href = 'product.aspx?CategoryId=155';
			break;
		case "brentanofabrics" :
			document.location.href = 'product.aspx?CategoryId=160';
			break;
		case "chellatextiles" :
			document.location.href = 'product.aspx?CategoryId=165';
			break;
		case "westburytextiles" :
			document.location.href = 'product.aspx?CategoryId=170';
			break;
		case "decorativeaccessories" :
			document.location.href = 'group.aspx?GroupId=900';
			break;
		case "elizabethdow" :
			document.location.href = 'product.aspx?CategoryId=175';
			break;
		case "mooregiles" :
			document.location.href = 'product.aspx?CategoryId=180';
			break;

	}
}

//=====================================================================
//  Initialise the array of displayable categories on group.aspx
//=====================================================================

function InitialiseCategories()
{
	Categories = new Array();
	NumberOfCategories = 0;
	OnPage = 0;
}

//=====================================================================
//  Add a category object to the category array
//=====================================================================

function AddCategory(groupId, Id, Name, imgURI, linkText, imgALT, linkURI, statusText, Description)
{
	Categories[NumberOfCategories++] = new Category(groupId, Id, Name, imgURI, linkText, imgALT, linkURI, statusText, Description.value);
}

//=====================================================================
//  Constructor to make a category
//=====================================================================

function Category(groupId, Id, Name, imgURI, linkText, imgALT, linkURI, statusText, Description)
{
	this.GroupId = groupId;
	this.Name = Name;
	this.ImgURI = imgURI;
	this.LinkText = linkText;
	this.ImgALT = imgALT;
	this.LinkURI = linkURI;
	this.StatusText = statusText;
	this.Description = Description;
}

//=====================================================================
//  Show the current page of categories in group.aspx
//=====================================================================

function ShowCategoryPage(pageNum)
{
	var i = 0;
	var pageStart = pageNum * CategoriesPerPage;
	var pageEnd = pageStart + CategoriesPerPage;
	
	ShowPreviousAndNext(pageNum);
	
	for(i = pageStart ; i < pageEnd ; i++)
	{
		if(i < NumberOfCategories)
		{
			var imageDiv = document.getElementById("idiv" + (i - pageStart));
			imageDiv.firstChild.innerHTML = "<a href='" + Categories[i].LinkURI + "' title='" + Categories[i].Name + "' " + 
									        "onmouseover='window.status=\"" + Categories[i].StatusText + "\";return true;' " +
							                "onmouseout=\"window.status='';return true;\">" +
											"<img src='" + Categories[i].ImgURI + "' alt='" + Categories[i].ImgALT + "' border='0' height='100' align='top'/></a>" ;

			var textDiv = document.getElementById("tdiv" + (i - pageStart));
			textDiv.firstChild.innerHTML = "<a href='" + Categories[i].LinkURI + "' title='" + Categories[i].Name + "' " +
									       "onmouseover='window.status=\"" + Categories[i].StatusText + "\";return true;' " +
							               "onmouseout=\"window.status='';return true;\"><strong><em>" + Categories[i].LinkText + "</em></strong></a>";

		}
		else
		{
			var imageDiv = document.getElementById("idiv" + (i - pageStart));
			imageDiv.firstChild.innerHTML = "<img src='./images/1x1.gif' alt='' border='0'  height='100' align='top'/>";
			var textDiv = document.getElementById("tdiv" + (i - pageStart));
			textDiv.firstChild.innerHTML = "";
		}
	}	
}

//=====================================================================
//  Control the visibility and greyness of the previous and next
//  buttons on group.aspx
//=====================================================================

function ShowPreviousAndNext(pageNum)
{
	var imgPrev = document.getElementById("prev");
	var imgNext = document.getElementById("next");
	var	maxPage = Math.floor((NumberOfCategories - 1) / CategoriesPerPage);
	
	if(pageNum == 0)
		if(pageNum < maxPage)
			imgPrev.src = "./images/leftgrey.jpg";
		else
			imgPrev.src = "./images/1x1.gif";
	else
		imgPrev.src = "./images/left.jpg";
		
	if(pageNum >= maxPage)
		if(pageNum != 0)
			imgNext.src = "./images/rightgrey.jpg";
		else
			imgNext.src = "./images/1x1.gif";
	else
		imgNext.src = "./images/right.jpg";
		
	imgPrev.title = (pageNum == 0) ? "" :  "previous page";
	imgNext.title = (pageNum >= maxPage) ? "" : "next page";

	imgPrev.style.cursor = (pageNum == 0) ? "" : "hand";
	imgNext.style.cursor = (pageNum >= maxPage) ? "" : "hand";
}
	
//=====================================================================
//  Go to the previous page of categories
//=====================================================================

function PreviousPage()
{
	if(OnPage > 0) ShowCategoryPage(--OnPage);
}

//=====================================================================
//  Go to the next page of categories
//=====================================================================

function NextPage()
{
	if(OnPage < Math.floor((NumberOfCategories - 1) / CategoriesPerPage)) ShowCategoryPage(++OnPage);
}

//=====================================================================
//  Initialise the array of displayable products on product.aspx
//=====================================================================

function InitialiseProducts()
{
	Products = new Array();
	NumberOfProducts = 0;
	OnPage = 0;
}

//=====================================================================
//  Add a product object to the product array
//=====================================================================

function AddProduct(categoryId, Id, Name, thumbImgURI, thumbImgALT, imgURI, imgALT, shortDescription, Description, logoURI)
{
	Products[NumberOfProducts++] = new Product(categoryId, Id, Name, thumbImgURI, thumbImgALT, imgURI, imgALT, shortDescription.value, Description.value, logoURI);
}

//=====================================================================
//  Constructor to make a product
//=====================================================================

function Product(categoryId, Id, Name, thumbImgURI, thumbImgALT, imgURI, imgALT, shortDescription, Description, logoURI)
{
	this.CategoryId = categoryId;
	this.Name = Name;
	this.ThumbImgURI = thumbImgURI;
	this.ThumbImgALT = thumbImgALT;
	this.ImgURI = imgURI;
	this.ImgALT = imgALT;
	this.ShortDescription = shortDescription;
	this.Description = Description;
	this.LogoURI = logoURI
}

//=====================================================================
//  Show the current page of products in product.aspx
//=====================================================================

function ShowProductPage(pageNum)
{
	var i = 0;
	var pageStart = pageNum * ProductsPerPage;
	var pageEnd = pageStart + ProductsPerPage;
	
	ShowPreviousAndNextProducts(pageNum);
	
	for(i = pageStart ; i < pageEnd ; i++)
	{
		var image = document.getElementById("prod" + (i - pageStart));
		if(i < NumberOfProducts)
		{
			image.src = Products[i].ThumbImgURI;
			image.alt = Products[i].ThumbImgALT;
			image.style.cursor = "hand";			
		}
		else
		{
			image.src = "./images/1x1.gif";
			image.alt = "";
			image.style.cursor = "";			
		}
	}	
}

//=====================================================================
//  Show the currently selected product
//=====================================================================

function SelectProduct(offset)
{
	var onProduct = OnPage * ProductsPerPage + offset;
	
	if(onProduct < NumberOfProducts)
	{
		var image = document.getElementById("mainimage");
		image.src = Products[onProduct].ImgURI;
		image.alt = Products[onProduct].ImgALT;
		var descDiv = document.getElementById("proddesc");
		descDiv.innerHTML = Products[onProduct].Description;
		
		if(Products[onProduct].LogoURI != "")
			document.getElementById('logotd').innerHTML = Products[onProduct].LogoURI;
		else
			document.getElementById('logotd').innerHTML = "<img src='images/1x1.gif' alt='' />";
		
		
	//	var nameDiv = document.getElementById("prodname");
	//	nameDiv.firstChild.innerHTML = Products[onProduct].Name;

	}
		
}
//=====================================================================
//  Control the visibility and greyness of the previous and next
//  buttons on product.aspx
//=====================================================================

function ShowPreviousAndNextProducts(pageNum)
{
	var imgPrev = document.getElementById("prev");
	var imgNext = document.getElementById("next");
	var	maxPage = Math.floor((NumberOfProducts - 1) / ProductsPerPage);
	
	if(pageNum == 0)
		imgPrev.src = "./images/1x1.gif";
	else
		imgPrev.src = "./images/bigleft.jpg";
		
	if(pageNum >= maxPage)
		imgNext.src = "./images/1x1.gif";
	else
		imgNext.src = "./images/bigright.jpg";
		
	imgPrev.title = (pageNum == 0) ? "" :  "previous products";
	imgNext.title = (pageNum >= maxPage) ? "" : "next products";

	imgPrev.style.cursor = (pageNum == 0) ? "" : "hand";
	imgNext.style.cursor = (pageNum >= maxPage) ? "" : "hand";
}
	
//=====================================================================
//  Go to the previous page of products
//=====================================================================

function PreviousProductPage()
{
	if(OnPage > 0) ShowProductPage(--OnPage);
}

//=====================================================================
//  Go to the next page of products
//=====================================================================

function NextProductPage()
{
	if(OnPage < Math.floor((NumberOfProducts - 1) / ProductsPerPage)) ShowProductPage(++OnPage);
}

//=====================================================================
//  Submit the contact us orm for processing
//=====================================================================

function SubmitContactForm()
{
	var form = document.getElementById("altfieldmainform");
	if(form.Name.value == "")
	{
		alert("Please enter your name");
		form.Name.focus();
		return;
	}
	if(form.Company.value == "")
	{
		alert("Please enter your company");
		form.Company.focus();
		return;
	}
	if(form.Email.value == "")
	{
		alert("Please enter a contact email address");
		form.Email.focus();
		return;
	}
	if(!CheckEmail(form.Email.value))
	{
		alert("Please enter a valid email address");
		form.Email.focus();
		return;
	}
	if(form.Email.value == "")
	{
		alert("Please enter a contact email address");
		form.Email.focus();
		return;
	}
	if(form.Message.value == "")
	{
		alert("Please enter a message");
		form.Message.focus();
		return;
	}
	
	form.submit();
}

function CheckEmail(e) 
{
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(var i = 0; i < e.length ; i++)
	{
		if(ok.indexOf(e.charAt(i)) == -1)  
			return false;
	}

	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
  	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
  	return (!e.match(re) && e.match(re_two));		
}


