﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Project			    : Assist2Sell
// Version			    : 1.0
// Full File Name	    : Library.js
// Comments			    : Common library of application
// Author			    : Pratik.Gohil
// Created On			: 18/01/2007
// Design Document Ref.	: NA
// Modified Details		: 
// Sr.No.    	Date		Modified By		Why & What is modified
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
//////////////////////////////////// String functions Starts Here //////////////////////////////////////////
// Functions used to replace old string with new string
// source       : String to be replaced
// replaceBy    : Replaced by string
String.prototype.replaceRegEx = function( source, replaceBy )
{
    var target  = this;
    target      = target.replace( new RegExp( source, "g" ), replaceBy );
    return target;
}

// Function used to encode html characters
String.prototype.encodeHTML = function()
{
    var encodedHTML	 = this;
    encodedHTML		 = encodedHTML.replace( new RegExp( "&", "g" ), "&amp;" );
    encodedHTML		 = encodedHTML.replace( new RegExp( "\"", "g" ), " & quot;" );
    encodedHTML		 = encodedHTML.replace( new RegExp( "<", "g" ), "&lt;" );
    encodedHTML		 = encodedHTML.replace( new RegExp( ">", "g" ), "&gt;" );
    encodedHTML		 = encodedHTML.replace( new RegExp( "'", "g" ), "&apos;" );
    return encodedHTML;
}

// Function used to decode html characters
String.prototype.decodeHTML = function()
{
    var decodeHTML	 = this;
    decodeHTML		 = decodeHTML.replace( new RegExp( "&amp;", "g" ), "&" );
    decodeHTML		 = decodeHTML.replace( new RegExp( "&quot;", "g" ), "\"" );
    decodeHTML		 = decodeHTML.replace( new RegExp( "&lt;", "g" ), "<" );
    decodeHTML		 = decodeHTML.replace( new RegExp( "&gt;", "g" ), ">" );
    decodeHTML		 = decodeHTML.replace( new RegExp( "&apos;", "g" ), "'" );
    return decodeHTML;
}

// Function used to validate html characters exists or not
String.prototype.hasHTML = function()
{
    var value	 = this;    
    if(value.indexOf("\"") > 0)
        return true;
    else if(value.indexOf("<") > 0)
        return true;
    else if(value.indexOf(">") > 0)
        return true;
    else if(value.indexOf("'") > 0)
        return true;
    else
        return false;
}

// Functions used to trim the input string
String.prototype.trim = function()
{
    return( this.replace( new RegExp( "^([\\s]+)|([\\s]+)$", "gm" ), "" ) );
}

// Functions used to left trim the input string
String.prototype.ltrim = function()
{
    return( this.replace( new RegExp( "^[\\s]+", "gm" ), "" ) );
}

// Functions used to right trim the input string
String.prototype.rtrim = function()
{
    return( this.replace( new RegExp( "[\\s]+$", "gm" ), "" ) );
}

// Function used to get length of trimed string
String.prototype.len = function()
{
    return( this.replace( new RegExp( "^([\\s]+)|([\\s]+)$", "gm" ), "" ) ).length;
}
//////////////////////////////////// String functions Ends Here //////////////////////////////////////////


//////////////////////////////////// Cookie functions Starts Here ////////////////////////////////////////
// Sets a Cookie with the given name and value. 
// name       : Name of the cookie
// value      : Value of the cookie
// [expires]  : Expiration date of the cookie (default: end of current session)
// [path]     : Path where the cookie is valid (default: path of calling document)
// [domain]   : Domain where the cookie is valid (default: domain of calling document)
// [secure]   : Boolean value indicating if the cookie transmission requires a secure transmission  
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


// Function used to get the value of the specified cookie.
// name     : Name of the desired cookie.
// Returns  : A string containing value of specified cookie, or null if cookie does not exist.
function getCookie( name )
{
	var prefix = name + "=";
	var begin = document.cookie.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = document.cookie.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = document.cookie.length;
	}
	return unescape(document.cookie.substring(begin + prefix.length, end));
}


// Function used to deletes the specified cookie.
// name      : Name of the cookie
// [path]    : Path of the cookie (must be same as path used to create cookie)
// [domain]  : Domain of the cookie (must be same as domain used to create cookie)
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

//////////////////////////////////// Cookie functions Ends Here //////////////////////////////////////////

function attachEventHanlder(_obj, _evt, _fnc) 
{	
	_evt = _evt.toLowerCase();
	if (_obj.addEventHandler) 
		_obj.addEventHandler(_evt, _fnc, false);
	else if (_obj.attachEvent) 
		_obj.attachEvent("on" + _evt, _fnc);
	else 
		_obj["on" + _evt] = _fnc;
}