/* Copyright (c) 1995-2006 Timecruiser Computing Corporation ("TCC")
 * All Rights Reserved.
 * Project:     Drag-N-Drop Channel
 * Author:      Ian Wang
 * Mail:        ian_wang2008@yahoo.com.cn
 * Date:        10/15/06
 * Description: MyCruiser Drag-N-Drop API.
 * 
 * Change history:
	10/15/06	Ian	created
 */

var Request = new Object();
Request.send = function(url, method, callback, data, sync, win) {
  if ( typeof(beforeCCRequest) == "function" ) {
    if ( !beforeCCRequest() ) { return null; }
  }
  if(!win){ win = window; }
  
  var req=false, async=true;
  if(sync) { async=false; }
   if(!data) data=null;
  
  if (win.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (win.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  if (method=="POST") {
    req.open("POST", url, async);
  } else {
    req.open("GET", url, async);
  }

  if(!sync){
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        if(req.status < 400){
          if(callback) { if(callback!=null)callback(req); }
        }
      }
    }
  }
  if (method=="POST") { 
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    if(data!=null && data){
      req.setRequestHeader("Content-length", data.length);
    }
    req.send(data);
    if(sync && callback) { if(callback!=null)callback(req); }
  } else {
    req.send(null);
    if(sync && callback) { if(callback!=null)callback(req); }
  }
  
  if ( typeof(ccResetTimeOut) == "function" ) { // ccResetTimeOut in ccSession.js : reset session timer. 
    ccResetTimeOut(); 
  }
  return req;
}
Request.sendPOST = function(url, data, callback, sync) {
  return Request.send(url, "POST", callback, data, sync);
}

Request.sendGET = function(url, callback, sync) {
  return Request.send(url, "GET", callback, null, sync);
}

Request.load= function(url, id, sync, msg){
  var e=document.getElementById(id);
  if(e && msg) { e.innerHTML = msg; }
  reqCallback=function(response){ 
    if(e) { e.innerHTML=response.responseText; }
  }
  Request.sendGET(url,reqCallback, sync);
}