var gbCurrencyName = "Kč";
var gbCurrencyBeforeAmount = false;
var gbCurrencyDecimalSeparator = ",";
var gbCurrencyThousandSeparator = "";
var gbCurrencySuffixNotDecimal = ",-";
var gbCurrencyDigitsAfterDecimalSeparator = 0;
var gbDisableVazbySelect = false;
function UpdateDeliveryPriceOnOrder(IdNewDelivery) {
	var nPrice, nDPH, nPriceSum;
    if ( !getE('DeliveryPrice') || !getE('DeliveryPrice_'+IdNewDelivery)) {alert('Došlo k nečekané chybě');return false;}
    var nPriceDelivery_Old = ConvertToNum( getE('DeliveryPrice').value );
    var nDPHDelivery_Old   = ConvertToNum( getE('DeliveryDPH').value );
    var nPriceDelivery     = ConvertToNum( getE('DeliveryPrice_'+IdNewDelivery).value );
    var nDPHDelivery       = ConvertToNum( getE('DeliveryDPH_'+IdNewDelivery).value );
    var sNameDelivery      = getE('DeliveryName_'+IdNewDelivery).value;
    getE('DeliveryPrice').value = nPriceDelivery; getE('DeliveryDPH').value = nDPHDelivery; getE('DeliveryName').value = sNameDelivery; nPrice = 0;
    if (nDPHDelivery_Old == nDPHDelivery) nPrice =  nPriceDelivery;
    ReSazba(nDPHDelivery_Old, nPrice);
    if (nDPHDelivery_Old != nDPHDelivery)     ReSazba(nDPHDelivery, nPriceDelivery);
    Summary(nPriceDelivery, nDPHDelivery);
	if (getE('tdDelItemName')) getE('tdDelItemName').innerHTML = sNameDelivery;
    if (getE('tdDelItemPrice')) getE('tdDelItemPrice').innerHTML = FormatCurrency(nPriceDelivery*((nDPHDelivery+100)*0.01),0);
    if (getE('tdDelItemPrice2')) getE('tdDelItemPrice2').innerHTML = FormatCurrency(nPriceDelivery*((nDPHDelivery+100)*0.01),0);

    //if (!gbDisableVazbySelect) VazbySelect(gvsOutput1, gvsOutput2, IdNewDelivery);
    if (!gbDisableVazbySelect) VazbySelect2(gvsOutput1, gvsOutput2, IdNewDelivery);
}


function VazbySelect(s1,s2,pkDel){
	var s1pole,s2pole,s1pom,s1p,pkP,NazevP,iHtml;
	
	var oPayment = getE('PaymentType');
	if (oPayment == null) return false;
	
	while (oPayment.length>0) oPayment.remove(0);
	
	iHtml = '<select tabindex="213" id="i213" size="1" name="PaymentType" class="INP RSEL"  style="width:201px">';
	s1pole = s1.split('$$$$');
	s2pole = s2.split('$$$$');
	for (i=0;i<s2pole.length-1; i++){
		if (s2pole[i] == pkDel){
			s1pom = s1pole[i].split('##');
			for(j=0;j<s1pom.length-1;j++){
				s1p = s1pom[j].split('||');
				var node=document.createElement('option');
                node.value = s1p[0]; node.text = s1p[1];
				try {oPayment.add(node, null);} catch(ex) {oPayment.add(node);}
			}
		}
	}
	
//    alert(iHtml);
}

function VazbySelect2(s1, s2, pkDel) {
    var s1pole, s2pole, s1pom, s1p, pkP, NazevP, iHtml;


    var oPayment = getE('platbybox');
    if (oPayment == null) return false;
    //oPayment.innerHTML = '';

    if (oPayment.hasChildNodes()) {
        while (oPayment.childNodes.length >= 1) {


            var ch = oPayment.firstChild;
            if (ch.hasChildNodes()) {
                while (ch.childNodes.length >= 1) {
                    ch.removeChild(ch.firstChild);
                }
            }
            
            
            oPayment.removeChild(oPayment.firstChild);
        }
    }
    var shtml='';
    
    s1pole = s1.split('$$$$');
    s2pole = s2.split('$$$$');
    for (i = 0; i < s2pole.length - 1; i++) {
        if (s2pole[i] == pkDel) {
            s1pom = s1pole[i].split('##');
            for (j = 0; j < s1pom.length - 1; j++) {
                s1p = s1pom[j].split('||');

                if (!Br.IE6 && !Br.IE7) {
                    var label = document.createElement('label'); label.setAttribute('for', 'PaymentType' + j);
                    var inp = document.createElement('input');
                    inp.type = 'radio';
                    inp.value = s1p[0];
                    inp.className = 'input_radio';
                    inp.id = 'PaymentType' + j;
                    inp.name = 'PaymentType';
                    if (j == 0) inp.checked = true;

                    label.appendChild(inp); label.appendChild(document.createTextNode(s1p[1]));
                    oPayment.appendChild(label);
                    oPayment.appendChild(document.createElement('br'));
                }
                else {
                    shtml = shtml + '<label for="PaymentType' + j + '"><input type="radio" value="' + s1p[0] + '" name="PaymentType" id="PaymentType' + j + '" ' + (j == 0 ? 'checked="checked"' : '') + ' />' + s1p[1] + '</lablel><br/>';
                
                }

                
                
            }
        }
    }
//    alert(shtml);
    if (Br.IE6 || Br.IE7) { oPayment.innerHTML = shtml; }
}

function DeliverySelect(pkDel){

	//gaDelivery - array from string 142$143$
	//gaDeliveryDPH - array from string 19$5$
	//gaPayment - array from string 41,43,$1,43,$
	//gaDefault - array from string 143,44  - defaultní ID dopravy a platby
	var oPTsel, sDeliveries, oPB, bPayOK;
	var bPaySelectedOK = false;
	var bPaySelected = false;
	alert(gaDelivery);
	for (var i=0; i < gaDelivery.length-1; i++){
		if (gaDelivery[i] == pkDel) {
			// provedu zobrazení/skrytí platebních metod podle vazby na dopravné
			for(var j = 0; ; j++) {
				oPT = getE('PaymentType' + j);
				if (!oPT) break;

                
				// nastavím bloku s typem platby modrou/šedou barvu
				//oPB = getE('PaymentBlock' + j);
				sPayments = ',' + gaPayment[i];
				bPayOK = (sPayments.indexOf(',' + oPT.value + ',') != -1);
				if (oPT.checked)
				{
					bPaySelectedOK = bPayOK; // je vybraná plat. met. stále možná i s nově zvoleným dopravným? existuje vazba?
                }
                
				if (bPayOK) 
				{
					if (!bPaySelected) { oPTsel = oPT; bPaySelected = true;	}
				}
				oPT.disabled = !bPayOK;
				if (!bPaySelected) oPT.checked = true;
			}
			
			// pokud je vybrána platební metoda, kterou není možné provést s vybraným typem dopravy, změním to na první možnou volbu
			if (bPaySelected && !bPaySelectedOK)
				if (pkDel == gaDefault[0]) {
					for (var j = 0; ; j++) {
						oPT = getE('PaymentType' + j);
						if (!oPT) break;
						if (oPT.value == gaDefault[1]) oPT.checked = true;
					}
				}
				else oPTsel.checked = true;
			break;
		}
		else {
			for (var j = 0; ; j++) {
				oPT = getE('PaymentType' + j);
				if (!oPT) break;
				oPT.disabled = !(oPT.value == gaDefault[1]);
				if (oPT.value == gaDefault[1]) {
					oPT.checked = true;
					//getE('PaymentBlock' + j).className = "PayDeliveryType";
					}
				else {
					//getE('PaymentBlock' + j).className = "PayDeliveryType payKO";
				}
			}
		}
	}
	
	shCScard();
	if (getE('DeliveryPrice')) getE('DeliveryPrice').value = getE('DeliveryPrice_'+pkDel).value;
	if (getE('DeliveryName')) getE('DeliveryName').value = getE('DeliveryName_'+pkDel).value;
	if (getE('DeliveryDPH')) getE('DeliveryDPH').value = getE('DeliveryDPH_'+pkDel).value;
}
function shCScard()
{
    if (getE('csID') == null || getE('cType') == null) return false;
    getE('cType').style.display = (getE('csID').value == getE('PaymentType').options[getE('PaymentType').selectedIndex].value) ? 'block' : 'none';
}

function ReSazba(nDPHValue, nPrice) 
{
  if (!getE('taxTable')) return false;
  var nHidden = ConvertToNum( getE('ValueTAX_'+nDPHValue).value );
  nPrice += nHidden;
  var nDPH        = ConvertToNum(nPrice * 0.01 * nDPHValue );
  var nPriceSum   = ConvertToNum(nPrice + nDPH);
    
  if (nPrice == 0)   {getE('trDRsazba_' + nDPHValue).style.display = 'none';}
  else
  {
        getE('tdDRbase_'  + nDPHValue).innerHTML = FormatCurrency(nPrice,2) + ' Kč';
        getE('tdDRdph_'   + nDPHValue).innerHTML  = FormatCurrency(nDPH,2) + ' Kč';
        getE('tdDRsum_'   + nDPHValue).innerHTML  = FormatCurrency(nPriceSum,2) + ' Kč';
        getE('trDRsazba_' + nDPHValue).style.display = 'block';
  }
}

function Summary(nDeliveryPrice, nDeliveryDPH) {
    var aSazby = new Array(0,5,9,15,19,10,20,22);    
    var i, nSumPrice, nSumDPH, nSumPriceDPH, nPrice, nRound;
    
    var nPriceRecycle = 0;//ConvertToNum(getE('nPriceRecycleWDPH').value);
    var nPriceAutor   = 0;//ConvertToNum(getE('nPriceAutorWDPH').value);
    var nCoupon       = 0;//ConvertToNum( (getE('nCouponPrice') ? getE('nCouponPrice').value : '0') );
    
    nSumPrice = nSumDPH = nSumPriceDPH = nPrice = nRound = 0;
    
    for (i=0;i<aSazby.length;i++)
    {
        if ( getE('ValueTAX_' + aSazby[i]) )
        {
            nPrice = ConvertToNum( getE('ValueTAX_' + aSazby[i]).value );
            if ( aSazby[i] == nDeliveryDPH ) nPrice = nPrice + nDeliveryPrice;
            nSumPrice = nSumPrice + nPrice; nSumDPH = nSumDPH + (nPrice*0.01*aSazby[i]);
        }
    }
    nSumPriceDPH = nSumPrice + nSumDPH;


   if (getE('tdDRbase')) getE('tdDRbase').innerHTML = FormatCurrency(nSumPrice, 2) + ' Kč';
   if (getE('tdDRdph'))  getE('tdDRdph').innerHTML = FormatCurrency(nSumDPH, 2) + ' Kč';
   if (getE('tdDRsum'))  getE('tdDRsum').innerHTML = FormatCurrency(nSumPriceDPH, 2) + ' Kč';
    
        nSumPriceDPH += nPriceRecycle+nPriceAutor+nCoupon;
		nRound = ( Math.round(nSumPriceDPH) - nSumPriceDPH).toFixed(2);
		
   if (getE('tdDRsum'))  		
   {
		if (nRound != 0) {getE('trDRround').style.display = '';getE('tdDRround').innerHTML = nRound.replace('.',',') + ' Kč'; }
		else { getE('trDRround').style.display = 'none';}
   }
		nSumPriceDPH = (parseFloat(nSumPriceDPH) + parseFloat(nRound)).toFixed(2);
   if (getE('tdDRSumToPay')) getE('tdDRSumToPay').innerHTML = FUCJS(nSumPriceDPH, 0, true);
   if (getE('tdDRSumToPay2')) getE('tdDRSumToPay2').innerHTML = FUCJS(nSumPriceDPH, 0, true);

}   

function ConvertToNum(sNum)
{
    var nNum = (sNum+'').replace(',','.'); sNum = parseFloat( nNum ); nNum = sNum*1;
    if ( isNaN(nNum) ) nNum = 0;
    return nNum;
}
function controlOrderSubmit()
{
    if ( !ChckF( 'lastname', 'Vyplňte příjmení fakturační adresy.') )                    { return false; }
    if ( !ChckF( 'firstname', 'Vyplňte jméno fakturační adresy.') )                      { return false; }

    if ( !ChckF( 'street', 'Vyplňte ulici fakturační adresy.') )                         { return false; }
    if ( !ChckF( 'city', 'Vyplňte město fakturační adresy.') )                           { return false; }
    if ( !ChckF( 'zipcode', 'Vyplňte PSČ fakturační adresy.') )                          { return false; }

  
    if ( !EmailControl(getE('email').value) ) {getE('email').focus(); return false;}
    if ( getE('Phone').value + getE('Mobil').value == '' ) {alert('Vyplňte telefon nebo mobil.'); getE('Phone').focus(); return false;}
  
    
    if ( getE('firstnameDelivery').value && getE('lastnameDelivery').value+getE('streetDelivery').value + getE('cityDelivery').value + getE('zipcodeDelivery').value+ getE('firmDelivery').value != '')
    {
        if ( !ChckF( 'lastnameDelivery',  'Vyplňte příjmení u dodací adresy.') )   { return false; }
        if ( !ChckF( 'firstnameDelivery', 'Vyplňte jméno u dodací adresy.') )      { return false; }
        if ( !ChckF( 'streetDelivery',    'Vyplňte ulici dodací adresy.') )        { return false; }
        if ( !ChckF( 'cityDelivery',      'Vyplňte město dodací adresy.') )        { return false; }
        if ( !ChckF( 'zipcodeDelivery',   'Vyplňte PSČ dodací adresy.') )          { return false; }
    }      
    return true;
}

function ControlDataOrder(){
	var isCheck = false;
	if (typeof(getF('OrderForm').DeliveryType.length) == 'number') {
		for (var i=0; i < getF('OrderForm').DeliveryType.length; i++) {if (getF('OrderForm').DeliveryType[i].checked) isCheck = true;}
	}
	else {if (getF('OrderForm').DeliveryType.checked) isCheck = true;}
	if (!isCheck) {alert("Vyberte způsob dopravy."); return false;}

	isCheck = false;
	if (typeof(getF('OrderForm').PaymentType.length) == 'number') {
		for (var i=0; i < getF('OrderForm').PaymentType.length; i++) {if (getF('OrderForm').PaymentType[i].checked) isCheck = true;}
	}
	else {if (getF('OrderForm').PaymentType.checked) isCheck = true;}
	if (!isCheck) {alert("Vyberte způsob platby.");	return false;}
}

function ControlUserDataSubmit(){

   
    if ( !ChckF( 'lastname', 'Vyplňte příjmení fakturační adresy.') )                    { return false; }
    if ( !ChckF( 'firstname', 'Vyplňte jméno fakturační adresy.') )                      { return false; }
   
    if ( !ChckF( 'street', 'Vyplňte ulici fakturační adresy.') )                         { return false; }
    if ( !ChckF( 'city', 'Vyplňte město fakturační adresy.') )                           { return false; }
    if ( !ChckF( 'zipcode', 'Vyplňte PSČ fakturační adresy.') )                          { return false; }

    if ( getE('ico').value != '' && !icoValidate(getE('ico').value) ) { alert('Chybný formát IČ.'); getE('ico').focus(); return false; }      

    if (getE('firmDelivery').value+getE('lastnameDelivery').value+getE('firstnameDelivery').value+getE('streetDelivery').value+getE('cityDelivery').value+getE('zipcodeDelivery').value != '')
    {
        if ( !ChckF( 'lastnameDelivery', 'Vyplňte příjmení dodací adresy.') )                    { return false; }
        if ( !ChckF( 'firstnameDelivery', 'Vyplňte jméno dodací adresy.') )                      { return false; }
       
        if ( !ChckF( 'streetDelivery', 'Vyplňte ulici dodací adresy.') )                         { return false; }
        if ( !ChckF( 'cityDelivery', 'Vyplňte město dodací adresy.') )                           { return false; }
        if ( !ChckF( 'zipcodeDelivery', 'Vyplňte PSČ dodací adresy.') )                          { return false; }
    }


    //if ( !ValidateAccount( getE('BankAccount').value ) ){ alert("Bankovní účet není platný, zkuste jej zadat znovu, nebo pole nevyplňujte."); getE('BankAccount').focus();return false };
    if ( !EmailControl(getE('email').value) ) {getE('email').focus(); return false;}
    if ( getE('Mobil').value == '' ) {alert('Vyplňte mobil.'); getE('Mobil').focus(); return false;}
    
    if ( !ChckF( 'Login', 'Vyplňte přihlašovací jméno.') )  { return false; }
    if ( getE('Login').value.length < 5) {alert("Přihlašovací jméno musí mít nejméně 5 znaků.");getE('Login').focus();return false;}
    
    if ( !ChckF( 'Password', 'Vyplňte heslo.') ) { return false; }

    if ( getE('Password').value.length < 5) {alert("Heslo musí mít nejméně 5 znaků.");getE('Password').focus();return false;}
    if ( getE('Password').value != getE('PasswordCheck').value) {alert("Potvrzené heslo není správně.");getE('PasswordCheck').focus();return false;}

    if ( getE('souhlas') && !getE('souhlas').checked ) {alert("Před odesláním registrace je nutno kliknutím na zatržítko vyjadřit svůj souhlas s obchodními podmínkami.");getE('souhlas').focus();return false;}
    if ( getE('souhlas2') && !getE('souhlas2').checked ) {alert("Před odesláním registrace je nutno kliknutím na zatržítko vyjadřit svůj souhlas s podmínkami o ochraně osobních údajů.");getE('souhlas2').focus();return false;}
    
    if ( getE('sChckCodeReg') && !ChckF( 'sChckCodeReg', 'Vyplňte ověřovací kód.'))  {return false;}
    
    return true;   
}
function showAvail(e, id)
{
    if (getE(id) == null) return false;
    var src = (window.event)? window.event.srcElement : e.target;
   

	//getE(id).style.top  =  (e.clientY-315)+'px';
	getE(id).style.left =  '345px';
	getE(id).style.display = 'block';
}

function FUCJS(nNumber, nDecimal, bCurrSymbol) {
	var sCurrency='0';
	nDecimal = (nDecimal > 0) ? nDecimal : gbCurrencyDigitsAfterDecimalSeparator;
	if (parseFloat(nNumber)) {sCurrency = FormatCurrency(nNumber, nDecimal);}
	sCurrency = sCurrency.replace(" ", gbCurrencyThousandSeparator);
	sCurrency = sCurrency.replace(",", gbCurrencyDecimalSeparator);
	//if (nDecimal == 0) sCurrency = sCurrency + gbCurrencySuffixNotDecimal;
	
	if (bCurrSymbol) {
		if (gbCurrencyBeforeAmount)
			sCurrency = "<small>" + gbCurrencyName + "</small>&nbsp;" + sCurrency;
		else
			sCurrency = sCurrency + "<small>" + gbCurrencyName + "</small>";
	}
	return sCurrency;
}

function ValidateAccount(sAccount) {
	var u1,u2,t1,t2,i;
	var vahy = new Array("1","2","4","8","5","10","9","7","3","6")
	u1 = sAccount.substring(0,sAccount.length - 10);
	u2 = sAccount.substring(sAccount.length - 10,12);
	t1 = 0;t2 = 0;
	for(i=u1.length ;i>0;i--) {
		t1 += u1.charAt(i - 1)*vahy[10 - i];
	}	  
	for(i=u2.length ;i>0;i--) {
		t2 += u2.charAt(i - 1)*vahy[10 - i];
	}
	return (((t2%11) == 0)&&((t1%11 == 0)))
}

function icoValidate(sIco)
{
   if (sIco.replace(/^\d{8}$/,"") != "" ) return false;
    
    var a = 0;
    for (i = 0; i < 7; i++) {a += sIco.substring(i,i+1) * (8 - i);
    }
    
    a = a % 11;
    var c = 0;
    if (a === 0) c = 1;
    else if (a === 10) c = 1;
    else if (a === 1) c = 0;
    else c = 11 - a;

    return (sIco.substring(7,8) == c);
}


