function favoritesAdd( type, id, nodeId, removeButton ){
    var req = new JsHttpRequest();    
    
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            
            if( req.responseJS.added ){
                var img;

                if( !(img = document.getElementById( nodeId + 'Img')) )
                    return;
                
                img.src =  img.src.replace(/fav1([s]?)\.gif/, 'fav2$1.gif');
                img.alt = 'В избранном'; img.title = 'В избранном';
                
                img.parentNode.onclick = function(){
                    favoriteRemove( type, id, nodeId );
                    return false;
                }                
            }
        }
    }
    
    req.open(null, '/ajax/favoritesAdd.php', true);
    req.send( {type: type, id:id, removeButton: removeButton} );    
}

function favoriteRemove( type, id, nodeId ){
    var req = new JsHttpRequest();  


    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            
            if( req.responseJS.removed ){
                var img;
                
                if( !(img = document.getElementById( nodeId + 'Img')) )
                    return;
                    
                img.src =  img.src.replace(/fav2([s]?)\.gif/, 'fav1$1.gif');
                img.alt = 'В избранное'; img.title = 'В избранное';
                
                img.parentNode.onclick = function(){
                    favoritesAdd( type, id, nodeId );
                    return false;
                }
            }
        }
    }
    
    req.open(null, '/ajax/favoritesRemove.php', true);
    req.send( {type: type, id:id} );        
}


function favoritesAdd_2( type, id, nodeId ){
    var req = new JsHttpRequest();    
 
    
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            
            if( req.responseJS.added ){
                var img;
                
                if( !(img = document.getElementById( nodeId + 'Img')) )
                    return;
                    
                if( img.src.match( /s\.gif/ ) )
                	return;

                img.src =  img.src.replace(/\.gif/, 's.gif');
                img.alt = 'В избранном'; img.title = 'В избранном';
                
                img.parentNode.onclick = function(){
                    favoritesRemove_2( type, id, nodeId );
                    return false;
                }                
            }
        }
    }
    
    req.open(null, '/ajax/favoritesAdd.php', true);
    req.send( {type: type, id:id } );
}



function favoritesRemove_2( type, id, nodeId ){
    var req = new JsHttpRequest();  

    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            
            if( req.responseJS.removed ){
                var img;
                
                if( !(img = document.getElementById( nodeId + 'Img')) )
                    return;
                    
                img.src =  img.src.replace(/s\.gif/, '.gif');
                img.alt = 'В избранное'; img.title = 'В избранное';
                
                img.parentNode.onclick = function(){
                    favoritesAdd_2( type, id, nodeId );
                    return false;
                }
            }
        }
    }
    
    req.open(null, '/ajax/favoritesRemove.php', true);
    req.send( {type: type, id:id} );        
}


function favorites_2_vacancy( id, remove )
{
    var req = new JsHttpRequest();  

    req.onreadystatechange = function() {
        if (req.readyState == 4) {

    		var done = remove ? req.responseJS.removed : req.responseJS.added;
    		
    		if( done )
    		{
    			var img = getObj( 'vacFavImg' + id );
    			var txt = getObj( 'vacFavTxt' + id );
    		}
        	
    		if( remove )
    		{
    			img.src =  img.src.replace(/s\.gif/, '.gif');
    			txt.innerHTML = 'Добавить в избранное';
    			
    			img.onclick = function(){ favorites_2_vacancy( id ); }
    			txt.onclick = function(){ favorites_2_vacancy( id ); return false; }
    		}
    		else
    		{
                if( !img.src.match( /s\.gif/ ) )
                	img.src =  img.src.replace(/\.gif/, 's.gif');
                	
            	txt.innerHTML = 'Удалить из избранного';
                	
            	img.onclick = function(){ favorites_2_vacancy( id, true ); }
            	txt.onclick = function(){ favorites_2_vacancy( id, true ); return false; }
    			
    		}
    		
                
/*                if( !(img = document.getElementById( nodeId + 'Img')) )
                    return;
                    
                
                img.alt = 'В избранное'; img.title = 'В избранное';
                
                img.parentNode.onclick = function(){
                    favoritesAdd_2( type, id, nodeId );
                    return false;
                }*/
            }
        }
    
    req.open('POST', '/ajax/' + ( remove ? 'favoritesRemove' : 'favoritesAdd' ) + '.php', true);
    req.send( {type: 'vacancy', id: id} );
}

function favorites_2_resume( id, remove )
{
    var req = new JsHttpRequest();  

    req.onreadystatechange = function() {
        if (req.readyState == 4) {

    		var done = remove ? req.responseJS.removed : req.responseJS.added;
    		
    		if( done )
    		{
    			var img = getObj( 'vacFavImg' + id );
    			var txt = getObj( 'vacFavTxt' + id );
    		}
        	
    		if( remove )
    		{
    			img.src =  img.src.replace(/s\.gif/, '.gif');
    			txt.innerHTML = 'Добавить в избранное';
    			
    			img.onclick = function(){ favorites_2_resume( id ); }
    			txt.onclick = function(){ favorites_2_resume( id ); return false; }
    		}
    		else
    		{
                if( !img.src.match( /s\.gif/ ) )
                	img.src =  img.src.replace(/\.gif/, 's.gif');
                	
            	txt.innerHTML = 'Удалить из избранного';
                	
            	img.onclick = function(){ favorites_2_resume( id, true ); }
            	txt.onclick = function(){ favorites_2_resume( id, true ); return false; }
    			
    		}
    		
                
/*                if( !(img = document.getElementById( nodeId + 'Img')) )
                    return;
                    
                
                img.alt = 'В избранное'; img.title = 'В избранное';
                
                img.parentNode.onclick = function(){
                    favoritesAdd_2( type, id, nodeId );
                    return false;
                }*/
            }
        }
    
    req.open(null, '/ajax/' + ( remove ? 'favoritesRemove' : 'favoritesAdd' ) + '.php', true);
    req.send( {type: 'resume', id: id} );
}


function fav2res( id, remove, tPrefix )
{
    var req = new JsHttpRequest();
	tPrefix = tPrefix?tPrefix:'';

    req.onreadystatechange = function()
    {
        if (req.readyState == 4) {

            if( req.responseJS.notes )
            {
	            getObj("notes").innerHTML = req.responseJS.notes;
                ShowNotes();
            }
            else
            {
	    		var txt = getObj( 'vacFavTxt' + id );
	    		var tbl = getObj( 'vacFavTbl' + id );
	    		var btn = getObj( 'vacFavBtn' + id );
				
	    		if( remove )
	    		{
	    			tbl.style.backgroundColor = '#f2f2f2';
	    			txt.innerHTML = 'Добавить в избранное';

	    			txt.onclick = function(){ fav2res( id, false, tPrefix ); return false; }
	    			btn.onclick = function(){ fav2res( id, false, tPrefix ); return false; }
	    		}
	    		else
	    		{
	    			tbl.style.backgroundColor = '#ffeab8';
	            	txt.innerHTML = 'Удалить из избранного';
	                	
	            	txt.onclick = function(){ fav2res( id, true, tPrefix); return false; }
	            	btn.onclick = function(){ fav2res( id, true, tPrefix); return false; }
	    		}
    		}
    	}
    }
    
    req.open(null, '/ajax/' + ( remove ? 'favoritesRemove' : 'favoritesAdd' ) + '.php', true);
	typeRes = tPrefix+'resume';
    req.send( {type: typeRes, id: id} );
}

function favorites_2_top_resume( id, remove )
{
    var req = new JsHttpRequest();  

    req.onreadystatechange = function() {
        if (req.readyState == 4) {

    		var done = remove ? req.responseJS.removed : req.responseJS.added;
    		
    		if( done )
    		{
    			var img = getObj( 'vacFavImg' + id );
    			var txt = getObj( 'vacFavTxt' + id );
    		}
        	
    		if( remove )
    		{
    			img.src =  img.src.replace(/s\.gif/, '.gif');
    			txt.innerHTML = 'Добавить в избранное';
    			
    			img.onclick = function(){ favorites_2_top_resume( id ); }
    			txt.onclick = function(){ favorites_2_top_resume( id ); return false; }
    		}
    		else
    		{
                if( !img.src.match( /s\.gif/ ) )
                	img.src =  img.src.replace(/\.gif/, 's.gif');
                	
            	txt.innerHTML = 'Удалить из избранного';
                	
            	img.onclick = function(){ favorites_2_top_resume( id, true ); }
            	txt.onclick = function(){ favorites_2_top_resume( id, true ); return false; }
    			
    		}
    		
                
/*                if( !(img = document.getElementById( nodeId + 'Img')) )
                    return;
                    
                
                img.alt = 'В избранное'; img.title = 'В избранное';
                
                img.parentNode.onclick = function(){
                    favoritesAdd_2( type, id, nodeId );
                    return false;
                }*/
            }
        }
    
    req.open(null, '/ajax/' + ( remove ? 'favoritesRemove' : 'favoritesAdd' ) + '.php', true);
    req.send( {type: 'top_resume', id: id} );
}

function favorites_new_actions(action, id)
{
    var req = new JsHttpRequest();   

    req.onreadystatechange = function() {
        if (req.readyState == 4)
        {
        	if( req.responseJS.win )
        	{
		        initDragItems();
		        var div = document.createElement('div');
		        div.className = 'infoBlock';
		        div.style.width = '450px';
		        document.di.add( div );
		                   
		        document.body.appendChild( div );
		        onCenter( div );
		                    
		        div.innerHTML = req.responseText;
	        }
	        
        	if( req.responseJS.reloadList )
        	{
        		div = getObj('resume_list');
		        div.innerHTML = req.responseText;
	        }
	        
            if( req.responseJS.notes )
            {
	            getObj("notes").innerHTML = req.responseJS.notes;
                ShowNotes();
            }
        }
    }

	var vars = {};

	if (document.forms['favbye'])
		vars.favbye = getFormForAjax('favbye');
	vars.action = action;
	vars.id = id;
                    
	req.open( 'POST', '/ajax/favoritesActions.ajax.php' , true); 
    req.caching = false;
    req.send( vars );
}

function favorites_new_resume_all(check)
{
	formObj = getObj('favbye');

	
	for (i=0; i<formObj.length; i++) {

		el = formObj[i]
		
		if (el.name == 'resume[]')
		{
   			el.checked = check;
   		}
	}
	
	favorites_new_resume_calc();
}

function favorites_new_resume_calc()
{
	var all_sum = 0;
	var all_cnt = 0;
	var all_select = true;
	
	formObj = getObj('favbye');

	for (i=0; i<formObj.length; i++)
	{

		el = formObj[i]
				
		if (el.name == 'resume[]')
		{
			if (el.checked) 
			{
				priceObj = getObj('price_'+el.value);
				all_sum += parseInt(priceObj.value);
				
				all_cnt++;
			}
			else
				all_select = false;				
   		}
	}
	
	getObj('resume_all').checked = all_select;
	
	for (i=0; i<resume_discount.length; i++)
	{
		el = resume_discount[i];
		
		if (all_cnt >= el[0])
			_el = el;
	}
	
	
	discount_proc = ( _el[2] + ( (_el[3]-_el[2]) / (_el[1]-_el[0]) * (all_cnt - _el[0]) ) ).toFixed(0) ;
	all_sum = all_sum - (all_sum * (discount_proc/100)).toFixed(0);
	
	countObj = getObj('resume_select_count');
	countObj.innerHTML = all_cnt;

	discountObj = getObj('resume_select_discount');
	discountObj.innerHTML = discount_proc;
	
	sumObj = getObj('resume_select_sum');
	sumObj.innerHTML = all_sum;

	sumObj = getObj('resume_select_sum_i');
	sumObj.value = all_sum;
	
}