﻿// // // Cookie 操作関数
// // // sierra.js より借用。
function setCookie( name, value, expires )
{
	if( document.location.href.indexOf( "intel.com" ) > -1 )
	{
		document.cookie = name + "=" + escape(value) + 
			"; path=/ ; domain=.intel.com " + 
			((expires == null) ? "" : "; expires=" + 
			expires.toGMTString());
	}
	else
	{
		document.cookie = name + "=" + escape(value) + 
		"; path=/ ; " + 
		((expires == null) ? "" : "; expires=" + 
		expires.toGMTString());
	}
}
function getCookie( name )
{
	var myUrl=location.href;
	var cname = name + "=";
	var dc = document.cookie;
	
	if (dc.length > 0) 
	{
		begin = dc.indexOf(cname);
		if (begin != -1) 
		{
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1) end = dc.length;
			return unescape( dc.substring( begin, end ) );
		}
	}
	return null;
}

// // // チェック関数群
var szPhone = escape( "0123456789-()" );
var szPcode = escape( "0123456789-" );
var szAlphaNum = 
	escape( 
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 #-:$%&_=()" 
	);

szKanji = "(漢字)";
szFurigana = "(ふりがな)";

// cookie Index
var cookIpi = "awrIBN";
var cookCnm = "awrCmp";
var cookAph = "awrKn1";
var cookAnm = "awrNm1";
var cookAtl = "awrPh1";
var cookAem = "awrEml";
var cookRnm = "awrNm2";
var cookRtl = "awrPh2";
var cookPcd = "awrPcd";
var cookAdr = "awrAdr";

function TrimStr( szTarget )
{
	while ( 
		( szTarget.length > 0 ) && 
		( isNaN( szTarget.charAt( 0 ) ) == false ) && 
		( parseInt( szTarget.charAt( 0 ) ) > 9 ) )
	{
		szTarget = szTarget.substring( 2, szTarget.length );
	}	

	if ( szTarget.length == 0 ) return "";

	while ( 
		( szTarget.length > 0 ) && 
		( isNaN( szTarget.charAt( szTarget.length - 1 ) ) == false ) &&
		( parseInt( szTarget.charAt( szTarget.length - 1 ) ) > 9)
	)
	{
		nLength = szTarget.length - 1;
		szTarget = szTarget.substring( 0, szTarget.length - 1 );
	}

	return szTarget;
}

function IsValue( szTemp, sCheck )
{
	var cCheckChar = "";
	
	if ( sCheck.length == 0 ) return false;
	
	for ( i = 0; i < sCheck.length; i++ )
	{
		cCheckChar = escape( sCheck.charAt(i).toUpperCase() );
		if ( szTemp.indexOf( cCheckChar, 0 ) == -1 ) 
			return false;
	}
	return true;
}

var szErrMsg = "";

function _CheckEntry( oTarget, bCheck, szErr )
{
	if (!bCheck) return true;
	if ( oTarget != "" )
		oTarget.focus();
	szErrMsg = szErr + "\n\n" + szErrMsg;
	return false;
}

var retNUL = "A";
var retNotEnough = "B";
var retNotValid = "C";
var retOK = "D";

function _CheckDate( dateY, dateM, dateD)
{
	var buyDateNotEnough = "OK";
	var buyDateNotValid = "OK";
	var now = new Date();
	// getYear()のリターンはブラウザーにより異なるため、
	// 下記コードでフォーマットを統一する。
	var thisYear = 1900 + ( now.getYear() % 1900 );
	
	if ( (	dateY.value.length + 
			dateM.value.length + 
			dateD.value.length ) == 0 )
		return retNUL;
	
	if ( dateD.value == "") buyDateNotEnough = dateD;
	if ( dateM.value == "") buyDateNotEnough = dateM;
	if ( dateY.value == "") buyDateNotEnough = dateY;
	
	if ( isNaN( dateD.value ) )	buyDateNotValid = dateD;
	if (( ( 1 > dateD.value ) || ( dateD.value > 31 ) ) && 
		( dateD.value != 99 ) )	buyDateNotValid = dateD;
	
	if ( isNaN( dateM.value ) )	buyDateNotValid = dateM;
	if (( ( 1 > dateM.value ) || ( dateM.value > 12 ) ) &&
		(dateM.value != 99 ) )	buyDateNotValid = dateM;
	
	if ( isNaN( dateY.value ) )	buyDateNotValid = dateY;
	if (( ( dateY.value < 1800 ) || ( thisYear < dateY.value ) ) &&
		( dateY.value != 99 ) &&
		( dateY.value != 999 ))	buyDateNotValid = dateY;
	
	if ( buyDateNotValid != "OK" )
	{
		buyDateNotValid.focus();
		return retNotValid;
	}
	else
	if ( buyDateNotEnough != "OK" )
	{
		buyDateNotEnough.focus();
		return retNotEnough;
	}
	return retOK;

}

function CheckDate()
{
	var ResultEU; 
	var ResultDST;
	var buyDate="OK";
	
	with ( document.frmInterface )
	{
		ResultDST = _CheckDate( buyDSTy, buyDSTm, buyDSTd );
		ResultEU = _CheckDate( buyEUy, buyEUm, buyEUd );
		
		if ( ( ResultEU == retOK ) || ( ResultDST == retOK ) )
			return true;
		
		_CheckEntry( 
			"", 
			(true), 
			"エンドユーザー購入日、代理店からの納入日が共に正しく入力されて" + 
			"いません。\nいずれかを入力する必要があります。" );
		
		if ( ResultEU == retNotEnough )
			szErrMsg = "エンドユーザー購入日の入力が不十分です。" + 
				"\n\n" + szErrMsg;
		if ( ResultEU == retNotValid )
			szErrMsg = "エンドユーザー購入日の入力が不正です。" + 
				"\n\n" + szErrMsg;
		if ( ResultDST == retNotEnough )
			szErrMsg = "代理店からの納入日の入力が不十分です。" + 
				"\n\n" + szErrMsg;
		if ( ResultDST == retNotValid )
			szErrMsg = "代理店からの納入日の入力が不正です。" + 
				"\n\n" + szErrMsg;
		
		return false;
	}
}

function CheckEntry()
{
	szErrMsg = "";
	var buyEU = "OK";
	
	with( document.frmInterface )
	{
		// 簡単な掃除から
		if ( ApplicantName.value == szKanji ) 
			ApplicantName.value = "";
		if ( ApplicantPhName.value == szFurigana )
			ApplicantPhName.value = "";
		
		// Issue / Detail
		_CheckEntry( 
			Issue, 
			(	( Issue.selectedIndex == 0 ) && 
				( Detail.value.length == 0 ) ), 
			"交換理由、問題の詳細が共に入力されていません。\n" + 
			"いずれかを入力する必要があります。");
		
		// buyEU / buyDST
		CheckDate();
		
		// Dist
		_CheckEntry(
			Dist, 
			(Dist.selectedIndex == 0), 
			"代理店を選択してください。");
		
		// ProdSerial
		if ( (_CheckEntry( 
				ProdSerial, 
				(ProdSerial.value == ""), 
				"シリアル番号が入力されていません。")) )
			_CheckEntry( 
				ProdSerial, 
				!( IsValue( szAlphaNum, ProdSerial.value ) ),
				"シリアル番号の入力が不正です。");
		
		// ProdCode
		if ( (_CheckEntry( 
				ProdCode, 
				(ProdCode.value == ""), 
				"製品コードが入力されていません。")) )
			_CheckEntry( 
				ProdCode, 
				!( IsValue( szAlphaNum, ProdCode.value ) ), 
				"製品コードの入力が不正です。");
		
		// ProdName
		_CheckEntry( 
			ProdName, 
			(ProdName.value == ""), 
			"製品名が入力されていません。");
		
		// ProdFamily
		_CheckEntry( 
			ProdFamily, 
			(ProdFamily.selectedIndex == 0), 
			"製品種別が入力されていません。");
		
		// Address
		_CheckEntry( 
			Address, 
			(Address.value == ""), 
			"住所が入力されていません。");
		
		// PostalCode
		if ( (_CheckEntry( 
				PostalCode, 
				(PostalCode.value == ""), 
				"郵便番号が入力されていません。")) )
			_CheckEntry( 
				PostalCode, 
				!( IsValue( szPcode, PostalCode.value ) ), 
				"郵便番号の入力が不正です。");
		
		// ReceiverTel
		if ( (_CheckEntry( 
				ReceiverTel, 
				(ReceiverTel.value == ""), 
				"送付先電話番号が入力されていません。")) )
			_CheckEntry( 
				ReceiverTel, 
				!( IsValue( szPhone, ReceiverTel.value ) ), 
				"送付先電話番号の入力が不正です。");
		
		// ReceiverName
		_CheckEntry( 
			ReceiverName, 
			(ReceiverName.value == ""), 
			"送付先担当者氏名が入力されていません。");
		
		// Email
		_CheckEntry( 
			Email, 
			(Email.value == ""), 
			"E-mailが入力されていません。");
		
		// ApplicantTel
		if ( (_CheckEntry( 
				ApplicantTel, 
				(ApplicantTel.value == ""), 
				"申込者電話番号が入力されていません。")) )
			_CheckEntry( 
				ApplicantTel, 
				!( IsValue( szPhone, ApplicantTel.value ) ), 
				"申込者電話番号の入力が不正です。");
		
		// ApplicantName
		_CheckEntry( 
			ApplicantName, 
			(ApplicantName.value == ""), 
			"申込者氏名が入力されていません。");
		
		// CompanyName
		_CheckEntry( 
			CompanyName, 
			(CompanyName.value == ""), 
			"会社名が入力されていません。");
		
		// IpiNum
		if ( (_CheckEntry( 
				IpiNum, 
				(IpiNum.value == ""), 
				"ICP 番号が入力されていません。")) )
			_CheckEntry(
				IpiNum, 
				( isNaN( IpiNum.value ) ), 
				"ICP 番号が不正です。");
	}
	return szErrMsg;
}

function TrimAllEntry()
{
	with ( document.frmInterface )
	{
		IpiNum.value = TrimStr( IpiNum.value );
		CompanyName.value = TrimStr( CompanyName.value );
		ApplicantPhName.value = TrimStr( ApplicantPhName.value );
		ApplicantName.value = TrimStr( ApplicantName.value );
		ApplicantTel.value = TrimStr( ApplicantTel.value );
		Email.value = TrimStr( Email.value );
		
		ReceiverName.value = TrimStr( ReceiverName.value );
		ReceiverTel.value = TrimStr( ReceiverTel.value );
		PostalCode.value = TrimStr( PostalCode.value );
		Address.value = TrimStr( Address.value );
		
		ProdName.value = TrimStr( ProdName.value );
		ProdCode.value = TrimStr( ProdCode.value );
		ProdSerial.value = TrimStr( ProdSerial.value );
		
		buyEUy.value = TrimStr( buyEUy.value );
		buyEUm.value = TrimStr( buyEUm.value );
		buyEUd.value = TrimStr( buyEUd.value );
		
		buyDSTy.value = TrimStr( buyDSTy.value );
		buyDSTm.value = TrimStr( buyDSTm.value );
		buyDSTd.value = TrimStr( buyDSTd.value );
		
		Detail.value = TrimStr( Detail.value );
	}
}

function CopyAwrForm()
{
	var szDate = "";
	
	with ( document.frmInterface )
	{
		document.awrForm.IPI.value = IpiNum.value;
		document.awrForm.Company_kanji.value = CompanyName.value;
		document.awrForm.Applicant_Kana.value = ApplicantPhName.value;
		document.awrForm.Applicant_kanji.value = ApplicantName.value;
		document.awrForm.Applicant_Tel.value = ApplicantTel.value;
		document.awrForm.email.value = Email.value;
		
		document.awrForm.Receiver_kanji.value = ReceiverName.value;
		document.awrForm.Receiver_Tel.value = ReceiverTel.value;
		document.awrForm.Receiver_Post.value = PostalCode.value;
		document.awrForm.Receipt_Point_kanji.value = Address.value;
		
		document.awrForm.Product_name.value = ProdName.value;
		document.awrForm.Product_Cord.value = ProdCode.value;
		document.awrForm.FPOBATCH.value = ProdSerial.value;
		document.awrForm.Distributor.value = 
			Dist.options[Dist.selectedIndex].value;
		
		if ( buyEUy.value != "" )
		{
			szDate = 
				buyEUy.value +"/"+ 
				buyEUm.value +"/"+ 
				buyEUd.value;
		}
		else
			szDate = "----/--/--";
		if ( buyDSTy.value != "" )
		{
			szDate = szDate + " " + 
				buyDSTy.value +"/"+ 
				buyDSTm.value +"/"+ 
				buyDSTd.value;
		}
		else
			szDate = szDate + " ----/--/--"
		document.awrForm.Date_buying.value = szDate;
		
		document.awrForm.Issue.value = 
			Issue.options[Issue.selectedIndex].value;
		document.awrForm.details.value = Detail.value;
	}
}

function ClickSubmit()
{
	TrimAllEntry();
	
	var szErrMsg = CheckEntry();
	
	if ( szErrMsg != "" ) 
	{
		alert( szErrMsg );
	}
	else
	{
		if ( !ConfirmForm() ) return false;
	
		SaveToCookie();
		CopyAwrForm();
		setTimeToWatch1();
		encodeAllFields();
		
		document.awrForm.submit();
	}
	return false;
}

function ClickReset( )
{
	Refresh();
}


function _refresh( szCookieName, oCheck, oText )
{
	var szCook;
	
	if ( oCheck.checked = 
		( ( szCook = /*sierra.*/getCookie( szCookieName ) ) != null ) )
		oText.value = szCook;
	else
		oText.value = "";
}


function Refresh()
{
	with( document.frmInterface )
	{
		_refresh( cookIpi, saveIpi, IpiNum );
		_refresh( cookCnm, saveCnm, CompanyName );
		_refresh( cookAph, saveAph, ApplicantPhName );
		_refresh( cookAnm, saveAnm, ApplicantName );
		_refresh( cookAtl, saveAtl, ApplicantTel );
		_refresh( cookAem, saveAem, Email );
		_refresh( cookRnm, saveRnm, ReceiverName );
		_refresh( cookRtl, saveRtl, ReceiverTel );
		_refresh( cookPcd, savePcd, PostalCode );
		_refresh( cookAdr, saveAdr, Address );
		
		ProdName.value = "";
		ProdCode.value = "";
		ProdSerial.value = "";
		Dist.selectedIndex = 0;
		buyEUy.value = "";
		buyEUm.value = "";
		buyEUd.value = "";
		buyDSTy.value = "";
		buyDSTm.value = "";
		buyDSTd.value = "";
		Issue.selectedIndex = 0;
		Detail.value = "";
	}
}


function _savetocookie( szCookieName, oCheck, oText )
{
	var expSave = new Date();
	expSave.setTime( expSave.getTime() + ( 1000*60*60*24*31 ) );
	
	if ( oCheck.checked )
		/*sierra.*/setCookie( szCookieName, oText.value, expSave );
}

function _deletecookie( szCookieName )
{
	var expDel = new Date();
	expDel.setTime( expDel.getTime() - ( 1000*60*60*24*31 ) );
	
	/*sierra.*/setCookie( szCookieName, "", expDel );
}


function SaveToCookie()
{
	with( document.frmInterface )
	{
		_savetocookie( cookIpi, saveIpi, IpiNum );
		_savetocookie( cookCnm, saveCnm, CompanyName );
		_savetocookie( cookAph, saveAph, ApplicantPhName );
		_savetocookie( cookAnm, saveAnm, ApplicantName );
		_savetocookie( cookAtl, saveAtl, ApplicantTel );
		_savetocookie( cookAem, saveAem, Email );
		_savetocookie( cookRnm, saveRnm, ReceiverName );
		_savetocookie( cookRtl, saveRtl, ReceiverTel );
		_savetocookie( cookPcd, savePcd, PostalCode );
		_savetocookie( cookAdr, saveAdr, Address );
	}
}

function ClickFinder( szHtml )
{
	window.open( 
		szHtml, 
		'Finder', 
		'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=530,height=570' 
		 );
}

function ClickProcFind()
{
	window.open( 
		'ProcFind2.htm', 
		'Finder', 
		'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=530,height=570' 
		 );
}
function ClickMbFind()
{
	window.open( 
		'MbFind.htm', 
		'Finder', 
		'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=530,height=570' 
		 );
}

function ConfirmForm()
{
	var szMsg = "次の内容で送信してよろしいですか?";
	
	with( document.frmInterface )
	{
		szMsg += "\n\n[申込者に関する情報]";
		szMsg += "\nICP 番号: " + IpiNum.value;
		szMsg += "\n会社名: " + CompanyName.value;
		szMsg += "\n氏名: " + ApplicantName.value;
		if ( ApplicantPhName.value != "" )
			szMsg += " (" + ApplicantPhName.value + ")";
		szMsg += "\nE-mail: " + Email.value;
		
		szMsg += "\n\n[送付先に関する情報]";
		szMsg += "\n交換担当者名: " + ReceiverName.value;
		szMsg += "\n電話番号: " + ReceiverTel.value;
		szMsg += "\n郵便番号: " + PostalCode.value;
		szMsg += "\n交換住所: " + Address.value;
		
		szMsg += "\n\n[製品に関する情報]";
		szMsg += "\n製品名: " + ProdName.value;
		szMsg += "\n製品コード: " + ProdCode.value;
		szMsg += "\nシリアル番号: " + ProdSerial.value;
		
		szMsg += 
			"\n\n購入代理店名: " + Dist.options[Dist.selectedIndex].text;
		
		if ( 
			( buyEUy.value != "" ) && 
			( buyEUm.value != "" ) && 
			( buyEUd.value != "" ) )
		{
			szMsg += "\nエンドユーザー購入日: ";
			if ( 
				( buyEUy.value == "99" ) || 
				( buyEUy.value == "999" ) || 
				( buyEUm.value == "99" ) || 
				( buyEUd.value == "99" ) )
			{
				szMsg += "不明";
			}
			else
			{
				szMsg += 
					buyEUy.value + "/" +
					buyEUm.value + "/" +
					buyEUd.value;
			}
		}
		if ( 
			( buyDSTy.value != "" ) && 
			( buyDSTm.value != "" ) && 
			( buyDSTd.value != "" ) )
		{
			szMsg += "\n代理店からの納入日: ";
			if ( 
				( buyDSTy.value == "99" ) || 
				( buyDSTy.value == "999" ) || 
				( buyDSTm.value == "99" ) || 
				( buyDSTd.value == "99" ) )
			{
				szMsg += "不明";
			}
			else
			{
				szMsg += 
					buyDSTy.value + "/" +
					buyDSTm.value + "/" +
					buyDSTd.value;
			}
		}
		
		szMsg += "\n";
		if ( Issue.selectedIndex != 0 )
			szMsg += 
				"\n交換理由: " + Issue.options[Issue.selectedIndex].text;
		
		if ( Detail.value != "" )
			szMsg += "\n問題の詳細: " + Detail.value;
		
	}

	return confirm( szMsg );

}

function ClearCookiesForApplicant()
{
	if ( confirm( "コンピューターに保存された" + 
		"申込者に関する情報を削除します。\n\nよろしいですか?" )  )
	{
		_deletecookie( cookIpi );
		_deletecookie( cookCnm );
		_deletecookie( cookAph );
		_deletecookie( cookAnm );
		_deletecookie( cookAtl );
		_deletecookie( cookAem );
		
		with( document.frmInterface )
		{
			IpiNum.value = "";
			CompanyName.value = "";
			ApplicantPhName.value = "";
			ApplicantName.value = "";
			ApplicantTel.value = "";
			Email.value = "";
			
			saveIpi.checked = false;
			saveCnm.checked = false;
			saveAph.checked = false;
			saveAnm.checked = false;
			saveAtl.checked = false;
			saveAem.checked = false;
		}
	}
}

function ClearCookiesForReceiver()
{
	if ( confirm( "コンピューターに保存された" + 
		"送付先に関する情報を削除します。\n\nよろしいですか?" )  )
	{
		_deletecookie( cookRnm );
		_deletecookie( cookRtl );
		_deletecookie( cookPcd );
		_deletecookie( cookAdr );
		
		with( document.frmInterface )
		{
			ReceiverName.value = "";
			ReceiverTel.value = "";
			PostalCode.value = "";
			Address.value = "";
			
			saveRnm.checked = false;
			saveRtl.checked = false;
			savePcd.checked = false;
			saveAdr.checked = false;
		}
	}
}

function Right( szTarget, nLen )
{
	if ( szTarget.length <= nLen ) return szTarget;
	
	return szTarget.substring( szTarget.length - nLen );
}

function  setTimeToWatch1()
{
	now1 = new Date();
	document.awrForm.watch1.value = 
		( 1900 + ( now1.getYear() % 1900 ) ) + "/" + 
		Right( "0" + ( now1.getMonth() + 1), 2 ) + "/" + 
		Right( "0" + ( now1.getDate() ), 2 ) + " " +
		Right( "0" + now1.getHours(), 2 ) + ":" + 
		Right( "0" + now1.getMinutes(), 2 ) + ":" + 
		Right( "0" + now1.getSeconds(), 2 );
}

function checkCookie()
{
	with( document.frmPopup )
	{
		if ( getCookie( "Popup") )
		{
			chkPopup.checked = true;
			// セットされていれば期間延長 (する？)
			setCookie( "Popup", "o", 60 * 60 * 24 * 31 );
		}
		else
		{
			chkPopup.checked = false;
		}
	}
}

function changeCookie()
{
	var expSave = new Date();
	expSave.setTime( expSave.getTime() + ( 60*60*24*31 ) );

	with( document.frmPopup)
	{
		if ( chkPopup.checked )
		{
			setCookie( "Popup", "o", expSave );
		}
		else
		{
			_deletecookie( "Popup" );
		}
	}
}

function doPopup()
{
	if ( !getCookie( "Popup" ) )
	{
		var selval = document.frmInterface.ProdFamily.options[document.frmInterface.ProdFamily.options.selectedIndex].value;
	
		if (selval=="Processors")
		{
			window.open("helpproc.htm", "awrhelp", "width=600,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no").focus();
		}
		else if (selval=="DeskTop Boards")
		{
		window.open("helpdesktop.htm", "awrhelp", "width=600,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no").focus();
		}
		else if (selval=="Servers")
		{
		window.open("helpserver.htm", "awrhelp", "width=600,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no").focus();
		}
		else if (selval=="SSD")
		{
		window.open("helpssd.htm", "awrhelp", "width=600,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no").focus();
		}		
		else if (selval=="LAN Cards")
		{
		window.open("helpnic.htm", "awrhelp", "width=600,height=600,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no").focus();
		}
	}
}

function encodeAllFields()
{
	var frmTarget = document.awrForm;
	var fieldList = frmTarget.transfields.value.split(',');

	for( var i = 0; i < fieldList.length; i++) 
		frmTarget[fieldList[i]].value = encodeURIComponent( frmTarget[fieldList[i]].value );
}

