function escapeURLonly(gURL){
	var returnURL = "";
	var headerIdx = gURL.indexOf("://");
	var headerPart = gURL.substring(0,headerIdx+3);
	var urlPart = gURL.substring(headerIdx+3,gURL.length);
	urlPart = escape(urlPart);
	returnURL = headerPart + urlPart;
	return returnURL;
}

// -------- for SSL fix (V.5.9.2) -----------
var CCShttpPrefix;
var CCShttpsPrefix;
var CCShttpProtocol;
var CCShttpsProtocol;

CCShttpProtocol = "http:";
CCShttpsProtocol = "https:";
CCShttpPrefix = CCShttpProtocol + "//pic1";
CCShttpsPrefix = CCShttpsProtocol + "//secure";

function CCS_checkSSL() {
	return (location.protocol == CCShttpsProtocol); 
}

function CCS_replaceStrSSLPrefix(givenStr){
	if (CCS_checkSSL()) {
	    if (givenStr.indexOf("clickchatsold") >= 0) {
			givenStr = givenStr.replace(CCShttpPrefix, CCShttpsPrefix);
		}
		givenStr = givenStr.replace(CCShttpProtocol, CCShttpsProtocol);	
	}
	return givenStr;
}
// -------- END for SSL fix (V.5.9.2) -----------

document.returnValue = true;

var DEF_MSG_prechat = "Please Wait while connecting to demo operators ";
var DEF_ccs_SysTxtColor = "#FF0000";
var DEF_ccs_bgcolor = ""
var DEF_ccs_bordercolor = ""
var DEF_ccs_logo_imgLoc = "http://pic1.clickchatsold.com/images/en/ccs_img_chatlogo.gif"
var DEF_ccs_chat_btnLoc = "http://pic1.clickchatsold.com/images/en/ccs_img_chat.gif";
var DEF_ccs_close_btnLoc = "http://pic1.clickchatsold.com/images/en/ccs_img_close.gif";	
var DEF_ccs_mini_close_btnLoc ="http://pic1.clickchatsold.com/images/btn_miniclose.gif";
var DEF_ccs_dot_tranLoc = "http://pic1.clickchatsold.com/images/dottran.gif";

var loadtimerId =0;
var checktimerId = 0;
var loc = document.URL;
loc = escapeURLonly(loc);
var accname = "cashprior";
var checksrv = "http://ultra5.clickchatsold.com";

var MSG_prechat = "Would you like to chat with an agent? ";
var ccs_SysTxtColor = "#008000";
var ccs_bgcolor = "#FFFFFF";
var ccs_bordercolor = "#C0C0C0";
var ccs_logo_imgLoc = "http://pic1.clickchatsold.com/images/bubble_mini.gif";
var ccs_chat_btnLoc = "http://pic1.clickchatsold.com/images/en/ccs_img_chat.gif";
var ccs_close_btnLoc = "http://pic1.clickchatsold.com/images/en/ccs_img_decline.gif";
var ccs_server_url = "http://ultra5.clickchatsold.com";

// V5.9.4.6
// ---------------------------------
var autoreqtimerId = 0;
var ccs_autoreqInSec = 0;

function ccs_varIsSet(ccs_variable)
{
	return( typeof(ccs_variable) != 'undefined' );
}

function ccs_setWithAPIVar(){
	try{
	if (ccs_varIsSet(ccs_api_autoreqInSec))
	{
		if (ccs_api_autoreqInSec >= 2) {
			ccs_autoreqInSec = ccs_api_autoreqInSec;
		}
	}
	} catch(ccs_jserror){}
}

ccs_setWithAPIVar();
// ---------------------------------

if (MSG_prechat == "") { MSG_prechat = DEF_MSG_prechat; }
if (ccs_SysTxtColor == "") { ccs_SysTxtColor = DEF_ccs_SysTxtColor; }
if (ccs_bgcolor == "") { ccs_bgcolor = DEF_ccs_bgcolor; }
if (ccs_bordercolor == "") { ccs_bordercolor = DEF_ccs_bordercolor; }
if (ccs_logo_imgLoc == "") { ccs_logo_imgLoc = DEF_ccs_logo_imgLoc; }
if (ccs_chat_btnLoc == "") { ccs_chat_btnLoc = DEF_ccs_chat_btnLoc; }
if (ccs_close_btnLoc == "") { ccs_close_btnLoc = DEF_ccs_close_btnLoc; }

if (CCS_checkSSL()) {
	ccs_logo_imgLoc = CCS_replaceStrSSLPrefix(ccs_logo_imgLoc);
	ccs_chat_btnLoc = CCS_replaceStrSSLPrefix(ccs_chat_btnLoc);
	ccs_close_btnLoc = CCS_replaceStrSSLPrefix(ccs_close_btnLoc);
	DEF_ccs_mini_close_btnLoc = CCS_replaceStrSSLPrefix(DEF_ccs_mini_close_btnLoc);
	DEF_ccs_dot_tranLoc = CCS_replaceStrSSLPrefix(DEF_ccs_dot_tranLoc);
}


function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	//alert(this.ver + " " + this.agent);
	this.dom=document.getElementById?1:0
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie8=(this.ver.indexOf("MSIE 8")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6||this.ie7||this.ie8
	plat = new String(navigator.platform);
	if ((plat.toUpperCase()).indexOf("MAC") != -1) { this.mac = true; } 
	else { this.mac = false; }
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie8 || this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	this.safari = (this.ver.indexOf("Safari")>-1 && this.dom)?1:0;	
	return this
}

var bw=new lib_bwcheck()

var cookiepiece = document.cookie.split("; "); 
var time = new Date();
var expiry = new Date(time.getTime() + 5 * 24 * 60 * 60 * 1000); 

var sitecid = Math.round(Math.random() * 999999 + time.getSeconds() * Math.random() * 999999 + time.getMinutes()* Math.random() * 999999);
var j = Math.round(sitecid+Math.random()*999999*Math.random());
var i = 0;
var tempWinHandle;
var ccs_Page;
var refid = 0;
var firstcheck = true;
var repeat = false;
var checktime = 0;

var reqchatnum = 0;
var reqchatnumMax = 1;

function getCookie(name) { 
    cookiepiece = document.cookie.split("; "); 
    for (var i=0; i < cookiepiece.length; i++) {
      next = cookiepiece[i].split("="); 
      if (next[0] == name) 
        return unescape(next[1]); 
    }
    return null;
}


function setCookie(name, value) { 
    if (value != null && value != "")
      document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString()+"; path=/;";    
}

var lasttime = getCookie(accname+"lasttime");
var ref = getCookie(accname+"ref");

if ( ref == null || ref == "" || ref == "null" ) {
	ref = document.referrer;
	setCookie(accname+"ref",ref);
}

if ( lasttime == null || lasttime == "" || lasttime == "null" ) {
	lasttime = "Never";
}

var refloc = navigator.appName+" ("+navigator.userAgent+")"+"\n"+lasttime+"\n"+ ref;
var extrainfo = escape(refloc);

setCookie(accname+"lasttime",time.toGMTString());

if ( getCookie(accname+"sitecid") != null ) {
	sitecid =  getCookie(accname+"sitecid");
	repeat = true;
} else {
	setCookie(accname+"sitecid",sitecid);
}


var msg;
var tmpimg;

function ccs_checkstatus() {		
	clearTimeout(checktimerId);		
	if ( firstcheck ) {
	     refid = getCookie(accname+"refid");
	     if ( refid == null ) {
	     	refid = 0;
	     } else {
	     	refid ++;
	     }
	     setCookie(accname+"refid",refid+"");	     	      
	     firstcheck = false;	
		if ((bw.mac) && ((bw.ie5) || (bw.safari)) ) {
			tmpimg = document.images["ccststImg2"];
		} else {
			tmpimg = new Image();	
			tmpimg.onload = trackresult;										
		}      
    	    tmpimg.src = checksrv+"/D0/"+accname+"."+sitecid+"."+extrainfo+"?"+loc+"."+j;	
    	     j++;
	     checktimerId = setTimeout('ccs_checkstatus()',15000);
	} else {	
		if ( refid == getCookie(accname+"refid") ) {			
			if ((bw.mac) && ((bw.ie5) || (bw.safari)) ) {
				tmpimg = document.images["ccststImg2"];
			} else {
				tmpimg = new Image();	
				tmpimg.onload = trackresult; 											
			}   
			
    	    tmpimg.src = checksrv+"/D0/"+accname+"."+sitecid+".?"+loc+"."+j;	
			if ( checktime <= 480 ) {
				checktimerId = setTimeout('ccs_checkstatus()',15000);
				checktime++;
			}
			j++;
		} 
	}
}

function trackresult(){	
	var width = 0;
	// for 5.9.2 (bug fix)
	if (tmpimg != null) {
		if ( !(bw.safari) ) {
			if (tmpimg != "") {
				width = tmpimg.width;
			} 
		} else {
			width = tmpimg.width;
		}
	}	
	if ( width == 2 ) {	
		ccs_doAction();
	}
}

function ccs_doAction() {	
	clearTimeout(checktimerId);	
	ccs_requestchat();
	checktimerId = setTimeout('ccs_checkstatus()',15000);
}


function popChatWin(groupint) {
	// check what to 'pop'
	if (!groupint) 
	 groupint = 0;	
	 	
	ccs_Page = ccs_server_url+"/D5/"+accname+"."+sitecid+"."+groupint+"."+j;
	tempWinHandle = window.open(ccs_Page, accname,'width=430,height=370,directories=no,location=no,menubar=no,scrollbars=no, status=no,toolbar=no,resizable=no');						 		

	if ( !(bw.safari) ) {
		if (tempWinHandle != null && tempWinHandle != "") {	
			tempWinHandle.focus();
		}
	}
	j++;
	
}

function ccs_requestchat(){
	var popblocked = false;
	
	if (bw.safari) {
		// if safari, then do check
		if (reqchatnum >= reqchatnumMax) return 0;
	}
			
	ccs_Page = ccs_server_url+"/D9/"+accname+"."+sitecid+".NA.html."+j;
	tempWinHandle = window.open(ccs_Page, accname+"show",'width=280,height=165,directories=no,location=no,menubar=no,scrollbars=no, status=no,toolbar=no,resizable=no');						 		
	if (tempWinHandle != null) {
		if ( !(bw.safari) ) {
			if (tempWinHandle != "") {
				tempWinHandle.focus();
			} else {
				popblocked = true;
			}
		}
	} else {
		// cannot return handle, pop blocked
		popblocked = true;	
	}
			
	if (popblocked) {
		// for popup friendly operator chat request
		processReqChange();
		CCS_crstart();
	}
	
	reqchatnum++;
	j++;
	
}

function startup() {	
	clearTimeout(checktimerId);	
	ccs_checkstatus();
	
	// V5.9.4.6
	if (ccs_autoreqInSec > 0) {
		clearTimeout(autoreqtimerId); 
		autoreqtimerId = setTimeout('CCS_autoreq()', ccs_autoreqInSec*1000);
	}
}

function writeImg() {
	if ((bw.mac) && ((bw.ie5) || (bw.safari))) {
		document.write("<img id='ccststImg2' name='ccststImg2' src='"+ DEF_ccs_dot_tranLoc  +"' onload='trackresult()'>");
	}
}

// javascript for floating operator chat request dialog
var CCS_YY=0;	// Y position
var CCS_XX=0;	// X position of the scrolling objects
var CCS_xstep=1;
var CCS_delay_time=10;
var CCS_ch=0;
var CCS_oh=0;
var CCS_cw=0;
var CCS_ow=0;
var CCS_yon=0;
CCS_crtimer=null;

var CCS_ns4=document.layers?1:0
var CCS_ie=document.all?1:0
var CCS_ns6=document.getElementById&&!document.all?1:0
	
var CCS_panelStr = "";
CCS_panelStr += "<div id='CCS_FLOWINGCR' STYLE='position:absolute;visibility:hidden;'>"
CCS_panelStr += ".";
CCS_panelStr += "</div>";

if (!CCS_ns4) {
	// if is netscape 4, Will not do the floating chat request
	// due to bug in netscape 4 on inline div style
	//alert(CCS_panelStr);
	document.write(CCS_panelStr);
}

function processReqChange() {
	var CCS_pcLayout = ""
	CCS_pcLayout += "<table width='250' border='1' cellspacing='0' cellpadding='0' bgcolor='"+ ccs_bgcolor +"' bordercolor='"+ ccs_bordercolor +"'>";
	CCS_pcLayout += "<tr><td><table width='100%' border='0' cellspacing='0' cellpadding='0' bgcolor='"+ ccs_bordercolor +"'><tr><td><font face='Arial' color='#808080' style='font-size: 10px;'>&nbsp;</font></td>";
	CCS_pcLayout += "	<td align='right'><a href='javascript:CCS_crdestory()' onMouseover=\"CCS_statwrite('');return true\"><img src='"+ DEF_ccs_mini_close_btnLoc +"' width='10' height='10' border='0' alt='' align='right'></a></td></tr>";
	CCS_pcLayout += "</table></td></tr>";
	CCS_pcLayout += "<tr><td align='center'><table width='100%' border='0' cellspacing='1' cellpadding='1'><tr><td>";
	CCS_pcLayout += "<img src='"+ ccs_logo_imgLoc +"' border='0' alt='' align='left'>";
	CCS_pcLayout += "<font face='Arial' size='2' color='"+ ccs_SysTxtColor +"'><b>"+ MSG_prechat +"</b></font>";
	CCS_pcLayout += "<br><br></td></tr>";
	CCS_pcLayout += "<tr><td align='center' bgcolor='"+ ccs_bordercolor +"'><br>";
	CCS_pcLayout += "<a href='javascript:popChatWin();javascript:CCS_crdestory();' onMouseover=\"CCS_statwrite('');return true\"><img src='"+ ccs_chat_btnLoc +"' border='0' alt=''></a>&nbsp;&nbsp;&nbsp;<a href='javascript:CCS_crdestory();' onMouseover=\"CCS_statwrite('');return true\"><img src='"+ ccs_close_btnLoc +"' border='0' alt=''></a>&nbsp;";
	CCS_pcLayout += "</td></tr></table></td></tr>";
	CCS_pcLayout += "<tr><td align='left' bgcolor='#EEEEEE'><font face='Arial' size='-2' color='#808080'>Powered by <a href='http://www.clickchatsold.com' target='_blank' style='color:#6A6A6A; text-decoration: none;'>CCS</a></font></td></tr></table>";
	if(CCS_ie){
		CCS_FLOWINGCR.innerHTML = CCS_pcLayout;
	}
	else if (CCS_ns6){
		document.getElementById('CCS_FLOWINGCR').innerHTML = CCS_pcLayout;
	}
}

if(CCS_ie){
	if (document.all["CCS_FLOWINGCR"]) {
		//CCS_XX=document.body.clientWidth;
		CCS_FLOWINGCR.style.top=CCS_YY;
		CCS_FLOWINGCR.style.left=0;
		CCS_FLOWINGCR.onmouseover=CCS_crpause;
		CCS_FLOWINGCR.onmouseout=CCS_crstart;
	}
}
else if (CCS_ns6){
	if (document.getElementById('CCS_FLOWINGCR')) {
		//CCS_XX=window.innerWidth;
		document.getElementById('CCS_FLOWINGCR').style.top=CCS_YY;
		document.getElementById('CCS_FLOWINGCR').style.left=0;
		document.getElementById('CCS_FLOWINGCR').onmouseover=CCS_crpause;
		document.getElementById('CCS_FLOWINGCR').onmouseout=CCS_crstart;	
	}
}


function CCS_reloc1()
{
	if(CCS_yon==0){CCS_XX=CCS_XX-CCS_xstep;}
	else{CCS_XX=CCS_XX+CCS_xstep;}

	if (CCS_ie){
		CCS_cw=document.body.clientWidth;
		CCS_ow=CCS_FLOWINGCR.offsetWidth;
		CCS_ch=document.body.clientHeight;
	}else if (CCS_ns6){
		CCS_cw=window.innerWidth;
		CCS_ow=document.getElementById("CCS_FLOWINGCR").offsetWidth;
		CCS_ch=window.innerHeight;
	}
		
	if(CCS_XX<0){CCS_yon=1;CCS_XX=0;}
	if(CCS_XX>=(CCS_cw-CCS_ow)){CCS_yon=0;CCS_XX=(CCS_cw-CCS_ow);}
	
	if(CCS_ie){
		CCS_FLOWINGCR.style.left=CCS_XX;
		CCS_FLOWINGCR.style.top=(CCS_ch/2)+document.body.scrollTop;
	}else if (CCS_ns6){
		document.getElementById("CCS_FLOWINGCR").style.left=CCS_XX
		document.getElementById("CCS_FLOWINGCR").style.top=(CCS_ch/2)+window.pageYOffset
	}
}

function CCS_onad()
{
	CCS_kickstartloop();
	if(CCS_ie){
		CCS_FLOWINGCR.style.visibility="visible";
	}
	else if (CCS_ns6){
		document.getElementById("CCS_FLOWINGCR").style.visibility="visible";
	}
}

function CCS_kickstartloop()
{
	if (CCS_crtimer == null) {
		CCS_crtimer=setTimeout('CCS_loopfunc()',CCS_delay_time);
	}
}

function CCS_loopfunc()
{
	CCS_reloc1();
	CCS_crtimer=setTimeout('CCS_loopfunc()',CCS_delay_time);
}

function CCS_crstart()
{
	if (!(CCS_ie||CCS_ns6))
		return;
	
	if(CCS_ie){
		CCS_FLOWINGCR.onmouseover=CCS_crpause;
		CCS_FLOWINGCR.onmouseout=CCS_crstart;
	}
	else if (CCS_ns6){
		document.getElementById('CCS_FLOWINGCR').onmouseover=CCS_crpause;
		document.getElementById('CCS_FLOWINGCR').onmouseout=CCS_crstart;	
	}
	CCS_onad();
}

function CCS_crpause()
{
	if (CCS_ie||CCS_ns4||CCS_ns6){
		if(CCS_crtimer!=null){
			clearTimeout(CCS_crtimer);
			CCS_crtimer = null;
		}
	}
}

function CCS_crdestory(){
	if(CCS_ie){
		CCS_FLOWINGCR.style.visibility="hidden";
		CCS_FLOWINGCR.onmouseout="";
	}
	else if (CCS_ns6){
		document.getElementById("CCS_FLOWINGCR").style.visibility="hidden";
		document.getElementById("CCS_FLOWINGCR").onmouseout="";
	}
	CCS_crpause();
	CCS_XX = 0;
	CCS_YY = 0;
}

function CCS_crreset()
{
	CCS_crdestory();
	CCS_crstart();
}

function CCS_statwrite(yourtext)
{
	window.status=yourtext;
}

// V5.9.4.6
function CCS_autoreq() 
{
	clearTimeout(autoreqtimerId);	
    ccs_requestchat();
}

writeImg();
startup();