/* Global variables */
var valueChanged = false;
var openImg = new Image();
var closedImg = new Image();

/** 
 * Text with different languages
 * String values are over written in english template values!
 * This file  contains only the default language string (finnish) 
 */
var TEXT_PRODUCTS_NOT_YET_ADDED_TO_SHOPPINGCART = "Tuotteiden määriä ei ole vielä tallennettu ostoslistaan. Tallenna määrät valitsemalla peruuta ja painamalla lisää ostoslistaan-painiketta. Jos jatkat, menetät muutokset. Haluatko jatkaa?"


openImg.src = "images/open.gif";
closedImg.src = "images/closed.gif";

/* Functions */
function Delivery_change(field) {
	if (field.value  == "Nouto") {
		window.location="?nouto=1&";
	} 
	if (field.value == "Posti") {
		window.location="?nouto=0&";
	}
}

function gotonextfield(currentField,nextField) {
    if (currentField.value.length == 8)
        document.addform[nextField].focus();
}

function show(object, val) {
	document.getElementById(object).style.top = getBrowserWindowsYOffset()+10;
	/* document.getElementById(object).style.bottom = 10; */
	document.getElementById(object).style.visibility = val;
	if (val == false) {
		document.getElementById(object).style.height = 0;
	}
	else {
		document.getElementById(object).style.height = 400;
	}
}

function open_window(urlStr, windowName) {
	window.open(urlStr, windowName, 'resizable=1,scrollbars=0,directories=no,toolbar=no,menubar=no,titlebar=no,status=no,left=20,top=20,width=480,height=450') 
}

function hideshowDiv(pass) {
	var element = document.getElementById(pass);
	if(element) {
		if (element.style.display=="none") {
			element.style.display="block";
			var c = getCookie('ecom_open');
			if(c.indexOf(pass.concat("|"))!=-1) {
			} else {
				newentry = c.concat(pass.concat('|'));
				setCookie('ecom_open',newentry,'10000');
			}
		} else {
			element.style.display="none";
			var c = getCookie('ecom_open');
			var c_array = c.split('|');
			var j,nc;
			nc='';
			for (j = 0; j < c_array.length; j++)
			{
				if(c_array[j]!=pass) {
					nc.concat(c_array[j]);
					nc.concat('|');
				}
			}
	        setCookie('ecom_open',nc,1);
		}
	}
}

function getCookie(NameOfCookie) {
	if (document.cookie.length > 0) {
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) {
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end));
		}
	}
	return '';
}

function setCookie(NameOfCookie, value, expiredays) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function delCookie (NameOfCookie) {
	if (getCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function markChanged(value)
{
	valueChanged = value;
}

function confirmValueLost(url) {
	if (valueChanged == true) {
		var confirmation = confirm(TEXT_PRODUCTS_NOT_YET_ADDED_TO_SHOPPINGCART);
		if (confirmation == true) {
			document.location = url;
		}
	}
	else {
		document.location = url;
	}
}

function showBranch(branch){
	var objBranch = document.getElementById(branch).style;
	if(objBranch.display=="block")
		objBranch.display="none";
	else
		objBranch.display="block";
}

function swapFolder(img){
	objImg = document.getElementById(img);
	if(objImg.src.indexOf('closed.gif')>-1)
		objImg.src = openImg.src;
	else
		objImg.src = closedImg.src;
}

function getBrowserWindowsYOffset() {
	return getScrollXY()[1];
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}