//* String Methods *//
String.prototype.ucFirst=function(){return(this.charAt(0).toUpperCase()+this.substr(1,this.length));}
String.prototype.stripSlashes=function(){return(this.replace(new RegExp("/+","g"),"/"));}
String.prototype.ltrim=function(){return this.replace(/^\\s+/,'');}
String.prototype.rtrim=function(){return this.replace(/\\s+$/,'');}
String.prototype.trim=function(){return this.replace(/^\\s+|\\s+$/g,'');}
String.prototype.chop=function(n){if(isNaN(n)){n=this.length-1;}return(this.substring(0,n));}
String.prototype.stripPrecedingSlashes=function(){
	try{
		return this.replace(new RegExp("/+","g"),"/").substring(1);
	} catch(e){}
}


function numOrdA(a, b){ return (a-b); }
function numOrdD(a, b){ return (b-a); }


//* Utility Methods *//
if(typeof TSCM.util=="undefined"){TSCM.util={};}
TSCM.util.isDefined=function(v){if(typeof v=='undefined'||v==null||v==''||v=='undefined'){return false}else{return true};}


TSCM.util.GetCookie = function (splitby,name) {

    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return TSCM.util.getCookieVal(splitby,j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }

    return null;
}

TSCM.util.getCookieVal =function(splitby,offset) {
    var endstr = document.cookie.indexOf (splitby,offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

var leftNavIncludeName="";




/* save/retrieve object from cookie 
usage:
   var state = new TSCM.util.PersistenceManager();
   state.id = "some_unique_string"; 
   state.expiry_days = <int number_of_days> 
   save object:       state.save(object o);
   get  object:       var o = TSCM.util.PersistenceManager.get();
*/


TSCM.util.PersistenceManager = function(ob){
   this.data = null;
   this.id = "tscs_data";
   this.expiry_days = 365;
   this.path = "/";
   this.flashid = null;
   this.session_cookie = false;

   this.get = function(){
      var s = this.getCookie(this.id);
      try {
         var str = "var o = " + s + "";
         eval(str);
         this.data = o;
      }catch(e){
      return null;
      }
      return this.data;
   }

   this.getstr = function(){
      return this.data_str;
   }

   this.getdata = function(){
      return this.data;
   }


   this.setData = function(ob){

      if(typeof ob == "object"){
         this.data = ob;
         try{
            this.data_str = JSON.stringify(ob);
         }catch(e){
            this.data_str = "";
         }
      }else if (typeof ob == "string"){
         this.data_str = ob;
         try{
             eval("var o = " + this.data_str);
             this.data = o;
         }catch(e){
            this.data = e;
         }
      }
   }

   this.setDataStr = function(str){
      this.data_str = str;
   }

   this.save = function(ob){
      if(typeof ob!="undefined"){ 
         this.setData(ob);
      }
      this.setCookie(this.id,this.data_str,this.expiry_days);
   }


   this.setCookie = function (cookieName,cookieValue,num_days) {
      var today = new Date();
      var expiry = new Date();
      if (num_days==null || num_days==0) num_days=this.expiry_days;
      expiry.setTime(today.getTime() + 3600000*24*num_days);
      try {
         var cs;
         if(this.session_cookie == true){
            cs = escape(cookieName ) + "=" + escape(cookieValue) + ";path=" + this.path; 
         }else{
            cs = escape(cookieName ) + "=" + escape(cookieValue) + ";expires=" + expiry.toGMTString() + ";path=" + this.path; 
         }
         document.cookie = cs;
      } catch(e){}
   }

   this.getCookie = function(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;
       }
       var t = unescape(document.cookie.substring(begin + prefix.length, end));
       return t;
   }

   this.serialize = function(ob){
      window.status = 'flash-save not implemented';
      return;
      if(typeof ob!="undefined")this.setData(ob);
      if(this.flashid == null){
         throw "error-flash object not defined";
      }else{
         var fl = document.ElementById(this.flashid);
         fl.SetVariable("mtvi_yeti_data",this.data_str);
      }
   }

   /* main */
   this.setData(ob);

}


function debug(msg){
   try {
      h = document.getElementById("debug").innerHTML;
      document.getElementById("debug").innerHTML=msg + "<br>" + h ;
   
   }catch(e){
      //alert(e.message);
   }


}

TSCM.util.getParameter = function(name){
   var qs = window.location.search;
   //var qs = top.window.location.search;

   if(top.location != self.location){
      qs = top.window.location.search;
   }


   var EQ = "=";
   var AMP = "&";
   var param = name + EQ;

   try {
      var loc = qs.indexOf(param);
      if(loc !=-1){
         var start = loc + param.length;
         var ss = qs.substring(start);
         var end = ss.indexOf(AMP);
         if(end != -1){
            return ss.substring(0,end);
         }else{
            return ss.substring(0);
         }
      }
   }catch(e){
      return "";
   }
}

var setText = function(id,txt){
   try {
      YAHOO.util.Dom.get(id).innerHTML = txt;
   }catch(e){
   }
}
   

TSCM.util.setText = function(id,txt){
   try {
      YAHOO.util.Dom.get(id).innerHTML = txt;
   }catch(e){
   }
}
   
TSCM.util.rmnonAlpha = function(txt){
   try {
      return txt.replace(/\ |\[|\]|\*|\!|\@|\#|\$|\%|\^|\&|\(|\)|\-|\:|\'|\"|\<|\>|\?|\\|\/|\~|\`/g,"");
   }catch(e){
      return;
   }
}
   

TSCM.util.attachScript = function(id,url){
   // your thing should have a callback
   var scr= document.createElement("script");
   scr.type = "text/javascript";
   scr.defer = true;
   scr.id = id;
   scr.src = url;
   var s = document.getElementById(id);
   var head = document.getElementsByTagName('head')[0]; 
   try {
      if(s){
         head.removeChild(s); 
      }
   }catch(e){
      //debug(e.message);
   }
   head.appendChild(scr);
   return;
}


function getEl(id){
   return YAHOO.util.Dom.get(id);
}


TSCM.register("ratings");

TSCM.ratings.getRecFromRating = function(the_letter){
   var the_rec;
   if(typeof the_letter != "string"){
      //console.log('error - no letter' );
      return '-';
   }
   if((the_letter.indexOf("A")!=-1)|| (the_letter.indexOf("B")!=-1)) the_rec = "BUY";
   if((the_letter.indexOf("C")!=-1)) the_rec = "HOLD";
   if((the_letter.indexOf("D")!=-1)||(the_letter.indexOf("E")!=-1)|| (the_letter.indexOf("F")!=-1)) the_rec = "SELL";
   return the_rec;
}

function log(msg) { 
   // use the error console if available (FF+FireBug or Safari) 
   if (typeof console != "undefined") { 
      console.log(msg); 
      // write the msg to a well-known div element 
   } else { 
      var el = document.getElementById("consoleelement"); 
      if (el) { 
         el.innerHTML += "<p>" + msg + "</p>"; 
      } 
   } 
} 


var attachScript = function(id,url){
   // your thing should have a callback
   var scr= document.createElement("script");
   scr.type = "text/javascript";
   scr.defer = true;
   scr.id = id;
   scr.src = url;
   var s = document.getElementById(id);
   var head = document.getElementsByTagName('head')[0]; 
   try {
      if(s){
         head.removeChild(s); 
      }
   }catch(e){
      //debug(e.message);
   }
   head.appendChild(scr);
   return;
}


TSCM.util.Quote =  function(callback,symb){

   // http://custom.marketwatch.com/custom/thestreet-com/xml-quote.asp?symb=dj,bby,xyq19,notasymbol&output=json&callback=quoteResultHandler
   this.server="http://custom.marketwatch.com"; 
   this.path="/custom/thestreet-com/xml-quote.asp?";
   this.params = {
      output:"json",
      symb:null,
      callback:null
   }

   this.params.callback = callback;
   this.params.symb = symb;

   this.getParams = function(){
      var s ='';
      for (var i in this.params){
         var o = this.params[i];
         if(o instanceof Array){
            var str = i;
            for(var j=0;j<o.length;j++){
               str+=o[j];
            }
            if(s!='') s+= '&';
            s += str;

         } else {
            if(s!='') s+= '&';
            str = i + "=" + this.params[i];
            s += str;
         }
      }
      return s;
   }

   this.url= this.server + this.path + this.getParams();
   this.get = function(){
     return attachScript(YAHOO.util.Dom.generateId(),this.url);
   }

   return this.url;

}

TSCM.util.addCommas = function(str){

   str += '';
   x = str.split('.');
   x1 = x[0];
   x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
   }
   return x1 + x2;

}

