
    $(document).ready(function(){	

        $("#nav li").hover(
	      function () {
		    $("ul:first",this).show();
	      }, 
	      function () {
		    $("ul",this).hide();
	      }
	    );	
    	
        $("a.popup").readyPopups();

    });


    jQuery.fn.extend({

        readyPopups: function() {
	        return this.bind('click', function(event) {
		        SysinPopup.NewPopup(this.href);
		        return false;
	        });
	        return false;
            } 
    });

    SysinPopup = {

	    // create a new popup (not back/foreward navigation within the history stack)
	    NewPopup: function(url) {
		    var ie6top = document.documentElement.scrollTop + 'px';	//for ie6 to get the height that is needed to position the poup
		    //see if the window is already present and visible
		    var popup;
		    if (document.getElementById('popupWindow') == null) {
			    popup = document.createElement('div'); 
			    popup.setAttribute('id','popupWindow'); 
			    jQuery('body').append(popup); 
		    }
		    else { jQuery('div#popupWindow').hide(); }
    		
		    //ajax get the contents and populate the popup container
		    jQuery.get(url, function(data) {
			    jQuery('div#popupWindow')
				    .html(data)
				    .css('_top', ie6top).show();
    			
		    });
    		
		    //set the dimensions of the iframe to match the window (IE6 ONLY FIX)
		    jQuery('div#popupWindow iframe')
			    .css('width',jQuery(window).width() + 'px')
			    .css('height',jQuery(window).height() + 'px');
    			
            return false;
    	
        }
    };

    function ClosePopup() { jQuery('div#popupWindow').hide(); return false; }
    	
	function validRequired(formField, fieldLabel)
	{
		if (formField.value == "")
		{
			alert('Please enter a value for the "' + fieldLabel +'" field.');
			formField.focus();
			return false;
		}
		else
			return true;
	}
	
	function validDate(formField, fieldLabel, required)
	{
		if ((formField.value == "") && !required)
		    return true;

		if (required && !validRequired(formField, fieldLabel))
			return false;

	    var isoDt = toIsoDate(formField.value);
	    
        if (!isoDt.isValidDate())
 		{
 			alert('Please enter a valid date in the form m/d/yyyy for the "' + fieldLabel +'" field.');
			formField.focus();		
			return false;
		}
		
		return true;         
    }
    
    function toIsoDate(USDate)
	{
      var USDateReg = new RegExp("^(\\d{1,2})[/-](\\d{1,2})[/-](\\d{4})$");

      var matches = USDateReg.exec(USDate);

      if (!matches) return '';
      
      return matches[3] + "-" + matches[1] + "-" + matches[2];
	
	}


    String.prototype.isValidDate = function() {
      var IsoDateRe = new RegExp("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$");

      var matches = IsoDateRe.exec(this);
      if (!matches) return false;

      var composedDate = new Date(matches[1], (matches[2] - 1), matches[3]);

      return ((composedDate.getMonth() == (matches[2] - 1)) &&
              (composedDate.getDate() == matches[3]) &&
              (composedDate.getFullYear() == matches[1]));

    }
	
	
	function validZip(formField, fieldLabel, required)
	{

		if ((formField.value == "") && !required)
		    return true;

		if (required && !validRequired(formField, fieldLabel))
			return false;

        var validZipFormat = /^\d{5}(-\d{4})?$/;
    
        if (!validZipFormat.test(formField.value))
 		{
 			alert('Please enter a valid zip code for the "' + fieldLabel +'" field.');
			formField.focus();		
			return false;
		}
		return true; 
	}



    // Digits only, no signs or whitespace
	function validInt(formField, fieldLabel, required)
	{
		if ((formField.value == "") && !required)
		    return true;

		if (required && !validRequired(formField, fieldLabel))
			return false;
	  
        var validIntFormat = /^\d+$/;
    
        if (!validIntFormat.test(formField.value))
 		{
 			alert('Please enter only digits for the "' + fieldLabel +'" field (no commas or spaces).');
			formField.focus();		
		    return false;
		}
		
		return true;
	}

    // number  in a range, plus signs and commas allowed
	function intInRange(formField, fieldLabel, required, RangeFrom, RangeTo)
	{

		if ((formField.value == "") && !required)
		    return true;

		if (required && !validRequired(formField, fieldLabel))
			return false;

        var validIntFormat = /^[+-]?\d+$/;
    
		if (!validIntFormat.test(formField.value))
		{
 			alert('Please enter a valid integer for the "' + fieldLabel +'" field (no commas or spaces).');
			formField.focus();		
			return false;
        }
    
		var theInt = parseInt(formField.value);
		
        if (RangeFrom == null) 
		    return true; 

        if (RangeTo == null)
        {
		    if (theInt < parseInt(RangeFrom))
		    {
 			    alert('Please enter an integer >= ' + RangeFrom + ' for the  "' + fieldLabel +'" field.');
			    formField.focus();		
			    return false;
            }
        }
        else
        {
            if ((theInt < parseInt(RangeFrom)) || (theInt > parseInt(RangeTo)))
		    {
 			    alert('Please enter an integer between ' + RangeFrom + ' and ' + RangeTo + ' for the  "' + fieldLabel +'" field.');
			    formField.focus();		
			    return false;
            }

        }
        

        return true;        
		
	}



	function validMail(formField, fieldLabel, required)
	{
		if ((formField.value == "") && !required)
		    return true;

		if (required && !validRequired(formField, fieldLabel))
			return false;

 	    var theval = formField.value;

 		if (!((theval.indexOf(".") > 0) && (theval.indexOf("@") > 0)))
 		{
 			alert('Please enter a valid email for the "' + fieldLabel +'" field.');
			formField.focus();		
			return false;
		}

		return true;
	}

	function validPhone(formField, fieldLabel, required)
	{

		if ((formField.value == "") && !required)
		    return true;

		if (required && !validRequired(formField, fieldLabel))
			return false;
	    
        var removeNoise = formField.value.replace(/[\+\(|\)|\-|\s|\.]/g, '');
        
        var validPhoneFormat = /^\d{10,14}((x|ext\.?)\d{1,6})?$/i;
    
        if (!validPhoneFormat.test(removeNoise))
        {
 			alert('Please enter a valid phone number for the "' + fieldLabel +'" field.');
			formField.focus();		
			return false;
        }
		
		return true;

	}


	function validFax(formField, fieldLabel, required)
	{
		if ((formField.value == "") && !required)
		    return true;

		if (required && !validRequired(formField, fieldLabel))
			return false;
	    
        var removeNoise = formField.value.replace(/[\+\(|\)|\-|\s|\.]/g, '');
        
        var validFaxFormat = /^\d{10,14}$/;
    
        if (!validFaxFormat.test(removeNoise))
        {
 			alert('Please enter a valid phone number for the "' + fieldLabel +'" field.');
			formField.focus();		
			return false;
        }
		
		return true;

	}

