//**************************************************
//文字列のバイト数をチェックする関数
function getByte(text,field_name,limit){

	count = 0;
	for (i=0; i<text.length; i++)
	{
		n = escape(text.charAt(i));
		if (n.length < 4) count++; else count+=2;
	}

	if(count>limit){
		alert("入力された "+field_name+" が長すぎます。\n恐れ入りますが指定された文字数内で再度ご入力ください。");
		return false;
	}else{
		return count;
	}

}




//**************************************************
//使用不可文字を取り除く関数
function removeSpecialChars(object,data){

	var chars=new Array("<",">","\"","&",",","\\");
	for(count=0;count<chars.length;count++){
		var data_array=data.split(chars[count]);
		for(i=0;i<data_array.length;i++){
			if(i==0){
				data = data_array[i];
			}else{
				data += data_array[i];
			}
		}
	}

	//フォームに入力
	object.value=data;

}




//**************************************************
//使用不可文字を可能な文字に変換する関数
function convSpecialChars(object,data){

	var chars=new Array("<=＜",">=＞","\"=”","&=＆",",=，","\\=￥");
	for(count=0;count<chars.length;count++){
		var chars2=chars[count].split("=");

		var data_array=data.split(chars2[0]);
		for(i=0;i<data_array.length;i++){
			if(i==0){
				data = data_array[i];
			}else{
				data += chars2[1]+data_array[i];
			}
		}
	}

	//フォームに入力
	object.value=data;

}




//**************************************************
//入力されたデータから余分なスペースを取り除く関数
function removeSpaces(object,data){

	var spaces=new Array(" ","　");
	for(count=0;count<spaces.length;count++){
		var data_array=data.split(spaces[count]);
		for(i=0;i<data_array.length;i++){
			if(i==0){
				data = data_array[i];
			}else{
				data += data_array[i];
			}
		}
	}

	//フォームに入力
	object.value=data;

}




//**************************************************
//注文フォームの入力データをチェックする関数
function checkData(path){

	//入力された値とオブジェクト名
	var email=document.order_form.email.value;	//メールアドレス
		var email_o=document.order_form.email;
	var email_confirm=document.order_form.email_confirm.value;	//メールアドレス確認入力
		var email_confirm_o=document.order_form.email_confirm;
	var goods=document.order_form.goods.value;	//お申し込み商品
		var goods_o=document.order_form.goods;
	var tel1=document.order_form.tel1.value;	//電話番号 市外局番
		var tel1_o=document.order_form.tel1;
	var tel2=document.order_form.tel2.value;	//電話番号 市内局番
		var tel2_o=document.order_form.tel2;
	var tel3=document.order_form.tel3.value;	//電話番号 番号
		var tel3_o=document.order_form.tel3;
	var name_kana1=document.order_form.name_kana1.value;	//フリガナ 姓
		var name_kana1_o=document.order_form.name_kana1;
	var name_kana2=document.order_form.name_kana2.value;	//フリガナ 名
		var name_kana2_o=document.order_form.name_kana2;
	var name1=document.order_form.name1.value;	//お名前 姓
		var name1_o=document.order_form.name1;
	var name2=document.order_form.name2.value;	//お名前 名
		var name2_o=document.order_form.name2;
	var pcode1=document.order_form.pcode1.value;	//郵便番号 前半
		var pcode1_o=document.order_form.pcode1;
	var pcode2=document.order_form.pcode2.value;	//郵便番号 前半後半
		var pcode2_o=document.order_form.pcode2;
	var pref=document.order_form.pref.value;	//都道府県
		var pref_o=document.order_form.pref;
	var address=document.order_form.address.value;	//市区町村以下
		var address_o=document.order_form.address;
	var byear=document.order_form.byear[document.order_form.byear.selectedIndex].value;	//生年月日-年
		var byear_o=document.order_form.byear[document.order_form.byear.selectedIndex];
	var bmonth=document.order_form.bmonth[document.order_form.bmonth.selectedIndex].value;	//生年月日-月
		var bmonth_o=document.order_form.bmonth[document.order_form.bmonth.selectedIndex];
	var bdate=document.order_form.bdate[document.order_form.bdate.selectedIndex].value;	//生年月日-日
		var bdate_o=document.order_form.bdate[document.order_form.bdate.selectedIndex];
	var now_year=document.order_form.now_year.value;	//本日の日付-年
		var now_year_o=document.order_form.now_year;
	var now_month=document.order_form.now_month.value;	//本日の日付-月
		var now_month_o=document.order_form.now_month;
	var now_date=document.order_form.now_date.value;	//本日の日付-日
		var now_date_o=document.order_form.now_date;
	var mtv_other=document.order_form.mtv_other.value;	//購入動機 その他
		var mtv_other_o=document.order_form.mtv_other;

	if(document.order_form.sex[0].checked==true){	//性別
		var sex=document.order_form.sex[0].value;
	}else if(document.order_form.sex[1].checked==true){
		var sex=document.order_form.sex[1].value;
	}else{
		sex="";
	}




////////////////////////////////////////////////
	//入力データから余分なスペースを取り除く
	removeSpaces(email_o,email);	//メールアドレス
		email=email_o.value;
	removeSpaces(email_confirm_o,email_confirm);	//メールアドレス確認入力
		email_confirm=email_confirm_o.value;
	removeSpaces(name_kana1_o,name_kana1);	//フリガナ 姓
		name_kana1=name_kana1_o.value;
	removeSpaces(name_kana2_o,name_kana2);	//フリガナ 名
		name_kana2=name_kana2_o.value;
	removeSpaces(name1_o,name1);	//お名前 姓
		name1=name1_o.value;
	removeSpaces(name2_o,name2);	//お名前 名
		name2=name2_o.value;



////////////////////////////////////////////////
	//使用不可文字を取り除く
	removeSpecialChars(email_o,email);	//メールアドレス
		email=email_o.value;
	removeSpecialChars(email_confirm_o,email_confirm);	//メールアドレス確認入力
		email_confirm=email_confirm_o.value;
	removeSpecialChars(name_kana1_o,name_kana1);	//フリガナ 姓
		name_kana1=name_kana1_o.value;
	removeSpecialChars(name_kana2_o,name_kana2);	//フリガナ 名
		name_kana2=name_kana2_o.value;



////////////////////////////////////////////////
	//使用不可文字を可能な文字に変換
	convSpecialChars(name1_o,name1);	//お名前 姓
		name1=name1_o.value;
	convSpecialChars(name2_o,name2);	//お名前 姓
		name2=name2_o.value;
	convSpecialChars(address_o,address);	//市区町村以下
		address=address_o.value;
	convSpecialChars(mtv_other_o,mtv_other);	//購入動機 その他
		mtv_other=mtv_other_o.value;



////////////////////////////////////////////////
	//入力必須項目のチェック
	var compulsory_items=new Array(email,email_confirm,goods,tel1,tel2,tel3,name_kana1,name_kana2,name1,name2,pcode1,pcode2,pref,address,byear,bmonth,bdate,sex);	//入力必須項目

	for(i=0;i<compulsory_items.length;i++){
		if(compulsory_items[i]==""){
			alert("* の項目は必ず入力してください。");
			return false;
		}
	}



////////////////////////////////////////////////
	//バイト数をチェック
	if(!getByte(email,"メールアドレス",50)){return false;}	//メールアドレス
	if(!getByte(name_kana1 + name_kana2,"フリガナ",24)){return false;}	//フリガナ
	if(!getByte(name1 + name2,"お名前",20)){return false;}	//お名前
	if(!getByte(address,"住所",56)){return false;}	//市区町村以下
	if(mtv_other){
		if(!getByte(mtv_other,"購入動機",60)){return false;}	//購入動機その他
	}



////////////////////////////////////////////////
	//メールアドレス2つが一致しているかどうかのチェック
	if(email!==email_confirm){
		alert("メールアドレスが正しく入力されていません。");
		return false;
	}



////////////////////////////////////////////////
	//メールアドレス形式のチェック
	var email_ok="True";

	var email_array=email.split("@");

	if(email_array.length==2){	//"@"が1つだけあるか
		if(email_array[0]=="" || email_array[1]==""){
			email_ok="False";
		}else{
			var email_bottom_array=email_array[1].split(".");
			if(email_bottom_array.length>1){	//"@"より後ろに"."が1つ以上あるか
				for(i=0;i<=email_bottom_array.length;i++){
					if(email_bottom_array[i]==""){	//その"."は先頭・末尾ではないか、また、連続していないか
						email_ok="False";
					}
				}
			}else{
				email_ok="False";
			}
		}
	}else{
		email_ok="False";
	}


	//半角で入力されているか
	count = 0;	//バイト数を数える
	for (i=0; i<email.length; i++)
	{
		n = escape(email.charAt(i));
		if (n.length < 4) count++; else count+=2;
	}

	if(count!==email.length){
		email_ok="False";
	}


	if(email_ok=="False"){
		alert("メールアドレスが不正です。");
		return false;
	}



////////////////////////////////////////////////
	//半角数字チェック
	var check_items=new Array(tel1,tel2,tel3,pcode1,pcode2);	//チェックする項目をセット
	var numbers= "0123456789";

	for(count=0;count<check_items.length;count++){
		var hankaku=true;
		for(i1=0;i1<check_items[count].length;i1++){
			for(i2=0;i2<numbers.length;i2++){
				if(check_items[count].charAt(i1)!==numbers.charAt(i2)){
					hankaku=false;
				}else{
					hankaku=true;
					break;
				}
			}
			if(hankaku==false){
				alert("電話番号、郵便番号は、半角数字でご入力ください。");
				return false;
			}
		}
	}



////////////////////////////////////////////////
	//半角カタカナチェック
	var check_items=new Array(email,name_kana1,name_kana2,name1,name2,address,mtv_other);	//チェックする項目をセット
	var small_kanas= "ｱｲｳｴｵｶｷｸｹｺｻｼｽｾｿﾀﾁﾂﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖﾗﾘﾙﾚﾛﾜｦﾝｧｨｩｪｫｬｭｮｯｰ､｡｢｣ﾞﾟ";

	for(count=0;count<check_items.length;count++){
		for(i1=0;i1<check_items[count].length;i1++){
			for(i2=0;i2<small_kanas.length;i2++){
				if(check_items[count].charAt(i1)==small_kanas.charAt(i2)){
					alert("半角カタカナは使用できません");
					return false;
				}
			}
		}
	}



////////////////////////////////////////////////
	//名前のフリガナチェック
	var check_items=new Array(name_kana1,name_kana2);	//チェックする項目をセット
	var big_kanas= "アイウヴエオカキクケコガギグゲゴサシスセソザジズゼゾタチツテトダヂヅデドナニヌネノハヒフヘホバビブベボパピプペポマミムメモヤユヨラリルレロワヲンァィゥェォャュョヮッヰヱ";

	for(count=0;count<check_items.length;count++){
		var big_kana=true;
		for(i1=0;i1<check_items[count].length;i1++){
			for(i2=0;i2<big_kanas.length;i2++){
				if(check_items[count].charAt(i1)!==big_kanas.charAt(i2)){
					big_kana=false;
				}else{
					big_kana=true;
					break;
				}
			}
			if(big_kana==false){
				alert("名前のフリガナは全角カタカナで入力してください。");
				return false;
			}
		}
	}



////////////////////////////////////////////////
	//住所を隠しフィールドにセット
	count = 0;
	for (i=0; i<address.length; i++)
	{
		n = escape(address.charAt(i));
		if (n.length < 4) count++; else count+=2;
		if(count>=29){
			document.order_form.address1.value=address.substr(0,i);
			document.order_form.address2.value=address.substr(i,address.length-i);
			break;
		}else{
			document.order_form.address1.value=address;
		}
	}



////////////////////////////////////////////////
	//電話番号を隠しフィールドにセット
	document.order_form.tel.value=tel1+"-"+tel2+"-"+tel3;

////////////////////////////////////////////////
	//フリガナを隠しフィールドにセット
	document.order_form.name_kana.value=name_kana1+" "+name_kana2;

////////////////////////////////////////////////
	//お名前を隠しフィールドにセット
	document.order_form.name.value=name1+" "+name2;

////////////////////////////////////////////////
	//郵便番号を隠しフィールドにセット
	document.order_form.pcode.value=pcode1+"-"+pcode2;



////////////////////////////////////////////////
	//年齢チェック
	var kids=false;
	var byear_ad_ar=byear.split("（");
	var byear_ad=byear_ad_ar[0];
	var byear_g_ar=byear_ad_ar[1].split("）");
	var byear_g=byear_g_ar[0];

	var age;
	age=now_year - byear_ad;
	if((now_month - bmonth)<0){
		age=age-1;
	}else if((now_month - bmonth)==0){
		if((now_date - bdate)<0){
			age=age-1;
		}
	}

	if(age<20){
		alert("20歳未満の方は、保護者の方がお電話にてお申し込みください。");
		return false;
	}

	//生年月日各種データを隠しフィールドにセット
	document.order_form.byear_ad.value=byear_ad;
	document.order_form.byear_g.value=byear_g;
	document.order_form.age.value=age;
	document.order_form.birthday.value=byear_ad+"/"+bmonth+"/"+bdate;



////////////////////////////////////////////////
	//アンケート回答を隠しフィールドにセット
	var motivations="";
	for(i1=0;i1<document.order_form.length;i1++){
		var this_object=document.order_form.elements[i1];
		if(this_object.name.substr(0,3)=="mtv" && this_object.name!="mtv_other"){
			if(this_object.checked==true){
				if(motivations==""){
					motivations=this_object.value;
				}else{
					motivations+="／"+this_object.value;
				}
			}
		}
	}

	var mtv_o=document.order_form.mtv_other;
	if(mtv_o.value!=""){
		if(motivations==""){
			motivations=mtv_o.value;
		}else{
			motivations+="／"+mtv_o.value;
		}
	}

	document.order_form.motivations.value=motivations;



////////////////////////////////////////////////
	var path=path.split("#");	//アンカー前後に分割
	var url=path[0] +"?"+ document.info.keys.value + "#" + path[1];	//リンク先URL生成
	document.order_form.action=url;
	document.order_form.submit();


}




//**************************************************
//ジャンプ_標準
function jumpNormal(path){
		var path=path.split("#");	//アンカー前後に分割
		var url=path[0] +"?"+ document.info.keys.value + "#" + path[1];	//リンク先URL生成
	location.href=url;	//ジャンプ
}



//**************************************************
//ジャンプ_新規ウィンドウ
function jumpNewWin(path,win_name,width,height,toolbar,location,directories,status,menubar,scrollbars,resizable){
	var path=path.split("#");	//アンカー前後に分割
	var url=path[0] +"?"+ document.info.keys.value + "#" + path[1];	//リンク先URL生成
	var option="width=" + width + ",height=" + height + ",toolbar=" + toolbar + ",location=" + location + ",directories=" + directories + ",status=" + status + ",menubar=" + menubar + ",scrollbars=" + scrollbars + ",resizable=" + resizable;	//オプション生成
	window.open(url,win_name,option);	//ジャンプ
}



//**************************************************
//アクセスログ取得
function getAcslog(){
	var url="/cgi-esp/getacslog.cgi?"+ document.info.keys.value;	//URL生成
	document.write("<img src=\""+url+"\" width=\"0\" height=\"0\" style=\"margin:0px;\">");
}

