// function for disabling/enabling other checkboxes in a group when the None checkbox is selected/deselected
function selectNoneCheckbox(GroupArray,GroupName) {
    OtherSpecifyFieldID = GroupName + '_other_specify';
    if(document.getElementById(GroupName + '_none').checked == true) {
        for(i=1; i<GroupArray.length; i++) {
            document.getElementById(GroupArray[i].id).checked = false;
            document.getElementById(GroupArray[i].id).disabled = true;
            if(GroupArray[i].id == GroupName + '_other') { 
                document.getElementById('fieldset_' + OtherSpecifyFieldID).style.display = 'none';                
                document.getElementById(OtherSpecifyFieldID).value = '';
            }
        }
    }
    if(document.getElementById(GroupName + '_none').checked == false) {
        for(i=1; i<GroupArray.length; i++) {
            document.getElementById(GroupArray[i].id).disabled = false;
        }
    }
}

//function to display Specify Other text input field after selecting an "Other" checkbox        
function displaySpecifyOther(OtherID) {    
    if(document.getElementById(OtherID).checked == true) {
        document.getElementById('fieldset_' + OtherID + '_specify').style.display = 'block';
        document.getElementById(OtherID + '_specify').focus();
    }    
    if(document.getElementById(OtherID).checked == false) {
        document.getElementById('fieldset_' + OtherID + '_specify').style.display = 'none';
        document.getElementById(OtherID + '_specify').value = '';
    }
}

//functions for Form validation
var defaultEmptyOK = false;
var mPrefix = "You did not enter a value into the ";
var mSuffix = " field. This is a required field. Please enter it now.";
var whitespace = " \t\n\r";
var iEmail = "This field must contain a valid e-mail address (eg. myname@yourcompany.com). Please re-enter it now."

function checkString(theField, s, emptyOK) {   
   // Next line is needed on NN3 to avoid "undefined is not a number" error
   // in equality comparison below.
   if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
   if ((emptyOK == true) && (isEmpty(theField.value))) return true;
   if (isWhitespace(theField.value)) return warnEmpty (theField, s);
   else return true;
}

function checkZipPostalCode(theField) {
    var PostalCodeVal = theField.value;
    var testPostalCodeVal = /^(\d{5}|\d{9}|\d{5}-\d{4}|[A-Z]\d[A-Z]\s*\d[A-Z]\d)$/.test(PostalCodeVal);
    if((PostalCodeVal.length != 5 && PostalCodeVal.length != 10 && PostalCodeVal.length != 7) || testPostalCodeVal == false) {
        return false;
    }
    else {
        return true;
    }
}

function checkCheckbox (theField, s) {
    if (theField.checked == false) return warnEmpty(theField, s);
    else return true;
}

function checkGroup(GroupArray) {  //checks to see if any of the form elements in a Checkbox/Radio Button group are selected
    for(i=0; i<GroupArray.length; i++) {
        checkedStatus = GroupArray[i].checked;
        if(checkedStatus==true) break;
    }
    if(checkedStatus==true) return true;
    else return false;
}

function isWhitespace (s) {   
var i;
    // Is s empty?
    if (isEmpty(s)) return true;
    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

function isEmpty(s) {   
return ((s == null) || (s.length == 0));
}
// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK, see comments of function isInteger.

function IsValidEmail(elElement) {
    var strReturnValue    =    "";
    var strValue    = elElement.value;
    var emailExp = /[a-z0-9_\-\.]+@[a-z0-9_\-\.]+\.[a-z]{2,6}$/i;
    var emailExp1 = /@/i;
        
    if (strValue.search(emailExp1) == 0)
        strReturnValue = "The field email address can only contain alphanumeric characters, @ and period(.) and should be of the form myname@mycompany.com";
    if (strValue.search(emailExp) < 0)
        strReturnValue    =    "The field email address can only contain alphanumeric characters, @ and period(.) and should be of the form myname@mycompany.com";
        
    return strReturnValue;
}

function checkEmail (theField, emptyOK) {   
  if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
  if ((emptyOK == true) && (isEmpty(theField.value))) return true;
  //!isEmail(theField.value, false)) 
  else if (IsValidEmail(theField) != "") return warnInvalid (theField, iEmail);
  else return true;
}

function warnEmpty (theField, s) {  
  document.location = '#fieldset_' + theField.name;   
  theField.focus();
  alert(mPrefix + s + mSuffix);
  return false;
}

function warnInvalid (theField, s) {  
  document.location = '#fieldset_' + theField.name;   
  theField.focus();
  theField.select();
  alert(s);
  return false;
}

var origFieldBkgrndImage = '';
var origFieldBkgrndRepeat = '';
var origFieldBkgrndPositionX = '';
var origFieldBkgrndPositionY = '';
var origFieldBkgrndColor = '';

function selectField (FieldID,FieldName) {
        FieldToFocus = FieldName;
        currentField = document.getElementById(FieldID);
        if(currentField.currentStyle){
            origFieldBkgrndColor = currentField.currentStyle['backgroundColor'];
            origFieldBkgrndImage = currentField.currentStyle['backgroundImage'];
            origFieldBkgrndRepeat = currentField.currentStyle['backgroundRepeat'];
            origFieldBkgrndPositionX = currentField.currentStyle['backgroundPositionX'];
            origFieldBkgrndPositionY = currentField.currentStyle['backgroundPositionY'];
        }
        else if(window.getComputedStyle) {
            origFieldBkgrndImage = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-image');
            origFieldBkgrndRepeat = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-repeat');
            origFieldBkgrndPositionX = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-position-x');
            origFieldBkgrndPositionY = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-position-y');
            origFieldBkgrndColor = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-color');
        }
        document.getElementById('label_'+FieldID).style.color = '#cc0000';
        document.getElementById(FieldID).style.background = '#FAC8C9';
        document.getElementById(FieldID).style.color = '#cc0000';
        //setTimeout("FieldToFocus.focus()",100);
}

function selectGroup (GroupArray,GroupName) {
    for(i=0; i<GroupArray.length; i++) {
        //document.getElementById(GroupArray[i].id).style.background='#cc0000';
        document.getElementById('label_'+GroupArray[i].id).style.color='#cc0000';
  }
  document.getElementById('label_' + GroupName).style.color='#cc0000';
}

function selectDateGroup (FieldID,FieldName,GroupID) {
        //FieldToFocus = FieldName;
        currentField = document.getElementById(FieldID);
        if(currentField.currentStyle){
            origFieldBkgrndColor = currentField.currentStyle['backgroundColor'];
            origFieldBkgrndImage = currentField.currentStyle['backgroundImage'];
            origFieldBkgrndRepeat = currentField.currentStyle['backgroundRepeat'];
            origFieldBkgrndPositionX = currentField.currentStyle['backgroundPositionX'];
            origFieldBkgrndPositionY = currentField.currentStyle['backgroundPositionY'];
        }
        else if(window.getComputedStyle) {
            origFieldBkgrndImage = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-image');
            origFieldBkgrndRepeat = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-repeat');
            origFieldBkgrndPositionX = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-position-x');
            origFieldBkgrndPositionY = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-position-y');
            origFieldBkgrndColor = document.defaultView.getComputedStyle(currentField,null).getPropertyValue('background-color');
        }
        document.getElementById('label_'+GroupID).style.color = '#cc0000';
        document.getElementById(FieldID).style.background = '#FAC8C9';
        document.getElementById(FieldID).style.color = '#cc0000';
        //setTimeout("FieldToFocus.focus()",100);
}

function selectCheckboxField (FieldID,FieldName) {
    document.getElementById('label_'+FieldID).style.color = '#cc0000';
}

function focusField(FieldID) {
    document.getElementById(FieldID).style.color = '#000000';
    document.getElementById(FieldID).style.fontWeight = 'bold';
}

function deselectField (FieldID) {
        currentField = document.getElementById(FieldID);
        currentField.style.backgroundImage = origFieldBkgrndImage;
        currentField.style.backgroundRepeat = origFieldBkgrndRepeat;
        currentField.style.backgroundPositionX = origFieldBkgrndPositionX;
        currentField.style.backgroundPositionY = origFieldBkgrndPositionY;
        currentField.style.backgroundColor = origFieldBkgrndColor;
        document.getElementById(FieldID).style.color = '#666666';
        document.getElementById(FieldID).style.fontWeight = 'normal';
        document.getElementById('label_'+FieldID).style.color = '#666666';
}

function deselectCheckboxField (FieldID) {
        document.getElementById('label_'+FieldID).style.color = '#666666';
}

function deselectGroup (GroupArray,GroupName) {
                for(i=0; i<GroupArray.length; i++) {
                //alert(GroupName[i].id);
                document.getElementById('label_'+GroupArray[i].id).style.color = '#666666';
                }
                document.getElementById('label_'+GroupName).style.color = '#666666';
}

function deselectDateGroup (FieldID,GroupID) {
        currentField = document.getElementById(FieldID);
        currentField.style.backgroundImage = origFieldBkgrndImage;
        currentField.style.backgroundRepeat = origFieldBkgrndRepeat;
        currentField.style.backgroundPositionX = origFieldBkgrndPositionX;
        currentField.style.backgroundPositionY = origFieldBkgrndPositionY;
        currentField.style.backgroundColor = origFieldBkgrndColor;
        document.getElementById(FieldID).style.color = '#666666';
        document.getElementById(FieldID).style.fontWeight = 'normal';
        document.getElementById('label_'+GroupID).style.color = '#666666';
}
//end
 
//function to toggle between a USA/Canada State/Province field and an International Region/State/Province field
function CountrySelect(CountryValue,FormRow1,FormRow2) {
        if(CountryValue != '' || country != '') {
                if(CountryValue == 'US' || CountryValue == 'CA' || CountryValue == 'United States' || CountryValue == 'Canada') {
                    if(FormRow1){
                        document.getElementById('fieldset_'+FormRow1).style.display = "block";
                        document.getElementById('fieldset_'+FormRow2).style.display = "none";
                        //document.location = '#fieldset_' + FormRow1;
                        //document.getElementById(FormRow1).focus();
                        document.getElementById(FormRow2).value = '';
                    }
                }
                else {
                    if(FormRow2) {
                        document.getElementById('fieldset_'+FormRow2).style.display = "block";
                        document.getElementById('fieldset_'+FormRow1).style.display = "none";
                        //document.location = '#fieldset_' + FormRow2;
                        //document.getElementById(FormRow2).focus();
                        document.getElementById(FormRow1).value = '';
                    }
                }
        }
        else {
        document.getElementById('fieldset_'+FormRow1).style.display = "none";
        document.getElementById('fieldset_'+FormRow2).style.display = "none";
        }
}
//end
