﻿var Selection = {
   Values: '', 
   clear: function() {
      Selection.Values = '';
      $$('input[type.Item]').each(function(cb) { cb.checked = false; });
   },
   selectAll: function() {
      Selection.Values = '';
      $$('input[type.Item]').each(function(cb) { Selection.Values+=(Selection.Values==''?'':',')+cb.get('PosId'); cb.checked=true; });
   },
   toggle: function ( state, item ) {
    if ( state ) this.add(item);
    else this.remove(item); 
   },
   add: function(item) {
        Selection.Values = Selection.Values.split(',').erase('').include(item+'').join(',')
   },
    remove: function(item) {
       Selection.Values = Selection.Values.split(',').erase('').erase(item+'').join(',')
   },
   printSelected: function() {
   //moty change
   if(!LoggedIn)
        {
           notLoggedIn();
            return;
        }
        
     if (Selection.Values!='')
        window.open( '../Seekers/JobsPrint.aspx?PosIds='+Selection.Values,'_blank');
    else
        myAlert.alert(mustSelect); 
   }
}

var notLoggedIn = function() {
    myAlert.confirm( mustLogin, {textBoxBtnOk: btnLogin, textBoxBtnCancel: btnCancel, onComplete: function(returnvalue) {
            if(returnvalue) location.href = '../Seekers/Login.aspx?ReturnURL='+location.href;
    }});
}

var SendCV = {
   Start: function ( PosIds, SendType ) {
        if ( PosIds == '' ) {
            myAlert.alert(mustSelect); 
            return;
        }
        else if ( !LoggedIn ) {
            notLoggedIn();
            return;
        }
        
        SendCV.PosIds = PosIds;
        SendCV.SendType = SendType;
        myAlert.prompt(addText, "", {InputStyles: { 'width': '380px', 'height': '60px' }, InputType: 'textarea', textBoxBtnOk: btnOk, textBoxBtnCancel: btnCancel, onComplete: function(returnvalue) {
            ///if(returnvalue===false)
                ///return;
            SendCV.Text = (returnvalue === false)? "" : returnvalue;
            SendCV.Send();
        }});
   },
   Send: function () {
        var jsonRequest = new Request.JSON( {url: "../Handlers/SendCV.ashx", onComplete: function(SendResults) {
            if ( SendResults == null ) {
                myAlert.error(errorOccured); 
                return;
            }
            switch (SendResults.SendType) {
                case 0:
                    switch (SendResults.Status) {
                        case 1:
                        case 2:
                           myAlert.info(SentSuccess);
                           break; 
                        case 3:
                           myAlert.alert(alreadyExists);
                           break; 
                        default:
                           myAlert.error(errorOccured); 
                           break; 
                    }
                    break;
               case 1:
                   switch ( SendResults.SumSuccess) {
                        case 0:
                           myAlert.alert(nothingSent); 
                           break; 
                        default:
                           var msg = sumSentSuccess.replace( '[SumSuccess]', SendResults.SumSuccess ).replace( '[SumSent]', SendResults.SumSent );
                           myAlert.info(msg); 
                           break; 
                    }
                    break; 
            }
       }}).get({'PosIds': SendCV.PosIds, 'Txt': SendCV.Text, 'SendType' : SendCV.SendType});
   }
} 

var AddToList = {
   Start: function ( PosIds, ListType ) {
        if ( PosIds == '' ) {
            myAlert.alert(mustSelect); 
            return;
        }
        else if ( !LoggedIn ) {
            notLoggedIn();
            return;
        }
        
        AddToList.PosIds = PosIds;
        AddToList.ListType = ListType;
        AddToList.Send();
   },
   Send: function () {
        var jsonRequest = new Request.JSON( {url: "../Handlers/AddToList.ashx", onComplete: function(SendResults) {
             if ( SendResults == null ) {
                myAlert.error(errorOccured); 
                return;
            }
             switch ( SendResults.SumSuccess) {
                case 0:
                   myAlert.alert(nothingSent); 
                   break; 
                default:
                   var msg = sumAddedToList.replace( '[SumSuccess]', SendResults.SumSuccess ).replace( '[SumSent]', SendResults.SumSent );
                   myAlert.info(msg); 
                   break; 
            }
       }}).get({'PosIds': AddToList.PosIds,'ListType' : AddToList.ListType});
   }
} 

var RemoveFromList = {
   Start: function ( PosIds ) {
        if ( PosIds == '' ) {
            myAlert.alert(mustSelect); 
            return;
        }
        else if ( !LoggedIn ) {
            notLoggedIn();
            return;
        }
        
        RemoveFromList.PosIds = PosIds;
        RemoveFromList.FavListType =  typeof(FavListType)!='undefined'?FavListType:'0';
        RemoveFromList.ListType =  typeof(ListType)!='undefined'?ListType:'0';
        RemoveFromList.Send();
   },
   Send: function () {
        var jsonRequest = new Request.JSON( {url: "../Handlers/RemoveFromList.ashx", onComplete: function(SendResults) {
             if ( SendResults == null ) {
                myAlert.error(errorOccured); 
                return;
            }
            switch ( SendResults.SumSuccess) {
                case 0:
                   myAlert.alert(nothingSent); 
                   break; 
                default:
                   var msg = sumRemoveFromList.replace( '[SumSuccess]', SendResults.SumSuccess ).replace( '[SumSent]', SendResults.SumSent );
                   myAlert.info(msg, {onComplete: function() {location.href = 'MyList.aspx?ListType='+RemoveFromList.ListType;}}); 
                   break; 
            }
        }}).get({'PosIds': RemoveFromList.PosIds, 'FavListType' : RemoveFromList.FavListType,'ListType' : RemoveFromList.ListType});
   }
} 