

var req = null;

function getXMLHttpRequest()
{ 
    var httpReq = null;
    if (typeof XMLHttpRequest != 'undefined') {
    
        httpReq = new XMLHttpRequest();
    	
    }else if (typeof ActiveXObject != "undefined") {
    
        httpReq = new ActiveXObject("Microsoft.XMLHTTP");
     	
    }
    return httpReq;
}

// Fehler:
// - Wenn Druckart, Lamination oder Front geändert wird, müssen die anderen
//	 Werte entsprechend auf 0 bzw. den Standardwert zurückgesetzt werden.
//	 Ansonsten kann die Preisberechnung nicht funktionieren!


function changeRequest(articleID) {
//     alert(articleID);
    var front = document.preisForm.front_ID.value;
    req = getXMLHttpRequest();

	if (!req) {

		document.getElementByName('preisDiv').innerHTML = 'Seite neu Laden!';

     } else {

        if (document.preisForm.format.value == null){
            document.preisForm.format.value = 0.5;
            document.preisForm.lamination_ID.value = 1;
//             document.preisForm.front_ID.value = 1;
        }

        if(document.preisForm.lamination_ID.value == 1){
            front = 1;
            document.preisForm.elements['front_ID'].disabled = true;
        }else
        	document.preisForm.elements['front_ID'].disabled = false;
        if((document.preisForm.lamination_ID.value == 3) && ((document.preisForm.front_ID.value == 1) || document.preisForm.front_ID.value == '')){

            document.preisForm.front_ID.value = 2;
            front = 2;

        }

        var basket = '';
        if(document.preisForm.basketid.value != null)
            basket = "&basketid="+document.preisForm.basketid.value;

        req.onreadystatechange = preisNeuBerechnen;
		req.open("GET", "/ajax/preisajax.php?article_ID="+articleID+"&format="+document.preisForm.format.value+"&lamination_ID="+document.preisForm.lamination_ID.value+"&front_ID="+front+basket, true);
		req.send(null);

	}

}


function preisNeuBerechnen () {

	if (req.readyState == 4 && req.status == 200) {
	
        var str = req.responseText;
        var arrRes = str.split("+");
        var allListe = str.split("#");
//Kaschierung neu schreiben
		var currentLamination = document.preisForm.lamination_ID.value;
        var lamination = document.getElementById('lamination_ID');
        var laminationListe = allListe[0].split("+");
        var laminationDesc = allListe[1].split("+");
        for (i=lamination.length; i >= 0 ;i-- ) {
        	lamination[i] = null;
        }


        for (i=0; i < laminationListe.length ; i++ ) {
        	

			if (currentLamination == laminationListe[i]) {
		  	    lamination[i] = new Option(laminationDesc[i],laminationListe[i],false,true);
		  	    				
		  	} else {

				lamination[i] = new Option(laminationDesc[i],laminationListe[i],false,false);

			}

        }
        
//Front neu Schreiben
		var currentFront = document.preisForm.elements['front_ID'].value;
        var front = document.getElementById('front_ID');
        var frontListe = allListe[2].split("+");
        var frontDesc = allListe[3].split("+");
        for (i=front.length; i >= 0 ;i-- ) {
        	front[i] = null;

        }
        for (i=0; i < frontListe.length ; i++ ) {

			if (currentFront == frontListe[i])
		  	    front[i] = new Option(frontDesc[i],frontListe[i],false,true);
			else
				front[i] = new Option(frontDesc[i],frontListe[i],false,false);
        	
        }
		document.getElementById('preisDiv').innerHTML = allListe[4];

		document.getElementById('textDiv').innerHTML = allListe[5];

		document.preisForm.elements['formula_ID'].value = allListe[6];
		
// previewImage
        var previewImg = document.getElementById('previewImg');

        if(allListe[6] == 4){
        
            document.getElementById('previewLink').innerHTML = '- <a href="" onMouseOver="tooltipShow(\'skizze\',230);" onMouseOut="tooltipHide(\'skizze\');" >Beispiel ansehen</a>';
            previewImg.src = '/_site/images/dibond_profil.jpg';
            
        }else if(allListe[6] == 5){
        
            document.getElementById('previewLink').innerHTML = ' - <a href="" onMouseOver="tooltipShow(\'skizze\',256);" onMouseOut="tooltipHide(\'skizze\');" >Beispiel ansehen</a>';
            previewImg.src = '/_site/images/dibond_plexi.jpg';
            
        }else{

            document.getElementById('previewLink').innerHTML = '';
            previewImg.src = '';
        }

	}

}


