// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// HISTORY
// ------------------------------------------------------------------
// December 9, 2003: Added script to the Javascript Toolbox
// December 10, 2003: Added the preProcessTrees variable to allow user
//      to turn off automatic conversion of UL's onLoad
// March 1, 2004: Changed it so if a <li> has a class already attached
//      to it, that class won't be erased when initialized. This allows
//      you to set the state of the tree when painting the page simply
//      by setting some <li>'s class name as being "liOpen" (see example)
/*
This code is inspired by and extended from Stuart Langridge's aqlist code:
		http://www.kryogenix.org/code/browser/aqlists/
		Stuart Langridge, November 2002
		sil@kryogenix.org
		Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/) 
		and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109)
*/

function emptycart(form){
	with(form){
		submit();	
	}
}

// Automatically attach a listener to the window onload, to convert the trees
addEvent(window,"load",convertTrees);

// Utility function to add an event listener
function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}

// Full expands a tree with a given ID
function expandTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeOpenClass);
}

// Fully collapses a tree with a given ID
function collapseTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeClosedClass);
}

// Expands enough nodes to expose an LI with a given ID
function expandToItem(treeId,itemId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	var ret = expandCollapseList(ul,nodeOpenClass,itemId);
	if (ret) {
		var o = document.getElementById(itemId);
		if (o.scrollIntoView) {
			o.scrollIntoView(false);
		}
	}
}

// Performs 3 functions:
// a) Expand all nodes
// b) Collapse all nodes
// c) Expand all nodes to reach a certain ID
function expandCollapseList(ul,cName,itemId) {
	if (!ul.childNodes || ul.childNodes.length==0) { return false; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (itemId!=null && item.id==itemId) { return true; }
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					var ret = expandCollapseList(sitem,cName,itemId);
					if (itemId!=null && ret) {
						item.className=cName;
						return true;
					}
				}
			}
			if (subLists && itemId==null) {
				item.className = cName;
			}
		}
	}
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
	setDefault("treeClass","mktree");
	setDefault("nodeClosedClass","liClosed");
	setDefault("nodeOpenClass","liOpen");
	setDefault("nodeBulletClass","liBullet");
	setDefault("nodeLinkClass","bullet");
	setDefault("preProcessTrees",true);
	if (preProcessTrees) {
		if (!document.createElement) { return; } // Without createElement, we can't do anything
		uls = document.getElementsByTagName("ul");
		for (var uli=0;uli<uls.length;uli++) {
			var ul=uls[uli];
			if (ul.nodeName=="UL" && ul.className==treeClass) {
				processList(ul);
			}
		}
	}
}

// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
	if (!ul.childNodes || ul.childNodes.length==0) { return; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					processList(sitem);
				}
			}
			var s= document.createElement("SPAN");
			var t= '\u00A0'; // &nbsp;
			s.className = nodeLinkClass;
			if (subLists) {
				// This LI has UL's in it, so it's a +/- node
				if (item.className==null || item.className=="") {
					item.className = nodeClosedClass;
				}
				// If it's just text, make the text work as the link also
				if (item.firstChild.nodeName=="#text") {
					t = t+item.firstChild.nodeValue;
					item.removeChild(item.firstChild);
				}
				s.onclick = function () {
					this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;
					return false;
				}
			}
			else {
				// No sublists, so it's just a bullet node
				item.className = nodeBulletClass;
				s.onclick = function () { return false; }
			}
			s.appendChild(document.createTextNode(t));
			item.insertBefore(s,item.firstChild);
		}
	}
}




// JavaScript Document
//-----------------------------------------------------------------
// Namn: ajax(inControl, inURL)
// Kort beskrivning: Funktionen anropar valfri fil via ajax
// Indata: 
// inControl : id på det input, span eller div som skall tilldelas output av den anropade filen
// inURL: sökvägen till den fil som ska anropas, skickas med GET, dvs. vi kan lägga till ?func=callme&id=54 om vi vill.
// Version: 1.0
// Senast ändrad: Michael Melander 2006-11-02
//	- Lade till funktionalitet för div, span och button (kan möjligen fungera för andra också)
//
// Tidigare ändrad: Bernt Johansson 2006-09-21
//
// Exempel: 	<input type="button" onClick="ajax('elementId', 'anropadFil.php');" value="Tryck här" />
//				<input type="button" onClick="ajax('elementId', 'anropadFil.asp?func=callMe');" value="Tryck här" />
// Stödjs av IE5+, Mozilla 1.0+, Opera, Safari, Netscape 6+
//-----------------------------------------------------------------
function ajax(inControl, inURL){
	var xmlhttp = null;
	document.body.style.cursor = 'wait';	
	
	if (window.XMLHttpRequest) { // Firefox, Opera, Safari och Netscape
	   xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // Internet Explorers olika versioner
	   try {
		  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");	
	   } catch (e) {
		  try {
			 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch (e) { alert("Din webbläsare stödjer inte Ajax"); }
	   }
	}
	
	if (xmlhttp){
		var url = inURL
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);
		
		var docObj = document.getElementById(inControl);
	
		/* Kontrollerar om vi ska skriva till value eller innerHTML */
		with( docObj ){
			var objType = docObj.type;
			
			switch( objType ){
				case "text":
				case "button":
					docObj.value = xmlhttp.responseText;
					break;
				default:
					docObj.innerHTML = xmlhttp.responseText;				
					break;
			}		
		}
		
		xmlhttp.abort();
	}
	
	document.body.style.cursor = 'auto';	
}


// FORM VALIDATION
// JavaScript Document

window.onload = attachFormHandlers;


function attachFormHandlers()
{
  // Ensure we're working with a 'relatively' standards 
  // compliant browser
  if (document.getElementsByTagName)
  {
    var objForm = document.getElementsByTagName('form');

    for (var iCounter=0; iCounter<objForm.length; iCounter++)
      objForm[iCounter].onsubmit = function(){return checkForm(this);}
  }
}

function checkForm(objForm)
{
  var arClass, bValid;
  var objField = objForm.getElementsByTagName('*');
  var reason;

  for (var iFieldCounter=0; iFieldCounter<objField.length; iFieldCounter++)
  {
    // Allow for multiple values being assigned to the class attribute
    arClass = objField[iFieldCounter].className.split(' ');
    for (var iClassCounter=0; iClassCounter<arClass.length; iClassCounter++)
    {
      switch (arClass[iClassCounter])
      {
        case 'string':
			if(objField[iFieldCounter].value != ""){
           		bValid = isString(objField[iFieldCounter].value.replace(/^\s*|\s*$/g, ''));
			}else{
				bValid = true;	
			}
		   reason = 0;
           break;
        case 'number' :
			if(objField[iFieldCounter].value != ""){
           		bValid = isNumber(objField[iFieldCounter].value);
			}else{
				bValid = true;	
			}		   
		   	reason = 0;
          	 break;
        case 'email' :
			if(objField[iFieldCounter].value != ""){
           		bValid = isEmail(objField[iFieldCounter].value);
			}else{
				bValid = true;	
			}		   
		   	reason = 0;
           	break;
		case 'required' :
			bValid = objField[iFieldCounter].value != "";
			reason = 1;
			break;
        default:
           bValid = true;
      }

      if (bValid == false)
      {
        // If this field is invalid, leave the testing early,
        // and alert the visitor to this error
		if (reason == 1){
			alert('Du måste ange: ' + objField[iFieldCounter].name);
			objField[iFieldCounter].select();
			objField[iFieldCounter].focus();
			return false;			
			
		}
		else{
			alert('Felaktigt värde i fältet: ' + objField[iFieldCounter].name);
			objField[iFieldCounter].select();
			objField[iFieldCounter].focus();
			return false;
		}
      }
    }
  }
  return true;
}

function isString(strValue)
{
  return (typeof strValue == 'string' && strValue != '' && isNaN(strValue));
}

function isNumber(strValue)
{
  return (!isNaN(strValue) && strValue != '');
}

function isEmail(strValue)
{
  var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;

  return (strValue != '' && objRE.test(strValue));
}




//Utskriftsvänliga sidor
function myprint(inCssPath, inDiv)
{
var div2Print = '<div id="divToPrint"><div id="printTop"></div>' + document.getElementById(inDiv).innerHTML + "</div>";
var d = new Date();
var datum;
var dag =d.getDate();
var manad =d.getMonth() + 1;
var ar =d.getFullYear();
if (manad < 10)
	manad2 = "0" + manad;
else manad2 = manad;

if (dag < 10)
	dag2 = "0" + dag;
else dag2 = dag;

datum = ar + "-" + manad2 + "-" + dag2;
window1= window.open ("", "mywindow");
window1.document.open();
window1.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><link href="'+inCssPath+'" rel="stylesheet" type="text/css"></head><body style="background-color:#ffffff">');
window1.document.write("<div id='main'>");
window1.document.write(div2Print);
window1.document.write("</div></body></html>");
window1.document.close();
window1.print();
}

function showDiv(inDivId){
	document.getElementById(inDivId).style.display='block';

}

function closeDiv(inDivId){
	document.getElementById(inDivId).style.display='none';

}

function togglebutton(inId){
	var btnStatus;
	btnStatus = document.getElementById(inId).disabled;
	if(btnStatus == true){
		document.getElementById(inId).disabled = false;
	}else{
		document.getElementById(inId).disabled = true;	
	}

}

function btnShowPrintableList(form){
	with(form){
		todo.value = "printcart";
		submit();
		
	}
}

function btnEmptyCart(form){
	with(form){
		todo.value = "emptycart";
		submit();
		
	}
}