
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 checkChatAvailabilityStartTimer = null;
var strEnv = "";//to check the environment ie,SIT/staging/Prodn
var aimsUrl = "";
var appId = "";
var ctcAvailabilityInterval = 10000;
var strServiceProduct = "";
var strCategoryId = "";
var chatChannel = "";
var strServiceProduct = "";
var strCategory = "";
var strSubCategory = "";
var strState = "";
var strIdName = "";
var strIdCode = "";
var strComment = "";
var strEmail = "";
var strUserId = "";
var strName = "";
var strPendingOrderNumber = "";
var strCustTN = "";
var strSelectedServiceChk = "";
var strSelectedCategoryChk = "";
var strSelectedSubCategoryChk = "";
var strCondtionChkforChat = "false";
var cntOfVisit = 0;//Added for WER - 2009040702

// Call this function only ONCE and it checks for availability every interval
function checkChatAvailabilityLoop()
{    
    strEnv = strEnvUsed;//To get the Host name
    
    //point to AIMS SIT, if we are in test env
	if(strEnv == 'http://www21.verizon.com/' || strEnv == 'https://www21.verizon.com/' || strEnv =='http://www20.verizon.com/' || strEnv == 'https://www20.verizon.com' )		
		aimsUrl = "https://collaborateextst.verizon.com";
	//point to AIMS Staging, if we are in staging env		 
	else if (strEnv == 'http://www98.verizon.com/' || strEnv == 'https://www98.verizon.com/')
		aimsUrl = "https://collaborateextstg.verizon.com";
	//point to AIMS Prodn, if we are in prodn env
	else
		aimsUrl = "https://collaborateext.verizon.com";
	
	SelectedC2CCenter();  
	 
	//Added for WER - 2009040702 - Starts
    //var initR = "yes";
	
	//if(cntOfVisit == 0)
	//{
	//    cntOfVisit = 1;
	//    initR = "yes";
	//}
	//else
	//{
	//    initR = "no";
	//}
    //Added for WER - 2009040702 - Ends
	    
	var ctcAvailabilityUrl = aimsUrl + "/aims/encore/CheckChat.serv?appid="+appId+"&state="+strState;
	if( checkChatAvailabilityStartTimer == null ){
		checkChatAvailability( ctcAvailabilityUrl );
		checkChatAvailabilityStartTimer = setInterval( "checkChatAvailability( '"+ctcAvailabilityUrl+"')", ctcAvailabilityInterval );
	}
}

//// Call this function to stop the availability check
//function stopCheckChatAvailabilityLoop()
//{
//	if( checkChatAvailabilityStartTimer!=null ) { clearInterval(checkChatAvailabilityStartTimer); checkChatAvailabilityStartTimer = null; }
//	if( checkImgTimer!=null ) { clearInterval(checkImgTimer); checkImgTimer = null; }
//	sendImage = true;
//	commandImage = null;
//}

// Call this function to stop the availability check
function stopCheckChatAvailabilityLoop()
{    
    if( checkChatAvailabilityStartTimer!=null )
    {        
        clearInterval(checkChatAvailabilityStartTimer);
        checkChatAvailabilityStartTimer = null;
        document.getElementById("IMG1").style.display = "none";
    }
	if( checkImgTimer!=null ) 
	{
	    clearInterval(checkImgTimer); 
	    checkImgTimer = null; 
	}
	sendImage = true;
	commandImage = null;
	
	strCondtionChkforChat = "false";
	
	strSelectedServiceChk = document.getElementById("ddlSelectService")[document.getElementById("ddlSelectService").selectedIndex].value;
	strSelectedCategoryChk = document.getElementById("ddlCategory")[document.getElementById("ddlCategory").selectedIndex].value;
	strSelectedSubCategoryChk = document.getElementById("ddlSubCategory")[document.getElementById("ddlSubCategory").selectedIndex].value;

	if (strSelectedCategoryChk.toUpperCase() == "SUPPORT" && strSelectedSubCategoryChk.toUpperCase() == "GET HELP WITH AN ORDER DUE TODAY OR PAST DUE")//FSC
    {
        strCondtionChkforChat = "true";
    }
	if (strSelectedCategoryChk.toUpperCase() == "SUPPORT" && strSelectedSubCategoryChk.toUpperCase() == "REPORT A REPAIR OR INSTALLATION ISSUE")//FSC
	{
	    strCondtionChkforChat = "true";
    }		
	if(strSelectedServiceChk.toUpperCase() == "WIRELESS")
	{
	    strCondtionChkforChat = "true";
	}	    

	if(strCondtionChkforChat.toUpperCase() != "TRUE")
	{
	    ChangeofCatSubCat();
	}
}

function ChangeofCatSubCat()
{
    if(strState != "")
    {        
        checkChatAvailabilityLoop();
    }
}

// Call this function when ever you want to check if chat is available or not
function checkChatAvailability( url )
{
    //Added for WER - 2009040702 - Starts
    var initR = "no";
	
	if(cntOfVisit == 0)
	{
	    cntOfVisit = 1;
	    initR = "yes";
	}
	else
	{
	    initR = "no";
	}
    //Added for WER - 2009040702 - Ends
    
    var url = aimsUrl + "/aims/encore/CheckChat.serv?appid="+appId+"&state="+document.getElementById("cboState")[document.getElementById("cboState").selectedIndex].value+"&initR="+initR;//Added initR for WER - 2009040702
	 if ( sendImage )
	 { 
 		 sendRequest( url );
 		 checkImgTimer = setInterval("checkImage()", 1000);
	 }
}

function sendRequest( imgUrl ) 
{ 

	 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; 
	 }
	 var pageID = Math.round(Math.random()*9999999999); 
	 imgUrl = imgUrl + "&id=" + pageID;	 

	 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(document.getElementById('ddlSelectService') != null && document.getElementById('ddlCategory') != null && document.getElementById('ddlSubCategory') != null && document.getElementById('cboState') != null)
    {
        strServiceProduct = document.getElementById("ddlSelectService")[document.getElementById("ddlSelectService").selectedIndex].value;
        strCategory = document.getElementById("ddlCategory")[document.getElementById("ddlCategory").selectedIndex].value;
        strSubCategory = document.getElementById("ddlSubCategory")[document.getElementById("ddlSubCategory").selectedIndex].value;
        strState = document.getElementById("cboState")[document.getElementById("cboState").selectedIndex].value;
    }
	//alert("!");
	//alert("checkChatAvailabilityResult( w, h )::w="+w+", h="+h);
	//window.status = (new Date()) + " checkChatAvailabilityResult( w, h )::w="+w+", h="+h;
	
	if((strServiceProduct == "Internet Service" && strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "HIGH SPEED INTERNET TECH SUPPORT (CALL/CHAT ONLY)") || (strServiceProduct == "TV or Entertainment" && strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "FIOS TECH SUPPORT (CALL/CHAT ONLY)"))//Added on 16/04/2009 for WER 2009040802
    {
        if( w=="1" && h=="1" )
	    {	
		    document.getElementById("divClickToChat").style.display = "block";
            document.getElementById("IMG1").style.display = "block";            
	    }
	    else
	    {	
	 	    document.getElementById("divClickToChat").style.display = "none";	
	 	    document.getElementById("IMG1").style.display = "none";
	 	    if(strState != "")
	 	    {
	 	        window.open(strEnv+"foryourhome/ContactUs/ContactUsPhoneState.aspx?CBOState=" + strState,"_self");
	 	    }
	 	    else
	 	    {
	 	        window.open(strEnv+"foryourhome/ContactUs/ContactUsPhoneState.aspx?CBOState=" + MD,"_self");
	 	    }
	 	     
	    }
	}
	else
	{		
	    if( w=="1" && h=="1" )
	    {	
		    document.getElementById("divClickToChat").style.display = "block";
            document.getElementById("IMG1").style.display = "block";        
	    }
	    else
	    {	
	 	    document.getElementById("divClickToChat").style.display = "none";	
	 	    document.getElementById("IMG1").style.display = "none";
	    }
	}
	
	/*if( w=="1" && h=="1" )
	{	
		document.getElementById("divClickToChat").style.display = "block";
        document.getElementById("IMG1").style.display = "block";        
	}
	else
	{	
	 	document.getElementById("divClickToChat").style.display = "none";	
	 	document.getElementById("IMG1").style.display = "none";
	}*/
}

function ctcChatAvailable(){
	try{
		document.getElementById("IMG1").style.display = "inline";		
	}catch(e){
		//alert("Chat icons are not included in the code");
	}
}

function ctcChatUnavailable(){
	try{
		document.getElementById("IMG1").style.display = "none";		
	}catch(e){
		//alert("Chat icons are not included in the code");
	}
}

var ctcChatWindow = null;

function openChatWindow( chatType, width, height, blankURL)
{
	var aimsChatURL = aimsUrl + "/aims/encore/VZCTC.serv";
	if( ctcChatWindow!=null && ctcChatWindow.closed==false ){
		ctcChatWindow.focus();
	}else{
		var chatWindowName = 'ctcChatWindow';
		var chatWindowFeatures = 'left=30,top=10,width='+width+',height='+height;
		ctcChatWindow = window.open( blankURL, chatWindowName, chatWindowFeatures);
	    
	    if(document.getElementById('txtATN1') != null)
        {
            strCustTN = document.getElementById('txtATN1').value + document.getElementById('txtATN2').value + document.getElementById('txtATN3').value;
        }    
        if(document.getElementById('txtPendingOrderNo') != null)
        {
            strPendingOrderNumber = document.getElementById('txtPendingOrderNo').value;
        }
        strName = document.getElementById('txtfName').value + " " + document.getElementById('txtlName').value;
        strUserId = "";
        strEmail = document.getElementById('txtEmail').value;
        strComment = document.getElementById('suggestions').value;
        if (strState=="MD" || strState=="DC" || strState=="VA" || strState=="WV")
	    {
		    strIdName = "Account Number";
		    strIdCode = document.frmContactUsEmail.txtactNum.value;
	    }
	    else if (strState=="NY" || strState=="CT" || strState=="NJ" || strState=="PA" || strState=="DE" || strState=="MA" || strState=="ME" || strState=="NH" || strState=="RI" || strState=="VT")
	    {
		    strIdName = "Customer Code";
		    strIdCode = document.frmContactUsEmail.txtactNum.value;
	    }
	    else
	    {
		    strIdName = "Account Number";
		    strIdCode = document.frmContactUsEmail.txtactNum.value;
	    }
        
        if (chatChannel == "FSC")
        {
            var chatXML = ctcChatXMLHeader + "<tn>" + strCustTN + "</tn><name>" + strName + "</name>";
            chatXML+= "<state>" + strState + "</state><email>" + strEmail + "</email><appid>" + appId + "</appid>";
            chatXML+= "<QuestionCategory id=" + strCategoryId + ">" + strServiceProduct + "(" + strCategory + ")(" + strSubCategory + ")" + "</QuestionCategory>";
            chatXML+= "<OrderNumber>" + strPendingOrderNumber + "</OrderNumber><params>" + ctcChatExtXMLParams + ctcChatXMLParams + "</params>";
            chatXML+= ctcChatXMLFooter;            
        }
        if (chatChannel == "RMCC")
        {
            var chatXML = ctcChatXMLHeader + "<tn>" + strCustTN + "</tn><name>" + strName + "</name>";
            chatXML+= "<state>" + strState + "</state><email>" + strEmail + "</email><appid>" + appId + "</appid><lob>residential</lob>";
            chatXML+= "<QuestionCategory id=" + strCategoryId + ">" + strServiceProduct + "(" + strCategory + ")(" + strSubCategory + ")" + "</QuestionCategory>";
            chatXML+= "<CustomerVerificationField><name>" + strIdName + "</name><value>" + strIdCode + "</value></CustomerVerificationField>";
            chatXML+= "<Question>" + strComment + "</Question><params>" + ctcChatExtXMLParams + ctcChatXMLParams + "</params>"
            chatXML+= ctcChatXMLFooter;
        }
        if (chatChannel == "eCenter")
        {
            var chatXML = ctcChatXMLHeader + "<tn>" + strCustTN + "</tn><name>" + strName + "</name>";
            chatXML+= "<state>" + strState + "</state><reg_user_id>" + strUserId + "</reg_user_id>";
            chatXML+= "<email>" + strEmail + "</email><appid>" + appId + "</appid>";
            chatXML+= "<QuestionCategory id=" + strCategoryId + ">" + strServiceProduct + "(" + strCategory + ")(" + strSubCategory + ")" + "</QuestionCategory>";
            chatXML+= "<CustomerVerificationField><name>" + strIdName + "</name><value>" + strIdCode + "</value></CustomerVerificationField>";
            chatXML+= "<Question>" + strComment + "</Question><params>" + ctcChatXMLParams + "</params>";
            chatXML+= ctcChatXMLFooter;
        }        
        
		document.forms["CTCCHATFORM"].strChatData.value = chatXML;		
		document.forms["CTCCHATFORM"].action = aimsChatURL;
		document.forms["CTCCHATFORM"].target = chatWindowName;
		document.forms["CTCCHATFORM"].method = "POST"
		document.forms["CTCCHATFORM"].submit();
}
}

function SelectedC2CCenter()
{
    strServiceProduct = document.getElementById("ddlSelectService")[document.getElementById("ddlSelectService").selectedIndex].value;
    strCategory = document.getElementById("ddlCategory")[document.getElementById("ddlCategory").selectedIndex].value;
    strSubCategory = document.getElementById("ddlSubCategory")[document.getElementById("ddlSubCategory").selectedIndex].value;
    strState = document.getElementById("cboState")[document.getElementById("cboState").selectedIndex].value;
    
    if(strServiceProduct == "Phone Service")
    {
        if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GENERAL QUESTIONS ABOUT YOUR BILL")//eCare (eCenter) C2C
	    {
		    strCategoryId = "82";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "VIEW OR PAY YOUR CURRENT BILL ONLINE")//eCare (eCenter) C2C
	    {
		    strCategoryId = "83";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "CHECK STATUS OF A PENDING ORDER")//eCare (eCenter) C2C
	    {	
		    strCategoryId = "84";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "PRODUCT INFORMATION" && strSubCategory.toUpperCase() == "CHECK SERVICE AVAILABILITY")//eCare (eCenter) C2C
	    { 
		    strCategoryId = "85";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "PRODUCT INFORMATION" && strSubCategory.toUpperCase() == "GENERAL INQUIRY")//eCare (eCenter) C2C
	    { 
		    strCategoryId = "86";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "VERIZON WEB SITE" && strSubCategory.toUpperCase() == "SIGN-IN/PASSWORD PROBLEMS")//eCare (eCenter) C2C
	    {
		    strCategoryId = "87";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "VERIZON WEB SITE" && strSubCategory.toUpperCase() == "REGISTRATION")//eCare (eCenter) C2C
	    {
		    strCategoryId = "88";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "MAKE PAYMENT ARRANGEMENTS")//RMCC
	    {
		    strCategoryId = "115";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH AN OVERDUE BILL")//RMCC
	    {
		    strCategoryId = "116";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH A FINAL BILL")//RMCC
	    {
		    strCategoryId = "117";
		    chatChannel = "RMCC";
	    }
    }
    
    if(strServiceProduct == "Internet Service")
    {
        if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GENERAL QUESTIONS ABOUT YOUR BILL")//eCare (eCenter) C2C
	    {
		    strCategoryId = "89";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "VIEW OR PAY YOUR CURRENT BILL ONLINE")//eCare (eCenter) C2C
	    {
		    strCategoryId = "90";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "CHECK STATUS OF A PENDING ORDER")//eCare (eCenter) C2C
	    {
		    strCategoryId = "91";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "GET TECHNICAL SUPPORT")//VOL Center
	    {
		    strCategoryId = "";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "HIGH SPEED INTERNET TECH SUPPORT (CALL/CHAT ONLY)")//VOL Center - Added on 16/04/2009 for WER 2009040802
	    {
		    strCategoryId = "";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "PRODUCT INFORMATION" && strSubCategory.toUpperCase() == "CHECK SERVICE AVAILABILITY")//eCare (eCenter) C2C
	    {
		    strCategoryId = "92";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "PRODUCT INFORMATION" && strSubCategory.toUpperCase() == "GENERAL INQUIRY")//eCare (eCenter) C2C
	    {
		    strCategoryId = "93";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "MAKE PAYMENT ARRANGEMENTS")//RMCC
	    {
		    strCategoryId = "118";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH AN OVERDUE BILL")//RMCC
	    {
		    strCategoryId = "119";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH A FINAL BILL")//RMCC
	    {
		    strCategoryId = "120";
		    chatChannel = "RMCC";
	    }
    }
    
    if(strServiceProduct == "TV or Entertainment")
    {
        if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GENERAL QUESTIONS ABOUT YOUR BILL")//eCare (eCenter) C2C
	    {
		    strCategoryId = "94";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "VIEW OR PAY YOUR CURRENT BILL ONLINE")//eCare (eCenter) C2C
	    {
		    strCategoryId = "95";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "CHECK STATUS OF A PENDING ORDER")//FSC
	    {
		    strCategoryId = "113";
		    chatChannel = "FSC";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "REPORT A REPAIR OR INSTALLATION ISSUE")//FSC
	    {
		    strCategoryId = "112";
		    chatChannel = "FSC";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "GET TECHNICAL SUPPORT")//FSC
	    {
		    strCategoryId = "111";
		    chatChannel = "FSC";
	    }
	    if (strCategory.toUpperCase() == "SUPPORT" && strSubCategory.toUpperCase() == "FIOS TECH SUPPORT (CALL/CHAT ONLY)")//FSC - Added on 16/04/2009 for WER 2009040802
	    {
		    strCategoryId = "111";
		    chatChannel = "FSC";
	    }
	    if (strCategory.toUpperCase() == "PRODUCT INFORMATION" && strSubCategory.toUpperCase() == "CHECK SERVICE AVAILABILITY")//eCare (eCenter) C2C
	    {
		    strCategoryId = "96";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "PRODUCT INFORMATION" && strSubCategory.toUpperCase() == "GENERAL INQUIRY")//eCare (eCenter) C2C
	    {
		    strCategoryId = "97";
		    chatChannel = "eCenter";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "MAKE PAYMENT ARRANGEMENTS")//RMCC
	    {
		    strCategoryId = "121";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH AN OVERDUE BILL")//RMCC
	    {
		    strCategoryId = "122";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH A FINAL BILL")//RMCC
	    {
		    strCategoryId = "123";
		    chatChannel = "RMCC";
	    }
    }
    
    if(strServiceProduct == "Other")
    {
        if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "MAKE PAYMENT ARRANGEMENTS")//RMCC
	    {
		    strCategoryId = "124";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH AN OVERDUE BILL")//RMCC
	    {
		    strCategoryId = "125";
		    chatChannel = "RMCC";
	    }
	    if (strCategory.toUpperCase() == "BILLING" && strSubCategory.toUpperCase() == "GET HELP WITH A FINAL BILL")//RMCC
	    {
		    strCategoryId = "126";
		    chatChannel = "RMCC";
	    }
	}
	
	if(chatChannel == "eCenter")
	{
        appId = "2";
    }
    else if(chatChannel == "RMCC")
    {
        appId = "12";
    }
    else if(chatChannel == "FSC")
    {
        appId = "8";
    }
}
