/*
*
*	Apricot JavaScript v1.0a
*	Author: Jarques Pretorius
*	Site: 07designs.com
*
*/
	$uname = 0;
	$em = 0;
	$rm = 0;
	
	function newAccount(){
		$("#loading").fadeIn();
		$username = document.getElementById("username").value;
		$email = document.getElementById("email").value;
		$room = document.getElementById("rname").value;
		$check = $uname + $em + $rm;
		if ($check == 3) {
			$.ajax({
			   type: "POST",
			   url: "backend/register.php",
			   data: "uname="+$username+"&email="+$email+"&room="+$room,
			   success: function(msg){
					//On Successful registration, send user to the login page with a successful registration notice
			   		document.location = 'login.php?reg=true';
			   }
			 });	
		} else {
			alert("Please check that you filled out the required fields.");
			$("#loading").fadeOut();
		}
	}
	
	function sendMes($user, $room){
		$user = $user;
		$room = $room;
		$txt = document.getElementById('send').value;
		$url = 'chats/rooms/'+$room+'/'+$room+'.html';
		/*if ($txt == "") {
			
		} else {	
			$("#emp").load("backend/send.php?user="+$user+"&room="+$room+"&txt="+$txt)
		}*/
		$.ajax({
		   type: "POST",
		   url: "backend/send.php",
		   data: "user="+$user+"&room="+$room+"&text="+$txt,
		   success: function(msg){
		     $('#chatbox').load($url);
		   }
		 });
		document.getElementById('send').value = "";
		$("#chatbox").animate({bottom: "0px"});
		$charLimit = 140;
		$("#charcount").text($charLimit);
	}
	
	function checkName(){
		$username = document.getElementById("username").value;
		$("#checkusername").load("backend/checkuser.php?user="+$username);
		if ($username == "") {
			$uname = 0;
			$("#uname").html("<img src='img/cross.png'/>");
		} else {
			//$uname = 1;
			//$("#uname").html("<img src='img/tick.png'/>");
			$("#checkusername").load("backend/checkuser.php?user="+$username);
			$("#uname").html("<img src='img/loading.gif'/>");
			var user = setTimeout("doUser()",2000);
		}
	}
	
	function doUser(){
		$check = $("#checkusername").html();
		if ($check == "fail") {
			$("#uname").html("<img src='img/cross.png'/>");
		} else {
			$uname = 1;
			$("#uname").html("<img src='img/tick.png'/>");
		}
	}
	
	function checkRName(){
		$rname = document.getElementById("rname").value;
		if ($rname == "") {
			$rm = 0;
			$("#room").html("<img src='img/cross.png'/>");
		} else {
			$rm = 1;
			$("#room").html("<img src='img/tick.png'/>");
		}
	}
	
	function checkEmail(){
		$email = document.getElementById("email").value;
		echeck($email);
		if ($ch == 1) {
			$em = 1;
			$("#em").html("<img src='img/tick.png'/>");
		} else {
			$em = 0;
			$("#em").html("<img src='img/cross.png'/>");
		}
	}
	
	function echeck(str) {
			var at="@"
			var dot="."
			var lat=str.indexOf(at)
			var lstr=str.length
			var ldot=str.indexOf(dot)
			if (str.indexOf(at)==-1){
			   $ch = 0;
			   return false
			}
			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			   $ch = 0;
			   return false
			}
			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			    $ch = 0;
			    return false
			}
			if (str.indexOf(at,(lat+1))!=-1){
			    $ch = 0;
			    return false
			}
			if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			    $ch = 0;
			    return false
			}
			if (str.indexOf(dot,(lat+2))==-1){
			    $ch = 0;
			    return false
		    }
			 if (str.indexOf(" ")!=-1){
			    $ch = 0;
			    return false
			 }
			 $ch = 1;
	 		 return true					
		}
		
		//Changes font color on text box when in focus
		function onTxt(){
			var chDiv = document.getElementById('send').value;
			if (chDiv == "enter your message here") {	
				$("#send").css({color: "#414141"});
				document.getElementById('send').value = "";
			}
		}
		function offTxt(){
			var chDiv = document.getElementById('send').value;
			if (chDiv == "") {
				$("#send").css({color: "#959595"});
				document.getElementById('send').value = "enter your message here";
			}
		}
		
		//140 Character Limit
		$charLimit = 140;
		
		function getCount(){
			if ($charlimit > 0) {
			$("#charcount").text($charLimit);
			$charLimit--;
			}
		}
		
		function limitText(limitField, limitCount, limitNum) {
			if (limitField.value.length > limitNum) {
				limitField.value = limitField.value.substring(0, limitNum);
			} else {
				limitCount.value = limitNum - limitField.value.length;
			}
		}
		
		//Load History 
		function loadHistory($id) {
			$("#rooms:hidden").slideDown("fast");
			$("#acset:visible").slideUp("fast");
			$("#acsetcl:visible").slideUp("fast");
			$("#mescent:visible").slideUp("fast");
			$("#chatset:visible").slideUp("fast");
			$("#chatsetcl:visible").slideUp("fast");
			$id = $id;
			$url = 'chats/rooms/'+$id+'/'+$id+'.html';
			$("#history").load($url);
			$("#history").fadeIn("fast");
			$("#hisop").fadeIn("fast");
		}
		
		function closeHistory(){
			$('html,body').animate({scrollTop: 0}, 100);
			$("#history").fadeOut();
			$("#hisop").fadeOut();
		}
		
		//Account Settings
		function openAccount($theid){
			$id = $theid;
			$("#rooms:hidden").slideDown("fast");
			$("#history:visible").slideUp();
			$("#hisop:visible").slideUp();
			$("#mescent:visible").slideUp("fast");
			$("#chatset:visible").slideUp("fast");
			$("#chatsetcl:visible").slideUp("fast");
			$("#acset").fadeIn("fast");
			$("#acsetcl").fadeIn("fast");
			$("#acset").load("includes/accountsettings.php?userid="+$id);
		}
		
		function closeAcSettings(){
			$('html,body').animate({scrollTop: 0}, 100);
			$("#acset").fadeOut("fast");
			$("#acsetcl").fadeOut("fast");
		}
		
		//Load Message Center
		function loadMes($theid) {
			$id = $theid;
			$("#history:visible").slideUp("fast");
			$("#hisop:visible").slideUp("fast");
			$("#acset:visible").slideUp("fast");
			$("#acsetcl:visible").slideUp("fast");
			$("#chatset:visible").slideUp("fast");
			$("#chatsetcl:visible").slideUp("fast");
			$("#rooms").slideUp("fast");
			$("#mescent").fadeIn("fast");
			$("#mescent").load("includes/messages.php?userid="+$id);
		}
		
		function closeMes(){
			$("#mescent").slideUp("fast");
			$("#rooms:hidden").slideDown("fast");
		}
		
		//Chat Room Settings
		function openChatSettings($theid) {
			$id = $theid;
			$("#rooms:hidden").slideDown("fast");
			$("#history:visible").slideUp();
			$("#hisop:visible").slideUp();
			$("#mescent:visible").slideUp("fast");
			$("#acset:visible").slideUp("fast");
			$("#acsetcl:visible").slideUp("fast");
			$("#chatset").slideDown("fast");
			$("#chatsetcl").fadeIn("fast");
			$("#chatset").load("includes/chatsettings.php?roomid="+$id);
		}
		
		function closeChatSettings(){
			$("#chatset:visible").slideUp("fast");
			$("#chatsetcl:visible").slideUp("fast");
		}
		
		// Send PM
		function sendPm($theid){
			$("#confirmsend").fadeIn();
			$id = $theid;
			$user = document.getElementById('pmuser').value;
			$subject = document.getElementById('pmsubject').value;
			$body = document.getElementById('pmbody').value;
			$.ajax({
			   type: "POST",
			   url: "backend/sendpm.php",
			   data: "id="+$id+"&to="+$user+"&subject="+$subject+"&body="+$body,
			   success: function(msg){
			     	document.getElementById('pmuser').value = "";
					document.getElementById('pmsubject').value = "";
					document.getElementById('pmbody').value = "";
					$("#compose").slideUp();
					$("#confirmsend").fadeOut();
			   }
			 });
		}
		
		//Setup reply
		function replyPM($mes){
			$mes = $mes;
			document.getElementById('pmbody').value = $mes;
		}
		
		//Update Chat Settings
		function checkTitle(){
			$title = document.getElementById("roomtitle").value;
			if ($title == "") {
				alert("Title cannot be empty.");
			}
		}
		
		function submitChatUpdate($theid){
			$id = $theid;
			$title = document.getElementById("roomtitle").value;
			$description = document.getElementById("descrip").value;
			$pubpriv = $("input[@name=publicPrivate]:checked").val();
			$.ajax({
			   type: "POST",
			   url: "backend/updatechatsettings.php",
			   data: "roomid="+$id+"&title="+$title+"&descrip="+$description+"&pubpriv="+$pubpriv,
			   success: function(msg){
					$("#saved").fadeIn();
					$("#saved").fadeOut(10000);
			   }
			 });
		}