function changeHttpTop(contextroot){
 	strUrl = "http://" + (location.hostname).replace('www.','store.') + contextroot;
 	location = strUrl;
}
 
document.onkeydown = function(e){
	var e = (e) ? e : ((event) ? event : null);
	var source = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
	if(e.keyCode==13){
		
		 if(source.name=="syohinnNm"){
		 	return fun_Productcheck();
		 }else if(source.tagName!="TEXTAREA")
    		{return fun_Productcheck();}
		 else if(source.readOnly==true)
		 	{return false;}
		 else if(source.disabled==true)
		 	{return false;}
	}
}

function fun_Productcheck(){
	var contextPath = $("#contextPath-data").val();
	var subShopIdDisp = $("#subShopId").val();
	var syohinnNm=$("[name=searchWord]").get(0).value;
	var syohinnNmMinLen=document.forms["cmHeaderForm"].elements["prodMinLen"].value;
	var syohinnNmMaxLen=document.forms["cmHeaderForm"].elements["prodMaxLen"].value;

	syohinnNm = syohinnNm.replace("\?","");
	syohinnNm = syohinnNm.replace("\？","");

	if(!hasValue(syohinnNm)){
		$("[name=searchWord]").get(0).value="";
		setMessage("MSG_G0034","検索語句");
	}
//	if(compareByteLength(syohinnNm, syohinnNmMaxLen) > 0
//	|| compareByteLength(syohinnNm, syohinnNmMinLen) < 0){
//		var msg = "商品名," + syohinnNmMinLen + "," + syohinnNmMaxLen;
//		setMessage("MSG_G0040",msg);
//	}
	if(syohinnNm == "検索語句を入力"){
		setMessage("MSG_G0034","検索語句");
	}

	if(!showMessage()){
		return false;
	} else {
		var subShopId;
		var topPageFlg = document.forms["cmHeaderForm"].elements["topPageFlg"].value;

		var categoryCd = document.forms["cmHeaderForm"].elements["categoryCd"].value;
		if (categoryCd == "") {
			categoryCd = " ";

			// TOPページから呼ばれた場合
			if (topPageFlg == "1") {
				subShopId = "1";
			} else {
				subShopId = document.forms["cmHeaderForm"].elements["subShopId"].value;
			}
		} else {
			subShopId = categoryCd.substring(1,2);
		}

		var searchWord = document.forms["cmHeaderForm"].elements["searchWord"].value
		searchWord = searchWord.replace(/\?/g,"");
		searchWord = searchWord.replace(/\？/g,"");
		if(searchWord == "") searchWord = " ";

		var url = contextPath + "/cmHeaderSearchProduct/doSearchProduct";
		url += "/" + document.forms["cmHeaderForm"].elements["screenId"].value;
		url += "/" + " ";
		url += "/" + categoryCd;
		url += "/" + subShopIdDisp;
		url += "?wd=" + convSignHeader(searchWord);
		//HTTPSの場合はHTTPへ変更し遷移
		changeHttpTop(url);
	}
	return false;
}

function commHeadClick(method) {
	var contextPath = $("#contextPath-data").val();
	var contentScreenId = document.forms["cmHeaderForm"].elements["contentScreenId"].value;

	if(method == "doLogin"  &&
	 (contentScreenId == "shEditOrdererInfo" || contentScreenId == "shDispGSAddresseeSelect"
	 || contentScreenId == "shDispGFAddresseeSelect" || contentScreenId == "shDispSPAddresseeSelect"
	 || contentScreenId == "shEditPurchContent" || contentScreenId == "shEditAddresseeChange"
	 || contentScreenId == "shEditClientChange" || contentScreenId == "shEditProductNumber"
	 || contentScreenId == "shEditDeliverySet" || contentScreenId == "shEditFestivalName"
	 || contentScreenId == "shEditWrappingChange" || contentScreenId == "shEditMemoInput"
	 || contentScreenId == "shConfirmSettlementMeth" || contentScreenId == "shDispPurchConfirm") ) {

		if (confirm("それまで入力した内容が破棄され買い物カゴ画面に戻してよろしいですか？")){
			// OKだったら、入力した内容が破棄され買い物カゴ画面に戻る。
			var url = contextPath + "/cmHeaderHeadClick/" + method;
			url += "/" + document.forms["cmHeaderForm"].elements["screenId"].value;
			url += "/" + document.forms["cmHeaderForm"].elements["subShopId"].value;
			url += "/" + document.forms["cmHeaderForm"].elements["contentScreenId"].value;
			location.href ="http://store.ishibashi.co.jp" + encodeURI(url);
			return true;
		}
	}else {
		var elements = $("[name=screenId]");
		for(i=0;i<elements.length;i++){
			if(elements.get(i).value == "srDispProductDetail"){
				changeDefImg();
				break;
			}
		}

		var url = contextPath + "/cmHeaderHeadClick/" + method;
		url += "/" + document.forms["cmHeaderForm"].elements["screenId"].value;
		url += "/" + document.forms["cmHeaderForm"].elements["subShopId"].value;
		if(method == "doLogin" || method == "doEditShoppingBasket" ){
			url += "/" + document.forms["cmHeaderForm"].elements["contentScreenId"].value;
		} else if(method == "doMyPage" ){
			document.forms["cmHeaderForm"].elements["contentScreenId"].value = "myPageMenu";
			url += "/" + document.forms["cmHeaderForm"].elements["contentScreenId"].value;
		} else if(method == "doKoJin" ){
			document.forms["cmHeaderForm"].elements["contentScreenId"].value = "cmKojinAgree";
			url += "/" + document.forms["cmHeaderForm"].elements["contentScreenId"].value;
		}else{
			url += "/" + "";
	 	}
		location.href = "http://store.ishibashi.co.jp" + encodeURI(url);
	}
	return false;
}

function goCmDispRanking(shopId, categoryCd) {
	var contextPath = $("#contextPath-data").val();
	var url = contextPath + "/cmDispTopRanking.html?";
	document.location.href = url + "shopId=" + shopId
								 + "&categoryCd=" + categoryCd
}

function doProductListLink(){
	var contextPath = $("#contextPath-data").val();
	var url = contextPath + "/srDispCategoryTreeLink/doSearchCategory/11100000000/1/2/1";
	document.location.href ="http://store.ishibashi.co.jp" + encodeURI(url);
	return false;
}

function convSignHeader(tempStr){
	var contextPath = $("#contextPath-data").val();
	var rtnStr = "";
	rtnStr = encodeURIComponent(tempStr).replace(/\./g,"%2E");
	return rtnStr;
}

function doInquiry() {
	var contextPath = $("#contextPath-data").val();
	var url = createHttps();
	url += contextPath + "/inquiry/cmTopPage";
	document.location.href = "http://store.ishibashi.co.jp" + encodeURI(url);
	return false;
}

function doBegginerLink(){
	var contextPath = $("#contextPath-data").val();
	var url = "guide/beginner/index1.html";
	backToTopForHttp(contextPath,url);
	return false;
}

function doHowToShopping(){
	var contextPath = $("#contextPath-data").val();
	var url = "guide/order/index5.html";
	backToTopForHttp(contextPath,url);
	return false;
}

function doGuide(){
	var contextPath = $("#contextPath-data").val();
	var url = "guide/index.html";
	backToTopForHttp(contextPath,url);
	return false;
}

function cmHeaderClickInquiry(subShopId) {
	var contextPath = $("#contextPath-data").val();
	var url = contextPath + "/cmHeaderChangePage/doChangeSubShop";
	url += "/" + subShopId;
	document.location.href = "http://store.ishibashi.co.jp" + encodeURI(url);
	return false;
}

function doDetailSearch(){
	var contextPath = $("#contextPath-data").val();
	var url = contextPath + "/cmHeaderSearchProduct/doSearchProductDetail";
	
	document.location.href ="http://store.ishibashi.co.jp" + encodeURI(url);
	return false;
}

//サブショップ制御
function doShopChange(category1){
	var shopCategory = 0;

	if($("[name=scCode_" + category1 + "]").get(0) != null){
		shopCategory = $("[name=scCode_" + category1 + "]").get(0).value.charAt(1);
	}
	$("#subShopId").val(shopCategory);
}

$(document).ready(function() {
	/*
	 * スマートフォン表示切替リンク
	 *
	 *
	 */

	var mode = $("#header-head .smtDisp");
	mode.hide();

	var contentScreenId = $("#contentScreenId");
	var smtUa = $("#smtUa");
	//alert(contentScreenId.val());
	if (smtUa.length > 0 && smtUa.val() == "smtua" && contentScreenId.length > 0
		&& (contentScreenId.val() == "srDispProductSearch"
			|| contentScreenId.val() == "srDispProductList"
			|| contentScreenId.val() == "srDispProductSearchList"
			|| contentScreenId.val() == "srDispProductDetail"
			|| contentScreenId.val() == "cmTopPage"
			|| contentScreenId.val().substring(0, 13) == "cmShopTopPage")) {
		mode.show();
		}
});

/*
 * スマートフォン表示切替 グローバル関数
 */
function switch_smt() {
	var url = location.href;
	$.post(
		url, 		// リクエストURL
		{"smt" : "1"}, 		// データ
		function() {	// 通信成功時にリロードしてスマートフォンデザイン表示
			window.location = url;
		},
		"html" 				// 応答データ形式
	);
}

$(document).ready(function(){
	$('div.globalNavigaiton ul li div.sub').css('display','none');
	$('div.globalNavigaiton ul li').hover(function(){
	$(this).children('div.sub').show(300);
	},function(){
	$(this).children('div.sub').hide(300);
	});

});

