// JavaScript Document
/////////////////////////////////////////////////////////////////////////
/// 公共数据对象
/// String
String.prototype.left = function( nLCount ){
    return this.substring(0,nLCount);
};
String.prototype.right = function( nRCount ){
    return this.substring(this.length-nRCount,this.length);
};
String.prototype.mid = function( nStartI,nEndI ){
    return this.substring(nStartI,nEndI+1);
};
String.prototype.replaceAll = function( strKey,strValue ){
    return this.replace(new RegExp("["+strKey+"]","g"),strValue);
}
String.prototype.countOfChar = function( ch ){
	var count;
	var c,i;
	count = 0;
	c = this.length;
	for( i=0;i<c;i++ ){
		if( this.charAt(i)==ch ){
			count++;
		}
	}
	return count;
}
/// Array
Array.prototype.addItem = function( vValue ){
	this[this.length] = vValue;
};
Array.prototype.InsertItem = function( vValue,nAt ){
    if( nAt<this.length ){
        var nCOfItem;
	    nCOfItem = this.length-1;
	    this[nCOfItem+1] = this[nCOfItem];
	    for( nIOfItem=nCOfItem;nCOfItem>nAt;nCOfItem-- ){
	        this[nCOfItem] = this[nCOfItem-1];
	    }
		this[nAt] = vValue;
		return true;
	}
	return false;
};
Array.prototype.delByIndex = function( nIOfItem ){
   if( nIOfItem>=this.length ){
       return;
   }
////////////////////////////////////////////////////////////
   var nCOfItem = this.length;
   if( nIOfItem<nCOfItem-1 ){
       nCOfItem -= 1;
       for( ;nIOfItem<nCOfItem;nIOfItem++ ){
           this[nIOfItem] = this[nIOfItem+1];
       }
   }
   
   this.length -= 1;
};
// Array::DelByValue : nCOfDel 为-1时删除所有值相等，否则删除指定数量
Array.prototype.delByValueAndCount = function( vValue,nCOfDel ){
	var nCOfItem   = this.length-1;
	var nIOfItem   = 0;
	var nIOfNext   = 0;
	var nCOfHadDel = 0;
    if( nCOfDel<0 ){
		for( ;nIOfItem<nCOfItem; ){
		    if( g[nIOfItem]==vValue ){
			    nIOfNext    = nIOfItem+1
			    g[nIOfItem] = g[nIOfNext];
				for( ;nIOfNext<nCOfItem;nIOfNext++ ){
				    g[nIOfNext] = g[nIOfNext+1];
				}
				nCOfHadDel++;
			}
			else{
			    nIOfItem++;
			}
		}
	}
	else{
		for( ;nIOfItem<nCOfItem;nIOfItem++ ){
		    if( g[nIOfItem]==vValue || g[nIOfItem]==null ){
				 nIOfNext   = nIOfItem+1;
			    g[nIOfItem] = g[nIOfNext];
				for( ;nIOfNext<nCOfItem; ){
				    g[nIOfNext] = g[nIOfNext+1];
				}
				nCOfHadDel++;
				
				if( nCOfHadDel==nCOfDel ){
				    break;
				}
			}else{
			    nIOfNext++;
			}
		}
	}
	
	if( g[nIOfItem]==vValue ){
	    nCOfHadDel += 1;
	}
	
	this.length -= nCOfHadDel;
	
	return nCOfHadDel;
};
Array.prototype.delByValue = function( vValue ){
    this.delByValueAndCount( vValue,-1 );
};
Array.prototype.sortAscending = function(){
	;
};
Array.prototype.sortDescending = function(){
	;
};
function floatToString(f, nDecimalNumber){
	var s = f.toString();
	if( s.countOfChar('.')<1 ){
		return s+".00";
	}
	return s.left(s.indexOf(".")+3);
}
/////////////////////////////////////////////////////////////////////////
/// 公共控件处理
function funSetTextFocus(t){
	t.style.border = "1px solid #f00";
}
function funSetTextBlur(t){
	t.style.border = "1px solid #999";
}
function funDivDisplay(id){
	document.getElementById(id).style.display = "inline";
}
function funDivDisplayNone(id){
	document.getElementById(id).style.display = "none";
}
function funObjClass(obj, cName){
	obj.className = cName;
}
/////////////////////////////////////////////////////////////////////////
/// 公共元素操作方法
function funHideMenu(c, idMenu, e){
	var menu = document.getElementById(idMenu);
	if( menu==null ){
		return;
	}
	if( window.navigator.userAgent.indexOf("MSIE")!=-1 ){
		if( !c.contains(e.toElement) ){
			menu.style.display = "none";
		}
	}else{
		var objRelTarget;
		if( e.relatedTarget!=null ){
			objRelTarget = e.relatedTarget;
		}else{
			if( e.fromElement!=null ){
				objRelTarget = e.fromElement;
			}
			else if( e.toElement!=null ){
				objRelTarget = e.toElement;
			} 
		}
		while( objRelTarget!=null && objRelTarget!=c ){
			objRelTarget = objRelTarget.parentNode;
		}
		if( objRelTarget!=c ){
			menu.style.display = "none";
		}
	}
}
/////////////////////////////////////////////////////////////////////////
/// 公共底层方法
function getWindowScrollY(){
	if( window.scrollY ){
		return window.scrollY;
	}else{
		return document.documentElement.scrollTop;
	}
}
function addTableAnRow(idTable, idRowBeforeRow, idNewRow, arNewRowAttribute, arNewRowCellAttribute, arCellInnerHTML){
	var tab = document.getElementById(idTable);
	var trBefore = document.getElementById(idRowBeforeRow);
	var trNew = tab.insertRow(trBefore.rowIndex+1);
	var nIOfAttribute;
	
	trNew.setAttribute("id",idNewRow);
	if( arNewRowAttribute!=null &&
	arNewRowAttribute.length>0 ){
		for( nIOfAttribute=0;
			nIOfAttribute<arNewRowAttribute.length;nIOfAttribute++ ){
			trNew.setAttribute(arNewRowAttribute[nIOfAttribute][0],arNewRowAttribute[nIOfAttribute][1]);
		}
	}
	
	var nIOfCell;
	var tdCellNew;
	for( nIOfCell=0;
	nIOfCell<arCellInnerHTML.length;nIOfCell++ ){
		tdCellNew = trNew.insertCell(nIOfCell);
		
		if( arNewRowCellAttribute!=null &&
		arNewRowCellAttribute[nIOfCell]!=null ){
			for( nIOfAttribute=0;
			nIOfAttribute<arNewRowCellAttribute[nIOfCell].length;nIOfAttribute++ ){
				tdCellNew.setAttribute(arNewRowCellAttribute[nIOfCell][nIOfAttribute][0],arNewRowCellAttribute[nIOfCell][nIOfAttribute][1]);
			}
		}
		
		tdCellNew.innerHTML = arCellInnerHTML[nIOfCell];
	}
}
function delTableAnRow(idTable, idRow){
	var tab = document.getElementById(idTable);
	var row = document.getElementById(idRow);
	tab.deleteRow(row.rowIndex);
}
/////////////////////////////////////////////////////////////////////////
/// 公共操作方法
function copyToClipBoard(strContent){
	if( !window.clipboardData ){
		alert("您正在使用的浏览器不支持自动复制功能,请您手动复制内容.");
	}else{
		window.clipboardData.setData("Text",strContent);
		alert("复制成功,您可以在需要的地方\"粘贴\"已复制的内容了.");
	}
}
function openURL(strURL, strTarget){
	if( strTarget==null ||
	strTarget=="_blank" ){
		window.open(strURL);
	}else if( strTarget=="_self" ){
		window.location = strURL;
	}
}
function submitForm(method, action, target, agParam, agValue){
	var frm;
	var c,n;
	var hid;
	frm = document.createElement("form");
	frm.method = method;
	frm.action = action;
	frm.target = target;
	
	c = agParam.length;
	for( n=0;n<c;n++ ){
		hid = document.createElement("input");
		hid.type = "hidden";
		hid.name = agParam[n];
		hid.value = agValue[n];
		frm.appendChild(hid);
	}
	document.body.appendChild(frm);
	frm.submit();
	document.body.removeChild(frm);
}
function addToFavorite(strTitle, strURL){
	if( !strURL ){
		strURL = window.location.href;
	}
	if( window.sidebar ){
		window.sidebar.addPanel(strTitle,strURL,'');
	}else if( document.all ){
		window.external.addFavorite(strURL,strTitle);
	}else{
		alert("您正在使用的浏览器不支持自动加入收藏,请您按\"Ctrl + D\"加入收藏.");
	}
}
