/**
 *
 * 全角チェックを行います
 *
 * @param   string  val     チェック対象文字列
 * @param   boolean bYenFlg ￥を許可フラグ
 * @param   boolean spaceFlg ' 'を許可フラグ
 *
 * @return  boolean 全て全角の場合 true, そうでなければ false
 *
 */
function isZen(val, bYenFlg, spaceFlg){
    var len = val.length;
    for(i = 0; i < len; i++){
        var c = val.charCodeAt(i);
        if(c < 256 || (0xff61 <= c && c <= 0xff9f)){
            // "\" 半角 \ だけOK
            if(bYenFlg){
                if(c == 92) continue;
            }
            if(spaceFlg){
                if(c == 32) continue;
            }

            // “×”、“÷”、“¶”、“°”、“±”、“§”、“¨”、“´”OK
            if(c == 215 || c == 247 || c == 182 || c == 176 || c == 177 || c == 167 || c == 168 || c == 180) continue;

            return false;
        }
    }
    return true;
}
//メールアドレスの簡易チェック
function mailIsRight(val_str){

　　var reg_str=new RegExp("^.+@.+\..+");

    if(val_str.match(reg_str) != null && val_str.indexOf(".")>2 &&　val_str.indexOf("@")>0){

        //alert(val_str.match(reg_str));
        return true;
}
    return false;
}
//電話番号の簡易チェック
function valid_tel(tel) {
    if(tel==""){
        return false;
    }
    if(tel.length < 10 || tel.length > 13)
        return false;
    if ((tel.match(/^[0-9]+\-[0-9]+\-[0-9]+$/) == null)) {
        return false;
    }
    return true;
}
function checkshopContact(){
    var err_info = "";
    var firstErrElement = null;
    if(document.frm_shopContact.surname.value == "") {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.surname;
    }
    if(!isZen(document.frm_shopContact.surname.value, false, false)) {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.surname;
    }
    if(document.frm_shopContact.prename.value == "") {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.prename;
    }
    if(!isZen(document.frm_shopContact.prename.value, false, false)) {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.prename;
    }
    if(document.frm_shopContact.surname.value == "" || document.frm_shopContact.prename.value == ""){
        err_info += "・お名前が入力されていません。<br>";
    }else{
        if(!isZen(document.frm_shopContact.prename.value, false, false) || !isZen(document.frm_shopContact.surname.value, false, false)) {
            err_info += "・氏名は全角で入力してください。<br>";
        }
    }

    if(document.frm_shopContact.surname_furigana.value == "") {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.surname_furigana;
    }
    if(!isZen(document.frm_shopContact.surname_furigana.value, false, false)) {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.surname_furigana;
    }
    if(document.frm_shopContact.prename_furigana.value == "") {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.prename_furigana;
    }
    if(!isZen(document.frm_shopContact.prename_furigana.value, false, false)) {
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.prename_furigana;
    }
    if(document.frm_shopContact.surname_furigana.value == "" || document.frm_shopContact.prename_furigana.value == ""){
        err_info += "・ふりがなが入力されていません。<br>";
    }else{
        if(!isZen(document.frm_shopContact.surname_furigana.value, false, false) || !isZen(document.frm_shopContact.prename_furigana.value, false, false)) {
            err_info += "・ふりがなは全角で入力してください。<br>";
        }
    }
    if(document.frm_shopContact.sex[0].checked == false && document.frm_shopContact.sex[1].checked == false ) {
        err_info += "・性別が選択されていません。<br>";
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.sex;
    }
    if(document.frm_shopContact.job.value == "") {
        err_info += "・ご職業が選択されていません。<br>";
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.job;
    }
    if(document.frm_shopContact.pref.value == "") {
        err_info += "・お住まいが選択されていません。<br>";
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.pref;
    }
    if(document.frm_shopContact.tel.value !== "") {
        if(!valid_tel(document.frm_shopContact.tel.value)) {
            err_info += "・正しい電話番号を入力してください。 <br>";
            if(firstErrElement == null) firstErrElement = document.frm_shopContact.tel;
        }
    }
    if(document.frm_shopContact.mail.value == "") {
        err_info += "・メールアドレスが入力されていません。<br>";
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.mail;
    }else{
        if(!mailIsRight(document.frm_shopContact.mail.value)){
            err_info += "・メールアドレスの形式が違います。<br>";
            if(firstErrElement == null) firstErrElement = document.frm_shopContact.mail;
        }
    }
/*
    if(document.frm_shopContact.contacttext.value == "") {
        err_info += "・ご意見・ご質問が入力されていません。<br>";
        if(firstErrElement == null) firstErrElement = document.frm_shopContact.contacttext;
    }else{
        if(!isZen(document.frm_shopContact.contacttext.value)){
            err_info += "・ご意見・ご質問は全角で入力してください。<br>";
            if(firstErrElement == null) firstErrElement = document.frm_shopContact.contacttext;
        }
    }
*/
    if(err_info != "") {

        var sp_message = document.getElementById("message");
        sp_message.innerHTML = err_info;
        if(firstErrElement != null){
            firstErrElement.focus();
            firstErrElement = null;
        }
        return false;
    }

    return true;
}
function to_send(){
    document.frm_shopContact.action="/php/salon/contact/shopContact.php?mode=new";
    document.frm_shopContact.submit();
}
function to_back(){
    document.frm_shopContact.action="/php/salon/contact/shopContact.php?mode=back";
    document.frm_shopContact.submit();
}
function to_confirm(){
//alert("a");
    if(checkshopContact()){
        document.frm_shopContact.action='/php/salon/contact/shopContact_confirm.php';
        document.frm_shopContact.submit();
    }
}
function to_reset(){
    document.frm_shopContact.reset();
}

