﻿var valid=true;
  
$(function() {
   $('input.HaveMessage').focus(function(){showMessage(this)});
   $('select.HaveMessage').focus(function(){showMessage(this)});
    PrepareValidation();
});

var customValidate = true;
function showMessage(element)
{
   $('.InputMessage').hide();
   var id = $("#"+element.id).attr("class");
   var type =$("#"+element.id).attr('RegexType')
   if (type == 'IdNumber' || type == 'Cell' || type == 'Status' || id.substring(0,7) == 'Selecte' )
        id = id.substring(20,id.length);
   else
        id = id.substring(32,id.length);

    var e = '#'+ id  +'Message.InputMessage';
       
    $(e).fadeIn('fast');
}

function AddDivHeight(DivClass,Height,ind)
{
    //alert("DivClass:'"+DivClass+"' - Height:'"+Height+"' - indicator:'"+ind+"'");
    var baseHeight = $('.'+DivClass+'').height();  
    var AddHeight= baseHeight + Height; 
    $(ind+DivClass).css('height',AddHeight + 'px');
    //alert("$("+ind+DivClass+").css('height'," + AddHeight + "px);");
}
var HeightAddition = 15;
var HeightDeduction = -15;

function PrepareValidation()
{  
   
     // test all the regex fields           
     $("input[@vldRegex]").each(function (i) {
        $(this).blur(function () {
            if (!new RegExp($(this).attr('vldRegex')).test($(this).val()))
            {
              $('.' + $(this).attr('RegexType') + 'Err').show();
              valid=false;
            }
            else{
                $('.' + $(this).attr('RegexType') + 'Err').hide();  

            }
       });   
    });
    
   
    // id number special check
    $('.TxtIdNumber').blur(function(){
    
        $('#IdNumberOldUserErr').hide();
    
        if (!checkidnum($('.TxtIdNumber').val()))
        {
           $('.' + $(this).attr('RegexType') + 'Err').show();   
        }
        else
        {
            $('.' + $(this).attr('RegexType') + 'Err').hide();  
             if (checkInputExists(this,$(this).attr('RegexType')))
                    valid=false;
            
        }
       
    });
    
     $("input.UniqueField").each(function (){
        $(this).blur(function () {
                  if (checkInputExists(this,$(this).attr('RegexType')))
                    valid=false;
                }
        );
    });
    
}

function checkInputExists(sender,type)
{   
    if ( $(sender).val() != '')
    {
        $.post('Handlers/UserExistsHandler.ashx',
        {
            Value : $(sender).val(),
            Type : type
     
        }, function(data)
        {
            $('.' + type + 'UniqueFieldErr').hide();
            $('.' + type + 'OldUserErr').hide();
            $('#IdNumberErr').hide();
        
            switch (data)
            {
                case "true":
                    $('.' + type + 'UniqueFieldErr').show();
                    return true;
                    break;
                case "OldUser":
                    $('.' + type + 'OldUserErr').show();
                    return true;
                    break;
                case "false":
                    $('.' + type + 'UniqueFieldErr').hide();
                    return false;
                    break;
                default:
                     return true;
                    break;
                
            }
                    
   
        });        
    }
}
function CheckStatus()
{
   if (CheckSpecificStatus())
      $('.LoginSectionDetailsFaculty').hide();
   else
      $('.LoginSectionDetailsFaculty').show();
}

function CheckSpecificStatus()
{
    var val = $('.ddlStatus')[0].options[$('.ddlStatus')[0].selectedIndex].value;
    // 1 = לפני לימודים
    // 7 = בוגר תואר ראשון
    // 10 = בוגר תארים מתקדמים
    if (val  == 1 || val == 7 || val == 10)
    { 
        $('#EducationCycleSelectERR').hide();
        return true;
    }
    else
       return false;
}
var subCycleIndex = 0;
var cycleIndex = 0;
var educationCycleIndex=0;
var name;
function getCycleOptions(parent,cycleIndexNum)
{
    var cycleId = null;
    if (parent != null)
        cycleId = $(parent).val();
    name = "EducationCycleContainer" + cycleIndexNum
    if (cycleIndexNum ==0 && subCycleIndex==0 )
        subCycleIndex++;
    $.post('../Handlers/GetCycleOptions.ashx',
    {
        cycleId : cycleId,
        cycleIndexNum : subCycleIndex
    },
    function(data)
    {
        if (data != '')
        {
            if (parent != null)
                while ($(parent).parent().next().length > 0)
                    $(parent).parent().next().remove();
            
            if ((subCycleIndex == 0)  && (cycleIndex > 0))
              $('#CycleContainer').append("<div  id='"  + name + "' style='width:210px;margin-top:10px;' ><div class='hr'></div><input type='hidden' id='txteducationCycleId" + cycleIndexNum + "' name='txteducationCycleId" + cycleIndexNum + "' class='txteducationCycleId' /><div><input id='removeCycle" +cycleIndexNum+"' class='removeCycle' type='button' value='הסר' onclick='RemoveEducationCycle(this);' /></div></div>");
            $('#' + name).append("<div > <select class='EducationCycleSelect' name='ddlCycle" + subCycleIndex + "' onchange='javascript:getCycleOptions(this," + cycleIndexNum +  ");'>" + data + "</select><div>");
            subCycleIndex++;
        }
        else
        {
           $('#txteducationCycleId' + cycleIndexNum).val($(parent).val());
           $('#EducationCycleSelectERR').hide();
           $('#errMSG').html('');
        }
    });
}

function AddEducationCycle()
{
    subCycleIndex =0;
    cycleIndex++;
   
    getCycleOptions(null,cycleIndex);
    LastAddition = 0;
    var baseHeight = $('.MyEducationLoginSection').height();  
    var AddHeight= baseHeight + 70; 
    $('.MyEducationLoginSection').css('height',AddHeight + 'px'); 
}

function checkidnum(idnum)
{ 
    while(idnum.length < 9)
    {
        idnum  = '0' + idnum;
    }
    idnum1=idnum.substr(0,1)*1;
    idnum2=idnum.substr(1,1)*2;
    idnum3=idnum.substr(2,1)*1;
    idnum4=idnum.substr(3,1)*2;
    idnum5=idnum.substr(4,1)*1;
    idnum6=idnum.substr(5,1)*2;
    idnum7=idnum.substr(6,1)*1;
    idnum8=idnum.substr(7,1)*2;
    idnum9=idnum.substr(8,1)*1;
 
    if (idnum1>9) idnum1=(idnum1%10)+1;
    if (idnum2>9) idnum2=(idnum2%10)+1;
    if (idnum3>9) idnum3=(idnum3%10)+1;
    if (idnum4>9) idnum4=(idnum4%10)+1;
    if (idnum5>9) idnum5=(idnum5%10)+1;
    if (idnum6>9) idnum6=(idnum6%10)+1;
    if (idnum7>9) idnum7=(idnum7%10)+1;
    if (idnum8>9) idnum8=(idnum8%10)+1;
    if (idnum9>9) idnum9=(idnum9%10)+1;
 
    var sumval=idnum1+idnum2+idnum3+idnum4+idnum5+idnum6+idnum7+idnum8+idnum9;
 
    sumval=sumval%10
    
    if (sumval>0){
        return false;
    }
    else
        return true;
 }
function RemoveEducationCycle(e,n)
{

    var index = parseInt(e.id.replace('removeCycle',''));
    var RemovalHeight= 0 - 12 - $('#EducationCycleContainer'+index).height(); 
    var baseHeight = $('.MyEducationLoginSection').height();
    // remove div
    $('#EducationCycleContainer'+index).remove(); 
    // Remove Div's Xtra Height
    if ((baseHeight - RemovalHeight)>93)
        $('.MyEducationLoginSection').css('height',baseHeight + RemovalHeight + 'px'); 
    // remove delete button
    $(e).remove();   
    cycleIndex--;
}
function afterFilterCycles(num) // Adds Height to the Container Div [MyEducationLoginSection]
{   
    alert(num);
    var baseHeight = $('.MyEducationLoginSection').height();  
    var AddHeight= baseHeight + 25; 
    var extraSelectAmount = $('#EducationCycleListSelection select').length - 1;
    var extraButtonAmuont = $('#EducationCycleListSelection input').length;
    $('.MyEducationLoginSection').css('height',AddHeight + 'px'); 
}
function ClientValidation()
{
    
    
    valid=true;

    var chk;
    chk=document.forms[0].chkAgreedToTerms.checked;
   
     
    // check id again   
    if (!checkidnum($('.TxtIdNumber').val()) || $('.TxtIdNumber').val() == '')
    {
       $('.' + $('.TxtIdNumber').attr('RegexType') + 'Err').show();   
       valid=false;
    }
    else
    {
        $('.' + $('.TxtIdNumber').attr('RegexType') + 'Err').hide();  
    }

   
        
   // test all the regex fields           
   $("input[@vldRegex]").each(function (i) {
   
        //alert($(this).attr('vldRegex'));
        if (!new RegExp($(this).attr('vldRegex')).test($(this).val()))
        {
          $('.' + $(this).attr('RegexType') + 'Err').show();
          valid=false;
        }
        else{
            $('.' + $(this).attr('RegexType') + 'Err').hide();  

        }
          
    });



 
   
   
    // check that the user filled the education cycle OR choose specific status 
    if (($('.txteducationCycleId').val() == "-1") && (!CheckSpecificStatus()))
        {
            $('#EducationCycleSelectERR').show();
            valid=false;
        }
    else
        $('#EducationCycleSelectERR').hide();
        
    // check the user agree to terms
    if (!chk)
    {
        $('#chkAgreedToTermsERR').show();
        valid=false;
    }
    else
        $('#chkAgreedToTermsERR').hide();

   
    // check the passwords are equals
    if ($('.TxtPassword').val() != $('.TxtRepeatPassword').val())
    {
        $('.RePasswordErr').show();
        valid=false;         
    }
    else
       $('.RePasswordErr').hide(); 
       
       
               
      if (CheckSpecificStatus())
         $('.txteducationCycleId').val("-1");
  

     
     return valid ;
}

function UserLogIn()
{

    if($("#txtUserId").val() != "" && $("#txtPassword").val() !=  "")
    {
        
      $.post(baseurl + 'Handlers/LogInHandler.ashx',
      { 
        txtIdNumber: $("#txtUserId").val(),
        password: $("#txtPassword").val(),
        isRememberMe : $("#chkRememberMe").attr("checked")
      }, afterLoginOrOut);
    }
    else
    {
        afterLoginOrOut("False");
    }
}

function afterLoginOrOut(data)
{
    if(data != "False")
    {
        
        window.location  = baseurl;//"../Default.aspx";
    }
    else
    {
       
        $("#LoginErrorMessage").html("מספר ת''ז או סיסמה שגויים ");
    }
}
function mypopup()
{
   window.open ("takanon.htm","mywindow","location=1,status=0,scrollbars=1, width=600,height=800");
} 