/*------------------------------------------
MARY QUANT ONLINE SHOP
AD-traKコード送出用JavaScriptファイル
------------------------------------------ */

//変数の定義
var i;
var ii;
var iii;
var id_str = null;
var rest_str = null;
var path_str = null;
var query_str = null;
var query_array = new Array();
var hash_str = null;
var tmpPath_str = null;
var tmpQuery_str = null;
var tmpQuery_array = new Array();
var tmpHash_str = null;
var tmpMatchIndex = 0;
var tmpMatchMaxPoint = 0;
var match_array = new Array();
var restPath_str = null;
var toBeExcepted;

//AD-traKコード送出ファンクション
function sendAdtrakCode(){
	//パス+クエリストリング文字列を取得
	path_str = location.pathname;
	query_str = location.search.split("?")[1];
	if(query_str){
		query_array = query_str.split("&");
	}
	hash_str = location.hash.split("#")[1];
	//Topページのみの処理
	if((location.pathname == "/")||(location.pathname == "/index.php")){
		id_str = "Top";
	//Top以外のページの処理
	}else{
		//ページID対照表から一致するIDを検索
		for(i=0;i<ref_array.length;i++){
			//ref_arrayの値を取得
			tmpPath_str = ref_array[i][0].split("?")[0];
			tmpQuery_str = ref_array[i][0].split("?")[1];
			if(tmpQuery_str){
				if(tmpQuery_str.indexOf("#")>-1){
					tmpTmpQuery_str = tmpQuery_str.split("#")[0];
					tmpQuery_str = tmpTmpQuery_str;
				}
				tmpQuery_array = tmpQuery_str.split("&");
			}
			tmpHash_str = ref_array[i][0].split("#")[1];
			//パス部分の文字列が部分一致する場合には処理を続行
			if(path_str.indexOf(tmpPath_str)>-1){
				//ポイントを1点に
				match_array[i] = 1;
				//URLとref_arrayの双方にクエリストリングがある場合に下記の処理を実行
				if((query_array.length > 0)&&(tmpQuery_array.length > 0)){
					for(ii=0;ii<tmpQuery_array.length;ii++){
						for(iii=0;iii<query_array.length;iii++){
							//URLとref_arrayのクエリストリングが一致したならば得点を加点
							if((query_array[iii].indexOf("categoryStep")==-1)&&(tmpQuery_array[ii] == query_array[iii])){
								match_array[i] ++;
								query_array.splice(iii,1);
								break;
							}
						}
					}
				}
				//新記録が出たら最高得点を更新
				if(match_array[i] > tmpMatchMaxPoint){
					tmpMatchMaxPoint = match_array[i];
				}
				//現在の得点が最高得点ならば暫定首位に設定
				if(match_array[i] == tmpMatchMaxPoint){
					tmpMatchIndex = i;
				}
			}else{
				//ポイントを0点に
				match_array[i] = 0;
			}
		}
		//ページID対照表に少しでも一致するものがあったら下記の処理
		if(tmpMatchMaxPoint > 0){
			//id_strに設定
			id_str = ref_array[tmpMatchIndex][1];
			/*
			//対照表と異なるパスがついている場合にはそれを追記
			tmpPath_str = ref_array[tmpMatchIndex][0].split("?")[0];
			if(path_str != tmpPath_str){
				restPath_str = path_str.split(tmpPath_str)[1].split("/").join("--").split(".").join("__");
				id_str += "___" + restPath_str;
			}
			//対照表と異なる変数がついている場合にはそれを追記
			if(query_array.length > 0){
				tmpQuery_array = new Array();
				//変数名が除外リストになく、変数の値が指定されているもののみピックアップ
				for(i=0;i<query_array.length;i++){
					toBeExcepted = false;
					for(ii=0;ii<exception_array.length;ii++){
						if(query_array[i].split("=")[0] == exception_array[ii]){
							toBeExcepted = true;
							break;
						}
					}
					if(toBeExcepted == false){
						if(query_array[i].split("=")[1] != ""){
							tmpQuery_array.push(query_array[i]);
						}
					}
				}
				if(tmpQuery_array.length > 0){
					id_str += "---" + tmpQuery_array.join("--").split("=").join("__");
				}
			}
			*/
			//末尾にゴミがついている場合にはそれを削除
			if(id_str.substr((id_str.length-3),3)=="---"){
				id_str = id_str.substring(0,(id_str.length-3));
			}else if(id_str.substr((id_str.length-2),2)=="--"){
				id_str = id_str.substring(0,(id_str.length-2));
			}else if(id_str.substr((id_str.length-1),1)=="-"){
				id_str = id_str.substring(0,(id_str.length-1));
			}
		//ページID対照表に全く一致するものが無かったら下記の処理
		}else{
			path_str = location.pathname.split("/").join("--").split(".").join("__");
			id_str = "___"+path_str.substr(2);
			if(query_str){
				query_array = query_str.split("&");
			}
			//変数がついている場合にはそれを追記
			if(query_array.length > 0){
				tmpQuery_array = new Array();
				//変数名が除外リストになく、変数の値が指定されているもののみピックアップ
				for(i=0;i<query_array.length;i++){
					toBeExcepted = false;
					for(ii=0;ii<exception_array.length;ii++){
						if(query_array[i].split("=")[0] == exception_array[ii]){
							toBeExcepted = true;
							break;
						}
					}
					if(toBeExcepted == false){
						if(query_array[i].split("=")[1] != ""){
							tmpQuery_array.push(query_array[i]);
						}
					}
				}
				if(tmpQuery_array.length > 0){
					id_str += "---" + tmpQuery_array.join("--").split("=").join("__");
				}
			}
			//末尾にゴミがついている場合にはそれを削除
			if(id_str.substr((id_str.length-3),3)=="---"){
				id_str = id_str.substring(0,(id_str.length-3));
			}else if(id_str.substr((id_str.length-2),2)=="--"){
				id_str = id_str.substring(0,(id_str.length-2));
			}else if(id_str.substr((id_str.length-1),1)=="-"){
				id_str = id_str.substring(0,(id_str.length-1));
			}
		}
	}
	
	//デバッグ
	if((query_str)&&(query_str.indexOf("debug=true")>-1)){
		alert([id_str]);
		return false;
	}
	
	//AD-traKコードの送出（ショッピングの終了ページを除く）
	if(path_str.indexOf("shopping_end.php") == -1){
		if ( location.protocol == 'http:' ){ 
			strServerName = 'http://ac.ad-trak.jp';
		} else { 
			strServerName = 'https://secure.ad-trak.jp';
		}
		cid = '97HCXMzR';
		pid = id_str;
		m1id='';
		a1id='';
		o1id='';
		o2id='';
		o3id='';
		o4id='';
		o5id='';
		document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + strServerName + "/ad-trak_tag.php?cid=" + cid + "&pid=" + pid + "&m1id=" + m1id +"&a1id=" + a1id + "&o1id=" + o1id + "&o2id=" + o2id + "&o3id=" + o3id + "&o4id=" + o4id + "&o5id=" + o5id + "\"></scr" + "ipt>");
	}
}

//コード送出ファンクションの実行
sendAdtrakCode();


