﻿function getElement(id) {
	return document.getElementById(id);
}
function createElement(type) {
	return document.createElement(type);
}
function removeElement(element) {
	try {
		if (element) {
			element.parentNode.removeChild(element);
		}
	}
	catch (e) {
	}
}
function hideElement(element) {
	if (element) {
		element.style.display = "none";
	}
}

function hideElementV(element) {
	if (element) {
		element.style.visibility = "hidden";
		element.style.display = "";
	}
}

function showElement(element) {
	if (element) {
		element.style.display = "";
		element.style.visibility = "";
	}
}
function clearElement(element) {
	for (var i in element) {
		try {
			
			element[i] = null;
		}
		catch (e) {
			continue;
		}
	}
}

function convertHTML(value) {
	if (!value) {
		return "";
	}
	return value.toString().split("&lt;").join("<").split("&gt;").join(">").split("&nbsp;").join(" ").split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;").split(" ").join("&nbsp;");
}

function GetOffsetPos(element) {
	var flag = element.tagName.toUpperCase() == "INPUT" ? true : false;
	var posTop = 0, posLeft = 0;
	do {
		posTop += element.offsetTop || 0;
		posLeft += element.offsetLeft || 0;
		element = element.offsetParent;
	} while (element);
	if (navigator.appVersion.indexOf("MSIE 6.0") != -1 && flag) {
		posLeft++;
	}
	return [posLeft, posTop];
}

function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.offsetWidth;
		myHeight = document.documentElement.offsetHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return([myWidth,myHeight]);
}   

function hideSelects(zIndexNow) {
	if (navigator.appVersion.indexOf("MSIE 6.0") != -1) {
		var selects = document.getElementsByTagName("select");
		var length = selects.length;
		for (var i = 0; i < length; i++) {
			if (selects[i].zIndexNow != zIndexNow && selects[i].style.visibility != "hidden") {
				selects[i].style.visibility = "hidden";
				selects[i].zIndexNow = zIndexNow;
			}
		}
	}
}

function showSelects(zIndexNow) {
	if (navigator.appVersion.indexOf("MSIE 6.0") != -1) {
		var selects = document.getElementsByTagName("select");
		var length = selects.length;
		for (var i = 0; i < length; i++) {
			if (selects[i].zIndexNow == zIndexNow) {
				selects[i].style.visibility = "";
				selects[i].zIndexNow = -1;
			}
		}
	}
}
var Mask = function () {
	this.zIndexNow = 100;
	this.maskDiv = null;
	this.indexArr = [];
	this.show = function () {
		this.zIndexNow += 10;
		if (this.indexArr.length == 0){
			var maskHeight = document.body.scrollHeight > getWindowSize()[1] ? document.body.scrollHeight : getWindowSize()[1];
			this.maskDiv = createElement("div");
			this.maskDiv.id = "MaskLayer";
			this.maskDiv.onclick = function () {
				var user_event = arguments[0] || window.event;
				user_event.cancelBubble = true;
			};
			this.maskDiv.style.cssText = "filter: alpha(opacity=50);background: #999999;position: absolute;left: 0px;top: 0px;";
			this.maskDiv.style.width = "100%";
			this.maskDiv.style.height = maskHeight + "px";
			document.body.appendChild(this.maskDiv);
		}
		this.maskDiv.style.zIndex = this.zIndexNow - 1;
		this.indexArr.push(this.zIndexNow - 1);
		hideSelects(this.zIndexNow);
	};
	this.clean = function () {
		showSelects(this.zIndexNow);
		this.indexArr.pop();
		if (this.indexArr.length > 0){
			this.maskDiv.style.zIndex = this.indexArr[this.indexArr.length - 1];
			this.zIndexNow = this.indexArr[this.indexArr.length - 1] + 1;
		}
		else {
			document.body.removeChild(this.maskDiv);
			this.zIndexNow = 100;
		}
	};
};

var DragAble = function (dragable,handler) {
	this.iDiffX = 0;
	this.iDiffY = 0;
	this.oDragable = dragable;
	this.oHandler = handler;
	
	this.mouseDown = function(){
		document.body.onselectstart = function () {return false;};
		document.body.style.userSelect = "none";
		document.body.style.MozUserSelect = "none";
		var object = arguments.callee.object;
		var user_event = arguments[0,0] || window.event;
		object.iDiffX = user_event.clientX - object.oDragable.offsetLeft;
		object.iDiffY = user_event.clientY - object.oDragable.offsetTop;
		document.body.onmousemove = object.mouseMove;
		document.body.onmouseup = object.mouseUp;
		object.oHandler.style.cursor = "move";
	};
	this.mouseDown.object = this;
	
	this.oHandler.onmousedown = this.mouseDown;
	
	this.mouseMove = function() {
		var object = arguments.callee.object;
		var user_event = arguments[0] || window.event;
		var wWidth = getWindowSize()[0];
		if ((user_event.clientX - object.iDiffX) >= 0 && (user_event.clientX - object.iDiffX + object.oDragable.clientWidth) <= wWidth - 20){
			object.oDragable.style.left = user_event.clientX - object.iDiffX + "px";
		}
		else {
			if ((user_event.clientX - object.iDiffX) < 0){
				object.oDragable.style.left = 0 + "px";
			}
			else {
				object.oDragable.style.left = wWidth - object.oDragable.clientWidth - 20 + "px";
			}
		}
		//obj.style.left = tempLeft + "px";
		if ((user_event.clientY - object.iDiffY) >= 0){
			object.oDragable.style.top = user_event.clientY - object.iDiffY + "px";
		}
		else {
			object.oDragable.style.top = 0 + "px";
		}
		
	};
	this.mouseMove.object = this;
	
	this.mouseUp = function() {
		var object = arguments.callee.object;
		document.body.onselectstart = "";
		document.body.style.userSelect = "";
		document.body.style.MozUserSelect = "";
		document.body.onmousemove = "";
		document.body.onmouseup = "";
		object.oHandler.style.cursor = "";
	};
	this.mouseUp.object = this;
};

var Popups = function () {
	this.containerElement = document.body;
	
	this.containerElement.onclick = function (){
		var user_event = arguments[0] || window.event;
		user_event.cancelBubble = true;
	};
	
	this.bindElement = function (stringTagName, stringClassName, objectContainerElement, objectContainer) {
		var element = createElement(stringTagName);
		element.id = "maskLayer";
		if(stringClassName != "")
		{
			//element.className = stringClassName;
			element.style.cssText = stringClassName;
		}
		if (objectContainerElement) {
			objectContainerElement.appendChild(element);
		}
		if (objectContainer){
			element.container = objectContainer;
		}
		return element;
	};
	
	this.initializeElement = function (stringType, param, functionCallback) {
		var container = this.bindElement("div", "position: absolute;margin-left:-100px; margin-top:180px; width: 800px;");
		container.bindElement = this.bindElement;
		//var shadow = this.bindElement("div", "position: absolute; background-color: #999999; width: 598px; left: 3px; top: 3px; filter: alpha(opacity=60);", container, container);
		var main = this.bindElement("div", "position: absolute; border: 1px solid #74C4FF; width: 796px;", container, container);
		var head = this.bindElement("div", "background-image: url(/images/mask/bg_title.gif); background-repeat: repeat-x;  height: 25px; width: auto;", main, container);
		container.head = head;
		var titleIcon = this.bindElement("div", "background-image: url(/images/mask/title_common.gif); background-repeat: no-repeat; color: #ff0000; padding-left: 28px; padding-top: 4px; width: 200px; height: 21px; float: left;", head, container);
		titleIcon.id = "PopErrorMessage";
		container.titleIcon = titleIcon;
		var closeIcon = this.bindElement("div", "background-image: url(/images/mask/ico_close_out.gif); background-repeat: no-repeat; display: inline; float: right; font-size: 0px; height: 18px; margin: 3px 4px 0px 0px; width: 19px; cursor: pointer;", head, container);
		closeIcon.onmouseover = function () {
			this.style.backgroundImage = "url(/images/mask/ico_close_over.gif)";
		};
		closeIcon.onmouseout = function () {
			this.style.backgroundImage = "url(/images/mask/ico_close_out.gif)";
		};
		closeIcon.onclick = function () {
			this.container.remove();
		};
		container.closeIcon = closeIcon;
		var user_body = this.bindElement("div", "float: left; background: #FFFFFF; width: 796px;", main, container);
		container.user_body = user_body;
		var element = this.bindElement("div", "clear: right; display: inline; float: left; height: auto; margin: 0px 0px; width: 796px; background-color: #ffffff; ", user_body, container);
		container.element = element;
		var buttons = this.bindElement("div", "border-top: 1px solid #74C4FF; background-color: #DFF1FE; height: 28px;text-align: right; padding-right: 10px; padding-top: 6px;", user_body, container);
		buttons.id = "PopHandleArea";
		container.buttons = buttons;
	    
	    var buttonCancel = this.bindElement("img", "margin-left: 10px;");
	    buttonCancel.src = "/images/btn_cancel.gif";
	    buttonCancel.container = container;
	    buttonCancel.onclick = function(){
	        this.container.remove();			
	    };
	    buttonCancel.onmouseover = function(){
	        this.style.cursor = "hand";
	    };
	    container.buttonCancel = buttonCancel;
	    buttons.appendChild(buttonCancel);
		this.containerElement.appendChild(container);
		hideElement(container);
		container.show = function () {
			this.style.left = (getWindowSize()[0] - 600) / 2 + document.documentElement.scrollLeft + "px";
			this.style.top = (getWindowSize()[1] - 402) / 2 + document.documentElement.scrollTop + "px";
			mask.show();
			this.style.zIndex = mask.zIndexNow;
			showElement(this);
		};
		container.remove = function () {
			mask.clean();
			CheckPlay();
			removeElement(this);
			//window.location ="index.asp";
		};
		container.hide = function () {
			hideElement(this);
		};
		return container;
	};
	this.createPopup = function (stringType, param, functionCallback) {
		if(stringType == "MaterialPreview")
		{
		    var popup = this.initializeElement(stringType, "", functionCallback);
	        popup.element.innerHTML = param;
	        return popup;
		}
	};
};


function numValidate(num)
{
    if (num == null || num.trim() == "" || num.length != 26)
        return false;

    var reg = /^[A-Za-z0-9]+$/;
    return reg.test(num);
}

String.prototype.trim = function(){　
    return this.replace(/(^s*)|(s*$)/g, "");　
}

function folderNameValidate(value)
{
    var result = value.match(/^[^\/\\:*?<>|"]+$/);
    if(result == null) return false;
    return true;
}
