$(document).ready(function() {	
	getChat();	
	$('.chatnow').click(function(){
		var datachat = $('select[name="cscrepresentative"]').val().split(',');
		if (datachat != 0 ) {
			openWindowChat(datachat[0],datachat[1],datachat[2]);
		} else {
			getChat();
		}
		return false;
	});	
});
function getChat(){
	
	var currentTime = new Date()
	var userID=$('#userID').html() 
	
	$.ajax({
		//url: 'http://192.168.21.143:8084/IBCChat/Rosters?callback=?',
		url: 'http://chat.ibcjapan.co.jp:8080/IBCChat/Rosters?callback=?',
		dataType: "jsonp",
		data: "&uid=" + userID + "&site=IBC&type=3",
		cache: false,
		success: function(data){		
			var cscSTRFav=""; 
			var cscSTR=''; 
				var cscSTRCnt=0;
				var cscSTRCntFav=0;
			
			var russiaSTRFAV="";				
			var russiaSTR='';
				var russiaSTRCnt=0;
				var russiaSTRCntFav=0;
				
			var spanishSTRFAV="";					
			var spanishSTR='';
				var spanishSTRCnt=0;
				var spanishSTRCntFav=0;
			
			var htmls = '';
			var htmlsNZ = '';
			var spanishHTMLS='';
			var chatNumber=0;
			var NZcounter=0;
			var chatNumberSpanish=0;

			$.each(data.ROSTERS.ENTRY, function(index,entry){
				if (entry['Groups'] != ""){
					chatNumber++;
					$.each (entry['Groups'], function(entryindex,dataentry){
						//CSC
						if (dataentry == '808002'){
							htmls += '<option value="'+ entry['JID'] +','+ entry['NAME'] +','+ dataentry +'"> - '+ entry['NAME']  +' - </option>';
							//chatNumber++;
						}
						//New Zealand
						if (dataentry == '808033'){
							htmlsNZ += '<option value="'+ entry['JID'] +','+ entry['NAME'] +','+ dataentry +'"> - '+ entry['NAME']  +'  (NZ) - </option>';
							//chatNumber++;
							NZcounter++;
						}						
						//SPANISH
						if (dataentry == '808021'){
							spanishHTMLS += '<option value="'+ entry['JID'] +','+ entry['NAME'] +','+ dataentry +'"> - '+ entry['NAME']  +' - </option>';
							chatNumberSpanish++;
						}
					});
				}				
			});
			
			var optLABEL="";
				optLABEL+="<OPTGROUP LABEL='Customer Representatives'>";
				optLABEL+=htmls;
				optLABEL+="</OPTGROUP>";
					 
			
			if(NZcounter>0){
				optLABEL+="<OPTGROUP LABEL='Advantage NZ'>";
				optLABEL+=htmlsNZ;
				optLABEL+="</OPTGROUP>";
			}
			
			var finalOptions='';
			
			if(getCookie('preferredLanguage') == 'eng') {
				finalOptions+= optLABEL;
			}else if(getCookie('preferredLanguage') == 'spa') {
				
				var optLABELSP='';
				optLABELSP="<OPTGROUP LABEL='Spanish Representatives'>";
				optLABELSP+=spanishHTMLS;
				optLABELSP+="</OPTGROUP>";
				
				finalOptions+= spanishHTMLS;
				chatNumber= chatNumberSpanish;
			}else{
				finalOptions+= optLABEL;
			}
			
			if (chatNumber==0){
				var el=$('select[name="cscrepresentative"]');
				el.attr('disabled', true);
				finalOptions="No Representatives Online";
				
				//$('select[name="cscrepresentative"]').val(1);
				$('select[name="cscrepresentative"] option:selected').text(finalOptions);
				try{
					$('#').html("" + (chatNumber-1) + "");
				}catch(e){
					
				}
				
			}else{
				$('select[name="cscrepresentative"]').html("");
				$('select[name="cscrepresentative"]').append(finalOptions);
	
				try{
					$('#onlineNum').html("" + (chatNumber -1)+ "");
				}catch(e){
					
				}
			
			}	
			callChat();
		}
	});	
}

function callChat(){
	setTimeout(function(){getChat()},60000);
}

function servletURL(){
	 this.popUpUrl = "chat.ibcjapan.co.jp";
	 this.folderPath = "IBCChat/";
	 this.serverPort = ":8080";
	 
	 //FOR ENGLISH MOTOHOUND
	 
	 this.CSCNewZealandGroupId = "808035";
	 this.CSCGroupId = "808002"; 
	 
	 
	 this.IDIRECTTRANSGroupId = "808030";
	 this.ACCOUNTSGroupId = "808003";
	 this.SHIPPINGSGroupId = "808028";
	 this.PARTSGroupId = "808029";
	 this.PURCHASEGroupId = "808032";
	 
	 //FOR RUSSIA
	 this.RUSSIAGroupId="808020";
	 
	 //FOR SPANISH
	 this.SPANISHGroupId="808021";
	 
	 //FOR JAPANESE
	 this.JAPANDomesticGroupId="808026";
}

function openWindowChat(jid,jname,deptId){
	var servleturl = new servletURL();
	try{
		var url = 'http://' + servleturl.popUpUrl + servleturl.serverPort +'/'+servleturl.folderPath+'index.jsp?uacc='+jid+'&un='+jname+'&deptId='+deptId;
		var new_window = window.open(url,'CHAT_IBC','location=1,width=450,height=385');
		if (window.focus) {
			new_window.focus();
		}
	}catch(e){
			alert("Exception thrown on Opening New Window : " + e.description);
	}
}

