﻿var cycleId = '';
$(function() {
    $(".courseName").autocomplete(
        "Handlers/GetCourseOptions.ashx",
        {
            delay:10,
            minChars:2,
            matchSubset:1,
            matchContains:1,
            cacheLength:10,
            onItemSelect:selectItem,
            onFindValue:findValue,
            autoFill:false,
            extraParams	: {cycleId : cycleId}
        }
    );
        

});

function initPage()
{
    if(typeof(isUserLoggedIn) != "undefined")
        if(isUserLoggedIn == null || isUserLoggedIn == "")
        {
            $(".RequiredLogIn").addClass("thickbox");
            tb_init('.RequiredLogIn');                      
        }
            
    if ($('#categoryType').val() != 0 && $('#categoryType').val() != null)
    {
        showReleventFields($('#categoryType').val(), $('#contentType').val());
    }
    else
        showReleventFields(1, null);
                        
    $('.TxtTags')
        .focus(function(){showMessage(this)})
        .blur(function(){clearMessage(this)});
}

function clearMessage(element)
{
    $('.InputMessage:visible').fadeOut('fast');
}
 
function showMessage(element)
{
   var id = $("#"+element.id).attr("class");
    $('#' + id + 'Message.InputMessage').fadeIn('fast');
}


function findValue(li) {
    if( li == null ) return; //alert("No match!");

    // if coming from an AJAX call, let's use the CityId as the value
    if( !!li.extra ) var sValue = li.extra[0];

    // otherwise, let's just display the value in the text box
    else var sValue = li.selectValue;
    $('.txtCourseId').val(sValue);
}

function selectItem(li) {
    findValue(li);
}

function formatItem(row) {
    return row[0] ;//+ " (id: " + row[1] + ")";
}

function onCourseNameChanged()
{
    $('.txtCourseId').val('');
}

var cycleIndex = 0;    
function getCycleOptions(parent)
{
    cycleId = $(parent).val();
    $('.txteducationCycleId').val(cycleId);
    $.post('../Handlers/GetCycleOptions.ashx',
    {
        cycleId : $(parent).val()
    },
    function(data)
    {
        if (data != '')
        {
            $('.txteducationCycleId').val('');
            
            while ($(parent).parent().next().length > 0)
                $(parent).parent().next().remove();
        
            $('#CycleContainer').append("<div class='EducationCycleContainer'><select class='Select' name='ddlCycle" + cycleIndex + "' onchange='javascript:getCycleOptions(this);'>" + data + "</select></div>");
            cycleIndex++;
            
        }
        else
        {
           $('.txteducationCycleId').val($(parent).val());
           $('#ddlEducationCycleIdErr').html('');
           
        }
    });
}

function loadContentTypes(category,typeid)
{
    $('.ContentAvaragePrice').html('');
    $.post('Handlers/GetContentType.ashx',
    {
        categoryId : category,
        typeId : typeid
    },
    function(data)
    {
        $('#ContentTypeContainer').html("<select name='ddlContentTypeId' class='Select' id='ddlContentTypeId' onchange='javascript:FillAvarageContentPrice(this,"+category+");' >" + data + "</select>");
    });
}

function showReleventFields(category,typeid)
{

    loadContentTypes(category,typeid);
    switch(parseInt(category))
    { 
        case 1:
            $('#ContentFields .ContentInput').hide();
            $('#ContentFields .PaperInput').show();
            break;
        case 5:
            $('#ContentFields .ContentInput:not(.TranslationInput)').hide();
            $('#ContentFields .TranslationInput').show();
            break;
        case 3:
            $('#ContentFields .ContentInput:not(.SummeryInput)').hide();
            $('#ContentFields .SummeryInput').show();
            break;
        case 2:
            
            $('#ContentFields .ContentInput:not(.ExamInput)').hide();
            $('#ContentFields .ExamInput').show();
            break;
        default:
             $('#ContentFields .ContentInput:not(.ExamInput)').hide();
            $('#ContentFields .ExamInput').show();
            break;
    }
   
}


function FillAvarageContentPrice(type,category)
{
    
    $.post('Handlers/GetContentTypeAvaragePrice.ashx',
    {
        contentTypeId : $(type).val()
    },
    function(data)
    {
        
        if(category == 1){//עבודות
            $('.ContentAvaragePrice').html(' המחיר המומלץ לעבודה אקדמית באתר הוא 200 ₪.<br />' +'כמה תרויחו על התוכן שלכם? 50% מכל רכישה! ');
        }else{
            $('.ContentAvaragePrice').html(' כמה תרויחו על התוכן שלכם? 50% מכל רכישה! המחיר המומלץ<br />' + 'לתוכן שלכם באתר כרגע הוא '+ data + ' ש"ח');
        }

    });
}