var userID;
var userInfo;
var friendInfo;

var fb_connect		= null;
var streamObject 	= null;

if ( window.addEventListener )
	{
		// safari + firefox
		window.addEventListener ( "load", startLoadingFbBootstrap, false );
	}
	
if ( window.attachEvent )
	{
		// IE
		window.attachEvent ( "onload", startLoadingFbBootstrap );
	}

function startLoadingFbBootstrap (  ) {
	FB.Bootstrap.requireFeatures ( ["Connect"], initAppliaction );
}

function initAppliaction (  ) {
	init (  );
}

function init (  ) {
    
    // streamObject = new StreamObject (  );
    
    fb_connect = new FB_connect (  );
    fb_connect.setAPIKey ( "651bb0d8a167ab300a9d2ca3a937a783" );
    
    var tmpCountryCode = document.getElementById ( "countryCode" );
    if ( tmpCountryCode != null )
    	{
    		fb_connect.setCountryCode ( tmpCountryCode.value );
    	}
    
    setTimeout ( "fb_connect.init (  )", 1000 );
}

function flashObject (  ) {
    this.flashMovie = null;
    
    this.CALLBACK_LOGIN 				= "login";
    this.CALLBACK_LOGOUT 				= "logout";
    this.CALLBACK_ALREADY_LOGGED_IN 	= "alreadyloggedin";
    this.CALLBACK_ENABLE_SITE 			= "enablesite";
    this.CALLBACK_SET_USER_DATA 		= "setuserdata";
}

flashObject.prototype = {
    getFlashMovie : function (  ) {
		
        this.flashMovie = ( navigator.appName.indexOf ("Microsoft") != -1 ) ? window["flashContent"] : document["flashContent"];
        
        return this.flashMovie;
    }
}



function StreamObject (  ) {
	this.title			= "";
	this.subTitle		= "";
	this.description	= "";
	this.url			= "";
	
	this.postUrl		= "http://campaign.sloggi.com/sf/";
}

StreamObject.prototype = {
	getInfo : function ( _value ) {
		return this[_value];
	},
	publishStream : function ( _name, _id, _flashVal1, _flashVal2, faceBookPagePost_0, faceBookPagePost_1, faceBookPagePost_2, faceBookPageID ) {
		var self = this;
		
		FB.ensureInit ( function (  ) {
			// alert ( fb_connect.getUserID (  ) );
			
			setTimeout ( function (  ) { 
				FB.Connect.streamPublish ( '', { 
					"name" : _name + " " + faceBookPagePost_0, 
					"href" : "http://www.facebook.com/profile.php?ref=profile&id=" + _id, 
					"caption" : _flashVal1,
					"description" : _flashVal2,
					"media" : [{'type': 'image', "src" : self.postUrl + "img/sloggi_sf_fb_post_icon.jpg", "href" : self.postUrl + "index.php?pageID=" + faceBookPageID + "&countryCode=" + fb_connect.getCountryCode (  )}]
				}, [{ "text": faceBookPagePost_2, "href": self.postUrl + "index.php?pageID=" + faceBookPageID + "&countryCode=" + fb_connect.getCountryCode (  )}], null,
				faceBookPagePost_1, self.stream_callback, false, null );
			}, 500 );
		} );
		
		/*
		var message = 'I support Bill The Aircraft Carrier in the SpayDay Online Photo Contest! Will you vote for Bill The Aircraft Carrier?';
		var attachment = {
			'name': 'Vote for Bill The Aircraft Carrier!',
			'href': 'http://spayday.factor360.com/contest.html?page=viewInd&id=48082&contestId=2', 'caption': '{*actor*} supports Bill The Aircraft Carrier in the SpayDay Online Photo Contest!',
			'description': 'Bill The Aircraft Carrier`s favorite thing to do is fight the Klingons, but sometimes gets in trouble because he/she likes to fight the Klingons. Bill The Aircraft Carrier makes me smile because... he has spock-like ears.',
			"media": [{ "type": "image", "src": "http://spayday.factor360.com/calendar_contest/images/69839_1.jpg", "href": "http://spayday.factor360.com/contest.html?page=viewInd&id=48082&contestId=2"}]
		};
		var action_links = [{ 'text': 'Vote for Bill The Aircraft Carrier!', 'href': 'http://spayday.factor360.com/contest.html?page=viewInd&id=48082&contestId=2'}];
		FB.Connect.streamPublish(message, attachment, action_links, null, "Share your support for Bill The Aircraft Carrier!", this.stream_callback, false, null);
		*/
	},
	stream_callback : function ( post_id, exception ) {
		
		/*
		var self = this;
		
		if (post_id) {
			var queryParams = { 
				"fb_id" 	: fb_connect.userID,
				"result_1" 	: fb_connect.result_1,
				"result_2" 	: fb_connect.result_2,
				"firstname"	: "", // fb_connect.getUserName (  ), NOT ALLOWED
				"lastname"	: "", // fb_connect.getUserLastName (  ), NOT ALLOWED
				"email"		: fb_connect.getProxiedMail (  ),
				"countryID"	: fb_connect.getCountryCode (  )
			};
			
			streamObject.ajaxFunction ( "http://campaign.sloggi.com/is/scripts/class_php/GatewayHandler.php?mode=fbpost&action=post", queryParams );
		}
		*/
	},
	ajaxFunction : function ( _url, _params ) {
		 var self = this;
		 
		 var url 	= _url;
		 var params = _params;
		 var paramstring = "";
		 
		 for ( var i in params )
		 	{
		 		paramstring += "&" + i + "=" + params[i];
		 	}
		 
		 var xmlHttp;
		 
          try {
            // Firefox, Opera 8.0+, Safari
          	xmlHttp=new XMLHttpRequest();
          }
          catch ( e ) {
            // Internet Explorer
          	try {
            	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            } catch ( e ) {
            	try {
                	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                } catch ( e ) {
                	alert("Your browser does not support AJAX!");
                	return false;
                }
              }
            }
            xmlHttp.onreadystatechange = function (  ) {
            	if  ( xmlHttp.readyState == 4 ) {
            		self.onRequestComplete (  );
            	}
            }
            
    	xmlHttp.open("POST",url + paramstring,true);
        xmlHttp.send(null);
	},
	onRequestComplete : function ( data ) {
		fb_connect.result_1 = null;
		fb_connect.result_2 = null;
	},
	onAjaxError : function (  ) {
		fb_connect.result_1 = null;
		fb_connect.result_2 = null;
	}
}

function FB_connect (  ) {
    var self = this;
    
    var self = this;
	
	this.userID 		= null;
	this.userData 		= {};
	this.countryCode 	= null;
	this.loginStatus 	= false;
	
	this.flashMovieObject	= new flashObject (  )
	this.flashMovie			= this.flashMovieObject.getFlashMovie (  );
	
	this.key    		= null;
    this.secret 		= null;
}

FB_connect.prototype = {
    setAPIKey : function ( _key ) {
        this.key = _key;
    },
    setAPISecret : function ( _secret ) {
        this.secret = _secret;
    },
    getAPIKey : function (  ) {
        return this.key;
    },
    init : function (  ) {
    	var self = this;
    	
    	FB.Facebook.init ( self.key, "xd_receiver.htm" );
    	
    	FB.Connect.get_status (  ).waitUntilReady ( function ( status ) {
			switch ( status ) {
				case FB.ConnectState.connected:
					self.loginStatus = true;
					
					var sess = FB.Facebook.apiClient.get_session (  );
					
					if ( sess != null ) {
						self.userID = FB.Facebook.apiClient.get_session (  ).uid;	
					}
				break;
				case FB.ConnectState.appNotAuthorized:
				case FB.ConnectState.userNotLoggedIn:
					self.loginStatus = false;
				break;
			}
		});
    },
    logIn : function (  ) {
    	var self = this;
    	
    	switch ( self.loginStatus ) {
    		case true:
    			fb_connect.getProfileData (  );
				self.sendUserDataToFlash ( self.flashMovieObject.CALLBACK_SET_USER_DATA );
    		break;
    		case false:
    			FB.Connect.requireSession ( fb_connect.sendLoginStatus, function() { fb_connect.onCancel(); } );
    		break;
    	}
    },
    logOut : function (  ) {
       //  FB.Connect.logout ( this.sendLogoutStatus );
       var self = this;
       
       FB.Connect.logout ( self.sendUserDataToFlash ( self.flashMovieObject.CALLBACK_LOGOUT ) );
    },
    checkFBConnect : function (  ) {
       	var self = this;
    	
    	FB.Connect.forceSessionRefresh ( null );
    	
    	FB.Connect.get_status (  ).waitUntilReady ( function ( status ) {
			switch ( status ) {
				case FB.ConnectState.connected:
					self.loginStatus = true;
					
					// FB.Connect.showPermissionDialog ( "stream_publish, email", function ( e ) {  } );
					
					var sess = FB.Facebook.apiClient.get_session (  );
					
					if ( sess != null ) {
						self.userID = FB.Facebook.apiClient.get_session (  ).uid;	
					}
					
					fb_connect.getProfileData (  );
				break;
				case FB.ConnectState.appNotAuthorized:
				case FB.ConnectState.userNotLoggedIn:
					self.loginStatus = false;
					
					self.sendUserDataToFlash ( self.flashMovieObject.CALLBACK_LOGIN );
				break;
			}
		});
		
		/*
    	switch ( self.loginStatus ) {
    		case true:
    			fb_connect.getProfileData (  );
    		break;
    		case false:
    			self.sendUserDataToFlash ( self.flashMovieObject.CALLBACK_LOGIN );
    		break;
    	}
    	*/
    	return false;
    },
    onCancel : function (  ) {
    	var self = this;
    	
    	// fb_connect.flashMovie.fb_enableSite (  );
    	
		self.sendUserDataToFlash ( self.flashMovieObject.CALLBACK_ENABLE_SITE );
    	
    	trackItem ( "Cancel Facebook connect" );
    	
    	return false;
    },
    testIsLogged : function ( _type ) {
    	alert ( _type );
    },
    testIsLoggedOff : function ( _type ) {
    	alert ( _type );
    },
    getProfileData : function (  ) {
    	var self 		= this;
    	var userInfo 	= null;
    	
    	FB.Facebook.apiClient.users_getInfo ( self.userID, ["email", "affiliations", "current_location", "sex", "first_name", "last_name", "name", "pic", "is_app_user", "proxied_email"], function ( result, ex ) {
			
			if ( result != null ) {
				userInfo = result.slice (  );
				
				self.userData = userInfo;
				
				for ( var i in self.userData ) {
					for ( var ii in self.userData[0] ) {
						// alert ( ii + " - " + self.userData[0][ii] );
					}
				}
				
				self.sendUserDataToFlash ( self.flashMovieObject.CALLBACK_SET_USER_DATA );
			} else {
				FB.Connect.forceSessionRefresh ( null );
			}
		});
    },
    getFriends : function ( faceBookPageInvite_0, faceBookPageInvite_1, faceBookPageInvite_2, faceBookPageInvite_3, faceBookPageID ) {
    	
		var finalURL = "?countryCode=" + this.getCountryCode (  ) + "&pageID=" + faceBookPageID + "&text0=" + escape ( htmlentities ( faceBookPageInvite_0 ) ) + "&text1=" + escape ( htmlentities ( faceBookPageInvite_1 ) ) + "&text2=" + escape ( htmlentities ( faceBookPageInvite_2 ) ) + "&text3=" + escape ( htmlentities ( faceBookPageInvite_3 ) );
		switch ( this.getCountryCode (  ) )
			{
				case "ru":
				case "pl":
				case "hu":
				case "si":
				case "hr":
				case "ro":
				case "bg":
				case "cz":
				case "gr":
				case "fr":
				case "be.off":
				case "nl":
				case "demo":
					var finalURL = "?countryCode=" + this.getCountryCode (  ) + "&pageID=" + faceBookPageID + "&text0=" + faceBookPageInvite_0  + "&text1=" + faceBookPageInvite_1 + "&text2=" + faceBookPageInvite_2 + "&text3=" + ( htmlentities ( faceBookPageInvite_3 ) );
					
				break;
			}
		
		$.facebox.settings.boxTitle = faceBookPageInvite_0;
		jQuery.facebox({ajax: "fb_invite_iframe.php" + finalURL});
		
		window.value = fb_connect;
    },
    closeOverlay : function (  ) {
    	fb_connect.dialog.close (  );
    },
    sendUserDataToFlash : function ( _actionType ) {
    	switch ( _actionType ) {
    		case this.flashMovieObject.CALLBACK_LOGIN:
    			if ( this.flashMovie.fb_logIn != null ) {
					this.flashMovie.fb_logIn (  );
				}
    		break;
    		case this.flashMovieObject.CALLBACK_LOGOUT:
    			if ( this.flashMovie.fb_logOut != null ) {
					this.flashMovie.fb_logOut (  );
					this.flashMovie.fb_enableSite (  );
				}
    		break;
    		case this.flashMovieObject.CALLBACK_ALREADY_LOGGED_IN:
    			if ( this.flashMovie.fb_setUserData != null ) {
					this.flashMovie.fb_setUserData ( this.userData );
				}
    		break;
    		case this.flashMovieObject.CALLBACK_ENABLE_SITE:
    			if ( this.flashMovie.fb_enableSite != null ) {
					this.flashMovie.fb_enableSite (  );
				}
    		break;
    		case this.flashMovieObject.CALLBACK_SET_USER_DATA:
    			if ( this.flashMovie.fb_setUserData != null ) {
					this.flashMovie.fb_setUserData ( this.userData );
				}
    		break;
    	}
    },
    postMsg : function ( _flashVal1, _flashVal2, faceBookPagePost_0, faceBookPagePost_1, faceBookPagePost_2, faceBookPageID ) {
    	this.result_1 = _flashVal1;
    	this.result_2 = _flashVal2;
    	
    	// FB.Connect.forceSessionRefresh ( null );
    	
    	streamObject = new StreamObject (  );
    	streamObject.publishStream ( this.getUserName (  ), this.userID, _flashVal1, _flashVal2, faceBookPagePost_0, faceBookPagePost_1, faceBookPagePost_2, faceBookPageID );
    },
    handleMessage : function ( perms ) {
    	
    },
    sendLoginStatus : function (  ) {
    	// var flash 		= flash = new flashObject (  )
    	// var flashMovie	= flash.getFlashMovie (  );
    	
    	var sess = FB.Facebook.apiClient.get_session (  );
        
    	if ( sess == null ) {
    		FB.Connect.forceSessionRefresh ( null );
		} else {
			// FB.Connect.showPermissionDialog ( "stream_publish" );
			// FB.Connect.showPermissionDialog ( "stream_publish,email", function ( e ) { alert ( e ) } );
			
			FB.ensureInit ( function (  ) {
				FB.Connect.requireSession ( function (  ) {
					FB.Facebook.apiClient.users_hasAppPermission ( "publish_stream,email", function ( _result ) {
						if ( _result == 0 ) {
							// show the facebook permission dialog
							FB.Connect.showPermissionDialog ( "publish_stream,email", function ( _result ) {
								if ( !result ) {
									// no permisson granted, pop up facebook dialog to post;
									// alert ( "no permisson granted, pop up facebook dialog to post" );
									// streamObject.publishStream ( this.getUserName (  ), this.userID, _flashVal1, _flashVal2, faceBookPagePost_0, faceBookPagePost_1, faceBookPagePost_2, faceBookPageID );
								} else {
									FB.Connect.forceSessionRefresh ( function (  ) {
										// permission granted, post without facebook dialog
										// alert ( "permission granted, post without facebook dialog" );
										// streamObject.publishStream ( this.getUserName (  ), this.userID, _flashVal1, _flashVal2, faceBookPagePost_0, faceBookPagePost_1, faceBookPagePost_2, faceBookPageID );
									} )
								}
							} )
						}
					} )
				} )
			} );
			
			fb_connect.userID = FB.Facebook.apiClient.get_session (  ).uid;
			fb_connect.getProfileData (  );
		}
    },
    getUserID : function (  ) {
        return this.userID;
    },
    getUserName : function (  ) {
        return this.userData[0].first_name;
    },
    getUserLastName : function (  ) {
        return this.userData[0].last_name;
    },
    getProxiedMail : function (  ) {
    	return this.userData[0].proxied_email;
    },
    setCountryCode : function ( _countryCode ) {
    	this.countryCode = _countryCode;
    },
    getCountryCode : function (  ) {
    	return this.countryCode;
    }
}

// flash calls
function checkFB (  ) {
	setTimeout ( "fb_connect.checkFBConnect (  )", 1000 );
}

function checkLogin (  ) {
	// fb_connect.checkFBConnect (  );
	setTimeout ( "fb_connect.logIn (  )", 1000 );
}

function post ( _flashVal1, _flashVal2, faceBookPagePost_0, faceBookPagePost_1, faceBookPagePost_2, faceBookPageID ) {
	fb_connect.postMsg ( 
		( _flashVal1 == null ) ? "_flashVal1" : _flashVal1, 
		( _flashVal2 == null ) ? "_flashVal2" : _flashVal2, 
		( faceBookPagePost_0 == null ) ? "faceBookPagePost_0" : faceBookPagePost_0,
		( faceBookPagePost_1 == null ) ? "faceBookPagePost_0" : faceBookPagePost_1,
		( faceBookPagePost_2 == null ) ? "faceBookPagePost_0" : faceBookPagePost_2,
		( faceBookPageID == null ) ? "_pageID" : faceBookPageID
	);
}

function getFriends ( faceBookPageInvite_0, faceBookPageInvite_1, faceBookPageInvite_2, faceBookPageInvite_3, faceBookPageID ) {
	fb_connect.getFriends ( 
		faceBookPageInvite_0,
		faceBookPageInvite_1,
		faceBookPageInvite_2,
		faceBookPageInvite_3,
		faceBookPageID
	);
}

function logout (  ) {
	fb_connect.logOut (  );
}

// ---------------------

function get_html_translation_table (table, quote_style) {
    // Returns the internal translation table used by htmlspecialchars and htmlentities  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/get_html_translation_table
    // +   original by: Philip Peterson
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: noname
    // +   bugfixed by: Alex
    // +   bugfixed by: Marco
    // +   bugfixed by: madipta
    // +   improved by: KELAN
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Frank Forte
    // +   bugfixed by: T.Wild
    // +      input by: Ratheous
    // %          note: It has been decided that we're not going to add global
    // %          note: dependencies to php.js, meaning the constants are not
    // %          note: real constants, but strings instead. Integers are also supported if someone
    // %          note: chooses to create the constants themselves.
    // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
    // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
    
    var entities = {}, hash_map = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
 
    useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
 
    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        throw new Error("Table: "+useTable+' not supported');
        // return false;
    }
 
    entities['38'] = '&amp;';
    if (useTable === 'HTML_ENTITIES') {
        entities['160'] = '&nbsp;';
        entities['161'] = '&iexcl;';
        entities['162'] = '&cent;';
        entities['163'] = '&pound;';
        entities['164'] = '&curren;';
        entities['165'] = '&yen;';
        entities['166'] = '&brvbar;';
        entities['167'] = '&sect;';
        entities['168'] = '&uml;';
        entities['169'] = '&copy;';
        entities['170'] = '&ordf;';
        entities['171'] = '&laquo;';
        entities['172'] = '&not;';
        entities['173'] = '&shy;';
        entities['174'] = '&reg;';
        entities['175'] = '&macr;';
        entities['176'] = '&deg;';
        entities['177'] = '&plusmn;';
        entities['178'] = '&sup2;';
        entities['179'] = '&sup3;';
        entities['180'] = '&acute;';
        entities['181'] = '&micro;';
        entities['182'] = '&para;';
        entities['183'] = '&middot;';
        entities['184'] = '&cedil;';
        entities['185'] = '&sup1;';
        entities['186'] = '&ordm;';
        entities['187'] = '&raquo;';
        entities['188'] = '&frac14;';
        entities['189'] = '&frac12;';
        entities['190'] = '&frac34;';
        entities['191'] = '&iquest;';
        entities['192'] = '&Agrave;';
        entities['193'] = '&Aacute;';
        entities['194'] = '&Acirc;';
        entities['195'] = '&Atilde;';
        entities['196'] = '&Auml;';
        entities['197'] = '&Aring;';
        entities['198'] = '&AElig;';
        entities['199'] = '&Ccedil;';
        entities['200'] = '&Egrave;';
        entities['201'] = '&Eacute;';
        entities['202'] = '&Ecirc;';
        entities['203'] = '&Euml;';
        entities['204'] = '&Igrave;';
        entities['205'] = '&Iacute;';
        entities['206'] = '&Icirc;';
        entities['207'] = '&Iuml;';
        entities['208'] = '&ETH;';
        entities['209'] = '&Ntilde;';
        entities['210'] = '&Ograve;';
        entities['211'] = '&Oacute;';
        entities['212'] = '&Ocirc;';
        entities['213'] = '&Otilde;';
        entities['214'] = '&Ouml;';
        entities['215'] = '&times;';
        entities['216'] = '&Oslash;';
        entities['217'] = '&Ugrave;';
        entities['218'] = '&Uacute;';
        entities['219'] = '&Ucirc;';
        entities['220'] = '&Uuml;';
        entities['221'] = '&Yacute;';
        entities['222'] = '&THORN;';
        entities['223'] = '&szlig;';
        entities['224'] = '&agrave;';
        entities['225'] = '&aacute;';
        entities['226'] = '&acirc;';
        entities['227'] = '&atilde;';
        entities['228'] = '&auml;';
        entities['229'] = '&aring;';
        entities['230'] = '&aelig;';
        entities['231'] = '&ccedil;';
        entities['232'] = '&egrave;';
        entities['233'] = '&eacute;';
        entities['234'] = '&ecirc;';
        entities['235'] = '&euml;';
        entities['236'] = '&igrave;';
        entities['237'] = '&iacute;';
        entities['238'] = '&icirc;';
        entities['239'] = '&iuml;';
        entities['240'] = '&eth;';
        entities['241'] = '&ntilde;';
        entities['242'] = '&ograve;';
        entities['243'] = '&oacute;';
        entities['244'] = '&ocirc;';
        entities['245'] = '&otilde;';
        entities['246'] = '&ouml;';
        entities['247'] = '&divide;';
        entities['248'] = '&oslash;';
        entities['249'] = '&ugrave;';
        entities['250'] = '&uacute;';
        entities['251'] = '&ucirc;';
        entities['252'] = '&uuml;';
        entities['253'] = '&yacute;';
        entities['254'] = '&thorn;';
        entities['255'] = '&yuml;';
        entities['ё'] = '&#1077;';
    }
 
    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#39;';
    }
    entities['60'] = '&lt;';
    entities['62'] = '&gt;';
 
 
    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        hash_map[symbol] = entities[decimal];
    }
    
    return hash_map;
}

function htmlentities (string, quote_style) {
    // Convert all applicable characters to HTML entities  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/htmlentities
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: nobbler
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // -    depends on: get_html_translation_table
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
    // *     example 2: htmlentities("foo'bar","ENT_QUOTES");
    // *     returns 2: 'foo&#039;bar'
    var hash_map = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
    
    if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }
    hash_map["'"] = '&#039;';
    for (symbol in hash_map) {
        entity = hash_map[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
    
    return tmp_str;
}
