﻿function GetCltID(e) {
    var tag = document.getElementById(e);
    if (tag) return tag;
    tag = document.getElementsByName(e);
    if (tag.tagName == undefined) return null;
    return tag;
}

function ajax() {
    if(window.XMLHttpRequest) return (new XMLHttpRequest());
    var arr_t = new Array(
    "MSXML2.XMLHTTP.4.0",
    "MSXML2.XMLHTTP.3.0",
    "MSXML2.XMLHTTP.2.6",
    // "MSXML2.XMLHTTP",   
    "Microsoft.XMLHTTP",
    "MSXML.XMLHTTP"
    );
    for (var i = 0; i < arr_t.length; i++) {
        try {
            xmlhttp_ver = arr_t[i];
            return new ActiveXObject(arr_t[i]);
        } catch (e) { }
    }
    return (null);
}
//参数 1（POST,GET）；2（请求的连接）；3（发送的参数）；4（回调函数）  2010-4-9@harry
function ajax_query(method, url, parameter,callback) {
    var xmlhttp;
    xmlhttp = ajax();
    if (!xmlhttp) {
        alert('错误！不能创建ajax对象！');
        return;
    }
    if (method == 'GET') {
        xmlhttp.open(method, url + '?' + parameter + '&r=' + Math.round(Math.random() * 9999), true);
    }
    else {
        xmlhttp.open(method, url, true);
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    xmlhttp.onreadystatechange = function() {
        if (!(xmlhttp.readyState == 4 && xmlhttp.status == 200)) {
            return;
        }
        var txt = xmlhttp.responseText;
        if (callback != null) {
            callback(txt);
        }
    }
    xmlhttp.send(parameter);
    delete xmlhttp;
}

function gl_cb(res) {
    alert(res);
}

function to2bits(num) {
    if (parseFloat(num) == num)
        return Math.round(num * 100) / 100;
    else
        return 0;
}
//----------------------select ctl------------------
function removeOptions(selectObj) {
    if (typeof selectObj != 'object') {
        selectObj = document.getElementById(selectObj);
    } // 原有选项计数
    var len = selectObj.options.length;
    for (var i = 0; i < len; i++) {  // 移除当前选项
        selectObj.options[0] = null;
    }
}

function setSelectOption(selectObj, optionList, firstOption, selected) {
    if (typeof selectObj != 'object') {
        selectObj = document.getElementById(selectObj);
    } // 清空选项
    removeOptions(selectObj);  // 选项计数
    var start = 0; // 如果需要添加第一个选项
    if (firstOption) {
        selectObj.options[0] = new Option(firstOption, ''); // 选项计数从 1 开始
        start++;
    }
    var len = optionList.length;
    for (var i = 0; i < len; i++) {
        // 设置 option
        selectObj.options[start] = new Option(optionList[i].val, optionList[i].key);  // 选中项
        if (selected == optionList[i].key) {
            selectObj.options[start].selected = true;
        }
        // 计数加 1
        start++;
    }
}

var CurImgCtl;
var pn;
function ChangeColor(obj, imgctl) {
    CurImgCtl = imgctl;
    pn = obj;
    ajax_query('GET', 'ajaxCore.aspx', 'action=ChangeColor&n=' + obj, cc_cb);
}

function cc_cb(res) {
    if (res.toString().split('|')[0] == "ERROR") {
        alert(res.toString().split('|')[1]);
    } else {
    ChangeImg(res.toString().split('|')[1], CurImgCtl, pn);
    }
}

function ChangeImg(fname,imgctl,obj) {
    GetCltID('img_' + imgctl).src = 'http://www.looksee.com.cn/MAIN/images/product/photo/' + fname;
    GetCltID('imga_' + imgctl).target = '_blank';
    GetCltID('a_' + imgctl).target = '_blank';
    GetCltID('imga_' + imgctl).href = 'ShopProductDetail.aspx?n=' + obj;
    GetCltID('a_' + imgctl).href = 'ShopProductDetail.aspx?n=' + obj;
}

function IsNum(obj)
{
    var a = GetCltID(obj).value;
    var pr=/^\d+$/;
    if(a!=""){
      if(pr.test(a)==false)
      {alert('请输入整数。');
      obj.focus();
      return false;
      }
    }
}
