﻿var intTimer;
var blnRetried=false;
var blnShowAccessories=false;
var blnShownHelp=0;
window.onscroll=CheckScrollAssist

function FSfncValidateEmailAddress(FormField,TheMessage,CheckTLD) {
	// CheckTLD is optional, it accepts values of true, false, and null.
	emailStr = FormField.value.toLowerCase()
	if (CheckTLD==null) {CheckTLD=true}
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|co|uk)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {alert(TheMessage); FormField.focus(); FormField.focus(); return false}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {if (user.charCodeAt(i)>127) {alert(TheMessage); return false}}
	for (i=0; i<domain.length; i++) {if (domain.charCodeAt(i)>127) {alert(TheMessage); FormField.focus(); return false}}
	if (user.match(userPat)==null) {alert(TheMessage); FormField.focus(); return false}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {for (var i=1;i<=4;i++) {if (IPArray[i]>255) {alert(TheMessage); FormField.focus(); return false}}; return true}
	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(TheMessage); FormField.focus(); return false}}
	if ((CheckTLD) && (domArr[domArr.length-1].length!=2) && (domArr[domArr.length-1].search(knownDomsPat)==-1)) {alert(TheMessage); FormField.focus(); return false}
	if (len<2) {alert(TheMessage); FormField.focus(); return false}
	return true;
	}

function swapPhoto(PHID,ImgWidth,ImgHeight,ImgFolder,AltText) {
	if (document.getElementById) {
		var Photo=document.getElementById("MainPhoto")
		if (Photo.filters) {Photo.filters.blendTrans.Apply()}
		Photo.src="/images/" + ImgFolder + "/fullsize/" + PHID + ".jpg";
		Photo.width=ImgWidth;
		Photo.height=ImgHeight;
		if (Photo.filters) {Photo.filters.blendTrans.play()}
		Photo.title=(AltText ? AltText : '');
		//if (document.getElementById("PhotoBrowser")) {document.getElementById("PhotoBrowser").style.width=(ImgWidth<300 ? "300px" : ImgWidth + "px")}
		if (document.getElementById("MainPhotoToggle")) {document.getElementById("MainPhotoToggle").style.display="block"}
		}
	}

function swap(place,imgName) {if (document.images) {document[place].src=eval(imgName + ".src")}	}

function setNav() {
	//trap();
	CheckScrollAssist()
	}

function right(e) {
	var msgie = "Copyright © The Kids Window\n\nIf you wish to add this page to favourites, \nplease use the 'add to favourites' option in \nthe 'Favourites' menu";
	var msgn = "Copyright © The Kids Window\n\n If you wish to bookmark this page, please use \nthe 'bookmarks' option in the 'Window' menu";
	if (navigator.appName == 'Netscape' && e.which == 3) {
		alert(msgn);
		return false;
		}
	if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
		alert(msgie);
		return false;
		}
	else return true;
	}

function trap(WithoutAssist) {
	document.onmousedown = right;
	document.onmouseup = right;
	if (!WithoutAssist) {CheckScrollAssist()} else {window.onscroll=""}
	}

function openWin(thispage) {window.open(thispage,'samples','width=500,height=350,scrollbars=yes,resizeable=no')}

function hideProduct() {
	if ((parent) && (parent.document.getElementById)) {
		document.getElementById("ContentTable").style.display="none";
		parent.document.getElementById("Product").height=0;
		}
	}

function setFrameSize() {
	if ((parent) && (parent.document.getElementById) && (parent.document.getElementById("Product"))) {
		var TargetHeight=document.getElementById('ContentTable').scrollHeight;
		parent.document.getElementById("Product").height=(TargetHeight>0 ? TargetHeight : 300);
		parent.window.scrollTo(0,0);
		parent.ShowTab('Product');
		if ((blnRetried==false) && (TargetHeight==0)) {blnRetried=true; window.setTimeout("setFrameSize()",100)}
		}
	}

function setAccessoryFrameSize() {
	if ((parent) && (parent.document.getElementById) && (parent.document.getElementById("Accessories"))) {
		var TargetHeight=document.getElementById('ContentTable').scrollHeight;
		parent.document.getElementById("Accessories").height=(TargetHeight>0 ? TargetHeight + 10 : 300);
		parent.window.scrollTo(0,0);
		parent.ShowTab('Accessories',true);
		if ((blnRetried==false) && (TargetHeight==0)) {blnRetried=true; window.setTimeout("setAccessoryFrameSize()",100)}
		}
	}

function StockObject(STID,OriginalPrice,OriginalPriceIncl,CurrentPrice,CurrentPriceIncl,FromAge,ToAge,Label,LabelUS,ClientLabel,ListItem,ClientLabel2,ListItem2,StockDays,LEID1,LEID2,DefaultLEID1,DefaultLEID2) {
	this.STID=STID;
	this.OriginalPrice=OriginalPrice;
	this.OriginalPriceIncl=OriginalPriceIncl;
	this.CurrentPrice=CurrentPrice;
	this.CurrentPriceIncl=CurrentPriceIncl;
	if (FromAge!="") {this.FromAge=parseInt(FromAge)}
	if (ToAge!="") {this.ToAge=parseInt(ToAge)}
	this.Label=Label;
	this.LabelUS=LabelUS;
	this.ClientLabel=ClientLabel;
	this.ListItem=ListItem;
	this.ClientLabel2=ClientLabel2;
	this.ListItem2=ListItem2;
	this.StockDays=StockDays;
	this.LEID1=LEID1;
	this.LEID2=LEID2;
	this.DefaultLEID1=DefaultLEID1;
	this.DefaultLEID2=DefaultLEID2;
	}

function locateStockItem(PID,STID) {
	var ProductArray=eval("ProductArray_" + PID);
	for (var i=0; i<ProductArray.length; i++) {if (STID==ProductArray[i].STID) {return ProductArray[i]}}
	}

function setupProduct(PID) {
	if (document.forms["BuyForm_" + PID]) {
		var ProductArray=eval("ProductArray_" + PID);
		if (eval("RequiresJS_" + PID)) {
			if (eval("HasSizes_" + PID)) {populateSize(PID,document.forms["BuyForm_" + PID])}
			else if (eval("HasOptions_" + PID)) {populateOption1(PID,document.forms["BuyForm_" + PID])}
			else if (eval("HasOptions2_" + PID)) {populateOption2(PID,document.forms["BuyForm_" + PID])}
			}
		else {showPrices(PID,ProductArray[0],document.forms["BuyForm_" + PID])}
		}
	}

function makeAgeDisplay(ProductItem) {
	var thisLabel,thisUnit;
	if ((Region=="US") && (ProductItem.LabelUS!="")) {thisLabel=ProductItem.LabelUS} else {thisLabel=ProductItem.Label}
	if (thisLabel!="") {return thisLabel}
	// Special cases
	if (ProductItem.FromAge<1) {
		switch (ProductItem.FromAge) {
			case -2 : return "Premature 3-5 lbs";
			case -1 : return "Small Baby 5-8 lbs";
			case 0 : return "Newborn"; break;
			}
		}
	// Set from age
	if (ProductItem.FromAge<24) {thisLabel=ProductItem.FromAge; thisUnit=" months"}
	else {thisLabel=(Math.floor(ProductItem.FromAge / 12)); thisUnit=" years"}
	// Set to age
	if ((ProductItem.ToAge<24) || (thisUnit==" months")) {if (ProductItem.ToAge>ProductItem.FromAge) {thisLabel=thisLabel + " - " + ProductItem.ToAge}}
	else {if (Math.floor(ProductItem.ToAge / 12)>thisLabel) {thisLabel=thisLabel + " - " + (Math.floor(ProductItem.ToAge / 12))}}
	return thisLabel + thisUnit;
	}

function showPrices(PID,StockItem,FormRef) {
	if ((document.getElementById) && (typeof(StockItem)!="undefined")) {
		var ThePriceField=document.getElementById("PriceField_" + PID);
		if (StockItem.CurrentPrice!=StockItem.OriginalPrice) {ThePriceField.innerHTML="<NOBR>was " + formatPrice(StockItem.OriginalPrice, StockItem.OriginalPriceIncl) + "</NOBR> <NOBR><B>now " + formatPrice(StockItem.CurrentPrice, StockItem.CurrentPriceIncl) + "</B></NOBR>"}
		else {ThePriceField.innerHTML="<B>" + formatPrice(StockItem.CurrentPrice, StockItem.CurrentPriceIncl) + "</B>"}
		if (document.getElementById("LeadTimeField_" + PID)) {
			strExtrMsg="";
			if ((StockItem.StockDays!="") && (parseInt(StockItem.StockDays)>0)) {
				StockDays=parseInt(StockItem.StockDays) + parseInt(eval("ProductDays_" + PID));
				if (parseInt(StockItem.StockDays)>10) {strExtrMsg="<DIV STYLE='color:red; font-size:11px'>Order now and we'll ship<BR>when it's back in stock</DIV"}
				}
			else {StockDays=eval("ProductDays_" + PID)}
			document.getElementById("LeadTimeField_" + PID).innerHTML=(StockDays<=1) ? "24 hours" : "Within " + StockDays + " working days" + strExtrMsg;
			FormRef.LeadTime.value=StockDays;
			}
		}
	}

function formatPrice(Price,PriceIncl) {
	switch (Region) {
		case "US" : return SymbolUS + Round(Price * RateUS, 2);
		case "ROW" : return SymbolWorld + Round(Price * RateWorld,2);
		case "Euro" : return SymbolEuro + Round(PriceIncl * RateEuro,2);
		default : return SymbolUK + Round(PriceIncl,2);
		}
	}

function Round(Number,DecimalPlaces) {
	var Rounded=(Math.round(Number * Math.pow(10,DecimalPlaces)) / Math.pow(10,DecimalPlaces)).toString();
	switch (Rounded.indexOf(".")) {
		case -1 : return Rounded + ".00";
		case Rounded.length - 2 : return Rounded + "0";
		default : return Rounded
		}
	return Rounded
	}

function changeRegion(FieldRef) {Region=FieldRef[FieldRef.selectedIndex].value; showPrices(locateStockItem(PID,FieldRef.form.STID.value),FieldRef.form)}

function populateSize(PID,FormRef) {
	var thisLabel,lastLabel;
	var ThisSelected=0;
	var ProductArray=eval("ProductArray_" + PID);
	removeOptions(FormRef.ItemSize);
	for (var i=0; i<ProductArray.length; i++) {
		thisLabel=makeAgeDisplay(ProductArray[i]);
		if (lastLabel!=thisLabel) {lastLabel=thisLabel; addOption(FormRef.ItemSize,thisLabel,ProductArray[i].STID)}
		if ((SearchSize!="") && (SearchSize>=ProductArray[i].FromAge) && (SearchSize<=ProductArray[i].ToAge)) {ThisSelected=FormRef.ItemSize.options.length - 1}
		}
	FormRef.ItemSize.selectedIndex=ThisSelected;
	sizeChanged(PID,FormRef.ItemSize);
	}

function populateOption1(PID,FormRef) {
	var thisLabel,lastLabel;
	var ProductArray=eval("ProductArray_" + PID);
	var SelectedIndex=0;
	removeOptions(FormRef.ItemOption);
	for (var i=0; i<ProductArray.length; i++) {
		if (((eval("HasSizes_" + PID)) && (makeAgeDisplay(ProductArray[i])==CurrentSize)) || (!eval("HasSizes_" + PID))) {
			thisLabel=ProductArray[i].ListItem;
			if (ProductArray[i].LEID1==ProductArray[i].DefaultLEID1) {SelectedIndex=FormRef.ItemOption.length}
			if (lastLabel!=thisLabel) {lastLabel=thisLabel; addOption(FormRef.ItemOption,thisLabel,ProductArray[i].STID)}
			}
		}
	FormRef.ItemOption.selectedIndex=SelectedIndex;
	option1Changed(PID,FormRef.ItemOption);
	}

function populateOption2(PID,FormRef) {
	var thisLabel,lastLabel;
	var ProductArray=eval("ProductArray_" + PID);
	var SelectedIndex=0;
	removeOptions(FormRef.ItemOption2);
	for (var i=0; i<ProductArray.length; i++) {
		if (((eval("HasSizes_" + PID)) && (makeAgeDisplay(ProductArray[i])==CurrentSize)) || (!eval("HasSizes_" + PID))) {
			if (((eval("HasOptions_" + PID)) && (ProductArray[i].ListItem==CurrentOption)) || (!eval("HasOptions_" + PID))) {
				thisLabel=ProductArray[i].ListItem2;
				if (ProductArray[i].LEID2==ProductArray[i].DefaultLEID2) {SelectedIndex=FormRef.ItemOption2.length}
				if (lastLabel!=thisLabel) {lastLabel=thisLabel; addOption(FormRef.ItemOption2,thisLabel,ProductArray[i].STID)}
				}
			}
		}
	FormRef.ItemOption2.selectedIndex=SelectedIndex;
	option2Changed(PID,FormRef.ItemOption2);
	}

function sizeChanged(PID,FieldRef) {
	FieldRef.form.STID.value=FieldRef[FieldRef.selectedIndex].value;
	CurrentSize=FieldRef[FieldRef.selectedIndex].text;
	if (eval("HasOptions_" + PID)) {populateOption1(PID,FieldRef.form)}
	else if (eval("HasOptions2_" + PID)) {populateOption2(PID,FieldRef.form)}
	else {showPrices(PID,locateStockItem(PID,FieldRef.form.STID.value),FieldRef.form)}
	}

function option1Changed(PID,FieldRef) {
	FieldRef.form.STID.value=FieldRef[FieldRef.selectedIndex].value;
	CurrentOption=FieldRef[FieldRef.selectedIndex].text;
	if (eval("HasOptions2_" + PID)) {populateOption2(PID,FieldRef.form)}
	else {showPrices(PID,locateStockItem(PID,FieldRef.form.STID.value),FieldRef.form)}
	}

function option2Changed(PID,FieldRef) {
	FieldRef.form.STID.value=FieldRef[FieldRef.selectedIndex].value;
	showPrices(PID,locateStockItem(PID,FieldRef.form.STID.value),FieldRef.form);
	}

function currencyConverter(Amount) {window.open("/html/General/currencyConv.asp?Amount=" + Amount,'Currency','width=500,height=450,scrollbars=auto,resizeable=no')}

function checkSearchForm() {
	if ((document.SearchForm.Age) && (document.SearchForm.Age.selectedIndex<2)) {alert("Please select an age"); document.SearchForm.Age.focus(); return false}
	if ((document.SearchForm.Gender) && (document.SearchForm.Gender.selectedIndex<2)) {alert("Please select a gender"); document.SearchForm.Gender.focus(); return false}
	document.SearchForm.submit();
	}

function checkQuickSearch(FormRef) {
	if ((FormRef.Dept.selectedIndex==0) && ((FormRef.quicksearch.value=="") || (FormRef.quicksearch.value=="Keyword"))) {alert("Please select a department or enter a keyword"); return false}
	return true;
	}

function populateItems(DeptRef) {
	removeOptions(DeptRef.form.Item,1);
	if (DeptRef.selectedIndex>0) {
		var SelectedIndex=0;	
		var objXML=XMLHTTP("GET","/Loaders/getDept.asp?XML=Y&ID=" + DeptRef[DeptRef.selectedIndex].value,"");
		if (window.ActiveXObject) {var objXMLnodes=objXML.selectNodes("//OPTION")} else {var objXMLnodes=objXML.getElementsByTagName("OPTION")}
		for (var i=0; i<objXMLnodes.length; i++) {
			if (objXMLnodes[i].childNodes[0]) {OptionText=objXMLnodes[i].childNodes[0].nodeValue} else {OptionText=""}
			addOption(DeptRef.form.Item,OptionText,objXMLnodes[i].getAttribute("VALUE"));
			if (objXMLnodes[i].getAttribute("SELECTED")=="SELECTED") {SelectedIndex=i + 1}
			}
		DeptRef.form.Item.selectedIndex=SelectedIndex;	
		}
	}

function XMLHTTP(TransferMode,URL,POSTdata) {
	if (window.ActiveXObject) {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")}
	else if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest()}
	if (xmlhttp) {
		//alert(URL);
		xmlhttp.open(TransferMode,URL,false);
		xmlhttp.send(POSTdata);
		return xmlhttp.responseXML;
		}
	}

function removeOptions(SelectRef,NewSize) {SelectRef.options.length=(NewSize ? NewSize : 0)}

function addOption(SelectRef,TheText,TheValue) {
	SelectRef.options[SelectRef.options.length] = new Option(TheText,TheValue);
	SelectRef.options[SelectRef.options.length - 1].title=TheText;
	}

function ShowTab(TabID,PreventReload) {
	window.focus();
	document.getElementById('PanelProduct').style.display='none';
	document.getElementById('PanelAccessories').style.display='none';
	document.getElementById('PanelHistory').style.display='none';
	if (document.getElementById('PanelResults')) {document.getElementById('PanelResults').style.display='none'}
	document.getElementById('Panel' + TabID).style.display='';
	document.getElementById('TabProduct').className="Tab";
	document.getElementById('TabAccessories').className="Tab";
	document.getElementById('TabHistory').className="Tab";
	if (document.getElementById('TabResults')) {document.getElementById('TabResults').className="Tab"}
	document.getElementById('Tab' + TabID).style.display='';
	document.getElementById('Tab' + TabID).className="Active";
	if (document.getElementById('HelpMsg')) {document.getElementById('HelpMsg').style.display='none'}
	if (TabID=="Product") {
		if ((document.getElementById('HelpMsg')) && (blnShownHelp<=1)) {blnShownHelp++; document.getElementById('HelpMsg').style.display=''}
		if (document.getElementById('PanelResults')) {document.getElementById('NoTab').style.width=(blnShowAccessories ? '263px' : '439px')}
		else {document.getElementById('NoTab').style.width=(blnShowAccessories ? '439px' : '615px')}
		document.getElementById('TabProduct').style.display='';
		document.getElementById('TabAccessories').style.display=(blnShowAccessories ? '' : 'none');
		document.getElementById('TabHistory').style.display='';
		}
	else if ((TabID=="Accessories") && (PreventReload!=true)) {
		var objProduct=(document.Product ? document.Product : Product);
		document.getElementById('Accessories').src=objProduct.location + "&ACC=Y";
		}
	else if (TabID=="History") {
		if ((document.getElementById('HelpMsg')) && (blnShownHelp<=1)) {blnShownHelp+=2; document.getElementById('HelpMsg').style.display=''}
		//document.getElementById('History').contentWindow.document.location.replace("ReadHistory.asp"); // <-- THIS WILL ALLOW BACK BUTTON TO RETURN VISITOR TO SEARCH PAGE!
		document.getElementById('History').src="ReadHistory.asp"
		}
	CheckScrollAssist()
	}

function setOptionalPanel(PanelID) {
	// unused - remains as reference code
	var elementHeight=document.getElementById(PanelID).scrollHeight;
	var windowHeight=(document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
	var targetHeight=(elementHeight>windowHeight - 220 ? elementHeight : windowHeight - 220);
	document.getElementById("OptionalPanel").height=targetHeight;
	document.getElementById("PanelHistory").height="1000px";
	}

function ScrollImg(Increment,IsAccessory) {
	arrPointer+=Increment;
	window.focus()
	if (arrPointer<=0) {
		arrPointer=0; 
		document.getElementById("PrevImgArrow").src="/images/Tabs/PrevArrowDisabled.gif";
		document.getElementById("PrevImgLink").style.cursor="default";
		}
	else {
		document.getElementById("PrevImgArrow").src="/images/Tabs/PrevArrow.gif";
		document.getElementById("PrevImgLink").style.cursor="pointer";
		}
	if (arrPointer>=(arrPhotos.length - 1)) {
		arrPointer=(arrPhotos.length - 1);
		document.getElementById("NextImgArrow").src="/images/Tabs/NextArrowDisabled.gif";
		document.getElementById("NextImgLink").style.cursor="default";
		}
	else {
		document.getElementById("NextImgArrow").src="/images/Tabs/NextArrow.gif";
		document.getElementById("NextImgLink").style.cursor="pointer";
		}
	swapPhoto(arrPhotos[arrPointer][1],arrPhotos[arrPointer][2],arrPhotos[arrPointer][3],arrPhotos[arrPointer][0],arrPhotos[arrPointer][4]);
	if (IsAccessory) {setAccessoryFrameSize()} else {setFrameSize()}
	}
	
function ScrollDown() {window.scrollBy(0,10); CheckScrollAssist()}

function StartScrollDown() {intTimer=window.setInterval("ScrollDown()",100)}

function StopScrollDown() {window.clearInterval(intTimer)}

function CheckScrollAssist() {
	if (document.getElementById("ScrollMarker")) {
		var ClientHeight=(document.body ? document.body.clientHeight : document.documentElement.clientHeight);
		var ScrollHeight=(document.body ? document.body.scrollHeight : document.documentElement.scrollHeight);
		var ScrollTop=(document.body ? document.body.scrollTop : document.documentElement.scrollTop);
		var ScrollTopCheck=(document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop);
		if (ScrollTopCheck>ScrollTop) {ScrollTop=ScrollTopCheck}
		if ((ScrollTop + ClientHeight)>=ScrollHeight) {document.getElementById("ScrollMarker").style.display="none"}
		else if (ClientHeight<ScrollHeight) {document.getElementById("ScrollMarker").style.display=""}
		}
	}

function setHistoryFrameSize() {
	if ((parent) && (parent.document.getElementById) && (parent.document.getElementById("History"))) {
		var TargetHeight=document.getElementById('ContentTable').scrollHeight;
		parent.document.getElementById("History").height=TargetHeight + 0;
		}
	}

function BuyProduct(PID,IsAccessory) {
	document.getElementById("BuyActions_" + PID).style.display="none";
	document.getElementById("BuyHeads_" + PID).style.display="none";
	document.getElementById("BasketAdd_" + PID).style.display="";
	}

function RevertProduct(PID,IsAccessory) {
	document.getElementById("BuyActions_" + PID).style.display="";
	document.getElementById("BuyHeads_" + PID).style.display="";
	document.getElementById("BasketAdd_" + PID).style.display="none";
	}
	
function showRangeProduct(ImgRef,PID,IsAccessory) {
	var objProdRows=document.getElementById("RangeProducts").rows;
	var objThumbImgs=document.getElementById("PhotoBrowser").firstChild.getElementsByTagName("IMG");
	for (var i=0; i<objProdRows.length; i++) {objProdRows[i].style.display="none"}
	for (var i=0; i<objThumbImgs.length; i++) {objThumbImgs[i].className="none"}
	document.getElementById("Product_" + PID).style.display="";
	document.getElementById("Thumb_" + PID).className="ThumbActive";
	window.focus();
	do {if ((arrPhotos.length>0) && (arrPhotos[arrPhotos.length - 1][5]==true)) {arrPhotos.length--}} while (arrPhotos.length>0 && arrPhotos[arrPhotos.length - 1][5]==true);
	var pointer=arrPhotos.length;
	for (var i=0; i<eval("arrPhotos_" + PID).length; i++) {arrPhotos[arrPhotos.length]=eval("arrPhotos_" + PID)[i]}
	ScrollImg(pointer-arrPointer,IsAccessory);
	document.getElementById("ImgScroller").style.display = (arrPhotos.length>1 ? "" : 'none');
	setupProduct(PID);
	}

