var newWindow;
function MakeArray(n) {
	this.length=n; 
	for(var i=1; i<=n; i++) {
		this[i]=0
	}
	return this
}
 
function openWindow(url, windName, nWidth, nHeight) {
	screenWidth = screen.availWidth; 
	screenHeight = screen.availHeight; 

	winTop = (screen.availHeight - nHeight) / 2; 
	winLeft = (screen.availWidth - nWidth) / 2; 

	var nProperty = "scrollbars=no,menubar=no,status=yes,toolbar=no,resizable=no,titlebar=no,alwaysRaised=yes,width="+(nWidth)+",height="+(nHeight)+",top="+(winTop)+",left="+(winLeft);

	OLTdgWindow = window.open(url, windName, nProperty)
	OLTdgWindow.focus();
}

function openWindow2(url, name) {
	newWindow = window.open(url,name,"scrollbars=yes,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes,width=780,height=460");
	newWindow.focus();
}

function openWindow4(url, name, nWidth, nHeight) {
	screenWidth = screen.width; 
	screenHeight = screen.height;
	winTop = 0;
	winLeft = 0;
	winWidth = nWidth;
	winHeight = nHeight;
	nProperty = "scrollbars=no,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes";

	if ((screen.availHeight-28) <= nHeight) {
		winHeight = screen.availHeight-28;
	}
	if ((screen.availWidth-10) <= nWidth) {
		winWidth = screen.availWidth-10;
	}
	
	winTop = (screen.availHeight-28 - winHeight) / 2; 
	winLeft = (screen.availWidth-10 - winWidth) / 2;
	nProperty = nProperty+
				",left="+(winLeft)+
				",top="+(winTop)+
				",width="+(winWidth)+
				",height="+(winHeight);		

	newWindow = window.open(url,name,nProperty);
//	if(document.layers) {
//		if (screen.height == 600)
//			newWindow.resizeTo(screen.availWidth-(newWindow.outerWidth-newWindow.innerWidth),screen.availHeight-(newWindow.outerHeight-newWindow.innerHeight)); 
//	}
	newWindow.focus();
}

function openWindow3(url){
	cWindow = window.open(url,"thewindow3","scrollbars=yes,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes,width=700,height=460");
	cWindow.focus();
}

function isNumber(inputVal, allownegative, allowdecimal) {
	oneDecimal = false
	inputStr = "" + inputVal
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			if (allownegative==true) 
				continue
			else
				return false
		}
		if (oneChar == "." && !oneDecimal) {
			if (allowdecimal==true) {
				oneDecimal = true
				continue
			} else
				return false
		}
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}

function checkfield(field1, type1, nlen, chknull, fname) {
	var   isvalid =true
	if (chknull) {
		if (field1.value == null || field1.value == "") {
			alert(fname + " จะต้องมีค่า")
			isvalid=false
		}
	}
	strVal=field1.value;
	if (nlen != 0 && isvalid==true) {          
		if (strVal.length < nlen) {
			alert(fname + " จะต้องมีความยาวอย่างน้อย " + nlen + " ตัวอักษร")
			isvalid=false
		}
	}
	if (type1 == "n-" && isvalid==true) {
		if (!isNumber(field1.value, true, true)) {
			alert(fname +" จะต้องประกอบด้วยตัวเลขเท่านั้น")
			isvalid=false
		}
	}
	if (type1 == "n" && isvalid==true) {
		if (!isNumber(field1.value, false, true)) {
			alert(fname +" จะต้องประกอบด้วยตัวเลขเท่านั้น")
			isvalid=false
		}
	}
	if (type1 == "i-" && isvalid==true) {
		if (!isNumber(field1.value, true, false)) {
			alert(fname +" จะต้องเป็นเลขจำนวนเต็มเท่านั้น")
			isvalid=false
		}
	}
	if (type1 == "i" && isvalid==true) {
		if (!isNumber(field1.value, false, false)) {
			alert(fname +" จะต้องเป็นเลขจำนวนเต็มเท่านั้น")
			isvalid=false
		}
	}
	
	if (isvalid==false) {
		field1.focus()
	}
	return isvalid
}

function validpswd(password1, password2){
	if (password1.value != password2.value) {
		alert ("รหัสผ่าน และ รหัสผ่านอีกครั้งหนึ่ง จะต้องมีค่าเดียวกัน")
		password1.focus()
		return false
	}
	return true        
}

function checkpassword(field1, type1, chknull, fname) {
	var   isvalid = true
	if (chknull) {
		if (field1.value == null || field1.value == "") {
			alert(fname + " จะต้องมีค่า")
			isvalid=false
		}
	}
	strVal = field1.value
	if (isvalid==true)   {
		var offset = 0
		offset = strVal.indexOf(' ')
		if (offset != -1)   {
			alert(fname + " ไม่สามารถมีช่องว่าง")
			isvalid = false
		}
	}
	strVal = field1.value
	
	if (isvalid==true) {          
		if( (strVal.length < 6) || (strVal.length > 10)) {
			alert(fname + " จะต้องมีความยาวอยู่ระหว่าง 6-10 ตัวอักษร")
			isvalid=false
		}
	}
	
	if (isvalid==false) {
		field1.focus()
	}
	return isvalid
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;
	if(!d)
		d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all)
		x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++)
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
		x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById)
		x=d.getElementById(n);
	return x;
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr;
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
		x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document;
	if(d.images) {
		if(!d.MM_p)
			d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
		for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0) {
				d.MM_p[j]=new Image;
				d.MM_p[j++].src=a[i];
			}
	}
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments;
	document.MM_sr=new Array;
	for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null) {
			document.MM_sr[j++]=x;
			if(!x.oSrc)
				x.oSrc=x.src;
			x.src=a[i+2];
		}
}

function validate2() {
	if (checkfield(document.loginForm.loginName,'s',0,true,'Login')==false) {
		return false;
	}
	if (checkpassword(document.loginForm.passwd,'s',true,'Password')==false) {
		return false;
	}
	return true;
}

function move(fbox, tbox) {
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}
	var fLength = 0;
	var tLength = arrTbox.length;

	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		} else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
		}
	}
	
	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;

	for(c = 0; c < arrFbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}

	var j = 1;
	var tmp = new Option();
	for(c = 1; c < arrTbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		if(no.text != tmp.text){
			tbox[j] = no;
			j = j+1;
			tmp.value = no.value;
			tmp.text  = no.text;
		}
	}//for

	tmp.value = arrLookup[arrTbox[0]];
	tmp.text = arrTbox[0];
	if(arrTbox[0] != arrTbox[1] ){
		tbox[0] = tmp;
	}else{
		tbox.options[0] = null;
	}
}

function moveAll(fbox, tbox) {
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}
	var fLength = 0;
	var tLength = arrTbox.length;
	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		}
	}

	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;
	
	for(c = 0; c < arrFbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}

	var j = 1;
	var tmp = new Option();
	for(c = 1; c < arrTbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		if(no.text != tmp.text){
			tbox[j] = no;
			j = j+1;
			tmp.value = no.value;
			tmp.text  = no.text;
		}
	}//for
	tmp.value = arrLookup[arrTbox[0]];
	tmp.text = arrTbox[0];
	if(arrTbox[0] != arrTbox[1] ){
		tbox[0] = tmp;
	}else{
		tbox.options[0] = null;
	}
}
