﻿function UUID(){
	this.id = this.createUUID();
}

UUID.prototype.valueOf = function(){ return this.id; }
UUID.prototype.toString = function(){ return this.id; }

UUID.prototype.createUUID = function(){
	var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
	var dc = new Date();
	var t = dc.getTime() - dg.getTime();
	var h = '-';
	var tl = UUID.getIntegerBits(t,0,31);
	var tm = UUID.getIntegerBits(t,32,47);
	var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2
	var csar = UUID.getIntegerBits(UUID.rand(4095),0,7);
	var csl = UUID.getIntegerBits(UUID.rand(4095),0,7);

	var n = UUID.getIntegerBits(UUID.rand(8191),0,7) + 
	UUID.getIntegerBits(UUID.rand(8191),8,15) + 
	UUID.getIntegerBits(UUID.rand(8191),0,7) + 
	UUID.getIntegerBits(UUID.rand(8191),8,15) + 
	UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long
	return tl + h + tm + h + thv + h + csar + csl + h + n; 
}

UUID.getIntegerBits = function(val,start,end){
	var base16 = UUID.returnBase(val,16);
	var quadArray = new Array();
	var quadString = '';
	var i = 0;
	for(i=0;i<base16.length;i++){
		quadArray.push(base16.substring(i,i+1));	
	}
	for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){
		if(!quadArray[i] || quadArray[i] == '') quadString += '0';
		else quadString += quadArray[i];
	}
	return quadString;
}

UUID.returnBase = function(number, base){
	return (number).toString(base).toUpperCase();
}

UUID.rand = function(max){
	return Math.floor(Math.random() * (max + 1));
}

function Readfromxml()
{  
    var Cdate = new Date()
    var randomnum = Cdate.getMinutes() + "" + Cdate.getSeconds();   
    
    if(pagerefreshed)
    {
        var vidckname = 'VID_'+ cmpnme;
        var nidckname = 'NID_'+ cmpnme;
        VID = readCookie(vidckname);
        NID = readCookie(nidckname);
        
        if(VID == null || NID == null)
        {
            VID = Cdate.getDate() + "" + Cdate.getMonth() + "" + Cdate.getFullYear() + "" + Cdate.getHours() + "" + Cdate.getMinutes() + "" + Cdate.getSeconds();
            var ONID = new UUID();
            NID = ONID.id;
            createCookie(vidckname, VID, 120);
            createCookie(nidckname, NID, 120);
        }
        else
        {
            firstinsert = "r";
            createCookie(vidckname, VID, 120);
            createCookie(nidckname, NID, 120);
        }
        
        visitorhit.src = ActualPath + "scripts/iptracker.aspx?num=" + randomnum + "&vid=" + VID + "&nid=" + NID + "&finsert=" + firstinsert + "&sloc=" + escape(window.location) + "&sref=" + escape(document.referrer);
        visitorhit.onload = loadimg;
        
        controlimage.src = ActualPath + "scripts/image.aspx?num=" + randomnum;
        controlimage.onload = popwinsize;
        
        Visitorvisit.src = ActualPath + "scripts/visittorvisit.aspx?num=" + randomnum;
        Visitorvisit.onload = EnableProSell;
    }
    else
    {
        visitorhit.src = ActualPath + "scripts/iptracker.aspx?num=" + randomnum + "&vid=" + VID + "&nid=" + NID + "&finsert=" + firstinsert;
        visitorhit.onload = loadimg;
    }
    
    firstinsert = "s";
    pagerefreshed = false;
    
    setTimeout('Readfromxml()', 10000);
}
         
function parseQuery ( query ) 
{
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) 
   {
       var KeyVal = Pairs[i].split('=');
       if ( ! KeyVal || KeyVal.length != 2 ) continue;
       var key = unescape( KeyVal[0] );
       var val = unescape( KeyVal[1] );
       val = val.replace(/\+/g, ' ');
       Params[key] = val;
   }
   return Params;
}

function loadimg()
{   
    var show = false;
        
    if(visitorhit.width == 2)
    {
        Online = true;
        ps_softaxis_ci.src = ActualPath+"RegUsers/"+cmpnme+"/image/status_on.gif";
        ps_softaxis_ci.alt = "LiveChat Online";
        show = true;
        
        if(visitorhit.height == 2)
        {
            var url = ActualPath+"visitor/livechat/invitation.aspx?vid=" + VID + "&nid=" + NID;
            if(ps_showpopup == false)
            {
                document.cookie = "prosell=done";
                var newpop = document.getElementById("ps_softaxis_chat_invitation");
                newpop.innerHTML = '<iframe id="chatinvitation" name="chatinvitation" allowtransparency="true" style="background:transparent;" src="' + url + ' "frameborder="no" width="271" height="191" scrolling="no"></iframe>';
                ps_showpopup = true;
                staticbar();
            }
        }
        
        ps_softaxis_ci.onclick=function()
        {
            document.cookie = "prosell=done";
            window.open(ActualPath + 'visitor/livechat/chatwindow.aspx?vid=' + VID + '&nid=' + NID,'_blank','titlebar=yes,toolbar=no,dependent=yes,scrollbars=no,height= ' + popheight +',width= '+ popwidth);
            return false;
        }
    }
    else if(visitorhit.width == 1)
    {
        Online = false;
        
        if(visitorhit.height == 3)
        {
            ps_softaxis_ci.src = ActualPath+"RegUsers/"+cmpnme+"/image/status_none.gif";
        }
        else 
        {       
            ps_softaxis_ci.src = ActualPath+"RegUsers/"+cmpnme+"/image/status_off.gif";
            ps_softaxis_ci.alt ="LiveChat Offline";
            show = true;

            if(visitorhit.height == 2)
            {
                ps_softaxis_ci.onclick=function()
                {
                    window.open(ActualPath+'scripts/redirectpage.aspx');
                }  
            }
            else
            {
                ps_softaxis_ci.onclick=function()
                {
                    window.open(ActualPath+'visitor/livechat/chatwindow.aspx?vid=' + VID + '&nid=' + NID,'_blank','titlebar=yes,toolbar=no,dependent=yes,scrollbars=no,height= ' + popheight +',width= '+ popwidth);
                    return false;
                }   
            }
        }
    }
    
    while (divUserlist.childNodes.length > 0)
	{
		divUserlist.removeChild(divUserlist.childNodes[0]);
	}
	if(show)
    {
        ps_softaxis_ci.onmouseover=function()
        {
             window.status='http://livechat.supportaxis.net/'+cmpnme+'';
             return true;
        };
        ps_softaxis_ci.onmouseout=function()
        {
            window.status='';
        };
        
        ps_softaxis_ci.style.cursor = "pointer";
        ps_softaxis_ci.border="0";
     
        divUserlist.appendChild(ps_softaxis_ci);
    }     
}
    
  
function popwinsize()
{
    popwidth = controlimage.width;
    if(popwidth == null || popwidth == "" || typeof(popwidth) == "undefined")
    {
        popwidth = 550;
    }
    popheight = controlimage.height;
    if(popheight == null || popheight == "" || typeof(popheight) == "undefined")
    {
        popheight = 420;
    }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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 init()
{ 
    var scripts = document.getElementById('SoftScript');
    var url = scripts.src;
    var lenght = url.split('scripts');
    
    if(lenght.length == 1)
    {
        lenght = url.split('Scripts');
    }
        
    ActualPath = lenght[0];
    
    var queryString = scripts.src.replace(/^[^\?]+\??/,'');
    var params = parseQuery( queryString );
    
    cmpnme=params['cmp'];
    
    divUserlist = document.getElementById("OpStatusImg");
    Readfromxml();             
}

var VID;
var NID;
var ps_softaxis_ci = new Image();
var controlimage = new Image();
var visitorhit = new Image();
var Visitorvisit = new Image();
var divUserlist;
var ps_showpopup = false;
var popwidth = 550;
var popheight = 420;
var firstinsert = "f";
var pagerefreshed = true;
var ActualPath;
var cmpnme;
init();

var persistclose=0 //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session

var verticalpos="fromtop" //enter "fromtop" or "frombottom"

function iecompattest()
{
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function get_cookie(Name) 
{
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) 
    {
        offset = document.cookie.indexOf(search)
        if (offset != -1) 
        {
            offset += search.length
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            returnvalue=unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}


function staticbar()
{   
    var startX = (document.body.offsetWidth/2)- 135//set x offset of bar in pixels
    if(navigator.userAgent.indexOf("Firefox")!=-1)
    {
        var startY = (window.innerHeight/2)- 95 //set y offset of bar in pixels
    }
    else
    {
        if(document.documentElement.clientHeight != 0)
        {  
            var startY = (document.documentElement.clientHeight/2)- 95 //set y offset of bar in pixels
        }
        else
        {
            var startY = (document.body.offsetHeight/2)- 95 //set y offset of bar in pixels
        }
    }
	barheight=document.getElementById("ps_softaxis_chat_invitation").offsetHeight
	var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var d = document;
	function ml(id)
	{
		var el=d.getElementById(id);
		if (!persistclose || persistclose && get_cookie("remainclosed")=="")
		el.style.visibility="visible"
		if(d.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
		el.x = startX;
		if (verticalpos=="fromtop")
		el.y = startY;
		else{
		el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
		el.y -= startY;
		}
		return el;
	}
	window.stayTopLeft=function()
	{
		if (verticalpos=="fromtop"){
		var pY = ns ? pageYOffset : iecompattest().scrollTop;
		ftlObj.y += (pY + startY - ftlObj.y)/8;
		}
		else{
		var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
		ftlObj.y += (pY - startY - ftlObj.y)/8;
		}
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml("ps_softaxis_chat_invitation");
	stayTopLeft();
}

window.onresize = staticbar;

function EnableProSell()
{ 
    if(Visitorvisit.width == 2)
    {
        ProActiveSell = true;
    }
    else
    {
        ProActiveSell = false;
    }
}

var Online = false;
var ProActiveSell = false;
var ClicktimerID = '';
var ClickThroughEvent = false;
var Clickdone = false;

function invokeprosell(e) 
{   
    if(ProActiveSell && Online)
    {  
	    Clickdone = true;

	    if (ClicktimerID != '') clearTimeout(ClicktimerID);	
            
	    if (!ClickThroughEvent) 
	    {    
	 	    var alreadyinvoked = chkcookie();
            if(!alreadyinvoked)
            {
	            //window.onbeforeunload = null;
	            CallProSellChat();

	            alert("Hold on!! Get a deal now, talk to a online live representative. Kindly click 'Cancel' on the next window to start talking."); 

                return "Press the 'Cancel' button below to chat with live agent.";
	        }
	        else
	        {
	            var cookie_name = "prosell";
	            var cookie_date = new Date ( );  
                cookie_date.setTime ( cookie_date.getTime() - 1 );
                document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
	        }
	    }
	}
}

window.onbeforeunload = invokeprosell;

function EventClick() 
{
	if (ClicktimerID != '') 
	    clearTimeout(ClicktimerID);

	ClickThroughEvent = true;
	ClicktimerID  = setTimeout("ClickInterval();", 1000);
}

function ClickInterval() 
{
	if(!Clickdone)
	{
		ClickThroughEvent = false;
	}
	ClicktimerID = '';
}

document.documentElement.onmousedown = EventClick;

document.documentElement.onclick = EventClick;

document.documentElement.onkeypress = EventClick;

function CallProSellChat() 
{ 
    document.cookie = "prosell=done";
    var newpop = document.getElementById("ps_softaxis_chat_invitation");
    var url = ActualPath + "visitor/livechat/prosell.aspx?vid=" + VID + "&nid=" + NID;
    newpop.innerHTML = '<iframe id="chatinvitation" name="chatinvitation" allowtransparency="true" style="background:transparent;" src="' + url + ' "frameborder="no" width="350" height="179" scrolling="no"></iframe>';
    
    staticbar();
}

function chkcookie()
{
    var results = document.cookie.match ( '(^|;) ?prosell=([^;]*)(;|$)' );
    if ( results )
    return true;
    else
    return false;
}


