


//******************************************************
//Toggle a form element "on" or "off" (disabled or enabled)
//******************************************************
function toggleElement (targetElement, onOff) {

  if (onOff == 'off') {
   document.forms['Program Info'].elements[targetElement].disabled = true;
    if (!document.all && !document.getElementById) {
      targetElement.oldOnFocus = 
        targetElement.onfocus ? targetElement.onfocus : null;
      targetElement.onfocus = skip;
    }
  }
  else {
    document.forms['Program Info'].elements[targetElement].disabled = false;
    if (!document.all && !document.getElementById) {
      targetElement.onfocus = targetElement.oldOnFocus;
    }
  }
}

function skip () { this.blur(); }



//******************************************************
//Function that checks for required and/or properly formatted fields.
//
//******************************************************

function validate(current_form) {
  	
  	if (current_form.elements['checkRequired'].value == 'true') {
  		nextStep = checkRequiredFields(current_form);
  	} else {
  		nextStep = true
  	}
  	if (nextStep) {
  		nextStep = checkNumberFields(current_form);
  	} else {return false}
  	if (nextStep) {
  		nextStep = checkFieldLengths(current_form);
  	} else {return false}
  	if (nextStep) {
  		nextStep = checkEmailFields(current_form);
			
  	} else {return false}
  	if (nextStep) {
  		nextStep = checkPasswords(current_form);
  	} else {return false}
  	if (nextStep) {
  		return true
  	} else {return false}
  
}

//*****************************************************
//change class of a page element based on its id.
//*****************************************************
function changeClass(id, className){
	var labelId = ""
	var lastDash = -1
	var year = ""
	
	if (null != document.getElementById(id + " Label")){		 
		document.getElementById(id + " Label").className = className
	}
	else if (! its_integer(id.substring(id.lastIndexOf("-") + 1, id.length))) {
		lastDash = id.lastIndexOf("-")
		if (lastDash != -1){
			
			labelId = id.substring(0, lastDash) + " Label" 
			//alert(labelId)
			if (null != document.getElementById(labelId)){
				document.getElementById(labelId).className = className
			}
		}
	}
	else{		

		lastDash = id.lastIndexOf("-")
		if (lastDash != -1){
			year = id.substring(lastDash + 1, id.length)
			
			if (its_integer(year)){
				labelId = id.substring(0, lastDash) + " Label" 
				//alert(labelId)
				if (null != document.getElementById(labelId)){
					document.getElementById(labelId).className = className
				}
			}
		}
	}	
}

//******************************************************
//confirm that password fields are the same
//******************************************************
function checkPasswords(current_form) {
    
    var missing_fields = new Array()
    var total_missing = 0
    var password = ""
		var passwordConfirm = ""
    // Loop through all the form elements
    for (counter = 0; counter < current_form.length; counter++) {
    
        // Find the elements with custom attributes of "Password"
				// and "Password Confirm"
        if (current_form[counter].password) {
					password = current_form[counter].value				
     		}       
				if (current_form[counter].passwordConfirm) {
					passwordConfirm = current_form[counter].value				
     		}  
    }
		
		//Compare Password to Password Confirm
		
   	if (password == passwordConfirm) {
			return true
		}
    else {    
       alert("Your passwords don't match!")        
    }
}

//******************************************************
//check to make sure certain fields are not empty
//******************************************************

function checkRequiredFields(current_form) {
    
    var missing_fields = new Array()
    var total_missing = 0
    
    // Loop through all the form elements
    for (counter = 0; counter < current_form.length; counter++) {
		
        // Is this a field that's mandatory?
        if (current_form[counter].mandatory) {
			var labelId = current_form[counter].name
			//alert(labelId)   
            // Is it an empty text field?
            if ((current_form[counter].type == "text" ||
						current_form[counter].type == "password" ||
						current_form[counter].type == "textarea") &&
						its_empty(current_form[counter].value)) {
              
                // If so, add the field to the array of missing fields
                missing_fields[total_missing] = current_form[counter]
                changeClass(labelId, 'error')	
                total_missing++
								
						// Does it contain only whitespace?
            } else if ((current_form[counter].type == "text" ||
						current_form[counter].type == "password" ||
						current_form[counter].type == "textarea") &&
						its_whitespace(current_form[counter].value)) {
              
                // If so, add the field to the array of missing fields
                missing_fields[total_missing] = current_form[counter]
                changeClass(labelId, 'error')	
                total_missing++
								
						// Is it a single-select list with no value?
            } else if ( current_form[counter].type == "select-one" && 
												current_form[counter].selectedIndex == "") {
              
                // If so, add the field to the array of missing fields
                missing_fields[total_missing] = current_form[counter]
                changeClass(labelId, 'error')		
                total_missing++
								
						// Is it a multi-select with no values?
            } else if ( current_form[counter].type == "select-multiple") {
              	// Check to see if all values are empty
								var somethingIsSelected = 0
								for (var i = 0; i < current_form[counter].options.length; i ++) {
										if (current_form[counter].options[i].selected) {
											somethingIsSelected = 1
										}
									}
								if (somethingIsSelected == 0) {	
                	// If so, add the field to the array of missing fields
                	missing_fields[total_missing] = current_form[counter]
                	changeClass(labelId, 'error')		
                	total_missing++
								}
			//Is it a checkbox that hasn't been checked?								
            } else if ((current_form[counter].type == "checkbox") && (! current_form[counter].checked)){
			// If so, add the field to the array of missing fields
				missing_fields[total_missing] = current_form[counter]
				changeClass(labelId, 'error')		
                total_missing++
            }
            else{
				changeClass(labelId, 'label')	
            }
            
        }
    }

    // Were there any fields missing?
    if (total_missing > 0) {
    
        // Start the message
        var missing_message = "Fields marked with '*' are required.\nPlease complete the following " +
			  (total_missing == 1 ? " field" : " fields\n") +
				" and then click the submit button again. " +
        "\n______________________________\n\n"
        
        // Loop through the missing fields
        for (counter = 0; counter < missing_fields.length; counter++) {
            missing_message += missing_fields[counter].name + "\n"
        }
    
        // Finish up and display the message
        missing_message += "\n______________________________\n\n" 
        alert(missing_message)
        
        // For emphasis, put the focus on the first missing field
        missing_fields[0].focus()
    }
    else {
    
        // Otherwise, go ahead and submit
        return true
    }
}

//******************************************************
//Check a string to see if it's empty (used by checkRequiredFields)
//******************************************************
function its_empty(string_value) {

    // Check for the empty string and null
    if (string_value == "" || string_value == null) {
    
        // If either, it's empty so return true
        return true
    }
    
    // Otherwise, it's not empty so return false
    return false
}


//******************************************************
//Check a string to see if it contains only whitespace (used by checkRequiredFields)
//******************************************************
function its_whitespace(string_value) {

    // These are the whitespace characters
    var whitespace = " \n\r\t"

    // Run through each character in the string
    for (var counter = 0; counter < string_value.length; counter++) {
        
        // Get the current character
        current_char = string_value.charAt(counter)
        
        // If it's not in the whitespace characters string,
        // return false because we found a non-whitespace character
        if (whitespace.indexOf(current_char) == -1) {
            return false
        }
    }
    
    // Otherwise, the string has nothing but
    // whitespace characters, so return true
    return true
}

//******************************************************
//check to make sure certain fields contain only numbers
//******************************************************

function checkNumberFields(current_form) {
    
    var non_numeric_fields = new Array()
    var total_non_numeric = 0
    
    // Loop through all the form elements
    for (counter = 0; counter < current_form.length; counter++) {
    
        // Is this a text field with custom attribute "numeric"?
        if ( (current_form[counter].type == "text" ||
						current_form[counter].type == "password" ||
						current_form[counter].type == "textarea") &&
						current_form[counter].numeric) {
			var labelId = current_form[counter].name	
            //trim the leading and trailing whitespace from all numeric fields
			current_form[counter].value = trim(current_form[counter].value)
            // If so, is it a non integer?
            if (!its_integer(current_form[counter].value)) {
              
                // If so, add the field to the array of non_numeric fields
                non_numeric_fields[total_non_numeric] = current_form[counter]
                changeClass(labelId, 'error')		
                total_non_numeric++
            }
            else{
				changeClass(labelId, 'label')	
            }
        }
    }

    // Were there any fields non_numeric?
    if (total_non_numeric > 0) {
    
        // Start the message
        var non_numeric_message = "Fields which take numeric input must not contain letters or special\n" +
															"characters such as currency symbols, commas, spaces or decimals.\n" +
															"Please correct the following" +
			  (total_non_numeric == 1 ? " field" : " fields\n") +
				"and then click the submit button again. " +
        "\n______________________________\n\n"
        
        // Loop through the non_numeric fields
        for (counter = 0; counter < non_numeric_fields.length; counter++) {
            non_numeric_message += non_numeric_fields[counter].name + "\n"
        }
    
        // Finish up and display the message
        non_numeric_message += "\n______________________________\n\n" 
        alert(non_numeric_message)
        
        // For emphasis, put the focus on the first non_numeric field
        non_numeric_fields[0].focus()
    }
    else {
    
        // Otherwise, go ahead and submit
        return true
    }
}

//******************************************************
//Checks a string to see if it's an integer (used by checkNumberFields)
//******************************************************
function its_integer(string_value) {

    // Run through each character in the string
    for (var counter = 0; counter < string_value.length; counter++) {
        
        // Get the current character
        current_char = string_value.charAt(counter)
        
        // If it's not a digit, return false
        if (!its_a_digit(current_char)) {
            return false
        }
    }
    
    // Otherwise, the string has nothing but
    // digits, so return true
    return true
}

//******************************************************
//Checks a character to see if it's a digit (used by its_integer)
//******************************************************
function its_a_digit(character) {

    var digit_characters = "0123456789"

    // If it's not in the digit_characters string,
    // then it's not a digit so return false
    
    if (digit_characters.indexOf(character) == -1) {
        return false
    }
    
    // Otherwise, it's a digit, so return true
    return true
}

/////////////////////////////////////////////////
//check to make sure certain fields do not exceed a specified length
////////////////////////////////////////////////

function checkFieldLengths(current_form) {
    
    var too_long_fields = new Array()
    var total_too_long = 0
    
    // Loop through all the form elements
    for (counter = 0; counter < current_form.length; counter++) {
    
        // Is this a text field with a custom maxchars attribute?
        if ( current_form[counter].type == "text" ||
						current_form[counter].type == "password" ||
						current_form[counter].type == "textarea" &&
						current_form[counter].maxchars > 0) {
            var labelId = current_form[counter].name
            // Is the actual length of the field value longer than the maxchar setting?
            if (current_form[counter].value.length > current_form[counter].maxchars) {
              	
                // If so, add the field to the array of too_long fields
                too_long_fields[total_too_long] = current_form[counter]
                changeClass(labelId, 'error')		
                total_too_long++
            } 
            else{
				changeClass(labelId, 'label')	
            }
        }
    }

    // Were there any fields too_long?
    if (total_too_long > 0) {
    
        // Start the message
        var too_long_message = "The following" +
			  (total_too_long == 1 ? " field" : " fields") +
				(total_too_long == 1 ? " exceeds" : " exceed") +
				" the specified chacter limit.\n Please check the character limit" +
				" and then click the submit button again. " +
        "\n______________________________\n\n"
        
        // Loop through the too_long fields
        for (counter = 0; counter < too_long_fields.length; counter++) {
            too_long_message += too_long_fields[counter].name + "\n"
        }
    
        // Finish up and display the message
        too_long_message += "\n______________________________\n\n" 
        alert(too_long_message)
        
        // For emphasis, put the focus on the first too_long field
        too_long_fields[0].focus()
    }
    else {
    
        // Otherwise, go ahead and submit
        return true
    }
}

//******************************************************
//check email fields to make sure they contain well-formatted addresses
//******************************************************

function checkEmailFields(current_form) {
    
    var email_fields = new Array()
    var total_email = 0
    
    // Loop through all the form elements
    for (counter = 0; counter < current_form.length; counter++) {
    
        // Is this a field with custom property "email"?
        if (current_form[counter].email) {
            var labelId = current_form[counter].name
            // Is it empty? If so, ignore and don't validate (just submit)
						  if (its_empty(current_form[counter].value)) {
								return true
							}
							if (its_whitespace(current_form[counter].value)) {
								return true
							}
						
						// If not empty, does the field not contain a well-formatted address?
            if (!valid_email(current_form[counter].value)) {
              
                // If so, add the field to the array of email fields
                email_fields[total_email] = current_form[counter]
                changeClass(labelId, 'error')	
                total_email++
            }
            else{
				changeClass(labelId, 'label')	
            } 
        }
    }

    // Were there any fields containing bad email addresses?
    if (total_email > 0) {
    
        // Start the message
        var email_message = "The follwing email" +
			  (total_email == 1 ? " address is" : " addresses are\n") +
				" not properly formatted.\n Please correct any errors " +				
				"and then click the submit button again. " +
        "\n______________________________\n\n"
        
        // Loop through the email fields
        for (counter = 0; counter < email_fields.length; counter++) {
            email_message += email_fields[counter].name + "\n"
        }
    
        // Finish up and display the message
        email_message += "\n______________________________\n\n" 
        alert(email_message)
        
        // For emphasis, put the focus on the first email field
        email_fields[0].focus()
    }
    else {
    
        // Otherwise, go ahead and submit
        return true
    }
}

//******************************************************
//Take a string and determine if it represents a well-formatted email address
//Used by checkEmailFields
//******************************************************
function valid_email(email_address) {

   
		//alert(email_address)
		// Check the length
    if (email_address.length < 5) {
        return false
    }
    
    // Check @ and .
    at_location = email_address.indexOf("@")
    dot_location = email_address.lastIndexOf(".")
    
    if (at_location == -1 || dot_location == -1 || at_location > dot_location ) {
        return false
    }

    // Is there at least one character before @?
    if (at_location == 0) {
        return false
    }
    
    // Is there at least one character between @ and .?
    if (dot_location - at_location < 2 ) {
        return false
    }

    // Is there at least one character after .?
    if (email_address.length - dot_location < 2) {
        return false
    }

    // Otherwise, it's a valid address, so return true
    return true
}





function displayWorkflowKey () {
	openKeyWindow()
}



function openKeyWindow(){
var popurl="../workflow_key.asp"
winpops=window.open(popurl,"","width=500,height=338,status,scrollbars,menubar,resizable,")
}


//******************************************************
//trim leading and trailing whitespace from the string argument
//******************************************************
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

//******************************************************
//check email fields to make sure they contain well-formatted addresses
//******************************************************
function submitToNewWindow(current_form){
	var strSearchURL = "/directory/search_results.asp"
	strSearchURL = strSearchURL + buildQueryString(current_form)
	newWindow(strSearchURL)
	return false
}

//******************************************************
//manage a search submission window
//******************************************************
// this is needed for function newWindow below
//var newWin = null
function newWindow(content){

	//if (newWin != null){
	//	newWin.close()
	//}
	newWin = window.open(content, 'newWin','width=800,height=500,scrollbars=yes,toolbar=yes,menubar=yes,location=yes,directories=no,status=yes,resizable=yes')
	newWin.focus()
}

//******************************************************
//
//******************************************************
function buildQueryString(current_form){
	var strQueryString = ""
	var strKeyValuePair = ""
	var firstElement = 1
	// Loop through all the form elements
    for (counter = 0; counter < current_form.length; counter++) {
		strKeyValuePair = ""
		if ( current_form[counter].type == "select-one" && !current_form[counter].selectedIndex == "") {
              strKeyValuePair = current_form[counter].name + "=" + current_form[counter].options[current_form[counter].selectedIndex].value
		} 
		// Is it a multi-select with no values?
        else if ( current_form[counter].type == "select-multiple") {
				var firstSelectedValue = 1
				for (var i = 0; i < current_form[counter].options.length; i ++) {
					if (current_form[counter].options[i].selected && current_form[counter].options[i].value != "0" && current_form[counter].options[i].value != "") {
						if (firstSelectedValue != 1){
							strKeyValuePair = strKeyValuePair + "&" +current_form[counter].name + "=" + current_form[counter].options[i].value
						}
						else{
							firstSelectedValue = 0
							strKeyValuePair = current_form[counter].name + "=" + current_form[counter].options[i].value
						}
					}
				}								
        } 
        //Is it a checkbox that hasn't been checked?
        else if ((current_form[counter].type == "checkbox") || (current_form[counter].type == "radio")){
			if(current_form[counter].checked){
				strKeyValuePair = current_form[counter].name + "=" + current_form[counter].value
			}
        }
        // Is it an empty text field?
        else if ((current_form[counter].type == "text" ||
						current_form[counter].type == "password" ||
						current_form[counter].type == "textarea" || current_form[counter].type == "hidden") &&
						!its_empty(current_form[counter].value) && !its_whitespace(current_form[counter].value)) {
				strKeyValuePair = current_form[counter].name + "=" + escape(current_form[counter].value)
        }
        
        if (strKeyValuePair != ""){
			if (firstElement != 1){
				strQueryString = strQueryString + "&" + strKeyValuePair
			}
			else{
				firstElement = 0
				strQueryString = "?" + strKeyValuePair
			}
        }
    
    }
	return strQueryString
}