/*
The SoftAd Group
Copyright (c) 2000-2004 The SoftAd Group, Inc.  All rights reserved
*/
// To use this object the following files must be also included.
// CNString.js

// -------------------------------------------------------------------------------
// File name        : CNString.js [class file]
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : July 2, 2003
// 
// This file contains the the CNString object that holds all the specific methods
// for form submittal that are used in ChannelNet.  CNString specifically deals
// with the dynamic posting of forms.
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------
// Copyright (c) 2003 The SoftAd Group, Inc.


// -------------------------------------------------------------------------------
// Function         : CNString()
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : July 2, 2003
// 
// This function creates the CNString
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------

/*Begin Class*/
function CNString() {
	//	********************** Public properties *******************
	//	************************************************************
	this.string = "";
	
	//	********************** Public methods **********************
	//	************************************************************
	this.create = create;
	this.LTrim = LTrim;
	this.RTrim = RTrim;
	this.Trim = Trim;
		
	this.createArray = createArray;
	this.isInString = isInString;
	this.isFirstChar = isFirstChar;
	this.concat = concat;
	this.getCharAt = getCharAt;
	this.getFirstOccurrence = getFirstOccurrence;
	this.getLastOccurrence = getLastOccurrence;
	this.getSubStr = getSubStr;
	this.getSubString = getSubString;
	this.getLowerCase = getLowerCase;
	this.getUpperCase = getUpperCase;
	
	this.escapeValues = escapeValues;
	this.cnReplace = cnReplace;
	this.urlencode_internal = urlencode_internal;
	this.urlencode = urlencode;
	this.urlencode_field = urlencode_field;
	this.XMLEncode = XMLEncode;
	this.replaceCRLF = replaceCRLF;
	
	//	************************** Globals *************************
	//	************************************************************
	var bReturn;
	var iReturn;
	var sReturn = "";
	var oReturn = null;
	
	// -------------------------------------------------------------------------------
	// Function         : create()
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 2, 2003
	// 
	// This function returns a new CNString object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function create(str){
		var oNewCNString = new CNString();
		oNewCNString.string = new String(str);
		return oNewCNString;		
	}	
	
	// -------------------------------------------------------------------------------
	// Function         : LTrim(str)
	// -------------------------------------------------------------------------------
	// This function removes spaces from the beginning of a string.
	// -------------------------------------------------------------------------------
	// Returns the trimmed string
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME		REQUIRED		TYPE        DESCRIPTION
	// str      Required        string      The string to remove spaces from.
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 02/06/2001   Chad Peck		Created
	// 07/02/2003	Ken Wimberley	Updated
	function LTrim(str) {
		sReturn = "";
		if (str.length == 0)
			return str;
		if(str) {
			sReturn = str.replace(/^\s+/g,"");
		} else {
			sReturn = this.string.replace(/^\s+/g,"");
		}		
		return sReturn;
	}

	// -------------------------------------------------------------------------------
	// Function         : RTrim(str)
	// -------------------------------------------------------------------------------
	// This function removes spaces from the end of a string.
	// -------------------------------------------------------------------------------
	// Returns the trimmed string
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME		REQUIRED		TYPE        DESCRIPTION
	// str      Required        string      The string to remove spaces from.
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 02/06/2001   Chad Peck		Created
	// 07/02/2003	Ken Wimberley	Updated
	function RTrim(str) {
		sReturn = "";
		if (str.length == 0)
			return str;
		if(str) {
			sReturn = str.replace(/\s+$/g,"");
		} else {
			sReturn = this.string.replace(/\s+$/g,"");
		}
		return sReturn;
	}

	// -------------------------------------------------------------------------------
	// Function         : Trim(str)
	// -------------------------------------------------------------------------------
	// This function removes spaces from the beginning and end of a string.
	// -------------------------------------------------------------------------------
	// Returns the trimmed string
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME		REQUIRED		TYPE        DESCRIPTION
	// str      Optional        string      The string to remove spaces from.
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 02/06/2001   Chad Peck		Created
	// 07/02/2003	Ken Wimberley	Updated
	function Trim(str) {
		try
		{
			sReturn = "";
			if (str.length == 0)
				return str;
			if(str){	
				sReturn = RTrim(LTrim(str));
			} else {
				sReturn = RTrim(LTrim(this.string));
			}
		} catch (e)
		{
			sReturn = "";
		}
		finally
		{
			return sReturn;
		}
	}
		
	function createArray(separator, str){
		oReturn = null;
		if(str){
			oReturn = str.split(separator);
		} else {
			oReturn = this.string.split(separator);
		}
		return oReturn;
	}
	
	function isInString(sCompare, str){
		bReturn = false;
		if(str){
			if(str.indexOf(sCompare,0) >= 0){
				bReturn = true;
			}
		} else {
			if(this.string.indexOf(sCompare,0) >= 0){
				bReturn = true;
			}
		}
		return bReturn;
	}
	
	function isFirstChar(sCompare, str){
		bReturn = false;
		if(str){
			if(str.charAt(0) == sCompare){
				bReturn = true;
			}
		} else {
			if(this.string.charAt(0) == sCompare){
				bReturn = true;
			}
		}
		return bReturn;
	}
	
	function concat(sConcatWith, strConcatTo){
		sReturn = "";
		if(strConcatTo){
			sReturn = strConcatTo.concat(sConcatWith);
		} else {
			this.string = this.string.concat(sConcatWith);
			sReturn = this.string;
		}
		return sReturn;	
	}
	
	function getCharAt(iNum, str){
		sReturn = "";
		if(str){
			sReturn = str.charAt(iNum);
		} else {
			sReturn = this.string.charAt(iNum);
		}
		return sReturn;
	}
	
	function getFirstOccurrence(sCompare, str){
		iReturn = -1;
		if(str){
			iReturn = str.indexOf(sCompare,0);
		} else {
			iReturn = this.string.indexOf(sCompare,0);
		}
		if(iReturn != -1){
			iReturn++;
		}
		return iReturn;
	}
	
	function getLastOccurrence(str){
		iReturn = -1;
		if(str){
			iReturn = str.lastIndexOf(sCompare,0);
		} else {
			iReturn = this.string.lastIndexOf(sCompare,0);
		}
		if(iReturn != -1){
			iReturn++;
		}
		return iReturn;
	}
	
	function getSubStr(iStart, iLength, str){
		sReturn = "";
		if(str){
			if(iLength){
				sReturn = str.substr(iStart, iLength);
			}
			sReturn = str.substr(iStart);
		} else {
			if(iLength){
				sReturn = this.string.substr(iStart, iLength);
			}
			sReturn = this.string.substr(iStart);		
		}
		return sReturn;
	}
	
	function getSubString(iStart, iEnd, str){
		sReturn = "";
		if(str){
			sReturn = str.substring(iStart, iEnd);
		} else {
			sReturn = this.string.substring(iStart, iEnd);
		}
		return sReturn;
	}
	
	function getLowerCase(str){
		sReturn = "";
		if(str){
			sReturn = str.toLowerCase();
		} else {		
			sReturn = this.string.toLowerCase();
		}
		return sReturn;
	}
	
	function getUpperCase(){
		sReturn = "";
		if(str){
			sReturn = str.toUpperCase();
		} else {		
			sReturn = this.string.toUpperCase();
		}
		return sReturn;
	}
	
	
	// -------------------------------------------------------------------------------
	// Function         : escapeValues(stringToEscape)
	// -------------------------------------------------------------------------------
	// This function escapes special characters in a string
	// -------------------------------------------------------------------------------
	// Returns a string with the special characters escaped.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME				REQUIRED	TYPE      DESCRIPTION
	// strintToEscape	Required	string    A string containing special characters
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 02/06/2001   Chad Peck		Created
	// 07/02/2003	Ken Wimberley	Updated

	function escapeValues(stringToEscape) {
		sReturn = "";
		var regExp = /&\w+=(.+)/g
		
		// doesn't escape the string if it contains name=value pairs separated by
		// ampersands such as a criteria string
		if(stringToEscape.search(regExp) == -1) {		
			// escapes the string if it doesn't contain any name/value pairs
			sReturn = escape(stringToEscape);			
		}		
		return sReturn;
	}

	// -------------------------------------------------------------------------------
	// Function         : cnReplace(strSource, strReplace, strWith)
	// -------------------------------------------------------------------------------
	// This function replaces occurrences of a substring strReplace with another string
	// strWith in a sting strSource.
	// -------------------------------------------------------------------------------
	// Returns the string with the replacement
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			REQUIRED	TYPE        DESCRIPTION
	// strSource    Required    string      The string to containing a substring to be replaced
	// strReplace   Required    string      The substring to replace.
	// strWith      Required	string      The substring to make the replacement with.
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 02/06/2001   Chad Peck		Created
	// 07/02/2003	Ken Wimberley	Updated

	function cnReplace(strSource, strReplace, strWith) {
		sReturn = "";
		if(strSource) {
			sReturn = strSource.replace(strReplace, strWith);
		} else {
			sReturn = '';
		}		
		return sReturn;		
	}

	// -------------------------------------------------------------------------------
	// Function         : urlencode_internal(sIn)
	// -------------------------------------------------------------------------------
	// This function encodes a string to send in a URL using the supplied translation
	// table.
	// -------------------------------------------------------------------------------
	// Returns a URL encoded string.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// szIn		Required	String      The plaintext for encoding
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 10/10/2000   Dan Boresjo		Created
	// 07/02/2003	Ken Wimberley	Updated

	function urlencode_internal(sIn, sURLEncodedChars){
		var LenStr = sIn.length;
		var sOut = "";
		var sReplace = "";
		var sURLChars = " ,~,!,#,$,%,^,&,(,),+,=,[,{,],},\,|,`,',:,;,/,<,>";
		var aURLChars = sURLChars.split(",");
		var aURLEncodedChars = sURLEncodedChars.split(",");

		if (LenStr > 0) {
			for (Count=0; Count<=LenStr; Count++) {
				StrChar = sIn.substring(Count, Count+1);
				for (j=0; j<=25; j++) {
					if (StrChar==aURLChars[j]) {
						sReplace = aURLEncodedChars[j];
					} 
				}
				if (sReplace != "") {
					sOut = sOut + sReplace;
					sReplace="";
				} else {
					sOut = sOut + StrChar;
				}
			}
		} else {
			return sIn;
		}
		return(sOut.substring(0, sOut.length-3));
	}

	// -------------------------------------------------------------------------------
	// Function         : urlencode(sIn)
	// -------------------------------------------------------------------------------
	// This function encodes a string to send in a URL.
	// -------------------------------------------------------------------------------
	// Returns a URL encoded string.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// sIn		Required	String      The plaintext for encoding
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME			DESCRIPTION
	// 10/10/2000   Dan Boresjo		Created
	// 07/02/2003	Ken Wimberley	Updated

	function urlencode(sIn){
		var sURLEncodedChars = "+,%7E,%21,%23,%24,%25,%5E,%26,%28,%29,%2B,%3D,%5B,%7B,%5D,%7D,%5C,%7C,%60,%27,%3A,%3B,%2F,%3C,%3E";
		return urlencode_internal(sIn, sURLEncodedChars);
	}

	// -------------------------------------------------------------------------------
	// Function         : urlencode_field(sIn)
	// -------------------------------------------------------------------------------
	// This function encodes a string to send in a URL, including double-encoding of
	// ampersands so that they pass through the responseloop correctly.
	// -------------------------------------------------------------------------------
	// Returns a URL encoded string.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME		REQUIRED		TYPE        DESCRIPTION
	// sIn		Required		String      The plaintext for encoding
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME        DESCRIPTION
	// 10/10/2000   Dan Boresjo		Created
	// 07/02/2003	Ken Wimberley	Updated

	function urlencode_field(sIn){
		return escape(sIn);
	}
	
	function XMLEncode(sIn) {
		sIn = sIn.replace(/&/g, "&amp;");
		sIn = sIn.replace(/</g, "&lt;");
		sIn = sIn.replace(/>/g, "&gt;");
		sIn = sIn.replace(/"/g, "&quot;");
		sIn = sIn.replace(/'/g, "&apos;");

		return sIn;
	}
	
	function replaceCRLF(str) {
		if(str && str.replace) {
			str = str.replace(/\r\n/g,"<br\/>");
			str = str.replace(/\n/g,"<br\/>");
		}
		return str;
	}
		
}
/*End Class*/

var oCNString = new CNString();

