// Java Document

	function sleep(delay)
	{
		var start = new Date().getTime();
		while (new Date().getTime() < start + delay);
	}

	function MM_openBrWindow(theURL,winName,features) { //v2.0
	  window.open(theURL,winName,features);
	}

	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

	function cursor_wait() {
	  document.body.style.cursor = 'wait';
	}
	
	function cursor_clear() {
	  document.body.style.cursor = 'default';
	}
	
    function call_produkter(/*produktId,*/ produktGruppId ) {
	  cursor_wait();
      // STEP 3: CALLING SERVER FUNCTION FROM CLIENT AGENT
      //str = document.getElementById("div_produkt").innerHTML;
	  
	  //if ((str.length)>0) {
		agent.call('','visaProduktGruppNamn','callback_visaProduktGruppNamn',produktGruppId);
	  	agent.call('','visaProdukter','callback_produkter',produktGruppId); // $produktGruppId
	    //agent.call('','visaProdukt','callback_produkt',produktId,produktGruppId);
		//agent.call('','andraTitle','callback_andraTitle',produktId);
      //}else{
	  //	this.location.href='index.php?produktId='+produktId+'&produktGruppId='+produktGruppId;
		//agent.call('','andraTitle','callback_andraTitle',produktId);
	 // }
    }
    // client callback function
    function callback_produkter(str) {
      document.getElementById("div_produkter").innerHTML = str;
	  cursor_clear();
    }
	function callback_visaProduktGruppNamn(str)
	{
	   document.getElementById("div_produktgruppnamn").innerHTML = str;
	}

	function call_andraVariant()
	{
		val = document.getElementById("variant").value;
		str = "<br><br><a href='javascript:call_shoppingbag("+val+",\"add_product\",1);'>Köp</a>";
		document.getElementById("div_kop").innerHTML = str;
	}
	
	function call_hamtaDeltagarId()
	{
		agent.call('','hamtaDeltagarId','callback_deltagarId', document.getElementById("user_kund").value, document.getElementById("losen_kund").value );
	}
	
	function callback_deltagarId(str)
	{
		if (str>0)
		{
			document.form1.action="index.php?p=dinasidor";
			document.form1.submit();
		}
		else
		{
			alert('Fel användarnamn eller lösenord. Försök igen eller kontakta oss.');
		}
	}
	
function call_hamtaMedarbetarId()
	{
		agent.call('','hamtaDeltagarId','callback_medarbetarId', document.getElementById("user_med").value, document.getElementById("losen_med").value );
	}
	
	function callback_medarbetarId(str)
	{
		if (str>0)
		{
			document.form1.action="index.php?p=medarbetarsidor";
			document.form1.submit();
		}
		else
		{
			alert('Fel användarnamn eller lösenord. Försök igen eller kontakta oss.');
		}
	}	
	
	function kollaAnmalan()
	{
		if (document.getElementById("form1").epost.value.length > 0 && document.getElementById("form1").mobil.value.length > 0 && (document.getElementById("form1").epost.value == document.getElementById("form1").epost2.value))
		{
				
			if (document.getElementById("form1").kursId.selectedIndex>0)
			{
				document.form1.anmalan.disabled="disabled";
				document.form1.action="index.php?p=tackanmalan";
				document.form1.submit();
				
			}
			else
				alert('Du måste välja kurs och kurstillfälle innan du kan anmäla dig. Välj i rullistan i början. Försök igen eller kontakta oss.');
		}else{
			alert('Du måste fylla i alla fält samt ange samma e-post i båda fälten om din e-postadress:\n\n'+document.getElementById("form1").epost.value+'\n'+document.getElementById("form1").epost2.value+'\n\nFörsök igen eller kontakta oss.');	
		}
			
	}

    // client function  
    function call_produkt(produktId, produktGruppId) {
	  cursor_wait();
      // STEP 3: CALLING SERVER FUNCTION FROM CLIENT AGENT
	  //var str="string";
	  //str = document.getElementById("div_produkt").innerHTML;
	  //alert((str));
	  //if ((str.length)>0) {
		  //alert('//');
	  		agent.call('','visaProdukt','callback_produkt',produktId,produktGruppId); //$produktId, $produktGruppId
			agent.call('','visaProduktGruppNamn','callback_visaProduktGruppNamn',produktGruppId);
			//agent.call('','andraTitle','callback_andraTitle',produktId);
      //	}else{
	  //		this.location.href='index.php?p='+produktId+'&pg='+produktGruppId;
		//}
    }
    // client callback function
    function callback_produkt(str) {
      document.getElementById("div_produkter").innerHTML = str;
	  cursor_clear();
	  scroll(0,0);
    }

	function andraTitle(produktId)
	{
		agent.call('','andraTitle','callback_andraTitle',produktId);
	}
	
	function visaMenyn(kategori)
	{
		agent.call('','visaMenyn','callback_visaMenyn',kategori);
		if(kategori=="omoss")
		{
			document.getElementById("omoss").src = 'images/top-bar-8_06_over.jpg';	
			document.getElementById("utbildning").src = 'images/top-bar-9_07.jpg';
		}else if(kategori=="utbildning"){
			document.getElementById("omoss").src = 'images/top-bar-9_06.jpg';	
			document.getElementById("utbildning").src = 'images/top-bar-8_07_over.jpg';
		}else{
			document.getElementById("omoss").src = 'images/top-bar-9_06.jpg';	
			document.getElementById("utbildning").src = 'images/top-bar-9_07.jpg';
		}
		
	}
	function callback_visaMenyn(str) {
      document.getElementById("div_menyn").innerHTML = str;
	  //cursor_clear();
	  //scroll(0,0);
    }
	
	function callback_andraTitle(str)
	{
		top.document.title = str;
		document.title = str;
	}
	
    function call_shoppingbag(products_id,action,cart_quantity) {
	  cursor_wait();
      // STEP 3: CALLING SERVER FUNCTION FROM CLIENT AGENT
      agent.call('','shoppingBag','',products_id,action,cart_quantity);
	  agent.call('','visaShoppingBagKassan','callback_visashoppingbag'); 
	  
	  document.getElementById("div_kop").innerHTML = "<br><br>Varan är nu placerad i varukorgen";
	  
	  /*if(action=='add_product')
	  	this.location.href='kassan.php';*/
	  
	  	//document.getElementById("div_kop").innerHTML = "<input name='laggtill' type='button' value=' Köp ' class='meny'  onClick=\'javascript:call_shoppingbag("+products_id+",\"add_product\",1);\'>";
    }

   // client callback function
    function callback_visashoppingbag(str) {
      document.getElementById("div_shoppingbag").innerHTML = str;
	  cursor_clear();
    }
	
  	function call_visaKassa() {
	  cursor_wait();
	  agent.call('','visaBestallForm','callback_produkter');
	  document.getElementById("div_produktgruppnamn").innerHTML = 'Orderbeställning';
    }
	
  	function call_skickaBestallning() {
	  email = document.getElementById("epost").value;
	 // alert(document.getElementById("fornamn").value);
	  cursor_wait(); 
	  
	  agent.call('','shoppingBag','','0','carry_out','0',document.getElementById("fornamn").value, document.getElementById("efternamn").value, document.getElementById("adressrad1").value, document.getElementById("adressrad2").value, document.getElementById("postnr").value, document.getElementById("ort").value, document.getElementById("foretag").value, document.getElementById("telefon").value, email, document.getElementById("nyheter").value, document.getElementById("kommentar").value);
	  
	  document.getElementById("div_produkter").innerHTML = '<center><br><br><br>Tack så mycket för din beställning! Vi mailar dig en orderbeställning inom några dagar.</center>';
	   
	  agent.call('','visaShoppingBagKassan','callback_visashoppingbag');
	  
	  agent.call('','mailaOrder','', email ); 
	  
    }
	
	
	function sendMailtoFriend(product_id)
	{	
		var val1=MM_findObj('mailtofriend_'+product_id+'_from');
		//var val3=document.getElementById('mailtofriend_'+product_id+'_from');
		var val2=MM_findObj('mailtofriend_'+product_id+'_to');
		var epost_from = ''; var epost_to = ''; 
		epost_from = val1.value; epost_to = val2.value;
		p1=epost_from.indexOf('@'); p2=epost_to.indexOf('@'); 
		f1=epost_from.indexOf('.',p1); f2=epost_to.indexOf('.',p2);
		d1=epost_from.length-f1; d2=epost_to.length-f2; 
        if (d1<3 || f1<1 || p1<1 || p1==(epost_from.length-1) || d2<3 || f2<1 || p2<1 || p2==(epost_to.length-1)) 
			alert('Ej giltlig epost. Skriv in både din epost (Från:) och till den som du önskar skicka till (Till:). Tack!');
		else			
		{
			var knapp=MM_findObj('sendMail');
			//alert(knapp.value);
			knapp.value = 'Skickar';
			knapp.disabled = 'true';
			val1.disabled = 'true';
			val2.disabled = 'true';
			agent.call('','skickaMailomProdukt','callback_skickaMailomProdukt',product_id,epost_from,epost_to);
		}
	}
	
   function callback_skickaMailomProdukt(str) {
      document.getElementById("div_mailtofriend").innerHTML = str;
	  cursor_clear();
    }	



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function cursor_wait() {
  document.body.style.cursor = 'wait';
}

function cursor_clear() {
  document.body.style.cursor = 'default';
}


function checkTextFields()
{
	val1 = document.form1.fornamn.value;
	val2 = document.form1.efternamn.value;
	val3 = document.form1.adressrad1.value;
	val4 = document.form1.postnr.value;
	val5 = document.form1.ort.value;
	val6 = document.form1.epost.value;
	val7 = document.form1.telefon.value;
	val8 = document.form1.personnummer.value;
	
	createCookie('foretag',document.form1.foretag.value,1);
	createCookie('fornamn',val1,1);
	createCookie('efternamn',val2,1);
	createCookie('adressrad1',val3,1);
	createCookie('postnr',val4,1);
	createCookie('ort',val5,1);
	createCookie('epost',val6,1);
	createCookie('telefon',val7,1);
	createCookie('personnummer',val8,1);
	createCookie('adressrad2',document.form1.adressrad2.value,1);
	
	if(document.form1.betalsatt[0].checked)
	{
		createCookie('betalsatt',document.form1.betalsatt[0].value,1);
		document.form1.action = "html/betalning.php?action=carry_out";
			document.form1.Slutfor.value="  Fortsätt  ";
	}else{
			document.form1.Slutfor.value="Slutför köp";
	}
	if(document.form1.betalsatt[1].checked)
	{
		createCookie('betalsatt',document.form1.betalsatt[1].value,1);
		document.form1.action = "kvitto.php?action=carry_out";
	}
	if(document.form1.betalsatt[2].checked)
	{
		createCookie('betalsatt',document.form1.betalsatt[2].value,1);
		document.form1.action = "kvitto.php?action=carry_out";
		//document.form1.personnummer.disabled=false;
		//showdiv('personnr');
		//showdiv('personnr_text');
	}
	if(document.form1.betalsatt[3].checked)
	{
		createCookie('betalsatt',document.form1.betalsatt[3].value,1);
		document.form1.action = "kvitto.php?action=carry_out";
	}
	/*else{
		document.form1.personnummer.disabled=true;
		hidediv('personnr');
		hidediv('personnr_text');
		val8 = "lugnt";
	}*/
	
	/*if(document.form1.nyheter.value)
		createCookie('nyheter','checked',999999);
	else
		createCookie('nyheter','',999999);
	*/
	
	/*if ((document.form1.betalsatt[0].checked || document.form1.betalsatt[1].checked || document.form1.betalsatt[2].checked || document.form1.betalsatt[3].checked) && val1.length>0 && val2.length>0 && val3.length>0 && val4.length>0 && val5.length>0 && val6.length>0 && val7.length>0 && val8.length>0){document.form1.Slutfor.disabled=false;}else{document.form1.Slutfor.disabled=true;}
*/


	/* Stäng öppna / slutförmöjlighet */
	// if (document.form1.betalsatt[0].checked || document.form1.betalsatt[1].checked || document.form1.betalsatt[2].checked || document.form1.betalsatt[3].checked) {document.form1.Slutfor.disabled=false;}else{document.form1.Slutfor.disabled=true;}
	

}

function visaInfoForKop()
{
	if(document.form1.Slutfor.disabled)
		alert("Vänligen fyll i samtliga fält med * för att kunna slutföra ditt köp.");
}

function andraTitle(str)
{
	top.document.title = document.title;
}

function enableUppdatera()
{
	upp=MM_findObj('uppdatera');
	upp.disabled=false;
}

function updateQuantity(product_id)
{	
	val=MM_findObj('antal'+product_id);
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (!anum.test(val.value)) 
	{
	 errors='Du måste fylla i ett nummer. (Sätt siffran 0 för att plocka ur produkten ur din shoppingbag)'; alert(errors); 
	 val.value = 1;
	}else{
	 cursor_wait();
  	 call_shoppingbag(product_id,'update_quantity',val.value); 
	 upp=MM_findObj('uppdatera');
	 upp.disabled=true;
	}
	//window.location = "kassan.php?action=update_quantity&products_id="+product_id+"&cart_quantity="+val.value;
}

function updateFraktBetallsatt()
{	
	 val1=document.betalinfo.summa;
	 val2=document.betalinfo.antalartiklar;
  	 agent.call('','fraktBetalsatt','callback_fraktbetal',val1.value,val2.value); 
}

function callback_fraktbetal(str) {
  document.getElementById("div_shoppingbagkassan").innerHTML = str;
}

// client callback function
function callback_shoppingBagKassan(str) {
  document.getElementById("div_shoppingbagkassan").innerHTML = str;
  checkQuantity();
  cursor_clear();
}

function checkQuantity()
{
	agent.call('','kollaOmDetFinnsProdukterIKorgen','callback_kollaOmDetFinnsProdukterIKorgen');
}


function callback_kollaOmDetFinnsProdukterIKorgen(str) {
	if(str>0) 
	{
		checkTextFields();
		updateFraktBetallsatt();
	}else{
		document.form1.Slutfor.disabled=true;
	}		
}


function showdiv(id)
{ 
	if (document.getElementById)
	{ 
		obj = document.getElementById(id); 
		obj.style.display = ""; 
	} 
} 
function hidediv(id)
{ 
	if (document.getElementById)
	{ 
		obj = document.getElementById(id); 
		obj.style.display = "none"; 
	} 
} 


function sendOrder()
{
	document.form1.Slutfor.value = 'Skickar... ';
	//document.form1.Slutfor.disabled = 'true';
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; nm=nm.substring(0,1).toUpperCase()+nm.substring(1); if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+=' - '+nm+' måste innehålla en giltlig e-postadress.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+=' - '+nm+' måste innehålla ett nummer.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+=' - '+nm+' måste vara ett nummer mellan '+min+' och '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += ' - '+nm+' måste fyllas i.\n'; }
  } if (errors) alert('Samtliga fält med * måste fyllas i för att kunna slutföra köpet.\n\n'+errors);
  document.MM_returnValue = (errors == '');
}

