/*  
COPYRIGHT EC-SERVER ALL RIGHT RESERVED
Author : Carl 
$Id: strutil.js,v 1.6 2008/02/27 08:08:56 carl Exp $
*/
function strUtil(p_str) {
   if(typeof(p_str)=='undefined') p_str = "";
	this.String = p_str;
	return this;
}
strUtil.prototype.length = function() {
	a=strUtil.prototype.length.arguments;
   if(typeof(a[0])!='undefined') p_in = a[0];
	else p_in = this.String;
	return p_in.length;
}

/**
*    check if the special string can be translation to an integer

*    @return bool
*/  
strUtil.prototype.isInteger = function() {
	a=strUtil.prototype.isInteger.arguments;
   if(typeof(a[0])!='undefined') p_in = a[0];
	else p_in = this.String;

   var p_val = p_in.toString();
   for(var i=0;i<p_val.length; i++)  {
       var oneChar = p_val.charAt(i);
       if(i==0 && oneChar =="-")       continue;
       if(oneChar<"0" || oneChar>"9")  return false;
   }
   return true;
}

/**
*   check if the special string can be translation to a positive integer

*   @return bool
*/
strUtil.prototype.isPosInteger = function() {
	a=strUtil.prototype.isPosInteger.arguments;
   if(typeof(a[0])!='undefined') p_in = a[0];
	else p_in = this.String;

   var p_val = p_in.toString();
   for(var i=0;i<p_val.length; i++)  {
       var oneChar = p_val.charAt(i);
       if(oneChar<"0" || oneChar>"9")  return false;
   }
   return true;
}

/**
*   check if input is empty;

*   @return bool
*/
strUtil.prototype.isEmpty = function () {
	a=strUtil.prototype.isEmpty.arguments;
   if(typeof(a[0])!='undefined') p_in = a[0];
	else p_in = this.String;
   p_val = this.trim(p_in);
   if(p_val == null || p_val=="")  return true;
   if(p_val.length==0) return true;
   return false;
}

/**
*   check if a string is an email address;

*   @return string
*/
strUtil.prototype.isEmail = function(){ 
	a=strUtil.prototype.isEmail.arguments;
   if(typeof(a[0])!='undefined') p_in = a[0];
	else p_in = this.String;

   var reg = new RegExp("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$"); 
   if(p_in.search(reg)!=-1)
      return true;
   else 
      return false; 
}

/**
*   check if a string is code;

*   @return bool
*/
strUtil.prototype.isCode = function()  {
	a=strUtil.prototype.isCode.arguments;
   if(typeof(a[0])!='undefined') p_in = a[0];
	else p_in = this.String;
   var  reg =new RegExp(/^[a-z\d\_]+$/i);
   if(p_in.search(reg)!=-1)
      return true;
   else 
      return false; 
}

/**
*   trim special char in a string at left;

*   @return string
*/
strUtil.prototype.ltrim = function(p_str)  {
	a=strUtil.prototype.ltrim.arguments;
	if(typeof(a[0])!='undefined') p_str = a[0];
	else p_str = this.String;
	return p_str.replace(/(^\s*)/g, ""); 
}

/**
*  trim special char in a string at left and right;
*  @return string;
*/
strUtil.prototype.trim = function(p_str) 
{ 
	a=strUtil.prototype.trim.arguments;
	if(typeof(a[0])!='undefined') p_str = a[0];
	else p_str = this.String;
	return p_str.replace(/(^\s*)|(\s*$)/g, ""); 
} 
String.prototype.trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

/**
*  trim special char in a string at right;
*  @return string;
*/
strUtil.prototype.rtrim = function(p_str)  {
	a=strUtil.prototype.rtrim.arguments;
	if(typeof(a[0])!='undefined') p_str = a[0];
	else p_str = this.String;
	return p_str.replace(/(\s*$)/g, ""); 
}


/*
strUtil.prototype.trim = function(p_str)  {
	a=strUtil.prototype.trim.arguments;

   if(typeof(a[0])!='undefined') p_str = a[0];
	else if(typeof(this.String)!='undefined') p_str = this.String;
	else return "";	

	p_str= p_str.replace(/(^\s*)|(\s*$)/g, "");
   return p_str;
}
*/

/**
*  Delete script / iframe /frameset / a href=javascript tags from source string
*  @return string;
*/
strUtil.prototype.wipeScript = function(p_str)  {
	var reg1 = /<script[^>]*>([\s|\S]*?)<\/script\s*>/i;
	var reg2 = /<iframe[^>]*>([\s|\S]*?)<\/iframe\s*>/i;
	var reg3 = /<frameset[^>]*>([\s|\S]*?)<\/frameset\s*>/i;
	var reg4 = /<a\s*href=[\s\S]*javascript[^>]*>([\s|\S]*?)<\/a\s*>/i;
	p_str = p_str.replace(reg1,"");
	p_str = p_str.replace(reg2,"");
	p_str = p_str.replace(reg3,"");
	p_str = p_str.replace(reg4,"");
   return p_str;
}
/**
*  Delete form tag 
*  @return string;
*/
strUtil.prototype.striptags = function(p_str)  {
	var reg1 = /<[^>]*>/i;
	var reg2 = /<\/[^>]*>/i;
	var reg3 = /&nbsp;/i;
	p_str = p_str.replace(reg1,"");
	p_str = p_str.replace(reg2,"");
	p_str = p_str.replace(reg3,"");
   return p_str;
}
/**
*  Delete form tag 
*  @return string;
*/
strUtil.prototype.wipeForm = function(p_str)  {
	var reg1 = /<form[^>]*>/i;
	var reg2 = /<\/form[^>]*>/i;
	p_str = p_str.replace(reg1,"");
	p_str = p_str.replace(reg2,"");
   return p_str;
}
/**
*  add aslashes 
*  @return string;
*/
strUtil.prototype.addslashes = function (str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}
/**
*  strip aslashes 
*  @return string;
*/
strUtil.prototype.stripslashes = function (str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}
/**
*  nl2br 
*  @return string;
*/
strUtil.prototype.nl2br = function (str) {
	return str.replace(/(\r\n)|(\n\r)|\r|\n/g,"<BR>");
}
/**
*  direct show input string 
*  @return string;
*/
strUtil.prototype.directShowInput = function (str) {
	return strUtil.prototype.nl2br(strUtil.prototype.htmlspecialchars(strUtil.prototype.stripslashes(str)));

}
/**
*  specialchars 
*  @return string;
*/
strUtil.prototype.htmlspecialchars = function(str)  {
	str = str.replace(/\</g,"&lt;");
	str = str.replace(/\>/g,"&gt;");
	str = str.replace(/\"/g,"&quot;");
	return str;
}
/**
*  specialchars 
*  @return string;
*/
strUtil.prototype.reversespecialchars = function(str)  {
	str = str.replace(/&lt;/g,"<");
	str = str.replace(/&gt;/g,">");
	str = str.replace(/&quot;/g,"\"");
	str = str.replace(/&amp;/g,"&");
	return str;
}
strUtil.prototype.markIp = function () {
   a=strUtil.prototype.markIp.arguments;
	ip = a[0];	
	if(typeof(a[1])!='undefined') part = a[1];
	else part = 3;

	if(part>4) return ip;
	ret = ip.split(".");
	str="";
	for(i=0;i<ret.length;i++) {
		if(i+1<part) str+=ret[i]+".";
		else str+="X.";
	}
	return str.substring(0,str.length-1);
}
