function AddSrcToDest(source,destin,updatefield,current,limit){
	src = document.getElementById(source);
	dest = document.getElementById(destin);

    if (src.selectedIndex != -1){	
		var already = 0;
		var curamount = eval(current)+1;
		//Check To See If It's allready in the list
		for (var check=0; check <= (dest.options.length); check++){
			if (dest.options[check] != null){
				if (dest.options[check].value == src.options[src.selectedIndex].value){
					already = 1;
				}
			}
		}
		
		if (already == 1){
			var str = String(src.options[src.selectedIndex].innerHTML);
			str = str.replace(/\&nbsp\;\s/gi,'');
			alert('Item \''+str+'\' is already on the list');
		}else{
			if (curamount <= limit){
				newslot = dest.options.length++;
				dest.options[newslot].value = src.options[src.selectedIndex].value;
				var str = String(src.options[src.selectedIndex].innerHTML);
				var result = str.replace(/\&nbsp\;\s/ig,'');
				var result = result.replace(/&amp;/ig,'');
				dest.options[newslot].text = result;
				eval(current+'+=1;');
				//Start++;
				updateRemain(1,updatefield,eval(current),limit,'subtract');
			}else{
				alert('You have already reached your limit');
			}
		}
	}
}

function updateRemain(amount,field,current,limit,op) {
	document.getElementById(field).innerHTML = parseInt(limit)-parseInt(current);
}

function AddSrcToSect(source1,source2,destin){
	src1 = document.getElementById(source1);
	src2 = document.getElementById(source2);
	dest = document.getElementById(destin);

    if (src1.selectedIndex != -1 && src2.selectedIndex != -1){	
		var already = 0;
	
		//Check To See If It's allready in the list
		for (var check=0; check <= (dest.options.length); check++){
			if (dest.options[check] != null){
				if (dest.options[check].value == src2.options[src2.selectedIndex].value){
					already = 1;
				}
			}
		}
		
		if (already == 1){
			if (src2.options[src2.selectedIndex].value == '') {
				alert('Item '+src1.options[src1.selectedIndex].text+' is already on the list');
			} else {
				alert('Item '+src1.options[src1.selectedIndex].text+' -> '+src2.options[src2.selectedIndex].text+' is already on the list');
			}
		}else{
			
			if ((secStart+1) <= secLimit){
				newslot = dest.options.length++;
				if (src2.options[src2.selectedIndex].value == '') {
					dest.options[newslot].value = src1.options[src1.selectedIndex].value;
					dest.options[newslot].text = src1.options[src1.selectedIndex].text+' -> All Relevant '+src1.options[src1.selectedIndex].text+' Sub Sectors';
				} else {
					dest.options[newslot].value = src2.options[src2.selectedIndex].value;
					dest.options[newslot].text = src1.options[src1.selectedIndex].text+' -> '+src2.options[src2.selectedIndex].text;
				}
				secStart++;
				updateRemain(1,"sector_remain",secStart,secLimit,'blah');
			}else{
				alert('You have reached your sector limit');
			}
		}
	}
}


function SelectAll(destin){
	//src = document.getElementById(source);
	dest = document.getElementById(destin);
	destlen = dest.options.length;
	for (var loop = 0; loop <= destlen; loop++){
		if (dest.options[loop] != null){
			dest.options[loop].selected = true;
		}
	}
	//window.document.forms[0].submit();
}

function SectRemoveItem(destin,updatefield,current,limit){
	dest = document.getElementById(destin);
	
	//Are They any to selected if not don't remove any
	do {
		for (var check=0; check <= (dest.options.length); check++){
			if (dest.options[check] != null){
				if (dest.options[check].selected == true){
					dest.options[check] = null;
					
					eval(current+'-=1;');
					updateRemain(1,updatefield,eval(current),limit,'add');
					
					/*if (type == 1) {
						LangStart--;
						updateRemain(1,"Language_remain",LangStart,LangdestLimit);
					}
					if (type == 0) {
						secStart--;
						updateRemain(1,"sector_remain",secStart,secLimit);
					}
					if (type == 2){
						Start--;
						updateRemain(1,"sector_remain",Start,destLimit);
					}*/
				}
			}
		}
	} while(nomorechecked(destin));
}

function RemoveItem(destin,updatefield,current,limit){
	dest = document.getElementById(destin);
	
	//Are They any to selected if not don't remove any
	do {
		for (var check=0; check <= (dest.options.length); check++){
			if (dest.options[check] != null){
				if (dest.options[check].selected == true){
					dest.options[check] = null;
					
					eval(current+'-=1;');
					updateRemain(1,updatefield,eval(current),limit,'add');
					
					/*if (type == 1) {
						LangStart--;
						updateRemain(1,"Language_remain",LangStart,LangdestLimit);
					}
					if (type == 0) {
						secStart--;
						updateRemain(1,"sector_remain",secStart,secLimit);
					}
					if (type == 2){
						Start--;
						updateRemain(1,"sector_remain",Start,destLimit);
					}*/
				}
			}
		}
	} while(nomorechecked(destin));
}

function nomorechecked(object){
	dest = document.getElementById(object);
	
	var found;
	found = false;
	for (var check=0; check <= (dest.options.length); check++){
		if (dest.options[check] != null){
			if (dest.options[check].selected == true){
				found = true;
			}
		}
	}
	
	if (found){
		return true
	}else{
		return false
	}
}

function AddSrcToSectLite(source1,source2,destin){
	src1 = document.getElementById(source1);
	src2 = document.getElementById(source2);
	dest = document.getElementById(destin);

    if (src1.selectedIndex != -1 && src2.selectedIndex != -1){	
		var already = 0;
	
		//Check To See If It's allready in the list
		for (var check=0; check <= (dest.options.length); check++){
			if (dest.options[check] != null){
				if (dest.options[check].value == src2.options[src2.selectedIndex].value){
					already = 1;
				}
			}
		}
		
		if (already == 1){
			if (src2.options[src2.selectedIndex].value == '') {
				alert('Item '+src1.options[src1.selectedIndex].text+' is already on the list');
			} else {
				alert('Item '+src1.options[src1.selectedIndex].text+' -> '+src2.options[src2.selectedIndex].text+' is already on the list');
			}
		}else{
			newslot = dest.options.length++;
			if (src2.options[src2.selectedIndex].value == '') {
				dest.options[newslot].value = src1.options[src1.selectedIndex].value;
				dest.options[newslot].text = src1.options[src1.selectedIndex].text+' -> All Relevant '+src1.options[src1.selectedIndex].text+' Sub Sectors';
			} else {
				dest.options[newslot].value = src2.options[src2.selectedIndex].value;
				dest.options[newslot].text = src1.options[src1.selectedIndex].text+' -> '+src2.options[src2.selectedIndex].text;
			}
		}
	}
}

function RemoveItemLite(destin,updatefield){
	dest = document.getElementById(destin);
	
	do {
		for (var check=0; check <= (dest.options.length); check++){
			if (dest.options[check] != null){
				if (dest.options[check].selected == true){
					dest.options[check] = null;
				}
			}
		}
	} while(nomorechecked(destin));
}
