
//http://www.trap17.com/index.php/Counting-Characters-Textarea_t23614.html

//#########################

//#########################


/*
var obj = null;
 
    function checkHover() {
      if (obj) {
         obj.find('ul').fadeOut('fast');
      } //if
   } //checkHover

   $(document).ready(function() {
      $('#Nav > li').hover(function() {
         if (obj) {
            obj.find('ul').fadeOut('fast');
           obj = null;
         } //if

        // $(this).find('ul').fadeIn('fast');
	
		$(this).find('ul').css("display", "block");
      }, function() {
         obj = $(this);
         setTimeout(
            "checkHover()",
            400);
      });
   });
   
//#############
 <ul id="Nav">
      <li>
         Test 1
         <ul class="Menu" style="display:none;">
            <li>Sub element 1</li>
            <li>Sub element 2</li>
         </ul>
      </li>
   </ul>
 
*/


/*
//might not use
(function($) {
	$.fn.showMenu = function(options) {
		var opts = $.extend({}, $.fn.showMenu.defaults, options);
		//$(this).bind("contextmenu",function(e){//right click});


		$(this).bind("mouseover",function(e){
//alert($(this).attr("class"));
			//if( $(this).attr("class") == "subdesc" || $(this).attr("class") == "subdesc2" ){
			$(opts.query).show().css({
				top:e.pageY+"px",
				left:e.pageX+"px",
				position:"absolute",
				opacity: opts.opacity,
				zIndex: opts.zindex
			});
			//}
			return false;
		});

		
		$(document).bind("click",function(e){
			$(opts.query).hide();
		});

	};
	
	$.fn.showMenu.defaults = {
		zindex: 2000,
		query: document,
		opacity: 1.0
	};
})(jQuery);
*/

/*
window.focus:
FF:Options/Content/Advanced/Raise or lower windows(check)

/*
while(typeof (k=window.open(url,"name",params) != "object")
alert("popUp Blocked")
*/




/*
function newExcitingAlerts() {
    var oldTitle = document.title;
    var msg = "New! Hello.";
    var timeoutId = setInterval(function() {
        document.title = document.title == msg ? ' ' : msg;
    }, 500);
    window.onmousemove = function() {
        clearInterval(timeoutId);
        document.title = oldTitle;
        window.onmousemove = null;
    };
}
*/





/*
$(document).ready(function()
{
   //ajaxTime.php is called every second to get time from server
   var refreshId = setInterval(function()
   {
     $('#timeval').load('ajaxTime.php?randval='+ Math.random());
   }, 1000);
   //1000 millisecond = 1 sec.

   //stop the clock when this button is clicked
   $("#stop").click(function()
   {
     clearInterval(refreshId);
   });
});

*/
/*


<noscript>
<p class="alert">
Please enable JavaScript for full functionality.
</p>
</noscript>

<script>
// JS object
var myData = {id1:'US', id2:'Europe', id3:'Antarctica'};

$("div.clickme").click(function() {
   var id = this.id;  // 'id1' or 'id2' or 'id3'
   alert(myData.id);  // 'US' or 'Europe' or 'Antarctica'
   // alert(myData[id]);  // same as above
});
</script>

<div id="id1" class="clickme">Text 1</div>
<div id="id2" class="clickme">Text 2</div>
<div id="id3" class="clickme">Text 3</div> 

*/
//###########################


function submitData(loc, id){

		var clientmsg = $(id).val();
		clientmsg.replace(/^\s+|\s+$/, '');
		/*
		if( clientmsg == "" ){
			//alert("empty");
			return false;
		}
		*/

//alert(loc + "###txt=" + clientmsg);
		$.post(loc, {text: clientmsg});
//$(id).load(loc);

//alert("done");			
		//$(id).attr("value", "");
//return true;
}

function submitChat(id){

//alert("usermsg=" + $("#usermsg").val());

/*
var fld = document.usermsgform.usermsg;
var fldvalue = fld.value;
alert(fldvalue);
*/

//id = "#" + id;
var loc = getKDIM() + '&w=2' + "&chat=s";
// + "&zt=" + $("#zt").val();
//alert(loc);
submitData(loc, id);

//alert("x");




		$(id).attr("value", "");
/*
		//$(id).empty();


		var x = $(id).val();
//alert(x.length);
var re = new RegExp("\n","g");
		x = x.replace("re", "");
		//x = $.trim(x);
		$(id).attr("value", x);
*/
		$(id).focus();

//return true;

}

/*
// jQuery Document
$(document).ready(function(){
//alert("rqt");
	//If user submits the form
	$("#submitmsg").click(function(){	
		var clientmsg = $("#usermsg").val();
		$.post("im_post.php", {text: clientmsg});				
		$("#usermsg").attr("value", "");
		return false;
	});
});
*/

//alert("y");


function chatboxKeyPress(evt){
//alert("xx");
  var keycode;  
 keycode = (window.event)?event.keyCode:evt.which;  
  //alert(keycode);  
  //return true;

	var formid="usermsg";
	var fld = document.getElementById(formid);

	var id = "#" + formid;

//alert("key=" + keycode);	
	if( keycode == 13 ){
//alert("done");
	
		submitChat(id);
		//false: to return cursor to the first line.
		return false;
//alert(fld.name + "=" + fld.value);
		//fld.value = "";
	}
	else{
		if( keycode != 8 ){
			//not a backspace
			//var len = fld.value.length;
			//alert(len);

		}

//alert(fld.value + "=" + fld.value.length);

	}

	//initUsermsg(id);	
	return true;
}





function imHandler(w, v){
	
	var loc = getKDIM();
	if( (w != "") && (v != "") ){
		loc = loc + "&w=" + w + "&imhv=" + v;
	$.get(loc);
//alert(loc);
	}


}



function alertInfo(obj){

	//$(obj).css("background-color", "EDF434");
	//$(obj).css("color", "333333");
	

	$(obj).slideDown("fast");
    //$(obj).fadeOut(800).fadeIn(800).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);

}


function linkHL(id, color){
//alert($(this));

//alert(id);
id = "#" + id;
$(id).css("text-decoration", "none");
$(id).css("background-color", color);


//$(this).css("color","#FFF278");
     //$(this)
         //Fade to the new color
        //$(this).animate({backgroundColor:"#fff278"}, 6000);
         //Fade back to original color
         //$(this).animate({backgroundColor:originalBG},750) 
		 
//return true;
}

/*
$(document).ready(function(){
$("#nojs").hide();
//$("#enabledjs").show();
});
*/





//###############################

// jQuery Document

$(document).ready(function(){
//alert("exit");
	//If user wants to end session
	
	//go away
   $(".close").click(function(){
        $("#info").animate({left:"+=10px"}).animate({left:"-5000px"});
    });
	
//alert("exit");
	//If user wants to end session
	$("#exit").click(function(){
		var exit = confirm("Are you sure you want to end the session?");
		if(exit==true){
		//alert("x");
		window.location = 'c2.php?logout=true';}		
	});


	
	$("div.clickme").click(function(){
/*
var myData = new Array();
myData[0] = "Saab";
myData[1] = "Volvo";
myData[2] = "BMW";
for (i=0;i<myData.length;i++)
{
document.write(myData[i] + "<br />");
}
*/
//var myData = {id1:"1:US", id2:"2:Europe", id3:"3:Antarctica"};
/*	
	alert("x");
   var id = this.id;  // id1 or id2 or id3
   alert(this.id);  // US or Europe or Antarctica
   //alert(myData[id]);  // same as above
*/	
	});
});




//############################


$(document).ready(function(){


	$('#submitmsg').click(function(){
		submitChat('#usermsg');
		return false;
	});

	
	$('a#clearchat').click(function(){
		clearchat();
	});

	
	$('a#smallwinchat').click(function(){
	//alert($(this).attr('href'));
	$(this).css("text-decoration", "none");
		smallwin($(this).attr('href'), 300,460, $(this).attr('target'));
		//myPopup($(this).attr('href'));
		return false;
	});

	
});


function clearchat(){

	var tmp = getKDIM();
	//alert(tmp + "&w=2&chat=c");
	$.get(tmp + "&w=2&chat=c");
	
	return true;

}


//###########################
	
	function loadJobs(){
	
loadIM();
loadChatrecent();


/*
	$('#fadeinbox').load('test.php?what=xnew');
displayfadeinbox();
*/

/*
var screenw = window.screen.width;
	$('#topbar').load('test.php?text=xnew' + screenw);
	staticbar();

*/

	}
	
	//setInterval (loadJobs, 12500);
setInterval (loadJobs, 2000);
	
/*
Every second:-
	setInterval (loadAlert, 12500);
1253079891|2009/09/16 01:44:51
1253079892|2009/09/16 01:44:52
1253079893|2009/09/16 01:44:53
1253079894|2009/09/16 01:44:54
1253079895|2009/09/16 01:44:55
*/

function getKDIM(){
//alert("h");
var dt = new Date();
var tz = dt.getTimezoneOffset();
var aid = $('#aid').attr("value");;
//var aid = $('#aid').val();
var g = $('#g').val();
var x = "/q";
//var x = "/index.php";
//aid = x + '?p=im&aid=' + aid + "&ugmt=" +  getTzOffset() + "&g=" + g;
aid = x + '?p=im&aid=' + aid + "&ugmt=" +  getTzOffset() + '&zt=' + $('#zt').val();
//alert(aid);
return aid;
}


function loadChatrecent(){
var loc = getKDIM();

//###################
var chatr='#chatrecent';
var chatrtmp;
var chatralert = '#chatnewmsgalert';
var chatnewdata = '#chatnewmsgdata';
//if( exists(chatr) && exists(chatnewdata) && (! exists("#usermsg")) ){
if( exists(chatr) && exists(chatnewdata) ){
//alert("exists");
//alert($(chatr).text());
//$(chatr).load(loc + '&w=chatrecent');

//x='#chatnewmsgalert';
$(chatnewdata).load(loc + '&w=ckNumoffmsg');
chatrtmp = $.trim($(chatnewdata).html());

	if( chatrtmp.length > 0 ){
//alert("y=" + $("#chatnewmsg").html());
	$(chatralert).html(chatrtmp);
		alertInfo(chatralert);
//newExcitingAlerts();

	}
	else{
		//$(x).hide("fast");
		$(chatralert).slideUp("fast");
		$(chatralert).html(chatrtmp);

	}

}
//#########

}





function loadIM(){

//newExcitingAlerts();
//window.focus();




//alert("x");
//alert($('#ckchatbox').val());
var loc = getKDIM();
//$.get(loc);

var x;
var tmp;


if( $('#ckim').val() == "1" ){
//$('#im').load(loc + '&w=0' + '&imkidset=T');
x = $('#im').text().replace(/(NEW MESSAGE:)/gi, "\n\nNEW MESSAGE:\n");

$('#imalert').text(x);
var msg = $('#imalert').text();
//alert("msg=" + msg);
if( msg != "" ){
	//window.focus();
	alert(msg);
	$('#imalert').data("");
	//alert( "after=" + $('#imalert').text());
}

//$('#im').display("hide");
//$('#im').attr("value", "");
	if( x != "" ){
		//x.replace(/SUBJ/g, "hh");
		//alert(x);
		//window.focus();
	}
//var id="#t1";
//var show;
//slideR(id, show);
}



var ol='#online';
var oltmp;
if( exists(ol) ){
//alert(x);
//$('#online').load(loc + '&w=1');
//IE got leading spacing.
$('#tmp').load(loc + '&w=1');
oltmp = $.trim($('#tmp').html());
if( isEmpty(oltmp) ){
	oltmp = 0;
}
$(ol).html(oltmp);
//alert("onDone:" + loc);
//alert("onDone2:" + $('#online').html());
}





//##################

var locChat = loc + '&w=2';

var chatst='#chatboxstatus';
var chatsttmp;
if( exists(chatst) ){
//alert(x);
var html = loadChat(locChat, '#chatbox');
chatsttmp = loc + '&w=chatboxstatus';
$(chatst).load(chatsttmp);
}




var umsg='#usermsg';
var umsgtmp;
if( exists(umsg) ){
//alert(x);
umsgtmp = locChat + "&chat=e" + "&usermsglen=" + imtyping();
$.get(umsgtmp);
$(umsg).focus();
//alert(tmp);
}


var chaterr='#ckchatsenderror';
var chaterrtmp;
if( exists(chaterr) ){
chaterrtmp = locChat + "&chat=s" + "&chatsenderror=1";
$("#chatsenderror").load(chaterrtmp);
}





}//end:loadIM()




function imtyping(){
/*
if( $('#ckusermsg').val() != "1" ){
	return 0;
}
*/
	var id = "#" + "usermsg";
	var v = $(id).val();
	
	v = v.replace(/^\s+|\s+$/, '');

//alert(v + "=" + v.length);
	return v.length;

/*		
//alert("x" + id);
//var x = document.getElementById("usermsg");
var loc = getKDIM();
loc = loc + '&w=imtyping' + '&zt=' + $('#zt').val() + '&num=' + v;
$.get(loc);
*/
/*
//alert(loc);
id = "#" + id;
submitData(loc, id);


//$.post(loc);
//alert(loc);
*/
}


	
function loadChat(url, id){
//alert("imid=" + $('#imid').val());
//alert("chatbox=" + $('#chatbox').text());

/*
if( $('#ckchatbox').val() != "1" ){
return;
}
*/

		
		
		var y = 200000;
		var oldscrollHeight = $(id).attr("scrollHeight") - y; //Scroll height before the request
		
		//var na = "/ui/1/readmsg/f/" + $('#imid').val();
		var loc = url + '&chat=r';
		//&zt=' + $('#zt').val();
//if( id == "#t1" )
//alert("id=" + id + "loc=" + loc);
	
		$('#tmp').load(loc + '&ck=clear');
		var isClear = $.trim($('#tmp').text());
		//alert("c=" + isClear);
		if( isClear == "T" ){
			$(id).empty();
			$(id).append('<br /><br /><br /><br /><br />');
		}
	
	
		var str;
		var x;
		
		$.ajax({
			url: loc,
			cache: false,
			success: function(html){		
				//$(id).html(html); //Insert chat log into the #chatbox div
				
				$('#tmp').html(html);
				str = $.trim($('#tmp').html());
//alert(str);
				x = str.substr(0,1);
				html = str.substr(1);
//alert(x + "=" + html);
				if( x == "T" ){
//alert(x);
					window.focus();
				}
				
				$(id).append(html);
					
//document.getElementById("styled_popup").style.display="block";
/*				
				//Auto-scroll			
				var newscrollHeight = $("#chatbox").attr("scrollHeight") - y; //Scroll height after the request
				if(newscrollHeight > oldscrollHeight){
					$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div

$("#bottom").focus();
//window.scrollBy(0,y);
self.scrollBy(0,y);
				}
*/
//alert("done=");
//alert("x" + $("#chatbox2").text());

//$("#chatbox").append("oook<br />");
//$("#chatbox").html($("#chatbox2").html());
//$("#chatbox").fadIn("slow");
//$("#bottom").focus();

//alert($('#t1').html());
self.scrollBy(0,y);
		
		  	}
		});

//var x = $(id).html();
/*
if( $(id).html() != "" ){
//setTimeout('window.focus()', 10000);
		//window.focus() not working on FF
		//window.focus();
		//alert("mes");
}
*/
	
return $(id).html();
}

	//setInterval(loadLog, 12500);
	
/*	
 $(document).ready(function(x){
    
    $("#showr").click(function () {
loc = getKDIM();
loadChat(loc + '&w=2');

      $("#t1" + ":eq(0)").show("fast", function () {
        // use callee so don't have to name the function
        $(this).next().show("fast", arguments.callee); 
      });
    });
    $("#hidr").click(function () {
      $("#t1").hide(2000);
    });

  });
*/

function slideR(id, what){

//alert(what);
//alert(id);
//id = '#' + id;
if( what == "show" ){
var loc = getKDIM();
var html = loadChat(loc + '&w=2', id);
//$(id).html(html);


	$(id).show("fast");
	//$(id).fadeIn("slow");
	//$(id).slideToggle("slow")
}
 
if( what == "hide" ){
     $(id).hide(2000);
	 //$(id).fadeOut("slow");
	 //$(id).slideToggle("slow")
}

}

