﻿        var chatTimeout;
       
        function instantiateChat()
        {
            window.resizeTo(400, 200);
			initiateChat();
			setChatAssignmentTimer();
			setTimeout('EndChat()', 120000);	
			
        }
        
        function loadChat()
        {
			if(status)
			{

			    window.location.href= "client.aspx?chatId="+chatId+"&rossId="+designerName;
                focus();
			}else{
				alert("Failed to initialize chat session! Pleas try again");
			}
        }
        
		function getXmlHttpObject(){ 
	        var xmlHttp=null;
	        if (window.XMLHttpRequest){
		        xmlHttp=new XMLHttpRequest();
	        }else if (window.ActiveXObject){
		        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	        }
	        return xmlHttp;
        }               
        
        function soapHeader12()
        {
         var hdr ="<?xml version='1.0' encoding='utf-8'?>" +
                 "<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'>"+
                 "<soap12:Body>";
                 return hdr;
        }
        
        function soapFooter12()
        {
            return "</soap12:Body>"+"</soap12:Envelope>";
        }
        
       function initiateChat()
       {
			xmlHttp=getXmlHttpObject();
			if (xmlHttp==null)
			{
				alert ("Browser does not support HTTP Request");
				return;
			} 

			xmlHttp.open("POST", chatServiceUrl, true);    
			xmlHttp.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
			xmlHttp.setRequestHeader("Content-Length","1289");		  						
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.onreadystatechange=initiateNewChatStatus;	
		     
			var soapRequest=soapHeader12();
			soapRequest+="<IniateChat xmlns='"+chatServiceNameSpace+"'>"+				
				"<token>"+token+"</token> "+        
				"<referrer><![CDATA["+referrer+"]]></referrer>"+
				"<promoID>"+promoId+"</promoID>"+
				"</IniateChat>";
            soapRequest+=soapFooter12();								

			xmlHttp.send(soapRequest);    
			return;        
       }    
       
		function initiateNewChatStatus()
		{     
		    try{
		    
			    if(xmlHttp.readyState==4 && xmlHttp.status == 200)
			    {		
			                   
                            var result = xmlHttp.responseXML.getElementsByTagName('IniateChatResult');
                            
                            /* soap 1.2 sometimes doesn't load properly in IE*/
                            if(result.length==0)
                            {
                                   xmlHttp.responseXML.load(xmlHttp.responseBody);
                                   result=   xmlHttp.responseXML.getElementsByTagName('IniateChatResult');
                                   
                                   if(result.length==0)
                                   {
                                      alert("Failed to initiate chat in IE");                                      
                                   } 
                                   else
                                   {
                                            chatId = result[0].childNodes[0].childNodes[2].firstChild.nodeValue;
                                            designerName = result[0].childNodes[0].childNodes[1].firstChild.nodeValue;
                                            
                                   }                                     
                            } /* firefox loads properly */
                            else
                            {

                                chatId = result[0].childNodes[0].childNodes[2].firstChild.nodeValue;
                                designerName = result[0].childNodes[0].childNodes[1].firstChild.nodeValue;
                                
                            }		
		                
		                if(chatId>0)
		                {		                    
		                   checkIfChatAccepted();
		                }
			    }//end if
			}catch(err){
			    status = false;
			    alert("An exception has occurred. Please try again. We apologize for the inconvenience");
			    }
		} //end function
		
		 
		function isChatAcceptedStatus()
        { 
            
            if (xmlHttp.readyState==4 && xmlHttp.status == 200)
            {                  
                var result = xmlHttp.responseXML.getElementsByTagName('IsChatAcceptedResult');
                
                /* soap 1.2 sometimes doesn't load properly in IE*/
                if(result.length==0)
                {
                       xmlHttp.responseXML.load(xmlHttp.responseBody);
                       result=   xmlHttp.responseXML.getElementsByTagName('IsChatAcceptedResult');
                       
                       if(result.length==0)
                       {
                          alert("Failed to initiate chat in IE");                                      
                       } 
                       else
                       {
                            status = eval(result[0].childNodes[0].nodeValue);
                       }                                     
                } /* firefox loads properly */
                else
                {
                    
                    status = eval(result[0].childNodes[0].nodeValue);                                                                
                }
                                                   
                if(!status)
                {        	                
                    checkIfChatAccepted();
                }
                else
                {
                    loadChat();
                }
	        }
	        
        }
        
        function isChatAccepted()
        {
        
	        xmlHttp=getXmlHttpObject();
	        if (xmlHttp==null)
	        {
		        alert ("Browser does not support HTTP Request");
		        return;
	        } 
        	    
			xmlHttp.open("POST", chatServiceUrl, true);    
			xmlHttp.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
			xmlHttp.setRequestHeader("Content-Length","1289");		  						
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.onreadystatechange=isChatAcceptedStatus;	
            
            var soapRequest=soapHeader12();
            
                soapRequest+="<IsChatAccepted xmlns='"+chatServiceNameSpace+"'>"+        
                "<chatID>"+chatId+"</chatID>"+ 
                "<rossID></rossID>"+                
                "</IsChatAccepted >";
                soapRequest+=soapFooter12();
                
            xmlHttp.send(soapRequest);     
               
          return;            
        }    
        
        function checkIfChatAccepted()
        {         
            clearTimeout(chatTimeout);            
            chatTimeout = setTimeout("isChatAccepted()", 5000);
        }    
        
        function showCallBackForm()
        {
            window.resizeTo(600, 550)
            clearTimeout(chatTimeout);            
            showHide('lyrInit', false);            
            showHide('lyrCall', true);
            document.body.style.background="#FFFFCC";
            
        }
        
        function showHide(id, bool)
        {
            var display = 'none';
            var visible ='hidden';
            if(bool)
            {
                display = 'block';
                visible= 'visible';
            }  
                      
            document.getElementById(id).style.display = display;            
            document.getElementById(id).style.visibility = visible;
        }
        
        
        function EndChatStatus()
        {       
            try{ 
                if (xmlHttp.readyState==4 && xmlHttp.status==200)
                {  
                    var result = xmlHttp.responseXML.getElementsByTagName('EndChatSessionResult');  
                    /* soap 1.2 sometimes doesn't load properly in IE*/
                    if(result.length==0)
                    {
                           xmlHttp.responseXML.load(xmlHttp.responseBody);
                           result=   xmlHttp.responseXML.getElementsByTagName('EndChatSessionResult');
                           
                           if(result.length==0)
                           {
                              alert("Failed to end chat in IE");                                      
                           } 
                           else
                           {
                                status = eval(result[0].childNodes[0].nodeValue);
                           }                                     
                    } /* firefox loads properly */
                    else
                    {
                        
                        status = eval(result[0].childNodes[0].nodeValue);                                                                
                    } 
                    
                    if(!status)
                        alert("Failed to terminate chat request");
                          
                    showCallBackForm();                
                }               
            }catch(e){} 
        }

        function EndChat()
        {        
 	            xmlHttp=getXmlHttpObject();
	            if (xmlHttp==null)
	            {
		            alert ("Browser does not support HTTP Request");
		            return;
	            } 
            	    
			    xmlHttp.open("POST", chatServiceUrl, true);    
			    xmlHttp.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
			    xmlHttp.setRequestHeader("Content-Length","1289");		  						
			    xmlHttp.setRequestHeader("Connection", "close");
                xmlHttp.onreadystatechange=EndChatStatus;	

                var soapRequest=soapHeader12();
                    soapRequest+= "<EndChatSession xmlns='"+chatServiceNameSpace+"'>"+        
                    "<chatID>"+chatId+"</chatID>"+            
                    "<sender>0</sender>"+
                    "</EndChatSession >";
                    soapRequest+=soapFooter12();
                    
                xmlHttp.send(soapRequest);     

               return false;   
        }        
        
        function setChatAssignmentTimer()
        {   
            clearTimeout(assignmentTimeOut);
            assignmentTimeOut =setTimeout('reassignConsultant()', 45000);            
        }
        
        function reassignChatStatus()
        {
            try{
              if (xmlHttp.readyState==4&& xmlHttp.status ==200){                      
                    setChatAssignmentTimer()
                   return;
                }
            }catch(err){alert("error reassigning: " + err.description);}
        }

        function reassignConsultant()
        {
            /* continue with current designer until a designer has accepted this chat*/
               
	        xmlHttp=getXmlHttpObject();
	        if (xmlHttp==null)
	        {
		        alert ("Browser does not support HTTP Request");
		        return;
	        } 
        	    
            	    
		    xmlHttp.open("POST", chatServiceUrl, true);    
		    xmlHttp.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
		    xmlHttp.setRequestHeader("Content-Length","1289");		  						
		    xmlHttp.setRequestHeader("Connection", "close");

            xmlHttp.onreadystatechange=reassignChatStatus;	
            
            var soapRequest=soapHeader12();
                soapRequest+= "<ReassignChat xmlns='"+chatServiceNameSpace+"'>"+        
                "<chatID>"+chatId+"</chatID>"+            
                "</ReassignChat >"+soapFooter12();

            xmlHttp.send(soapRequest); 
            return;    
      }        