// JavaScript Document
var InvalidForm = Class.create();
InvalidForm.prototype = {
  initialize: function() {
	  if (arguments!=null){
		  if (arguments[0]!=null){
        this.msg = arguments[0].evalJSON();
			}
		}
  },
	init: function(msg){
	  this.msg = msg.evalJSON();
	},
	startInvalid: function (){
		for (var i = 0 ; i<this.msg.length;i++){
			var f = this.getObject(this.msg[i].id);
			var t = this._getFormType(f);
			if (t=='text' || t=='textarea'){
				f.value = f.value.strip();
				if (f.value==''){
					alert(this.msg[i].msg);
					f.focus();
					return false;
				}else if (f.isEmail){
					if (!this.checkEmail(f.value)){
						alert(this.msg[i].msgEmail);
						f.focus();
						return false;
					}
				}else if(f.isPhone){
					
				}else if(f.isFax){
					
				}
			}else if (t=='checkbox' || t=='radio'){
				if (!this.checkSelected(f)){
					alert(this.msg[i].msg);
					this.getFocus(f);
					return false;
				}
			}
		}
		return true;
	},
  getObject:function(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=this.getObject(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n); return x;
	},
	_getFormType:function(obj){
		if (obj != null){
			if(obj.length==null){
				return obj.type;
			}else{
				return obj[0].type;
			}
		}
	},
	_getKeyCode: function(e){
		var code;
		if (!e) var e = window.event;
		if (e.keyCode){
			code = e.keyCode;
		}else if (e.which){
			code = e.which;
		}
		return code;
	},
	//-2.36onkeypress
	checkFloat:function(obj,e)	{
		var code = this._getKeyCode(e);
		if ((code<45 || code>57 || code==47) && code!=45 && code!=9 && code!=8) return false;
		if(code==46 && obj.value.indexOf(".")>-1) return false;
		return true;
	},	
	//325onkeypress
	checkInt: function(obj,e)	{
		var code = this._getKeyCode(e);
		if ((code<48 || code>57) && code!=45 && code!=9 && code!=8) return false;
		return true;
	},

	checkEmail:function (mail) { 
		return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(mail)); 
	},
	
	checkExt:function(path,extList){
    var ext = this.getFileExt(path);
    extList = extList.toUpperCase();
    var cExt = extList.indexOf("," + ext + ",");
    if (ext == "") return -1;
    if (cExt==-1){
			return 0;
    }else{
			return 1;
    }
  },
	selectText:function(obj){
		if (obj.type=='text' || obj.type=='textarea'){
			obj.select();
		}
	},
	getFullName:function (path){
		var tmp = path;
		var pos = -1;
		pos = tmp.lastIndexOf("\\");
		if (pos==-1){
			pos = tmp.lastIndexOf("/");
		}
		if (pos==-1){
			return path;
		}else{
			return tmp.substring(pos+1);
		}
	},
	getFileExt:function(path){
    var tmp = path;
    tmp = tmp.substring(tmp.lastIndexOf(".")+1);
    return tmp.toUpperCase();
  },
	formatNumber:function(as_str,ai_digit,as_type){
		var fdb_tmp = 0;
		var fi_digit = 0;
		var fs_digit = "1";
		var fs_str = "" + as_str;
		var fs_tmp1 = "";
		var fs_tmp2 = "";
		var fi_pos = 0;
		var fi_len = 0;
		fdb_tmp = parseFloat(isNaN(parseFloat(fs_str))?0:fs_str);
		
		switch (true) {
			case (ai_digit==null)://
				fdb_tmp = fdb_tmp;
				break;
			case (ai_digit==0)://
				fdb_tmp = Math.round(fdb_tmp);
				break;
			case (ai_digit>0)://
				for (var i=0;i<ai_digit;i++) fs_digit +="0";
				fi_digit = parseInt(fs_digit);
				fdb_tmp = Math.round(fdb_tmp * fi_digit) / fi_digit;
				if (as_type=="str"){
					fs_tmp1 = fdb_tmp.toString();
					fs_tmp1 +=((fs_tmp1.indexOf(".")!=-1)?"":".") + fs_digit.substr(1);
					fi_pos = fs_tmp1.indexOf(".") + 1 + ai_digit;
					fdb_tmp = fs_tmp1.substr(0,fi_pos);
				}
				break;
		}
		return fdb_tmp;
	},
	clearSelected:function(obj,index){
		if (obj != null){
			if(obj.length==null){
				if (index==-1) obj.checked = false;
			}else{
				for (i = 0 ; i< obj.length ; i++){
					if (index!=-1 && index==i){
						obj[i].checked = true;
					}else{
						obj[i].checked = false;
					}
				}
			}
		}
	},
	selectedAll:function(obj){
		if (obj != null){
			if(obj.length==null){
				obj.checked = true;
			}else{
				for (i = 0 ; i< obj.length ; i++){
					obj[i].checked = true;
				}
			}
		}
	},
	getFocus:function(obj){
		if (obj != null){
			if(obj.length==null){
				obj.focus();
			}else{
				obj[0].focus();
			}
		}	
	},
	checkSelected:function(obj){
		var ischeck = false;
		if (obj != null){
			if(obj.length==null){
				if (obj.checked == true) ischeck = true;
			}else{
				for (i = 0 ; i< obj.length ; i++){
					if (obj[i].checked == true){ischeck = true;break;}
				}
			}
		}
		return ischeck;
	},
	setSelectOption:function(ddl,v){
		for (var i = 0 ; i<ddl.options.length;i++){
			if (ddl.options[i].value==v){
				ddl.options[i].selected = true;
				break;
			}
		}
	}
}

var objInvalid = new InvalidForm();


function InputOtherInfo(objSel,inputFormId){
	var objInputForm = getObject(inputFormId);
	objInputForm.style.display = ((objSel.value=="other")?"":"none");
	if (objSel.value=="other"){	objInputForm.focus();}
}
