/**
 * popup hompy window
 */
var hompywindow = '';
function popupHompy(url) 
{
    var name = "miniWorld";
    var re = /(hompy=)+\w/g;
    if (re.test(url))
        var argv = "menubar=no,toolbar=no,scrollbars=no,location=no,status=no,width=1000,height=665";
    else 	
        var argv = "";

    if (!hompywindow.closed && hompywindow.location) {
        hompywindow.location.href = url;
    } else {
        hompywindow = window.open(url,name,argv);
        if (!hompywindow.opener) hompywindow.opener = self;
    }
    if (window.focus) { hompywindow.focus(); }
    return false;
}

/*
 * popup window for club menubar
 */
var hompywindow1 = '';
function popupHompy1(url)
{
    var re = /(hompy=)+\w/g;

    if (re.test(url))
        var argv = "menubar=no,toolbar=no,scrollbars=no,location=no,status=no,width=1000,height=665";
    else
        var argv = "";

//    if (!hompywindow1.closed && hompywindow1.location) {
//        hompywindow1.location.href = url;
//    }
//    else {
        hompywindow1 = window.open(url,'',argv);

//    if (!hompywindow1.opener) hompywindow1.opener = self;
//    }

    if (window.focus) { hompywindow1.focus(); }
        return false;
}

/**
 * resize image and keep proportion
 */
function img_resize(img, max_img_w, max_img_h) 
{
    if ((img.width > max_img_w) || (img.height > max_img_h)) {
        if (img.width > img.height) {
            img.width = max_img_w;
            img.height = parseInt(img.height*img.width/max_img_w);
        } else {
            img.height = max_img_h;
            img.width = parseInt(img.width*img.height/max_img_h);
        }
    }
}

/**
 * open gallery list
 */
function insertGalleryToDiaryPic()
{
    window.open('/myclub/gallerylist.php', 'uploadpic', 'menubar=no,scrollbars=no,width=550,height=500,top=150,left=150');
}

/**
 * insert selected photo into text editor
 */
function insertGIntoEditor(my_value, uid)
{
    if(my_value != "") {
	oImage = window.opener.FCK.CreateElement( 'IMG' ) ;
	oImage.src = "http://rvp.mwsrv.com/gallerypic/" + uid + "/" + my_value;
	oImage.border = '0';
    }
    window.close();
}
 
function insertDiaryPic()
{
    window.open('/myclub/uploadpic.php', 'uploadpic', 'menubar=no,scrollbars=no,width=550,height=500,top=150,left=150');
}

function insertPicValue(my_value)
{
    if(my_value != "") {
	pic = "<img src=\"/diarypic/tmp/"+my_value+"\">";
	window.opener.document.ClubPost.cover.value = my_value;
	window.opener.document.getElementById('insertPic').innerHTML = pic;
    }
    window.close();
}

function deleteDiaryPostPic(form)
{
    var is_confirmed = confirm('執行本按鈕會將您的照片刪除，若您日記未編修完成也請先存檔，以免資料遺失之憾');
    if(is_confirmed) {
	document.ClubPost.cover.value = "";
	document.getElementById('insertPic').innerHTML = "";
    }
}

function switchReadBoardPage(bid, btype, page)
{
    window.location = '/myclub/main.php?boardId='+bid+'&boardType='+btype+'&page='+page;
}

function switchReadGemPage(bid, gid, page)
{
    window.location = '/myclub/main.php?boardId='+bid+'&op=gem&gemId='+gid+'&page='+page;
}

function switchReadArticlePage(bid, btype, aid, page, pos)
{
    loc = '';
    if(pos)
        loc = '#reply'+pos;
    window.location = '/myclub/main.php?boardId='+bid+'&boardType='+btype+'&op=article&articleId='+aid+'&page='+page+loc;
}

function switchBoardVisitorPage(bid, btype, page)
{
    window.location = '/myclub/main.php?boardId='+bid+'&boardType='+btype+'&op=statistics&page='+page;
}

function goEditContent(bid, btype, id, action, page)
{
    if(action == "a")
        window.location = '/myclub/main.php?boardId='+bid+'&boardType='+btype+'&op=post&e='+id;
    else
        window.location = '/myclub/main.php?boardId='+bid+'&boardType='+btype+'&op=reply&e='+id+'&page='+page;
}

function checkReplyContent()
{
    if(document.getElementById('replyContent').value == "") {
        document.getElementById('replyContent').focus();
        alert('請輸入回應內容');
	return false;
    }
    return true;
}

function CreateBookmark(title, url) 
{
    if(window.sidebar) { // Mozilla/firefox
        window.sidebar.addPanel(title, url,"");
    } 
    else if(window.external) {
        window.external.AddFavorite(url,title);
    } 
    else if(window.opera && window.print) {
        return true;
    }
}

function confirmDelete(theLink) {
    if(typeof(window.opera) != 'undefined')
        return true;
    var is_confirmed = confirm('確定要刪除嗎?');
    return is_confirmed;
}

function confirmDelete2(theLink) {
    if(typeof(window.opera) != 'undefined')
        return true;
    var is_confirmed = confirm('此群組下還有其它版,確定要刪除嗎?');
    return is_confirmed;
}

function cantDelete(theLink) {
    alert("此分類下還有其它版或是預設分類目錄, 不能刪除!");
    return false;
}

function check_article_cat_form(form) {
    if(form.catadd.value == "") {
        alert("請輸入新增分類名稱!");
        return false;
    }        
    return true;
}

function check_article_cat_form2(form) {
    for(var i = 0; i < form.elements.length; i++) {
        if(form.elements[i].name == "cname") {
           if(form.elements[i].value == "") {
              alert("分類名稱不能空白!");
              return false;
           }
         }
         if(form.elements[i].name == "id") {
            if(form.elements[i].value == 0) {
               alert("此為預設分類，不能修改！");
               return false;
            }
         }
    }
    return true;
}

function checkSortValue(form) {
    var arr = new Array();
    for(var i = 0, j = 0; i < form.elements.length; i++) {
        if(form.elements[i].name == "newpos[]") {
           arr[j++] = form.elements[i].value;
        }
    }
    for(var i = 0; i < arr.length-1; i++) {
        for(var j = i+1; j < arr.length; j++) {
            if(arr[i] == arr[j]) {
               alert("位置值不能相同!");
               return false;
            }
        }
    }
    return true;
}

function showMessage(){
    var msg = ajaxObj.response.split("&");
    if(msg[0]=='msg'){
       switch(msg[1]){
              case '01':
	      alert('完成加入!');
	      break;
       }
    }
    else if(msg[0]=='err'){
            switch(msg[1]){
	    case '01':
	    alert('請先登入');
            break;
	    case '02':
	    alert('你已經加過此俱樂部到我的最愛囉');
	    break;
            default:
	    alert('不正確的操作模式');
            break;
	    }
    }
}

function useAjax(target,responseType) {
    var url = target;
    var reType = responseType;
    if(reType=='alert'){
       ajaxObj.requestFile = url;
       ajaxObj.onCompletion = showMessage;
    }
       ajaxObj.runAJAX();
}
