//A JavaScript function similar to the PHP include to include other JS file in the current JS file
function js_include(jsFile)
{
  var url = 'http://ultrasilvam.ro';
  document.writeln('<script type="text/javascript" src="' + url + jsFile + '"></scr' + 'ipt>');
}
// JavaScript Document
js_include('/JavaScript/jQuery/SuperFishMenu/superfish.js');

//An array containing all the functions to be executed by __init__ function
var f = ['CheckNewSubscriber', 'FillVideo'];

//Execute this function on document.ready: includes all functions
function __init__(options)
{
    start(f,options); // iterate through the available function list and try to execute each function in the 'f' var
}

//Start the functions
function start(f /*the function list*/, options /*the options object*/)
{
    for(i in f)
    {
        var fn = f[i]; // get the function name
        if(typeof( window[fn] ) == 'function')
            try{window[fn](options);}catch(err){alert(err);} // try to execute the function
        else
            return false;
    }
}


function FillVideo(options)
{
    if(options.page != 'inscriere')
    {
        var fo = new FlashObject('/flashplayer/video_player.swf?fileVideo=', "FlashObject", 397, 221, "6", "");
        fo.addParam("allowScriptAccess", "sameDomain");
        fo.addParam("quality", "best");
        fo.addParam("loop", "true");
        fo.addParam("allowFullScreen", "true");
        fo.addParam("wmode", "transparent");
        fo.write('Player');
    }

    return false;
}


function SuperFishMenu(options)
{
    var menuOptions =
    {
      delay:       0,                  // one second delay on mouseout
      speed:       'fast',               // faster animation speed
      autoArrows:  false                 // disable generation of arrow mark-up
    };

    $("ul.sf-menu").superfish(menuOptions);
}


function ResetForm(form)
{
    var f = $('#'+form)[0];

    hideCompanyFields();
    f.reset();

    return false;
}

function showCompanyFields()
{
    checkRadio();
    $('.u-persJuridica').fadeIn();
    return false;
}

function hideCompanyFields()
{
    checkRadio();
    checked = false;
    $('.u-persJuridica').fadeOut();
    return false;
}

var oneChecked = false;
var checked = false;

function checkRadio()
{
    oneChecked = true;
}

function checkElement(el)
{
    var elName = el.attr('id');
    var val = el.val(); // Get the value of the checked element
    var size = val.length; // Get the numeric length of the returned value
    var type = el.attr('type');


    if(type == 'radio')
    {
        if(!oneChecked)
        {
            alert('Trebuie sa selectezi o modalitate de plata!');
            el.focus();
            return false;
        }

        checked = $('#plata2').attr('checked');
        alert(checked);
    }

    if(checked)
    {
        if(val == '')
        {
            if(elName == 'pj_nume')
            {
                alert('Te rugam sa introduci numele persoanei juridice!')
                el.focus();
                return false;
            }
            if(elName == 'pj_adresa')
            {
                alert('Te rugam sa introduci adresa sediului social!')
                el.focus();
                return false;
            }

            if(elName == 'pj_banca')
            {
                alert('Te rugam sa introduci banca persoanei juridice!')
                el.focus();
                return false;
            }
            if(elName == 'pj_cont')
            {
                alert('Te rugam sa introduci contul IBAN al persoanei juridice!')
                el.focus();
                return false;
            }
        }
    }


    if(val == '')
    {
        if(elName == 'nume')
        {
            alert('Trebuie sa iti completezi numele!');
            el.focus();
            return false;
        }

        if(elName == 'prenume')
        {
            alert('Trebuie sa iti completezi prenumele!');
            el.focus();
            return false;
        }

        if(elName == 'adresa')
        {
            alert('Trebuie sa iti completezi adresa permanenta!');
            el.focus();
            return false;
        }

        if(elName == 'program')
        {
            alert('Trebuie sa selectezi programul pentru care te inscrii!');
            el.focus();
            return false;
        }


        if(elName == 'locatie')
        {
            alert('Trebuie sa completezi locatia unde doresti sa participi la curs!');
            el.focus();
            return false;
        }

        if(elName == 'studii')
        {
            alert('Trebuie sa completezi ultima institutie de invatamant absolvita, nota si data!');
            el.focus();
            return false;
        }

        if(elName == 'luna')
        {
            alert('Trebuie sa selectezi luna din an in care ai dori sa participi la curs!');
            el.focus();
            return false;
        }

        if(elName == 'cnp')
        {
            if(size != 13)
            {
                alert('CNP-ul trebuie sa contina 13 cifre! Te rugam sa verifici CNP-ul!');
                el.focus();
                return false;
            }

            if(!isNumeric(val))
            {
                alert('La CNP poti introduce doar cifre!');
                el.focus();
                return false;
            }
        }

        if(elName == 'serie_buletin')
        {
            if(size != 2)
            {
                alert('Seria cartii de identitate sau a buletinului poate sa contina doar 2 caractere!');
                el.focus();
                return false;
            }

            if(isNumeric(val))
            {
                alert('Seria cartii de identitate sau a buletinului nu poate contine cifre!');
                el.focus();
                return false;
            }
        }

        if(elName == 'numar_buletin')
        {
            if(size != 6)
            {
                alert('Numarul cartii de identitate sau a buletinului trebuie sa fie de 6 caractere!');
                el.focus();
                return false;
            }

            if(!isNumeric(val))
            {
                alert('Numarul cartii de identitate sau a buletinului poate contine doar cifre!');
                el.focus();
                return false;
            }
        }

        if(elName == 'telefon_fix' || elName == 'telefon mobil')
        {
            if(size < 10)
            {
                alert('Te rugam sa completezi numarul de telefon (cu prefix la telefoane fixe), fara spatii sau alte semne!');
                el.focus();
                return false;
            }

            if(!isNumeric(val))
            {
                alert('Te rugam sa introduci doar cifre!');
                el.focus();
                return false;
            }
        }
    }

    if(elName == 'email')
    {
        if(val == '')
        {
            alert('Te rugam sa introduci o adresa de email!');
            el.focus();
            return false;
        }

        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
        if(reg.test(val) == false)
        {
            alert("Te rugam sa introduci o adresa de email valida!\nCea introdusa de tine: \n\n"+ val+"\n\nnu este corecta!");
            el.focus();
            return false;
        }
    }

    return true;
}

function CheckNewSubscriber(options)
{
    var page = options.page;

    if(page != 'inscriere')
        return false;

    var form = $('#SubmitNewSubscribtion');



    function checkForm()
    {
        var result = true;

        $('input, select',form)
        .not(':button, :submit, :reset, :hidden')
        .each(function()
        {
            if(result)
            {
                var el = $(this); // Get the input element checked
                result = checkElement(el);
            }
        });

        return result;
        //----------------------
    }

    function executeSubmit()
    {
      var result = checkForm();

       if(result)
       {
           $('#SubmitNewSubscribtion').fadeOut("normal");
           sendData();
       }

        return false;

    }

    function showResponse(data)
    {
        if(checked)


        $('#SubmitNewSubscribtion').fadeOut()
        $('#U-FilledForm').fadeIn('slow');
        $('#U-FilledForm').html(data);
        return false;
    }

    function sendData()
    {
        var formData = $('#SubmitNewSubscribtion').formSerialize();

        var opts =
        {
            type: 'POST',
            url: '/Modules/Inscrieri/fisa-de-inscriere.php',
            data: formData,
            dataType: 'html',
            success: showResponse
        };

        $.ajax(opts);
        return false;
    }

    $('#U-submitForm').click(executeSubmit);

    return false;
}

function SendData()
{
    var form = $('#ReadyToSubmit');


    function checkForm()
    {
        var result = true;
        var c = $('#agreement').is(':checked');

        if(!c)
        {
            alert('Te rugam sa confirmi ca ai citit si inteles toate conditiile de participare si ca datele furnizate\n\
            in formularul de inscriere sunt corecte!');
            return false;
        }

        $('input:hidden',form)
        .each(function()
        {
            if(result)
            {
                var el = $(this); // Get the input element checked
                result = checkElement(el);
            }
        });

        return result;
        //----------------------
    }

    function clearData()
    {
        $('#U-ajaxLoader').fadeOut();
        $('#U-sendForm').fadeIn();
        ResetForm('ReadyToSubmit');
        $('.u-persJuridica').hide();
        $('#U-FilledForm').fadeOut().html('');
        ResetForm('SubmitNewSubscribtion');
        $('#SubmitNewSubscribtion').fadeIn();
        return false;
    }

    function showResponse(data)
    {
          clearData();

          if(data)
          {
              if(data.status == 'error')
                  alert('Datele tale exista deja in sistemul nostru!');

              if(data.status == 'success')
                  alert('Inscrierea la curs s-a efectuat cu succes!\nTe rugam sa iti verifici casuta de email!');
          }

          return false;
    }

    function sendData()
    {
        var formData = $('#ReadyToSubmit').formSerialize();

        //alert(formData);

        var opts =
        {
            type: 'POST',
            url: '/Modules/Inscrieri/Inscriere.class.php',
            data: formData,
            dataType: 'json',
            success: showResponse
        };

        $.ajax(opts);
        return false;
    }

    function executeSubmit()
    {
       var result = checkForm();

       if(result)
       {
           $('#U-sendForm').fadeOut('normal', function()
           {
                 $('#U-ajaxLoader').fadeIn('normal', sendData);
           });

       }

        return false;

    }

    executeSubmit();

    return false;
}

function EditForm()
{
    function editForm()
    {
        function callback()
        {
            $('#U-FilledForm').fadeOut().html('');
            $('#SubmitNewSubscribtion').fadeIn();
            return false;
        }

        $('#U-FilledForm').fadeOut('normal', callback)

        return false;
    }

    editForm();
    //$('#U-editForm').click(editForm);

    return false;
}


function isNumeric(val)
{
    return !isNaN(parseFloat(val)) && isFinite(val);
}


function ChangeStyle(options)
{
    var page = options.page;

    if(page != 'inscriere' && page != 'contact')
        return false;

    $('input, select').bind("mouseover mouseout", function(e){
        $(this).toggleClass("u-inputHover");
	})
    .bind("focus blur", function(e)
    {
        $(this).toggleClass("u-inputFocus");
    });
    return false;
}

function DetectIE(options)
{
    var page = options.page;

    if(page != 'inscriere')
        return false;

    function getInternetExplorerVersion()
    {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer')
        {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    }

    function checkVersion()
    {
        var msg = "You're not using Windows Internet Explorer.";
        var ver = getInternetExplorerVersion();
        if (ver > -1)
        {
            if (ver == 8.0)
                msg = "Folositi Internet Explorer 8. Va rugam pentru o mai buna experienta de navigare\n\
                        a acestui site sa utilizati Mozilla Firefox sau alta versiune de Internet Explorer!"
            alert(msg);
        }

    }

    checkVersion();
}

//$(document).ready(__init__(options));

