﻿function XmlHttpRequest ()
{  
  this.CreateXMLHttpRequest = _CreateXMLHttpRequest;
}
function _CreateXMLHttpRequest()
{        var xmlHttp = false;
        /* Create a new XMLHttpRequest object to talk to the Web server */
        
        try { //internet explorer için obje yaratılıyor
          xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
          return xmlHttp;
        } catch (e) {
          try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
             return xmlHttp;
          } catch (e2) {
            xmlHttp = false;
          }
        }
        if (!xmlHttp &&(window.XMLHttpRequest||typeof XMLHttpRequest != 'undefined')) 
        { //Mozilla ve diğer browser lar için obje yaratılıyor
          xmlHttp = new XMLHttpRequest();
           return xmlHttp;
        }   
}
function SimAjax()
{         
     this.Success = "";
     this.Process = "";
     this.Failure = "";     
     this.ResponseText = "";  
     this.SendType="POST";                    
     this.Send = function (url,sendParameters,Succ,Proc,Fail,IsXML)
                         {                           
                           var request = new XmlHttpRequest();  
                           var xml = request.CreateXMLHttpRequest();  
                           if(this.SendType == "GET")  
                           {
                              url = url+"?"+sendParameters;  
                              sendParameters = null; 
                           }                         
                           
                           xml.open(this.SendType, url, true); 
                           
                           var ItemType = "";   
                                            
                           if(IsXML==false)
                              ItemType = "application/x-www-form-urlencoded; charset=UTF-8";
                           else
                              ItemType = "text/xml";
                              
                           xml.setRequestHeader("Content-Type", ItemType);                            
                           //Function ne yapacagi belli ediliyor
                           
                           var self = this;                           
                           xml.onreadystatechange = function ()
                           {
                                if (xml.readyState == 1||xml.readyState == 2||xml.readyState == 3) 
                                {      
                                   if(Proc!="")
                                   {
                                      eval(Proc); 
                                   }
                                  document.getElementById('divAjaxLoading').style.display="";
                                  document.disabled = true;
                                }
                                else 
                                if(xml.readyState == 4) //tamamlandı demek oluyor , 1 ve 2 stateler her browser icin calismiyor
                                {
                                   document.getElementById('divAjaxLoading').style.display="none";
                                 
                                  if(xml.status == 200)
                                  {                                                        
                                           self.ResponseText = xml.responseText; 
                                           
                                          if(Succ!="")                                          
                                          {
                                              eval(Succ); 
                                          }
                                           xml.abort();//hata var demek      
                                           request = null;
                                           self.ResponseText = "";
                                           self.ResetData();                                           
                                           
                                   }else
                                   {
                                     if(Fail!="")
                                     {
                                         eval(Fail); //Server hatasi var ise
                                     }else
                                     {
                                       document.write(xml.responseText);//alert("Server side Eror. Status Code is not 200.");
                                     }
                                     xml.abort();
                                     request = null;
                                     self.ResetData();     
                                   }         
                               }                  
                           }                           
                           //Function bitti                                 
                          xml.send(sendParameters);         
                         }
                         
    this.ResetData = function ()
    {
     this.Success = "";
     this.Process = "";
     this.Failure = "";     
     this.ResponseText = "";  
     this.SendType = "POST";
    }
  
}//end of the constructor
function AjaxInAction(SuccSent,InProcSent,FailSent)
{

   this.SuccessSentence = SuccSent;
   this.InProcSentence = InProcSent;
   this.FailureSentence = FailSent;  
   
   this.SendRequest = function (url,sendParameters,type,IsXML)
                    {
                        var request = new SimAjax(); //Global variable to wait browser to execute the script                                            
                        request.Success = this.SuccessSentence;
                        request.Process = this.InProcSentence;
                        request.Failure = this.FailureSentence;
                        
                        if(this.ControlSendType(type))
                           request.SendType = type;
                        else
                           return ;
                           
                        if(this.ControlIsXML(IsXML,type))
                           request.Send(url , sendParameters, request.Success,request.Process,request.Failure,IsXML);  
                        else
                           return ;                   
                    }
                    
   this.ControlSendType = function(type)
                   {
                      if(type!="POST"&&type!="GET")
                      {
                        alert("Send type can be GET or POST. Your SendType is "+type +". Your Request was cancelled.");
                        return false;
                      }else
                        return true;
                   }
   this.ControlIsXML = function(IsXML,type)
                   {
                      if(IsXML!=false&&IsXML!=true)
                      {
                        alert("You did not describe which format is used to send data.It can be xml or plenty text.Your Request was cancelled.");
                        return false;
                      }else if(type!="POST"&&IsXML==true)
                      {
                         alert("The Get Method cannot be used with type 'textxml'.");
                         return false;
                      }
                         return true;
                   }  
   
}
  function PrintSource(Id)
  {
     var source=document.getElementById("ctl00_ContentPlaceHolder1_ctl01_"+Id).innerHTML;
     alert(Id);
     var ajax = new AjaxInAction("ExecutePrint(self.ResponseText)","","");
     ajax.SendRequest("AjaxPages/Print.aspx","icerik=" + source,"POST",false);
	        
    }
    
 function ExecutePrint(ResponseText)
  {
     //alert(ResponseText);           
  }
    
