/*
   This page holds the functions for how the dealers will call chat
   from their site.  There are 2 formats below to choose from.  The
   third format will be used to enable chat for all existing Outsell
   dealers who are on Talisma chat without them having to change their
   website code
*/

// start chat session using expanded layout
function StartChatBig(dealerstring) {
var strUrl = "";
var strQuerystring = "";
var startchatfile = "";
strUrl = escape(top.window.location.href);
startchatfile = "startchat/startchat.php";
strQuerystring = startchatfile + "?" + dealerstring + "&curURL=" + strUrl;
window.open(strQuerystring, 'custclient',
'location=0,width=500,height=320,scrollbars=0');
return false;
}

// start chat session using small layout
// sample dealerstring is dealerid=20981&useprechatform=0
function StartChatSmall(dealerstring) {
var strUrl = "";
var strQuerystring = "";
var startchatfile = "";
strUrl = escape(top.window.location.href);
startchatfile = "startchat/startchat.php";
strQuerystring = startchatfile + "?" + dealerstring + "&curURL=" + strUrl;
window.open(strQuerystring, 'custclient',
'location=0,width=600,height=160,scrollbars=0');
return false;
}

// default chat code for old chat migration from talisma, expanded layout, no prechat form
/*function Outsell_StartChat(dealerid, groupid) {
var strUrl = "";
var strQuerystring = "";
var startchatfile = "";
strUrl = escape(top.window.location.href);
startchatfile = "http://www.outsellchat.com/startchat/startchat.php";
strQuerystring = startchatfile + "?" + "dealerid=" + dealerid + "&groupid=" + groupid + "&useprechatform=0" + "&curURL=" + strUrl;
//alert (strQuerystring);
window.open(strQuerystring, 'custclient',
'location=0,width=500,height=320,scrollbars=0');
//return false;
}*/
//14Apr2011: Changed the functionality to accommodate bold chat
function Outsell_StartChat(dealerid, groupid) {
	//04May2011: Dinesh added this to handle new customer id for every new request
	chatParamsJson=undefined;
		
	var script = document.createElement("script");
	script.type = "text/javascript";
		/*script.src = "http://www.outsellchat.com/IS_IPanel/Bold_Chat_Init_dinesh.php?groupid="+groupid+"&dealerid="+dealerid;*/
		script.src = "http://www.outsellchat.com/IS_IPanel/Bold_Chat_Init.php?groupid="+groupid+"&dealerid="+dealerid;
	document.body.appendChild(script);
	
	
	if(chatParamsJson==undefined || chatParamsJson==null){
		//setTimeout(startChatAftJson,1000);
		intIntrvl=setInterval("startChatAftJson()",1000);
		return false;
	}
	else{
		startChatAftJson();
		return false;
	}
	
}

function startChatAftJson(){
	if(chatParamsJson==undefined || chatParamsJson==null){
		return false;
	}
	intIntrvl=window.clearInterval(intIntrvl);
	chatParams=chatParamsJson[0];
		
	//alert(chatParams.Platform);	
	var strUrl = top.window.location.href;
	if(chatParams.Platform!=null && chatParams.Platform!=undefined && chatParams.Platform=='BC'){
		var customURL="http://www.outsellchat.com/IS_IPanel/AC_Interaction.php?";
		//var customURL2="?curURL=http://www.google.com&dealerid=4b58727ef27ee&groupid=4b16ee9856368&dealerzcode=55414&alt_id=&agentid";
		customURL+="curl="+strUrl;
		customURL+="&groupid="+chatParams.GroupId;
		customURL+="&customerid="+chatParams.CustomerId; /* Z changes this to +chatParams.CustomerId from chatParams.Parameters.CustomerId 4/25/2011 */
		customURL+="&alt_id="+chatParams.AltId;
		customURL+="&dealerid="+chatParams.DealerId;
		customURL+="&AccountId="+chatParams.AccountId;  /* Z adds this to the parameters passed to Custom URL 4/25/2011 */
		customURL+="&dealerzcode=";
		//var url="https://livechat.boldchat.com/aid/"+chatParams.AccountId+"/bc.chat?cwdid="+chatParams.Parameters.ChatWindowDefId+"&amp;rdid="+chatParams.Parameters.DepartmentId+"&amp;vr=&amp;vn=&amp;vi=&amp;ve=&amp;vp=&amp;iq=&amp;curl=";
		var url="https://livechat.boldchat.com/aid/"+chatParams.AccountId+"/bc.chat";
		//alert(url);
		//url+=customURL;
		//url=url+escape(customURL);
		//window.open((window.pageViewer && pageViewer.link || function(link){return link;})(url + (url.indexOf('?')>=0 ? '&amp;' : '?') + 'url=' + escape(document.location.href)), 'Chat4597524645565751997', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480');
		var myForm = document.createElement("form");
		myForm.method="post" ;
		myForm.action=url ;
					
		myForm.setAttribute("target","custclient");
		 var myInput = document.createElement("input") ;
		    myInput.setAttribute("name", "cwdid") ;
		    myInput.setAttribute("value", chatParams.Parameters.ChatWindowDefId);
		    myInput.setAttribute("type", "hidden") ;
		    myForm.appendChild(myInput) ;
		    var myInput = document.createElement("input") ;
		    myInput.setAttribute("name", "rdid") ;
		    myInput.setAttribute("value", chatParams.Parameters.DepartmentId);
		    myInput.setAttribute("type", "hidden") ;
		    myForm.appendChild(myInput) ;
		    var myInput = document.createElement("input") ;
		    myInput.setAttribute("name", "curl") ;
		    myInput.setAttribute("value", customURL);
		    myInput.setAttribute("type", "hidden") ;
		    myForm.appendChild(myInput) ;
		    var myInput = document.createElement("input") ;
		    myInput.setAttribute("name", "vr") ;
		    myInput.setAttribute("value", chatParams.DealerId+","+chatParams.GroupId+","+chatParams.Parameters.DepartmentId);
		    myInput.setAttribute("type", "hidden") ;
		    myForm.appendChild(myInput) ;
		    var myInput = document.createElement("input") ;
		    myInput.setAttribute("name", "vi") ;
		    myInput.setAttribute("value", chatParams.AccountId+","+chatParams.CustomerId);
		    myInput.setAttribute("type", "hidden") ;
		    myForm.appendChild(myInput) ;
		    document.body.appendChild(myForm) ;
		    window.open('about:blank', 'custclient', 'location=0,width=500,height=320,scrollbars=0');
		    myForm.submit();
			return false;
	}else if(chatParams.Platform!=null && chatParams.Platform!=undefined && chatParams.Platform=='IS'){
		if(chatParams.Parameters.PreChatForm=='1'){
			var url="http://www.outsellchat.com/IS_IPanel/PreChatForm.php";
			var myForm = document.createElement("form");
			myForm.method="post" ;
			myForm.action=url ;
						
			myForm.setAttribute("target","custclient");
			 var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "Dealer_ID") ;
			    myInput.setAttribute("value", chatParams.DealerId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "Dep_ID") ;
			    myInput.setAttribute("value", chatParams.Parameters.DepartmentId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "curURL") ;
			    myInput.setAttribute("value", strUrl);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "Group_ID") ;
			    myInput.setAttribute("value", chatParams.GroupId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "ALT_ID") ;
			    myInput.setAttribute("value", chatParams.AltId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "AccountId") ;
			    myInput.setAttribute("value", chatParams.AccountId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    document.body.appendChild(myForm) ;
			    window.open('about:blank', 'custclient', 'location=0,width=500,height=320,scrollbars=0');
			    myForm.submit();
		}else{
			//var divForm=document.createElement("div");
			var myForm = document.createElement("form");
			myForm.method="post" ;
			myForm.action="https://admin.instantservice.com/Customer" ;
						
			myForm.setAttribute("target","custclient");
			 var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "optionaldata") ;
			    myInput.setAttribute("value", chatParams.DealerId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "ai") ;
			    myInput.setAttribute("value", chatParams.AccountId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "di") ;
			    myInput.setAttribute("value", chatParams.Parameters.DepartmentId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "optionaldata1") ;
			    myInput.setAttribute("value", strUrl);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "optionaldata2") ;
			    myInput.setAttribute("value", chatParams.GroupId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "optionaldata3") ;
			    myInput.setAttribute("value", "");
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "optionaldata4") ;
			    myInput.setAttribute("value", chatParams.AltId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    var myInput = document.createElement("input") ;
			    myInput.setAttribute("name", "optionaldata5") ;
			    myInput.setAttribute("value", chatParams.AccountId);
			    myInput.setAttribute("type", "hidden") ;
			    myForm.appendChild(myInput) ;
			    document.body.appendChild(myForm) ;
			    window.open('about:blank', 'custclient', 'location=0,width=500,height=320,scrollbars=0');
			    myForm.submit();
			    //window.open(myForm.submit(), 'custclient','location=0,width=500,height=320,scrollbars=0');
			    
			
		} 
	}
	
}

//Dinesh\Kailash on 14Apr2011:Added this code to make the ajax call to populate the json with all the params
var chatParams;
var chatParamsJson;
var intIntrvl;
var intIntrvl1;
//Dinesh 02June2011:Added following code to handle proactive chat.In case of dealers not in proactive, dealrid and grpid would be missing  
if(!(dealrid==undefined || grpid==undefined || dealrid=='' || grpid=='')){
	var script = document.createElement("script");
	script.type = "text/javascript";
		/*script.src = "http://www.outsellchat.com/IS_IPanel/Bold_Chat_Init_dinesh.php?groupid="+groupid+"&dealerid="+dealerid;*/
		script.src = "http://www.outsellchat.com/IS_IPanel/Bold_Chat_Init.php?groupid="+grpid+"&dealerid="+dealrid;
	document.body.appendChild(script);
	if(chatParamsJson==undefined || chatParamsJson==null){
		//setTimeout(startChatAftJson,1000);
		intIntrvl=setInterval("checkAndLoadPro()",1000);
	}
	else{
		checkAndLoadPro();
	}
}



function checkAndLoadPro(){
	if(chatParamsJson==undefined || chatParamsJson==null){
		return false;
	}
	intIntrvl=window.clearInterval(intIntrvl);
	chatParams=chatParamsJson[0];
	if(chatParams.Platform=='BC' && chatParams.Active=='Yes'){
		var url="http://vms.boldchat.com/aid/"+chatParams.AccountId+"/bc.vms3/vms.js";
		loadjsfile(url);
		//setTimeout(setProChatParams,2000);
		intIntrvl1=setInterval("setProChatParams()",1000);
	}
}





function loadchatParams(){
	var did,gid;
	var bFound=false;
	try{
		var a_array = document.getElementsByTagName("a");
		
		if(a_array===undefined){
			setTimeout(loadchatParams,500);
			return;
		}
		for(var i =0;i<a_array.length;i++) {
			if(a_array[i].getAttribute('onclick')!=undefined && a_array[i].getAttribute('onclick')!=null){
				if(a_array[i].getAttribute('onclick').indexOf('Outsell_StartChat',0)!=-1) {
					bFound=true;
					var  s = a_array[i].getAttribute('onclick');
					var r = s.match(/[\d\.]+/g);
			         did = r[0];
					 gid = r[1];
					break;
				}
			}
			
		}
		if(!bFound){
			setTimeout(loadchatParams,500);
			return;
		}
		getAjaxChatParams(gid,did);
		
	}catch(ex){
		//TODO
	}
}

function getAjaxChatParams(DGroupId,DealerId){
	try{
		if(DGroupId==undefined || DGroupId==null || DealerId==undefined || DealerId==null){
			return;
		}
		var strUrl = escape(top.window.location.href);
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else {// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		var strUrl = escape(top.window.location.href);
		var parameters = "groupid="+DGroupId+"&dealerid="+DealerId+"&CURL="+strUrl;
		
		xmlhttp.open("POST",'http://dev.outsellchat.com/IS_IPanel/Bold_Chat_Init.php',false);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", parameters.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(parameters);			
		chatParams = eval('('+xmlhttp.responseText+');');
		//chatParams=xmlhttp.responseText;
		checkAndLoadPro();
	}catch(ex){
		var excep=ex.description;
		//TODO
	}
}

function loadjsfile(filename){
 var fileref;
 fileref=document.createElement('script');
 fileref.setAttribute("type","text/javascript");
 fileref.setAttribute("src",filename); 
 if (typeof fileref!="undefined"){
	 document.body.appendChild(fileref);
 }
}

function setProChatParams(){
	if(_bcvm==undefined || _bcvm==null){
		return false;
	}
	intIntrvl1=window.clearInterval(intIntrvl1);
	var strUrl = "";
	try {
		strUrl = escape(top.window.location.href);
		var customURL="http://www.outsellchat.com/IS_IPanel/AC_Interaction.php?";
		customURL+="curl="+strUrl;
		customURL+="&groupid="+chatParams.GroupId;
		customURL+="&customerid="+chatParams.CustomerId; /* Z changes this to +chatParams.CustomerId from chatParams.Parameters.CustomerId 4/25/2011 */
		customURL+="&alt_id="+chatParams.AltId;
		customURL+="&dealerid="+chatParams.DealerId;
		customURL+="&AccountId="+chatParams.AccountId;  /* Z adds this to the parameters passed to Custom URL 4/25/2011 */
		customURL+="&dealerzcode=";
		
		var pageViewer = _bcvm.getPageViewer(chatParams.AccountId);	
		pageViewer.setParameter("ChatWindowDefID",chatParams.Parameters.ChatWindowDefId);
		pageViewer.setParameter("ChatWidth", "500");
		pageViewer.setParameter("ChatHeight", "320");
		pageViewer.setParameter("InvitationDefID", chatParams.Parameters.InvitationId);
		pageViewer.setParameter("WindowScheme", "https");
		pageViewer.setParameter("CustomUrl",customURL);
		pageViewer.setParameter("VisitRef", chatParams.DealerId+","+chatParams.GroupId+","+chatParams.Parameters.DepartmentId);
		pageViewer.setParameter("VisitInfo", chatParams.AccountId+","+chatParams.CustomerId);
		pageViewer.pageViewed();
		//var traceurl="http://vms.boldchat.com/aid/"+chatParams.AccountId+"/bc.vmi?vr="+chatParams.DealerId+","+chatParams.GroupId+","+chatParams.Parameters.DepartmentId+";vi="+chatParams.AccountId+","+chatParams.CustomerId+";vn=&amp;vp=&amp;ve=&amp;curl="+customURL;
		var traceurl="http://vms.boldchat.com/aid/"+escape(chatParams.AccountId)+"/bc.vmi?vr="+escape(chatParams.DealerId)+","+escape(chatParams.GroupId)+","+escape(chatParams.Parameters.DepartmentId)+";vi="+escape(chatParams.AccountId)+","+escape(chatParams.CustomerId)+";curl="+escape(customURL);
		//loadjsfile(traceurl);
	}
	catch (excep) {
		alert(excep.message);
	}
	//return false;
}
