﻿
var isIE = /msie/i.test(navigator.userAgent);
String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
}
if(!isIE){
	Event.prototype.__defineGetter__("srcElement",function(){
		var node=this.target;
		while(node.nodeType!=1)node=node.parentNode;
		return node;
	});
	HTMLElement.prototype.attachEvent=function(sType,foo){
		this.addEventListener(sType.slice(2),foo,false);
	}
	HTMLElement.prototype.__defineGetter__("innerText",function(){
		return this.textContent;
	});
	HTMLElement.prototype.__defineSetter__("innerText",function(str){
		this.textContent = str;
	});
	HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){
		switch(where){
			case "beforeBegin":
                this.parentNode.insertBefore(parsedNode,this);
                break;
            case "afterBegin":
                this.insertBefore(parsedNode,this.firstChild);
                break;
            case "beforeEnd":
                this.appendChild(parsedNode);
                break;
            case "afterEnd":
                if(this.nextSibling)
                    this.parentNode.insertBefore(parsedNode,this.nextSibling);
                else
                    this.parentNode.appendChild(parsedNode);
                break;
           
                
		}
	}
    HTMLElement.prototype.insertAdjacentHTML=function(where,htmlStr){
        var r=this.ownerDocument.createRange();
        r.setStartBefore(this);
        var parsedHTML=r.createContextualFragment(htmlStr);
        this.insertAdjacentElement(where,parsedHTML);
	}

}
else document.execCommand("BackgroundImageCache",false,true);
function $(id){return (typeof id == "string" ? document.getElementById(id) : id);}
function $N(name){return document.getElementsByName(name);}
function $TN(name,root){return root ? $(root).getElementsByTagName(name) : document.getElementsByTagName(name);}
function $F(id){return exist(id) ? $(id).value.trim() : null;}
function $IH(id,s){$(id).innerHTML = s;}
function $IT(id,s){$(id).innerText = s;}
function $iF(id,s){$(id).value = s;}
function $DC(name){return document.createElement(name);}
function isEmpty(str){return str.replace(/(?:null)|(?:undefined)/i,"").length == 0;}
function exist(id){return $(id)!= null;}
function hide(id){if(exist(id))$(id).style.display="none";}
function show(id){if(exist(id))$(id).style.display="";}
function nextSibling(id)
{

return (($(id).nextSibling.nodeName == "#text" && $(id).nextSibling.Value == "") ? $(id).nextSibling.nextSibling : $(id).nextSibling);}
function removeNode(id){
	if(exist(id)){
		$(id).parentNode.removeChild($(id));
	}
}

JValidator = function (form) {
	var form = $(form);
	var elements = form.elements;
	checkOne = function (obj){
		var pass = true;
		var tip = $DC("span");
		var same = obj.getAttribute("same");
		var selectone = obj.getAttribute("selectone");
		if(selectone != null)selectone = $F(selectone);
		var pattern = obj.getAttribute("pattern");
		var msg = obj.getAttribute("msg") != null ? obj.getAttribute("msg") : " ";
		var required = obj.getAttribute("required") != "no";
		var span = nextSibling(obj);
		
		var checked = span != null ? span.nodeName == "SPAN" : false;
		if(same != null)same = $F(same);
		obj.value = $F(obj);
		tip.style.padding = "1px 1px 1px 20px";
		 
		if(checked) removeNode(span);
		
		if(selectone!=null){
		
		  if(selectone=="" && obj.value==""){
		       with (tip.style){
					border = "1px dashed #eef7fd";
					background = "#B6DBEB url(http://www.bejapan.com/images/error.gif) no-repeat 1px -104px";
				}
				pass = false;
		    	}
			else {
				tip.style.background = "url(http://www.bejapan.com/images/error.gif) no-repeat 1px -8px";
				msg = " ";
			    }
			    $IT(tip,msg);
			obj.insertAdjacentElement("afterEnd",tip);
			obj.focus();
		
		}
		else if(same != null){
		
			if(same != obj.value){
				with (tip.style){
					border = "1px dashed #eef7fd";
					background = "#B6DBEB url(http://www.bejapan.com/images/error.gif) no-repeat 1px -104px";
				}
				pass = false;
			}
			else {
				tip.style.background = "url(http://www.bejapan.com/images/error.gif) no-repeat 1px -8px";
				msg = " ";
			}
			$IT(tip,msg);
			obj.insertAdjacentElement("afterEnd",tip);
			obj.focus();
			
		}
		else if(pattern != null){
			pattern = new RegExp(pattern);
			if(required || obj.value.length > 0){
			
				if(!pattern.test(obj.value)){
				
					with (tip.style){
						border = "1px dashed #eef7fd ";
						background = "#B6DBEB url(http://www.bejapan.com/images/error.gif) no-repeat 1px -104px";
					}
					pass = false;
				}
				else {
				
					tip.style.background = "url(http://www.bejapan.com/images/error.gif) no-repeat 1px -8px";
					msg = " ";
				}
				$IT(tip,msg);
				if(obj.getAttribute("errid")!=null)
				{
				   
			        var divID= obj.getAttribute("errid");
			       
			        $(divID).style.display = "block";
			        for(var i = 0 ;i<$(divID).childNodes.length;i++)
			        {
			            $(divID).removeChild($(divID).childNodes[i]);
			        }
			       
				    $(divID).appendChild(tip);
				}
				else
				{
				    obj.insertAdjacentElement("afterEnd",tip);
				    obj.focus();
				}
				
				   
			}
		}
		return pass;
	}
	this.checkScope = function(id){
		if(!$(id)) return true;
		var pass = true;
		var elements = new Array();
		elements[0] = $TN("input",$(id));
		elements[1] = $TN("select",$(id));
		elements[2] = $TN("textarea",$(id));
		for(var i = 0; i < elements.length; i++){
			for(var j = 0; j< elements[i].length; j++) {
				if(!checkOne(elements[i][j]) && pass) pass = false;
			}
		}
		return pass;
	}
	this.check = function(){
        
		var pass = true;
	
		for(var i = 0; i < elements.length; i++){
		
			if(elements[i].nodeName != "BUTTON" && elements[i].nodeName != "FIELDSET" && !checkOne(elements[i]) && pass)
			{
			     
			        
			         
			         pass = false;
			         return pass;
			         break;
			}
	
		}
		
		
		//RadioButtonList1
		
		
		
		return pass;
	}
	
	for(var i = 0; i < elements.length; i++){
		if(elements[i].getAttribute("pattern") != null || elements[i].getAttribute("same") != null){
			elements[i].attachEvent("onchange",function(event){checkOne(event.srcElement);});
		}
	}
}
