var undefined;
VI_App = navigator.appName;
VI_popupNo = 0;
VI_popup1 = undefined;
VI_popup2 = undefined;
var pos = document.location.href.indexOf("vividinteractive.co.uk");
if( pos >= 0 ) {
	pos = document.location.href.indexOf("/", pos);
	pos = document.location.href.indexOf("/", pos+1);
	var cgi_bin = document.location.href.substr(0,pos) + "/cgi";
	testMode = true;
} else {
	var cgi_bin = "http://www.lambsbricks.com/cgi";
	testMode = false;
}

// Miscellaneous functions

function noOp() {};

function initResize(elem) {
	window.onresize = function() {resizeAll()};
	resizeAll();
}
	
function resizeAll(forPrint) {
	var scrollArea = getElement("scrollarea");
	var topNav = getElement("topnav");
	var sideNav = getElement("sidenav");
	var splitX = 150;
	var splitY = 173;
	var winW = document.all?document.body.clientWidth:window.innerWidth;
	var winH = document.all?document.body.clientHeight:window.innerHeight;
	var varW = (winW-splitX)+"px";
	var varH = (winH-splitY)+"px";
	scrollArea.style.width = varW;
	scrollArea.style.height = sideNav.style.height = varH;
	topNav.style.width = winW+"px";
}

function printInfo() {
	var scrollArea = getElement("scrollarea");
	scrollArea.style.height = "3000px"
	window.print();
}

function VI_FloatingWindow(url, xSize, ySize) {
	newWindow = window.open(url, "detail", "height=" + ySize + ",width=" + xSize + ",menubar,toolbar,resizable,scrollbars,dependent");
	newWindow.focus();
}

function VI_Popup(url, xSize, ySize, extraOpts) {
	if( extraOpts ) {
		var opts = "height=" + ySize + ", width=" + xSize + "," + extraOpts;
	} else if( url.indexOf("zoom") >= 0 || url.indexOf("photos") >= 0 || url.indexOf("detail.htm") >= 0) {
		var opts ="height=" + (ySize+36) + ",width=" + (xSize+12) + ",dependent";
	} else {
		var opts = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, height=" + ySize + ", width=" + xSize;
	}
	if( VI_popupNo == 1 ) {
		if( VI_popup1 != undefined && ! ( MacOS && ( Safari || InternetExplorer ) ) ) VI_popup1.close();
		VI_popup2 = window.open(url, window.name+"popup2", opts);
		VI_popup2.focus();	
		VI_popupNo = 2;
		return(VI_popup2);
	} else {
		if( VI_popup2 != undefined && ! ( MacOS && ( Safari || InternetExplorer ) ) ) VI_popup2.close();
		VI_popup1 = window.open(url, window.name+"popup1", opts);
		VI_popup1.focus();	
		VI_popupNo = 1;
		return(VI_popup1);
	}
}

function VI_zoomWin(VI_Width, VI_Height, VI_Page) {
	if( VI_Page.indexOf("zoom.htm") >= 0 ) {
		var page = VI_Page;
	} else {
		var page = "../photos/" + VI_Page;
	}
	
	VI_Popup(page, VI_Width, VI_Height);
}

function writeNavDivs() {
	var d = document;
	var page = VI_docName(d.location.href);
	var args = VI_getArgs(d.location.href);
	if( flashEnabled ) {
		var extraArgs = args["transition"] == "0" || args["transition"] == "1" ? "&transition="+args["transition"] : "";
		extraArgs += ( args["query"] == undefined ? "" : "&query="+escape(args["query"]) );
		if( InternetExplorer && Windows ) {
			extraArgs += "&winie=1";
			d.write('<SCRIPT LANGUAGE=VBScript\> \n');
			d.write('on error resume next \n');
			d.write('Sub topswf_FSCommand(ByVal command, ByVal args)\n');
			d.write('  call topswf_DoFSCommand(command, args)\n');
			d.write('end sub\n');
			d.write('</SCRIPT\> \n');
		}
		d.write('<div id="topnav">');
		embedFlash("topswf", "../swfs/layout.swf?panel=top&page="+page+extraArgs, 2000, 173);
		d.write('</div>');
		d.write('<div id="sidenav">');
		embedFlash("sideswf", "../swfs/layout.swf?panel=side&page="+page+extraArgs, 150, 1327);
		d.write('</div>');
	} else {
		writeNoFlashDivs(page, args);
	}
}

function topswf_DoFSCommand(command, args) {
	var topswfObj = InternetExplorer ? topswf : document.topswf;
	switch( command ) {
		case "contact":
			contact_window();
			break;
		case "search":
			//top.document.location.href = "http://siteLevel.whatUseek.com/query.go?query="+escape(args)+"&crid=35bfb64f71aaa917";
			top.document.location.href = "../info/search.htm?query="+escape(args);
			break;
		case "catalogue":
			order_brochure_window();
			break;
		case "download":
			download_window();
			break;
		case "products":
			goto_products(args);
			break;
		default:
			alert(command);
			break;
	}
}

function goto_products(cat) {
	top.document.location.href = "../nav/catalog.htm?cat="+cat;
}

function contact_window(name) {
	if( name != undefined ) {
		VI_Popup("../info/contact.htm?area="+name, 600, 500);
	} else {
		VI_Popup("../info/contact.htm", 600, 500);
	}
}

function order_brochure_window() {
	VI_Popup("../specs/quote.htm?mode=brochure", 700, 500);
}

function download_window() {
	VI_Popup("../specs/quote.htm?mode=download", 700, 500);
}

function embedFlash(id, url, w, h) {
	var d = document;
	d.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"');
	d.write('codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"');
	d.write('width="'+w+'" height="'+h+'" id="',id,'">');
	d.write('<param name="allowScriptAccess" value="sameDomain" />');
	d.write('<param name="movie" value="',url,'" />');
	d.write('<param name="quality" value="high" />');
	d.write('<param name="scale" value="noscale" />');
	d.write('<param name="wmode" value="window" />');
	d.write('<param name="bgcolor" value="#ffffff" />');
	d.write('<embed src="',url,'" quality="high" scale="noscale"');
	d.write('wmode="window" bgcolor="#ffffff" width="'+w+'" height="'+h+'"');
	d.write('name="',id,'" allowScriptAccess="sameDomain" ');
	d.write('type="application/x-shockwave-flash"');
	d.write('pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	d.write('</object>');
}
// returns the category associated with a product type,

// e.g. "brick" becomes "bricks" etc.  Everything from

// the first "_" is ignored.

function VI_catOf(someProduct) {
	return someProduct.split("_")[0];
}

function VI_numOf(someProduct) {
	return someProduct.split("_")[1];
}

// returns the file portion of a URL with the path removed.

// works with both DOS paths and URLs.  Strips any query strings.

function VI_fileName(someUrl) {
	VI_Pos = someUrl.lastIndexOf("/");
	VI_Pos2 = someUrl.lastIndexOf("\\");
	if( VI_Pos2 > VI_Pos ) {
		VI_Pos = VI_Pos2;
	}
	VI_Name = someUrl.substr(VI_Pos + 1);
	VI_Pos = VI_Name.indexOf("?");
	if( VI_Pos != -1 ) VI_Name = VI_Name.substr(0, VI_Pos);
	return VI_Name;
}



// returns just the document name, without path or extension.

function VI_docName(someUrl) {
	VI_Name = VI_fileName(someUrl);
	VI_Pos = VI_Name.indexOf(".");
	if( VI_Pos != -1 ) VI_Name = VI_Name.substr(0, VI_Pos);
	return VI_Name;
}



// returns the section name of document, i.e. strips numbers

// from page names.

function VI_sectionName(someUrl) {
	VI_Name = VI_docName(someUrl);
	VI_Pos = VI_Name.lastIndexOf("_");
	VI_Suffix = VI_Name.substr(VI_Pos + 1);
	if( ! isNaN(VI_Suffix) ) {
		VI_Name = VI_Name.substr(0, VI_Pos);
	}
	return VI_Name;
}



// returns a collection of arguments contained within the query string.

// Argument can be a complete URL or just the query string.

// If it's a URL with no query string no arguments are returned.

function VI_getArgs(someUrl) {
	VI_Arg = new Object;
	VI_Pos = someUrl.indexOf("?");
	VI_String = someUrl.substr(VI_Pos + 1);
	if( VI_String.indexOf("=") != -1 ) {
		VI_Args = VI_String.split("&");
		for( a = 0; a < VI_Args.length; a++ ) {
			VI_Part = VI_Args[a].split("=");
			var val = VI_Part[1];
			VI_Arg[VI_Part[0]] = val == "" || val == undefined || val == undefined ? "" : unescape(val);
		}
	}
	return VI_Arg;
}



// Saves all control values into a cookie, the name of which

// is supplied as an argument.

// Controls whose names begin with "company" are saved in a

// seperate cookie named "company".

// Assumes the first form in the document.

// Requires that cookie.js is included in the document.

function VI_saveValues(cookieName) {
	var VI_Form = document.forms[0];
	var VI_Data = "";
	var VI_CompanyData = "";
	var VI_SiteData = "";
	var control;
	var VI_Value;
	
	for( e = 0; e < VI_Form.elements.length; e++ ) {
		control = VI_Form.elements[e];
		if( control.type == "select-one" ) {
			VI_Value = control.selectedIndex;
		} else if( control.type == "radio" ) {
			if( control.checked ) {
				VI_Value = control.value;
			} else {
				continue;
			}
		} else {
			VI_Value = control.value;
		}
		//alert(control.name + ": " + VI_Value);
		if( VI_Value == "" ) continue;
		if( control.name.indexOf("company") == 0 ) {
			if( VI_CompanyData != "" ) VI_CompanyData += "&";
			VI_CompanyData += control.name + "=" + escape(VI_Value);
		} else if( control.name.indexOf("site") == 0 ) {
			if( VI_SiteData != "" ) VI_SiteData += "&";
			VI_SiteData += control.name + "=" + escape(VI_Value);
		} else {		
			if( VI_Data != "" ) VI_Data += "&";
			VI_Data += control.name + "=" + escape(VI_Value);
		}
	}
	var expdate = new Date ();
	FixCookieDate (expdate);
	expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000));
	if( cookieName != undefined ) SetCookie(cookieName, VI_Data, expdate, "/");
	SetCookie("company", VI_CompanyData, expdate, "/");
	SetCookie("site", VI_SiteData, expdate, "/");
}

// Gets all control values from a cookie, the name of which

// is supplied as an argument.

// Controls whose names begin with "company" come from a

// seperate cookie named "company".

// Assumes the first form in the document.

// Requires that cookie.js is included in the document.

function VI_getValues(cookieName) {
	var VI_Form = document.forms[0];
	var VI_Values = new Object;
	var VI_CompanyValues = new Object;
	var VI_SiteValues = new Object;
	var VI_Data;
	var VI_Bit;
	var control;

	if( cookieName != undefined ) {
		var VI_Cookie = GetCookie(cookieName);
		if( VI_Cookie != undefined ) {
			VI_Data = VI_Cookie.split("&");
			for( d = 0; d < VI_Data.length; d++ ) {
				VI_Bit = VI_Data[d].split("=");
				VI_Values[VI_Bit[0]] = unescape(VI_Bit[1]);
			}
		}
	}

	VI_Cookie = GetCookie("company");
	if( VI_Cookie != undefined ) {
		VI_Data = VI_Cookie.split("&");
		for( d = 0; d < VI_Data.length; d++ ) {
			VI_Bit = VI_Data[d].split("=");
			VI_CompanyValues[VI_Bit[0]] = unescape(VI_Bit[1]);
		}
	}

	VI_Cookie = GetCookie("site");
	if( VI_Cookie != undefined ) {
		VI_Data = VI_Cookie.split("&");
		for( d = 0; d < VI_Data.length; d++ ) {
			VI_Bit = VI_Data[d].split("=");
			VI_SiteValues[VI_Bit[0]] = unescape(VI_Bit[1]);
		}
	}

	for( e = 0; e < VI_Form.elements.length; e++ ) {
		control = VI_Form.elements[e];
		if( control.name.indexOf("company") == 0 ) {
			VI_Value = VI_CompanyValues[control.name];
		} else if( control.name.indexOf("site") == 0 ) {
			VI_Value = VI_SiteValues[control.name];
		} else {
			VI_Value = VI_Values[control.name];
		} 
		if( VI_Value != undefined && VI_Value != undefined ) {
			if( control.type == "select-one" ) {
				control.selectedIndex = VI_Value;
			} else if( control.type == "radio" ) {
				if( control.value == VI_Value ) {
					control.checked = true;
				}
			} else {
				control.value = VI_Value;
			}
		}
	}
}


function VI_blur(someObject) {
	if( InternetExplorer ) someObject.blur();
}

// CHANGE - ash glazed header sizes
function limitToDigits(inputBox) {
	return limitToPattern(inputBox, /^[0-9 ()]*$/, "Please enter numerals only.");
}
function limitToInteger(inputBox) {
	return limitToPattern(inputBox, /^[0-9]*$/, "Please enter numerals only.");
}
function limitToPhone(inputBox) {
	return limitToPattern(inputBox, /^[0-9 ()+]*$/, "Please enter valid telephone numbers only.\n(Allowed characters: 0-9 + ( )");
}

function limitToPattern(inputBox, pattern, message) {
	inputBox.value = trimString(inputBox.value);
	var invalid = ! (pattern.test(inputBox.value) || inputBox.value == "");
	if( invalid ) {
		alert(message);
		inputBox.value = '';
		ib = inputBox;
		setTimeout("ib.focus(); delete ib", 100);
	}
	return(!invalid);
}

function limitToEmail(inputBox) {
	return limitToPattern(inputBox, /^([\-A-Z0-9_\.])+\@(([\-A-Z0-9])+\.)+([A-Z0-9]{2,4})+$/i, 'Please enter a valid email address.');
}
// END

function trimString(val) {
	while( val.length > 0 && val.charAt(0) == " " ) val = val.substr(1);
	while( val.length > 0 && val.charAt(val.length-1) == " " ) val = val.substr(0,val.length-1);
	return val;
}

function viewPdf(url) {
	if( Windows ) {
		pdfWindow = window.open(url, "pdfWin");
	} else {
		document.location.href = url;
	}
}
		
