var selectedList;
var availableList;
function createListObjects(){
	availableList=document.getElementById(availableField);
	selectedList=document.getElementById(selectedField);
}

function delAttribute(){
	var selIndex = selectedList.selectedIndex;
	if(selIndex < 0)return;
	availableList.appendChild(selectedList.options.item(selIndex));
	selectNone(selectedList,availableList);
	//setSize(availableList,selectedList);
}

function addAttribute(){
	var addIndex = availableList.selectedIndex;
	if(addIndex < 0)return;
	selectedList.appendChild(availableList.options.item(addIndex));
	selectNone(selectedList,availableList);
	//setSize(selectedList,availableList);
}

function setTop(top){
	document.getElementById('someLayer').style.top = top;
}
function setLayerTop(lyr,top){
	lyr.style.top = top;
}

function setSize(list1,list2){
	list1.size = getSize(list1);
	list2.size = getSize(list2);
}

function selectNone(list1,list2){
	list1.selectedIndex = -1;
	list2.selectedIndex = -1;
	addIndex = -1;
	selIndex = -1;
}

function getSize(list){
    /* Mozilla ignores whitespace, 
       IE doesn't - count the elements in the list */
    var len = list.childNodes.length;
    var nsLen = 0;
    //nodeType returns 1 for elements
	for(i=0; i<len; i++){
    		if(list.childNodes.item(i).nodeType==1)
            	nsLen++;
    }
    if(nsLen<2)
        return 2;
    else
        return nsLen;
}

function delAll(){
    var len = selectedList.length -1;
    for(i=len; i>=0; i--){
        availableList.appendChild(selectedList.item(i));
    }
    selectNone(selectedList,availableList);
    //setSize(selectedList,availableList);
}

function addAll(){
    var len = availableList.length -1;
    for(i=len; i>=0; i--){
        selectedList.appendChild(availableList.item(i));
    }
    selectNone(selectedList,availableList);
    //setSize(selectedList,availableList);
    
}

function showSelected(){
    var optionList =document.getElementById("selectedOptions").options;
    var data = '';
    var len = optionList.length;
    for(i=0; i<len; i++){
    	data += ',';
	data += optionList.item(i).value;
    }
    alert(data);
}







//start-selection-script
function populateSelected(){
	if ("" == $('#' + selectedHiddenField).attr("value")) {return;}
	
	var input = $('#' + selectedHiddenField).attr("value").split(",");
	var inputlen = input.length;
	var answer = '';
	
	if (0 == inputlen) {return;}
	for (var i = 0; i < inputlen; i++) 
	{
		answer = input[i];
		
		//add to selected
		var option = '<option value="' + answer + '" selected="selected">' + answer + '</option>';
		$('#' + selectedField).append(option);

		//remove from available
		$('#' + availableField).find('option').each(function(idx, item)
		{
			if($(this).attr("value") == answer) {
				$(this).remove();
			}
		});		
	}
	
	document.getElementById(selectedField).selectedIndex = -1;
}

$(window).load(function () {
	createListObjects();
	
	$("#contactform").submit(function(e){
		var selectedpackages = '';
		var docontinue = false;
		
		$('#' + selectedField).find('option').each(function(idx, item)
		{
			if (docontinue) { selectedpackages += ","; }
			selectedpackages += $(this).attr("value");
			docontinue = true;
		});
		
		$('#' + selectedHiddenField).attr("value", selectedpackages);
	});
	
	//this populates the selected item when we reload the page
	//maybe we should use 
	populateSelected();
});
//end-selection-script
