/**
 * @desc		Validate Check Common
 * @author		nettop.
 * @version		1.00. 
 * @since		Created at 2006-08-23 by nettop.
 */

Uhsoft_JS_Validate = function(){	this.isCheckVars = true; };

/**
 patten
 	¼ýÀÚ~¼ýÀÚ 	= ±ÛÀÚ¼ö from ~ to
 
 	regNum 			= ¼ýÀÚ¸¸
 	regNumDash	= ¼ýÀÚ + -¸¸
	regPhone 		= ÀüÈ­¹øÈ£
	regMail 		= ¸ÞÀÏ
	regAlpha		= ¿µ¹®¸¸
	regAlphaUpper	= ¿µ¹®´ë¹®ÀÚ¸¸
	regAlphaUpperNum	= ¿µ¹®´ë¹®ÀÚ¼ýÀÚ¸¸
	regAlphaNum		= ¿µ¹®¼ýÀÚ¸¸
 	regHangulEng 	= ÇÑ±Û¿µ¹®¸¸
 	regHangulOnly 	= ÇÑ±Û¸¸
 	regId			= ID(6~12ÀÚ¸¸,Ã¹±ÛÀÚ¿µ¹®, ¿µ¹®¼ýÀÚ¸¸, guest, admin, root, master ¾ÈµÇ°Ô )
 	regDate 		= ³¯Â¥
	regHost 		= Host
	regDomain 		= Domain

	isFileCheck		= ÆÄÀÏÃ¼Å©(jsp, asp, php, aspx, html, htm, cgi, js È®ÀåÀÚ ¾ÈµÇ°Ô)

	isImageFile		= ÀÌ¹ÌÁö ÆÄÀÏ¸¸
	isMultiFile		= isMultiFile
	isNvl				= ÇÊ¼ö»çÇ×
**/
// write Check Form °øÅë
Uhsoft_JS_Validate.prototype.checkPattenThisForm = function(thisForm, chkname, patten, alertMsg) {

	var checkObj;
	var checkObjValue;
	var checkObjtype;
	eval( "checkObj = thisForm." + chkname)

	if( !thisForm ){
		this.MessAge("°³¹ßÀÚ. thisForm È®ÀÎ ¿ä¸Á.");
		this.isCheckVars = false;
		return;
	}
	if(this.isNvl(chkname)){
		this.MessAge("°³¹ßÀÚ. chkname È®ÀÎ ¿ä¸Á.");
		this.isCheckVars = false;
		return;
	}
	if( !checkObj ){
		this.MessAge("°³¹ßÀÚ. chkname È®ÀÎ ¿ä¸Á." + chkname);
		this.isCheckVars = false;
		return;
	}

	if(this.isNvl(patten)){
		this.MessAge("°³¹ßÀÚ. patten È®ÀÎ ¿ä¸Á.");
		this.isCheckVars = false;
		return;
	}

	if(this.isNvl(alertMsg)){
		this.MessAge("°³¹ßÀÚ. alertMsg È®ÀÎ ¿ä¸Á.");
		this.isCheckVars = false;
		return;
	}	

	if( !this.isCheckVars ){
		return;
	}	

	if(checkObj.length == null){ // ¶óµð¿À¹öÆ°ÀÌ °°Àº name À¸·Î ÇÏ³ª¹Û¿¡ ¾ø´Ù¸é
		checkObjtype = checkObj.type;
	}else{
		checkObjtype = checkObj[0].type;
		if(checkObjtype == null){
			checkObjtype = checkObj.type;
		}
	}

	if(checkObjtype == "radio"){
		checkObjValue = this.radioObjValue( checkObj );
	}else if(checkObjtype == "select-one"){
		checkObjValue = checkObj.value;
		checkObjValue = this.strRpc(checkObjValue, "0", "");
	}else if(checkObjtype == "checkbox"){
		checkObjValue = this.radioObjValue( checkObj );
	}else if(checkObjtype == "hidden"){
		checkObjValue = checkObj.value;
		checkObjValue = this.strRpc(checkObjValue, "¡×", "");
	}else{
		checkObjValue = checkObj.value;
	}

	if(this.isCheckVars){


		if( this.isNumberMul(patten) ){
			 if( ( this.checkStr(checkObjValue) < this.fSplit(patten, 0) ) || ( this.checkStr(checkObjValue) > this.fSplit(patten, 1) ) ){
				this.MessAge(alertMsg);
				try{checkObj.focus();}catch(e){/*alert(e);*/}
				this.isCheckVars = false;
				return;
			 }
		}else{

			if( this.isNvl(checkObjValue) ){
			
				if(patten == "isNvl"){
					this.MessAge(alertMsg);
					try{checkObj.focus();}catch(e){/*alert(e);*/}
					this.isCheckVars = false;
					return;
				}
				
			}else{
	

				if(patten == "isImageFile"){

					if( !this.isImageFile(checkObjValue) ){
						this.MessAge(alertMsg);
						try{checkObj.focus();}catch(e){/*alert(e);*/}
						this.isCheckVars = false;
						return;
					}
				}else if(patten == "isMultiFile"){
					if( !this.isMultiFile(checkObjValue) ){
						this.MessAge(alertMsg);
						try{checkObj.focus();}catch(e){/*alert(e);*/}
						this.isCheckVars = false;
						return;
					}
				}else if(patten == "isFileCheck"){
					if( !this.isFileCheck(checkObjValue) ){
						this.MessAge(alertMsg);
						try{checkObj.focus();}catch(e){/*alert(e);*/}
						this.isCheckVars = false;
						return;
					}
				}else if(patten != "isNvl"){
				
					if( !this.isPatten(checkObjValue, patten)  ){
						this.MessAge(alertMsg);
						try{checkObj.focus();}catch(e){/*alert(e);*/}
						this.isCheckVars = false;
						return;
					}


					if(patten == "regId"){
						if( !this.isIdCheckBelong(checkObjValue) ){
							this.MessAge("guest, root, admin, master Àº ¾µ¼ö ¾ø´Â È¸¿øID ÀÔ´Ï´Ù. \n \n" + alertMsg);
							try{checkObj.focus();}catch(e){/*alert(e);*/}
							this.isCheckVars = false;
							return;
						}
					}


				}
			
			}	
		
		}
		
	}
};




/**
 inputValue°¡ patten °ú ¸ÂÀ¸¸é true return.
 patten
 	regNum 			= ¼ýÀÚ¸¸
 	regNumDash	= ¼ýÀÚ + -¸¸ 	
	regPhone 		= ÀüÈ­¹øÈ£
	regMail 		= ¸ÞÀÏ
	regAlpha		= ¿µ¹®¸¸
	regAlphaUpper	= ¿µ¹®´ë¹®ÀÚ¸¸
	regAlphaUpperNum	= ¿µ¹®´ë¹®ÀÚ¼ýÀÚ¸¸
	regAlphaNum		= ¿µ¹®¼ýÀÚ¸¸
 	regHangulEng 	= ÇÑ±Û¿µ¹®¸¸
 	regHangulOnly 	= ÇÑ±Û¸¸
 	regId			= ID
 	regDate 		= ³¯Â¥
	regHost 		= Host
	regDomain 		= Domain

	regImage		= ¿µ¹®¼ýÀÚ-_¸¸(ÀÌ¹ÌÁöÆÄÀÏ¸í ±ÔÄ¢)
**/
Uhsoft_JS_Validate.prototype.isPatten = function(inputValue, patten){ 
	var regNum 			=/^[0-9]+$/; 
	var regNumDash	=/^[0-9-]+$/; 	
	var regPhone 		=/^[0-9]{2,3}-[0-9]{3,4}-[0-9]{4}$/; 
//	var regMail 		=/^[_a-zA-Z0-9-]+@[._a-zA-Z0-9-]+.[a-zA-Z]+$/; 
    var regMail			= /([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)\.([0-9a-zA-Z_-]+)/;
	var regDomain 		=/^[.a-zA-Z0-9-]+.[a-zA-Z]+$/;
	var regAlpha		=/^[a-zA-Z]+$/; 
	var regAlphaUpper	=/^[A-Z]+$/; 
	var regAlphaUpperNum=/^[0-9A-Z]+$/;
	var regAlphaNum		=/^[0-9a-zA-Z]+$/;
	var regHost 		=/^[a-zA-Z-]+$/; 
	var regHangulEng 	=/^[°¡-ÆRa-zA-Z]+$/; 
	var regHangulOnly 	=/^[°¡-ÆR]*$/; 
	var regId			=/^[a-zA-Z]{1}[a-zA-Z0-9_-]{5,12}$/; 
	var regDate 		=/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/; 
	var regImage 		=/^[_0-9a-zA-Z]+$/; 

	patten = eval(patten); 
	if(!patten.test(inputValue)){ 
		return false; 
	} 
	return true; 
};

// id check ½Ã guest, admin, root, master °ªÀÌ ¾øÀ¸¸é true return
Uhsoft_JS_Validate.prototype.isIdCheckBelong = function (inputValue){

	if( this.strBelong(inputValue, "guest") ) {
		return false;
	}else if( this.strBelong(inputValue, "admin") ) {
		return false;
	}else if( this.strBelong(inputValue, "root") ) {
		return false;
	}else if( this.strBelong(inputValue, "master") ) {
		return false;
	}else{
		return true;        
	}

};

// inputValue°¡ Null or '' °ªÀÌ¸é true return
Uhsoft_JS_Validate.prototype.isNvl = function (inputValue){
	if( this.checkStr(inputValue, " ", "") == 0 ){
		return true;        
	}else{
		return false;        
	}
};



// isFileCheck ÆÄÀÏÃ¼Å©(jsp, asp,php, html, htm, cgi, js È®ÀåÀÚ ¾øÀ¸¸é true return)
Uhsoft_JS_Validate.prototype.isFileCheck = function (inputFileValue){
  var extFile = inputFileValue.split("\\");
  var FileInfo = extFile[extFile.length-1];
  var ext = FileInfo.split(".");
    if (  ext[1].toUpperCase() == "JSP" || ext[1].toUpperCase() == "ASP" || ext[1].toUpperCase() == "PHP" || ext[1].toUpperCase() == "ASPX" || ext[1].toUpperCase() == "HTML" || ext[1].toUpperCase() == "HTM" || ext[1].toUpperCase() == "CGI" || ext[1].toUpperCase() == "JS"   ){
        return false;
	}else{
      return true;
	}
}



// ImageFileValue°¡ ÀÌ¹ÌÁö ÆÄÀÏÀÌ¸é true
Uhsoft_JS_Validate.prototype.isImageFile = function (ImageFileValue){
  var extFile = ImageFileValue.split("\\");
  var ImgInfo = extFile[extFile.length-1];
  var ext = ImgInfo.split(".");
    if (ext[1].toUpperCase() == "JPG" || ext[1].toUpperCase() == "GIF"){
		
      if (this.isPatten(ext[0], "regImage")){
        return true;
      }else{
        return false;
      }
	  
	}else
      return false;
}


// checkFileValue°¡ ¹Ìµð¾î ÆÄÀÏÀÌ¸é true
Uhsoft_JS_Validate.prototype.isMultiFile = function (checkFileValue){
  var extFile = checkFileValue.split("\\");
  var ImgInfo = extFile[extFile.length-1];
  var ext = ImgInfo.split(".");
    if (ext[1].toUpperCase() == "ASF" || ext[1].toUpperCase() == "AVI" || ext[1].toUpperCase() == "WMV"){
      return true;
    }else
      return false;
}

//ÁÖ¹Î¹øÈ£ ¸ÂÀ¸¸é true return
Uhsoft_JS_Validate.prototype.isRegNum = function(regNum1,regNum2) {
	if(regNum1.length != 6 || regNum2.length != 7 || isNaN(regNum1) || isNaN(regNum2))	{			
		return false;
	}
	var sum = 0;
	var j = 2;
	var resno = regNum1 + regNum2;
	for(var i = 0;i < 12;i++){
		sum = sum + parseInt(resno.charAt(i)) * (j++);
		if(j == 10)				j = 2;
	}
	var re = 11 - sum % 11;
	if(re > 9 && (re - 10) != parseInt(resno.charAt(12))) {	
		return false;
	}	
	if(re < 9 && re != parseInt(resno.charAt(12))){
		return false;
	}
	return true;
};




// ¾²Áö ¸»°Í.....
// ¸Þ½ÃÁö º¸ÀÌ±â °øÅë
Uhsoft_JS_Validate.prototype.MessAge = function(massage) {
	alert(massage);
};


// ¾²Áö ¸»°Í.....
// String replace
Uhsoft_JS_Validate.prototype.strRpc = function (in_str,str1,str2){
	var r_str = "/"+str1+"/gi" ;
	return in_str.replace(eval(r_str),str2);
};
// ¾²Áö ¸»°Í.....
// String Belong
Uhsoft_JS_Validate.prototype.strBelong = function (orgStr, belongStr){

	try{
		if (orgStr.toUpperCase().indexOf(belongStr.toUpperCase()) == -1){
			return false;
		}else{
			return true;
		}	
	}catch(e){
		return false;	
	}

};

// ¾²Áö ¸»°Í.....
// objÀÇ °ª return
Uhsoft_JS_Validate.prototype.radioObjValue = function (obj){
	var radioObj = obj;
	var retValue = "";
	if(radioObj.length == null){ // ¶óµð¿À¹öÆ°ÀÌ °°Àº name À¸·Î ÇÏ³ª¹Û¿¡ ¾ø´Ù¸é
		if(radioObj.checked){
			retValue = radioObj.value;
		}
	}else{ // ¶óµð¿À ¹öÆ°ÀÌ °°Àº name À¸·Î ¿©·¯°³ ÀÖ´Ù¸é
		for(i=0; i<radioObj.length; i++){
			if(radioObj[i].checked){
				retValue = radioObj[i].value;
				break;
			}
		}
	}
	return retValue;
};

// ¾²Áö ¸»°Í.....
// inputValueÀÇ byte ±æÀÌ
Uhsoft_JS_Validate.prototype.getByteLength = function(inputValue) {
	return (inputValue.length + (escape(inputValue) + "/%u").match(/%u/g).length-1);
};



// ¾²Áö ¸»°Í.....
// inputValue °¡ ¼ýÀÚ¿Í "-"  ·Î¸¸ µÇ¾î ÀÖÀ¸¸é true
Uhsoft_JS_Validate.prototype.isNumberDash = function (inputValue){
    var chars = "-0123456789";
    return this.containsCharsOnly(inputValue,chars);
};
// ¾²Áö ¸»°Í.....
// inputValue °¡ ¼ýÀÚ¿Í "~"  ·Î¸¸ µÇ¾î ÀÖÀ¸¸é true
Uhsoft_JS_Validate.prototype.isNumberMul = function (inputValue){
    var chars = "~0123456789";
    return this.containsCharsOnly(inputValue,chars);
};

// ¾²Áö ¸»°Í.....
/**
 * ÀÔ·Â°ªÀÌ Æ¯Á¤ ¹®ÀÚ(chars)¸¸À¸·Î µÇ¾îÀÖ´ÂÁö Ã¼Å©

 * Æ¯Á¤ ¹®ÀÚ¸¸ Çã¿ëÇÏ·Á ÇÒ ¶§ »ç¿ë
 * ex) if (!containsCharsOnly(form.blood,"ABO")) {
 *         alert("Ç÷¾×Çü ÇÊµå¿¡´Â A,B,O ¹®ÀÚ¸¸ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.");
 *     }
 */
Uhsoft_JS_Validate.prototype.containsCharsOnly = function(inputValue,chars) {
    for (var inx = 0; inx < inputValue.length; inx++) {
       if (chars.indexOf(inputValue.charAt(inx)) == -1)
           return false;
    }
    return true;
}

// ¾²Áö ¸»°Í.....
// strFindÀ» strChange¹Ù²Ù°í ±æÀÌ(byte) ¹ÝÈ¯
Uhsoft_JS_Validate.prototype.checkStr = function (strOriginal, strFind, strChange){ 
	var position, strOri_Length; 
	strOriginal = strOriginal + "";
	position = strOriginal.indexOf(strFind);   
	while (position != -1){ 
		strOriginal = strOriginal.replace(strFind, strChange); 
		position = strOriginal.indexOf(strFind); 
	} 
	strOri_Length = this.getByteLength( strOriginal ); 
	return strOri_Length; 
};
Uhsoft_JS_Validate.prototype.fSplit = function (string_val, i){
	try{
		var retVal = string_val.split('~')[i];
		if(retVal){
			return retVal;
		}
		return "";
	} catch (e){
		//alert (e);
		return "";
	}
};
var UhsoftJsValidate = new Uhsoft_JS_Validate();