

/*------------------- AJAX --------------------*/
var req;
var currentPricebook;

var tempXCompLink;
var tempYCompLink;
var tempZCompLink;
var xcompXML="0";
var ycompXML="0";
var zcompXML="0";

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 runAjaxrequest(picurl) {
   var url2 = encodeURI(picurl);
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url2, true);
   req.onreadystatechange = callback;
   req.send(null);
}

function callback() {
	var statusdiv = document.getElementById("statusbar");
	if(req.readyState == 0){
		
	}
	if(req.readyState == 1){
	}
	if(req.readyState == 2){
	}
	if(req.readyState == 3){
	}
    if (req.readyState == 4) {
        if (req.status == 200) {
        	//alert("req.text:"+req.responseText);
            setComps();
        }
    }
}

/*-------------------- END AJAX ---------------------*/



function resetcache(){
	alert("Is reseting the cache...");
	window.location=getPath()+"/parameters/resetcache.do";
}

/*--------------------- 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 progressbar(numb){
	setTimeout("progressbar2("+numb+")",0);
}
function progressbar2(numb){
	var statusbarImg2 = document.getElementById("statusbarimg");
	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;
	//alert("xcompXML:"+xcompXML+"   ycompXML:"+ycompXML+"    zcompXML:"+zcompXML);
	variantexist = data.variantexist;
	var variantonstock = data.variantonstock;
	nronstock = variantonstock;
	var variantprice = data.variantprice;
	var variantwsprice = data.variantwsprice;
	var selectedvariantidXML = data.selectedvariantid;
	selectedPicture2 = selectedvariantidXML;
	var numbercompsinuse = data.numberCompsInUse;
	}catch(em){};

	
	if(tempXCompLink){
		tempXCompLink.style.fontWeight = "normal";
		tempXCompLink.style.color = "#666666";
	}
	if(tempYCompLink){
		tempYCompLink.style.fontWeight = "normal";
		tempYCompLink.style.color = "#666666";
	}
	if(tempZCompLink){
		tempZCompLink.style.fontWeight = "normal";
		tempZCompLink.style.color = "#666666";
	}
	var xcompLinkStr = "xcomp_"+xcompXML;
	var ycompLinkStr = "ycomp_"+ycompXML;
	var zcompLinkStr = "zcomp_"+zcompXML;
	
	var xcompLink = document.getElementById(xcompLinkStr);
	var ycompLink = document.getElementById(ycompLinkStr);
	var zcompLink = document.getElementById(zcompLinkStr);

		if(xcompLink){
			if(variantexist == "false"){
				xcompLink.style.color = "#FF0000"; //red
			}
			else if(variantonstock <= 0){
				xcompLink.style.color = "#666666";//B48F10"; //green
			}
			xcompLink.style.fontWeight="bold";
			tempXCompLink = xcompLink;
		}
		if(ycompLink){
			if(variantexist == "false")
				ycompLink.style.color = "#FF0000";
			else if(variantonstock <= 0)
				ycompLink.style.color = "#666666";//B48F10";
			ycompLink.style.fontWeight="bold";
			tempYCompLink = ycompLink;
		}
		if(zcompLink){
			if(variantexist == "false")
				zcompLink.style.color = "#FF0000";
			else if(variantonstock <= 0)
				zcompLink.style.color = "#666666"; //B48F10";
			zcompLink.style.fontWeight="bold";
			tempZCompLink = zcompLink;
		}

	/* Change the background picture in the eske webshop */
	if(variantexist == "true"){
		changeBackgroundpic(selectedvariantidXML, currentPricebook);
		
	}
	var onstockText;
	nronstock = variantonstock;
	if(variantexist == "true"){
		var buydiv = document.getElementById("productonstockimgdiv");
		if( buydiv != null)
			buydiv.style.visibility = "visible";
		//changePic(selectedvariantidXML);
		/*if(variantonstock < 1)
			onstockText = "Men varen er beklagligvis ikke på lager";
		else*/
			onstockText = "";
		document.getElementById("pricevariant").innerHTML = "kr "+variantprice+",-    "+onstockText;
		if(document.getElementById("pricevariant").style.visibility == 'hidden')
			document.getElementById("pricevariant").style.visibility = 'visible';
		//shownumberonstock(variantonstock, variantexist);
	}
	
	else if((numbercompsinuse == 3 && threecompcheck(xcompXML, ycompXML, zcompXML)) ||
		(numbercompsinuse == 2 && twocompcheck(xcompXML, ycompXML)) ||
		(numbercompsinuse == 1 && onecompcheck(xcompXML))){
		//shownumberonstock(variantonstock, variantexist);
		document.getElementById("pricevariant").style.visibility = "hidden";

	}
	else{
		if(document.getElementById("pricevariant").style.visibility == "hidden")
			document.getElementById("pricevariant").style.visibility = "visible";
			
		document.getElementById("pricevariant").innerHTML = " Denne varianten finnes ikke";
		//shownumberonstock(variantonstock, variantexist);
		document.getElementById("productonstockimg").style.visibility = "hidden";
		insertPic("variant_not_exist.jpg");
	}
	
}
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 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, path){
	var url = path+"/products/selectproduct.do?id="+id;
	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 changeBackgroundpic(picid, pricebook){
	
	/*var backg1 = document.getElementById("backgroundEske1");
	var backg2 = document.getElementById("backgroundEske2");
	var backgroundImage = document.getElementById("backgroundPic");
	var backgroundPicDiv = document.getElementById("backgroundPicDiv");

	backg1.style.display = 'block';
	backgroundImage.style.display = 'block';
	backg2.style.display = 'none';
	


	var picurl = getPath()+"/imageservlet?type=product&imagesize=large&id="+picid+"&pricebook="+pricebook;
	var url2 = encodeURI(picurl);
	backgroundImage.style.background = "url("+url2+")";
	var background = document.getElementById("backgroundPic").style.background;
	
	selectedPicture2=picid;*/
	if(document.getElementById('skumainpic') != null){
		document.getElementById('skumainpic').src = getPath()+"/imageservlet?type=product&imagesize=large&id="+picid+"&pricebook="+pricebook;
	
	}
	
}

function changemainpic(picid, pricebook){
	//changeBackgroundpic(picid, pricebook);
	var urlSku = getPath()+"/products/compVariant.do?selectedvariantid="+picid+"&pricebook="+pricebook;
	runAjaxrequest(urlSku);
}
function changePic(picid){

	var mainpic = document.getElementById("backgroundPic");
	var backg1 = document.getElementById("backgroundEske1");
	var backg2 = document.getElementById("backgroundEske2");
		
	var picurl = getPath()+"/imageservlet?type=product&imagesize=large&id="+picid;
	var url2 = encodeURI(picurl);
	//mainpic.src = url2;
	backg1.style.display = 'block';
	backg2.style.display = 'none';
	mainpic.style.background = "url("+url2+")";
	selectedPicture2=picid;
	variantexist="true";
}

function changeXcomp(pricebook, xcomp){
	currentPricebook = pricebook;
	var xcomptmp = document.getElementById("xcomp");
	//var xcomp = xcomptmp.options[xcomptmp.selectedIndex].value;
	var urlSku = getPath()+"/products/changeComps.do?xcomp="+xcomp+"&pricebook="+pricebook;
	runAjaxrequest(urlSku);
}

function changeYcomp(pricebook, ycomp){
	currentPricebook = pricebook;
	var ycomptmp = document.getElementById("ycomp");
	//var ycomp = ycomptmp.options[ycomptmp.selectedIndex].value;
	var urlSku = getPath()+"/products/changeComps.do?ycomp="+ycomp+"&pricebook="+pricebook;
	
	runAjaxrequest(urlSku);
}
function changeZcomp(pricebook, zcomp){
	currentPricebook = pricebook;
	var zcomptmp = document.getElementById("zcomp");
	//var zcomp = zcomptmp.options[zcomptmp.selectedIndex].value;
	var urlSku = getPath()+"/products/changeComps.do?zcomp="+zcomp+"&pricebook="+pricebook;
	
	runAjaxrequest(urlSku);
}



function addtocartSku(pricebook){
	if(variantexist== "false")
		alert("Du må velge en variant først");
	else
		addtocart(selectedPicture2, pricebook);
}

function addtocart(id, pricebook) {

	var onStock = nronstock;
	var qty = 1;
	
	
   /*	if(onStock < 1 && !isAllownegativestock()){
   		alert("Varen er ikke på lager og kan derfor ikke kjøpes");
   	}
   	else if (qty>onStock && !isAllownegativestock()){
   		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);
	      document.cartForm.id.value=id;
	      document.cartForm.qty.value=qty;
	      document.cartForm.pricebook.value=pricebook;
	      //alert("Varen er lagt i handlevognen");
	      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;
}







/* 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+$/,"");
}
