﻿function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();


addEvent(window,'unload',EventCache.flush);

var total = 1;
function copyDiv(){
    var _new = $('#others_progr').clone().removeAttr('id');
	_new.attr('class', 'new');
    _new.find('input').val('').attr('name','aedit_prog_name'+total);
    _new.find('select').attr('name', 'aedit_prog_level'+total);
    _new.find('select:nth-child(1)').attr("selected", "selected");
    _new.insertAfter($('#others_progr').parent().find('div:last'));
    total++;
	$('#minus1').show();
    if(total > 2) $('#plus').hide();
}

function removeDiv(){
	$($('#others_progr').parent().find('div.new:last')).remove();
	total--;
	$('#plus').show();
	var n = $('div.new').length;
	if (n==0) $('#minus1').hide();
}

var total_ = 1;
function copyDiv1(){
    var _new = $('#others_progr1').clone().removeAttr('id');
	_new.attr('class', 'new');
    _new.find('input').val('').attr('name','askills_equipment_name'+total_);
    _new.find('select').attr('name', 'askills_equipment_level'+total_);
    _new.find('select:nth-child(1)').attr("selected", "selected");

    //Вставляем получившийся элемент после последнего div-a
    _new.insertAfter($('#others_progr1').parent().find('div:last'));
    total_++;
	$('#minus2').show();
    
    //Если уже 2 раза кликнули, то убираем плюсик
    if(total_ > 2) $('#plus1').hide();
}

function removeDiv1(){
	$($('#others_progr1').parent().find('div.new:last')).remove();
	total_--;
	$('#plus1').show();
	var n_ = $('div.new').length;
	if (n_==0) $('#minus2').hide();
}


/*var total = 0;
function copyDiv(){
var $new = $('#others_progr').clone(); 
var newTotal = document.getElementById('aedit_prog_name').name=(total+1);
document.getElementById('aedit_prog_level').name=(total+1);
total = newTotal;
$($new).insertAfter ('#others_progr');
$('#minus1').show ();
}

var total1 = 0;
function copyDiv1(){
var $new = $('#others_progr1').clone();
var newTotal1 = document.getElementById('askills_equipment_name').name=(total1+11);
document.getElementById('askills_equipment_level').name=(total1+11);
total1 = (newTotal1-10);
$($new).insertAfter ('#others_progr1');
$('#minus2').show ();
} */

$(document).ready(function() { 
$.viewSelect = { 
'0' : $([]), 
//THIS IS THE NAME OF THE DIV WRAPPING THE HIDDEN FIELD 
'industry' : $('#industry'), 
}; 

$('#agoal').change(function() { 
// HIDES THE INPUT FIELD IF ANOTHER DROPDOWN ITEM IS SELECTED ONCE THE HIDDEN FIELD IS LOADED 
$.each($.viewSelect, function() { this.hide(); }); 
// SHOWS THE INPUT FIELD ITEM IF SELECTED 
$.viewSelect[$(this).val()].show(); 
}); 

});

$(document).ready(function() { 
$.viewInput = { 
'0' : $([]), 
//THIS IS THE NAME OF THE DIV WRAPPING THE HIDDEN FIELD 
'other' : $('#other'), 
}; 

$('#aindustry_hiden').change(function() { 
// HIDES THE INPUT FIELD IF ANOTHER DROPDOWN ITEM IS SELECTED ONCE THE HIDDEN FIELD IS LOADED 
$.each($.viewInput, function() { this.hide(); }); 
// SHOWS THE INPUT FIELD ITEM IF SELECTED 
$.viewInput[$(this).val()].show(); 
}); 

});
