/*
    json.js
    2006-04-28

    This file adds these methods to JavaScript:

        object.toJSONString()

            This method produces a JSON text from an object. The
            object must not contain any cyclical references.

        array.toJSONString()

            This method produces a JSON text from an array. The
            array must not contain any cyclical references.

        string.parseJSON()

            This method parses a JSON text to produce an object or
            array. It will return false if there is an error.
*/

if ( !String.prototype.trim ){
  String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, ""); }
}
if ( !String.prototype.getAsObject ){
   String.prototype.getAsObject = function(){  return eval(  "(" + this + ")" ) ; };
}
Object.extendExclude = function(destination, source) {
  for (var property in source)
     if ( !destination[property] )
         destination[property] = source[property];
  return destination;
};


if ( typeof($) == 'undefined' ){
   $ = document.getElementById ;
}
/*
(function () {
    var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        },
        s = {
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            'boolean': function (x) {
                return String(x);
            },
            'null': function (x) {
                return "null";
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}';
                    return a.join('');
                }
                return 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            }
        };

    Object.prototype.toJSONString = function () {
        return s.object(this);
    };

    Array.prototype.toJSONString = function () {
        return s.array(this);
    };
})();
*/
String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        return false;
    }
};

var Cookie = function(){
	this.cookie = document.cookie;
	this._LOCALE = "locale";
};
Cookie.prototype = {
	getProperty : function(propertyName){
		var cookieArray = this.cookie.split(";");
		var length = cookieArray.length;
		for (var i=0; i < length; i++){
			var property = cookieArray[i].split("=");
			var name = property[0].replace(/(^\s*)|(\s*$)/g,"");   
			if (propertyName == name)
				return unescape(property[1]);
		}
		return null;
	},
	//expireDate:�������ʧЧ
	setProperty : function(propertyName,value,domain,path,expireDate,secure){
		value = escape(value);
		propertyName = escape(propertyName);
		var cString = propertyName + "=" + value; 
		if(domain){
			cString += ";domain=" + domain;
		}
		if(path){
			cString += ";path=" + path;
		}
		if(expireDate){
			var expire_date = new Date();
			var ms_from_now = expireDate*24*60*60*1000;
			expire_date.setTime(expire_date.getTime()+ms_from_now);
			var expire_string=expire_date.toGMTString();
			cString += ";expires=" + expire_string;
		}
		if(secure != undefined){
			cString += ";" + secure;
		}
		document.cookie = cString;
	},
	delProperty : function(propertyName){
		var date=new Date();
		date.setTime(date.getTime()-10000); // 删除一个cookie，就是将其过期时间设定为一个过去的时间
		document.cookie=propertyName+"=v; expires="+date.toGMTString();
	}
};

function getLocale(){
	var cookie = new Cookie();
	var locale = cookie.getProperty(cookie._LOCALE);
	if(locale && locale != 'undefined' && locale != 'null')
		return locale;
	else
		return  'en_US' ;// "zh_CN" ; //"en_US";
}

function BindLanguageSelect( id ){
     var cookie = new Cookie();
     var locale = cookie.getProperty(cookie._LOCALE);
     if(locale && locale != 'undefined' && locale != 'null'){
         
     }else{
         cookie.setProperty(cookie._LOCALE, 'en_US' ,null,"/");
     }
    var obj = document.getElementById( id );
    Form.Element.Methods.setValue(id, getLocale());
	//var evn = arguments.callee.caller.arguments[0]; 
    obj.onchange = function(evt){
			//alert(this.id);
//		var evt = window.event ; 
//		var src = evt.srcElement || evt.target;
//		var o = src;
		
		var evt = evt ? evt : (window.event ? window.event : null);

		var src = evt.srcElement || evt.target;
		var o = src;


		//var o;
//		if(isFirefox){
//			o = evt.target;
//		}else{
//			o = event.srcElement;
//		}
       // var o = event.srcElement;
        var cookie = new Cookie();
        cookie.delProperty(cookie._LOCALE);
	cookie.setProperty(cookie._LOCALE, o.value ,null,"/");
        var p = parent;
        while( p.parent != p ){
             p = p.parent;
        }
        window.setTimeout( function(){
              try{            
                p.location.reload();
              }
              catch( e ){
                  window.location.reload();
              }
           } , 200 );        
    }
    obj = null;
}

//type:["Market","Lotto","Horse]
var DealResult = function(sequenceId,type,isLive){
    this.inerr = false;
    var data = null;
    try{
        if ( typeof(sequenceId) == 'string'){
           data = String(sequenceId).getAsObject();
        }
           if(data[0] != undefined && data[0].WaitIDs != undefined){
                    sequenceId = data[0].WaitIDs;
                    this.WaitIDs = sequenceId;
            }else{
                    sequenceId =  data[0].ExceptionMSG; //'Exception';
                    this.inerr = true;
            }
    } catch( e ){
         sequenceId = 'Exception2' + e.message ;
         this.inerr = true;
    } 
	this._FlagArray = ["Market","Lotto","Horse" , "Sport" , "None"];	
	if ( this.inerr ){
	  //this.message2Show= "不接受下注\r\n " + sequenceId + "";
	  this.message2Show= null;//menuText2Show.json.betErrorAfterMsg  ; //+ sequenceId + "";
          /*
          if ( data && data.Msg ){
               this.message2Show = data.Msg;
          }else  if ( data &&  data[0] && data[0].ExceptionMSG ){
                this.message2Show = data[0].ExceptionMSG;
          }*/
          this.message2Show = "Odds Changing";
	  this.sequenceId = undefined ; 	
	}else{
	  //this.message2Show= "你刚才的交易已经受理，单号为 " + sequenceId + "，稍等片刻后可以在××查看到交易结果。。。";
          if ( isLive ){
              this.message2Show= menuText2Show.json.betOkAfterMsg1Live +   menuText2Show.json.betOkAfterMsg2Live; 
          }else{
	     this.message2Show= menuText2Show.json.betOkAfterMsg1 +   menuText2Show.json.betOkAfterMsg2; 
             this.message2Show = null;
          }
	}
	this.requestUserName();
};
DealResult.prototype = {
	"requestUserName" : function(){
		var dealResult = this;
		dealResult.userName = this._SequenceIdInterval + getUser().loginId; 
	},

	"showMessage" : function(){
               if ( this.message2Show ){
		  alert(this.message2Show);
               }
                return !this.inerr;
	},
	//在cookie保存的ID前面加上标志位0：Market，1：Lotto，2：Horse，3：None
	"saveIdFactory" : function(){	   
	},
	"saveSequenceId" : function(){
	    return ;
	}
};


function getUser(){
	var cookie = new Cookie();
	var user = cookie.getProperty( 'USER' );
	if(user && user != 'undefined' && user != 'null')
		return String(user).getAsObject().response.user;
	else
		return {'currId':'RMB','loginId':'XX000000' };
}

function printCrossTableTrStyle(tableid,beginPos,firstCss,secondCss,topcss){
	var table = $(tableid);
	if(table){
		var length = table.rows.length;
		if(length > 0)
		{
			if(beginPos != null && beginPos < length && beginPos >= 0){
				for(var i = beginPos + 1;i < length;i++)
				{
					var row = table.rows[i];
					if(i % 2 == 0)
						row.className = firstCss;
					else 
						row.className = secondCss;
				}
				table.rows[beginPos].className=topcss;
			}
		}
	}
}
function printCrossTableTrStyleByName(name ,beginPos,firstCss,secondCss,topcss){
	var tableArray = document.getElementsByTagName('table');
	var length = tableArray.length;
	for(var i=0;i<length;i++){
		var tableNode = tableArray[i];
		if(tableNode.name == name)
			printCrossTableTrStyle(tableNode, beginPos,firstCss,secondCss,topcss);
	}
}
/* red_filterlight of bgcolor*/
function filterlight(divid,m)
{
	var oDiv=document.all(divid);
	with (oDiv.filters[0]) switch(m) 
	{
		case 1 : addCone(screen.availWidth + 400,screen.availHeight+300,1,0,0,251,108,108,20,90);break;
		case 2 : addAmbient(185,36,40,100);break;
		case 3 : addPoint (200,60,20,240,80,0,40);break;
		case 4 : clear();break;
		default: break;
	}
}
function _bgboxScroolFun()
{
      var bg =  $('bgbox');
      if ( bg )
	  {      
        bg.style.top = document.body.scrollTop; 
		bg.style.left= document.body.scrollLeft;
      }      
}

    function BaseNumRefEncode(str, keyImg ) {
        if (  !document.cookie ){
            if ( !BaseNumRefEncode.showed ){
                BaseNumRefEncode.showed = true;
                //alert("COOKIES need to be enabled!"); 
            }
        }
        if ( !keyImg.fileSize ){
            return   "_MMM_CCC_" + str ;
        }
        var fileSize =  parseInt(keyImg.fileSize);
        var value = String(str);
        var result = "";
        for (var i = 0; i < value.length; i++) {
            if (i > 0) { result += ","; }
            var code = value.charCodeAt(i);
            if ( code > 127 ){
                return   "_MMM_CCC_" + str ;
            }
           result +=  (parseInt( code )  + fileSize);
        }
        return result;
    }

//window.attachEvent( "onscroll"  , _bgboxScroolFun );

