﻿function trim(str, chars)    
 {   
  return ltrim(rtrim(str, chars), chars);   
 }   
     
 function ltrim(str, chars)    
 {   
  chars = chars || "\\s";   
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");   
 }   
     
 function rtrim(str, chars)    
 {   
  chars = chars || "\\s";   
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function CreateRequest() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    else {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            return new ActiveXObject("Msxml12.XMLHTTP");
        }
    }
}


function call_post(_name) {
  
    if (_name) {
        $.post(
                    "/Post/Proxy/"
                    ,
                    {  name: _name.value  }
                    ,
                       function (r, status) {
                           if (status == "success") {
                               if (r.InsertedPk > -1) {
                                   postDone(r.Path);
                               }
                               else {
                                   alert(r.Error);
                               }
                           }
                       }
                     ,
                     "json"
            );
    }
    returnValue = false;
    return false;

    function postDone(_path) {
        //        var path = document.getElementById('path');
        if (_path) {
            location = '/Edit/' + _path;
        }
        else {
            alert('error occur. please inform administrator');
        }
        returnValue = false;
        return false;
    }
}


function call_comment_update(the_pk, the_content) {
    if (the_pk) {
        $.post(
                    "/Comment/Proxy/"
                    ,
                    { pk: the_pk, content: the_content }
                    ,
                     function (data, status) {
                         if (status == "success") {
                             if (data.ok) {
                                 top.leave();
                             }
                             else {
                                 top.alert("not ok " + data.message);
                             }
                         }
                         else {
                             alert("not success " + data.message);
                         }
                     }
                     ,
                     "json"
            );
    }
    returnValue = false;
    return false;
}




function call_update(the_pk, the_content) {
 
    if (the_pk) {

        $.post(
                    "/Edit/Proxy/"
                    ,
                    { pk: the_pk, content: the_content }
                    ,
                     function (data, status) {

                         if (status == "success") {
                           
                             if (data.ok) {

                                 top.leave();
                                 // try { MyEditor.EditorDocument.body.innerHTML = ""; } catch (e) { alert(e); }
                             }
                             else {
                                 top.alert("not ok " + data.message);
                             }
                         }
                         else {
                             alert("not success " + data.message);
                         }
                     }
                     ,
                     "json"
            );
    }
    returnValue = false;
    return false;
}



function call_insert(_pk, _name, the_path) {
    if (_pk && _name && the_path) {
        $.post(
                    "/Insert/Proxy/"
                    ,
                    {
                        pk: _pk,
                        name: _name,
                        the_path: the_path
                    }
                    ,
                       function(r, status) {
                           if (status == "success") {
                               if (r.InsertedPk > -1) {
                                   insertDone(r.Path);
                               }
                               else {
                                   alert(r.Error);
                               }
                           }
                       }
                     ,
                     "json"
            );
    }
    returnValue = false;
    return false;

    function insertDone(_path) {
        //        var path = document.getElementById('path');
        if (_path) {
           
            location = '/Edit/' + _path;
        }
        else {
            alert('error occur. please inform administrator');
        }
        returnValue = false;
        return false;
    }
}


detectCookie();
function detectCookie() {
    document.cookie = "1";
    if (!document.cookie.length)
        alert("Cookies NOT enabled!");
  
}
