/* AJAX code */
$j = jQuery.noConflict();
$j(document).ready(function(){
    //JQuery ready is quicker than onload
    imgLoader = new Image();// preload image
    imgLoader.src =  "image/loading_animation.gif";
    var loading = '<img src="image/loading_animation.gif">';
    /*$j(".detail_link").click(function(){
        var url = document.frm_talkcool.action;
        var showResult = $j("#page_detail");
        $j.ajax({
            type : "POST", 
            url : url,
            dataType : "html", 
            cache : false, 
            success : function(html){ showResult.html(loading); },
            complete : function(XMLHttpRequest) { showResult.html(XMLHttpRequest.responseText); }
        });
    });*/

    $j("#frm_update_position").submit(function(){
        var err = '';
        if (document.frm_update_position.passwd.value == "") {
            err = 'ท่านไม่ได้กรอกรหัสผ่าน\n';
        }
        if (document.getElementById("update_position").checked == false && document.getElementById("update_expire").checked == false) {
            err += 'ท่านไม่ได้เลือกรายการที่ต้องการดำเนินการ';
        }
        if (err != '') {
            alert(err);
            return false;
        }

        var url = document.frm_update_position.action;
        var data = "pdid="+document.getElementById("product_id").value+"&passwd="+document.frm_update_position.passwd.value+"&expire="+document.getElementById("expire").value+"&update_position="+document.getElementById("update_position").checked+"&update_expire="+document.getElementById("update_expire").checked+"&action=check";
        $j.ajax({
            type : "POST", 
            url : url,
            data : data,
            cache: false,
            complete : function(XMLHttpRequest) { 
                var resp = XMLHttpRequest.responseText;
                if (resp == 'true'){
                    alert("ดำเนินการเรียบร้อย");
                    window.location.reload( false );
                    return false;
                } else if (resp == "over") {
                    alert("ท่านสามารถเลื่อนเวลาได้อีกครั้งหลังจากนี้ประมาณ 1 ชม.");
                } else if (resp == "pass") {
                    alert("ท่านกรอกรหัสผ่านไม่ถูกต้อง");
                } else {
                    alert("เกิดข้อผิดพลาดในการดำเนินการ");
                }
            }
        });
        return false;
    });
});
/* End AJAX code */

function disable_expire(){
    if (document.getElementById("update_expire").checked == true){
        document.getElementById("expire").disabled = false;
    } else {
        document.getElementById("expire").disabled = true;
    }
}

function ajax_update_position(choice){
    $j("#update_section").attr({ style:"display:block" });
    document.frm_update_position.passwd.value = "";
    document.frm_update_position.update_position.checked = "";
    if (choice == "0") {
        document.frm_update_position.update_expire.checked = "";
        document.frm_update_position.expire.disabled = "true";
    }
    return false;
}

function ajax_edit_reg_free(choice){
    if (choice == "1") { document.frm_manage_shop.action = "register_free.php"; }
    else if (choice == "2") { document.frm_manage_shop.action = "register_free_del.php"; }
    else { return false; }
    $j("#password_section").attr({ style:"display:block" });     /*display:block; คือ แสดง object id นั้นในลักษณะเป็น Block*/
    return false;
}

function chkRegExp(ptn, data) {
    var pattern;
    if ( ptn == "mail" ) { pattern = /^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/; }

    if (data.match(pattern) == null) { return true; } 
    else { return false; }
}
