
 function utacheckin(flag,callbackfunc,applevel,objectid)
{ var data='action=utacheckin&callback='+encodeURIComponent(callbackfunc)+'&applevel='+applevel+'&objectid='+objectid;
  return  in_utaajaxpost('/Ajax/uta/in_utils.jsp',data,flag);
}

 function getservertime(flag,callbackfunc)
{ var data='action=getservertime&callback='+encodeURIComponent(callbackfunc);
  return  in_utaajaxpost('/Ajax/uta/in_utils.jsp',data,flag);
}

 function savefield(flag,callbackfunc,token,data)
{ var data='action=savefield&callback='+encodeURIComponent(callbackfunc)+'&token='+token+'&data='+encodeURIComponent(data);
  return  in_utaajaxpost('/Ajax/uta/in_utils.jsp',data,flag);
}

function getreportbuildercombo(flag,callbackfunc,combotype,tabletype,id,entity)
{ var data='action=getreportbuildercombo&callback='+encodeURIComponent(callbackfunc)+'&combotype='+combotype+'&tabletype='+tabletype+'&id='+id+'&entity='+entity;
  return  in_utaajaxpost('/Ajax/uta/in_utils.jsp',data,flag);
}

function getfieldsearchcombo(flag,callbackfunc,entity,fieldtype,appid,applevel)
{ var data='action=getfieldsearchcombo&callback='+encodeURIComponent(callbackfunc)+'&entity='+entity+'&fieldtype='+fieldtype+'&appid='+appid+'&applevel='+applevel;
  return  in_utaajaxpost('/Ajax/uta/in_query.jsp',data,flag);
}

function getsearchquery(flag,callbackfunc,entity,appid,applevel,typeid,statusid,rset)
{ var data='action=getsearchquery&callback='+encodeURIComponent(callbackfunc)+'&entity='+entity+'&appid='+appid+'&applevel='+applevel+'&typeid='+typeid+'&statusid='+statusid;
  var criteriastr="";
  if (rset) {
    for (var i=1; i <= rset.recordcount; i++)
      criteriastr+=rset.getfieldbyname(i,"fieldtype")+"<c>"+rset.getfieldbyname(i,"fieldid")+"<c>"+rset.getfieldbyname(i,"criteria")+"<c>"+rset.getfieldbyname(i,"matchoption")+"<c>"+rset.getfieldbyname(i,"andor")+"<r>";
  }
  data+="&criteriastr="+criteriastr;
  return  in_utaajaxpost('/Ajax/uta/in_query.jsp',data,flag);
}
function getfiltertemplate(flag,callbackfunc,filterid) {
  var data='action=getfiltertemplate&callback='+encodeURIComponent(callbackfunc)+'&filterid='+filterid;
  return  in_utaajaxpost('/Ajax/uta/in_query.jsp',data,flag);
}
function getuserfilters(flag,callbackfunc,objecttype,applevel) {
  var data='action=getuserfilters&callback='+encodeURIComponent(callbackfunc)+'&objecttype='+objecttype;
  if (applevel>0)
    data+='&applevel='+applevel;
  return  in_utaajaxpost('/Ajax/uta/in_query.jsp',data,flag);
}
function getbilllevelcost(flag,callbackfunc,userid,projectid,timecode,appid) {
  var data='action=getbilllevelcost&callback='+encodeURIComponent(callbackfunc)+'&userid='+userid+'&projectid='+projectid+'&timecode='+timecode+'&appid='+appid;
  return  in_utaajaxpost('/Ajax/uta/in_utils.jsp',data,flag);
}
function getcustomfields(flag,callbackfunc,fields,filter,entity,itemid) {
  var data='action=getcustomfields&callback='+encodeURIComponent(callbackfunc)+'&fields='+fields+'&entity='+entity+'&itemid='+itemid+'&filter='+filter;
  return  in_utaajaxpost('/Ajax/uta/in_utils.jsp',data,flag);
}
//External exchange rate
function getCurrencyExchangeRate(flag,callbackfunc,q,base,forcur)
{ var data='action=exchangerate&callback='+encodeURIComponent(callbackfunc)+'&q='+q+'&base='+base+'&forcur='+forcur;
  return  in_utaajaxpost('/Ajax/uta/in_utils.jsp',data,flag);
}

//Schduling API

function getschedule(flag,callbackfunc,actiontype,objectid,start,para)
{
 var data='action='+actiontype+'&callback='+encodeURIComponent(callbackfunc)+'&objectid='+objectid+'&startdate='+start+"&para="+para;
  return  in_utaajaxpost('/Ajax/aj_scheduling.jsp',data,flag);
}


//---------------End Scheduling API

//--------------------------------------- Ajax Script ----------------------------------

 var in_uta_http_request = false;


function in_utaajaxpost(url,parameters,asyn) {
        var in_uta_http_request = false;
              

        //var isFF=navigator.userAgent.indexOf('Firefox')>-1;
        if (window.XMLHttpRequest) { 
             
            in_uta_http_request = new XMLHttpRequest();
            if (in_uta_http_request.overrideMimeType) {
                in_uta_http_request.overrideMimeType('text/plain');
       
            }
        } else if (window.ActiveXObject) { // IE
            try {
                in_uta_http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    in_uta_http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!in_uta_http_request) {
            return null;
        }
if(asyn)
       in_uta_http_request.onreadystatechange =function() { in_uta_ajaxcallback(in_uta_http_request); };
      
      in_uta_http_request.open('POST', url, asyn);
      in_uta_http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      in_uta_http_request.setRequestHeader("Content-length", parameters.length);
      in_uta_http_request.setRequestHeader("Connection", "close");
      in_uta_http_request.send(parameters);

    if(!asyn)  //syn process, return result immediately
       {var mstr=in_uta_http_request.responseText; 
             if(mstr.indexOf("OK::")>-1){
              mstr=mstr.substring(mstr.indexOf('OK::')+4);
              return in_uta_processreturn(mstr,true);  
          
           } else {

            alert(mstr);
            return null;
           }

        }

    }

function in_uta_ajaxcallback(req)
{ //callback function name  is submitted from client to server and server passes it back to client
 
  //result string should always like this  call back function char 2 field1 name =field1 value char 2 field2 name =field2 value ........................
   
   if (req.readyState == 4) {
           if (req.status == 200) {
             var mstr=req.responseText; 
             if(mstr.indexOf("OK::")>-1){
              mstr=mstr.substring(mstr.indexOf('OK::')+4);
               in_uta_processreturn(mstr,false);  
          
           } else {
            alert(mstr);
           }
            

     }else
      alert('Error response:'+req.responseText);
}}

function in_uta_processreturn(mstr,needreturn)
{ //not sure what is the delimiter, use char2 and char1 for now

   var row='\u0001';
   var col='\u0002';
   var arr=mstr.split(row);

   var records =new Array();
   var arrcount=0;
   for (var i=1;i<arr.length;i++)
   if(arr[i].length>0){
    records[arrcount]=arr[i].split(col);
    arrcount++;
   }

  var rset= new in_ssRecordSet(records);     
  if (needreturn)
         return rset;
  else if(arr[0]!=''){
  eval(arr[0]+'(rset)');  
  }
}


// Recordset class
function in_ssRecordSet(result)  
{ if(result==null){ //result is a two dimension array, the first element is fieldnames and the rest are records
  this.recordcount=0;
  this.fieldcount=0;
  this.fieldnames='';
  this.arrset=new Array();
  }else{
  this.recordcount=result.length-1;
  this.fieldcount=result[0].length;
  this.fieldnames=result[0];
  this.arrset=result;
  }
 //---------------Class Body ------------------------------- 

  this.getrawdata= function (){
     return this.arrset;
     }

 //----------------Parsing ------------------------------  
  this.getfieldname=function(idx){
  if(idx>0&&idx<=result[0].length)
    return  this.arrset[0][idx-1];
  else
   return null;
  }
 
        this.getfield=function (recnum,fieldidx){
                 if(recnum<this.arrset.length&&recnum>0)
                   return this.arrset[recnum][fieldidx-1];
                 else
                    return null;
              }
        this.getfieldbyname=function (recnum,fieldname){
        var idx =-1;
        for (var i=0;i<this.arrset[0].length;i++)
         if(this.arrset[0][i]==fieldname)
                 return this.getfield(recnum,i+1);
       return null;
      }

    this.addfieldnames=function (fieldlist){  //Single name or fieldnames delimited by commas
    this.fieldnames=fieldlist;
    this.arrset[0]=fieldlist.split(",");
    this.fieldcount=this.arrset[0].length;
     
    }

    this.getnewrecnumber= function (){
     this.recordcount++;
     this.arrset[this.recordcount] =new Array();
     return this.recordcount;
     }

    this.addfieldvalue= function (recnum,fieldidx,value){
      if(recnum<=this.recordcount&&fieldidx<=this.fieldcount)
      {this.arrset[recnum][fieldidx-1]=value;return 1;
       }else
           return 0;
     }
   
   this.addfieldvaluebyname= function (recnum,fieldname,value){
  for (var i=0;i<this.arrset[0].length;i++)
         if(this.arrset[0][i]==fieldname)
               return this.addfieldvalue(recnum,i+1,value);
     return 0;
     }

   this.getformstr=function(recnum){
    var data="";

   if(recnum>0 &&recnum<=this.recordcount)
     for (var x=0;x<this.fieldcount;x++)
     if(this.arrset[recnum][x]!=null)
     data+="&"+this.arrset[0][x]+"="+encodeURIComponent(this.arrset[recnum][x]);
    return data;
   }
}

