function SetBoxchecked(id){
    document.adminForm.boxchecked.value=id;
}
	
function FormSubmit(action){
    document.adminForm.action.value=action;
    document.adminForm.submit();
}
	
function FormDelete(){
    if(!confirm('Confirm to delete item?')){
        return;
    }
		
    document.adminForm.task.value='delete';
    document.adminForm.submit();
}
	
function DeleteList(task){
    if(!confirm('Confirm to delete item?')){
        return;
    }
		
    document.adminForm.task.value=task;    
    document.adminForm.submit();
}
	
function CancelTask(option){
    location.href="?option="+option;
}
	
function Goto(option,param){
    location.href="?option="+option+(param!=""?"&"+param:"");
}
	
function Back(){
    history.back();
}
	
function checkAll(fieldname){
    var f=document.adminForm;
    var id=f.elements;
    var boolValue=f.toggle.checked;
		
    for (i=0;i<id.length;i++){
        if(id[i].name=="id[]"){
            id[i].checked=boolValue;
        }
    }
}
	
function changeOrdering(){
    var i=0;
    var f=document.adminForm;
    var id=f.elements;
		
    for (i=0;i<id.length;i++){
        if(id[i].name=="id[]"){
            id[i].checked=true;
        }
    }
    if(i>0){
        document.adminForm.task.value="changeOrdering";
        document.adminForm.submit();
    }
}
	
function isChecked(isitchecked){
    if(isitchecked==true){
        document.adminForm.boxchecked.value++;
    }else{
        document.adminForm.boxchecked.value--;
    }
}

function formValidate(){
    return false;
}

function openWindow(filename,WindowName,w,h,strFeatures) {
    var sw=screen.width/2;
    var sh=(screen.height/2)-50;
    window.open(filename,WindowName,'top='+(sh-(h/2))+',left='+(sw-(w/2))+',width='+w+',height='+h+','+strFeatures+' ');
}

function checkNumber(e) {
    if(navigator.userAgent.indexOf("Firefox")==-1){ //for IE
        e_k=event.keyCode
        if (e_k != 13 && (e_k < 48) || (e_k > 57)) {
            if(e_k==46){
                return true;
            }
            event.returnValue = false;
        }
    }else{
        keyPressed = e.which;
        if (keyPressed != 13 && (keyPressed < 48) || (keyPressed > 57)) {
            if(keyPressed==46){
                return true;
            }
            return false;
        }
    }
}

//Youtube Tag replade
function YoutubeTag(url,bodi){
    //http://www.youtube.com/watch?v=5du1oc0j2KQ
    url=url.replace("http://www.youtube.com/watch?v=", "");
    url=url.trim();
    surroundText('[youtube]'+url+'[/youtube]', '', document.getElementById(bodi));
//document.getElementById('youtube').style.display='none';
}

//taps is array id of element
//name is id of display element
function swabtap(taps,name){
    for(tap in taps){
        $("#"+taps[tap]).hide();
    }
    $("#"+name).fadeIn(300);
}


$(function(){
    //*************************** User ***************************

    var url=window.location.toString().replace("http://","").split("/");
    var index="http://"+url[0]+"/";
    
    // Register
    $("#register").click(function(){
        $("#error").html("");
        var url=index+"user/save";
        var dataSet={
            register_date: $("#register_date").val(),
            lastvisit_date: $("#lastvisit_date").val(),
            realname: $("#realname").val(),
            birthday: $("#datepicker").val(),
            gender: $('input:radio[name=gender]:checked').val(),
            email: $("#email").val(),
            province: $("#province").val(),
            username: $("#username").val(),
            passwd: $("#passwd").val(),
            code: $("#code").val()
        };
        $.post(url, dataSet, function(data){
            if(data=="success"){
                alert("ระบบได้บันทึกข้อมูลทองท่านแล้ว \n ท่านสามารถเปลี่ยนข้อมูลภายหลังได้.");
                window.location.href=index+"user/login";
            }else{
                $("#register_error").html(data);
            }
        });
    });
    // เช็คว่าอีเมล์ใช้งานแล้วหรือไม่
    $("#email").blur(function(){
        $("#msg_email").html("");
        var url=index+"user/chkEmailExist";
        var dataSet={
            email: $("#email").val()
        };
        $.post(url, dataSet, function(data){
            if(data!="")
                $("#msg_email").html("<font style=\"background-color: #ffe786; padding:3px; margin:2px; font-size:10px;\">ขออภัย::อีเมล์นี้ถูกใช้งานแล้ว</font>");
        });
    });
    // เช็คชื่อผู้ใช้งานว่าใช้งานแล้วหรือไม่
    $("#username").blur(function(){
        $("#msg_username").html("");
        var url=index+"user/chkUsernameExist";
        var dataSet={
            username: $("#username").val()
        };
        $.post(url, dataSet, function(data){
            if(data!="")
                $("#msg_username").html("<font style=\"background-color: #ffe786; padding:3px; margin:2px; font-size:10px;\">ขออภัย::ชื่อผู้ใช้งานนี้ถูกใช้งานแล้ว</font>");
        });
    });
    // User login
    $("#bt_login").click(function(){
        $("#login_error").html("");
        var url=index+"user/login";
        var dataSet={
            username: $("#username").val(),
            passwd: $("#passwd").val()
        };
        $.post(url, dataSet, function(data){
            if(data==""){
                window.location.href=index;
            }else{
                $("#login_error").html(data);
            }
        });
    });
    // เปลี่ยนแปลงข้อมูลส่วนตัว
    $("#profile_save").click(function(){
        $("#profile_error").html("");
        var url=index+"user/profile";
        var dataSet={
            realname: $("#realname").val(),
            tel: $("#tel").val(),
            address: $("#address").val(),
            province: $("#province").val(),
            postcode: $("#postcode").val(),
            idcard: $("#idcard").val(),
            avatar: $("#avatar").val(),
            passwd: $("#passwd").val()
        };
        $.post(url, dataSet, function(data){
            $("#profile_error").html(data);
        });
    });
    // เปลี่ยนรหัสผ่าน
    $("#changepass_save").click(function(){
        $("#changepass_error").html("");
        var url=index+"user/changePassword";
        var dataSet={
            old_password: $("#old_password").val(),
            new_password: $("#new_password").val(),
            confirm_password: $("#confirm_password").val()
        };
        $.post(url, dataSet, function(data){
            $("#changepass_error").html(data);
        });
    });

    //***********************************************************

    //*************************   Content   *****************************
    $("a.notice").click(function(){
        var url=$(this).attr("title");
        var dataSet={};
        $.get(url, dataSet, function(data){
            if(data=="")
                alert("ได้รับการแจ้งลบแล้ว");
            else
                alert(data);
        });
    });

    $("#save").click(function(){
        $("#error").html("");

        var url=index+"comment/post";
        var dataSet={
            id: $("#id").val(),
            comment_control: $("#comment_control").val(),
            comment_parent: $("#comment_parent").val(),
            user_id: $("#user_id").val(),
            comment_author: $("#comment_author").val(),
            comment_author_ip: $("#comment_author_ip").val(),
            comment_date: $("#comment_date").val(),
            comment_body: $("#comment_body").val(),
            code: $("#code").val()
        };
        $.post(url, dataSet, function(data){
            if(data=="success")
                window.location.reload();
            else
                $("#error").html(data);
        });
    });

    $(".quote").click(function(){
        $("#comment_body").html($("#comment_body").val()+"[quote]"+$("#qutemessage"+$(this).attr("alt")).val()+"[/quote]");
    });
//***********************************************************
});
