﻿function validateDate(scr, arg) {
  var month = ddlmonth.value;
  var day = ddlday.value;        
  if (((month == 'month') && (day  != 'day')) || ((month != 'month') && (day  == 'day'))) {
    arg.IsValid = false; // If specifies one of them, then require the other one
  } else {
    if ( (( month=='April') || (month == 'June' ) || (month == 'September' ) || (month == 'November') ) && ( parseInt(day) > 30 ) ) {
      arg.IsValid = false; // These months can't have more than 30 days
    } else { 
      if ( month=='February' &&  parseInt(day) > 29 ) {
        arg.IsValid = false; // This month can't have more than 29 days
      } else {
        arg.IsValid = true; // Rest of cases are valid
      }  
    }    
  }
}
function ValidateBirthDay(scr, arg) {
  var day = arg.Value;
  var month = ddlmonth.value;            
  arg.IsValid = ( (day != 'day') && (month != 'month') )  && ( ((day <= 29) && (month == 'February')) 
                || ((day <= 30) && ((month == 'April') || (month == 'June') || (month == 'September') || (month == 'November')) ) 
                || ((day <=31) && ((month == 'January') || (month == 'March') || (month == 'July') || (month == 'August') || (month == 'December')) ) )                                                     
}
function ValidatePassword(scr, arg) { 
  var pwd = arg.Value;            
  arg.IsValid = (pwd.length > 3 && pwd.length <16);
}
var QuestionControl;
var CurrentCustomValue = '';
function CheckSecretQuestion(rblist) {
  var SecretQuestionControl = txtquestion;
  var SecretAnswerControl = txtanswer;            
  if (GetRadioValue(rblist) == '-1') {
     SecretQuestionControl.disabled = false;
     SecretQuestionControl.focus();                
     if (CurrentCustomValue != '' && CurrentCustomValue != 'Enter custom question here') {
       SecretQuestionControl.value = CurrentCustomValue;
       CurrentCustomValue = '';
     } else {
       SecretQuestionControl.value = ''; 
     }
  } else {
    if (SecretQuestionControl.value != 'Enter custom question here') {
      CurrentCustomValue = SecretQuestionControl.value;
    }
    SecretQuestionControl.value = 'Enter custom question here';
    SecretQuestionControl.disabled = true;
    SecretAnswerControl.focus();
    SecretAnswerControl.select();
  }
}        
function GetRadioValue(rblist) {
  var a=''; 
  var e = document.getElementsByName(rblist.replace(/_/g,'$'));
  for (var i=0; i < e.length; i++) { 
    if (e[i].checked) { 
      a = e[i].value;	break;
    }
  }
  return a; 
}
function SelectedQuestion(controlID) {
  QuestionControl = controlID;
}
function ValidateSecretQuestion(scr, arg) { 
  var secretQuestion = arg.Value;
  if (QuestionControl == undefined) {
    QuestionControl = rblquestion;                
  }
  arg.IsValid = ((GetRadioValue(QuestionControl) != '-1') || (GetRadioValue(QuestionControl) == '-1' && secretQuestion.length > 0 && secretQuestion != 'Enter custom question here'));
}
function stateChanged(value) {
  var city = document.getElementById('ddlCity');
  var location = document.getElementById('ddlLocation');
  var added = 0;            
  removeAllOptions(city);
  removeAllOptions(location);
  if ( value != "-1" ) {
    addOption(city, "-1", "choose your city");
    addOption(location, "-1", "---");
    for ( var row = 0; row < cityArray.length; ++row ) {
	  if ( cityArray[row][0] == value ) {
	    added = 1;
	    addOption(city, cityArray[row][1], cityArray[row][1]);
      }
    }
	if ( added != 1) {
      alert("We're sorry, but there are currently no locations in the state you selected. Please select a different state.");
    }
  } else {
    addOption(city, "-1", "---");
    addOption(location, "-1", "---");
  }          
  return;
}        
function cityChanged(value) {
  var state = document.getElementById('ddlState');
  var location = document.getElementById('ddlLocation');
  var added = 0;          
  removeAllOptions(location);          
  if ( value != "-1" ) {
    addOption(location, "-1", "choose your location");          
    for ( var row = 0; row < locationArray.length; ++row ) {
      if ( locationArray[row][0] == state.value && locationArray[row][1] == value ) {
	    added = 1;
	    addOption(location, locationArray[row][3], locationArray[row][2]);
      }
    }	            
    if ( added != 1) {
      alert("No locations were found for the selected city.");
    }
  } else {
    addOption(location, "-1", "---");
  }            
  return;
}      
function removeAllOptions(selectbox) {
  for(var i = selectbox.options.length-1; i >= 0; i--) {
    selectbox.remove(i); 
  }
}
function addOption(selectbox, value, text ) {
  var optn = document.createElement("OPTION");          
  optn.text = text;
  optn.value = value;          
  selectbox.options.add(optn);
}