var CMS_DELIMITA = "_|_";

function $(tagId)
{
	var tagObj = document.getElementById(tagId);
	
	if (tagObj)
	{
		tagObj.setText = function (str)
		{
			if (tagObj.textContent)
			{
				// for Firefox
				if ((tagObj.hasChildNodes()) && (tagObj.firstChild.textContent))
				{
					tagObj.firstChild.textContent = str;
				}
				else
				{
					tagObj.textContent = str;
				}
			}
			else
			{
				tagObj.innerText = str;
			}
		}
		
		tagObj.setNodeValue = function (str)
		{
			if ((tagObj.firstChild) && (tagObj.firstChild.nodeValue))
			{
				tagObj.firstChild.nodeValue = str;
			}
		}
		
		tagObj.hide = function ()
		{
			tagObj.style.visibility = "hidden";
		}
		
		tagObj.remove = function ()
		{
			tagObj.style.display = "none";
		}
		
		tagObj.show = function ()
		{
			tagObj.style.visibility = "visible";
			tagObj.style.display = "block";
		}
		
		tagObj.isShowed = function ()
		{
			if ((tagObj.style.visibility != "hidden") && (tagObj.style.display != "none"))
			{
				return true;
			}
			return false;
		}
		
		tagObj.tempValue = "";
		
		return tagObj;
	}
	return null;
}

function trim(str)
{
	return str.replace(/^[ ]*/gim, "").replace(/[ ]*$/gim, "");
}

function getSelectedIndex(obj)
{
	for (var n = 0; n < obj.options.length; n++)
	{
		if (obj.options[n].selected) return n;
	}
	return null;
}

function checkFileName(str)
{
	var result = str.match(/(\\|\/|:|\*|\?|"|<|>|\|)/i);
	return (result == null);
}

function checkFilePath(str)
{
	var nameCheck = str.match(/(\\|:|\*|\?|"|'|<|>|\||%|#|&)/i);
	var pathCheck = str.match(/\/$/i);
	var errCheck  = str.match(/(\/\/|\.\/|\.$)/i);
	
	return ((nameCheck == null) && (pathCheck == null) && (errCheck == null));
}

function checkWindowsFilePath(str)
{
	var nameCheck = str.match(/(\*|\?|"|<|>|\|)/i);
	var pathCheck = str.match(/\/$/i);
	
	return ((nameCheck == null) && (pathCheck == null));
}

function checkMailAddress(str)
{
	var result = str.match(/[^A-Za-z0-9._\/-]/);
	return (result == null);
}

function isHankaku(str)
{
	var result = str.match(/[^ -~]+/i);
	
	return (result == null);
}

function isHankakuKatakana(str)
{
	return (str.match(/[\uFF61-\uFF9F]/) != null);
}

function isRelativePath(str)
{
	var result = str.match(/((^\.)|(\/\.\/)|(\/\.\.\/))/i);
	
	return (result != null);
}

function isDotFile(str)
{
	if (str.substr(0, 1) != "/") str = "/" + str;
	var result  = str.match(/\/\./i);
	
	return (result != null);
}

function isFirstHyphen(str)
{
	if (str.substr(0, 1) != "/") str = "/" + str;
	var result  = str.match(/\/-/i);
	
	return (result != null);
}

function getFileName(filePath)
{
	if ((filePath != null) && (filePath != ""))
	{
		tmpPath = replaceAllStr(filePath, "\\", "/");
		if (tmpPath.indexOf("/") < 0) return filePath;
		
		var tmpArray = tmpPath.split("/");
		return tmpArray[tmpArray.length - 1];
	}
	return null;
}

function replaceAllStr(str, fromStr, toStr)
{
	while (str.indexOf(fromStr) >= 0)
	{
		str = str.replace(fromStr, toStr);
	}
	return str;
}

function getExtension(fileName)
{
	if ((fileName != null) && (fileName != ""))
	{
		var tmp = fileName.toLowerCase() + ".tmp";
		var tmpArray = tmp.split(".");
		return tmpArray[tmpArray.length - 2];
	}
	return null;
}

function removeExtension(fileName)
{
	var pos = fileName.lastIndexOf(".");
	if (pos == -1) return fileName;
	return fileName.substr(0, pos);
}

function createHiddenObj(name, value, id)
{
	var hiddenObj = document.createElement("input");
	hiddenObj.type = "hidden";
	hiddenObj.name = name;
	hiddenObj.value = value;
	hiddenObj.id = id;
	
	return hiddenObj;
}

function getShortName(str, length)
{
	var len = str.length;
	if (len <= (length * 2)) return str;
	
	return (str.substr(0, length) + "..." + str.substr(len - length));
}

function splitCmsValues(str)
{
	var cmsArray = str.split(CMS_DELIMITA);
	for (var n = 0; n < cmsArray.length; n++)
	{
		cmsArray[n] = decodeCmsValue(cmsArray[n]);
	}
	return cmsArray;
}

function createCmsValues(ary)
{
	var retValue = encodeCmsValue(ary[0]);
	for (var n = 1; n < ary.length; n++)
	{
		retValue += CMS_DELIMITA + encodeCmsValue(ary[n]);
	}
	return retValue;
}

function encodeCmsValue(str)
{
	return str.replace(/\|/g, "||");
}

function decodeCmsValue(str)
{
	return str.replace(/\|\|/g, "|");
}

function getThisURL()
{
	var locationObj = window.location;
	var path = locationObj.pathname;
	
	var pathArray = path.split("/");
	var arrayMax = pathArray.length - 1;
	
	switch (getExtension(pathArray[arrayMax]))
	{
		case "php":
		case "cgi":
		case "html":
		case "htm":
			arrayMax--;
			break;
	}
	
	var setPath = "";
	for (var i = 0; i <= arrayMax; i++)
	{
		if (pathArray[i] != "") setPath += "/" + pathArray[i];
	}
	
	return locationObj.protocol + "//" + locationObj.hostname + setPath + "/";
}

function isIE()
{
	return (navigator.appVersion.indexOf("MSIE") != -1);
}

function isIE_version(version)
{
	return (navigator.appVersion.indexOf("MSIE " + version) != -1);
}

function reportError(errObj)
{
	return;
}

function dummy()
{
	return;
}