/*  
COPYRIGHT EC-SERVER ALL RIGHT RESERVED
Author : Carl 
$Id: strutil.js,v 1.1.1.1 2007/07/12 02:01:17 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()  {
	a=strUtil.prototype.ltrim.arguments;
   if(typeof(a[0])!='undefined') p_char = a[0];
	else p_char = " ";

   if(typeof(a[1])!='undefined') p_str = a[1];
	else p_str = this.String;
	if (typeof(p_str) == "undefined") return p_str;
   for(var i=0;i<p_str.length;i++)  {
      if(p_str.substr(i,1) != p_char)  break;  
      p_str = p_str.substr(i+1);
   }
   return p_str;
}

/**
*  trim special char in a string at right;
*  @return string;
*/
strUtil.prototype.rtrim = function()  {
	a=strUtil.prototype.rtrim.arguments;
   if(typeof(a[0])!='undefined') p_char = a[0];
	else p_char = " ";

   if(typeof(a[1])!='undefined') p_str = a[1];
	else p_str = this.String;

	if (typeof(p_str) == "undefined") return p_str;
   for(var i=p_str.length-1;i>=0;i--)  {
      if(p_str.substr(i,1) != p_char)  break;  
      p_str = p_str.substr(0,i);
   }
   return p_str
}

/**
*  trim special char in a string at left and right;
*  @return string;
*/

strUtil.prototype.trim = function(p_char,p_str)  {
	a=strUtil.prototype.trim.arguments;
   if(typeof(a[0])!='undefined') p_char = a[0];
	else p_char = " ";

   if(typeof(a[1])!='undefined') p_str = a[1];
	else p_str = this.String;
   p_str = this.ltrim(p_char,p_str);
   p_str = this.rtrim(p_char,p_str);
   return p_str;
}

/*
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.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;
}
