
function miljoredirect(){
    window.location="http://webshop.susoft.com/miljotransportb2c/showlogon.do";
}

/*------------------- AJAX --------------------*/
var req;
var arrayProg;
var pricebookGlobal;

function runAjaxrequest(picurl) {
   var url2 = encodeURI(picurl);
	//alert("url:"+url2);
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   progressbar();
   req.open("GET", url2, true);
   req.onreadystatechange = callback;
   req.send(null);
}
function forgottenPassword(){
	if (!window.focus) return true;

	var windowW = 330;
    var windowH = 200;

    var left = (screen.width-windowW)/2;
    var top = (screen.height-windowH)/2;

    var props =  'height='+windowH+', ';
    	props += 'width='+windowW+', ';
        props += 'top='+top+', ';
        props += 'left='+left+', ';
        props +='scrollbars=no, ';
        props +='resizable=no';
        window.open(getPath()+'/sendpassword.do','ForgottenPassword', props);
        return false;
}
function resetcache(){
	alert("Is reseting the cache...");
	window.location=getPath()+"/parameters/resetcache.do";
}

function callback() {
	var statusdiv = document.getElementById("statusbar");
	if(req.readyState == 0){
		//progressbar(0);
	}
	if(req.readyState == 1){
		//progressbar(1);
	}
	if(req.readyState == 2){
		//progressbar(2);
	}
	if(req.readyState == 3){
		//progressbar(3);
	}
    if (req.readyState == 4) {
        if (req.status == 200) {
        	//alert("req.text:"+req.responseText);
        	endProgressbar();
            setComps();
        }
    }
}

/*-------------------- END AJAX ---------------------*/

/*--------------------- XML parser --------------------*/
function ajaxwithout(){
	
	var urlSku = "http://localhost:8080/susoft/products/compVariant.do?selectedvariantid=34S%20%202%20%201";
	//alert("urlSku:"+urlSku);
	addScript(urlSku);
}
function addScript(url) {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    document.body.appendChild(script);
}
/*function json_callback(innhold){
	alert("innh.xcomp:"+innhold.xcomp)
}*/


function initProgress(){
	//alert("initProgress() 1")
	//var progressPic = document.getElementById("statusbarimg");
	arrayProg = new Array();
	arrayProg[0] = new Image();
	arrayProg[0].src = getPath()+"/images/step_0.jpg";
	
	arrayProg[1] = new Image();
	arrayProg[1].src = getPath()+"/images/step_1.jpg";
	
	arrayProg[2] = new Image();
	arrayProg[2].src = getPath()+"/images/step_2.jpg";
	
	arrayProg[3] = new Image();
	arrayProg[3].src = getPath()+"/images/step_3.jpg";
	
	arrayProg[4] = new Image();
	arrayProg[4].src = getPath()+"/images/step_4.jpg";
}


function endProgressbar(){
	var statusbarImg2 = document.getElementById("statusbarimg");
	statusbarImg2.src = getPath()+"/images/pixel.gif";
}
function progressbar(){
	progressbar2()
}
function progressbar2(){

	var statusbarImg2 = document.getElementById("statusbarimg");
	
	statusbarImg2.src = getPath()+"/images/ajax-loader.gif";
	//statusbarImg2.src = arrayProg[numb].src;
}
/*function setComps2(){
	var evaluert = eval("(" + req.responseText + ")");
}*/

function setComps(){
	try{
	/*var xcompXML = req.responseXML.getElementsByTagName('xcomp')[0].firstChild.nodeValue;
	var ycompXML = req.responseXML.getElementsByTagName('ycomp')[0].firstChild.nodeValue;
	var zcompXML = req.responseXML.getElementsByTagName('zcomp')[0].firstChild.nodeValue;

	var variantexist = req.responseXML.getElementsByTagName('variantexist')[0].firstChild.nodeValue;
	var variantonstock = req.responseXML.getElementsByTagName('variantonstock')[0].firstChild.nodeValue;
	var variantprice = req.responseXML.getElementsByTagName('variantprice')[0].firstChild.nodeValue;
	var selectedvariantidXML = req.responseXML.getElementsByTagName('selectedvariantid')[0].firstChild.nodeValue;*/
	var data = eval("(" + req.responseText + ")");
	
	var xcompXML = data.xcomp;
	var ycompXML = data.ycomp;
	var zcompXML = data.zcomp;
	var variantexist = data.variantexist;
	var variantonstock = data.variantonstock;
	var variantprice = data.variantprice;
	var variantwsprice = data.variantwsprice;
	var selectedvariantidXML = data.selectedvariantid;
	var numbercompsinuse = data.numberCompsInUse;
	var islicenses = data.islicenses;
	//var allownegativestock = data.allownegativestock;
	
	}catch(em){};

	//change the url when maximazing
	if(document.getElementById('skumainpiclink') != null)
		document.getElementById('skumainpiclink').href=getPath()+"/imageservlet?type=product&imagesize=large&id="+selectedvariantidXML+"&pricebook="+pricebookGlobal;;
	
	nronstock = variantonstock;
	if(variantexist == "true"){
		var buydiv = document.getElementById("productonstockimgdiv");
		buydiv.style.visibility = "visible";
		changePic(selectedvariantidXML);
		document.getElementById("pricevariantspan").innerHTML = variantprice;
		try{
			document.getElementById("productid").innerHTML = "Vareid: "+selectedvariantidXML;
		}catch(em){}
		
	//former pricevariant
		if(document.getElementById("pricevariantspan").style.visibility == 'hidden'){
			document.getElementById("pricevariantspan").style.visibility   = 'visible';//former pricevariant
		}
		if(document.getElementById("pricevariantending").style.visibility = 'hidden'){
			document.getElementById("pricevariantending").style.visibility = 'visible';
		}
		shownumberonstock(variantonstock, variantexist, islicenses);
	}
	else if((numbercompsinuse == 3 && threecompcheck(xcompXML, ycompXML, zcompXML)) ||
		(numbercompsinuse == 2 && twocompcheck(xcompXML, ycompXML)) ||
		(numbercompsinuse == 1 && onecompcheck(xcompXML))){

		shownumberonstock(variantonstock, variantexist, islicenses);
		document.getElementById("pricevariantspan").style.visibility = "hidden";//former pricevariant
		document.getElementById("pricevariantending").style.visibility = 'hidden';
		document.getElementById("productonstockimg").style.visibility = "hidden";
		
		insertPic("fillallvariants.jpg");
	}
	else{
		document.getElementById("pricevariantspan").innerHTML = " Denne varianten finnes ikke";//former pricevariant
		document.getElementById("pricevariantending").style.visibility = 'hidden';
		shownumberonstock(variantonstock, variantexist, islicenses);
		document.getElementById("productonstockimg").style.visibility = "hidden";
		insertPic("variant_not_exist.jpg");
	}
	
	

	try{ 
		var optsX = document.getElementById("xcomp").options;
		//var dropDownvalue = compvalueOrfullprodid(xcompXML.trim(), selectedvariantidXML);
		for(var i=0;i<optsX.length; i++){
			if(optsX[i].value.toUpperCase() == xcompXML.trim()){
				optsX.selectedIndex = i;
			}
		}
	}catch(em){ }
	try{
		var optsY = document.getElementById("ycomp").options;
		for(var i=0;i<optsY.length; i++){
			//alert("optsY[i].value:"+optsY[i].value.toUpperCase()+"   ycompXML.trim():"+ycompXML.trim());
			if(optsY[i].value.toUpperCase() == ycompXML.trim()){
				optsY.selectedIndex = i;
			}
		}
	}catch(em){ }
	try{ 
		var optsZ = document.getElementById("zcomp").options;
		
		for(var i=0;i<optsZ.length; i++){
			if(optsZ[i].value.toUpperCase() == zcompXML.trim()){
				optsZ.selectedIndex = i;
			}
		}
	}catch(em){ }
	
	
}
function threecompcheck(x, y, z){
	return (x == 0 || x == -1 || y == 0 || y == -1 || z == 0 || z == -1);
}
function twocompcheck(x, y){
	return (x == 0 || x == -1 || y == 0 || y == -1);
}
function onecompcheck(x){
	return (x == 0 || x == -1);
}
/*--------------------- diverse script -----------------*/
/*function changeSizeMama(){
var height = document.getElementById("skumainpic").height;
	var width  = document.getElementById("skumainpic").width;

	if(width > 215)
		document.getElementById("skumainpic").width="215";
}*/

function changeSize(){
	
	var height = document.getElementById("skumainpic").height;
	var width  = document.getElementById("skumainpic").width;

	if(width > 280)
		document.getElementById("skumainpic").width="290";
}
;
function insertPic(image){
	var mainpic = document.getElementById("skumainpic");
	var picurl = getPath()+"/images/"+image;
	//variant_not_exist.jpg";
	var url2 = encodeURI(picurl);
	mainpic.src = url2;
	variantexist="false";
}

function skuselect(evt, pricebook) {
  document.forms[0].motherid.value=evt;
  document.forms[0].pricebook.value=pricebook;
  document.forms[0].submit();
}
/* Sørger for på mouseover på similar og associated */
function changeStyle(row){
	row.style.backgroundColor="#7B7B7B";
	row.style.color = "#fff";
}
function similarReg(id, pricebook, path){
	var url = path+"/products/selectproduct.do?id="+id+"&pricebook="+pricebook;
	
	//products/selectproduct.do?id=9785720&pricebook=18
	
	document.location.href = url;
}


function backToNormalLight(row){
	row.style.backgroundColor="#fff";
	row.style.color = "#666";
}
function backToNormalDark(row){
	row.style.backgroundColor="#eee";
	row.style.color = "#666";
}




var selectedPicture2;
var variantexist="false";
var nronstock=0;

function changemainpic(picid, pricebook){

	var mainpic = document.getElementById("skumainpic");
	var picurl = getPath()+"/imageservlet?type=product&imagesize=large&id="+picid+"&pricebook="+pricebook;
	//setTimeout("progressbar2("+numb+")",0);
	var url2 = encodeURI(picurl);

	mainpic.src = url2;
	selectedPicture2=picid;
	var urlSku = getPath()+"/products/compVariant.do?selectedvariantid="+picid+"&pricebook="+pricebook;
	
	//change the url when maximazing
	//document.getElementById('skumainpiclink').href=getPath()+"/imageservlet?type=product&imagesize=large&id="+picid+"&pricebook="+pricebook;;
	pricebookGlobal = pricebook;
	
	
	runAjaxrequest(urlSku);
}
function changePic(picid){

	var mainpic = document.getElementById("skumainpic");
	var picurl = getPath()+"/imageservlet?type=product&imagesize=large&id="+picid;
	var url2 = encodeURI(picurl);
	mainpic.src = url2;
	selectedPicture2=picid;
	variantexist="true";
}

function changeXcomp(pricebook){
	var xcomptmp = document.getElementById("xcomp");
	var xcomp = xcomptmp.options[xcomptmp.selectedIndex].value;
	var urlSku = getPath()+"/products/changeComps.do?xcomp="+xcomp+"&pricebook="+pricebook;
	
	pricebookGlobal = pricebook;
	
	runAjaxrequest(urlSku);
}

function changeYcomp(pricebook){
	var ycomptmp = document.getElementById("ycomp");
	var ycomp = ycomptmp.options[ycomptmp.selectedIndex].value;
	var urlSku = getPath()+"/products/changeComps.do?ycomp="+ycomp+"&pricebook="+pricebook;
	
	pricebookGlobal = pricebook;
	
	runAjaxrequest(urlSku);
}
function changeZcomp(pricebook){
	var zcomptmp = document.getElementById("zcomp");
	var zcomp = zcomptmp.options[zcomptmp.selectedIndex].value;
	var urlSku = getPath()+"/products/changeComps.do?zcomp="+zcomp+"&pricebook="+pricebook;
	
	pricebookGlobal = pricebook;
	
	runAjaxrequest(urlSku);
}

function addtocartSku(pricebook, allownegativestock){
	if(variantexist== "false")
		alert("Du må velge en variant først");
	else
		addtocart(selectedPicture2, pricebook, allownegativestock);
}

function addtocart(id, pricebook, allownegativestock) {

	
	var onStock = nronstock;
	var qty = document.getElementById("qtyID").value;
	
  	//alert("document.getElementById(qty).value:"+document.getElementById("qtyID").value);
  	if (qty==""){
    	qty = 1;
   	}

   	if(onStock < 1 && !allownegativestock){
   		alert("Varen er ikke på lager og kan derfor ikke kjøpes");
   	}
   	else if (qty > onStock && !allownegativestock){
   		alert("Du har valgt for mange varer, det er ikke så mange eksemplarer igjen av denne varianten igjen på lager");
   	}
   	else{
		var qtyVal = parseFloat(qty);
	    if ( (qtyVal>0 && qtyVal<=onStock) || allownegativestock){
	      document.cartForm.id.value=id;
	      document.cartForm.qty.value=qty;
	      document.cartForm.pricebook.value=pricebook;
	      document.cartForm.submit();
	      
	    }    
	}
}
function jumpPage() {
		var newLoc = document.getElementById("zcomp");
		var newPage = newLoc.options[newLoc.selectedIndex].value;
		document.forms[2].zcomp.value = newPage;
		document.forms[2].submit();
	}

function showImage(){
	var id;
	if(typeof selectedPicture2 != "undefined"){
		id=selectedPicture2;
		
	}
	else{
		id = document.forms[0].motherid.value;
	}
    	if (!window.focus) return true;
		var windowW = 800;
        var windowH = 652;

        var left = (screen.width-windowW)/2;
        var top = (screen.height-windowH)/2;

        var props =  'height='+windowH+', ';
        	props += 'width='+windowW+', ';
            props += 'top='+top+', ';
            props += 'left='+left+', ';
            props +='scrollbars=no, ';
            props +='resizable=no';
	if(id != "")
        window.open(getPath()+'/products/largePic.do?selectedvariantid='+id,'',props);
    else
    	window.open(getPath()+'/products/largePic.do','',props);
    return false;
}


function forgottenPassword(){
	if (!window.focus) return true;

	var windowW = 330;
    var windowH = 200;

    var left = (screen.width-windowW)/2;
    var top = (screen.height-windowH)/2;

    var props =  'height='+windowH+', ';
    	props += 'width='+windowW+', ';
        props += 'top='+top+', ';
        props += 'left='+left+', ';
        props +='scrollbars=no, ';
        props +='resizable=no';
        window.open(getPath()+'/sendpassword.do','ForgottenPassword', props);
        return false;
}




var RED = "#E15B5B";
var ALARMRED = "#E42D2D";
var GREEN = "#5AF389";
var TRANSPARENT = "transparent";

function invoiced(orderno, cell){
	var parent = cell.parentNode;
	var invoiced = document.getElementById("invoiced");
	$.get(getPath()+"/orderstatuse/invoiced.do?weHateIE="+myTimestamp(), { order_invoice: orderno }, 
	function(data){
		data = data.trim();
		if(data == "sUninvoiced"){
			parent.style.backgroundColor = TRANSPARENT;
			cell.checked = false;
			if(invoiced != null){
				invoiced.innerHTML = "Ikke fakturert";
			}
		}
		else if(data == "sInvoiced"){
			parent.style.backgroundColor = GREEN;
			cell.checked = true;
			if(invoiced != null)
				invoiced.innerHTML = "Fakturert";
		}
		else if(data == "fUninvoiced"){
			parent.style.backgroundColor = ALARMRED;
			cell.checked = false;
			if(invoiced != null)
				invoiced.innerHTML = "Feil, ordre ikke fakturert";
		}
		else if(data == "fInvoiced"){
			parent.style.backgroundColor = ALARMRED;
			cell.checked = true;
			if(invoiced != null)
				invoiced.innerHTML = "Feil, men ordre er fakturert";
		}
		else if (data == "crash"){
			parent.style.backgroundColor = ALARMRED;
		}
	} );
}

function formcheckout(){
	if(document.getElementById("cartFreetext") != null)
		document.getElementById("checkoutform").cartFreetext.value = document.getElementById("cartFreetext").value;
	
	document.getElementById("checkoutform").submit();
}

//lreturns a timestamp. Used to avoid IE caching Ajax requests
function myTimestamp(){
    tstmp = new Date();    
    return tstmp.getTime();
} 

/* Trimmer strengen du gir som input */

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
