
function PreloadImg()
{
	var appVer=parseInt(navigator.appVersion);
	var isNC=false,isN6=false,isIE=false;
	if (document.all && appVer >= 4) isIE=true; else
		if (document.getElementById && appVer > 4) isN6=true; else
			if (document.layers && appVer >= 4) isNC=true;
	if (isNC||isN6||isIE){
		if (document.images){
			var imgName = PreloadImg.arguments[0];
			var cnt;
			swImg[imgName] = new Array;
			for (cnt = 1; cnt < PreloadImg.arguments.length; cnt++){
				swImg[imgName][PreloadImg.arguments[cnt]] = new Image();
				swImg[imgName][PreloadImg.arguments[cnt]].src = PreloadImg.arguments[cnt];
			}
		}
	}
}

function NewImg(doc, imgName)
{
	for (var i=0; i < doc.layers.length; i++){
		var img = doc.layers[i].document.images[imgName];
		if (!img) img = NewImg(doc.layers[i], imgName);
		if (img) return img;
	}
	return null;
}

function SwapImg(imgName, imgSrc)
{
	var appVer=parseInt(navigator.appVersion);
	var isNC=false,isN6=false,isIE=false;
	if (document.all && appVer >= 4) isIE=true; else
		if (document.getElementById && appVer > 4) isN6=true; else
			if (document.layers && appVer >= 4) isNC=true;
	if (isNC||isN6||isIE){
		if (document.images){
			var img = document.images[imgName];
			if (!img) img = NewImg(document, imgName);
			if (img) img.src = imgSrc;
		}
	}
}

function WinOpen(url,name,width,height,resize)
{
	if ( !width || width<=0 ) {width = 600}
	if ( !height || height<=0 ) {height = 480}
	if ( !resize ) {resize = "yes"}
	var options = "width=" + width + ",height=" + height + "toolbar=no,location=no,directories=no,status=" + resize + ",menubar=no,scrollbars=" + resize + ",resizable=" + resize + ",copyhistory=no";
	new_window = window.open(url, name, options);
	window.self.name = "main";
	new_window.focus();
}

function ImgWinOpen(url,name,width,height)
{
	if (!width) {width = 320}
	if (!height) {height = 240}
	var options = "width=" + width + ",height=" + height + "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no";
	new_window = window.open(url, name, options);
	window.self.name = "main";
	new_window.focus();
}

function WinLink(url)
{
	if (opener.closed) {
		main_window = window.open("", "main");
		main_window.location.href=url;
	} else {
		opener.location.href=url;
	}
	opener.focus();
}

function DirectLink(linkLoc)
{
	if (linkLoc != "" ) {window.location = linkLoc}
}

function fsmGetElementById(id)
{
	if (document.getElementById) {
		return (document.getElementById(id));
	} else if (document.all) {
		return (document.all[id]);
	} else {
		if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) {
			return (document.layers[id]);
		}
	}
}

function appendSelectOption(selectMenuId, optionName, optionValue){
	var selectMenu = fsmGetElementById(selectMenuId);
	var newoption = new Option(optionName, optionValue);
	newoption.selected = true;
	selectMenu.options[selectMenu.options.length] = newoption;
}

function disableElement(target){
	var targetDom = fsmGetElementById(target);
	if (targetDom.disabled != true) {
		targetDom.disabled = true;
	} else {
		targetDom.disabled = false;
	}
}
function fsmCheckAll(formname, switchid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = fsmGetElementById(switchid);
	for (var i = 0; i < ele.length; i++) {
		var e = ele[i];
		if ( (e.name != switch_cbox.name) && (e.type == 'checkbox') ) {
			e.checked = switch_cbox.checked;
		}
	}
}

function fsmCheckGroup(formname, switchid, groupid) {
	var ele = document.forms[formname].elements;
	var switch_cbox = fsmGetElementById(switchid);
	for (var i = 0; i < ele.length; i++) {
		var e = ele[i];
		if ( (e.type == 'checkbox') && (e.id == groupid) ) {
			e.checked = switch_cbox.checked;
			e.click(); e.click();  // Click to activate subgroups
									// Twice so we don't reverse effect
		}
	}
}

function fsmCheckAllElements(elementIds, switchId) {
	var switch_cbox = fsmGetElementById(switchId);
	for (var i = 0; i < elementIds.length; i++) {
		var e = fsmGetElementById(elementIds[i]);
		if ((e.name != switch_cbox.name) && (e.type == 'checkbox')) {
			e.checked = switch_cbox.checked;
		}
	}
}

function fsmSavePosition(id)
{
	var textareaDom = fsmGetElementById(id);
	if (textareaDom.createTextRange) {
		textareaDom.caretPos = document.selection.createRange().duplicate();
	}
}

function fsmInsertText(domobj, text)
{
	if (domobj.createTextRange && domobj.caretPos){
  		var caretPos = domobj.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else if (domobj.getSelection && domobj.caretPos){
		var caretPos = domobj.caretPos;
		caretPos.text = caretPos.text.charat(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else {
		domobj.value = domobj.value + text;
  	}
}

function fsmCodeSmilie(id, smilieCode) {
	var revisedMessage;
	var textareaDom = fsmGetElementById(id);
	fsmInsertText(textareaDom, smilieCode);
	textareaDom.focus();
	return;
}

function showImgSelected(imgId, selectId, imgDir, extra, fsmUrl) {
	if (fsmUrl == null) {
		fsmUrl = "./";
	}
	imgDom = fsmGetElementById(imgId);
	selectDom = fsmGetElementById(selectId);
	imgDom.src = fsmUrl + "/"+ imgDir + "/" + selectDom.options[selectDom.selectedIndex].value + extra;
}

function fsmValidate(subjectId, textareaId, submitId, plzCompletePhrase, msgTooLongPhrase, allowedCharPhrase, currCharPhrase) {
	var maxchars = 65535;
	var subjectDom = fsmGetElementById(subjectId);
	var textareaDom = fsmGetElementById(textareaId);
	var submitDom = fsmGetElementById(submitId);
	if (textareaDom.value == "" || subjectDom.value == "") {
		if (plzCompletePhrase == null) {
			plzCompletePhrase = "Please complete the subject and message fields.";
		}
		alert(plzCompletePhrase);
		return false;
	}
	if (maxchars != 0) {
		if (textareaDom.value.length > maxchars) {
			if (msgTooLongPhrase == null) {
				msgTooLongPhrase = "Your message is too long.";
			}
			if (allowedCharPhrase == null) {
				allowedCharPhrase = "Allowed max chars length: ";
			}
			if (currCharPhrase == null) {
				currCharPhrase = "Current chars length: ";
			}
			alert(msgTooLongPhrase + "\n\n" + allowedCharPhrase + maxchars + "\n" + currCharPhrase + textareaDom.value.length + "");
			textareaDom.focus();
			return false;
		} else {
			submitDom.disabled = true;
			return true;
		}
	} else {
		submitDom.disabled = true;
		return true;
	}
}

// changebg(レイヤー名,色or背景画像)
function changebg(id,val){
	var ie=!!document.all
	var nn=!!document.layers
	var d1=!!document.getElementById
	if((ie||d1)&&(!val||val.charAt(0)!='#'))val='url('+val+')'
	if(ie||d1)document[ie?'all':'getElementById'](id).style['background'+(val.charAt(0)=='#'?'Color':'Image')]=val;else
	if(nn)if(val&&val.charAt(0)=='#')document.layers[id].bgColor=val;else document.layers[id].background.src=val
}

// changestyle(レイヤー名,CSSプロパティ,値) ※NN4では無効
function changestyle(id,attr,val){
	var ie=!!document.all
	var nn=!!document.layers
	var d1=!!document.getElementById
	if(ie||d1)document[ie?'all':'getElementById'](id).style[attr]=val
}
