﻿
function getSelectedRadioFormValue(radioSelectID){
	var selectedFormValue = null;
	jQuery(document).ready(function($) {
		selectedFormValue =$("input[@name="+radioSelectID+"]:checked").val();
	});	
	return selectedFormValue;
}

function locationhref(link_url){
	document.location.href = link_url;
}

function setNewValue(targetID, newValue){
	if(document.getElementById(targetID) != null){
		document.getElementById(targetID).value = newValue;
	}
}

function loadSwf(swfPath, width, height, divID) {
	src = '';
	src += '<object codebase="'+document.location.protocol+'//fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle" height="'+height+'" width="'+width+'">';
	src += '  <param name="allowScriptAccess" value="sameDomain" />';
	src += '  <param name="movie" value="'+swfPath+'" />';
	src += '  <param name="WMODE" value="Transparent" />';
	src += '  <param name="quality" value="High" />';
	src += '  <embed src="'+swfPath+'" wmode="Transparent" quality="High" bgcolor="#ffffff" name="top" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="'+document.location.protocol+'//www.macromedia.com/go/getflashplayer" align="middle" height="'+height+'" width="'+width+'">';
	src += '</object>';
	//set html
	document.getElementById(divID).innerHTML = src;
}

function getSwfHtml(swfPath, width, height, inputWmode) {
	src = '';
	var wmodeParameter = '';
	//
	src += '<object width="'+width+'" height="'+height+'">';
	src += '<param name="movie" value="'+swfPath+'" />';
	src += '<param name="allowFullScreen" value="true"></param>';
	if(inputWmode != null){
		src += '<param name="WMODE" value="Transparent" />';
		wmodeParameter = 'wmode="'+inputWmode+'"';
	}
	src += '<embed src="'+swfPath+'" type="application/x-shockwave-flash" '+wmodeParameter+' allowfullscreen="true" width="'+width+'" height="'+height+'"></embed>';
	src += '</object>';
	return src;
}

function getBrowserWidth ( ) {  
	if ( window.innerWidth ) { 
		return window.innerWidth; 
	}  
	else if ( document.documentElement && document.documentElement.clientWidth != 0 ) { 
		return document.documentElement.clientWidth; 
	}  
	else if ( document.body ) { 
		return document.body.clientWidth; 
	}  
	return 0;  
}  

function chgDisplayType(openDivID, closeDivID) {
	if(closeDivID != null){
		if(document.getElementById(closeDivID) != null){
			document.getElementById(closeDivID).style.display = 'none';
		}
	}
	if(openDivID != null){
		if(document.getElementById(openDivID) != null){
			document.getElementById(openDivID).style.display = 'block';
		}
	}
}

function createXMLHttpRequest() {
	//XMLHttpRequestのインスタンスを作って返す
	var xmlhttp = null;
	if( window.XMLHttpRequest ) {
		// IE以外の場合
		xmlhttp = new XMLHttpRequest();
	}
	else if( window.ActiveXObject ) {
		// IE用
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		if(!xmlhttp){
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		}		
	}
	return xmlhttp;
}

function chkTextIsURL(inputText){
	var chkResult = false;
	if(inputText.search(/((http|https):\/\/[0-9a-z-\/._?=&%\[\]~]+)/i) != -1){
		chkResult = true;
	}
	return chkResult;
}

function getRandomNumber(inputMaxNumber){
	//0からMaxNumberの間からランダムに取得
	return Math.ceil( Math.random() * inputMaxNumber );
}

function getAuthFeedURL(xmlFileName){
	if(xmlFileName != null){
		//SWFなどhostnameが必要な場合があるため現在のホスト名を付加した上でXMLパスをパラメータに追加する
		xmlFileName = window.location.protocol+'//'+window.location.hostname+'/util/api/authfeed?'+encodeURIComponent(xmlFileName);
	}
	return xmlFileName;	
}

function getUrlParameterSegment(baseURL){
	var urlParameterSegment = '?';
	//AuthApi経由のURLの場合(URLエンコーディングを考慮する)
	if(baseURL.indexOf('/util/api/authfeed?') >= 0){
		if(baseURL.indexOf('%3F') >= 0){ //?
			urlParameterSegment = '%26'; //&
		}
		else{
			urlParameterSegment = '%3F'; //?
		}
	}
	//通常のURLの場合
	else if(baseURL.indexOf('?') >= 0){
		urlParameterSegment = '&';
	}
	return urlParameterSegment;
}

function initFormValue(formID, initValue) {
	//フォーム値がディフォルトと一致する場合のみ初期化する
	if(initValue == document.getElementById(formID).value){
		document.getElementById(formID).value = '';
	}
}
function initPwFormValue(formID, divID, width){
	//Type=passwordのフォームを設定 (IEではTypeの変更を行えないためinnerHTMLでフォームごと置換する)
	document.getElementById(divID).innerHTML = '<input name="'+formID+'" type="password" id="'+formID+'" style="width:'+width+'px;" />';
	//置換後のフォームにフォーカスを再現 (IEの場合なぜか2回行う必要がある)
	document.getElementById(divID).childNodes[0].focus();
	document.getElementById(divID).childNodes[0].focus();
}

/**
	日付関連
*/
function getLocalDateFormat(commonDateFlag, calendarFlag) {
	var localDateFormat;
	//ディフォルトを設定
	if(commonDateFlag){
		localDateFormat = 'dd/MM/yyyy';
	}
	else if(calendarFlag){
		localDateFormat = 'd/m/Y';
	}
	//言語別設定
	if(_languageID == 'de'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'en'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'es'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'fr'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'it'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'ja'){
		if(commonDateFlag){
			localDateFormat = 'yyyy/MM/dd';
		}
		else if(calendarFlag){
			localDateFormat = 'Y/m/d';
		}
	}
	else if(_languageID == 'ko'){
		if(commonDateFlag){
			localDateFormat = 'yyyy/MM/dd';
		}
		else if(calendarFlag){
			localDateFormat = 'Y/m/d';
		}
	}
	else if(_languageID == 'nl'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'pt'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'ru'){
		if(commonDateFlag){
			localDateFormat = 'dd/MM/yyyy';
		}
		else if(calendarFlag){
			localDateFormat = 'd/m/Y';
		}
	}
	else if(_languageID == 'zh'){
		if(commonDateFlag){
			localDateFormat = 'yyyy/MM/dd';
		}
		else if(calendarFlag){
			localDateFormat = 'Y/m/d';
		}
	}
	return localDateFormat;
}


/**
	SelectフォームのOptionを入れ替える共通関数
	newSelectOptionData[0][0][0]: 置き換え対象SelectのID
	newSelectOptionData[1][][0]: Option名
	newSelectOptionData[1][][1]: Option値
*/
function chgSelectOptionData(newSelectOptionData){
	if(newSelectOptionData != null){
		//既存Optionを消去
		var sl = document.getElementById(newSelectOptionData[0][0][0]);
		while(sl.lastChild){
			sl.removeChild(sl.lastChild);
		}
		//指定Optionを追加
		if(newSelectOptionData[1] != null){
			for(var i = 0; i < newSelectOptionData[1].length; i++){
				document.getElementById(newSelectOptionData[0][0][0]).options[i] = new Option(newSelectOptionData[1][i][0], newSelectOptionData[1][i][1]);
			}
		}
	}
}

//Http読み込み中断処理
function httpAbort(xmlHttpRequest){
	if(xmlHttpRequest != null){
		xmlHttpRequest.abort();
	}
}

/**
	FCKeditor関連
*/
function getFCKeditorCreateHtml(divID, width, height, value){
	var fck = new FCKeditor(divID);
	fck.Config['CustomConfigurationsPath'] = '/js/conf/fck_conf.js';
	fck.BasePath = '/js/fckeditor/';
	fck.ToolbarSet = 'STANDARD';
	fck.Width = width;
	fck.Height = height;
	//Replace DivTag
	if(value != null){
		fck.Value = value;
	}
	return fck.CreateHtml();
}

function excuteFCKeditorCommand(divID, commandName){
	var fck = FCKeditorAPI.GetInstance(divID) ;
	if(fck != null){
		fck.Commands.GetCommand(commandName).Execute();
	}
}

function insertHtmlToFCKeditor(insertSrc, FckInstanceName){
	var fck = FCKeditorAPI.GetInstance(FckInstanceName) ;
	if(fck != null){
		fck.InsertHtml(insertSrc);
	}
}

function getFCKeditorValue(divID){
	var value = null;
	var fck = FCKeditorAPI.GetInstance(divID) ;
	if(fck != null){
		value = fck.GetXHTML();
	}
	return value;
}

function FCKeditor_OnComplete(editorInstance){
	//変更時呼び出し This fck event is fired when the actual selection in the editor area changes (including cursor position and keystrokes). Note: In IE, this event does not fire on every keystroke, but only on some random keystrokes.
    editorInstance.Events.AttachEvent('OnSelectionChange',callbackOnSelectionChange) ;
}
function callbackOnSelectionChange(editorInstance) {
	//文字数制限確認
    countLimitWithFCKeditor(editorInstance.Name);
}
function countLimitWithFCKeditor(FckInstanceName){
	var result = true; //超過がなければtrueを返す
	//CountLimit欄がある場合
	if(document.getElementById(FckInstanceName+'_count_limit') != null){
		if(getFCKeditorValue(FckInstanceName) != null){
			jQuery(document).ready(function($) {
				//最大文字数を取得(HTMLタグを含む最大文字数。ただしこのタグを一部サニタイジングしてDBに保管するのでDB列幅の8割程度を指定する必要がある)
				var maxlength = $('#'+FckInstanceName+'_count_limit').attr('limit');
				//残りの文字数を算出
				var lengthleft = maxlength - getFCKeditorValue(FckInstanceName).length;
				//残りの文字数を表示
				document.getElementById(FckInstanceName+'_count_limit').innerHTML = lengthleft
				//文字数を超過している場合
				if(lengthleft < 0){
					chgDisplayType(FckInstanceName+'_count_limit_over', null);
					//document.getElementById(FckInstanceName+'_count_limit_over').style.display = 'block';
					result = false;
				}
				else{
					chgDisplayType(null, FckInstanceName+'_count_limit_over');
					//document.getElementById(FckInstanceName+'_count_limit_over').style.display = 'none';
				}
			})
		}
	}
	return result;
}

