﻿$(document).ready(function () {
       IsTabsValid = false;
//     $(".Search .Tabs .Tab").mouseover(
//      function () {
//        $(".Search .Tabs .Tab").removeClass("Selected");
//        $(".Search .Contents .Content").hide();
//        $(this).addClass("Selected");
//        $(".Search .Contents .Content[tabId='" + $(this).attr('id') + "']").show();
//      });
      
       $(".Search .Contents .Field input").click(function () {
            $(this).select();
       });

       $(".Institutes .Header .NameDiffer").click(function() {
            ToggleSponser(this.parentNode);
      });

      $(".Institutes .HeaderDiffer").click(function() {
          ToggleSponser(this);
      });
      
      $(".Institutes .Body .ScholarshipHeader").click(function () {
            ToggleScholarshipNew(this);
      });
       
       //SetScholarshipsTextTables();
});

function SetScholarshipsTextTables(body)
{
    jQuery(".Top .Text table", body).each(function(){
        $(this).width('90%');
    });
    
    jQuery(".Top .Text table tbody tr", body).each(function(){
        $(this).children('td:even').width('17%');
        $(this).children('td').css('padding-top','5px');
    });
}
function ScrollTo(Element)
{
    var offset = Element.offset().top;
    $('html, body').animate({scrollTop: offset + 'px'}, 1000);
}

function ToggleSponser(object) {    
    $(object).next('.Body').toggle();
    $(object).children('.Line').toggle();
    $(object).css('backgroundColor','#F8F8F8');
    jQuery('.Count',object).toggleClass('Open');
}

function ToggleScholarship(object)
{
    $(object).next().next('.ScholarshipBody').toggle();
    $(object).toggleClass('Open');
    //.slideToggle('slow')
    
    if($(object).next().next('.ScholarshipBody').children('.Top').children('.Text').text().length<=1)
        $(object).next().next('.ScholarshipBody').children('.Top').children('.AjaxIndicator').show();
        $.post("/Handlers/GetScholarshipText.ashx",
             {ScholarshipId: $(object).attr('sid')},
             function(data){
                $(object).next().next('.ScholarshipBody').children('.Top').children('.Text').empty().append(data);
                $(object).next().next('.ScholarshipBody').children('.Top').children('.AjaxIndicator').hide();
             });

}

function ToggleScholarshipNew(object)
{
    var head = $(object);
    var body = $(object).next().next('.ScholarshipBody');
    
    head.toggleClass('Open');
    body.toggle();
    //.slideToggle('slow')
    if(body.text().length<=1)
    {
        $.post("/Handlers/GetScholarshipText.ashx",
             {
                ScholarshipId: $(object).attr('sid'),
                CategoryId: CategoryId
             },
             function(data){
                $(object).next().next('.ScholarshipBody').append(data);
                SetScholarshipsTextTables(body);
             });
    }

}

function SentLeadForInstitute(InstituteID,CategoryID, sender)
{
    $.post("/Handlers/SendLeadToInstitute.ashx",
     {
         InstituteID: InstituteID,
         CategoryID: CategoryID
     },
     function(data) {
         if (data == "True") {
             var ilimudimIframe = "<iframe src='http://www.ilimudim.co.il/GetCookie.aspx?InstituteId=" + InstituteID + "' style='display:none;'></iframe>";
             $('#SLTI').empty().append(ilimudimIframe);

             if ($(sender).hasClass("ContactMain")) {
                 $(sender).empty().append('פרטייך נשלחו!');
                 $(sender).addClass("ContactMainSent").removeClass("ContactMain");
                 $(sender).removeAttr('onclick');

                 $(sender).parent().next().find(".Link").each(function() {
                     $(this).empty().append('פרטייך נשלחו!');
                     $(this).addClass('Faded').removeAttr('onclick');
                 });
             }
             else {
                 $.each(jQuery('.Contact', $(sender).parents('.Body')), function() {
                     if ($(this).hasClass('Link'))
                         $(this).empty().append('פרטייך נשלחו!');

                     $(this).addClass('Faded').removeAttr('onclick');
                 });
             }
         }
         else
             window.open('http://www.ilimudim.co.il/GUI/Index/ShowInstitute.aspx?ID=' + InstituteID + '&ref=Milgot', '_self');
     });

}

//function InsertUpdateUser(firstName, lastName, email, phone, address)
//{    
//    $.getScript("http://www.ilimudim.co.il/Handlers/GetScript.ashx?Function=InsertUpdateUser&FirstName=" + firstName + "&LastName=" + lastName + "&Email=" + email + "&Phone=" + phone + "&Address=" + address,
//         function(){
//    });
//}

function NextTab(tab)
{
    var NextTab = $('#' + $(tab).attr('nextTab'));
    var ThisTab = $(tab);
    
    
    $(".Search .Tabs .Tab").removeClass("Selected");
    $(".Search .Contents .Content").hide();
    NextTab.addClass("Selected");
    $(".Search .Contents .Content[tabId='" + ThisTab.attr('nextTab') + "']").show();
}

function SubmitAgainSearchCategories() {
    $('.ErrorMSG').empty();
    if (!$("input[name=IsStudent]").is(':checked')) {
        fadeInAndOutInvalid($('.ErrorMSG').text('אנא סמן האם אתה סטודנט או מועמד ללימודים.'),3);
        return false;
    }

    var CategoryID = -1;
    if ($("#sWorldsCategories").val() == 257 || $("#sWorldsCategories").val() == 254 || $("#sWorldsCategories").val() == 308 || $("#sWorldsCategories").val() == 643 || $("#sWorldsCategories").val() == 699 || $("#sWorldsCategories").val() == 710)
        CategoryID = $("#sMainCategories").val();
    else
        CategoryID = $("#sSubCategories").val();

    if (CategoryID != -1 && CategoryID != null) {
        $('#CategoryID').val(CategoryID);
        //alert($("#sWorldsCategories option:selected").text());
        //alert($("#sSubCategories option:selected").text());
        //alert($("#sMainCategories option:selected").text());
        return true;
    }
    else {
        if ($("#sWorldsCategories").val() == -1) {
            fadeInAndOutInvalid($('.ErrorMSG').text('אנא בחר סוג לימודים'), 3);
        }
        else if ($("#sMainCategories").val() == -1) {
        fadeInAndOutInvalid($('.ErrorMSG').text('אנא בחר תחום לימודים'), 3);
        }
        else {
            fadeInAndOutInvalid($('.ErrorMSG').text('אנא בחר מסלול לימודים'), 3);
        }
        return false;
    }
}

function BindAgainMainCategoriesList() {
    $.post("/Handlers/GetCategoriesAgain.ashx",
         { SelectedIndex: $("#sWorldsCategories").val(), type: "MainCategories" },
         function(data) {
             $("#dvSearchMainCategoriesContainer").empty();
             $("#dvSearchMainCategoriesContainer").append(data);
             //         if($("#sWorldsCategories").val()==-1)
             //         {
             $("#dvSearchSubCategoriesContainer").empty();
             $("#dvSearchSubCategoriesContainer").append(
                 "<select id='sSubCategories' class='HPSearchCategoriesSelect'" +
                 "<option value='-1' selected='selected'>--בחר מסלול לימודים--</option></select>");
             //         }
         });
     }
     function BindAgainSubCategoriesList() {
         $.post("/Handlers/GetCategoriesAgain.ashx",
         { SelectedIndex: $("#sMainCategories").val(), type: "SubCategories" },
         function(data) {
             $("#dvSearchSubCategoriesContainer").empty();
             $("#dvSearchSubCategoriesContainer").append(data);
         });
     }

     function fadeInAndOutInvalid(sender, num) {
         if (!num || num > 0)
             $(sender).fadeOut(300, function() {
                 $(this).fadeIn(300);
                 if (num)
                     fadeInAndOutInvalid(sender, num - 1);
             });
     }