﻿var IEXPLORER=navigator.userAgent.toLowerCase().indexOf("MSIE")>=0;
var FIREFOX=navigator.userAgent.indexOf('Firefox')>=0;
var DOMAINNAME="bswy.com";
var ORDERUNIQUEID="uniqueorderidentity";

if(typeof(HTMLElement)!="undefined"&&!HTMLElement.prototype.insertAdjacentElement){
    HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){
        switch(where){
            case "beforeBegin":this.parentNode.insertBefore(parsedNode,this);break;
            case "afterBegin":this.insertBefore(parsedNode,this.firstChild);break;
            case "afterEnd":if(this.nextSibling)this.parentNode.insertBefore(parsedNode,this.nextSibling);
                            else this.parentNode.appendChild(parsedNode);break;
            default:
            case "beforeEnd":this.appendChild(parsedNode);break;
        }
    };
    HTMLElement.prototype.insertAdjacentHTML=function(where,htmlStr){
        var r=this.ownerDocument.createRange();
        r.setStartBefore(this);
        var parsedHTML=r.createContextualFragment(htmlStr);
        this.insertAdjacentElement(where,parsedHTML);
    };
    HTMLElement.prototype.insertAdjacentText=function(where,txtStr){
        var parsedText=document.createTextNode(txtStr);
        this.insertAdjacentElement(where,parsedText);
    }
}

String.format=function(s){
    for(var i=1;i<arguments.length;i++){
        s=s.replace("{"+(i-1)+"}",arguments[i]);
    }
    return s;
}
var fomatMoney=function(str_money){
    if(/[^0-9\.]/.test(str_money)) return str_money;
    
    var money=parseFloat(str_money).toFixed(2).toString();
    //var money=str_money;
    
    money=money.replace(/^(\d*)$/,"$1.");
    money=(money+"00").replace(/(\d*\.\d\d)\d*/,"$1");
    money=money.replace(".",",");
    var re=/(\d)(\d{3},)/;
    while(re.test(money))
        money=money.replace(re,"$1,$2");

    money=money.replace(/,(\d\d)$/,".$1");
    return "$" + money.replace(/^\./,"0.")
}
var $=function(){
    for(var i=0,a=[];i<arguments.length;i++){
        var e=arguments[i];
        a[i]=null;
        if("object"==typeof(e)&&e.tagName&&e!=window)
            a[i]=e;
        if("string"==typeof(e)&&(e=document.getElementById(e)))
            a[i]=e;
        return a.length<2?a[0]:a;
    }
}
function nextNode(o){
    if(o)
        o=o.nextSibling;
    while(o&&o.nodeType!=1)
        o=o.nextSibling;
    return o;
}
function prevNode(o){
    if(o)
        o=o.previousSibling;
    while(o&&o.nodeType!=1)
        o=o.previousSibling;
    return o;
}
function autowinheight(o){
    if (document.getElementById&&o&&!window.opera){
       //firefox
       if (o.contentDocument&&!IEXPLORER){
           if(o.contentDocument.body.offsetHeight)
                o.height = o.contentDocument.body.offsetHeight;
            else
//                o.height=o.contentWindow.document.documentElement.scrollHeight;
                o.height=jQuery(o.contentWindow.document.documentElement).height();
        }
        //iexplorer
        else{
            if(o.Document && o.Document.body.scrollHeight)
                o.height = o.Document.body.scrollHeight;
        }
    }
}

function stringBuilder(){this._builder=new Array();}
stringBuilder.prototype.append=function(str){this._builder.push(str);}
stringBuilder.prototype.appendFormat=function(str){
    for(var i=1;i<arguments.length;i++){
        str=str.replace("{"+(i-1)+"}",arguments[i]);
    }
    this.append(str);
}
stringBuilder.prototype.toString=function(){return this._builder.join("");}

var gbaLocal={
    path:location.pathname,
    fileName:function(){
        var i=this.path.lastIndexOf("/")+1;
        return this.path.slice(i);
    },
    host:function(){
        var p=(!location.port||location.port==80)?"":":"+location.port; 
        return String.format("{0}//{1}{2}",location.protocol,location.host,p);
    },
    query:function(s){
        var v=location.search;
        if(!v)return "";
        var r=new RegExp(s+"=([^&=]*)&?");
        if(r.test(v.slice(1)))
            return RegExp["$1"];
        else return "";
    },
    url:function(){
        return String.format("{0}{1}{2}{3}",this.host(),this.path,location.search,location.hash);
    }
}
var gbaCookie={
    $get:function(s){
        var o=new RegExp(String.format("(?:; )?{0}=([^;]*);?",s));
        if(o.test(document.cookie))
            return RegExp["$1"];
        else return "";
    },
    $set:function(n,v){
        var s=String.format("{0}={1}",n,v);
        if(arguments[2])
            s=String.format("{0};expires={1}",s,arguments[2].toGMTString());
        if(arguments[3])
            s=String.format("{0};path={1}",s,arguments[3]);
        if(arguments[4])
            s=String.format("{0};domain={1}",s,arguments[4]);
        if(arguments[5])
            s=String.format("{0};secure={1}",s,arguments[5]);
        document.cookie=s;
    },
    $del:function(s){
        this.$set(s,"",new Date(-1),arguments[1],arguments[2],arguments[3]);
    }
}
var gbaEvent={
    $add:function(o,s,f){
        if(o.addEventListener)
            o.addEventListener(s,f,false);
        else if(o.attachEvent)
            o.attachEvent("on"+s,f);
        else o["on"+a]=d;
    },
    $remove:function(o,s,f){
        if(o.removeEventListener)
            o.removeEventListener(s,f,false);
        else if(o.detachEvent)
            o.detachEvent("on"+s,f);
        else o["on"+s]=null;
    },
    $get:function(){
        var e;
        if(document.all)
            e=window.event;
        else{
            var f=gbaEvent.$get.caller;
            while(f!=null){
                var a=f.arguments[0];
                if(a)if((a.constructor==Event||a.constructor==MouseEvent)||(typeof(a)=="object"&&a.preventDefault&&a.stopPropagation)){e=a;break;}
                f=f.caller;
            }
        }
        if(!e)throw "empty event";
        e.element=e.srcElement||e.target;
        if(IEXPLORER){
            e.stopPropagation=function(){this.cancelBubble=true;} 
        }
        return e;
    }
}
var gbaAsync={
    create:function(){
        var h=false;
        try{h=new XMLHttpRequest();}
        catch(m){
            try{h=new ActiveXObject("Msxml2.XMLHTTP");}
            catch (m2){
                try{h=new ActiveXObject("Microsoft.XMLHTTP");}
                catch(f){h = false;}
            }
        }
        return h;
    },
    setHeader:function(h,n,v){h.setRequestHeader(n,v);},
    open:function(h,m,u,e){
        if(!h){alert("request is not init yet");return;}
        h.open(m||"POST", u, true);
        h.onreadystatechange=e;
    },
    send:function(h,d){
        if(!h){alert("request is not init yet");return;}
        if(d){
            this.setHeader(h,"Content-Length",d.length);
            this.setHeader(h,"Content-Type","application/x-www-form-urlencoded");
        }
        h.send(d);
    }
}

function Ajax(url)
{
	var m_xmlReq=null;
	if(window.ActiveXObject)
	{
	    try 
	    {
	        m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP'); 
	    }
	    catch(e)
	    {
	        try{m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
	    }
	}
	else if(window.XMLHttpRequest)
	{
	    m_xmlReq = new XMLHttpRequest();
	}
	
	this.post=function(d)
	{
	    if(!m_xmlReq)  return;
	    m_xmlReq.open('POST',url,false);
	    m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
	    m_xmlReq.send(d);
	    return m_xmlReq.responseText;
	}
}
