var refreshRate= 5000;
var chatStatus= 'closed';

$(document).ready(function () {	
	addChatDom();
	update_me();
	checkWindowClose();
	checkStart();
	checkChatForm();
});

function addChatDom(){
	$('body').append("<div style='display:none;'> <div id='chat-box' style='padding:10px;'></div> </div>");
}

function update_me(setStatus) {
	var page= document.URL; 
	$.get(royalChatPath+'/visitors/update_visitor.php', { action:'ajax', page:page, setStatus:setStatus, uniqueId: getUniqueId() }, function(results){											   
		eval (results);
		checkStart();
	});

	timer= setTimeout ('update_me()', refreshRate);
}

function checkWindowClose() {
	onLink= false; userStatus='';
	
	$('a').click( function() {onLink=true; } );						
							
	$(window).bind('beforeunload', function(e) {
											
		message = 'One of our agents would like to chat with you to answer any questions that you might have. Please click on "Cancel" to chat now, or "OK" if you would rather not chat with an agent at this time.';
		if (userStatus == 'picked' && onLink == false) {
			openChat();
			
			if (e){  // For IE and Firefox
				e.returnValue = message;
			}
			return message;  // For Safari
		}
	});
	
}

function checkStart() {
	if (userStatus == 'started' && chatStatus == 'closed') {
		chatStatus= 'open';
		openChat();
	}
	
}

function openChat() {
	$('#chat-box').html("<iframe src='"+ royalChatPath + "/chat/index.php?session=" +session +"' frameborder='0' border='0' cellspacing='0' width='100%' height='440' marginwidth='0' marginheight='0' style='height:440px'></iframe>");
	$.fn.colorbox({inline: true, href:"#chat-box", opacity:0.7, overlayClose: false, open: true, width: "580px", height: "490px", onComplete: function(){ changeStatus('started');  }, onCleanup: function(){changeStatus('closed'); } });
}

function changeStatus(status) {
	clearTimeout(timer); 
	chatStatus= status;
	userStatus= 'none';
	update_me(status); 
	return false; 	
}

function getUniqueId() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 12;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function checkChatForm() {
	$('#chat-form').submit( function() {
 		var name= $('#user-name').val();
		var mail= $('#user-mail').val();
		var phone= $('#user-phone').val();
		
		var message='';
		if (name=='') {message= 'Please enter your name\n'; }
		if (mail=='') {message+= 'Please enter your email address\n'; }
		else { 
			var pattern= /^.+@.+\..+$/; 
			var validMail= pattern.test(mail); 
			if (!validMail) {message+= 'Please enter a valid email address\n'; }
		}
		if (phone=='') {message+= 'Please enter your phone number\n'; }
			
		if (message != '') {alert (message); }
		else { window.open(royalChatPath + '/chat/start-chat.php?name='+ name +'&mail='+ mail +'&phone='+ phone , 'chat_Window', 'width=555,height=450,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no,menubar=no'); }
		return false;
	});
}