/*
 *****************************************************************************************************************
 * Project			: kotra b2b e-trade system
 * Folder			  :	/common/js
 * File Name		: comValid.js
 * Description  :	validation check javascript
 * Input Param  :
 * Output Param :
 * Include File	:
 * Using Table  :
 * Return Value :
 * Sub Function :
 * SE Name                               Description                                             Date
 * -----------------------------------   -----------------------------------------------------   -----------
 * laonhiker(laonhiker@laonsum.com)      Initial Create                                          2006.04.03
 *****************************************************************************************************************
 */

// delete
function sqeeze(str) {
	str = str.replace(/ /g, "");
	return str.length;
}

// check input empty
function checkEmpty(obj, msg) {
	if (typeof(obj)=="object") {
		if (obj.type == "select-one") {
			if (sqeeze(obj.options[obj.selectedIndex].value)>0) {
				return false;
			} else {
				alert(msg);
				obj.focus();
				return true;
			}
		} else if (typeof(obj.length)=="undefined") {
			if (obj.type=="text" || obj.type=="textarea" || obj.type=="password"){
				if (sqeeze(obj.value)>0) {
					return false;
				} else {
					alert(msg);
					obj.focus();
					return true;
				}
			} else if (obj.type=="radio" || obj.type=="checkbox"){
				if (obj.checked) {
					return false;
				} else {
					alert(msg);
					obj.focus();
					return true;
				}
			}
		} else {
			for (i=0; i<obj.length; i++) {
				if (obj[i].checked) {
					return false;
				}
			}
			alert(msg);
			obj[0].focus();
			return true;
		}
	} else {
		alert(msg);
	}
	return true;
}

// frameset move
function setSrcFrame(leftPage, viewPage) {
	parent.menu.location.href = leftPage;
	parent.view.location.href = viewPage;
}

// page location
function goLocate(str) {
	location.href = str;
	//parent.user_view.location.href = str;
}

function goLocation(str, param) {
	//parent.user_view.location.href = str+"?"+param;
	location.href = str+"?"+param;
}

function goAlertLocation(str, param, msg) {
	alert(msg);
	location.href = str+"?"+param;
	//parent.user_view.location.href = str+"?"+param;
}

// open poup
function popWin(strUrl, strWin, strLeft, strTop, strWidth, strHeight, strTool, strMenu, strStatus, strScroll, strResize) {
	var opt = "left="+strLeft+", top="+strTop+", width="+strWidth+", height="+strHeight+", toolbar="+strTool+", menubar="+strMenu+", status="+strStatus+", scrollbars="+strScroll+", resizable="+strResize;
	window.open(strUrl, strWin, opt);
}

//checkbox all select
function selectAllCheckBox(fm, objname, v) {
	var CheckCount = 0;
	var obj = fm.elements[objname];
	if (typeof(obj)=="object") {
		if (typeof(obj.length)=="undefined") {
			obj.checked=v;
		} else {
			for (i=0; i<obj.length; i++) {
				obj[i].checked=v;
			}
		}
	}
}

// number check
function isNumber(obj) {
	var str = obj.value;
	for(var i=0; i < str.length; i++) {
		if(!(('0' <= str.charAt(i) && str.charAt(i) <= '9') || str.charAt(i) == "-")) {
			alert("Please. Only Number Insert.");
			obj.focus();
			return true;
		}
	}
	return false;
}

// Price number check
function isNumberPrice(obj) {
	var str = obj.value;
	for(var i=0; i < str.length; i++) {
		if(!(('0' <= str.charAt(i) && str.charAt(i) <= '9') || str.charAt(i) == "-" || str.charAt(i) == ".")) {
			alert("Please. Only Number Insert.");
			obj.focus();
			return true;
		}
	}
	return false;
}

// number & dot check
function isDotNumber(obj) {
	var str = obj.value;
	for(var i=0; i < str.length; i++) {
		if(!(('0' <= str.charAt(i) && str.charAt(i) <= '9') || str.charAt(i) == ".")) {
			alert ("Check please, dot and numbers.");
			obj.value = "";
			obj.focus();
			return true;
		}
	}
	return false;
}


function download( pathType, fileName ){
	location.href = "/common/include/download.jsp?pathType=" + pathType + "&fileName=" + fileName;
}

function download( pathType, subDir, fileName ){
	location.href = "/common/include/download.jsp?pathType=" + pathType + "&fileName=" + fileName + "&subDir="+ subDir;
}

function jsProductView( productCode ){
	location.href = "/sub.jsp?pgNo=102&proCode=" + productCode;
}

function jsProfileView( userId ){
	popWin( "/102/popProfile.jsp?popId=" + userId, "popProfile", "250", "350", "617", "450", "no", "no", "no", "yes", "no" );
}

function bannerPop(url){
	popWin( url, "B2B e-Trade", "250", "350", "550", "500", "no", "no", "no", "yes", "no" );
}


function attachFileCheck(obj, msg) {
	var str = obj.value;
	var i = str.indexOf(".");
	if(i < 0) {
		alert( msg );
		obj.focus();
		return true;
	}
	if(str != "" ) {
		var str1 = str.split(".");
		var str2 = str1[str1.length-1];
		str2 = ToLower( str2 );
		if( str2 == "jsp" || str2 =="js" || str2 =="htm"|| str2=="php" || str2 == "css" || str2 == "html" ) {
			alert( msg );
			return true;
		} else {
			return false;
		}
	} else {
		alert( msg );
		return true;
	}
}


function attachFileCheckEmpty(obj, msg) {
	var str = obj.value;
	if( str != "" ){
		var str1 = str.split(".");
		var str2 = str1[str1.length-1];
		str2 = ToLower( str2 );
		if( str2 == "jsp" || str2 =="js" || str2 =="htm"|| str2=="php" || str2 == "css" || str2 == "html" ) {
			alert( msg );
		return true;
		} else {
			return false;
		}
	}
}

function ToLower( arg ){
     var str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     var str2 = "abcdefghijklmnopqrstuvwxyz";
     return alterString( arg, str1, str2 );
}

function alterString(str,before,after) {
	var returnStr = "";
	for( i = 0; i < str.length; i++) {
		value = str.charAt(i);
		index = before.indexOf(value);
		if( index >= 0 ) value = after.charAt(index);
		returnStr += value;
	}
	return returnStr;
}

function isEmail(obj, msg) {
	var str = obj.value;
	str = str.toUpperCase();
	if( str == "" ){
		alert( msg );
		obj.focus();
		return true;
	}
	for(var i=0; i < str.length; i++) {
		if(!(('A' <= str.charAt(i) && str.charAt(i) <= 'Z') || ('0' <= str.charAt(i) && str.charAt(i) <= '9') || ( str.charAt(i) == '@') || ( str.charAt(i) == '.') || ( str.charAt(i) == '_') || ( str.charAt(i) == '-'))) {
			alert("Email address is not valid.\n\rPlease check your email address.");
			obj.focus();
			return true;
		}
	}
	var i = str.indexOf("@");
	if(i < 0) {
		alert("Email address is not valid.\n\rPlease check your email address.");
		obj.focus();
		return true;
	}

	var j = str.indexOf(".");

	if(i < 0) {
		alert("Email address is not valid.\n\rPlease check your email address.");
		obj.focus();
		return true;
	}

	return false;
}

function jsLogin(){
	location.href = "/sub.jsp?pgNo=411";
}

function jsLogout(){
	location.href = "/sub.jsp?pgNo=415";
}


function formatAmount(str)
{
	fStr = "";
	dotIdx = str.indexOf(".");
	if(dotIdx<0) dotIdx=str.length;
	intStr = str.substr(0, dotIdx);
	cnt = 0;
	for(i=intStr.length-1; i>=0; i--)
	{
		if(cnt%3==0 && cnt>0) fStr=','+fStr;
		fStr = str.substr(i,1)+fStr;
		cnt++;
	}
	
	return fStr+str.substr(dotIdx);
}

function isId(obj) {
	var str = obj.value;
	if( str == "" ){
		alert("Please Enter Id.");
		obj.focus();
		return true;
	}
	for(var i=0; i < str.length; i++) {
		if(!(('a' <= str.charAt(i) && str.charAt(i) <= 'z') || ('0' <= str.charAt(i) && str.charAt(i) <= '9') || ( str.charAt(i) == '_') || ( str.charAt(i) == 'blank'))) {
			alert("ID is not valid.\n\rPlease check..(blank, symbol..).");
			obj.focus();
			return true;
		}
	}
	return false;
}