String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "")
};

function ShowBonus(iId)
{
	var sShowBonus = 'bonus' + iId;
	Hide();
	ClearDefinition();
	document.getElementById(sShowBonus).style.display = "block";
	document.getElementById('radio' + iId).checked = true;
}

function ClearDefinition()
{
	var elements = document.getElementsByName('definice');
	for (var i = 0; i < elements.length; i++)
	{
		elements[i].checked = false;
	}
}

function getElementsByClass( searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}

function Hide()
{
	var elements = getElementsByClass('bonus', document, 'div');
	
	for (var i = 0; i < elements.length; i++)
	{
		elements[i].style.display = "none";
	}
	
	var bonuses = location.hash;

	if(bonuses.length > 0)
	{
		bonuses = bonuses.replace("#", "");
				
		if(bonuses.length > 1)
		{
			if (document.getElementById('check' + bonuses) != null) document.getElementById('check' + bonuses).click();
			if (document.getElementById('radio' + bonuses) != null && document.getElementById('radio' + bonuses).checked != false) document.getElementById('radio' + bonuses).click();
		}
	}
}

function FreeBonus(iIdZonerCz_Bonus)
{
	if(!document.getElementById(iIdZonerCz_Bonus).checked) document.getElementById(iIdZonerCz_Bonus).checked = true;
}

function ChangeActionPrice(iPrice, iPriceDph, iIdZonerCz)
{
	var sElementPrice = "ActionPrice" + iIdZonerCz;
	var sElementPriceDph = "ActionPriceDph" + iIdZonerCz;

	document.getElementById(sElementPrice).value = iPrice;
	document.getElementById(sElementPriceDph).value = iPriceDph;

	ChangePrice(0, 0, iIdZonerCz);
}

function ChangePrice(iPrice, iPriceDph, iIdZonerCz, bCheck)
{
	var sElementPrice = "DefaultPrice" + iIdZonerCz;
	var sElementPriceDph = "DefaultPriceDph" + iIdZonerCz;

	var sElementActionPrice = "ActionPrice" + iIdZonerCz;
	var sElementActionPriceDph = "ActionPriceDph" + iIdZonerCz;

	var sElementFinalPrice = "FinalPrice" + iIdZonerCz;
	var sElementFinalPriceDph = "FinalPriceDph" + iIdZonerCz;

	var iActualPrice = parseInt(String(document.getElementById(sElementPrice).value).replace(/\s+/g,''));
	var iActualPriceDph = parseInt(String(document.getElementById(sElementPriceDph).value).replace(/\s+/g,''));

	var iActionActualPrice = parseInt(String(document.getElementById(sElementActionPrice).value).replace(/\s+/g,''));
	var iActionActualPriceDph = parseInt(String(document.getElementById(sElementActionPriceDph).value).replace(/\s+/g,''));

	if(bCheck)
	{
		iActualPrice = iActualPrice + iPrice;
		iActualPriceDph = iActualPriceDph + iPriceDph;
	}
	else
	{
		iActualPrice = iActualPrice - iPrice;
		iActualPriceDph = iActualPriceDph - iPriceDph;
	}

	document.getElementById(sElementPrice).value = iActualPrice;
	document.getElementById(sElementPriceDph).value = iActualPriceDph;

	iActualPrice += iActionActualPrice;
	iActualPriceDph += iActionActualPriceDph;

	var sFinalPrice = Currency(String(iActualPrice));
	var sFinalPriceDph = Currency(String(iActualPriceDph));

	document.getElementById(sElementFinalPrice).value = sFinalPrice;
	document.getElementById(sElementFinalPriceDph).value = sFinalPriceDph;
}

function Mirror(sText)
{
	var sMirror = "";
	for(var i = 0; i < sText.length; i++)
	{
		sMirror = sText.substring(i,i+1) + sMirror;
	}
	return sMirror;
}

function Currency(sPrice)
{
	if(sPrice.length > 3)
	{
		var sPriceMirror = Mirror(sPrice);
		var sMirrorTemp = "";

		sPrice = "";
		while(sPriceMirror.length > 3)
		{
			sMirrorTemp = sPriceMirror.substring(0,3);
			sPriceMirror = sPriceMirror.replace(sMirrorTemp, '');
			sPrice = sPrice + sMirrorTemp + ' ';
		}
		sPrice = sPrice + sPriceMirror;
		sPrice = Mirror(sPrice);
	}
	return sPrice;
}

function SetFinalPrice(payment)
{
	var iFinalPrice = parseInt(document.getElementById('orderPriceTemp').value);
	if(payment == 'dobírka (+30 Kč)') iFinalPrice = iFinalPrice + 30;
	document.getElementById('orderPrice').innerHTML = Currency(String(iFinalPrice));
}

function isValid()
{
	if(document.getElementById('nazev').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit název společnosti!");
		document.getElementById('nazev').style.backgroundColor = "#ffb0b0";
		document.getElementById('nazev').focus();
		return false;
	}
	else if(document.getElementById('adresa1').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit adresu!");
		document.getElementById('adresa1').style.backgroundColor = "#ffb0b0";
		document.getElementById('adresa1').focus();
		document.getElementById('nazev').style.backgroundColor = "#ffffff";
		return false;
	}
	else if(document.getElementById('mesto').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit město!");
		document.getElementById('mesto').style.backgroundColor = "#ffb0b0";
		document.getElementById('mesto').focus();
		document.getElementById('adresa1').style.backgroundColor = "#ffffff";
		return false;
	}
	else if(document.getElementById('psc').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit PSČ!");
		document.getElementById('psc').style.backgroundColor = "#ffb0b0";
		document.getElementById('psc').focus();
		document.getElementById('mesto').style.backgroundColor = "#ffffff";
		return false;
	}
	else if(document.getElementById('osoba').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit odpovědnou osobu!");
		document.getElementById('osoba').style.backgroundColor = "#ffb0b0";
		document.getElementById('osoba').focus();
		document.getElementById('psc').style.backgroundColor = "#ffffff";
		return false;
	}
	else if(document.getElementById('telefon').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit telefon!");
		document.getElementById('telefon').style.backgroundColor = "#ffb0b0";
		document.getElementById('telefon').focus();
		document.getElementById('osoba').style.backgroundColor = "#ffffff";
		return false;
	}
	else if(!checkEmail(document.getElementById('email').value.trim()))
	{
		window.alert("Před odesláním formuláře musíte vyplnit email");
		document.getElementById('email').style.backgroundColor = "#ffb0b0";
		document.getElementById('email').focus();
		document.getElementById('telefon').style.backgroundColor = "#ffffff";
		return false;
	}
	else if(document.getElementById('platba').value=="0")
	{
		window.alert("Před odesláním formuláře musíte vyplnit způsob platby");
		document.getElementById('platba').style.backgroundColor = "#ffb0b0";
		document.getElementById('platba').focus();
		document.getElementById('email').style.backgroundColor = "#ffffff";
		return false;
	}
	else
	{
		document.getElementById('platba').style.backgroundColor = "#ffffff";
		return true;
	}
}

function isValidEdu()
{
	document.getElementById('platba').style.backgroundColor = "#ffffff";
	document.getElementById('jmeno').style.backgroundColor = "#ffffff";
	document.getElementById('prijmeni').style.backgroundColor = "#ffffff";
	document.getElementById('adresa1').style.backgroundColor = "#ffffff";
	document.getElementById('mesto').style.backgroundColor = "#ffffff";
	document.getElementById('psc').style.backgroundColor = "#ffffff";
	document.getElementById('email').style.backgroundColor = "#ffffff";
	document.getElementById('email2').style.backgroundColor = "#ffffff";
	document.getElementById('jmeno_skoly').style.backgroundColor = "#ffffff";
	document.getElementById('adresa2').style.backgroundColor = "#ffffff";
	document.getElementById('telefon').style.backgroundColor = "#ffffff";
	document.getElementById('prodejce').style.backgroundColor = "#ffffff";
	document.getElementById('serial').style.backgroundColor = "#ffffff";
	document.getElementById('potvrzeni').style.backgroundColor = "#ffffff";
	
	var arrDefinition = document.getElementsByName('definice3');
	var bOrder = false;
	
	for(var i = 0; i < arrDefinition.length; i++)
	{
		if (arrDefinition[i].checked) bOrder = true;
	}
	
	if(!bOrder)
	{
		window.alert("Před odesláním formuláře musíte vybrat produkt!");
		return false;
	}
	else if(document.getElementById('platba').value=="0")
	{
		window.alert("Před odesláním formuláře musíte vyplnit způsob platby!");
		document.getElementById('platba').style.backgroundColor = "#ffb0b0";
		document.getElementById('platba').focus();
		return false;
	}
	else if(document.getElementById('jmeno').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit jméno!");
		document.getElementById('jmeno').style.backgroundColor = "#ffb0b0";
		document.getElementById('jmeno').focus();
		return false;
	}
	else if(document.getElementById('prijmeni').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit příjmení!");
		document.getElementById('prijmeni').style.backgroundColor = "#ffb0b0";
		document.getElementById('prijmeni').focus();
		return false;
	}
	else if(document.getElementById('adresa1').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit ulici!");
		document.getElementById('adresa1').style.backgroundColor = "#ffb0b0";
		document.getElementById('adresa1').focus();
		return false;
	}
	else if(document.getElementById('mesto').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit město!");
		document.getElementById('mesto').style.backgroundColor = "#ffb0b0";
		document.getElementById('mesto').focus();
		return false;
	}
	else if(document.getElementById('psc').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit PSČ!");
		document.getElementById('psc').style.backgroundColor = "#ffb0b0";
		document.getElementById('psc').focus();
		return false;
	}
	else if(!checkEmail(document.getElementById('email').value.trim()))
	{
		window.alert("Před odesláním formuláře musíte vyplnit soukromý email");
		document.getElementById('email').style.backgroundColor = "#ffb0b0";
		document.getElementById('email').focus();
		return false;
	}
	else if(!checkEmail(document.getElementById('email2').value.trim()))
	{
		window.alert("Před odesláním formuláře musíte vyplnit školní email");
		document.getElementById('email2').style.backgroundColor = "#ffb0b0";
		document.getElementById('email2').focus();
		return false;
	}
	else if(document.getElementById('jmeno_skoly').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit jméno školy!");
		document.getElementById('jmeno_skoly').style.backgroundColor = "#ffb0b0";
		document.getElementById('jmeno_skoly').focus();
		return false;
	}
	else if(document.getElementById('adresa2').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit adresu školy!");
		document.getElementById('adresa2').style.backgroundColor = "#ffb0b0";
		document.getElementById('adresa2').focus();
		return false;
	}
	else if(document.getElementById('telefon').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit telefon do školy!");
		document.getElementById('telefon').style.backgroundColor = "#ffb0b0";
		document.getElementById('telefon').focus();
		return false;
	}
	else if(document.getElementById('prodejce').value.trim()=="" && document.getElementById('serial').value.trim()=="")
	{
		window.alert("Před odesláním formuláře musíte vyplnit datum zakoupení školní multilicence nebo číslo faktury!");
		document.getElementById('prodejce').style.backgroundColor = "#ffb0b0";
		document.getElementById('prodejce').focus();
		document.getElementById('serial').style.backgroundColor = "#ffb0b0";
		return false;
	}
	else if(!document.getElementById('potvrzeni').checked)
	{
		window.alert("Před odesláním formuláře musíte potvrdit, že jste v pracovněprávním nebo jiném obdobném vztahu k uvedené škole!");
		document.getElementById('potvrzeni').style.backgroundColor = "#ffb0b0";
		document.getElementById('potvrzeni').focus();
		return false;
	}
	else
	{
		return true;
	}
}

function checkEmail(address)
{
	re = /^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,4}$/;
	return address.search(re) == 0;
}

function Summe_Gesamtpreiss()
{
	var iPriceZPS9P = parseFloat(document.getElementById('zps9p_gesamtpreiss').value);
	var iPriceZPS9H = parseFloat(document.getElementById('zps9h_gesamtpreiss').value);
	var iPriceZPS9C = parseFloat(document.getElementById('zps9c_gesamtpreiss').value);
	var iPriceZD5 = parseFloat(document.getElementById('zd5_gesamtpreiss').value);
	var iPriceZPM = parseFloat(document.getElementById('zpm_gesamtpreiss').value);
	var iPriceZ3DPM = parseFloat(document.getElementById('z3dpm_gesamtpreiss').value);
	var iPriceZPP = parseFloat(document.getElementById('zpp_gesamtpreiss').value);
	var iPriceZWG = parseFloat(document.getElementById('zwg_gesamtpreiss').value);
	var iPriceZBS2 = parseFloat(document.getElementById('zbs2_gesamtpreiss').value);
	var iPriceZGA5 = parseFloat(document.getElementById('zga5_gesamtpreiss').value);

	var iPriceTax = parseFloat(iPriceZPS9P + iPriceZPS9H + iPriceZPS9C + iPriceZD5 + iPriceZPM + iPriceZ3DPM + iPriceZPP + iPriceZWG + iPriceZBS2 + iPriceZGA5);
	var iTax = iPriceTax - (iPriceTax / 1.2);
	iTax = Math.round(iTax * 100) / 100;
	var iPriceWithoutTax = Math.round((iPriceTax - iTax) * 100) / 100;
	iPriceTax = Math.round(iPriceTax * 100) / 100;

	document.getElementById('SumWithoutTax').value = iPriceWithoutTax;
	document.getElementById('Tax').value = iTax;
	document.getElementById('SumWithTax').value = iPriceTax;

	return true;
}
