
$(function() {

    $('#email_result').hide();
    $('input.text-input').css({
        backgroundColor:"#FFFFFF"
    });
    $('input.text-input').focus(function(){
        $(this).css({
            backgroundColor:"#FFDDAA"
        });
    });
    $('input.text-input').blur(function(){
        $(this).css({
            backgroundColor:"#FFFFFF"
        });
    });


    $("#submit_btn").click(function() {
        // validate and process form
        // first hide any error messages
        $('.error').hide();


        var email = $("input#email").val();
        apos=email.indexOf("@");
        dotpos=email.lastIndexOf(".");
        if (apos<1||dotpos-apos<2)
        {
            $('#email_result').html(invalidEmailVar);
            $("#email_result").show();
            $("input#email").focus();
            return false;
        }

        var dataString = 'mail='+ email +"&time=" + new Date() ;

        $('#email_result').show();
        $('#email_result').html(mailSendingVar);

        $.ajax({
            type: "GET",
            url: "/mailsender",
            data: dataString,
            timeout: 20000,
            error: function(){
                alert(mailFailedVar);
            },
            success: function() {

                $('#emailForm').hide();
                $('#email_result').show();
                $('#email_result').html(mailOkVar)
                .hide()
                .fadeIn(1500, function() {
                    $('#email_result').hide();
                });
            }

        });
        return false;
    });
});

runOnLoad(function(){

    $("input#name").select().focus();
    $("a").tooltip();
});

function runOnLoad(f) {
    if (runOnLoad.loaded) f();
}


function clearBasketJobs(viewallurl) {
            $('#email_result').html(clearBasketMsgVar)
        .append(clearBasketConfirmVar)
        .hide()
        .fadeIn(1500, function() {
            $('#email_result').hide();
            clearSavedJobs();
             window.location = viewallurl;
        });
}

function settupBaskets(currentviewmode)
{
    var savedJobs = readCookie("savedJobs");
    if (savedJobs != null)
    {
        var jobIdStr = "" + unescape(savedJobs);
        var jobIds = jobIdStr.split(" ");
        for (i = 0; i < jobIds.length; i++)
        {

            var saveElement = document.getElementById(jobIds[i]);
            if (saveElement != null)
            {
                saveElement.innerHTML =removeVar;
            }
        }
    }

    var clearSaveJob ;
    var emailBasket;
    //these default modes values are defined in jbasket_en.dyn
    if (currentviewmode != null && currentviewmode == viewbasketmode)
    {
        //ok, show the submenu of empty and send email basket
        clearSaveJob = document.getElementById('clearSaveJob');
        emailBasket = document.getElementById('emailBasket');
        if (clearSaveJob != null)
            clearSaveJob.style.visibility = 'visible';
        if (emailBasket != null)
            emailBasket.style.visibility = 'visible';
    }
    else
    {
        //disable them
        clearSaveJob = document.getElementById('clearSaveJob');
        emailBasket = document.getElementById('emailBasket');
        if (clearSaveJob != null)
            clearSaveJob.style.visibility = 'hidden';
        if (emailBasket != null)
            emailBasket.style.visibility = 'hidden';
    }
}


function setupLocationBox() {
  var selectedCat = $('#searchCategory').val();
  
  if (selectedCat == "Education & Training") {
        //get the page
        $('#locationDynDiv').load('/partial/jobs-location/universities_en.html',
       function() {
       if (jsISourcesVar != '')
         $("#searchSources").val(jsISourcesVar);
    });
  } else {
      
      $('#locationDynDiv').load('/partial/jobs-location/defaultLocationSearch.html',
       function() {
       if (jsLocVar != '')
        $("#searchLocation").val(jsLocVar);
      applyWaterMark($("#searchLocation"), locationDefault);
    });

  }
}

$(document).ready(function()
    {
        applyWaterMark($("#searchKeyword"), skillDefault);
        //applyWaterMark($("#searchLocation"), locationDefault);
        settupBaskets(viewmode);
        
        if ($("#locationRefinementTree").length != 0)
        $("#locationRefinementTree").treeview({
            animated: "fast",
            collapsed: true

        });

        if (enbleCustomizedMenu) {
            $('#searchCategory').change(function() {
                setupLocationBox();
            });
            setupLocationBox();
        }
        else {
            applyWaterMark($("#searchLocation"), locationDefault);
        }

    });
