function $(element) {
    if (typeof element == 'string')
        element = document.getElementById(element);
    return element;
}


//var PosHelper = {
////    getPosition : function(o){

////        var nLt=0;
////        var nTp=0;
////        var offsetParent = o;

////        while (offsetParent!=null && offsetParent!=document.body) {

////            nLt+=offsetParent.offsetLeft;
////            nTp+=offsetParent.offsetTop;

////            if(!this.IsNav()){
////                parseInt(offsetParent.currentStyle.borderLeftWidth)>0?nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
////                parseInt(offsetParent.currentStyle.borderTopWidth)>0?nTp+=parseInt(offsetParent.currentStyle.borderTopWidth):"";
////            }
////            offsetParent=offsetParent.offsetParent;
////        }

////        return {
////            x:nLt,
////            y:nTp
////        };
////    },


////    IsIE:function(){
////        return ( navigator.appName=="Microsoft Internet Explorer" );
////    },

////    IsNav:function (){
////        return ( navigator.appName=="Netscape" );
////    },

//    SetOffset:function(sobj,tobj,x,y )
//    {

//        if (typeof sobj == 'string') sobj = document.getElementById(sobj);
//        if (typeof tobj == 'string') tobj = document.getElementById(tobj);

//        var pos = this.getPosition(sobj);
//        if(tobj)
//        {
//            tobj.style.left= pos.x + x + "px";
//            tobj.style.top = pos.y + y +  "px";
//            tobj.style.display="block";
//        }
//    }

//}

function getPrdData(pid)
{

    if ( typeof probJson != 'undefined' )
    {

        var len = probJson.prod.length;

        for(var i=0 ;i < len ;i ++)
        {
            if(probJson.prod[i].pid == pid) return probJson.prod[i];
        }

    }
}


function checkall()
{
    if ( typeof probJson != 'undefined' )
    {
        //20090615修改，要求默认选择最前面的商品组合
        var len = probJson.prod.length;
        for(var i=1 ;i < len ;i ++)
        {
            if(probJson.prod[i])
            {
                var id = probJson.prod[i].pid;
                var ock = $("ck"+id);
                if(ock && i!=1)
                {
                    ock.checked = false;
                }
                else
                {
                    ock.checked = true;
                }
            }
        }
    }
}

function addprd(obj,prdid)
{

    document.getElementById('packMess').style.display="none";

    var prdData = getPrdData(prdid);
    var pobj =document.getElementById("pr"+prdid);
    var cobj =document.getElementById("ck"+prdid);

    if(cobj.checked)
    {
        pobj.className = "group_product_b";

    }
    else
    {
        pobj.className = "group_product_c";
    }

    rcaltPrice(prdid);

}


function rcaltPrice(prdid)
{

    var count =1;
    var sumPrice = probJson.prod[0].price;;
    var osumPrice = probJson.prod[0].oprice;;

    var len = probJson.prod.length;
    for(var i=0 ;i < len ;i ++)
    {
        if(probJson.prod[i])
        {
            var id = probJson.prod[i].pid;
            if(document.getElementById("ck"+id) &&document.getElementById("ck"+id).checked)
            {
                sumPrice += probJson.prod[i].price;
                osumPrice += probJson.prod[i].oprice;
                count++;
            }
        }
    }

//    SetOffset:function(sobj,tobj,x,y )
//    {

//        if (typeof sobj == 'string') sobj = document.getElementById(sobj);
//        if (typeof tobj == 'string') tobj = document.getElementById(tobj);

//        var pos = this.getPosition(sobj);
//        if(tobj)
//        {
//            tobj.style.left= pos.x + x + "px";
//            tobj.style.top = pos.y + y +  "px";
//            tobj.style.display="block";
//        }
//    }

    if(count <=1){
//        PosHelper.SetOffset("ck"+prdid,"packMess",0,47);
        document.getElementById("ck"+prdid).checked = true;
        document.getElementById("pr"+prdid).className = "group_product_b";
        return ;
    }

    document.getElementById("packCount").innerHTML = count -1;

    var priceobj = document.getElementById("sumpriceDiv");
    var opriceobj = document.getElementById("sumopriceDiv");

    if(priceobj)priceobj.innerHTML = sumPrice.toFixed(2);
    if(opriceobj)opriceobj.innerHTML = osumPrice.toFixed(2);

}


function AddPackage()
{

    var ischeck = false;
    var product_id =  probJson.prod[0].pid;
    var len = probJson.prod.length;
    for(var i=0 ;i < len ;i ++)
    {
        if(probJson.prod[i])
        {
            var id = probJson.prod[i].pid;
            if(document.getElementById("ck"+id)&&document.getElementById("ck"+id).checked)
            {
                product_id += "-" + id;
                ischeck = true;
            }
        }
    }


//    if(!ischeck) {
//    document.getElementById('packMess').style.display="block";
////        $("packMess").style.display
//        return ;
//    }

    var url=null;
    if(product_id==null || product_id<1)
        url="http://www.mmloo.com/shopping_cart.aspx";
    else
        url="http://www.mmloo.com/buy/shoppingcart.aspx?pid="+product_id;
    location.href=url;
//    var popup=window.open(url,"shoppingcart");
//    popup.focus()
}

