var NS = (document.layers) ? true : false; 
var IE = (document.all) ? true : false; 
var DOM = (document.getElementById) ? true : false; 
if (IE) DOM = false; 
var MAC = (navigator.platform) && (navigator.platform.toUpperCase().indexOf("MAC") >= 0); 
if (NS) MAC = false; 

var commandImage; 
var sendImage = true;
var checkImgTimer = null;
var chatAvailableFunction = null;
var chatNotAvailableFunction = null;
var ctcInterval = null;
var ctcInitR = true;

function getSubCookie(c_name, sub_key)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
			{ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			var subCookie = unescape(document.cookie.substring(c_start,c_end))
			s_start = subCookie.indexOf(sub_key + "=")
			if (s_start != -1)
			{
			s_start=s_start + sub_key.length+1 
			s_end=subCookie.indexOf("&",s_start)
			if (s_end==-1) s_end=subCookie.length
				return subCookie.substring(s_start,s_end);				
			}			
		}
		return '';
	}
    var x=unescape(dc.substring(begin + prefix.length, end));    
    return unescape(dc.substring(begin + prefix.length, end));
}



function startChatAvailabilityCheck()
{
try{


var url = window.location.href;
//if(url.toLowerCase().indexOf("espanol") != -1)

if(url.toLowerCase().indexOf("espanol") == -1)
{	
	checkChatAvailability('showCTCIcon()','hideCTCIcon()', appid, aims_state, aims_chat_availability_url );
	ctcInterval = setInterval( "checkChatAvailability('showCTCIcon()','hideCTCIcon()', '"+appid+"', '"+aims_state+"', '"+aims_chat_availability_url+"')", 10000 );	
}


//	checkChatAvailability('showCTCIcon()','hideCTCIcon()', appid, aims_state, aims_chat_availability_url );
//	ctcInterval = setInterval( "checkChatAvailability('showCTCIcon()','hideCTCIcon()', '"+appid+"', '"+aims_state+"', '"+aims_chat_availability_url+"')", 10000 );
    }
catch(e){}
}


// Call this function when ever you want to check if chat is available or not
function checkChatAvailability( chatAvailableFunc, chatNotAvailableFunc, appId, state, url )
{

	chatAvailableFunction = chatAvailableFunc;
	chatNotAvailableFunction = chatNotAvailableFunc;


	 if ( sendImage )
	 { 
 		 
 		 sendRequest( url, appId, state );
 		 checkImgTimer = setInterval("checkImage()", 1000);
                
                /* if(sendImage==true)
                 { 
                   setCookie("IsChatAvailable","s",null)
                 }
                 else
                  {
                  setCookie("IsChatAvailable",null,null)
                  }
                 */
	 }
}

function sendRequest( imgUrl, appId, state ) 
{ 

	 if (DOM && IE) { 
	 		 document.body.removeChild(commandImage); 
	 		 commandImage = document.createElement('IMG'); 
	 		 commandImage.style.visibility = "hidden"; 
	 		 document.body.appendChild(commandImage); 
	 } 
	else if (!MAC) {
	 		 commandImage= new Image; 
	 }else{
			commandImage= new Image; 
	 }
	 var pageID = Math.round(Math.random()*9999999999); 
	 imgUrl = imgUrl + "?appid=" + appId + "&state=" + state + "&id=" + pageID;
	 if( ctcInitR ){
		 ctcInitR = false;
		imgUrl += "&initR=yes";
	 }
	 

	 commandImage.src = imgUrl;  

	 sendImage = false; 
}

function checkImage() 
{ 
	if (!sendImage) 
	{
 		 if( !commandImage.complete )
		 { 
 		 	 return; 
 		 } 
 		 var w = commandImage.width; 
 		 var h = commandImage.height; 
 		 sendImage = true;
		 if( checkImgTimer!=null )
		{
		 	 clearInterval( checkImgTimer );
		 	 checkImgTimer = null;
		 }
 		 checkChatAvailabilityResult(w,h); 
 	 } 
}

function checkChatAvailabilityResult( w, h )
{
	if( w=="1" && h=="1" )
	{	
		// Chat Available
		if( chatAvailableFunction!=null && typeof chatAvailableFunction != "undefined" ){
			 eval( chatAvailableFunction );
		}
	}
	else
	{		
	 	// Chat not available
		if( chatNotAvailableFunction!=null && typeof chatNotAvailableFunction != "undefined" ){
			eval( chatNotAvailableFunction );
		}
	}
}


function checkRedirectPage(popUp,url)
	{
	

	  if(popUp==1)
	   {
	    window.location.href=url;	
	   }	
	}







// Chat Availability check for CallUs Page  


function startCallUsChatAvailabilityCheck()
{


	checkChatAvailability('showCTCIcon()','hideCTCIcon()', appid, aims_state, aims_chat_availability_url );
	//ctcInterval = setInterval( "checkChatAvailability('showCTCIcon()','hideCTCIcon()', '"+appid+"', '"+aims_state+"', '"+aims_chat_availability_url+"')", 10000 );
}


// Call this function when ever you want to check if chat is available or not
function checkCallUsChatAvailability( chatAvailableFunc, chatNotAvailableFunc, appId, state, url )
{

	chatAvailableFunction = chatAvailableFunc;
	chatNotAvailableFunction = chatNotAvailableFunc;


	 if ( sendImage )
	 { 
 		 
 		 sendRequest( url, appId, state );
 		 //checkImgTimer = setInterval("checkImage()", 1000);
                
                /* if(sendImage==true)
                 { 
                   setCookie("IsChatAvailable","s",null)
                 }
                 else
                  {
                  setCookie("IsChatAvailable",null,null)
                  }
                 */
	 }
}
