var ProdNo;

function str_grapQuickorderData(ProdNo2, quantity)
{
	ProdNo = ProdNo2;
	
	if( quantity == "" )
		quantity = 1;
	
	if (ProdNo != "")
	{
		xmlHttp = GetXmlHttpObject();
		
		if (xmlHttp == null)
		{
			// no ajax support, submit the form
			var quickForm = document.quickOrderForm;
			quickForm.submit();
			return
		}
		
		var docURL = document.location;
		var docTitle = document.title;
		
		var url = "inc/_ajax.quickorder.php";
		url = url + "?ProdNo=" + ProdNo;
		url = url + "&quantity=" + quantity;
		
		xmlHttp.onreadystatechange=doOutput;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function doOutput()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{
		if (xmlHttp.responseText != "")
		{
			//alert(xmlHttp.responseText);
			
			var dataArr = xmlHttp.responseText.split("\t");
			
			document.getElementById('str_quickOrder_descr').innerHTML = decode64(dataArr[1]);
			document.getElementById('str_quickOrder_stocks').innerHTML = dataArr[2];
			document.getElementById('str_quickOrder_TrInf1').innerHTML = dataArr[3];
			document.getElementById('str_quickOrder_TrInf2').innerHTML = dataArr[5];
			document.getElementById('str_quickOrder_price').innerHTML = dataArr[4];
			document.getElementById('str_quickOrder_minNumPrice').innerHTML = dataArr[6];
			
			// Her setter vi noen variabler slik at produktet blir lagt til i handlekurven.
			document.quickOrderForm.temp_ProdNo.value = dataArr[0];
			document.quickOrderForm.temp_addpid.value = dataArr[0];
			document.quickOrderForm.str_hurtig_prodno.value = dataArr[0];
			document.quickOrderForm.num.value = dataArr[7];
		}
		else
		{
			document.getElementById('str_quickOrder_descr').innerHTML = "<i>Fant ingen produkter</i>";
			document.getElementById('str_quickOrder_stocks').innerHTML = "";
			document.getElementById('str_quickOrder_TrInf1').innerHTML = "";
			document.getElementById('str_quickOrder_TrInf2').innerHTML = "";
			document.getElementById('str_quickOrder_price').innerHTML = "";
		}
	}
}

function GetXmlHttpObject(){
     var objXMLHttp=null
     if(window.XMLHttpRequest){
          objXMLHttp = new XMLHttpRequest()
     }
     else if (window.ActiveXObject){
          objXMLHttp = new ActiveXObject("Microsoft.XMLHttp")
     }
     return objXMLHttp
}

//Heres the decode function
function decode64(inp)
{
	//First things first, set up our array that we are going to use.
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
	"abcdefghijklmnopqrstuvwxyz" + //all lowercase
	"0123456789+/="; // all numbers plus +/=
	
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
	var i = 0; //Position counter

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;

	if (base64test.exec(inp)) { //Do some error checking
		alert("There were invalid base64 characters in the input text.\n" +
		"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
		"Expect errors in decoding.");
	}
	inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do { //Here.s the decode loop.

		//Grab 4 bytes of encoded content.
		enc1 = keyStr.indexOf(inp.charAt(i++));
		enc2 = keyStr.indexOf(inp.charAt(i++));
		enc3 = keyStr.indexOf(inp.charAt(i++));
		enc4 = keyStr.indexOf(inp.charAt(i++));

		//Heres the decode part. There.s really only one way to do it.
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		//Start to output decoded content
		out = out + String.fromCharCode(chr1);

		if (enc3 != 64) {
			out = out + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			out = out + String.fromCharCode(chr3);
		}

		//now clean out the variables used
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //finish off the loop

	//Now return the decoded values.
	return out;
}

//Heres the encode function
function encode64(inp)
{
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
	var i = 0; //Position counter

	do { //Set up the loop here
		chr1 = inp.charCodeAt(i++); //Grab the first byte
		chr2 = inp.charCodeAt(i++); //Grab the second byte
		chr3 = inp.charCodeAt(i++); //Grab the third byte

		//Here is the actual base64 encode part.
		//There really is only one way to do it.
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		//Lets spit out the 4 encoded bytes
		out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
		keyStr.charAt(enc4);

		// OK, now clean out the variables used.
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";

	} while (i < inp.length); //And finish off the loop

	//Now return the encoded values.
	return out;
}
