﻿//各種拡張

String.prototype.trim=function(){
        return this.replace(/^[ 　]+|[ 　]+$/g, '');
    }



//////// 指定位置のアイテムを除去 //////////////////////////////
Array.prototype.removeAt = function(idx) {
    if (idx < 0) {return;}
    this.splice(idx, 1);
}
//////// 指定位置にアイテムを挿入 //////////////////////////////
Array.prototype.insertAt = function(idx, value) {
    if (idx < 0) {return;}
    this.splice(idx, 0, value);
}

//////// オブジェクトリストのプロパティ検索 ////////////////////////

Array.prototype.indexOfName=function(name,key){
    for(var i=0,j=this.length;i<j;i++){
        if(this[i][name]==key){
            return i;
        }
    
    }
    return -1;
}

//////// オブジェクトリストの特定プロパティArray摘出 ////////////////////////

Array.prototype.propertyArray=function(name){
    var rtn=new Array();
    for(var i=0,j=this.length;i<j;i++){
        rtn.push(this[i][name]);
    }
    return rtn;
}


ElementUtil={
    getOffsetRect:function(element){
        var el=element;
        var w=el.offsetWidth;
        var h=el.offsetHeight;
        var x=el.offsetLeft;
        var y=el.offsetTop;
        while(el.offsetParent!=null){
            el=el.offsetParent;
            x+=el.offsetLeft;
            y+=el.offsetTop;
        }
        return {x:x,y:y,width:w,height:h};
    },
    getWinRect:function(){
	        	
    	//中心位置移動
    	var winWidth=window.innerWidth;
        var winHeight=window.innerHeight;
        if (winWidth==undefined) { 
		    winWidth = document.documentElement.clientWidth;
	    }
	    
        if (winHeight==undefined) { 
		    winHeight = document.documentElement.clientHeight;
	    }	
    	
    	
        var winTop,winLeft;
        if (self.pageYOffset) {
		    winTop = self.pageYOffset;
		    winLeft = self.pageXOffset;
	    } else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		    winTop = document.documentElement.scrollTop;
		    winLeft = document.documentElement.scrollLeft;
	    } else if (document.body) {// all other Explorers
		    winTop = document.body.scrollTop;
		    winLeft = document.body.scrollLeft;	
	    }
    	
        return {x:winLeft,y:winTop,width:winWidth,height:winHeight};        
   
   },
   getDocSize:function(){
       var w= document.documentElement.scrollWidth || document.body.scrollWidth;
       var h= document.documentElement.scrollHeight || document.body.scrollHeight;
        return {width:w,height:h};
   },
   scrollTo:function(y){
       var win=this.getWinRect();
       var doc=this.getDocSize();
       
       if(y>doc.height-win.height){
            y=doc.height-win.height;
       }   
       this.scrollToCore(y);
   },
   scrollToCore:function(y){
        win=this.getWinRect();
		
		var ty=win.y + (y-win.y)/2;
        
        window.scrollTo(0,ty);
        
        win=this.getWinRect();
	    if(Math.abs(ty-win.y)<1 && Math.abs(ty-y)>1){
            setTimeout(this.scrollToCore.bind(this,y),50);    
	    }else{
	        window.scrollTo(0,y);    
	    }
	    
        
   }


}


XMLUtil={
    childData:function(node,nodename){
        var rtn="";
        var els = node.getElementsByTagName(nodename);
        if(els.length>0){
           var cld=els[0].firstChild;
           while(cld!=null){
             rtn+=cld.nodeValue;
             cld=cld.nextSibling;
           }
        }
        return rtn;
        
    },
    tagChilds:function(node,tagname){
        //指定ノード配下の指定タグ一覧を取得
       var rtn=new Array();
       var cld=node.firstChild;
       
       while(cld!=null){
         if(cld.tagName==tagname){
            rtn.push(cld);
         }
         cld=cld.nextSibling;
       }

        return rtn;
        
    },
    xmlEncode:function(s){
        var text=s.replace(/&/g, '&amp;');
        text=text.replace(/"/g, '&quot;');
        text=text.replace(/'/g, "&apos;");
        text=text.replace(/</g, '&lt;');
        text=text.replace(/>/g, '&gt;');
        
        return text;   
    },
    htmlEncodeWithBR:function(s){
    var text=s.replace(/&/g, '&amp;');
        text=text.replace(/"/g, '&quot;');
        text=text.replace(/</g, '&lt;');
        text=text.replace(/>/g, '&gt;');
        text=text.replace(/\n/g, '<br/>');
        
        return text;   
    },
    htmlDecodeWithBR:function(s){
    var text=s.replace(/<br\/>/g, '\n');
        text=text.replace(/&gt;/g, '>');
        text=text.replace(/&lt;/g, '<');
        text=text.replace(/&quot;/g, '"');
        
        return text;   
    }
}


ElementCreator={    
    createPanel:function(panel,tag,id,classname,text){
    	var el = document.createElement(tag);
    	if(id!=null && id!=""){
    	    el.setAttribute("id",id);
    	}
    	if(classname!=null && classname!=""){
		    el.className=classname;
		}
		if(text != undefined){
		    el.innerHTML=text;
		}		
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	},
    createLink:function(panel,id,classname,text,img,url){
    	var el = document.createElement("a");
    	if(url==undefined){
    	    el.setAttribute("href","javascript:void(0);");
    	}else{
    	    el.setAttribute("href",url);
    	}
    	if(id!=null && id!=""){
    	    el.setAttribute("id",id);
    	}
    	if(classname!=null && classname!=""){
		    el.className=classname;
		}
		if(text != undefined){
		    if(img!=undefined && img!=null){
		        imgel = document.createElement("img");
		        imgel.setAttribute("src",img);
		        imgel.setAttribute("alt",text);
		        el.setAttribute("title",text);
		        el.appendChild(imgel);
		    }else{
		        el.innerHTML=text;
		    }
		    
		}
		panel.appendChild(el);
	    return el;	
	},
    createDataContainer:function(panel,title){
        var item=this.createItem(panel,"div","item");
        this.createItem(item,"div","title",title);
        var data=this.createItem(item,"div","data2");
        return data;
    },    
    createItem:function(panel,tag,classname,text){
    	var el = document.createElement(tag);
    	if(classname!=null && classname!=""){
		    el.className=classname;
		}
		if(text != undefined){
		    el.innerHTML=text;
		}		
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	},
    createForm:function(panel,id,action,method,enctype){
        var el = document.createElement("form");
    	if(id!=null && id!=""){
        	el.setAttribute("name",id);
    	    el.setAttribute("id",id);
    	}
    	if(method==undefined){method="post";}
    	if(enctype==undefined){enctype="multipart/form-data";}
    	
    	el.setAttribute("method",method);
    	el.setAttribute("action",action);
    	el.setAttribute("enctype",enctype);
    	
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	},
    createSubmit:function(panel,id,text){
        var el = document.createElement("input");
    	el.setAttribute("type","submit");
    	if(id!=null && id!=""){
        	el.setAttribute("name",id);
    	    el.setAttribute("id",id);
    	}
    	el.setAttribute("value",text);
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	},
    createText:function(panel,size,maxlength){
    	var el = document.createElement("input");
    	el.setAttribute("type","text");
    	el.setAttribute("size",size);
    	if(maxlength!=undefined && maxlength>0){
    	    el.setAttribute("maxLength",maxlength);
    	}
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	},
    createTextArea:function(panel,cols,rows){
    	var el = document.createElement("textarea");
    	el.setAttribute("cols",cols);
    	el.setAttribute("rows",rows);
    	el.style.width="95%";
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	},
	createButton:function(panel,text){
    	var el = document.createElement("input");
    	el.setAttribute("type","button");
    	el.setAttribute("value",text);
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	},
	createFile:function(panel){
    	var el = document.createElement("input");
    	el.setAttribute("type","file");
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	}, 
	createImage:function(panel,src,text){
    	var el=new Image();
        el.src=src;
        if(text != undefined){
    	    el.setAttribute("alt",text);
    	}
		if(panel!=null){
		    panel.appendChild(el);
		}
	    return el;	
	}, 
	createCheck:function(panel,id,text){
    	var el = document.createElement("input");
    	el.setAttribute("type","checkbox");
    	    	    	
		if(id!=undefined && id!=null){
		    el.setAttribute("id",id);
		    
		    if(panel!=null){
		        panel.appendChild(el);
		        if(text!=undefined){
		            var lbl=document.createElement("label");
		            if(id!=undefined && id!=null){
		                lbl.htmlFor =id;
		            }
		            lbl.innerHTML=text;
    		        panel.appendChild(lbl);
		        }
		    }
		
		}else{
		    
		    if(panel!=null){
    		    
		        if(text!=undefined){
                    var lblel=document.createElement("label");
		            panel.appendChild(lblel);
    	            lblel.appendChild(el);

                    var lbltxt=document.createElement("span");
	                lbltxt.innerHTML=text;
	                lblel.appendChild(lbltxt);


                    
		        }else{
		           panel.appendChild(el);
		        }
		    }
		
		}
		
		
		
	    return el;	
	}
}



