function changeStyle(object) {
	if (object.className == 'menuItem')
		object.className = 'menuItemHighlight';
	else if (object.className != 'menuItemSelected')
		object.className = 'menuItem';
}

function load(url) {
	if(getCookie('WEBSESSION', 'ID'))
		document.location.href = url;
	else
		if(url.indexOf('?') > 0)
			document.location.href = url + '&websessionid=' + websessionid;
		else
			document.location.href = url + '?websessionid=' + websessionid;
}

function popup(url, width, height, scrollbars, extras) {
	if(extras!=null)
		extras = ',' + extras;
	else
		extras = ',' + 'locationbar=0,menubar=0,statusbar=0,toolbar=0,status=0,location=0,resizable=no';
	newwin = window.open(url, '','width=' + width +',height=' + height + ', scrollbars=' + scrollbars + extras);
	newwin.focus();
}

function submitForm(id,method) {
	if(method)
		document.getElementById(id).elements["method"].value=method;
	document.getElementById(id).submit();
}

function validateForm(id,method) {
	var objForm = document.getElementById(id);
    var why = "";
    
    if(method == "step1")
		why += checkProducts(objForm);
	else if(method == "update")
		why += checkProducts(objForm);
	else if(method == "order") {
		why += checkProducts(objForm);
		why += isEmpty(objForm.elements["fname"].value,"Förnamn");
		why += isEmpty(objForm.elements["lname"].value,"Efternamn");
		why += isEmpty(objForm.elements["email"].value,"E-postadress");
		if(objForm.elements["email"].value.length>0)
			why += checkEmail(objForm.elements["email"]);
		
		// *** Kolla så att båda lösenorden är lika, om inte visa fel.
		if(objForm.elements["password"]) {
			if(objForm.elements["password"].value.length > 0 && objForm.elements["confirmpassword"].value.length > 0)
				why += isEqual(objForm.elements["password"].value, objForm.elements["confirmpassword"].value, "Fälten önskat lösenord och bekräfta lösenord måste vara lika");
			else {
				why += isEmpty(objForm.elements["password"].value,"Önskat lösenord");
				why += isEmpty(objForm.elements["confirmpassword"].value,"Bekräfta lösenord");
			}
		}
		
		why += isEmpty(objForm.elements["address"].value,"Adress");
		why += isEmpty(objForm.elements["zipcode"].value,"Postnummer");
		why += isEmpty(objForm.elements["city"].value,"Ort");
		why += isChecked(objForm.elements["approve"].checked,"Godkänn villkoren till höger genom att sätta ett kryss i rutan.");
		
		//why += checkPassword(objForm.password.value);
		//why += checkUsername(objForm.username.value);
		//why += isEmpty(objForm.notempty.value);
		//why += isDifferent(objForm.different.value);
		//for (var i=0, n=objForm.radios.length; i<n; i++) {
		//    if (objForm.radios[i].checked) {
		//        var checkvalue = objForm.radios[i].value;
		//        break;
		//    } 
		//}
		//why += checkRadio(checkvalue);
		//why += checkDropdown(objForm.choose.selectedIndex);
	}
	else if(method == "save") {
		why += isEmpty(objForm.elements["fname"].value,"Förnamn");
		why += isEmpty(objForm.elements["lname"].value,"Efternamn");
		why += isEmpty(objForm.elements["email"].value,"E-postadress");
		if(objForm.elements["email"].value.length>0)
			why += checkEmail(objForm.elements["email"]);
		
		// *** Kolla så att båda lösenorden är lika eller tomma, om inte visa fel.
		if(objForm.elements["password"]) {
			if(objForm.elements["password"].value.length > 0 || objForm.elements["confirmpassword"].value.length > 0)
				why += isEqual(objForm.elements["password"].value, objForm.elements["confirmpassword"].value, "Fälten nytt lösenord och bekräfta lösenord måste vara lika");
		}
		
		why += isEmpty(objForm.elements["address"].value,"Adress");
		why += isEmpty(objForm.elements["zipcode"].value,"Postnummer");
		why += isEmpty(objForm.elements["city"].value,"Ort");
	}
	
    if (why != "")
       alert(why);
	else
		submitForm(id,method);
}

function checkProducts(objForm) {
	var valueAvailable = false;
	for(var i=0; i<objForm.elements.length; i++) {
		if(objForm.elements[i].name.indexOf("product_")>=0 && objForm.elements[i].value != '') {
			valueAvailable = true;
			if(isNaN(parseInt(objForm.elements[i].value)))
				return "Fyll i antalet du vill beställa med siffror, tack!\n";
		} 
	}
	if(!valueAvailable)
		return "Du måste välja vad du vill beställa genom att mata in det antal du önskar.\n"
	else
		return "";
}

function isEqual(strng1, strng2, fieldText) {
	var error = "";
		if (strng1 != strng2) {
			error = fieldText + "\n";
		}
		return error;	  
}

function isEmpty(strng, fieldText) {
	var error = "";
		if (strng.length == 0) {
			error = "Fältet \'" + fieldText + "\' måste fyllas i för att din order ska kunna behandlas.\n";
		}
		return error;	  
}

function isChecked(checked, fieldText) {
	var error = "";
		if (!checked) {
			error = fieldText;
		}
		return error;	  
}

function checkEmail(objEmail) {
	var error="";
	if(objEmail.value.search(/(\w|\.|\-)+\@(\w|\.|\-)+\.[a-z]{2,6}$/)) {
		error = "Fältet 'E-postadress' är inte korrekt ifyllt.\n";
	}
	else {
		//test email for illegal characters
		if (objEmail.value.match(/[\(\)\<\>\,\;\:\\\"\[\]]/))
			error = "Fältet 'E-postadress' innehåller ogiltiga tecken.\n";
    }
	return error;    
}

function getCookie(name, subname) {
	var cookie = document.cookie; 
	var index = cookie.indexOf(name + "=");
	
	if (index == -1) return null;
	index = cookie.indexOf("=", index) + 1; 
	var endstr = cookie.indexOf(";", index);
	if (endstr == -1) endstr = cookie.length;
	cookie = cookie.substring(index, endstr);
	if (subname) {
		index = cookie.indexOf(subname + "=");
		if (index == -1) return null;
		index = cookie.indexOf("=", index) + 1; 
		endstr = cookie.indexOf("&", index);
		if (endstr == -1) endstr = cookie.length;
		cookie = cookie.substring(index, endstr);
	}
	
	return unescape(cookie);
}

function writeCookie(name, value) {
	var cCookie;
	var date = new Date();
	var newDate = new Date((date.getFullYear()+1),date.getMonth(),date.getDate());
	cCookie = name + '=' + escape(value.replace('+','[plus]')) + '; expires=' + newDate + '; path=/';
	document.cookie = cCookie;
}

function removeFromCookie(name,string) {
	var cookie = getCookie('CNPVotes');
	writeCookie(name,cookie.replace(string,''));
}

function initPage() {
	//Check if we support cookies otherwise try to add it to href
	if (null == getCookie('WEBSESSION', 'ID'))
		for(i=0; i<document.links.length; i++) {
			if(document.links[i].href.substring(0,4)=='http')
				if(document.links[i].href.indexOf('?') > 0)
					document.links[i].href += '&websessionid=' + websessionid;
				else
					document.links[i].href += '?websessionid=' + websessionid;
		}
}

function sendPass() {
	document.getElementById('btbLostPass').style.display = 'block';
	document.getElementById('forgotPassText').style.display = 'none';
	document.getElementById('btbLogin').style.display = 'none';
	document.getElementById('btbCustomer').style.display = 'none';
	document.getElementById('btbTryBeforeYouBuy').style.display = 'none';
}

function ajax(action, strng, originalStrng) {
	makeRequest('ajax.asp?action=' + action + '&text=' + strng + '&originaltext=' + originalStrng);
}

 function makeRequest(url) {

    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = function() { alertContents(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);

}

function alertContents(http_request) {

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
			if(http_request.responseText)
			eval(http_request.responseText);
        } else {
            alert('There was a problem with the request.');
        }
    }

}

function clearField(formName, fieldName) {
	var theField = document.forms[formName].elements[fieldName];
	var theOldField = document.forms[formName].elements[fieldName + "_old"];
	if(theOldField)
		theField.value = theOldField.value;
	else
		theField.value = "";
	theField.focus();
	theField.select();
}

function autoPost() {
	clearTimeout(timer);
	timer = setTimeout("flipPage('formPageFlip')",2000);
}

function autoPost2() {
	clearTimeout(timer);
	timer = setTimeout("flipPage('formPageFlipSmall')",2000);
}

function flipPage(formName) {
	document.forms[formName].submit();
}

function entsub(event,theform) {
	if(window.event) {
		if (window.event && window.event.keyCode == 13)
			theform.submit();
	}
	else if (event && event.which == 13)
		theform.submit();
	else
		return true;
}