﻿// returns value from specified set of radio buttons
function radioValue(radiofield)
{
	var returnValue = '';
	if(radiofield.length == null)
	{
		if(radiofield.checked)
		{
			return radiofield.value;
		}
		else
		{
			return '';
		}
	}
	for(j=0; j<radiofield.length; j++)
	{
		if(radiofield[j].checked == true)
		{
			returnValue = radiofield[j].value;
			return returnValue;
		}
	}
	return returnValue;
}


//returns the value from a selectlist
function selectlistValue(formfield)
{
	var formfieldIndex = formfield.selectedIndex;
	return formfield.options[formfieldIndex].value;
}

// verifies that a value has been chosen in a dropdown selectlist (assumes '' is not a value)
function selectlistRequired(formfield, errorMsg)
{
	var fieldValue = selectlistValue(formfield);
	if(fieldValue != '')
	{
		return true;
	}
	else
	{
		alert(errorMsg);
		return false;
	}
}

//returns the value(s) from a set of checkboxes 
function checkboxValue(formfield)
{
	var hits = 0;
	var returnValue = '0';
	if(formfield.length == null)
	{
		if(formfield.checked)
		{
			return formfield.value;
		}
		else
		{
			return 0;
		}
	}
	for(i=0; i<formfield.length; i++)
	{
		if(formfield[i].checked == true)
		{
			hits++;
			if(hits==1)
			{
				returnValue = formfield[i].value;
			}
			if(hits!=1)
			{
				returnValue = returnValue +',' +formfield[i].value;
			}
		}
	}
	return returnValue;
}

// verifies that the specified textfield is not blank
function checkBlank(formfield,errorMsg)
{
	var fieldLen = formfield.value.length;
	var fieldValue = formfield.value;
	var hits = 0;
	for(i=0; i<fieldLen; i++)
	{
		if(fieldValue.substr(i,1) != ' ')
		{
			hits ++;
		}
	}
	if(hits == 0)
	{
		alert(errorMsg);
		return false;
	}
	return true;
}

// verifies that a radio button has been checked
function radioRequired(formfield, errorMsg)
{
	var fieldValue = radioValue(formfield);
	if (fieldValue > 0)
	{
		return true;
	}
	else
	{
		alert(errorMsg);
		return false;
	}
}	

// verifies that a checkbox has been selected
function checkboxRequired(formfield, errorMsg)
{
	var fieldValue = checkboxValue(formfield);
	if(fieldValue != 0)
	{
		return true;
	}
	else
	{
		alert(errorMsg);
		return false;
	}
}

// verifies that the value in a textfield is an integer
function checkInteger(formfield,errorMsg)
{
	var fieldLength = formfield.value.length;
    if(formfield.value != '')
    {
    	var foundNumber = false;
        var positionCounter = 0;
        var numberCounter = 0;
			
        while(positionCounter < fieldLength)
        {
            while(numberCounter < 10)
            {
                if(formfield.value.substr(positionCounter,1) == numberCounter)
                {
                    foundNumber = true;
                    numberCounter = 11;
                }
                else
                {
                    foundNumber = false;
                    numberCounter++;
                }
            }
            
            if(!foundNumber)
            {
                positionCounter = 100;
            }
            else
            {
                positionCounter++;
                numberCounter = 0;
            }
        }
            
        if(!foundNumber)
        {
            alert(errorMsg)
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return true;
    }
}

// verifies that the integer in a textfield is in the specified range
function checkIntegerRange(formfield,fieldLength,bottomRange,topRange,errorMsg)
{
	var integer = checkInteger(formfield,fieldLength,errorMsg);
	if(!integer)
	{
		return false;
	}
	else
	{
  		if (bottomRange != null)
		{
    		if (formfield.value < bottomRange)
			{
				alert(errorMsg);
				return false;
			}
		}
  		if (topRange != null)
		{
			if (formfield.value > topRange)
			{
				alert(errorMsg);
				return false;
			}
 		}
		return true;
	}
}

//Verifies that a form text field contains a valid number
function checkNumber(formfield,errorMsg)
{
    object_value = formfield.value;
    //Returns true if value is a number or is NULL.  otherwise returns false	
    if (object_value.length == 0)
    return true;
    //Returns true if value is a number defined as having an optional leading + or -.
    // having at most 1 decimal point.  otherwise containing only the characters 0-9.
    var start_format = " .+-0123456789";
    var number_format = " .0123456789";
    var check_char;
    var decimal = false;
    var trailing_blank = false;
    var digits = false;
    //The first character can be + - . blank or a digit.
    check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
    if (check_char == 1)
    	decimal = true;
    else if (check_char < 1)
		{
		alert(errorMsg);
    	return false;
		}
    //Remaining characters can be only . or a digit, but only one decimal.
    for (var i = 1; i < object_value.length; i++)
    {
    	check_char = number_format.indexOf(object_value.charAt(i))
    	if (check_char < 0)
			{
			alert(errorMsg);
    		return false;
			}
    	else if (check_char == 1)
   		{
    		if (decimal)	
				{
				// Second decimal.
				alert(errorMsg);
    			return false;
				}
    		else
    			decimal = true;
    	}
    	else if (check_char == 0)
    	{
    		if (decimal || digits)	
				{
				// ignore leading blanks
    			trailing_blank = true;
				}
    	}
    	else if (trailing_blank)
			{
			alert(errorMsg);
    		return false;
			}
    	else
    		digits = true;
    }	
    //All tests passed, so...
    return true
}

// verifies that a selection is made in formfield2 when a specified value is selected in formfield1
function selectlistMatch(formfield1, formfield2, value1, errorMsg)
{
	var field1Value = selectlistValue(formfield1);
	var field2Value = selectlistValue(formfield2);
	if(field1Value == value1 && (field2Value == 0 || field2Value == ''))
	{
		alert(errorMsg);
		return false;
	}
	return true;
}	

// verifies that a date field contains a valid date
function checkDate(monthField, dayField, yearField, errorMsg) 
{	
	var Month = selectlistValue(monthField);
	var Day = selectlistValue(dayField);
	var Year = selectlistValue(yearField);
	
	if((Month == 4 || Month == 6 || Month == 9 || Month == 11) & Day == 31)
	{
		alert(errorMsg);
		dayField.selectedIndex=0;
		return false;
	}

	if ((Month == 2 & (Day == 31 || Day == 30)) || (Month == 2 & Day == 29 & (Year != 2000 & Year != 2004)))
	{
		alert(errorMsg);	
		monthField.selectedIndex=0;
		return false;
	}
	return true;
}

// verifies that a choice has been made from a set of date field dropdowns
function dateRequired(monthField, dayField, yearField, errorMsg) 
{	
	if(selectlistValue(monthField) == 'mm' || selectlistValue(dayField) == 'dd' || selectlistValue(yearField) == 'yyyy')
	{
		alert(errorMsg);	
		return false;
	}
	return true;
}

//enables Check All checkbox
function checkAll(formName,fieldName,checkallFieldName)
{
	var arrayValues = new Array();
	var stringCat = formName+'.'+fieldName;
	arrayValues = eval(stringCat);
	var stringField = formName+'.'+checkallFieldName;
	stringField = eval(stringField);
	for (k=0; k < arrayValues.length; k++)
	{
		if (stringField.checked == true)
		{
			arrayValues[k].checked = true;
		} else {
			arrayValues[k].checked = false;
		}
	}
}

// verifies that text in a formfield is at least as long as specified min value
function checkTextLen(formfield,min,errorMsg) 
{
	var fieldLen = formfield.value.length;
	if(fieldLen < min)
	{
		alert(errorMsg);
		return false;
	}
	return true;
}	

// verifies that an email is correctly formatted
function checkEmail(formfield) 
{
	var emailString = formfield.value;
    emailString = trim(emailString,'both');
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailString.match(emailPat);
	if (matchArray==null) 
	{
		alert("The EMAIL ADDRESS you entered seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null) 
	{
	    alert("The EMAIL ADDRESS username does not seem to be valid.");
	    return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
	{
		for (var i=1;i<=4;i++) 
		{
		    if (IPArray[i]>255) 
			{
		        alert("The EMAIL ADDRESS Destination IP address for is invalid!");
				return false;
			}
		}
	}
	var domainArray=domain.match(domainPat);
	if (domainArray==null) 
	{
		alert("The EMAIL ADDRESS domain name doesn't seem to be valid.")
	    return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
	{
	   	alert("A valid EMAIL ADDRESS must end in a three-letter domain, or two letter country.");
	   	return false;
	}
	if (len<2) 
	{
	   	alert("The EMAIL ADDRESS you entered is missing a hostname!");
	   	return false;
	}
	return true;
}

// verifies that the specified password and confirm password fields match
function checkPasswordMatch(passwordFormfield,confirmFormfield,errorMsg)
{
    var passwordValue = passwordFormfield.value;
    var confirmValue = confirmFormfield.value;
    if(passwordValue != confirmValue)
    {
        alert(errorMsg);
        return false;
    }
    return true;
}

//puts the name of a file in a browse field into a hidden field
function captureFileName(filefield,hiddenfield) 
{
		file_name = getFileName(filefield.value.toString());
		hiddenfield.value = file_name;
}

// gets the name of a file from a full filepath
function getFileName(filepath) 
{
		temp_array = filepath.split('\\');
		file_name = temp_array[ temp_array.length - 1 ];
		temp_array = file_name.split('/');
		file_name = temp_array[ temp_array.length - 1 ];
		return file_name
}

// used for renaming uploaded files (populates a rename field with the initial name of the file)
function populateRenameField(filefield,renamefield) 
{
	var file_path_1 = '';
	file_path = filefield.value.toString();
	if(file_path != file_path_1) 
	{
		file_path_1 = file_path;
		file_name = getFileName(file_path);
		renamefield.value = file_name;
	}
}

