var alphaNumericLists = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var bigAlphaNumericLists = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var telFaxLists = "-0123456789";

function toBig(frm) {
	frm.value = frm.value.toUpperCase();
}

function hasSmall(frm) {
	i = 0;
	cLength = frm.value.length;
	while(i < cLength) {
		if(isSmall(frm.value)) {
			return true;
		}
		i++;            
	}
	return false;
}

function isSmall(variable) {
	if(variable.charCodeAt(variable.length - 1) >= 97 && variable.charCodeAt(variable.length - 1) <= 122) {
		//alert("true");
		return true;                
	} else {
		return false;
	}
}

// ´ë¹®ÀÚ ÀÔ·Â °Ë»ç
function allowOnlyCapital(frm) {
	toBig(frm);
	/*
	if(frm.value.charCodeAt(frm.value.length - 1) >= 65 && frm.value.charCodeAt(frm.value.length - 1) <= 90) {
		//alert("big");
	} else if(hasSmall(frm)) {
		//alert("small");
		toBig(frm);
	} else if(frm.value != "") {
		alert("You should write in capital letters.");
		frm.value = "";
	}
	*/
}

function testCall() {
	alert("Did you call me?");
}

// ¼ýÀÚ ÀÔ·Â °Ë»ç/¼Ò¼öÁ¡ ÀÌÇÏ ÀÚ¸´¼öµµ ÇÔ²² °Ë»ç
function checkDecimals(fieldName, fieldValue, decallowed) {
	//decallowed = 2;  // how many decimals are allowed?

	if (isNaN(fieldValue)) {
		alert("That does not appear to be a valid number.  Please try again.");
		fieldName.select();
		fieldName.focus();
	// ¼Ò¼öÁ¡ ÀÌÇÏ ÀÚ¸®¼ö Á¦ÇÑÀÌ ÀÖ´Â °æ¿ì
	} else if(decallowed != null) {
		if (fieldValue.indexOf('.') == -1)  fieldValue += ".";
		dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);

		if (dectext.length > decallowed) {
			alert ("Please enter a number with up to " + decallowed + " decimal places.  Please try again.");
			fieldName.select();
			fieldName.focus();
		} else {
			//alert ("That number validated successfully.");
			return true;
		}
   } else {
	   if(fieldValue.indexOf('.') != -1) {
			alert("That does not appear to be a valid number.  Please try again.");
			fieldName.select();
			fieldName.focus();
	   } else {
			return true;
	   }
   }
   return false;
}

function changeUnit(frm) {
	/*
	var max = 2;
	for(i = 1; i <= max; i++) {
		var unitValue = eval('frm.unit_' + i +'.value');
		if(unitValue == "M") {
			unitValue = "FT";
		} else {
			unitValue = "M";
		}
	}
	*/
	if(frm.unit.value == "M") {
		frm.unit.value = "FT";
	} else {
		frm.unit.value = "M";
	}
}

function Trim(fieldName) {
	lTrim(fieldName);
	RTrim(fieldName);
}

function lTrim(fieldName) {
	while('' + fieldName.value.charAt(0) == ' ') {
		fieldName.value = fieldName.value.substring(1, fieldName.value.length);
	}
}

function RTrim(fieldName) {
	while('' + fieldName.value.charAt(fieldName.value.length - 1) == ' ') {
		fieldName.value = fieldName.value.substring(0, fieldName.value.length-1);
	}
}

// °ø¹é °Ë»ç
// °ø¹éÀÌ¸é true
function isBlankField(field) {
	var fieldValue = field.value;
//	alert(fieldValue);
	if(fieldValue == "" || isAllSame(field, " ")) {
		alert ("The mandatory field is blank. \nPlease fill out the box.");
		field.focus();
		return true;
	}
	return false;
}

// °ø¹é °Ë»ç
// °ø¹éÀÌ¸é true
function isBlankFieldMsg(field, msg) {
	var fieldValue = field.value;
//	alert(fieldValue);
	if(fieldValue == "" || isAllSame(field, " ")) {
		if(msg == "msg") {
			alert ("The mandatory field is blank. \nPlease fill out the box.");
			field.focus();
		}
		
		return true;
	}
	return false;
}


function isBlankFieldValue(fieldValue, msg) {
	if(fieldValue == "" || isAllSameValue(fieldValue, " ")) {
		if(msg == "msg") {
			alert ("The mandatory field is blank. \nPlease fill out the box.");
//			field.focus();
		}
		
		return true;
	}
	return false;
}


// °ø¹é °Ë»ç
// °ø¹éÀÌ¸é true
function isBlankFieldParam(field, param) {
	var fieldValue = field.value;
//	alert(fieldValue);
	if(fieldValue == "" || isAllSame(field, " ")) {
		alert ("The " + param + " field is a prerequisite. \nPlease fill out the field, first.");
		field.focus();
		return true;
	}
	return false;
}


function isAllSame(field, aValue) {
	var value = field.value;
	return isAllSameValue(value, aValue);
	/*
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = field.value.substring(i, i+1);
		if (temp != aValue) {
			return false;
	    }
	}
	return true;
	*/
}

function isAllSameValue(value, aValue) {
	var temp;
	for (var i=0; i<value.length; i++) {
		temp = value.substring(i, i+1);
		if (temp != aValue) {
			return false;
	    }
	}
	return true;
}


function checkBlankField(fieldName) {
	var fieldValue = fieldName.value;
	if(fieldValue == "" || !invalidate(fieldName, " ")) {
		alert ("The necessary field is blank. \nPlease fill out the box.");
		fieldName.focus();
		return false;
	}
	return true;
}

// ÀÏ¹ÝÀûÀÎ VALIDATION
function validateValue(fieldName, fieldValue, validList, alertMessage) {
	if(validList == null) {
		validList = "abcdefghijklmnopqrstuvwxyz0123456789";
	}
	if(alertMessage == null) {
		alertMessage = "Only characters and numbers are accepted!";
	}
//	var ok = "yes";
	var temp;
	for (var i=0; i<fieldValue.length; i++) {
		temp = "" + fieldValue.substring(i, i+1);
		if (validList.indexOf(temp) == "-1") {
//			ok = "no";
//			alert("Only characters and numbers are accepted!");
			alert(alertMessage);
			fieldName.focus();
			fieldName.select();
			return false;
	    }
	}
	return true;
}

// ÀÏ¹ÝÀûÀÎ VALIDATION
function validate(field, validList, alertMessage) {
	if(validList == null) {
		validList = "abcdefghijklmnopqrstuvwxyz0123456789";
	}
	if(alertMessage == null) {
		alertMessage = "Only characters and numbers are accepted!";
	}
//	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (validList.indexOf(temp) == "-1") {
//			ok = "no";
//			alert("Only characters and numbers are accepted!");
			alert(alertMessage);
			field.focus();
			field.select();
			return false;
	    }
	}
	return true;
}

// ÀÏ¹ÝÀûÀÎ VALIDATION
function invalidate(field, validList, alertMessage) {
	if(validList == null) {
		validList = "abcdefghijklmnopqrstuvwxyz0123456789";
	}
	if(alertMessage == null) {
		alertMessage = "The necessary field is blank. \nPlease fill out the box.";
	}
//	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (validList.indexOf(temp) != "-1") {
//			ok = "no";
//			alert("Only characters and numbers are accepted!");
			//alert(alertMessage);
//			field.focus();
//			field.select();
			return false;
	    }
	}
	return true;
}

function emailCheck (emailField) {

	var emailStr = emailField.value;

	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */

	var checkTLD=1;

	/* The following is the list of known TLDs that an e-mail address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */

	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include () < > @ ,; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {

		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */

		alert("Email address seems incorrect (check @ and .'s)");
		emailField.focus();
		emailField.select();
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters.");
			emailField.focus();
			emailField.select();
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters.");
			emailField.focus();
			emailField.select();
			return false;
	   }
	}

	// See if "user" is valid 

	if (user.match(userPat)==null) {

		// user is not valid

		alert("The username doesn't seem to be valid.");
		emailField.focus();
		emailField.select();
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

		// this is an IP address

		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				emailField.focus();
				emailField.select();
				return false;
		   }
		}
		return true;
	}

	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name does not seem to be valid.");
			emailField.focus();
			emailField.select();
			return false;
	   }
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		emailField.focus();
		emailField.select();
		return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) {
		alert("This address is missing a hostname!");
		emailField.focus();
		emailField.select();
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function validateTemperatureUnit(frm) {
	var tempValue = frm.value;
//	var endIndex = tempValue.charAt(tempValue.length - 1);
//	var numeric = tempValue.substring(0, endIndex);
	var unit = tempValue.substring(tempValue.length - 1, tempValue.length);

	if(unit == "C" || unit == "F") {
		return true;
	} else {
		alert("Please input units(C/F).");
		frm.focus();
		return false;
	}
}

function checkSelectIndex(frm) {
    if(frm.selectedIndex == 0) {
        alert('Please select one item.');
        frm.focus();
        return false;
    } else {
        return true;
    }
}

function SetSortKey(sortKey) {
	document.form.SortKey.value = sortKey;
}

function SetSortOrder(sortOrder) {
	if(sortOrder == "" || sortOrder == "DESC") {
		sortOrder = "ASC";
	} else if(sortOrder == "ASC") {
		sortOrder = "DESC";
	}
	document.form.SortOrder.value = sortOrder;
}

function Set_PageNum(sPage) {
	document.form.ListPage.value = sPage;
}

function NoticeCfm() {
	alert("This content is already confirmed by JPB Administrator.\nYou cannot modify it.");
}


function checkAllCntrLists() {
    var allChecked = false;
    checkboxes = document.getElementsByName("CheckBoxes");
    for(i = 0; i < checkboxes.length; i++) {
        if(checkboxes[i].checked) {
            allChecked = true;
        } 
    }

    for(i = 0; i < checkboxes.length; i++) {
        if(!allChecked) {
            checkboxes[i].checked = true;
        } else {
            checkboxes[i].checked = false;
        }
    }
}

function GeneralCntrInq() {
	x = screen.width;
	y = screen.height;

	var WinMode   = "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes" 
	WinMode = WinMode + ",width=560,height=620,left="+((x - 560) / 2)+",top="+((y - 550) / 2);


	if(this.document.form.TheGenCntrInqTxtBox.value == null || this.document.form.TheGenCntrInqTxtBox.value == "") {
		alert('Input the container number !     ');
		this.document.form.TheGenCntrInqTxtBox.focus();
		return false;
	}

	var param = "?TheGenCntrInqTxtBox=" + this.document.form.TheGenCntrInqTxtBox.value;
	var TargetDir = "/jpb/Template/Win_Cntr.jsp" + param;

	
	D_Window = window.open(TargetDir, "Container", WinMode);

	D_Window.focus();
}


function KeyUp_Change(command , obj) {
	var val = obj.value;
	val += "";

	if(val == "undefined" || val == "null" || val == "") {

	}else{
		obj.value = val.toUpperCase();

		if(command == "Temp") {
			validateTemperatureUnit(obj);
		} else if (command == "FE") {
			if (obj.value == "F" || obj.value == "E") {
			} else {
				alert("Typed data format is not accepted. \n Typed data format " + "\n\n        F : Full \n        E : Empty ")
				obj.value = "F";
				obj.focus();
			}
		}
	}
}


function GnrlCheck(command, obj, param1) {
	var val = obj.value;

	// Decimal
	// a - allowed decimal size
	if(command == "Decimal") {
		checkDecimals(obj, val, param1); 
	
	// Number
	} else if(command == "Number") {
		checkDecimals(obj, val);		

	// Capital
	} else if(command == "Big") {
		toBig(obj)
	}
}


function popSessionOut() {
	parent.location.replace ('index.html');
}


// Whether 'val' is the member of 'array' or not
function isMember(array, val) {
	strImage = array.toString();
	if(strImage.indexOf(val) != -1) {
		return true;
	} else {
		return false;
	}
}


// Get index of 'val' in Array 'array'
function getArrayIndex(array, val) {
	for(i = 0; i < array.length; i++) {
		if(array[i] == val) {
			return i;
		}
	}
	return -1;
}


// Show array's all items with alert message
function showArray(array) {
	for(i = 0; i < array.length; i++) {
		alert(array[i]);
	}
}	


// Get different frame name's number
function getDifferentFrameNum(docu) {
	var nameArr = new Array();

	if(docu.frames == null) return null;

	for(i = 0; i < docu.frames.length; i++) {
		if(i == 0) {
			nameArr.push(docu.frames[0].name);
		} else {
			if(!isMember(nameArr, docu.frames[i].name)) {
				nameArr.push(docu.frames[i].name);
			}
		}
	}

	return nameArr.length;
}


// Get 'frameName' order in 'docu' document object
function getFrameOrder(docu, frameName) {
	var nameArr = new Array();

	if(docu.frames == null) return null;

	for(i = 0; i < docu.frames.length; i++) {
		if(i == 0) {
			nameArr.push(docu.frames[0].name);
		} else {
			if(!isMember(nameArr, docu.frames[i].name)) {
				nameArr.push(docu.frames[i].name);
			}
		}
	}
	
	return getArrayIndex(nameArr, frameName);
}


// Get vsl code from 'jpvc'
function getVslNameFromJPVC(jpvc) {
	hyphen = jpvc.indexOf("-");
	return jpvc.substring(2, hyphen);
}


// Get year from 'jpvc'
function getYearFromJPVC(jpvc) {
	var year = "";
	if(jpvc != null && jpvc != "") {
		year = '20' + jpvc.substring(0, 2);
	}
	return year;
}


// Get seq from 'jpvc'
function getSeqFromJPVC(jpvc) {
	var hyphen = jpvc.indexOf("-");
	return jpvc.substring(hyphen + 1, jpvc.length);
}


function openWin(mode, element, title, iframeNm, iframeFm, iframeHd, sValue) {
	var x,y,w,h;
	var WinMode = "scrollbars=no,toolbar=no,location=no,status=no,menubar=no";

	if (document.layers){ document.captureEvents(Event.MOUSEMOVE) }
	x = (document.layers) ? loc.pageX : event.clientX;
	y = (document.layers) ? loc.pageY : event.clientY;

	w = 280; h = 200 + 40;

	if (screen.availwidth < event.clientX + w) { 
		x = event.clientX - (w + 20);
	} else { x = x + 20 }

	if (screen.availheight < event.clientY + h) { 
		y = event.clientY - (screen.availheight - event.clientY);
		if (y < 100) { y = 100; }
	} else { y = y + 80 }

	WinMode = WinMode + ",width="+w+",height="+h+",left="+x+",top="+y;

	var TargetDir = "../Template/Win_List.jsp?TheMode=" + mode + "&TheElm=" + element + "&TheTtl=" + title + "&TheIFrm=" + iframeNm + "&TheIFrmFrm=" + iframeFm
					+ "&TheIFrmHd=" + iframeHd + "&sValue=" + sValue;

	if (parent.window.closed != true) {
		D_Window = window.open(TargetDir, "OpenList", WinMode);
		D_Window.focus();
	} else parent.window.focus();
	
}


