﻿// *********************************************** //
// ** STRING PROTOTYPES
// ** extends the _JavaScript STRING object with
// ** additional functionality
// ********************************************** //

// string.trim()
    String.prototype.trim = function()
    {   
            return this.replace(/(^\s*)|(\s*$)/g, "");
    }
// string.ltrim()    
    String.prototype.ltrim = function()
    {
            return this.replace(/(^\s*)/g, "");
    }

// string.rtrim()    
    String.prototype.rtrim = function()
    {
            return this.replace(/(\s*$)/g, "");
    }

// string.isEmpty()    
    String.prototype.isEmpty = function()
    {
        if(null == this)
            return(true);      
              
        var sValue = this.trim();
        
        if(sValue.length > 0)
            return(false);
        else
            return(true);
    } 
    
// string.isPhone()
    String.prototype.isPhone = function()
    {
        var pattern = /^1?( |-|\.)?(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$/;
        return( this.trim().match(pattern) );
    }  
    
// string.isZipCode()
    String.prototype.isZipCode = function()
    {
        var pattern = /^\d{5}(-\d{4})?$/;
        return( this.trim().match(pattern) );
    }    
    
// string.isEmail()
    String.prototype.isEmail = function()
    {
		var pattern	= /^([\w][\.|\-]?)*?([\w])+@([\w]+\.)+(com|net|org|biz|bz|eu|edu|info|me|mobi|pro|tel|tv|co|us|cc|de|name|mil|uk|COM|NET|ORG|BIZ|BZ|EU|EDU|INFO|ME|MOBI|PRO|TEL|TV|CO|US|CC|DE|NAME|MIL|UK)$/;
//      var pattern = /^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$/;
        return( this.trim().match(pattern) ) ;
    }    
    
// string.isURL()
    String.prototype.isURL = function()
    {
        var pattern = /^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|UK|INFO|US|NAME|BIZ|DE|TV|CC|BZ)$/;
        return( this.trim().match(pattern) );
    }    
    
// string.toHTML
    String.prototype.toHTML = function()
	{
		var result	= null;
		var pattern 	= /\n/g;
		result 		= this.replace(pattern, "<br/>");
		
		pattern		= /\r/g;
		result 		= result.replace(pattern, "<br/>");
		
		return(result);
	}
	
// string.changeSpecialChar
	String.prototype.changeSpecialChar	= function()
	{
		var txValue		= this;		
		for(var i=0; i<txValue.length; i++){
		
			character 	= txValue.substr(i,1);
			code 		= character.charCodeAt(0);
			
			if(code >= 192 && code <= 197)
					txValue = txValue.replace(character, "A");
			else if(code == 199)
					txValue = txValue.replace(character, "C");
			else if(code >= 200 && code <= 203)
					txValue = txValue.replace(character, "E");
			else if(code >= 204 && code <= 207)
					txValue = txValue.replace(character, "I");
			else if(code == 209)
					txValue = txValue.replace(character, "N");
			else if(code >= 210 && code <= 214)
					txValue = txValue.replace(character, "O");
			else if(code >= 217 && code <= 220)
					txValue = txValue.replace(character, "U");
			else if(code >= 224 && code <= 229)
					txValue = txValue.replace(character, "a");
			else if(code == 231)
					txValue = txValue.replace(character, "c");
			else if(code >= 232 && code <= 235)
					txValue = txValue.replace(character, "e");
			else if(code >= 236 && code <= 239)
					txValue = txValue.replace(character, "i");
			else if(code == 241)
					txValue = txValue.replace(character, "n");
			else if(code >= 242 && code <= 246)
					txValue = txValue.replace(character, "o");
			else if(code >= 249 && code <= 252)
					txValue = txValue.replace(character, "u");
			else if(code == 255)
					txValue = txValue.replace(character, "y");
		}
		
		return(txValue);
	};
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	