 var interval; 
var landingTime = new Date(); 
 var statusFlag=true; 
 var popupCheckEnable='0'; 
 var  isOnline = "0";
 var popupDetails = new Array(5);
 var newwindowForChat = null;
 var varglobal='0';
var popUpImagePath='';
var regEx = /<[^>]*>/g;
var ref = document.referrer.toString(); 
    ref = ref.toString().replace(regEx, "");
var loc = document.location.toString();
    loc=loc.toString().replace(regEx, "");
var tit = document.title.toString(); 
    tit=tit.toString().replace(regEx, "");
var urlforstr = "&wr=" + ref + "&wl=" + loc + "&wt=" + tit; 

function Get_Cookie(name) {   
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;   
   return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    var cookieString = name + "=" +escape(value) +
       //( (expires) ? ";expires=" + expires.toGMTString() : "") +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ( (secure) ? ";secure" : "");
    document.cookie = cookieString;
}

function Delete_Cookie(name,path,domain) {
   if (Get_Cookie(name)) document.cookie = name + "=" +
      ( (path) ? ";path=" + path : "") +
      ( (domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

var today = new Date();
var zero_date = new Date(0,0,0);
today.setTime(today.getTime() - zero_date.getTime());
var cookie_expire_date = new Date(today.getTime() + (8 * 7 * 86400000));

function setVisitorID() {
   if (Get_Cookie('VisitorID')) {
       var VisitorID = Get_Cookie('VisitorID');
   }else{
       Set_Cookie('VisitorID',Math.random(),cookie_expire_date);       
   }
}

function setSessionID() {
   if (!Get_Cookie('SessionID'))
       Set_Cookie('SessionID',Math.random());
}

function setOperatorStatus() {
   if (!Get_Cookie('Ostatus'))
       Set_Cookie('Ostatus',true);
}

function setVisitorInfo(value) {
   if (!Get_Cookie('ViRequest'))
       Set_Cookie('ViRequest',value);
}

var loaded_script = true; 

function readCookie(name) {
	var nameEQ = name + "=";	
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

 function GeoLocation()
 {
    var etime_url = 'http://gd.geobytes.com/Gd?after=-1&variables=GeobytesCountry,GeobytesCity,GeobytesRegion';		
    AjaxCallFullUrl( etime_url, 'geolocation');   
    setTimeout("generateLocation()",2000);
 }
//===Region Mainak Start=========
function GetImageWidth(src)
{
    return 250;
}

function GetImageHeight(src)
{
    var objImage = new Image();
    objImage.src = src;
    var height = parseInt(objImage.height);
    return height+50; // add close image height
}



count=0;           
orgPosition=0;
NoOfShakes=20;
shiftIndex=0;
//direction = true/false
function Shake(id,blnDirection) 
{  
   	if(count == 0){
   	    orgPosition = parseInt(document.getElementById(id).style.left); 
   	}
   	if(count<(NoOfShakes/2))
   	{   	    
   	        shiftIndex += 3;
   	}
   	else{
   	        shiftIndex -= 3; 
   	}  	  	
   	if(blnDirection){
   	    var temp = orgPosition + shiftIndex
   	    document.getElementById(id).style.left = temp + "px";
   	}
   	else{
   	    var temp = orgPosition - shiftIndex
   	    document.getElementById(id).style.left = temp + "px";
   	} 	  
   	if(count<NoOfShakes){
   	  	setTimeout("Shake('" + id + "'," + (!blnDirection) + ");", 100);
   	  	count++;
   	}   	
   	else{
   	    document.getElementById(id).style.left = orgPosition + "px";  
   	    count=0;           
        shiftIndex=0;
   	    return;
   	}
}

//====================================================================================
//onclick="javascript:moveRight(divPic,400);"
function moveRight(id,finalPos) 
{       	
   	var orgPos = parseInt(document.getElementById(id).style.left);   	
    moveObjRight(id,orgPos,finalPos);  
}

function moveObjRight(id,pos,finalPos)
{  
    document.getElementById(id).style.left = pos + "px";
    var newPos = 2 + pos;
    if(pos<finalPos){
        setTimeout("moveObjRight('" + id + "'," + newPos + "," + finalPos + ");", 10);
    }  
}
//====================================================================================
//=========onclick="javascript:moveLeft(divPic,400);
function moveLeft(id,finalPos)  
{       	
   	var orgPos=parseInt(document.getElementById(id).style.left);   	
    moveObjLeft(id,orgPos,finalPos);  
}

function moveObjLeft(id,pos,finalPos)
{  
    document.getElementById(id).style.left = pos + "px";
    var newPos = pos - 2;
    if(pos>finalPos){
        setTimeout("moveObjLeft('" + id + "'," + newPos + "," + finalPos + ");", 10);
    }  
}
//======================================================================================
//=======onclick="javascript:moveDown(divPic,350)
function moveDown(id,finalPos)  
{       	
    var orgPos=parseInt(document.getElementById(id).style.top);   	
    moveObjDown(id,orgPos,finalPos);  
}

function moveObjDown(id,pos,finalPos)
{  
    document.getElementById(id).style.top = pos + "px";
    var newPos = pos + 2;
    if(pos<finalPos){
        setTimeout("moveObjDown('" + id + "'," + newPos + "," + finalPos + ");", 10);
    }  
}


//======================================================================================

function moveUp(id,finalPos)  
{       	
   	var orgPos=parseInt(document.getElementById(id).style.top);   	
    moveObjUp(id,orgPos,finalPos);
}

function moveObjUp(id,pos,finalPos)
{  
    document.getElementById(id).style.top = pos + "px";
    var newPos = pos - 2;
    if(pos>finalPos){
        setTimeout("moveObjUp('" + id + "'," + newPos + "," + finalPos + ");", 10);
    }  
}

function GetWindowWidth()
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return parseInt(myWidth);
}


function GetWindowHeight()
{
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return parseInt(myHeight);
}

//==========================================================================
function AjaxCall(url,id)
{
        var head = document.getElementsByTagName('head').item(0); 	
	    var old = document.getElementById(id); 
	    if (old)
	    { 
		    head.removeChild(old); 
	    }        
        var s = document.createElement('script'); 
        s.src  = prefix_url + url + '&random=' + Math.random(); 
	    s.type = 'text/javascript'; 
	    s.defer = true; 
	    s.id = id;
	    void(head.appendChild(s));
}

function AjaxCallFullUrl(url,id)
{
        var head = document.getElementsByTagName('head').item(0); 	
	    var old = document.getElementById(id); 
	    if (old)
	    { 
		    head.removeChild(old); 
	    }        
        var s = document.createElement('script'); 
        s.src  = url + '&random=' + Math.random(); 
	    s.type = 'text/javascript'; 
	    s.defer = true; 
	    s.id = id;
	    void(head.appendChild(s));
}

function createXmlHttpRequest(){
    if (window.XMLHttpRequest){
        var oHttp = new XMLHttpRequest();
        return oHttp;
    }
    else if (window.ActiveXObject){
        var versions = new Array("MSXML2.XmlHttp.6.0", "MSXML2.XmlHttp.3.0","Microsoft.XMLHTTP");
        for (var i = 0; i < versions.length; i++){
            try{
                var oHttp = new ActiveXObject(versions[i]);
                return oHttp;
            }
            catch (error){}
        }
    }
    return null;
}



function OnLoadCheckingCallback()
{
    var check = true;
    if(parseInt(sessionTimeout)==0){
        check = true;
    }
    else{
        var tDiff = (new Date() - landingTime) / 1000; 
        if ( tDiff < (parseInt(sessionTimeout)*60) ){
            check = true;
        }
       else{
            check = false;
       } 
    }    
	if ( check )
	{
        checkProcess();        
        setTimeout("OnLoadCheckingCallback();",5000);     
    }
}

function autoPopupCallback()
{
    if( proactivepopUpvaluere != null && proactivepopUpvaluere != "" ){
        setTimeout("showAutoPopup();", (  (parseInt(proactivepopUpvaluere)) *1000) );
    }   
}
function GetActualLeft(left)
{
    var offset;
    if(window.pageXOffset){
        offset = window.pageXOffset;
    }else if(document.body != null && document.body.scrollLeft){
        offset = document.body.scrollLeft;
    }else if(document.documentElement != null && document.documentElement.scrollLeft){ 
        offset = document.documentElement.scrollLeft;
    }else{
        offset = 0;
    } 
    var total = parseInt(left) + parseInt(offset);
    return total;
}

function GetActualTop(top)
{
    var offset;
    if(window.pageYOffset){
        offset = window.pageYOffset;
    }else if(document.body != null && document.body.scrollTop){
        offset = document.body.scrollTop;
    }else if(document.documentElement != null && document.documentElement.scrollTop){ 
        offset = document.documentElement.scrollTop;
    }else{
        offset = 0;
    } 
    var total = parseInt(top) + parseInt(offset);
    return total;
}

  function generateOperatorProactivePopUp(popupArray,isAuto)    //popUpImagePath
{
    if(popupArray[4] != '' && popupArray[4] != null)
    {       
            varglobal=0;  
            popupCheckEnable='0';  
            if(!document.getElementById("rtpopup_jagenerated"))
            { 
                var ee_container = document.createElement("DIV");
                ee_container.id = 'rtpopup_jagenerated';       
                ee_body = document.getElementsByTagName("BODY")[0];        
                void(ee_body.appendChild(ee_container));    
                var parentPath = prefix_url;
                var imageSrc = parentPath + popupArray[4];
                var css_path = parentPath + "css/popupChatbox.css";
                var top = popupArray[0] + "px";
                var left = popupArray[1] + "px";
                
                 var headID = document.getElementsByTagName("head")[0];                 
                 var fileref = document.createElement("link");
                 fileref.setAttribute("rel", "stylesheet");
                 fileref.setAttribute("type", "text/css");
                 fileref.setAttribute("href", css_path);
                 headID.appendChild(fileref);
             
                var final_left = 0;           
                var final_top = 0;                 
                //alert('Scroll Left = ' + GetActualLeft(0) + ' & Scroll Top = ' + GetActualTop(0) );
                if(popupArray[2] == 'Top left'){
                    final_left = parseInt(popupArray[1]);           
                    final_top = parseInt(popupArray[0]);     
                }else if(popupArray[2] == 'Bottom left'){
                    final_top = parseInt(GetWindowHeight()) - ( parseInt(GetImageHeight(imageSrc)) + parseInt(popupArray[0]) );
                    final_left = parseInt(popupArray[1]);   
                }else if(popupArray[2] == 'Top Right'){
                    final_left = parseInt(GetWindowWidth()) - ( parseInt(GetImageWidth(imageSrc)) + parseInt(popupArray[1])  );
                    final_top = parseInt(popupArray[0]);
                }else if(popupArray[2] == 'Bottom Right'){
                    final_left = parseInt(GetWindowWidth()) - ( parseInt(GetImageWidth(imageSrc)) + parseInt(popupArray[1])  );
                    final_top = parseInt(GetWindowHeight()) - ( parseInt(GetImageHeight(imageSrc)) + parseInt(popupArray[0]) );
                }
                if(final_left < 0 ){final_left = 0;}
                if(final_top < 0){final_top = 0;}
                
                final_left = GetActualLeft(final_left);
                final_top = GetActualTop(final_top);

                var initial_left;           
                var initial_top;                   
                if(popupArray[3] == 'Left to Right'){                        
                        initial_left = GetActualLeft(0);
                        initial_top = final_top;
                       //alert('left to right : initial_left=' + initial_left +' initial_top=' + initial_top + ' final_left=' + final_left + ' final_top=' + final_top);
                       SetPopupBody( 'rtpopup_jagenerated',initial_left,initial_top,imageSrc,isAuto);
                       moveRight('formsettings',final_left);
                }else if(popupArray[3] == 'Top to Bottom'){
                        initial_left = final_left;
                        initial_top = GetActualTop(0);
                        SetPopupBody( 'rtpopup_jagenerated',initial_left,initial_top,imageSrc,isAuto); 
                        moveDown('formsettings',final_top);
                }else if(popupArray[3] == 'Bottom to Top'){
                        initial_left = final_left;
                        initial_top = GetActualTop(parseInt(GetWindowHeight()) - parseInt(GetImageHeight(imageSrc)));   
                        SetPopupBody( 'rtpopup_jagenerated',initial_left,initial_top,imageSrc,isAuto); 
                        moveUp('formsettings',final_top);     
                }else if(popupArray[3] == 'Right to Left'){            
                        initial_left = GetActualLeft(  parseInt(GetWindowWidth()) - parseInt(GetImageWidth(imageSrc))  );                    
                        initial_top = final_top;               
                        SetPopupBody( 'rtpopup_jagenerated',initial_left,initial_top,imageSrc,isAuto); 
                        moveLeft('formsettings',final_left);
                }else if(popupArray[3] == 'Shake'){                                             
                        initial_left = final_left;
                        initial_top = final_top;        
                        SetPopupBody( 'rtpopup_jagenerated',initial_left,initial_top,imageSrc,isAuto);                     
                        Shake('formsettings',true);           
                }else if(popupArray[3] == 'Still'){
                        SetPopupBody( 'rtpopup_jagenerated',final_left,final_top,imageSrc,isAuto);                    
                }
             }        
     }     
}

function opInvite()
{    
    if(varglobal == '1' && readCookie('Ostatus'))
    {
        proactivepopUpvaluere=0;            
        if( !newwindowForChat && popupCheckEnable=='1' ){	            
            generateOperatorProactivePopUp(popupDetails,'0');
        }        
    }
}

function showAutoPopup()
{    
          if( popupCheckEnable == '1' && readCookie('Ostatus') && varglobal != "1" && !newwindowForChat ){   
                generateOperatorProactivePopUp(autoPopupDetails,'1');  
                AutoPopupShown();
          }     
}

function Initiate(wid,did,cid)
{
    setVisitorID();
    var x = readCookie('VisitorID');
    if(x == null){
        alert("Please enable browser cookie for proper working of chat");
    } 
   else{
        var url = 'https://livechat.reliabletechnologies.in/ServerCallPage.aspx?id=' + x + '&wid='+ wid + '&did=' +did+ '&cid=' + cid;
        AjaxCallFullUrl( url,'Initiate');
   } 
}
//===Region Mainak End=========
