// External JavaScript Resource
// Last modified 16OCT09 by Julian Madle

function popUp(NextPage,width,height,winName) {
	window.open(NextPage,(winName ? winName : "PopUp"),'toolbar=no,width=' + width + ',height=' + height + ',directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
	}
	
function openWin(loc) {
	window.open(loc,'HangingGuide','width=800,height=600,scrollbars=yes,resizeable=yes')
	}

function emailCheck(emailStr) {
	emailStr = emailStr.toLowerCase(); // Validate email address
	var checkTLD=1; // Verify that the address ends in a two-letter country or well-known TLD.  1 means check it, 0 means don't.
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|co|uk)$/; // Known TLDs that an e-mail address must end with.
	var emailPat=/^(.+)@(.+)$/; // Pattern to check if the entered e-mail address fits the user@domain format, also used to separate the username from the domain.
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; // Pattern for matching all special characters. Do not allow special characters in address. These include ( ) < > @ , ; : \ " . [ ]
	var validChars="\[^\\s" + specialChars + "\]"; //Range of characters allowed in username or domainname (really states which chars aren't allowed).
	var quotedUser="(\"[^\"]*\")"; // Pattern applies if "user" is a quoted string (in which case, there are no rules about which characters are allowed and which aren't).  E.g. "jiminy cricket"@disney.com is a legal e-mail address.
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; // Pattern applies for domains that are IP addresses, rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal e-mail address (square brackets are required).
	var atom=validChars + '+'; // Represents an atom (basically a series of non-special characters).
	var word="(" + atom + "|" + quotedUser + ")"; // Represents one word in the typical username. Eg. in john.doe@somewhere.com, john and doe are words. Basically, a word is either an atom or quoted string.
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); // Pattern describes the structure of the user
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); // Pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat, shown above.

	// Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze.
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		// Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address.
		alert("The Email address you entered seems incorrect (check @ and .'s)");
		return false;
		}
	var user=matchArray[1];
	var domain=matchArray[2];
	// Check that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {if (user.charCodeAt(i)>127) {alert("The username you entered contains invalid characters. Please try again.");return false}}
	for (i=0; i<domain.length; i++) {if (domain.charCodeAt(i)>127) {alert("The domain name you entered contains invalid characters. Please try again.");return false}}
	// See if "user" is valid
	if (user.match(userPat)==null) {alert("The username you entered doesn't seem to be valid.");return false}
	// if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid.
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {if (IPArray[i]>255) {alert("Destination IP address is invalid!");return false}}
		return true;
		}
	// Domain is symbolic name.  Check if it's valid.
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {if (domArr[i].search(atomPat)==-1) {alert("The domain name does not seem to be valid.");return false}}
	// Make sure domain name ends in a known top-level domain or a two-letter word representing country (uk, nl)
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {alert("The email address you enter must end in a well-known domain or two letter " + "country.");return false}
	// Make sure there's a host name preceding the domain.
	if (len<2) {alert("This address is missing a hostname!");return false}
	// Email address is valid!
	return true;
	}

function checkMiniSearch(FormRef) {
	if ((FormRef.Colour.selectedIndex==0) && (FormRef.Design.selectedIndex==0) && (FormRef.Type.selectedIndex==0) && (FormRef.Price.selectedIndex==0) && (FormRef.Pattern.selectedIndex==0)) {alert("Please select at least one search parameter."); return false}
	}

// ANIMATED PORTFOLIO SCRIPT

var isIE=(navigator.appName=="Microsoft Internet Explorer");
var portfolioSpeed = isIE ? 150 : 100;
var globalCount = 0;
var looper;
var imgArray;

function preloadImagesForAnim() {
	imgArray=new Array();
	for (var i=0; i<imageNumbers.length; i++) {
		imgArray[i]=new Image();
		imgArray[i].src=srcPath + (imageNumbers[i]);
		imageNumbers[i]=i + 1;
		}
	}

function playPortfolioAnim() {if (document.getElementById) {globalCount=1;looper=window.setInterval("showPortfolioImage()",portfolioSpeed)}}

function showPortfolioImage() {
	var intImageID=Math.round(Math.random() * imageNumbers.length);
	if (document.getElementById("Panel_" + imageNumbers[intImageID])) {
		var elemRotator=document.getElementById("Panel_" + imageNumbers[intImageID]);
		if (isIE) {elemRotator.filters.blendTrans.Apply()}
		elemRotator.src = imgArray[imageNumbers[intImageID] - 1].src;
		if (isIE) {elemRotator.filters.blendTrans.play()}
		}
	imageNumbers.splice(intImageID,1);
	if (imageNumbers.length==0) {window.clearInterval(looper)}
	}

function panelEnlarge(ImgRef,State,OverlayID) {
	if (State) {ImgRef.src=ImgRef.src.replace("/homegrid/","/homegrid_large/"); if (OverlayID!="") {document.getElementById(OverlayID).style.display="none"}}
	else {ImgRef.src=ImgRef.src.replace("/homegrid_large/","/homegrid/"); if (OverlayID!="") {document.getElementById(OverlayID).style.display=""}}
	}

//iphone etc detection code; coded by Jim Hare

var iosAlertMessage = 'Hi, we see you\'re on an iOS device. \nWould you prefer to use our dedicated App for iPhone and iPad, for the fastest and best experience?';
// Reminders. Specify null for infinity.
var iosAlertDaysRemindIfDeclined = 60;
var iosAlertDaysRemindIfAccepted = 180;

function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			break;
		}
	}
	
	if(unescape(y).length) 
	{
		return unescape(y);
	} else
	{
		return false;
	}
}


if (!getCookie('iosAlerted') && navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad)/))
{
	if(confirm(iosAlertMessage))
	{
		setCookie('iosAlerted','declined',iosAlertDaysRemindIfDeclined);
		setTimeout(function(){window.location = 'http://itunes.apple.com/gb/app/tektura/id456355644?mt=8';},200);
	} else
	{
		setCookie('iosAlerted','accepted',iosAlertDaysRemindIfAccepted);
	}
}


