base_url='http://roohit.com/' ;

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function removeHTMLTags(htmlstring) {
	var re = /(<([^>]+)>)/gi;
	for (i=0; i < htmlstring.length; i++)
		htmlstring[i].value = htmlstring[i].value.replace(re, "");
}

function removeHTMLTags2(htmlstring) {
	if(htmlstring) {
		var stringdiv = document.createElement("div");
		stringdiv.innerHTML = htmlstring;
		if(document.all) { return stringdiv.innerText; }
		else { return stringdiv.textContent; }
	}
	return "";
}
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags3(htmlstring){
 		/* 
  			This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	htmlstring = htmlstring.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		var strTagStrippedText = htmlstring.replace(/<\/?[^>]+(>|$)/g, "");
		return strTagStrippedText ;
}


function createXMLHttp()
{
   if (typeof XMLHttpRequest != "undefined")    { return new XMLHttpRequest();   }
   else if (window.ActiveXObject)
   {
      var aVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
      for (var i = 0; i < aVersions.length; i ++ )
      {
         try
         {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
         }
         catch (oError) {}         // Do nothing
      }
   }
   throw new Error("XMLHttp object could be created.");
}

function do_post(url, data, suc)
{
   var oXmlHttp = createXMLHttp();
   var method_ = "";
   if(data == "" || data == null)  method_ = "get";
   else method_ = "post";
   oXmlHttp.open(method_, url , true);
   oXmlHttp.onreadystatechange = function ()
   {
      if (oXmlHttp.readyState == 4)
      {
         if (oXmlHttp.status == 200)
         {
            var response ;
            response = oXmlHttp.responseText;
            if(suc == onMailResponse)
            {
               suc(response);
            }
            else
            {
               var transport = new Object;
               transport.responseText = response;
               suc(transport);
            }
         }
      }
   }
   ;
   // if (data != "" || data != null) oXmlHttp.overrideMimeType('text/html');
   if (data != "" || data != null) oXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   if(data == "" || data == null ) oXmlHttp.send(null);
   else
   {
      oXmlHttp.send(data);
   }
}

function fbIt(shinyurl, shortcutId, msgToPost)
{
    var tweet = removeHTMLTags3( trim(msgToPost) ) ;
    //var tweet = trim(msgToPost) ;
    var urlFrom = shinyurl + shortcutId ;
    /*
    var urlMsg = '..' + tweet + '..' + ' [See this hilighted at ' + urlFrom + ']' ;
    var urlMsg = '[' + urlFrom + ' has entire page with this highlighted for you.]\r\n ' + tweet ;
    var urlMsg = '[See ' + urlFrom + ' for entire page with the following text HIGHLIGHTED FOR YOU.]\r\n ' + tweet ;
    var urlMsg = '[See page - highlighted for you - at ' + urlFrom + ']\r\nHighlights:\r\n' + tweet ;
    */
    var urlMsg = '[See ' + urlFrom + ' for page highlighted for you.]\r\n\r\nHighlights are: ' + tweet ;
    tweet = urlMsg ;
//tweet = stripslashes(tweet) ;
    window.location = "http://apps.facebook.com/rooh_it/?msg=" + encodeURIComponent(tweet) + "&url=" + self.location ; // + shinyurl + shortcutId ; // Rest commented out because this is from view1 page and we dont want the user to leave view1
    trackSharing( urlFrom, "Facebook_View") ;
}

function fbIt_v1st(shinyurl, shortcutId, msgToPost)
{
        var promo2 = "" ;
        var tweet = trim(msgToPost) ;
    if (tweet.length > 100)
        tweet = trim(tweet.substring(0,99)) + ".." ;
    else if (tweet.length < 50)
        promo2 = " (Its FREE, No Signup reqd)" ;

    	var url = shinyurl;
    if (shortcutId.length > 1)          //actually greater than zero is all we need
    {
    		url = url + shortcutId;
            promo = ": See MY Hilights " + shinyurl + shortcutId ; // 6+1 +8+1+2+1+  15+ 5 = 34
    }
    else
    {
            promo = " Free Hilights by " + shinyurl ;
    }
    tweet += promo + promo2 ;
        //alert (tweet) ;
    //window.location.href = "http://www.facebook.com/sharer.php?u=" + shinyurl + shortcutId + "&t=" + encodeURIComponent(document.title) ;

//   // First, get the array of meta-tag elements
//    metatags = document.getElementsByTagName("meta");
//    // Iterate through the array, listing them all
//    // Update only the Title meta tag
//    for (cnt = 0; cnt < metatags.length; cnt++)
//    {
//        var name = metatags[cnt].getAttribute("name");
//        var content = metatags[cnt].getAttribute("content");
//
//        // List the original meta tag name=content
//        document.write(name + " = " + content + "");
//
//        // Update the Title meta tag
//        if (metatags[cnt].getAttribute("name") == "Title")
//              metatags[cnt].setAttribute("content", "SeeMyHighlights at "+shinyurl+shortcutId);
//    }

    //trackSharing("Facebook") ;
    window.location.href = "http://www.facebook.com/sharer.php?u=" + url + "&t=" + encodeURIComponent(tweet) ;
}

function tweetIt(shinyurl, shortcutId, msgToPost)
{
    var promo = "" ;
    var tweet = removeHTMLTags2( trim(msgToPost) ) ;
    tweet = stripCRLFnTabs(tweet) ;

    if (tweet.length > 100) 
    {
        tweet = trim(tweet.substring(0,99)) ;	// 100
    }
    tweet += "..." ; // 3
	
    if (shortcutId.length > 0)	// IF we have a valid shinyurl, show it
    {
        promo = "My #hilights at " + shinyurl + shortcutId ; // 2+1 9+1 2+1 7+4+1+2+1 +5 = 36
    }


    if (tweet.length > 60)
    {	
        if (shortcutId.length > 0) {}
        else	// There is no shinyurl available: show Generic Promotion
        {
            promo = "Make FREE #hilights at " + shinyurl ;	// 38 (including un-needed trailing slash) <<<<<<<< NO NEED to remove trailing slash it works on Twitter as it is
        }
    }
    else // 60		// user message is small enuff that we can add more of our own marketing
    {
        if (shortcutId.length > 0)		// but not that much, so keep it short
        {
            promo += " Type " + shinyurl + " before any URL" ;	// 35
            if (tweet.length < 45)
            {
            promo += " to hilight." ;		// 12
            }
        }
        else	// We have more space, show longer message
        {
            promo = " Type " + shinyurl + " before any URL to make FREE #hilights" ;	// 59
            if (tweet.length < 45)
            {
            promo += " of your own." ;		// 13
            }
        }
    }

    tweet += promo ;
    //alert (tweet) ;
   window.open ( "http://twitter.com/home?via=@rooh_it&status=" + encodeURIComponent(tweet) ) ;
   trackSharing( shinyurl+shortcutId, "Twitter_View") ;
}





function trackSharing(urlShared, medium)
{
   data = "sesid=" + '' ; 
   data = data.trim() ;
   data += "&url=" + urlShared ;
   data += "&medium=" + medium ;
   var url4ajax =  base_url + "emtrk.php" ;
   do_post(url4ajax, data);
}

/*
function addslashes(str) 
{
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\0/g,'\\0');
    return str;
}

function stripslashesi_FROMWEB(str) 
{
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\0/g,'\0');
    str=str.replace(/\\\\/g,'\\');
    return str;
}
*/
function stripslashes(str) 
{
    str=str.replace(/\\/g,'');
    return str;
}
function stripCRLFnTabs(str)
{
	str = str.replace(/[\r\n\t]+/g," ") ;
	return str.replace(/[ ]+/g," ") ;
}
function stripEncodedCRLF(str)
{
	return str.replace(/(%0[dD]%0[aA])+/g," ") ;
}

