var stepOne = 'new';
var email = '';
var paymentMethod = 'prevod';
var litem1 = '';
var litem2 = '';
var litem3 = '';
var litem4 = '';
var licenceVersion = '';

var ln20 = '';
var ln21 = '';
var ln22 = '';
var ln23 = '';
var ln24 = '';
var ln25 = '';
var ln26 = '';
var ln27 = '';
var ln28 = '';

if('cz' == language) var currency = 'Kč';
else var currency = '€';

var homeCount = 0;
var profiCount = 0;

var homePrice = 0;
var profiPrice = 0;
var homeUpgradePrice = 0;
var profiUpgradePrice = 0;
var boxPrice = 0;
var shippingPrice = 0;
var codPrice = 0;

var czk_homePrice = 0;
var czk_profiPrice = 0;
var czk_homeUpgradePrice = 0;
var czk_profiUpgradePrice = 0;
var czk_boxPrice = 0;
var czk_shippingPrice = 0;

var eur_homePrice = 0;
var eur_profiPrice = 0;
var eur_homeUpgradePrice = 0;
var eur_profiUpgradePrice = 0;
var eur_boxPrice = 0;
var eur_shippingPrice = 0;

var boxes = 0;

var fa_firstname = '';
var fa_surname = '';
var fa_company = '';
var fa_street = '';
var fa_city = '';
var fa_psc = '';
var fa_state = '';
var fa_dic = '';
var fa_phone = '';
var fa_fax = '';

var do_firstname = '';
var do_surname = '';
var do_company = '';
var do_street = '';
var do_city = '';
var do_psc = '';
var do_state = '';
var do_phone = '';
var do_fax = '';


function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function changeContent(type, other){
	$.ajax({
	   type: "POST",
	   url: "http://" + location.hostname + "/shop_" + language + ".php",
	   cache: false,
	   data: "type=" + type + "&other=" + other + "&ln=" + language + "&cardTest=" + cardTest,
	   dataType: "html",
	   scriptCharset: "utf-8",
	   success: function(msg){
		   $("#shop").html(msg);
	   }
	});
	return true;
}

function changePricesByCurrency(){
	if(currency == 'Kč'){
		homePrice = czk_homePrice;
		profiPrice = czk_profiPrice;
		homeUpgradePrice = czk_homeUpgradePrice;
		profiUpgradePrice = czk_profiUpgradePrice;
		boxPrice = czk_boxPrice;
		shippingPrice = czk_shippingPrice;
		codPrice = czk_codPrice;
	}
	else{
		homePrice = eur_homePrice;
		profiPrice = eur_profiPrice;
		homeUpgradePrice = eur_homeUpgradePrice;
		profiUpgradePrice = eur_profiUpgradePrice;
		boxPrice = eur_boxPrice;
		shippingPrice = eur_shippingPrice;
		codPrice = eur_codPrice;
	}
}
function validateEmail(email){
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str = email;
	if(str.match(emailRegEx)){
		return true;
	}else{
		alert(ln20);
		return false;
	}
}



//
// NEW LICENCE
//
function changeNewLicenceCurrency(){
	document.getElementById('homePriceCurrency').innerHTML = currency;
	document.getElementById('profiPriceCurrency').innerHTML = currency;
	document.getElementById('homePriceFullCurrency').innerHTML = currency;
	document.getElementById('profiPriceFullCurrency').innerHTML = currency;
	document.getElementById('fullPriceCurrency').innerHTML = currency;
}
function recalculate(){
	changePricesByCurrency();
	changeNewLicenceCurrency();
	document.getElementById('homePrice').innerHTML = homePrice;
	document.getElementById('profiPrice').innerHTML = profiPrice;
	document.getElementById('homePriceFull').innerHTML = (document.getElementById('pocet_home').value * homePrice).toFixed(0);
	document.getElementById('profiPriceFull').innerHTML = (document.getElementById('pocet_profi').value * profiPrice).toFixed(0);
	document.getElementById('price').innerHTML = ((document.getElementById('pocet_home').value * homePrice) + (document.getElementById('pocet_profi').value * profiPrice)).toFixed(0);
	//document.getElementById('vat').innerHTML = ((document.getElementById('pocet_home').value * homePrice * 0.20) + (document.getElementById('pocet_profi').value * profiPrice * 0.20)).toFixed(0);
	//document.getElementById('fullPrice').innerHTML = ((document.getElementById('pocet_home').value * homePrice * 1.20) + (document.getElementById('pocet_profi').value * profiPrice * 1.20)).toFixed(0);
	//document.getElementById('boxPrice').innerHTML = ((document.getElementById('pocet_home').value * boxPrice) + (document.getElementById('pocet_profi').value * boxPrice)).toFixed(0);
}



//
// BASKET
//
function changeCurrencyBasket(){
	document.getElementById('homePrice1Currency').innerHTML = currency;
	document.getElementById('homePrice2Currency').innerHTML = currency;
	document.getElementById('profiPrice1Currency').innerHTML = currency;
	document.getElementById('profiPrice2Currency').innerHTML = currency;
	document.getElementById('boxPrice1Currency').innerHTML = currency;
	document.getElementById('boxPrice2Currency').innerHTML = currency;
	document.getElementById('priceCurrency').innerHTML = currency;
	document.getElementById('vatCurrency').innerHTML = currency;
	document.getElementById('fullPriceCurrency').innerHTML = currency;
	
	if('Kč' == currency){
		homePrice = czk_homePrice;
		profiPrice = czk_profiPrice;
		homeUpgradePrice = czk_homeUpgradePrice;
		profiUpgradePrice = czk_profiUpgradePrice;
		boxPrice = czk_boxPrice;
		shippingPrice = czk_shippingPrice;
		codPrice = czk_codPrice;
	}
	else{
		homePrice = eur_homePrice;
		profiPrice = eur_profiPrice;
		homeUpgradePrice = eur_homeUpgradePrice;
		profiUpgradePrice = eur_profiUpgradePrice;
		boxPrice = eur_boxPrice;
		shippingPrice = eur_shippingPrice;
		codPrice = eur_codPrice;
	}
	
	document.getElementById('homePrice1').innerHTML = homePrice;
	document.getElementById('profiPrice1').innerHTML = profiPrice;
	document.getElementById('boxPrice1').innerHTML = boxPrice;
}
function recalculateBasket(){
	changeCurrencyBasket();
	changePricesByCurrency();
	document.getElementById('homePrice').innerHTML = (document.getElementById('pocet_home').value * homePrice).toFixed(0);
	document.getElementById('profiPrice').innerHTML = (document.getElementById('pocet_profi').value * profiPrice).toFixed(0);
	if(document.getElementById('boxes').checked){
		hp = homePrice+boxPrice;
		pp = profiPrice+boxPrice;
	}else{
		hp = homePrice;
		pp = profiPrice;
	}
	document.getElementById('price').innerHTML = ((document.getElementById('pocet_home').value * hp) + (document.getElementById('pocet_profi').value * pp)).toFixed(0);
	document.getElementById('vat').innerHTML = ((document.getElementById('pocet_home').value * hp * 0.20) + (document.getElementById('pocet_profi').value * pp * 0.20)).toFixed(0);
	document.getElementById('fullPrice').innerHTML = ((document.getElementById('pocet_home').value * hp * 1.20) + (document.getElementById('pocet_profi').value * pp * 1.20)).toFixed(0);
	document.getElementById('boxPrice').innerHTML = ((document.getElementById('pocet_home').value * boxPrice) + (document.getElementById('pocet_profi').value * boxPrice)).toFixed(0);
}



//
// USER DATA
//
function userDataCheck(){
	var selected_index;
	var selected_option_value;
	var selected_option_text;
	fa_firstname = document.getElementById('fa_firstname').value;
	fa_surname = document.getElementById('fa_surname').value;
	fa_company = document.getElementById('fa_company').value;
	fa_street = document.getElementById('fa_street').value;
	fa_city = document.getElementById('fa_city').value;
	fa_psc = document.getElementById('fa_psc').value;
	/*
	selected_index = document.getElementById('fa_state').selectedIndex;
	selected_option_value = document.getElementById('fa_state').options[selected_index].value;
	fa_state = selected_option_value;
	*/
	fa_state = document.getElementById('fa_state').value;
	fa_dic = document.getElementById('fa_dic').value;
	fa_phone = document.getElementById('fa_phone').value;
	fa_fax = document.getElementById('fa_fax').value;
	
	do_firstname = document.getElementById('do_firstname').value;
	do_surname = document.getElementById('do_surname').value;
	do_company = document.getElementById('do_company').value;
	do_street = document.getElementById('do_street').value;
	do_city = document.getElementById('do_city').value;
	do_psc = document.getElementById('do_psc').value;
	do_state = document.getElementById('do_state').value;
	do_phone = document.getElementById('do_phone').value;
	do_fax = document.getElementById('do_fax').value;
	
	email = document.getElementById('email1').value;
	email2 = document.getElementById('email2').value;
	tmpAlert = '';
	if(trim(email) == '') tmpAlert = tmpAlert + ln21 +'\n';
	if(trim(fa_firstname) == '') tmpAlert = tmpAlert + ln22 +'\n';
	if(trim(fa_surname) == '') tmpAlert = tmpAlert + ln23 +'\n';
	if(trim(fa_street) == '') tmpAlert = tmpAlert + ln24 +'\n';
	if(trim(fa_city) == '') tmpAlert = tmpAlert + ln25 +'\n';
	if(trim(fa_psc) == '') tmpAlert = tmpAlert + ln26 +'\n';
	if(trim(fa_state) == '') tmpAlert = tmpAlert + ln27 +'\n';
	if(email != email2) tmpAlert = tmpAlert + ln28 +'\n';
	if(trim(tmpAlert) != ''){
		alert(tmpAlert);
		return false;
	}
	else
		return true;
}

function fillUserDataForm(){
	document.getElementById('fa_firstname').value = fa_firstname;
	document.getElementById('fa_surname').value = fa_surname;
	document.getElementById('fa_company').value = fa_company;
	document.getElementById('fa_street').value = fa_street;
	document.getElementById('fa_city').value = fa_city;
	document.getElementById('fa_psc').value = fa_psc;
	document.getElementById('fa_state').value = fa_state;
	document.getElementById('fa_dic').value = fa_dic;
	document.getElementById('fa_phone').value = fa_phone;
	document.getElementById('fa_fax').value = fa_fax;
	
	document.getElementById('do_firstname').value = do_firstname;
	document.getElementById('do_surname').value = do_surname;
	document.getElementById('do_company').value = do_company;
	document.getElementById('do_street').value = do_street;
	document.getElementById('do_city').value = do_city;
	document.getElementById('do_psc').value = do_psc;
	document.getElementById('do_state').value = do_state;
	document.getElementById('do_phone').value = do_phone;
	document.getElementById('do_fax').value = do_fax;
	
	document.getElementById('email1').value = email;
	document.getElementById('email2').value = email;
}



//
// PAYMENT
//
function setPayment(){
	radioObj = document.forms['paymentMethod'].elements['payment']
	for(var i = 0; i < 3; i++) {
		if(radioObj[i].checked)
			paymentMethod = radioObj[i].value;
	}
}



//
// SUMMARY
//
function changeCurrency(){
	document.getElementById('homePriceCurrency').innerHTML = currency;
	document.getElementById('profiPriceCurrency').innerHTML = currency;
	document.getElementById('boxPriceCurrency').innerHTML = currency;
	document.getElementById('priceCurrency').innerHTML = currency;
	document.getElementById('fullPriceCurrency').innerHTML = currency;
	document.getElementById('fullPrice2Currency').innerHTML = currency;
	document.getElementById('shippingCurrency').innerHTML = currency;
	document.getElementById('fullPrice3Currency').innerHTML = currency;
}
function recalculateSummary(){
	changeCurrency();
	changePricesByCurrency();
	document.getElementById('homePrice').innerHTML = (homeCount * homePrice).toFixed(0);
	document.getElementById('profiPrice').innerHTML = (profiCount * profiPrice).toFixed(0);
	document.getElementById('homeCount').innerHTML = homeCount;
	document.getElementById('profiCount').innerHTML = profiCount;
	if(boxes){
		document.getElementById('boxCount').innerHTML = (homeCount*1) + (profiCount*1);
		document.getElementById('boxPrice').innerHTML = ((homeCount*1) + (profiCount*1)) * boxPrice;
		hp = homePrice+boxPrice;
		pp = profiPrice+boxPrice;
	}
	else{
		hp = homePrice;
		pp = profiPrice;
	}
	document.getElementById('price').innerHTML = ((homeCount * hp) + (profiCount * pp)).toFixed(0);
	tmp = ((homeCount * hp * 1.20) + (profiCount * pp * 1.20)).toFixed(0);
	document.getElementById('fullPrice').innerHTML = tmp;
	document.getElementById('fullPrice2').innerHTML = tmp;
	boxes = boxes * 1;
	if(paymentMethod == 'dobirka' || boxes){
		if(paymentMethod == 'dobirka') shippingPrice = shippingPrice + codPrice;
		document.getElementById('shipping').innerHTML = shippingPrice;
		document.getElementById('fullPrice3').innerHTML = ((homeCount * hp * 1.20) + (profiCount * pp * 1.20) + (shippingPrice * 1)).toFixed(0);
	}
	else document.getElementById('fullPrice3').innerHTML = ((homeCount * hp * 1.20) + (profiCount * pp * 1.20)).toFixed(0);
}
function fillOrderForm(){
	document.getElementById('order').email.value = email;
	document.getElementById('order').paymentMethod.value = paymentMethod;
	document.getElementById('order').lang.value = language;
	document.getElementById('order').litem1.value = litem1;
	document.getElementById('order').litem2.value = litem2;
	document.getElementById('order').litem3.value = litem3;
	document.getElementById('order').litem4.value = litem4;
	document.getElementById('order').licenceVersion.value = licenceVersion;
	document.getElementById('order').currency.value = currency;
	
	document.getElementById('order').homeCount.value = homeCount;
	document.getElementById('order').profiCount.value = profiCount;
	document.getElementById('order').homePrice.value = homePrice;
	document.getElementById('order').profiPrice.value = profiPrice;
	document.getElementById('order').boxPrice.value = boxPrice;
	boxes = boxes * 1;
	document.getElementById('order').boxes.value = boxes;
	if(paymentMethod == 'dobirka' || boxes > 0){
		document.getElementById('order').shipping.value = shippingPrice;
	}
	else
		document.getElementById('order').shipping.value = 0;
	
	document.getElementById('order').fa_firstname.value = fa_firstname;
	document.getElementById('order').fa_surname.value = fa_surname;
	document.getElementById('order').fa_company.value = fa_company;
	document.getElementById('order').fa_street.value = fa_street;
	document.getElementById('order').fa_city.value = fa_city;
	document.getElementById('order').fa_psc.value = fa_psc;
	document.getElementById('order').fa_state.value = fa_state;
	document.getElementById('order').fa_dic.value = fa_dic;
	document.getElementById('order').fa_phone.value = fa_phone;
	document.getElementById('order').fa_fax.value = fa_fax;
	
	document.getElementById('order').do_firstname.value = do_firstname;
	document.getElementById('order').do_surname.value = do_surname;
	document.getElementById('order').do_company.value = do_company;
	document.getElementById('order').do_street.value = do_street;
	document.getElementById('order').do_city.value = do_city;
	document.getElementById('order').do_psc.value = do_psc;
	document.getElementById('order').do_state.value = do_state;
	document.getElementById('order').do_phone.value = do_phone;
	document.getElementById('order').do_fax.value = do_fax;
}



//
// SUMMARY UPGRADE
//
function recalculateSummaryUpgrade(){
	changeCurrency();
	changePricesByCurrency();
	if(licenceVersion == 'home') homeCount = 1;
	if(licenceVersion == 'profi') profiCount = 1;
	homePrice = homeUpgradePrice;
	profiPrice = profiUpgradePrice;
	document.getElementById('homePrice').innerHTML = (homeCount * homePrice).toFixed(0);
	document.getElementById('profiPrice').innerHTML = (profiCount * profiPrice).toFixed(0);
	document.getElementById('boxPrice').innerHTML = ((homeCount*1) + (profiCount*1)) * boxPrice;
	hp = homePrice+boxPrice;
	pp = profiPrice+boxPrice;
	document.getElementById('price').innerHTML = ((homeCount * hp) + (profiCount * pp)).toFixed(0);
	document.getElementById('fullPrice').innerHTML = ((homeCount * hp * 1.20) + (profiCount * pp * 1.20)).toFixed(0);
	document.getElementById('fullPrice2').innerHTML = ((homeCount * hp * 1.20) + (profiCount * pp * 1.20)).toFixed(0);
	boxes = boxes * 1;
	if(paymentMethod == 'dobirka' || boxes > 0 || litem1){
		if(paymentMethod == 'dobirka') shippingPrice = shippingPrice + codPrice;
		document.getElementById('fullPrice3').innerHTML = ((homeCount * hp * 1.20) + (profiCount * pp * 1.20) + (shippingPrice * 1)).toFixed(0);
		document.getElementById('shipping').innerHTML = shippingPrice;
	}
	else document.getElementById('fullPrice3').innerHTML = ((homeCount * hp * 1.20) + (profiCount * pp * 1.20)).toFixed(0);
}



//
// UPGRADE
//
function checkLicenceNumber(item1, item2, item3, item4, version){
	$.ajax({
	   type: "POST",
	   url: "http://" + location.hostname + "/checkLicence_" + language + ".php",
	   data: "item1=" + item1 + "&item2=" + item2 + "&item3=" + item3 + "&item4=" + item4 + "&version=" + version,
	   scriptCharset: "utf-8",
	   success: function(msg){
		   litem1 = item1;
		   litem2 = item2;
		   litem3 = item3;
		   litem4 = item4;
		   boxes = 1;
		   $("#shop").html(msg);
	   }
	});
}
function changeUpgradeCurrency(){
	document.getElementById('homePriceCurrency').innerHTML = currency;
	document.getElementById('profiPriceCurrency').innerHTML = currency;
}
function recalculateUpgrade(){
	changePricesByCurrency();
	changeUpgradeCurrency();
	document.getElementById('homePrice').innerHTML = homeUpgradePrice;
	document.getElementById('profiPrice').innerHTML = profiUpgradePrice;
}

