function getHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
    try {
    	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    	try {
    		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    	} catch (E) {
    		xmlhttp = false;
    	}
	}
  	@else
  	xmlhttp = false;
  	@end @*/
  	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
  	{
    	try 
    	{
    		xmlhttp = new XMLHttpRequest();
    	} catch (e) 
    	{
      		xmlhttp = false;
    	}
  	}
  		return xmlhttp;
}
	var http = getHTTPObject();

function SendRequest(url) 
{
	XmlHttpGET(http, url);
}

function XmlHttpGET(http, url) {
	http.open('GET', url, true);
	http.send(null);
}

function SaveData(action,kunde,arrname,navn,val)
{
	SendRequest("inc_action.php?action=" + action + "&navn=" + navn + "&val=" + val + "&arrname=" + arrname + "&kunde=" + kunde + "&PHPSESSID=" + sessionid);
}
function DestroySession()
{
	SendRequest("inc_action.php?action=logout");
}

function pop_vindu(url,navn,w,h)
{
	var popit = window.open(url,navn,'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+',left=50,screenX=50,top=50,screenY=50');
}

function kost_add(felt)
{
	var sum	= Number(document.getElementById('put_'+felt).value);
	var now = Number(document.getElementById('save_'+felt).value);
	
	
	if(felt == "divk" || felt == "fri" || felt == "divkost")
	{
		var total = now + sum;
		var out   = total.toFixed(2);
		document.getElementById('save_'+felt).value = out;
		document.getElementById('put_'+felt).value = "0.00";	
	}
	else
	{
		document.getElementById('save_'+felt).value = Number(now) + Number(sum);
		document.getElementById('put_'+felt).value = 0;
	}
}

var kampanjeurl	= "inc_action.php?action=kampanje&p=";
function validStatus(knd)
{
	if(!isWorking && http)
	{
		var tempfelt	= document.getElementById('save_kampanje');
		var wkampanje	= tempfelt.options[tempfelt.selectedIndex].value;
		var cust		= "&k=";
		http.open("GET", kampanjeurl + wkampanje + cust + knd, true);
		http.onreadystatechange = handleKampanjestatus;
		isWorking = true;
		http.send(null);
	}
}

function handleKampanjestatus()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			var field = document.getElementById('save_status');
			for(var i=0;i<field.options.length;i++)
			{
				if(field.options[i].value == results)
					field.options[i].selected = true;
					
			}
			isWorking = false;
		}
	}
}
	

var editurl = "internal_public.php?action=edit";
function editpub(part, field, knr, kunde)
{
	var tempfelt	= document.getElementById(field);
	var nowstatus	= tempfelt.options[tempfelt.selectedIndex].value;
		
	if(part == 'stat')
	{
		var mode = "&mode=status";
		var uid	 = "&knr="+knr;
		var nst	 = "&status="+nowstatus;
		var fld	 = "&field="+field;
		var knd	 = "&knd="+kunde;
		http.open("GET", editurl + mode + uid + nst + fld + knd, true);
		http.onreadystatechange = handleeditpub;
		isWorking = true;
		http.send(null);
	}
}
			
function handleeditpub()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			document.getElementById(results).style.backgroundColor = '#CCCCCC';
			
			isWorking = false;
		}		
	}
}

	

function pop_status(url,navn,w,h)
{
	var getval	= document.getElementById('save_status');
	var isopen	= getval.options[getval.selectedIndex].value;
	if(isopen == "P" || isopen == "S")
	{
		var popit 	= window.open(url,navn,'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+',left=50,screenX=50,top=50,screenY=50');
	}
	else
	{
		return;
	}
}

function pop_status2(url,navn,w,h)
{
	var getval	= document.getElementById('save_status');
	var isopen	= getval.options[getval.selectedIndex].value;
	var addvar	= "&nystatus="+isopen;
	if(isopen == "P" || isopen == "S")
	{
		var popit 	= window.open(url + addvar,navn,'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+',left=50,screenX=50,top=50,screenY=50');
	}
	else
	{
		return;
	}
}

function pop_search(url,navn,w,h)
{
	var popit = window.open(url,navn,'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h);
}

var url = "inc_action.php?action=postnummer&p=";
function finnSted() 
{
	if (!isWorking && http) 
	{
		var pnrVerdi = document.getElementById("save_postnr").value;
  		http.open("GET", url + escape(pnrVerdi), true);
  		http.onreadystatechange = handleHttpResponse;
  		isWorking = true;
  		http.send(null);
	}
}

function handleHttpResponse() 
{
	if (http.readyState == 4) 
	{
		if (http.responseText.indexOf('invalid') == -1) 
	  	{
			results = http.responseText;
			document.getElementById('sted').value = results;
			if(results == "Ugyldig" || results == "UKJENT POSTNUMMER")
			{
				document.getElementById('pnrlink').style.display = 'block';
				document.getElementById('warning').value = 'Postnummer er feil / mangler.';
			}
			else
			{
				document.getElementById('pnrlink').style.display = 'none';
				document.getElementById('warning').value = '';
			}
			isWorking = false;
		}
    }
}
var isWorking = false;

var giverurl = "inc_action.php?action=giverinfo&abonr=";
var giveradd = "&k=";

function finnGiver()
{
	if(!isWorking && http)
	{
		var abo 	= document.getElementById('regn_knr').value;
		var kunde	= document.getElementById('hidden_kunde').value;
		
		http.open("GET", giverurl + abo + giveradd + kunde, true);
		http.onreadystatechange = handleHttpGiver;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpGiver()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			document.getElementById('giverinfo').style.display = 'block';
			document.getElementById('show_giver').value = results;
			isWorking = false;
		}
	}
}

var tempurl = "inc_action.php?action=tempabo&abonr=";
var tempadd = "&k=";

function finnTempabo()
{
	if(!isWorking && http)
	{
		var abo 	= document.getElementById('import_knr').value;
		var kunde	= document.getElementById('hidden_kunde').value;
		
		http.open("GET", tempurl + abo + tempadd + kunde, true);
		http.onreadystatechange = handleHttpTempabo;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpTempabo()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			document.getElementById('giverinfo').style.display = 'block';
			document.getElementById('show_giver').value = results;
			isWorking = false;
		}
	}
}

var url0 = "inc_action.php?action=getnum&p=";
function finnNummer()
{
	if(!isWorking && http)
	{
		var nummerVerdi = document.getElementById('save_kode').value;
		http.open("GET", url0 + escape(nummerVerdi), true);
		http.onreadystatechange = handleHttpFinnNr;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpFinnNr()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			document.getElementById('save_nummer').value = results;
			isWorking = false;
		}
	}
}

var url1 = "inc_action.php?action=getdate&p=";
function finnDato()
{
	if(!isWorking && http)
	{
		var datoVerdi = document.getElementById('save_startdato').value;
		http.open("GET", url1 + escape(datoVerdi), true);
		http.onreadystatechange = handleHttpDato;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpDato()
{
	if(http.readyState == 1)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			document.getElementById('show_dato').value = results;
			isWorking = false;
		}
	}
}

var url2 = "inc_action.php?action=pnr_soek&p=";
function finnPNR()
{
	if(!isWorking && http)
	{
		var pnrVerdi = document.getElementById('save_postnummer').value;
		http.open("GET", url2 + escape(pnrVerdi), true);
		http.onreadystatechange = handleHttpPnr;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpPnr()
{
	if(http.readyState == 1)
	{
		document.getElementById('save_sted').value = "Henter...";
		document.getElementById('save_kommune').value = "Henter...";
		document.getElementById('save_fylke').value = "Henter...";
	}
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText.split(",");
			if(results[0] != "" && results[1] !="")
			{
				document.getElementById('save_sted').value = results[0];
				document.getElementById('save_kommune').value = results[1];
				document.getElementById('save_fylke').value = results[2];
			}
			else
			{
				document.getElementById('save_sted').value = "Ugyldig";
				document.getElementById('save_kommune').value = "Ugyldig";
				document.getElementById('save_fylke').value = "Ugyldig";
			}
			isWorking = false;
		}
	}
}

var url3 	 = "inc_action.php?action=newdate&p=";
var fromdate = "&f=";
function futureDate()
{
	if(!isWorking && http)
	{
		var days = document.getElementById('save_days').value;
		var from = document.getElementById('save_fakdate').value;
		http.open("GET", url3 + escape(days) + fromdate + from, true);
		http.onreadystatechange = handleHttpFuture;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpFuture()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			document.getElementById('save_forfall').value = results;
			isWorking = false;
		}
	}
}

var dateurl		= "inc_action.php?action=fixdate&dato=";
function fixDate()
{
	if(!isWorking && http)
	{
		var date	= document.getElementById('save_fakdate').value;
		http.open("GET", dateurl + date, true);
		http.onreadystatechange = handleHttpFixDate;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpFixDate()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			document.getElementById('save_fakdate').value = results;
			isWorking = false;
		}
	}
}

var select_tab = ""
var tchecked   = false

function handle_tab(aobject)
{
	select_tab = aobject.href
	tchecked=(document.tabcontrol && document.tabcontrol.tabcheck.checked) ? true: false
	if(document.getElementById && !tchecked)
	{
		var tabobj = document.getElementById("tab_basic")
		var tabobjlinks = tabobj.getElementsByTagName("A")
		for(i=0; i<tabobjlinks.length; i++)
			tabobjlinks[i].className=""
			aobject.className="current"
			document.getElementById("internal").src=select_tab
			return false
	}
	else
	return true
}

function pop_form(myform,wname,actionurl,h,w)
{
	if(!window.focus)return true;
	window.open('',wname, 'height='+h+',width='+w+',scrollbars=auto');
	myform.target=wname;
	myform.action= actionurl;
	return true;
}

function pop_baj(myform,wname,w,h)
{
	if(!window.focus)return true;
	window.open('',wname,'height='+h+',width='+w+',scrollbars=yes');
	myform.target = wname;
	return true;
}

function pop_pdf(myform,wname,w,h)
{
	if(!window.focus)return true;
	window.open('',wname,'height='+h+',width='+w+',scrollbars=auto');
	myform.target = wname;
	myform.action = 'pdf.php';
	return true;
}

function doMath(felt1,felt2)
{
	var en 	= eval(felt1);
	var to 	= eval(felt2);
	var res	= en * to;
	
	felt2.value = res;
}

function summarise(rows)
{
	var sum  = 0;
	var som	 = 0;
	for(var i = 0; i < rows; i++)
	{
		var ent	= Number(document.getElementById('save_sum_'+i).value);
		var ont = Number(document.getElementById('save_totalrow_'+i).value);
		sum += ent;
		som += ont;
	}
	document.getElementById('save_total_ink').value = som;
	document.getElementById('save_total_eks').value = sum;
}

function doMva(row)
{
	var felt	= document.getElementById('save_konto_'+row);
	var dest	= felt.options[felt.selectedIndex].value;
	var array	= dest.split("|");
	
	if(array[1] == 1)
	{
		document.getElementById('save_totalrow_'+row).value = Math.round((document.getElementById('save_sum_'+row).value * 1.25)*100)/100;
	}
	else
	{
		document.getElementById('save_totalrow_'+row).value = Math.round((document.getElementById('save_sum_'+row).value)*100)/100;
	}
}

function incMva(row)
{
	var box		= document.getElementById('save_mva_'+row);
	var rad		= document.getElementById('save_sum_'+row);
	var rod		= document.getElementById('save_totalrow_'+row);
	if(box.checked == false)
	{
		rad.value	= Math.round((rad.value * 1.25)*100/100);
		rod.value	= Math.round((rod.value * 1.25)*100/100);
	}
	else
	{
		rad.value	= Math.round((rad.value / 1.25)*100/100);
		rod.value	= Math.round((rod.value / 1.25)*100/100);
	}
}

var url4 = "inc_action.php?action=convert&p=";
function convertCD(row)
{
	if(!isWorking && http)
	{
		var add			= "&r=";
		var rad			= row;
		var felt 		= document.getElementById('save_pris_'+row);
		var nummerVerdi = felt.value;
		http.open("GET", url4 + escape(nummerVerdi) + add + escape(rad) , true);
		http.onreadystatechange = handleHttpConvert;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpConvert()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText.split(",");
			document.getElementById('save_pris_'+results[1]).value = results[0];
			isWorking = false;
		}
	}
}

function convertCD2(row)
{
	if(!isWorking && http)
	{
		var add			= "&r=";
		var rad			= row;
		var felt 		= document.getElementById('save_antall_'+row);
		var nummerVerdi = felt.value;
		http.open("GET", url4 + escape(nummerVerdi) + add + escape(rad) , true);
		http.onreadystatechange = handleHttpConvert2;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpConvert2()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText.split(",");
			document.getElementById('save_antall_'+results[1]).value = results[0];
			isWorking = false;
		}
	}
}

function avsluttabo(url,navn,w,h) 
{
	if (document.getElementById("save_avslutt").checked) 
	{
		var popit 	= window.open(url,navn,'toolbar=no,directories=no,menubar=no,scrollbars=yes,resizable=no,width='+w+',height='+h+',left=50,screenX=50,top=50,screenY=50');
		return false;
	}
}

var producturl	= "inc_action.php?action=produkt&p=";
function findProdukt(row,kunde)
{
	if(!isWorking && http)
	{
		var kid		= kunde;
		var rad		= row;
		var add		= "&r=";
		var kud		= "&k=";
		var produkt	= document.getElementById('save_kode_'+rad).value;
		http.open("GET", producturl + produkt + add + rad + kud + kid, true);
		http.onreadystatechange = handleProduktSearch;
		isWorking = true;
		http.send(null);
	}
}
function handleProduktSearch()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results	= http.responseText.split("|");
			konto	= results[3].replace(/-/,"|");
			document.getElementById('save_produkt_'+results[1]).value = results[0];
			document.getElementById('save_pris_'+results[1]).value = results[2];
			document.getElementById('save_konto_'+results[1]).value = konto;
			isWorking = false;
		}
	}
}

var feedbackurl	= "inc_action.php?action=feedback&p=";
function mailFeedback(kunde,knd)
{
	if(!isWorking && http)
	{
		var kid		= kunde;
		var temp 	= "&k=";
		http.open("GET", feedbackurl + kid + temp + knd, true);
		http.onreadystatechange = handleFeedbackretur;
		isWorking	= true;
		http.send(null);
	}
}

function handleFeedbackretur()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			if(results == "Ok")
			{
				document.getElementById('feedb').value = 'Sendt';
			}
			else
			{
				document.getElementById('feedb').value = 'Sendt';
			}
			isWorking = false;
		}
	}
}

var mailurl	= "inc_action.php?action=mail&p=";
function mailRetur(kunde,knd,kind,altknr,sms)
{
	if(!isWorking && http)
	{
		var kid		= kunde;
		var temp	= "&k=";
		var what	= "&w=";
		var alt		= "&q=";
		var asms	= "&s=";
		var cust	= knd;
		var wtyp	= kind;
		var altk	= altknr;
		var issms	= sms;
		http.open("GET", mailurl + kid + temp + cust + what + wtyp + alt + altk + asms + issms, true);
		if(kind == 'mottaker')
		{
			http.onreadystatechange = handleMailretur;
		}
		else
		{
			http.onreadystatechange = handleMailretur2;
		}
		isWorking = true;
		http.send(null);
	}
}

function handleMailretur()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			if(results == "Ok")
			{
				document.getElementById('mail').value = 'Sendt';
			}
			else
			{
				document.getElementById('mail').value = 'Error';
			}
			isWorking = false;
		}
	}
}

function handleMailretur2()
{
	if(http.readyState == 4)
	{
		if(http.responseText.indexOf('invalid') == -1)
		{
			results = http.responseText;
			if(results == "Ok")
			{
				document.getElementById('mail2').value = 'Sendt';
			}
			else
			{
				document.getElementById('mail2').value = 'Error';
			}
			isWorking = false;
		}
	}
}

function calcMVA(row)
{
	var antall	= document.getElementById('save_antall_'+row).value;
	var stk		= document.getElementById('save_pris_'+row).value;
	var felt	= document.getElementById('save_konto_'+row);
	var dest	= felt.options[felt.selectedIndex].value;
	var array	= dest.split("|");
	var ismva	= array[1];
	
	var worksum = antall * stk;
	var mva	= (worksum * 0.25)*100/100;
	
	if(document.getElementById('save_mva_'+row).checked)
	{
		if(ismva == 0)
		{	
			document.getElementById('save_sum_'+row).value = (worksum - mva);
			document.getElementById('save_totalrow_'+row).value = (worksum - mva);
		}
		if(ismva == 1)
		{
			document.getElementById('save_totalrow_'+row).value = worksum;
			document.getElementById('save_sum_'+row).value = Math.round(worksum - mva);
		}
	}
	else
	{
		if(ismva == 0)
		{
			document.getElementById('save_totalrow_'+row).value = worksum;
			document.getElementById('save_sum_'+row).value = worksum;
		}
		if(ismva == 1)
		{
			document.getElementById('save_totalrow_'+row).value = Math.round(worksum - mva);
			document.getElementById('save_sum_'+row).value = Math.round(worksum - mva);
		}
	}
}

var highlightcolor="#FFFFCC";
var ns6=document.getElementById&&!document.all;
var previous='';
var eventobj;

var intended=/INPUT|TEXTAREA|SELECT|OPTION/;

function checkel(which)
{
	if (which.style&&intended.test(which.tagName))
	{
		if (ns6&&eventobj.nodeType==3)
			eventobj=eventobj.parentNode.parentNode
			return true
	}
	else
	return false
}

function highlight(e)
{
	eventobj=ns6? e.target : event.srcElement
	if (previous!='')
	{
		if (checkel(previous))
			previous.style.backgroundColor=''
			previous=eventobj
		if (checkel(eventobj))
			eventobj.style.backgroundColor=highlightcolor
	}
	else
	{
		if (checkel(eventobj))
			eventobj.style.backgroundColor=highlightcolor
			previous=eventobj
	}
}
	
var highlightcolor="lightyellow"

var ns6=document.getElementById&&!document.all
var previous=''
var eventobj

var intended=/INPUT|TEXTAREA|SELECT|OPTION/

function checkel(which){
if (which.style&&intended.test(which.tagName)){
if (ns6&&eventobj.nodeType==3)
eventobj=eventobj.parentNode.parentNode
return true
}
else
return false
}

function highlight(e)
{
	eventobj=ns6? e.target : event.srcElement
	if (previous!='')
	{
	if (checkel(previous))
		previous.style.backgroundColor=''
		previous=eventobj
	if (checkel(eventobj))
		eventobj.style.backgroundColor=highlightcolor
	}
	else
	{
		if (checkel(eventobj))
		eventobj.style.backgroundColor=highlightcolor
		previous=eventobj
	}
}

function kundevalidate()
{
	if(""==document.getElementById('warning').value)
	{
		return true;
	}
	else
	{
		alert("Vennligst rett opp feilene");
		return false;
	}
}

function validcustomer()
{
	var sel	= document.getElementById('kildenavn');
	var sal	= document.getElementById('kampanje');
	var err = "Vennligst korriger: ";
	var errmsg	= false;
	if(sel.value == '')
	{
		err += "Kilde ";
		errmsg	= true;
	}
	if(sal.value == '')
	{
		err += "Kampanje ";
		errmsg	= true;
	}
	if(errmsg)
	{
		alert(err);
		return false;
	}
	else
	{
		return true;
	}
}
