﻿function formCheck(){
  if(nullCheck() == false){ return false; }
  if(formatCheck() == false){ return false; }
  document.form1.submit();
}

function nullCheck(){
  var myForm = document.form1;

  if(myForm.name.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Name'", myForm.name);
    return false;
  }
  if(myForm.post.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Postal code'", myForm.post);
    return false;
  }
  if(myForm.prefecture.value == 0){
    alertAndFocus("It is not inputted to 'Prefecture'", myForm.prefecture);
    return false;
  }
  if(myForm.prefecture.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Prefecture'", myForm.prefecture);
    return false;
  }
  if(myForm.address1.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Other Address'", myForm.address1);
    return false;
  }
  if(myForm.tel.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Daytime Phone'", myForm.tel);
    return false;
  }
  if(myForm.email.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'E-mail Address'", myForm.email);
    return false;
  }
  if(myForm.car_year.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Model Year'", myForm.car_year);
    return false;
  }
  if(myForm.touroku1.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'First-year registration'", myForm.touroku1);
    return false;
  }
  if(myForm.touroku2.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'First-year registration'", myForm.touroku2);
    return false;
  }
  if(myForm.car_name.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Model name'", myForm.car_name);
    return false;
  }
  if(myForm.gread.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Glead'", myForm.gread);
    return false;
  }
  if(myForm.haiki.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Emissions'", myForm.haiki);
    return false;
  }
  if(myForm.color1.value == 0){
    alertAndFocus("It is not inputted to 'Color'", myForm.color1);
    return false;
  }
  if(myForm.kyori.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Mileage'", myForm.kyori);
    return false;
  }
  if(myForm.syaken1.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Inspection'", myForm.syaken1);
    return false;
  }
  if(myForm.syaken2.value.match(/^\s*$/)){
    alertAndFocus("It is not inputted to 'Inspection'", myForm.syaken2);
    return false;
  }
  if( radioCheck(myForm.shift) == false ){
    alertAndJump("It is not inputted to 'Transmission'", "anchor_shift");
    return false;
  }
  if( radioCheck(myForm.door) == false ){
    alertAndJump("It is not inputted to 'The number of Door'", "anchor_door");
    return false;
  }
  if( radioCheck(myForm.kudou) == false ){
    alertAndJump("It is not inputted to 'Driving method'", "anchor_kudou");
    return false;
  }
  if( radioCheck(myForm.fuel) == false ){
    alertAndJump("It is not inputted to 'Fuel type'", "anchor_fuel");
    return false;
  }
  if( radioCheck(myForm.touroku) == false ){
    alertAndJump("It is not inputted to 'Registration'", "anchor_touroku");
    return false;
  }
  if( radioCheck(myForm.loan1) == false ){
    alertAndJump("It is not inputted to 'Auto loan balance'", "anchor_loan1");
    return false;
  }

}

function formatCheck(){
  var myForm = document.form1;

  if(! isLegalChar(myForm.post.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'Postal code' is included", myForm.post);
    return false;
  }
  if(! isLegalChar(myForm.tel.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'Daytime Phone' is included", myForm.tel);
    return false;
  }
  if(! isLegalChar(myForm.keitai.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'Evening Phone' is included", myForm.keitai);
    return false;
  }
  if(! isLegalChar(myForm.fax.value, "0123456789-")){
    alertAndFocus("The letter which cannot be used for 'FAX Number' is included", myForm.fax);
    return false;
  }
  if(! isLegalChar(myForm.email.value, "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")){
    alertAndFocus("The letter which cannot be used for 'E-mail Address' is included", myForm.email);
    return false;
  }
  if(myForm.email.value.indexOf("@") == -1){
    alertAndFocus("The format of the 'E-mail Address' is wrong", myForm.email);
    return false;
  }

}

function alertAndFocus(alrtMsg, obj){
  alert(alrtMsg);
  obj.focus();
}

function alertAndJump(alrtMsg, dst){
  alert(alrtMsg);
  location.hash = dst;
}

function radioCheck(radioObj){
  var isChecked = 0;
  var index = 0;
  var i;
  for(i = 0; i < radioObj.length; i++){
    if(radioObj[i].checked){
      isChecked++;
      index = i;
      break;
    }
  }
  if(i == radioObj.length){
    return false;
  }
  else{
    return radioObj[index];
  }
}

function isLegalChar(target, canUse){
  for(i = 0; i < target.length; i++){
    if(canUse.indexOf(target.charAt(i)) == -1){
      return false;
    }
  }
  return true;
}
