var xmlHttp;
var xmlHttp2;
var fieldName;
var VAT = 1.23;

function zaokraglenie(liczba) {
liczba = liczba * 100;
var wynik = Math.round(liczba)/100;
return wynik;
}



function switchMultiHero(which) {
	var max = 4;
	for(i=1;i<=4;i++) {
		if(which != i) {
			if(navigator.appVersion.indexOf("MSIE 7.") == -1) {
				var param = 'class'; 
			} else {
				var param = 'className';
			}
			document.getElementById('multihero'+i).setAttribute(param, "multi last-active");
			//document.getElementById("button"+i).style.backgroundPosition = 'right bottom';
		}
	}
	document.getElementById('multihero'+which).setAttribute(param, "multi active");
	//document.getElementById("button"+which).style.backgroundPosition = 'left bottom';
	document.forms["multi"].elements['active'].value = which;
}

function orderLanding(order,price) {
	document.forms['order'].elements['final'].value = order;
	document.forms['order'].elements['cena'].value = price;
	document.forms['order'].submit();
}

function calculate(name) {
	
	xmlHttp2=GetXmlHttpObject();
	if(xmlHttp2==null) {
		alert ("Brak wsparcia dla AJAXa.");
		return;
	} 

	var rabat = document.forms['universalGenerator'].elements['generator[rabat]'].value;
	var stanowiska = document.forms['universalGenerator'].elements['generator[stanowiska]'].value;
	var serwery = document.forms['universalGenerator'].elements['generator[serwery]'].value;
	var program = document.forms['universalGenerator'].elements['generator[program]'].value;
	
	fieldName = name;
	
	var url="Ajax.php?action=calculator&rabat="+rabat+"&stanowiska="+stanowiska+"&serwery="+serwery+"&program="+program;
	url=url+"&sid="+Math.random();
	xmlHttp2.onreadystatechange=stateChanged;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
	
}

function stateChanged() {
	if(xmlHttp2.readyState==4) { 
		document.getElementById(fieldName).innerHTML=xmlHttp2.responseText;
	}
}


function switchNosnik() {
	// Licencja elektroniczna ma value = 3
	if(document.jar.shippMethod.value == 3) {
		document.getElementById('nosnik').style.display = 'none';
		document.getElementById('addElektroniczna').style.display = '';
		document.getElementById('addSendInfo').style.display = 'none';
		document.jar.nosnik[0].checked = false;
		document.jar.nosnik[1].checked = true;
		checkNosnik(0);
		hideRow();
	} else {
		document.getElementById('nosnik').style.display = 'none';
		document.getElementById('addElektroniczna').style.display = 'none';
		document.getElementById('addSendInfo').style.display = '';
		if(document.jar.nosnik[0].checked == true) {
			
			//Cena nośnika w przypadku 1 stanowiskówego = 0
			if(document.jar.ilestanowisk.value == 1) {
				checkNosnik(0);
				displayRow(0);
			} else {
				//checkNosnik(16);
				//displayRow(16);
			}
			
		} else {
			document.jar.nosnik[0].checked = true;
			document.jar.nosnik[1].checked = false;
			
			if(document.jar.ilestanowisk.value == 1) {
				checkNosnik(0);
				displayRow(0);
			} else {
				//checkNosnik(16);
				//displayRow(16);
			}
			
			//checkNosnik(0);
			//hideRow();
		}
	}
	
	
	
}

function displayRow(nosnik){

dostawa = getDostawa();
//dostawa = 0;
nosnik = zaokraglenie(nosnik*VAT);
var liczba1 = (kwota+dostawa+nosnik);
var liczba2 = ((kwota+dostawa+nosnik)/VAT);
document.jar.cena.value=liczba1.toFixed(2);
document.jar.cena_netto.value=liczba2.toFixed(2);
var row = document.getElementById("caption");
row.style.display = '';
}

function GetXmlHttpObject2() {
	var xmlHttp2=null;
	try {
		//Firefox, Opera 8.0+, Safari
		xmlHttp2=new XMLHttpRequest();
	}

	catch (e) {
		//Internet Explorer
		try {
			xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
		}
  
		catch (e) {
			xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp2;
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		//Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}

	catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
  
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp;
}

function getDostawa() {
	which = document.jar.shippMethod.value;
	
	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null) {
		alert ("Brak wsparcia dla AJAXa.");
		return;
	} 
	
	var url="Ajax.php?action=getShippingPrice&shippingID="+which;
	url=url+"&sid="+Math.random();
	//xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	
	if (xmlHttp.readyState==4) { 
		dostawa = xmlHttp.responseText;
	}
	
	dostawa = zaokraglenie(dostawa);
	
	return dostawa;
}

function checkNosnik(ile) {
	document.jar.hiddenNosnik.value = ile;
}

function changeKwota() {
	
	var nosnik = document.jar.hiddenNosnik.value;
	nosnik = zaokraglenie(nosnik*VAT);
	dostawa = getDostawa();
	
	var liczba1 = (kwota+dostawa+nosnik);
	var liczba2 = ((kwota+dostawa+nosnik)/VAT);
	document.jar.cena.value=liczba1.toFixed(2);
	document.jar.cena_netto.value=liczba2.toFixed(2);
	document.getElementById('priceShipping1_one').innerHTML = dostawa.toFixed(2)+' zł';
	document.getElementById('priceShipping1_all').innerHTML = dostawa.toFixed(2)+' zł';
	switchNosnik();
	
}

function showShipping() {
	if(document.getElementById('shipping').style.display == '') {
		document.getElementById('shipping').style.display = 'none';
	} else {
		document.getElementById('shipping').style.display = '';
	}
}

function hideRow(){
dostawa = getDostawa();
	
var liczba1 = kwota+dostawa;
var liczba2 = ((kwota+dostawa)/VAT);
document.jar.cena.value=liczba1.toFixed(2);
document.jar.cena_netto.value=liczba2.toFixed(2);
var row = document.getElementById("caption");
row.style.display = 'none';
}

function osoba(){
var osoba = document.getElementById("osoba");
var firma = document.getElementById("firma");
osoba.style.display = '';
document.jar.kto.value=1;
firma.style.display = 'none';
document.jar.zamawiajacy2.value = document.jar.zamawiajacy.value; 
document.jar.ulica2.value = document.jar.ulica.value;
document.jar.kod2.value = document.jar.kod.value;
document.jar.miasto2.value = document.jar.miasto.value;
document.jar.telefon2.value = document.jar.telefon.value;
document.jar.mail2.value = document.jar.mail.value;
//document.jar.serial2.value = document.jar.serial.value;
document.jar.licencja2.value = document.jar.licencja.value;
}

function firm(){
var osoba = document.getElementById("osoba");
var firma = document.getElementById("firma");
osoba.style.display = 'none';
document.jar.kto.value=2;
firma.style.display = '';
document.jar.zamawiajacy.value = document.jar.zamawiajacy2.value; 
document.jar.ulica.value = document.jar.ulica2.value;
document.jar.kod.value = document.jar.kod2.value;
document.jar.miasto.value = document.jar.miasto2.value;
document.jar.telefon.value = document.jar.telefon2.value;
document.jar.mail.value = document.jar.mail2.value;
//document.jar.serial.value = document.jar.serial2.value;
document.jar.licencja.value = document.jar.licencja2.value;
}

function hideshow(nazwa){
	var divname = nazwa;
	var row = document.getElementById(nazwa);
	if(row.style.display == 'none')
	{
		row.style.display = '';
	}
	else
	{
		row.style.display = 'none';
	}
}

