/*
	Author: Josh.Li 
	date:  2005-3-6
	description:
	      ??????(String)???.

*/


/*********************************************************************************
 * Functions to test a string
 *********************************************************************************/
String.prototype.isNumRE = /[0-9]+(\.[0-9])*|\.[0-9]+/;
String.prototype.isNumber = function() {
	var m = this.match(this.isNumRE);
	return (m && this == m[0]);
	//return (this.isNumRE.test(this));
}


/*********************************************************************************
 * Function: String.containsInvalidChars() -> String or null
 *		Strip leading whitespace from a string
 * Inputs:
 *		none
 * Outputs:
 *		result:String of invalidChars
 * Examples:
 *		"abc{def[".containsInvalidChars() -> "{["
 *		"abcdef".containsInvalidChars() -> null
 */

String.prototype.invalidSearchChars = /[\?\(\)\/{}\[\]<>\\]/g;
String.prototype.containsInvalidSearchChars = function() {
	var res = this.match(this.invalidSearchChars);
	return (res ? res.join('') : null);
}

//String.prototype.invalidChars = /[{}\[\]<>"\\]/g;   //comment by mxz at 2003-2-23 for check invalid chars
String.prototype.invalidChars = /[*~!#$%^&{}\(\)\[\]<>"'\\]/g; 
String.prototype.containsInvalidChars = function() {
	var res = this.match(this.invalidChars);
	var c = null;

	if (USERBROWSER.ie) 
	{
		if (!res) res = [];
		for (var i=0; i < this.length; i++) 
		{
			c = this.charCodeAt(i);
			/*
			if (c > 127) {
				c = c.toString(16);
				for (var j=c.length;j<4; j++) {
					c = '0' + c;
				}
				eval('res[res.length] ="\\u' + c + '"');
			}
			*/
			if (c > 255) 
			{				
			   c = "&#" + c.toString(16) + ";" ;
			   // alert("c=" + c);
		           // convert char to &#nnnn;
			   res.join(c);
			}
		}
	}
	
	return (res ? res.join('') : null);
}

String.prototype.invalidDNChars = /[\\\n\r\",+=<>#;&\/]/g;
String.prototype.containsInvalidDNChars = function() {
	var res = this.match(this.invalidDNChars);
	var c = null;
	
	if (USERBROWSER.ie) 
	{
		if (!res) res = [];
		
		for (var i=0; i < this.length; i++) 
		{
			c = this.charCodeAt(i);
		/*	
			if (c > 127) 
			{
				c = c.toString(16);
				for (var j=c.length;j<4; j++) 
				{
					c = '0' + c;
				}
				eval('res[res.length] ="\\u' + c + '"');
			}
		*/
			if (c > 255) 
			{				
			   c = "&#" + c.toString(16) + ";" ;
			   // alert("c=" + c);
		           // convert char to &#nnnn;
			   res.join(c);
			}
																  
		}
        }
                
	return (res ? res.join('') : null);
}


/*********************************************************************************
 * Functions to trim whitespace from a string
 *********************************************************************************/
 
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,""); 
}
 
 
/*********************************************************************************
 * Function: String.rtrim() -> String
 *		Strip trailing whitespace from a string
 * Inputs:
 *		none
 * Outputs:
 *		result:String - Source string with trailing whitespace removed
 * Examples:
 *		"   test  ".rtrim() -> "   test"
 */

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}


/*********************************************************************************
 * Function: String.trim() -> String
 *		Strip leading and trailing whitespace from a string
 * Inputs:
 *		none
 * Outputs:
 *		result:String - Source string with leading and trailing whitespace removed
 * Examples:
 *		"   test  ".trim() -> "test"
 */

String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g,"") 
}
String.prototype.trim2 = function() { 
	return this.replace(/\s+/g,"%20") 
}

/**
 * Function to take a string and convert it into a title.
 * 
 *	Converts first letter of each word to uppercase like the style used in book titles.   
 *	Keeps existing uppercase unchanged.
 *	Some words stay lowercase in the middle ("is", "a", "the", "an")
 * Examples:
 *		"john's test page" -> "John's Test View"
 *		"the IBM way" -> "The IBM Way"
 *		"this is a test" -> "This is a Test"
 *		"String Utility to Put UpperCase into Titles" -> "String Utility To Put UpperCase Into Titles"
 */


String.prototype.titleCase = function() {
	if (!this) return "";

	var p = /(^(\s*)([a-z]))|( \bis\b| \ban?\b| \bthe\b)|((\s)([a-z]))/g;
	var s = this.replace(p,"$2<$3>$4$6<$7>").replace(/<>/g,"").replace(/<[a-z]>/g,"*^*");
	for (var i=s.indexOf("*^*"); i>=0; i=s.indexOf("*^*"))
		s = s.substr(0,i) + this.charAt(i).toUpperCase() + s.substr(i+3);
	return s;
}


/*********************************************************************************
 * Function: String.IsStartWith(v) -> Boolean
 *		Returns true is the string starts with the specified string
 * Inputs:
 *		v:String - Value to check for at the start of the string
 * Outputs:
 *		result:Boolean - TRUE if starts with specified value
 * Examples:
 *		"this and that".IsStartWith("this") -> true
 *		"this and that".IsStartWith("that") -> false
 */

String.prototype.IsStartWith = function(v) {
	return (this.substring(0,v.length) == v)
}

/*********************************************************************************
 * Function: String.IsEndWith(v) -> Boolean
 *		Returns true is the string ends with the specified string
 * Inputs:
 *		v:String - Value to check for at the end of the string
 * Outputs:
 *		result:Boolean - TRUE if ends with specified value
 * Examples:
 *		"this and that".IsEndWith("this") -> false
 *		"this and that".IsEndWith("that") -> true
 */

String.prototype.IsEndWith = function(v) {
	return (this.substring(this.length-v.length) == v)
}


/*********************************************************************************
 * Function: String.IsContains(v) -> Boolean
 *		Test if the string IsContains the specified value
 * Inputs:
 *		v:String or RegExp - Value to check for within the string
 * Outputs:
 *		result:Boolean - TRUE if IsContains the specified value
 * Examples:
 *		"abc".IsContains("b") -> true
 *		"abc".IsContains(/a.c/) -> true
 */

String.prototype.IsContains = function(v) {
	if (!v) return true;
	if (v.constructor == String) {
		return (this.indexOf(v) != -1);
	} else {
		return v.test(this);
	}
}

String.prototype.InClude = function(v){
	if(this.length < 0) 
		return false
	var arr = this.split(",")
	var i = 0,bFlag = false;
	for(i=0;i<arr.length-1;i++){
		if(arr[i] == v){
			bFlag = true;
			break;
		}
	}
	return bFlag;
}


/*********************************************************************************
 * Function: String.IsNotContain(v) -> Boolean
 *		Test if the string does not contain the specified value
 * Inputs:
 *		v:String or RegExp - Value to check for within the string
 * Outputs:
 *		result:Boolean - TRUE if does not contain the specified value
 * Examples:
 *		"abc".IsNotContain("d") -> true
 *		"abc".IsNotContain(/c/) -> false
 */

String.prototype.IsNotContain = function(v) {
	if (!v) return true;			// Return true if no value specified
	if (v.constructor == String) {
		return (this.indexOf(v) == -1);
	} else {
		return !v.test(this);
	}
}




/*********************************************************************************
 * Functions to perform type conversion
 *********************************************************************************/


/*********************************************************************************
 * Function: String.toObject() -> Object
 *		Converts a string representation of an object to a JavaScript object
 * Inputs:
 *		none
 * Outputs:
 *		result:Object - The Object that is represented as the string
 * Examples:
 *		"{a:1,b:2}" -> {a:1,b:2}
 *		"a:3,b:4" -> {a:3,b:4}
 */

String.prototype.toObject = function() {
	if (this) {
		var r = WEBBROWSER.eval(((this.charAt(0) == "{") ? this : "{" + this + "}"));
		if (r == null) r = {};		// Always return a valid object
	} else {
		r = {};
	}
	return r;
}


/*********************************************************************************
 * Function: String.toArray() -> Array
 *		Converts a string representation of an Array to a JavaScript Array
 * Inputs:
 *		none
 * Outputs:
 *		result:Array - The Array that is represented as the string
 * Examples:
 *		"[1,2,3]" -> [1,2,3]
 *		"1,2" -> [1,2]
 */

String.prototype.toArray = function() {
	if (this) {
		var r = WEBBROWSER.eval(this);
		if (r == null) r = [];		// Always return a valid object
	} else {
		r = [];
	}
	return r;
	//*** OMIT BRACKETS IF THEY ARE ALREADY IN THE STRING??? ***
}

String.prototype.checkDate = function(){
	//检查一个类似1978-11-12的日期是否正确
	//返回true正确
	//返回false错误

	re=/([0-9]{4}\-[0-9]{0,2}\-[0-9]{0,2})/
	if(this.length>10 || this.length<8 ){return false;}
	ss=this.split("-");year=ss[0];	month=ss[1];day=ss[2];
	yn=year%4;
	if(month<1||month>12){return false;}
	if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
	{if(day<1||day>31){return false;}}
	if(month==2)
	{	if(yn==0){if(day<1||day>29)
			{return false;}
	}
		if(day<1||day>28){return false;}	
	}
	if((month==4)||(month==6)||(month==9)||(month==11))
	{if(day<1||day>30){return false;}}		
	return true;
}

String.prototype.checkEmail = function(){
	//检查一个电子邮件是否正确
	//返回true正确
	//返回false错误
	var strr;
	re=/(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
	re.exec(this);
	if (RegExp.$3!=""&&RegExp.$3!="."&&RegExp.$2!=".") 
		strr=RegExp.$1+RegExp.$2+RegExp.$3
	else
		if (RegExp.$2!=""&&RegExp.$2!=".") 
			strr=RegExp.$1+RegExp.$2
		else  
			strr=RegExp.$1
	if (strr != this) 
		return false;
  return true;

}

String.prototype.isempty = function()
{
	//检查字符串是否为空值
	//strsome是要检查的字符串
	//返回false 非空字符串
	//返回true  是空字符串
//	aa=strsome;
	bb=this.length;
	for(i=1;i<bb+1;i++)
	{
		cc=this.substring(i-1,i);
		if(cc!=" ")
		{
			return false;
		}
	}	
	return true;
}



String.prototype.JsHtmlEncode = function()
{
	//把字段串转换成HTML代码，以便显示在HTML页面中
	//替换[<  > 空格 换行]

	var temp = "";
	temp=this.replace(/&/g,"&amp;");
	temp=temp.replace(/</g,"&lt;");
	temp=temp.replace(/>/g,"&gt;");
	return temp;
}

String.prototype.toDocument = function(){
	
	var oStrDoc = new ActiveXObject("microsoft.xmldom")
	oStrDoc.async = false
	oStrDoc.loadXML(this)
	if(oStrDoc.parseError.errorCode == 0){
		var oRoot = oStrDoc.documentElement
		return oRoot
	}
	else {
		return null;
	}
	
}

String.prototype.IsValidString = function()
{
	// 判断字符串是否有效，0-9,a-z,A-Z
	var isvalid=true;
	for(var i=0;i<this.length;i++)
	{
		var c = this.charAt(i);
		if(!(c>='0'&&c<='9' || c>='a'&&c<='z' || c>='A'&&c<='Z' ))
		{
			isvalid=false;
			break;
		}
	}
	return (isvalid);
}

String.prototype.change2Date = function(){
	
	if(!this.checkDate())
		return "";
	var arrDate = this.split("-");
	var oDate = new Date(arrDate[0],arrDate[1],arrDate[2]);
	return oDate;
}
