var radio_selection = "";

function submitForm(oForm){
  var bResult = false;
  switch (oForm.name){
  case 'frmRegister':
    bResult = validateRegisterForm(oForm);
    break;
  case 'frmRegisterChild':
    bResult = validateRegisterChildForm(oForm);
    break;
  case 'frmForgot':
    bResult = validateForgotForm(oForm);
    break;
  case 'frmLogin':
    bResult = validateLoginForm(oForm);
    break;
  default:
    alert("Oops, the form is not configured!");
    bResult = false;
    break
  }
  if(bResult){
    oForm.submit();
  }
}
//
function validateLoginForm(oForm){
  var o = new Object;
  o.bResult = true;
  o.sAlert = "";

  o = valNotNullField(oForm.nickname,"You didn't give us your nickname.\n",o);
  o = valNotNullField(oForm.password,"You didn't give us your password.\n",o);
  if (!o.bResult){
    alert("Oops!\n"+o.sAlert+"\nPlease try again...");
  }
  return o.bResult;
}
//
//
function validateForgotForm(oForm){
  var o = new Object;
  o.bResult = true;
  o.sAlert = "";

  o = valNotNullField(oForm.nickname,"You didn't give us your nickname.\n",o);
  if (!o.bResult){
    alert("Oops!\n"+o.sAlert+"\nPlease try again...");
  }
  return o.bResult;
}
//
function validateRegisterForm(oForm){
  var o = new Object;
  o.bResult = true;
  o.sAlert = "";

  // Validate
  o = valNotNullField(oForm.nickname,"You didn't give us your nickname.\n",o);
  o = valNotNullField(oForm.password,"You didn't give us your password.\n",o);
  o = valNicknameField(oForm.nickname,"Please use letters and numbers only for your nickname.\n",o);
  //
  o = valNotNullField(oForm.firstname,"You didn't give us your first name.\n",o);
  o = valNotNullField(oForm.lastname,"You didn't give us your last name.\n",o);
  o = valNotNameField(oForm.firstname,"Your first name may only contain letters.\n",o);
  o = valNotNameField(oForm.lastname,"Your first name may only contain letters.\n",o);

  var firstname = new String(oForm.firstname.value);
  if (firstname.indexOf(' and ', 0) != -1 || firstname.indexOf(' And ', 0) != -1
   || firstname.indexOf(' AND ', 0) != -1 ){
    o = _doError(oForm.firstname, o, "Sorry! You can't register two people at once. Please register separately.\n");
  }
  var lastname = new String(oForm.lastname.value);
  if (lastname.indexOf(' and ', 0) != -1 || lastname.indexOf(' And ', 0) != -1
   || lastname.indexOf(' AND ', 0) != -1 ){
    o = _doError(oForm.lastname, o, "Sorry! You can't register two people at once. Please register separately.\n");
  }

  o = valNotNullField(oForm.address,"You didn't give us your address.\n",o);
  if (!isAddr(oForm.address.value)){
    o = _doError(oForm.address, o, "Your address may only contain letters and numbers.\n");
  }
  o = valNotNullField(oForm.suburb, "You didn't give us your suburb.\n",o);
  o = valAlphaNumField(oForm.suburb,"Your suburb may only contain letters and numbers.\n",o);
  if(!oForm.state_id.selectedIndex){
    o = _doError(oForm.state_id, o, "You didn't give us your state.\n");
  }
  o = valNotNullField(oForm.postcode, "You didn't give us your postcode.\n",o);
  o = valNumberField(oForm.postcode,'Your postcode should be just numbers.',o);

  if (oForm.country.value==''){
    o = _doError(oForm.country_opt, o, "You didn't give us your country.\n");
  }
  
  /*
  if(!oForm.dob_day.selectedIndex){
    o = _doError(oForm.dob_day, o, "You didn't give us your date of birth.\n");
  }
  if(!oForm.dob_mon.selectedIndex){
    o = _doError(oForm.dob_mon, o, "You didn't give us your date of birth.\n");
  }
  if ((oForm.dob_day.selectedIndex == 31 && (oForm.dob_mon.selectedIndex == 2 || oForm.dob_mon.selectedIndex == 4 || oForm.dob_mon.selectedIndex == 6 || oForm.dob_mon.selectedIndex == 9 || oForm.dob_mon.selectedIndex == 11))
      || (oForm.dob_day.selectedIndex == 30 && oForm.dob_mon.selectedIndex == 2)
  ){
    o = _doError(oForm.dob_day, o, "There aren't that many days in that month!\n");
  }
  o = valNotNullField(oForm.age,"You didn't tell us how old you are!\n",o);

  oForm.dob_year.value = oForm.age.value;


  o = valOptionSelected(oForm.gender,"You didn't tell us if you are a girl or boy.\n",o);
  o = valNotNullField(oForm.home_no,"You didn't give us your home phone number.\n",o);
  */

  /*
  o = valPhoneNumberField(oForm.home_no,"Your home phone number must only contain numbers and the symbols ( ) and +.\n",o);
  if (oForm.home_no.value.length < 7){
    o = _doError(oForm.home_no, o, "Your home phone number doesn't seem long enough.");
  }
  */

  // todo remove o = valOptionSelected(oForm.paytv,"You didn't tell us if you have Nick JR on TV.\n",o);

  if (!o.bResult){
    alert("Oops!\n"+o.sAlert+"\nPlease try again...");
  }
  return o.bResult;
}

//
function validateRegisterChildForm(oForm){
  var o = new Object;
  o.bResult = true;
  o.sAlert = "";

  // Validate
  o = valNotNullField(oForm.firstname,"You didn't give us your child's first name.\n",o);
  o = valNotNullField(oForm.lastname,"You didn't give us your child's last name.\n",o);
  o = valNotNameField(oForm.firstname,"Your child's first name may only contain letters.\n",o);
  o = valNotNameField(oForm.lastname,"Your child's first name may only contain letters.\n",o);

  var firstname = new String(oForm.firstname.value);
  if (firstname.indexOf(' and ', 0) != -1 || firstname.indexOf(' And ', 0) != -1
   || firstname.indexOf(' AND ', 0) != -1 ){
    o = _doError(oForm.firstname, o, "Sorry! You can't register two children at once. Please register them separately.\n");
  }
  var lastname = new String(oForm.lastname.value);
  if (lastname.indexOf(' and ', 0) != -1 || lastname.indexOf(' And ', 0) != -1
   || lastname.indexOf(' AND ', 0) != -1 ){
    o = _doError(oForm.lastname, o, "Sorry! You can't register two children at once. Please register them separately.\n");
  }
  //
  //alert(oForm.gender[1].checked);
  if(!oForm.gender[1].checked && !oForm.gender[0].checked) {
     o = _doError(oForm.lastname, o, "Don't forget to select a gender!\n");
  }
  if (!o.bResult){
    alert("Oops!\n"+o.sAlert+"\nPlease try again...");
  }
  return o.bResult;
}

function onSubmitHandler(oForm){
  submitForm(oForm);
  return false; // always
}

function valNotNameField(oField,sAlert,o){
  if (!isName(oField.value)){
    o = _doError(oField,o,sAlert);
  }
  return o;
}
function isName(str){
  return (str.match("[^a-zA-Z ']") == null);
}
function isAddr(str){
  return (str.match("[^a-zA-Z 0-9,\-]") == null);
}
//-------------------------------------------------------

function countryCheck(sValue , sActiveState ) {
  ChangeStates( sValue , sActiveState )
  if( sValue == 'Other' ) {
    document.getElementById('countryrow').style.display = 'block';
    document.forms.frmRegister.country.value=document.forms.frmRegister.country_other.value;
  }
  else {
    document.getElementById('countryrow').style.display = 'none';
    document.forms.frmRegister.country.value=sValue;
  }
}

function ChangeStates( sCountry , sActiveState ) {
  var aStates_DropDown_Options = new Array();
  
  var sTmpCountry = 'AU';
  aStates_DropDown_Options[sTmpCountry] = new Array();
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 1 , 'ACT' );
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 2 , 'NSW' );
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 3 , 'NT' );
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 4 , 'QLD' );
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 5 , 'SA' );
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 6 , 'TAS' );
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 7 , 'VIC' );
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 8 , 'WA' );

  var sTmpCountry = 'NZ';
  aStates_DropDown_Options[sTmpCountry] = new Array();
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 9 , 'Other' );

  var sTmpCountry = 'Other';
  aStates_DropDown_Options[sTmpCountry] = new Array();
  aStates_DropDown_Options[sTmpCountry][aStates_DropDown_Options[sTmpCountry].length] = new Array( 9 , 'Other' );
  
  var select = document.forms.frmRegister.state_id;
  select.options.length = 0;

  if( typeof aStates_DropDown_Options[sCountry] != 'undefined' ) {
    select.options[0] = new Option('Select' , '');
    for( var i = 0; i < aStates_DropDown_Options[sCountry].length; i++ ) {
      select.options[(i+1)] = new Option(aStates_DropDown_Options[sCountry][i][1],aStates_DropDown_Options[sCountry][i][0]);
      if( sActiveState == aStates_DropDown_Options[sCountry][i][0] ) {
        select.options[(i+1)].selected = true;
      }
    }
  }
}



function countryOther() {
  

  var other = document.getElementById('othercountry')
  var show = document.getElementById('countryrow')
  var aus = document.getElementById('auscountry')

var colRadio = document.getElementsByName("country_opt");
for (var i = 0; i < colRadio.length; i++){
  if (colRadio[i].checked){
    if (colRadio[i].value == "other"){
        show.style.display = "block"
    }else if(colRadio[i].value == "AU"){
      show.style.display = "none"
    }

  }

}

}
