var http;
var addr = "lib/ajax_funcs.php?";
var addrSess = "lib/ajax_Session.php?";
var addrPrint = "print.php?";
var isWorking = false;
var divTag ;


function handleGetData() {
  id = getObject(divTag);
  if (http.readyState == 4) {
  	if (http.status == 200) {
	  resp = http.responseText;
	    layerContent = resp ;
	  id.innerHTML = layerContent ;
      isWorking = false;
      id.style.display = 'block';
	}
     else
	alert(http.status + ' ???\n' + addr);
    }
}

/*
Sender oss videre til ajax_funcs.php med riktige parametre
*/
function getData(v,tag){
    divTag = tag;
    http = false;
              
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			http = new XMLHttpRequest();
        } catch(e) {
			http = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	http = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		http = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		http = false;
        	}
		}
    }
	if(http) {
		http.onreadystatechange = handleGetData;
		http.open("GET", addr + v, true);
		http.send("");
	}
}

function getObject( obj ) {
  if ( document.getElementById ) {
    if (obj = document.getElementById( obj )) {

    } else obj = null;
  } else if ( document.all ) {
    if (obj = document.all.item( obj )) {

    } else obj = null;
  } else {
    obj = null;
  }
  return obj;
}

/*
Funksjon for å legge til en artikkel i handlevogna. 
Innparameter id er artikkelens id. Kaller ajax_Session.php som oppdaterer
en sessionsvariabel med denne artikkel-id'en.
*/
function addArtikkel(id) {	
    httpSess = false;
    // branch for native XMLHttpRequest object 
    var antallFelt = document.getElementById(id);    
    if(isNumeric(antallFelt.value)==true) { 
    	var antall = antallFelt.value * 1;	
	    if(window.XMLHttpRequest) {
		try {
				httpSess = new XMLHttpRequest();
				totaltantall = document.getElementById('handlevognstatusantall').innerHTML * 1;
				document.getElementById('handlevognstatusantall').innerHTML = (totaltantall + antall);
		} catch(e) {
				httpSess = false;
		}
	    // branch for IE/Windows ActiveX version
	    } else if(window.ActiveXObject) {
		try {
			httpSess = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				httpSess = new ActiveXObject("Microsoft.XMLHTTP");          		
				totaltantall = document.getElementById('handlevognstatusantall').innerHTML * 1;
				document.getElementById('handlevognstatusantall').innerHTML = (totaltantall + antall);
			} catch(e) {
				httpSess = false;
			}
			}
	    }

		if(httpSess) {
			httpSess.open("GET", addrSess + "art="+id+"&antall="+antall , true);
			httpSess.send("");
		}
	}
}

/*
Funksjon for å oppdatere antallet av en artikkel som skal bestilles
*/
function updateArtikkel(id,obj,tag) {
	divTag = tag;
    http = false;
    
    //handle inparam obj as an input-object:
    if(obj.tagName == "INPUT") {
    	if (obj.type == "text") {    		
           getstr = "&antall=" + obj.value;
           v = "a=update&id="+id+getstr;
        }
    }
    if(isNumeric(obj.value)==true) {
	    // branch for native XMLHttpRequest object
	    if(window.XMLHttpRequest) {
		try {
				http = new XMLHttpRequest();
		} catch(e) {
				http = false;
		}
	    // branch for IE/Windows ActiveX version
	    } else if(window.ActiveXObject) {
		try {
			http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				http = false;
			}
			}
	    }
		if(http) {
			http.open("GET", addrSess + v, true);
			http.send("");
		}
	}
}

function sok(nr, a, o, tag) {
	var getstr = "";
	var v = "";
   	
	if(a.checked)
		getstr = "type=a&";
	else if(o.checked)
		getstr = "type=o&";
	
	getstr = getstr+nr.name+"="+nr.value;
	v = "a=sokres&"+getstr;
          
	getData(v, tag);
}

function sendBestilling(navn,adr,nr,sted,orgnr,knr,email,merket,tag) {
	var fields = "navn=" + navn + "&adr=" + adr + "&nr=" + nr + "&sted=" + sted + "&orgnr=" + orgnr + "&knr=" + knr + "&email=" + email + "&merket=" + merket;
	if(navn == "" || nr == "") {
		getData('a=bekreft&failed=true&' + fields, tag);
	}else {
		v = "a=send&" + fields;
		getData(v, tag);
	}
}

function isNumeric(strString)   
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;	
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);      
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
function checkRequired(knr, email, tag) {
	var fields = "email=" + email + "&knr=" + knr;
	if(email == "" || knr == "") {
		getData('a=pris&failed=true&' + fields, tag);
	}else {
		v = "a=pris&failed=false&" + fields;
		getData(v, tag);
	}
}
