// Ehitelek Javascript függvények

function changeEszFajta(tipus)
{
    // puska
//  car
//    <option <?php if($_data['EszkFajta'] == 'szgk') echo 'selected="selected"' ?> value="szgk">Személygépkocsi</option>
//    <option <?php if($_data['EszkFajta'] == 'kishaszon') echo 'selected="selected"' ?> value="kishaszon">Kishaszonjármű (3,5t-ig)</option>
//  bike
//    <option <?php if($_data['EszkFajta'] == 'tura') echo 'selected="selected"' ?> value="tura">Túra/Chopper</option>
//    <option <?php if($_data['EszkFajta'] == 'robogo') echo 'selected="selected"' ?> value="robogo">Robogó/Sport</option>
    if(tipus == 'car')
    {
        sel_eszkfajta.options.length = 0;
        sel_eszkfajta.options.length = 2;
        sel_eszkfajta.options[0] = new Option('Személygépkocsi', 'szgk', true, false);
        sel_eszkfajta.options[1] = new Option('Kishaszonjármű', 'kishaszon', true, false);
    }
    if(tipus == 'bike')
    {
        sel_eszkfajta.options.length = 0;
        sel_eszkfajta.options.length = 2;
        sel_eszkfajta.options[0] = new Option('Túra/Chopper', 'tura', true, false);
        sel_eszkfajta.options[1] = new Option('Robogó/Sport', 'robogo', true, false);
    }
}

function setProcMsg()
{
    resultdiv.innerHTML = '<span style="margin-left:5px;">kalkuláció folyamatban...</span>';
}

function reverse( string )
{
	var reversed = "";

	for(i=string.length-1; i>=0; i--)
	{
		reversed += string.slice(i, i+1);
	}

	return reversed;
}


function formatMoney( input )
{
	var moneytmp = "";
	var money = reverse(input.value);
	money = money.replace( /\./g, '' );

    var ertek = money;

	if (isNaN(ertek)) {
        if (hasNumbers(ertek)) {
            // spaceket törli
            ertek = ertek.replace( /\s/g, "" );
            // megkeresi a számot
            ertek = ertek.replace(/[^0123456789]+/g, '');
            //alert(ertek);
        }
        else {
			ertek = '';
        }
    }

	money = ertek;

	for(i=0; i+3<=money.length; )
	{
		var ii = i+3;
		moneytmp += (money.slice(i, ii))+".";
		money = money.slice(ii);
	}
	moneytmp += money;
	if(moneytmp.slice(moneytmp.length-1, moneytmp.length) == ".") moneytmp = moneytmp.slice(0, moneytmp.length-1);

	input.value = reverse(moneytmp);
}

function delText( input )
{
	input.value = '';
}

function hasNumbers(t)
{
    var regex = /\d/;
    return regex.test(t);
}


// számkorrekciós függvény - spaceket, nemszám karaktereket törli
// - itt közvetlenül nem használt
function szam_korr(elem) {
    //ertek = ertek.replace(/(,)/,'.');

    var ertek = elem.value;

    if (isNaN(ertek)) {
        if (hasNumbers(ertek)) {
            // spaceket törli
            ertek = ertek.replace( /\s/g, "" );
            // megkeresi a számot
            ertek = ertek.replace(/[^0123456789]+/g, '');
            //alert(ertek);
            elem.value = ertek;
        }
        else {
            elem.value = '';
        }
    }
}

// telefonszam korrekció
function tel_korr(input)
{
    var ertek = input.value;

	if (isNaN(ertek))
	{
            // spaceket törli
            //ertek = ertek.replace( /\s/g, "" );
            // megkeresi a számot
            ertek = ertek.replace(/[^0123456789 +\-()/#\[\]\\]+/g, '');
            //alert(ertek);
            input.value = ertek;
    }
}


function check_contacts(input)
{
	if ((input.phone.value == '') && (input.email.value != ''))
	{
		var phone = prompt('Kérjük adja meg telefonszámát is, hogy munkatársunk felkeresse Önt!', 'Telefonszám helye');
		input.phone.value = phone;
	}

	return;
}


function switch_type()
{
    var elem = '';
    var elem2 = '';
    if (document.getElementById('select4').value == 1)
    //if (document.getElementById('casco_check').checked == 1)
    {
        document.getElementById('select5').options.length = 0;
        document.getElementById('select5').options.length = document.getElementById('brand_p_num').value;
        var k = 0;
        while(elem = document.getElementsByName('brand_p_v_'+k))
        {
            elem = elem[0];
            elem2 = document.getElementsByName('brand_p_l_'+k);
            elem2 = elem2[0];
            document.getElementById('select5').options[k]=new Option(elem2.value, elem.value, true, false);
            k++;
        }
    }
    else
    {
        document.getElementById('select5').options.length = 0;
        document.getElementById('select5').options.length = document.getElementById('brand_b_num').value;
        k = 0;
        while(elem = document.getElementsByName('brand_b_v_'+k))
        {
            elem = elem[0];
            elem2 = document.getElementsByName('brand_b_l_'+k);
            elem2 = elem2[0];
            document.getElementById('select5').options[k]=new Option(elem2.value, elem.value, true, false);
            k++;
        }
    }
}


function enable_casco(elem)
{
    if (elem.checked)
    {
        //irsz, select5, select6
        document.getElementById('irsz').disabled = false;
        document.getElementById('select5').disabled = false;
        document.getElementById('select6').disabled = false;
    }
    else
    {
        document.getElementById('irsz').disabled = true;
        document.getElementById('select5').disabled = true;
        document.getElementById('select6').disabled = true;
    }
}

// ugrik köv mezőre
function c4(val) {
    var value = val.value;
    if (value.length == 4 )  { dhonap.focus(); }
}

function c2(val) {
    var value = val.value;
    if (value.length == 2 )  { dnap.focus(); }
}


function helyiertek(szam) {
    szam_korr(szam);
    var sepchar =" ";
    var value=szam.value;
    r=value.split(sepchar);
    value=r.join("");
    hossz=value.length;
    tmp=parseInt(value.substring(hossz-1,hossz));
    if(tmp>=0 && tmp<=9){
    } else {
        value=value.substring(0,hossz-1);
    }
    hossz=value.length;
    if (hossz>1 && parseInt(value.substring(0,1)) <= 0 ) {
        value='0';
    }
    if(hossz>=10) {
        value=value.substring(0, hossz-9)+sepchar+value.substring(hossz-9, hossz);
        hossz=value.length;
    }
    if(hossz>=7) {
        value=value.substring(0, hossz-6)+sepchar+value.substring(hossz-6, hossz);
        hossz=value.length;
    }
    if(hossz>=4) {
        szam.value=value.substring(0, hossz-3)+sepchar+value.substring(hossz-3, hossz);
    } else {
        szam.value = value;
    }
}



function szazalek2ft()
{
	x=parseInt(SajatResz.value.toString().replace(/ /g,"") );
    if(isNumeric(SajatResz2.value.toString().replace(/ /g,"") ))
    {
        x2=parseInt(SajatResz2.value.toString().replace(/ /g,"") );
        if(x2 > 100)
        {
            SajatResz2.value = SajatResz2.value.substring(0,SajatResz2.value.length-1);
            alert('A sajátrész nem lehet nagyobb 100%-nál!');
            return;
        }
    }
    else
        return;

    if(isNumeric(BrAr.value.toString().replace(/ /g,"")))
    {
		y=parseInt(BrAr.value.toString().replace(/ /g,"") );
    }
    else
        return;
    SajatResz.value = parseInt((y/100)*x2, 0);
}

function ft2szazalek()
{
    if(isNumeric(SajatResz.value.toString().replace(/ /g,"") ))
    {
        x=parseInt(SajatResz.value.toString().replace(/ /g,"") );
    }
    else
        return;

    x2=parseInt(SajatResz2.value.toString().replace(/ /g,"") );
    if(isNumeric(BrAr.value.toString().replace(/ /g,"") ))
    {
		y=parseInt(BrAr.value.toString().replace(/ /g,"") );
    }
    else
        return;

    var szazalek = (x/y)*100;
    SajatResz2.value = szazalek.toFixed(0);
}

function isNumeric(strString)
//  check for valid numeric strings
{
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
    return blnResult;
}
