/* 設計師專用 */
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}
/*          */


var theForm = document.forms[0];
function reset_form() {
    var theForm = document.forms[0];
    theForm.reset();
}
function submit_form() {
    var theForm = document.forms[0];
    theForm.submit();
}
function toEdit(page, mode, selection) {
    var theForm = document.forms[0];
    var choice = false;
    var check_box;
    for (i = 1; i < theForm.elements.length; i++) {
        check_box = theForm.elements[i];
        if (check_box.name == "cid[]" && check_box.checked) {
            choice = true;
            break;
        }
    }

    if (!choice) {
        alert("Please Select Item to Edit.");
        return;
    }

    location.href = page + ".aspx?id=" + check_box.value + "&selection=" + selection + "&mode=" + mode;
}
function toDel(page, mode, selection, sid) {
    var theForm = document.forms[0];
    var check_box;
    var selected_items = "";
    for (i = 1; i < theForm.elements.length; i++) {
        check_box = theForm.elements[i];
        if (check_box.name == "cid[]" && check_box.checked) {
            selected_items += check_box.value + ",";
        }
    }
    if (selected_items.length > 0) {
        selected_items = selected_items.substring(0, selected_items.length - 1);

        if (confirm('確定要刪除資料?')) {
            location.href = page + ".aspx?act=del&delid=" + selected_items + "&selection=" + selection + "&mode=" + mode + "&sid=" + sid;
        }
    } else {
        alert("請勾選資料。");
        return;
    }
    return;
}


function checkall() {
    var theForm = document.forms[0];
    for (var i = 0; i < theForm.elements.length; i++) {
        if (theForm.elements[i].type == 'checkbox') {
            theForm.elements[i].checked = !(theForm.elements[i].checked);
        }
    }
    theForm.checkbox.checked = !(theForm.checkbox.checked);
}
function isChecked(isitchecked) {
    var theForm = document.forms[0];
    if (isitchecked == true) {
        theForm.boxchecked.value++;
    }
    else {
        theForm.boxchecked.value--;
    }
}
function regInput(obj, reg, inputStr) {
    var docSel = document.selection.createRange()
    if (docSel.parentElement().tagName != "INPUT") return false
    oSel = docSel.duplicate()
    oSel.text = ""
    var srcRange = obj.createTextRange()
    oSel.setEndPoint("StartToStart", srcRange)
    var str = oSel.text + inputStr + srcRange.text.substr(oSel.text.length)
    return reg.test(str)
}

function change_sort(page, root_id, mode, selection) {
    var theForm = document.forms[0];
    if (root_id != "0") {
        location.href = page + ".aspx?cate_id=" + theForm.cate_id.value + "&chg_sort=" + root_id + "&selection=" + selection + "&mode=" + mode;
    }
    else {
        location.href = page + ".aspx?cate_id=&selection=" + selection + "&mode=" + mode;
    }
}
function change_url(page, mode, selection) {
    location.href = page + ".aspx?selection=" + selection + "&mode=" + mode;
}
function reSize(myImg, maxWidth, maxHeight) {
    var iniPicWidth = myImg.width;
    var iniPicHeight = myImg.height;
    var iniRate = iniPicHeight / iniPicWidth;

    if (iniPicWidth > maxWidth || iniPicHeight > maxHeight) {
        var newW = maxWidth;
        var newH = Math.floor(newW * iniRate);
        if (newH > maxHeight) {
            var newH = maxHeight;
            var newW = Math.floor(newH / iniRate);
        }
        myImg.width = newW;
        myImg.height = newH;
    }
}
//認證身分證
function TwIDValidate(oSrc, args) {

    //判斷現在為那一國的身份證，若為本國則需要驗證
    var item = $('input[name=idnoType][@checked]').val();
    if (item == 0) {
        //建立字母分數陣列(A~Z)
        var city = new Array(
			 1, 10, 19, 28, 37, 46, 55, 64, 39, 73, 82, 2, 11,
			20, 48, 29, 38, 47, 56, 65, 74, 83, 21, 3, 12, 30
		);
        var id = "";
        id = args.Value.toUpperCase();
        // 使用「正規表達式」檢驗格式
        if (id.search(/^[A-Z](1|2)\d{8}$/i) == -1) {
            return args.IsValid = false;
        } else {
            //將字串分割為陣列(IE必需這麼做才不會出錯)
            id = id.split('');
            //計算總分
            var total = city[id[0].charCodeAt(0) - 65];
            for (var i = 1; i <= 8; i++) {
                total += eval(id[i]) * (9 - i);
            }
            //補上檢查碼(最後一碼)
            total += eval(id[9]);
            //檢查比對碼(餘數應為0);
            if (total % 10 == 0) args.IsValid = true;
            else args.IsValid = false;
        }
    } else {
        args.IsValid = true;
    }

}

//認證只能為電話格式欄位
function TelValidate(oSrc, args) {

    args.IsValid = /^[0-9\-\(\)\ ]+$/.test(args.Value);

}
//認證只能為數值欄位
function NumberValidate(oSrc, args) {

    args.IsValid = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(args.Value);

}
//認證email
function EmailValidate(oSrc, args) {
    args.IsValid = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(args.Value);
}
function addtofavorite() {
    if (navigator.appName != "Netscape") {
        window.external.AddFavorite("http://www.ngng.com.tw/", "ngng");
    }
}

function del_product(id) {

    if (confirm('確定要刪除此產品嗎?')) {
        $.ajax({
            url: 'ajax_data.ashx?act=del_product&id=' + id,
            success: function(data) {
                window.location.reload();
            }
        });  
    }
}

function change_element(id, obj) {

    $.ajax({
        url: 'ajax_data.ashx?act=change_element&id=' + id+"&element="+obj.value,
        success: function(data) {
            window.location.reload();
        }
    });

}
function newCode(obj) {
    obj.src = 'VerifyCode.aspx?time=' + Math.random();
}