function addToFavorites(pageName,urlAddress){
	var ver = navigator.appName;
	var num = parseInt(navigator.appVersion);
	if ((ver == "Microsoft Internet Explorer")&&(num >= 4)) {
		window.external.AddFavorite(urlAddress,pageName);
	}else{
		window.sidebar.addPanel(pageName, urlAddress, "")
	}
}

function selectAll(field, ids){
	var aIDs = ids.split(",");
	for(i=0;i < aIDs.length;i++){
		id = document.getElementById(aIDs[i]);
		id.checked = field.checked;
	}
}

function drawPageTools(){
	document.write("<ul id='pageOptions'>");
	document.write("<li><a href=\"#\" onclick=\"window.print(); return false\" title=\"Print page\">Print page</a></li>");
	document.write("<li class=\"last\"><a href=\"#\" onclick=\"addToFavorites('The Princes Teaching Institute','http://www.princesteachinginstitute.co.uk/');\" title=\"Bookmark page\">Bookmark page</a></li>");
	document.write("</ul>");		
}

// Expenses select box fix for IE
var BaseID="category";
var CurrentlyShown=0;
var Clicked=false;
var IsIE = document.all?true:false;

function selectMouseOver(Number){
	if(IsIE && !Clicked){
		showSelect(Number);
		CurrentlyShown=Number;
	}
}

function selectMouseDown(Number) {
	if(IsIE){
		Clicked=true;
	}
}

function selectMouseOut(Number) {
	if(IsIE && !Clicked){
		document.getElementById(BaseID+Number).className = "";
		CurrentlyShown=0;
	}
}

function selectChange(Number) {
	if(IsIE){
		Clicked=false;
		selectMouseOut(Number);
	}
}

function showSelect(Number){
	i=1;
	while(ob=document.getElementById(BaseID+i)){
		ob.className = (i==Number)?"expanded":"";
		i++;
	}
}

$(document).ready(function(){
// add/remove delegate function
	var delegates = ($("#Counter").val()!='')?parseInt($("#Counter").val()):1;
        
	$('a.add-delegate').live('click', function() {                

		// increment our delegates count
		delegates = delegates + 1;

		// create a clone of the delegate fieldset and put into a variable for now
		//myClone = $(this).parent().clone(); 
		myClone = $('fieldset.delegate:first').clone(); 
		
                
		myClone.find("input, select").each(function (i) {
			// increment all name attributes on inputs
			currentName = $(this).attr('name');
			currentName = currentName.substring(0, currentName.length - 1);
			currentName = currentName + delegates;
			$(this).attr('name', currentName);
			
			// increment all id attributes on inputs
			currentId = $(this).attr('id');
			currentId = currentId.substring(0, currentId.length - 1);
			currentId = currentId + delegates;
			$(this).attr('id', currentId);
			
		});
		
		myClone.find("label").each(function (i) {
			// increment all 'for' attributes on labels
			currentLabel = $(this).attr('for');
			currentLabel = currentLabel.substring(0, currentLabel.length - 1);
			currentLabel = currentLabel + delegates;
			$(this).attr('for', currentLabel);
		});
		
		// increment legend
		currentLegend = myClone.find('legend').text();
		currentLegend = currentLegend.substring(0, currentLegend.length - 1);
		currentLegend = currentLegend + delegates;
		myClone.find('legend').text(currentLegend);
		
		//empty all fields on the clone
		myClone.find('input, textarea').val('');
                myClone.find('select').val('-');
                
                // Update delegate count
                $("#Counter").val(delegates);
		
		// append our updated clone to the form
		myClone.appendTo('fieldset.delegateInfo');

		return false;
	});
	
	$('a.remove-delegate').live('click', function () {
                if (delegates > 1){
                    delegates--;
                    $('fieldset.delegate:last').remove();
                    $("#Counter").val(delegates);
                }
	});

        $('a.filterPub').click(function () {
            $('#filter').submit();
            
        });
		
		
		
		//-----------------------------------------------------------------------------------------------
		//              apply CSS to checkboxes checked - Filter results
		//-----------------------------------------------------------------------------------------------	

				
			$('#filterCheckBoxes input:checkbox').change(function() {
				if($(this).is(':checked')) 
					$(this).parent().addClass('selected'); 
				else 
					$(this).parent().removeClass('selected');
			});
        
        
	 
        /*---------------------------------------------------------------*/
        /* Staffroom Profile drop down menu */
        /*---------------------------------------------------------------*/

		$("#profileOptions").click(function (e)
			{
					e.preventDefault();		
					$("#profileDropDown").slideToggle(200); 			
			}); 
			
			//Clickout - checks if click event occured OUTSIDE area of interest
		$(document).click(function(e)
		{
			//if parent of this click is NOT #siteTools AND this click is NOT #profileDropDown then hide menu
			if($(e.target).parents().is('#siteTools') == false && $(e.target).is('#profileDropDown') == false)
			{
				$("#profileDropDown").slideUp(200);
			}
			
		});
	
        /*---------------------------------------------------------------*/
        /* grab these form elements and apply thess classes to them */
        /*---------------------------------------------------------------*/	
		
		$("label[for='SpecialRequirements'], label[for='PersonalSummary'], label[for='PhotoUpload'], label[for='PhotoRemove']").parent().addClass('fullwidth_label');
		
		$("form[id='edit-profile']").find('div.info:last').addClass('info_leftPos');
	
 
 });
        
        
