﻿function openmod(url)
{
    var result=showModalDialog(url, window, 'dialogwidth:840px;dialogheight:720px;help:0;center:yes;resizable:0;status:0;scroll:yes');
    if(result)
//    window.location.href=window.location.href;
        window.form1.submit();
}
function openmodsize(url, width, height)
{
    var result=showModalDialog(url, window, 'dialogwidth:' + width.toString()+ 'px;dialogheight:' + height.toString() +'px;help:0;center:yes;resizable:0;status:0;scroll:yes');
    if(result)
//    window.location.href=window.location.href;
    window.form1.submit();
}
function openmodsizepostback(url, width, height)
{
    var result=showModalDialog(url, window, 'dialogwidth:' + width.toString()+ 'px;dialogheight:' + height.toString() +'px;help:0;center:yes;resizable:0;status:0;scroll:yes');
    if(result)
        pagepostback();
}
function $1(id)
{
    return window.document.getElementById(id);
}
function setcolor(Ob)
{
    if(Ob.checked)
    Ob.parentElement.parentElement.style.backgroundColor="#e5e5e5";
    else
    Ob.parentElement.parentElement.style.backgroundColor="White";        
}
//全选
function checkall(ArrName,IdValue)
{
    var Arr=window.document.getElementsByName(ArrName);
    var IdCheck=window.document.getElementById(IdValue);
    if(IdCheck.checked)
    {
        for(var i=0;i<Arr.length;i++)
        {
            Arr[i].checked=true;
            setcolor(Arr[i]);
        }
    }
    else
    {
        for(var i=0;i<Arr.length;i++)
        {
            Arr[i].checked=false;
            setcolor(Arr[i]);
        }
    }
}
//检查选择项
function checkselect(ArrName,IdValue)
{
     var Arr=window.document.getElementsByName(ArrName);
     var IdCheck=window.document.getElementById(IdValue);
     IdCheck.value="";
       for(var i=0;i<Arr.length;i++)
        {
           if(Arr[i].checked)
           if(IdCheck.value=="")
               IdCheck.value=Arr[i].value;
           else
               IdCheck.value+=","+Arr[i].value;
        }
    if(IdCheck.value!="")
    {
        if(window.confirm('您确定操作么？'))
        return true;
        else
        return false;
    }
    else
    {
        alert('请选择再操作！');
        return false;
    }
}
function arrReplace(oldstr, newstr)
{
    var arr1 = oldstr.split(',');
    var arr2 = newstr.split(',');
    for(var i=0; i<arr2.length; i++)
    {
        var flag = true;
        for(var j=0; j<arr1.length; j++)
        {
            if(arr2[i]==arr1[j]  )
            {
                flag = false;
                break;
            }
        }
         if(flag && arr2[i]!='')
            oldstr += (oldstr==''? arr2[i]:',' + arr2[i]);
    }
    return oldstr;
}

function getPath(obj)  
{    
    if(obj)      
    {       
        if (window.navigator.userAgent.indexOf("MSIE")>=1)        
        {          
            obj.select();         
            return document.selection.createRange().text;        
        }       
        else if(window.navigator.userAgent.indexOf("Firefox")>=1)        
        {        
            if(obj.files)          
            {           
                return obj.files.item(0).getAsDataURL();          
            }        
            return obj.value;        
        }      
            return obj.value;     
      }  
}  

function trim(str)
{
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

function setImagePre(file_id, img_id)
{
    var file=$1(file_id);
    var img=$1(img_id);
    img.src=getPath(file);
}

var errormsg='some error occured!'
var successmsg='operation successfully finished!';
var confirmmsg='you are sure to process?';


//function attachEvent(target, eventName, handler, argsObject)
//{
//    var eventHandler = handler;
//    if(argsObject)
//    {
//        eventHander = function(e)
//        {
//            handler.call(argsObject, e);
//        }
//    }
//    if(window.attachEvent)//IE
//        target.attachEvent("on" + eventName, eventHander );
//    else//FF
//        target.addEventListener(eventName, eventHander, false);
//}

function cleartabledata(tableid)
{
    for(var i=$1(tableid).rows.length-1; i >= 0; i--)
    {
        $1(tableid).deleteRow(i);
    }
}

function setWaiting(tableid)
{
    cleartabledata(tableid)
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    td.className='loading';
    tr.appendChild(td);        
    $1(tableid).appendChild(tr);
}

///return input checkbox of table into hiddenfield
function checkValueToHd(tableid, hdid)
{
    var table = $1(tableid);
    var hd=$1(hdid);
    var arr=table.getElementsByTagName('input');
    hd.value='';
    for(var i=0; i<arr.length; i++)
    {
        if(arr[i].type=='checkbox' && arr[i].checked)
//        if(arr[i].checked)
        hd.value += (hd.value==''? arr[i].value: ','+ arr[i].value);
    }   
}

function getTotalValue(tableid, htmltype, objid)
{
    var table = $1(tableid);
    var arr=table.getElementsByTagName(htmltype);
    var totalValue = 0;
    for(var i=0; i<arr.length; i++)
    {
        totalValue += parseFloat(arr[i].innerHTML);  
    }
    $1(objid).innerHTML = totalValue.toFixed(2);    
}


function getArrStrToObjid(tableid, htmltag, htmltype)
{
    var table = $1(tableid);
    var arr=table.getElementsByTagName(htmltag);
    var result='';
    for(var i=0; i<arr.length; i++)
    {
        if(arr[i].type==htmltype)
            result += (result==''?arr[i].value.toString():','+arr[i].value.toString());
    }
    return result;
}

//ob check control object
//msgdiv infodiv
function checkemail(ob, msgdiv)
{
    var reg=/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

    if(!reg.test(ob.value))
    {
        msgdiv.innerHTML='Please fill a valid email!';
        msgdiv.style.display='block';
        return false;
    }
    else
    {
        msgdiv.innerHTML='';
        msgdiv.style.display='none';
        return true;
    }            
}

function checknum(ob, msgdiv,which)
{
    var reg="";
    if(which=="us")
    {
        reg= /^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/;
        
        if(ob.value=="")
        {
            msgdiv.innerHTML='Please enter your phone number!';
            msgdiv.style.display='block';
            return false;
        }
        else
        {
             var str=ob.value.toString();
            if(!reg.test(str))
            {
                msgdiv.innerHTML='Please enter your valid phone number!';
                msgdiv.style.display='block';
                return false;
            }
            else
            {
                msgdiv.innerHTML='';
                msgdiv.style.display='none';
                return true;
            }       
        }
    }
    else if(which=="nonus")
    {
         if(ob.value=="")
        {
            msgdiv.innerHTML='Please enter your phone number!';
            msgdiv.style.display='block';
            return false;
        }
        else
            return true;
        
    }        
}
function clearmsg()
{
    var divarr=window.document.getElementsByTagName('div');
    for(var i=0; i< divarr.length; i++)
    {
        if(divarr[i].className=='msg')
        divarr[i].style.display='none';
    }
}

function getSelectValue(objSelect)
{
    return objSelect.options[objSelect.selectedIndex].value;
}

//显示积分
function showBonus(which)
{
    var bonus=which.bonusDetail();
    //document.getElementById("mybonus").innerHTML="$"+bonus.value;
    document.getElementById("s1").innerHTML="$"+bonus.value;
    var total=document.getElementById("total").innerHTML;
    var flag=bonus.value-total*30;
    var finaTotal=0;
    if(flag>0)
        finaTotal=total*30;
    else
        finaTotal=bonus.value;
    document.getElementById("s2").innerHTML="$"+finaTotal;
    document.getElementById("s3").innerHTML="$"+total;
    
}
//计算可用积分
function getSavedBonus()
{
    var availableBonus=document.getElementById("s2").innerHTML;
    availableBonus=availableBonus.replace("$","");
    var inputBonus=document.getElementById("inputBonus").value; 
    var total=document.getElementById("total").innerHTML;
    total=total.replace("$","");
    var flag= availableBonus-inputBonus;
    if(flag>=0)
    {
        document.getElementById("s3").innerHTML="$"+ (total-(inputBonus/100)).toFixed(2);
        document.getElementById("total").innerHTML="$"+(total-(inputBonus/100)).toFixed(2);
        document.getElementById("s2").innerHTML="$"+flag;
        
    }
    else
    {
        alert('Please enter a valid number!');
        document.getElementById("inputBonus").select();
    }
}

function showorhidden()
{
    if(log==1)
     {
       document.getElementById("bonusDetail").style.display="block";
       document.getElementById("bonus").style.display="none";
     }
     else
     {
       document.getElementById("bonus").style.display="block";
       document.getElementById("bonusDetail").style.display="none";
     }
}
function reducetozero()
{
     document.getElementById("s1").innerHTML="$0.00";
     document.getElementById("s2").innerHTML="$0.00";
     document.getElementById("s3").innerHTML="$0.00";
}

function getcartcount()
{
    var result = AjaxUtil.count();
    if(result.value==null)
        result.value='0';
    $1('cartcount').innerHTML = result.value;
}


//返回上一页面
function goback()
{
    window.history.go(-1);
}



