$(document).ready(function(){

   //Help popups
   $("a.helpGrey").click(function(e){
   		
   		//Hide all open popups
   		$('.popupBox').hide();
   		//Get the ID of the link clicked   		   	
   		var theID = $(this).attr('id');
   		//Strip the last two characters off the link
   		theID = theID.substring(0,theID.length - 2);
   		//Add 'Div' to the end so it matches the target div
   		thePopup = theID + 'Div';
   		//Clean invalid characters out of the id
   		thePopupNewId = thePopup.replace(/:/,'');
   		thePopupNewId = thePopupNewId.replace(/_/,'');
   		//Set the ID of the target to the new, cleaner ID
   		document.getElementById(thePopup).setAttribute('id', thePopupNewId);
   		
   		//Set the position
		$('#' + thePopupNewId).addClass('newPopup');
		var $div = $('<div/>').attr('class', 'newPopup popupBox');
		var $html = $('#' + thePopupNewId).html();
		
		$div.html($html);
		$div.appendTo('body');
		$div.css('left', e.pageX  + 5 + 'px');
		$div.css('top', e.pageY  - 12 + 'px');
		$div.click(function(){
			if ($.browser.msie && $.browser.version <= 7 ){
				$div.hide();
   			}else{
				$div.fadeOut();
   			}
		});		
   		//Do the animation
   		if ($.browser.msie && $.browser.version <= 7 ){
				$div.show();
   		}else{
				$div.fadeIn();
   		}
   		
   		//Set the ID of the target back to it's original, dirty self.
   		document.getElementById(thePopupNewId).setAttribute('id', thePopup);
   		return false;
   
   });
   
   $("a.help2").click(function(e){
   		
   		//Hide all open popups
   		$('.popupBox2').hide();
   		//Get the ID of the link clicked   		   	
   		var theID = $(this).attr('id');
   		//Strip the last two characters off the link
   		theID = theID.substring(0,theID.length - 2);
   		//Add 'Div' to the end so it matches the target div
   		thePopup = theID + 'Div';
   		//Clean invalid characters out of the id
   		thePopupNewId = thePopup.replace(/:/,'');
   		thePopupNewId = thePopupNewId.replace(/_/,'');
   		//Set the ID of the target to the new, cleaner ID
   		document.getElementById(thePopup).setAttribute('id', thePopupNewId);
   		
   		//Set the position
		$('#' + thePopupNewId).addClass('newPopup');
		var $div = $('<div/>').attr('class', 'newPopup popupBox2');
		var $html = $('#' + thePopupNewId).html();
		
		$div.html($html);
		$div.appendTo('body');
		$div.css('left', e.pageX  + 5 + 'px');
		$div.css('top', e.pageY  - 12 + 'px');
		$div.click(function(){
			if ($.browser.msie && $.browser.version <= 7 ){
				$div.hide();
   			}else{
				$div.fadeOut();
   			}
		});		
   		//Do the animation
   		if ($.browser.msie && $.browser.version <= 7 ){
				$div.show();
   		}else{
				$div.fadeIn();
   		}
   		
   		//Set the ID of the target back to it's original, dirty self.
   		document.getElementById(thePopupNewId).setAttribute('id', thePopup);
   		return false;
   
   }
   
   
   );
   
      
   

      
   /*
   //Form Validation
   
   // Display the error and highlight the fields
   function throwError(theInput){
   		if (!document.getElementById("err")){
			var container = document.getElementById("mainBody");
			var theForm = container.firstChild; 
			var errorDiv = document.createElement("div");
		   	errorDiv.setAttribute("id", "err");
		   	container.insertBefore(errorDiv, container.firstChild);
		   	var errPara = document.createElement("p");
   			var newErr = document.createTextNode("The fields in red below have not been completed correctly. Please check and re-enter.");
   			errPara.appendChild(newErr);
   			document.getElementById("err").appendChild(errPara);
   		}
   		var inputElement = document.getElementById(theInput);
		if(inputElement.parentNode.getAttribute("class") != "err"){
			$(inputElement).wrap("<span class='err'></span>");   		
		}
		if(document.getElementById("err").display = "none"){
			$("#err").fadeIn("slow");
		}
   }
    */
   
	/* Clear pre-filled inputs */
	/*
	$('input').css('color', '#666');
	$('input').focus(function(){
		if(!$(this).hasClass('cleared')){
			$(this).attr('value', '');
			$(this).addClass('cleared');
		}
		$(this).css('color', '#000');
	});
	 */
	/*    
   //Find Address
   if(document.getElementById("findAddress") && document.getElementById("foundAddress")){
  
   		if(document.getElementById("foundAddress").display != "none"){
   			$("#findAddress").click(function(){
   				if(document.getElementById("form2:postcode").value != "" && document.getElementById("form2:houseNumberName").value != ""){
   					$("#foundAddress").slideDown();
   					return false;
   				}else{
   					throwError("form2:houseNumberName");
   					throwError("form2:postcode");
   					return false;
   				}
   			});
   		
   		}
   }
   */
   
   // Doc Upload Information boxes
   if($('.alert')){
   
   		$("table.documentTypes li a, .intro a").click(function(){
			var theAlert = $(this).attr('href');
  			if($(theAlert).css("display", "none")){
  				$(theAlert).fadeIn();
  				return false;
  			}
   		});
   		
   		$(".alert h4").click(function(){
   			$(this).parent("div").fadeOut();
   		});
   }
   
   
   /* Assign all input fields parents a classname */
   $("input").parent('p').addClass('inputPara');
   $("select").parent('p').addClass('inputPara');
   
  
   
   
   /* Make Blue headings collapsible in Step 3 */
   
   if($('h2.boxTitle')){
   		$('h2.boxTitle').click(function(){
   			$(this).siblings().slideToggle();
   			$(this).toggleClass('closed');
   		});
   }
   
   /* Add errorField spans for errors */	
   	if(!$('select.errorField').parent('span.errorField')){
		$('select.errorField').wrap("<span class='errorField'></span>");
   	}
   
 });
 
    //Just so the old inline JS code doesn't throw errors.
   function drag(arg1, arg2){
   	//Do nothing
   }
   
   
    function hidePop(theId){
   	//do nothing
    }
    
	/* Copy from popus.js to use new scripts for enrolment restructure */
   	function Browser() {
		var ua = window.navigator.userAgent;
  
  		this.isIE = false;
  		this.vers = null;

  		var msie = ua.indexOf ( "MSIE " )
  		if ( msie > 0) {
  			this.isIE = true;
  			this.vers = parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
  		} else {
    		this.isIE = false;
  		}
	}

	var myBrowser = new Browser();   
   