// Finding the browsere info
var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
function getRef(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}
// getting the style id object
function getStyle(id) {
	return (isNS4 ? getRef(id) : getRef(id).style);
}

//function to open the search window
searchDelay = 0;
searchInFocus = false
function searchOpen(focusField)
{
	clearTimeout(searchDelay);
	searchStyle = getStyle("searchMenu")
	searchStyle.visibility = "visible"
	if(!isNS4 && focusField=='text')
		document.forms[0].quickSearch.focus();
}

function searchClose()
{
	if(!searchInFocus)
	{
		if(!isNS4)
			document.forms[0].quickSearch.blur();
		searchDelay = setTimeout('searchMenuHide()', 200);
	}
}

function searchMenuHide()
{
	searchStyle = getStyle("searchMenu")
	searchStyle.visibility = "hidden"
	searchInFocus = false;
}

function doSearch(fieldName) 
{
	stSearch = document.getElementById(fieldName).value;
	//ppm 040623 document.location='/Site tools/Main.ACO.aspx?path=/aco/denmark/home/site tools/search&layout={252AE6E5-50DA-44AC-BA9B-5DBEBBC2F3EF}&search='+stSearch;
	// the link below will work fine, having the quality that is not hardcoded altough the page navigator needs a "path" and a "layout".
	// at the first run, the "page" and the "layout" doesn't exists, but we show "all results" by default anyway.
	// at second run, if filtering by items per page, the "path" and "layout" will be added by sitecore
	// if we plan to show 10, 20 items per page at first search that we'll have to find another solution for this function
	// and make it context sensitive...
	document.location='/Site tools/Search.aspx?search='+stSearch;
}

function hasPressedEnter() 
{
	if(event.keyCode==13) {
		doSearch('quickSearch');
	} 
}

function upDateSearch() {
	document.forms[0].submit();
}

// show flash
function doFlash(itemid,flashfile,flashwidth,flashheight) {
	var output = "";
	if (flashinstalled==2 && flashversion>=5) {
		output = "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='"+flashwidth+"' height='"+flashheight+"'>"
		output += "<param name='movie' value='"+flashfile+"?id="+itemid+"'/>"
		output += "<param name='quality' value='high'/>"
		output += "<param name='bgcolor' value='#FFFFFF'/>"
		output += "<PARAM name='WMode' value='Transparent'/>"
		output += "<param name='menu' value='0'/>"
		output += "<embed src='"+flashfile+"?id="+itemid+"' NAME=flash22 width='"+flashwidth+"' height='"+flashheight+"' wmode='transparent' autostart='false' quality='high' bgcolor='#FFFFFF' TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'></embed>"
		output += "</object>"
	}
	document.write(output);
}

// forgot password link popup
function DoPassword(url) {
	if (document.all)
	{
		/* the following is only available after onLoad */
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	else if (document.layers)
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}

	var popW = 350, popH = 250;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	window.open(url, "passwordWindow", "toolbar=0,location=center,directories=0,status=0,menubar=no,scrollbars=no,resizable=no,width="+popW+",height="+popH+",top="+topPos+",left="+leftPos);
}

// email link popup
function DoEmail(url,pid) {
	if (document.all)
	{
		/* the following is only available after onLoad */
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	else if (document.layers)
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}

	var popW = 450, popH = 350;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	window.open(url+"?id="+pid, "dviPrint", "toolbar=0,location=center,directories=0,status=0,menubar=no,scrollbars=no,resizable=no,width="+popW+",height="+popH+",top="+topPos+",left="+leftPos);
}

// print page popup
function DoPrint() {
	var loc = location.href;
	var pri = "?print=1&autopopup=1";
	if (loc.indexOf("?")>0) {
		pri = "&print=1";
	}
	window.open(loc+pri, "", "toolbar=1,location=0,directories=0,status=0,menubar=yes,scrollbars=yes,resizable=yes,width=720,height=400");
}

// preview popup (download list)
function DoPreview(url) {
	window.open("/forms/Preview.aspx?picurl="+url, "", "toolbar=0,location=0,directories=0,status=0,menubar=no,scrollbars=no,resizable=yes,width=50,height=50");
}

function SubmitForm(action) {
	document.forms[0].action = action;
	document.forms[0].submit();
}

function OnPageLoad() {
	if (document.forms[0].username) {
		document.forms[0].username.focus();
	}
	if (location.search.length > 0) {
		GetRetailers(document.getElementById("productgroup"),true);
	}
}

function isValidEmail(emailAddress) {
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return re.test(emailAddress);
}

function CheckForm(form) {
	var isValid = true;
	var brochuresOK = false;
	var formElements = form.elements;
	// Check that at least 1 brochure was ordered
	for (i=0; i<formElements.length; i++) {
		if (formElements[i].name.indexOf("amount{") > -1) {
			if (formElements[i].value != "")
				brochuresOK = true;
		}
	}
	if (!brochuresOK) {
		isValid = false;
	}

	document.getElementById("error1").style.display = "none";
	document.getElementById("error2").style.display = "none";
	document.getElementById("error3").style.display = "none";
	document.getElementById("error4").style.display = "none";
	if (formElements["name"].value=="") {document.getElementById("error1").style.display = "inline";isValid = false;}
	if (formElements["address"].value=="") {document.getElementById("error2").style.display = "inline";isValid = false;}
	if (formElements["zip"].value=="") {document.getElementById("error3").style.display = "inline";isValid = false;}
	if (formElements["city"].value=="") {document.getElementById("error3").style.display = "inline";isValid = false;}
	if (formElements["email"].value!="")
		if (!isValidEmail(formElements["email"].value)) {document.getElementById("error4").style.display = "inline";isValid = false;}
	if (isValid) {
		SubmitForm('/Forms/Brochure.aspx');
	}
}

function popUp(URL) {

	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=auto,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 240,top = 212');");
}
