$(document).ready(function() {
/*
    $('body').on('hover','.toDoArea',function(){
	$(this).find(".toDoLink").show();
	},function(){
	$(this).find(".toDoLink").hide();
	});
*/
	$(".toDoArea").hover(function(){
	$(this).find(".toDoLink").show();
	},function(){
	$(this).find(".toDoLink").hide();
	});
	$('.mouseoverg').mouseover(function(){
		$(this).css("backgroundColor","#f7f5f5");
	}).mouseout(function(){
		$(this).css("backgroundColor","");
	});
	$('.mouseoveryellow').mouseover(function(){
		$(this).css("backgroundColor","#fffc00");
	}).mouseout(function(){
		$(this).css("backgroundColor","");
	});
	
	$(".topLinks").hover(function(){
$(this).css("backgroundColor","#fffc00","color","red");
	},function(){
$(this).css("backgroundColor","");
	});
	$('.textAlignmentCheck').keyup(function(){
	    $obj = $(this);
		var textVal = $obj.val().trim();
	    if(textVal.length != 0)
	    {
			var reg =  new RegExp("[\x00-\x80]+"); // is ascii
			if(reg.test(textVal.charAt(0)))
			{
				$obj.css("direction", "ltr");
			}
			else
			{
				$obj.css("direction", "rtl");
			}
		}
	});
	$("#updateSettings").click(function(){
		$("#updateStatus").html(" ");
		profileStatus = ($('#profileStatus').is(':checked')?1:0);
		allowToReceivEmail = ($('#allowToReceivEmail').is(':checked')?1:0);
		allowUserToComment = ($('#allowUserToComment').is(':checked')?1:0);
		emailNotificationMessage = ($('#emailNotificationMessage').is(':checked')?1:0);
		emailNotificationComment = 1;
		$.ajax({
			type : 'POST',
			url : '/users/useraction.php',
			dataType : 'json',
			data: {	rt : 12, profileStatus : profileStatus, allowToReceivEmail : allowToReceivEmail, allowUserToComment : allowUserToComment, emailNotificationMessage:emailNotificationMessage, emailNotificationComment:emailNotificationComment},
			success : function(data){
				if(data.result==1)
				{
					$("#updateStatus").html(data.txt);
				}
			}
		});
	});
 
	$(".hidenotificationMessageBox").click(function () {
	$('#notificationMessageBox').hide('slow');
	$('#curtain').hide('slow');
	});
/*
	$(".addComment").click(function () {
		userText =  $(this).closest("div").find("textarea").val();
		entryID = $(this).parents("div").parents("div").attr("toDoID");
		requestType = $(this).attr("requestType");
		userSelect(entryID,requestType,userText);
		userText = $(this).closest("div").find("textarea").val('');
		$(this).closest("div").hide("slow");
	});
        $('body').on('click','.removeComment',function(){
	entryID = $(this).closest("div").attr("entryID");
	requestType = $(this).attr("requestType");
	userSelect(entryID,requestType,'');
	boxname = $(this).closest("div").attr("id");
	$('#'+boxname).hide("slow");
	});
        $('.showComment').click(function(){
	boxname = $(this).attr('boxname');
	$('#'+boxname).show("slow");
	});
*/
	$('.hideLocalLoginOption').click(function(){
		$('#localLoginOption').hide();
		$('#openIDLoginOption').show();
	});
	$('.showLocalForgotPassword').click(function(){
		$('#localLogin').hide();
		$('#localForgostPassword').show();
		document.getElementById('passwordimg').src = '/captcha/securimage_show.php?sid=' + Math.random();
		$('#un').focus();
	});
	$('.showLocalRegister').click(function(){
		$('#localLogin').hide();
		$('#localRegister').show();
		document.getElementById('registerimage').src = '/captcha/securimage_show.php?sid=' + Math.random();
		$('#emailreg').focus();
	});
	$('.hideLocalForgotPassword').click(function(){
		$('#localForgostPassword').hide();
		$('#localLogin').show();
		$('#email').focus();
	});
	$('.hideLocalRegister').click(function(){
		$('#localRegister').hide();
		$('#localLogin').show();
		$('#email').focus();
	});
	$(".ckeckRegisterForm").focus(function(){
		id = $(this).attr('id');
		if(id=="emailreg")
		{
			$('#miniHelpInput').html("Help:
");
		profileStatus = ($('#profileStatus').is(':checked')?1:0);
		allowToReceivEmail = ($('#allowToReceivEmail').is(':checked')?1:0);
		allowUserToComment = ($('#allowUserToComment').is(':checked')?1:0);
		emailNotificationMessage = ($('#emailNotificationMessage').is(':checked')?1:0);
		emailNotificationComment = 1;
		$.ajax({
			type : 'POST',
			url : '/users/useraction.php',
			dataType : 'json',
			data: {	rt : 12, profileStatus : profileStatus, allowToReceivEmail : allowToReceivEmail, allowUserToComment : allowUserToComment, emailNotificationMessage:emailNotificationMessage, emailNotificationComment:emailNotificationComment},
			success : function(data){
				if(data.result==1)
				{
					$("#updateStatus").html(data.txt);
				}
			}
		});
	});
 
	$(".hidenotificationMessageBox").click(function () {
	$('#notificationMessageBox').hide('slow');
	$('#curtain').hide('slow');
	});
/*
	$(".addComment").click(function () {
		userText =  $(this).closest("div").find("textarea").val();
		entryID = $(this).parents("div").parents("div").attr("toDoID");
		requestType = $(this).attr("requestType");
		userSelect(entryID,requestType,userText);
		userText = $(this).closest("div").find("textarea").val('');
		$(this).closest("div").hide("slow");
	});
        $('body').on('click','.removeComment',function(){
	entryID = $(this).closest("div").attr("entryID");
	requestType = $(this).attr("requestType");
	userSelect(entryID,requestType,'');
	boxname = $(this).closest("div").attr("id");
	$('#'+boxname).hide("slow");
	});
        $('.showComment').click(function(){
	boxname = $(this).attr('boxname');
	$('#'+boxname).show("slow");
	});
*/
	$('.hideLocalLoginOption').click(function(){
		$('#localLoginOption').hide();
		$('#openIDLoginOption').show();
	});
	$('.showLocalForgotPassword').click(function(){
		$('#localLogin').hide();
		$('#localForgostPassword').show();
		document.getElementById('passwordimg').src = '/captcha/securimage_show.php?sid=' + Math.random();
		$('#un').focus();
	});
	$('.showLocalRegister').click(function(){
		$('#localLogin').hide();
		$('#localRegister').show();
		document.getElementById('registerimage').src = '/captcha/securimage_show.php?sid=' + Math.random();
		$('#emailreg').focus();
	});
	$('.hideLocalForgotPassword').click(function(){
		$('#localForgostPassword').hide();
		$('#localLogin').show();
		$('#email').focus();
	});
	$('.hideLocalRegister').click(function(){
		$('#localRegister').hide();
		$('#localLogin').show();
		$('#email').focus();
	});
	$(".ckeckRegisterForm").focus(function(){
		id = $(this).attr('id');
		if(id=="emailreg")
		{
			$('#miniHelpInput').html("Help:
- Enter your email address. Your email address is you User ID.");
		}
		else if(id=="password")
		{
			$('#miniHelpInput').html("Help:
- Specify your password. It must be at least 6 characters.");
		}
		else if(id=="captcha")
		{
			$('#miniHelpInput').html("Help:
- To show that you are a human please enter captcha code.");
		}
		$('#miniHelpInput').show("slow");
		$('#aboutYourAnswer').show("slow");
	});
	$(".checkLimit").keyup(function(){checkLimitChar($(this).attr('id'));});
	//$(".checkLimit").keyup(function(){checkLimitWords($(this).attr('id'));});
	$(".hideBox").mouseover(function () {
                var boxname = $(this).attr('boxname');
		boxnameArray = boxname.split(",");
		if(boxnameArray.length>1)
		{
			for(i=0; i< boxnameArray.length; i++) {
				if(boxnameArray[i]=='w')
				{
					document.getElementById('k').value=0;
					document.getElementById('k').name='';
				}
				$('#'+boxnameArray[i]).hide('slow');
			}
		}
		else
		{
			$('#'+boxnameArray).hide('slow');
		}
	});
	$("#hideMiniProfileBox").mouseover(function () { 
	$('#miniProfileBox').hide('slow');});
});
function checkLimitChar(id)
{
//	title = $(this).attr('title');
	stringVal = $('#'+id).val();
	var limit = parseInt($('#'+id).attr('maxlength'));
	if(stringVal.length > limit)
	{
		$('#'+id).val(stringVal.substr(0, limit));
		$("#errorMessage").html("You've reached to "+limit+" Characters.");
		$("#errorMessage").css("color","red"); 
	}
	else
	{
		$("#errorMessage").css("color",""); 
		$("#errorMessage").html(limit-$('#'+id).val().length + " character(s) left");
	}
	var offset = $('#'+id).position();
        $('#errorMessage').css({ top: offset.top-$('#errorMessage').height()-11, left: offset.left+370});//, left: offset.left+(($('#errorMessage').width())/2)
}
function checkLimitWords(id)
{
	stringVal = $('#'+id).val();
    var words = $('#'+id).val().match(/\S+/g).length;
	var limit = parseInt($('#'+id).attr('maxlengthWords'));
	
    if (words > limit) {
		// Split the string on first X words and rejoin on spaces
		var trimmed = $('#'+id).val().split(/\s+/, limit).join(" ");
		// Add a space at the end to make sure more typing creates new words
		$('#'+id).val(trimmed + " ");
		$("#errorMessage").html("You've reached to "+limit+" words.");
		$("#errorMessage").css("color","red"); 
    }
    else {
		$("#errorMessage").css("color",""); 
		$("#errorMessage").html(limit-words + " word(s) left");
    }
	var offset = $('#'+id).position();
	$('#errorMessage').css({ top: offset.top-$('#errorMessage').height()-11, left: offset.left+370});//, left: offset.left+(($('#errorMessage').width())/2)
}
function showNotificationMessage(msg)
{
	$("#notificationMessage").html(msg);
	$('#curtain').show('slow');
	$("#notificationMessageBox").show("slow");
}
function showNotificationMessageParent(msg)
{
	window.parent.$("#notificationMessage").html(msg);
	window.parent.$('#curtain').show('slow');
	window.parent.$("#notificationMessageBox").show("slow");
}
function hideBox(nameContainer,boxID)
{
	$("#"+boxID).hide("slow");
}
function userSelect(objID,mID,rt,txt)
{
//alert(eID+'-'+rt+'-'+txt);
	$.ajax({
		type : 'POST',	url : '/users/useraction.php',dataType: 'json',data: {id: mID, rt: rt, txt: txt},
		success : function(data){
			if(data.sessionValid==0)
			{
				//loginRequierdMessage();
				showNotificationMessage(data.visualMessage);
			}
			else
			{
				if(data.actionDone==1)
				{
					if(data.hideElement==1)
					{
						$('#'+objID).hide('slow');
						//$("#boradList").html(data.userBoardlist);
					}
				}
			}
		}
	});
}
function loginRequierdMessage()
{
	alert('Please login and try again.');
	return false;
}
$(document).ready(function() {
$('body').on('click','.userRequest',function(){
	toDoID = $(this).closest(".toDoArea").attr("toDoID");
	objID = $(this).closest(".toDoArea").attr("id");
	requestType = $(this).attr("requestType");
	userSelect(objID,toDoID,requestType,'');
	});
});
function settoDoPrio(toDoPrio)
{
	$('#'+toDoPrio).prop("checked", true);
}
function toDoTypeChange()
{
	var toDoType = $('#toDoType option:selected').val();
	if(toDoType==1)
	{
		$('.toDoStartTimeArea').hide();
	}
	else
	{
		$('.toDoStartTimeArea').show();
	}
	return true;
}
function getBoardList(boardID)
{
	$.ajax({
	type : 'POST',
	url : '/users/useraction.php',
	dataType : 'json',
	data: {	id: boardID, rt : 7},
	success : function(data){
		if(data.result==1)
		{
			if(data.userBoardlist=="")
			{
				$("#createBoradNamemsg").html('');
				$("#boardName").val('');
				$("#createnewBorad").show();
			}
			else
			{
				$("#cancelCreate").show();
				userBoardAccessList = data.userBoardAccessList;
				boardIDArray = data.boardIDArray;
				boardStartTimeArray = data.boardStartTimeArray;
			}
			$("#boradList").html(data.userBoardlist);
			$("#boradList").show();
		}
	}
	});
}
function createBoard()
{
	var boardPublic = 0;
	var boardName = $("#boardName").val();
	if($("#boardPublic").is(':checked')) boardPublic = 1;
	if(boardName!="")
	{
		$.ajax({
		type : 'POST',
		url : '/users/useraction.php',
		dataType : 'json',
		data: {	boardName: boardName, rt : 6, boardPublic: boardPublic},
		success : function(data){
			if(data.result==1)
			{
				$("#createBoradNamemsg").html(data.txt);
				getBoardList(data.boardID);
				$("#createnewBorad").hide();
				$("#boardName").val('');
			}
			else
			{
				$("#createBoradNamemsg").html(data.txt);
			}
		}
		});
	}
	else
	{
		$("#createBoradNamemsg").html('Error');
	}
}
function showToDoByBoard(obj)
{
	boardID = $(obj).val();
	location.href = '/users/?bid=' + boardID;
}
function editBoard(boardID,editAction)
{
	if(boardID==0)
	{
		boardID = $('#boardID option:selected').val();
		//alert('/users/eboard.php?bid='+boardID);
		openColorboxiFrame('/users/eboard.php?bid='+boardID);
	}
	else
	{
		if(editAction==15||editAction==16||editAction==17||editAction==18||editAction==19)
		{
		//Delete:15 accept:17 igonre:16 accep:17 done:18 re-open:19
			var userConfirm = true;
			if(editAction==3) userConfirm = confirm('Do you really want to delete? This action could not be rollback!');
			if(userConfirm)
			{
				$.ajax({
					type : 'POST',	url : '/users/useraction.php',dataType: 'json',data: {rt: editAction, id: boardID},
					success : function(data){
						if(data.sessionValid==0)
						{
							//loginRequierdMessage();
							showNotificationMessage(data.visualMessage);
						}
						else
						{
							if(data.actionDone==1)
							{
								if(data.hideElement==1)
								{
									$('#boardRow'+boardID).hide('slow');
								}
								else if(data.boardAceept==1)
								{
									$('#boardRow'+boardID).find(".boardStatus").html('accepted | ignore! || Done!');
								}
								else if(data.boardAceept==2)
								{
									$('#boardRow'+boardID).find(".boardStatus").html('Done! | Open!');
								}
								else if(data.boardAceept==3)
								{
									$('#boardRow'+boardID).find(".boardStatus").html('Edit!  | Delete! || Done!');
								}
								else if(data.boardAceept==4)
								{
									$('#boardRow'+boardID).find(".boardStatus").html('Done! | Open!');
								}
							}
						}
					}
				});
			}
		}
		else if(editAction==7)
		{
			openColorboxiFrame('/users/eboard.php?bid='+boardID);
		}	
	}
}
function addToDo()
{
	var toDoType = 1;
	var toDoStartTime = '';
	var toDoPrio = 1;
	var toDoRepetitionType = 0;
	var eTit = $('#eTit').val().trim();
	if(eTit.length>=storyTitleLength_min||imageID!=0)
	{
		toDoType = $('#toDoType option:selected').val();
		if(toDoType>1&&toDoType<5)
		{
			var toDoStartTime = $('#toDoStartTime').val().trim();
			if(toDoStartTime!='')
			{
				toDoRepetitionType = $('#toDoRepetitionType option:selected').val();
			}
			else
			{
				$('#toDoStartTime').focus();
				return false;
			}
		}
		//toDoPrio = $('input[name=toDoPrio]:checked', '#myToDoForm').val()
		boardID = $('#boardID option:selected').val();
		
//	alert('eTit:' + eTit + '--toDoType:' + toDoType + '--toDoRepetitionType:' + toDoRepetitionType + '--boardID:' + boardID + '--toDoStartTime:' + toDoStartTime);
			$.ajax({
				type : 'POST',	url : '/users/useraction.php',dataType: 'json',data: {rt: 20, eTit: eTit, toDoType: toDoType, toDoRepetitionType: toDoRepetitionType, boardID: boardID, toDoStartTime: toDoStartTime, imageID: imageID},
				success : function(data){
					if(data.sessionValid==0)
					{
						//loginRequierdMessage();
						showNotificationMessage(data.visualMessage);
					}
					else
					{
						if(data.actionDone==1)
						{
							$("#myToDoList").prepend(data.toDoBox);
							$('#eTit').val('');
							$('#toDoStartTime').val('');
							$("#boradList").html(data.userBoardlist);
							if(imageID!=0)
							{
								imageID = 0;
								$("#toDoImage").attr("src",'');
								$('#imageArea').hide();
								$("#uploadAPhoto").show();
								$('#imageUploadArea').show();
							}
						}
					}
				}
			});
	}
	else
	{
		alert('ToDo text must have at least 2 characters!');
	}
}
function openColorboxiFrame(url){    
  $.colorbox({width:"80%", height:"80%", iframe:true, href:url});
}
function enableEdiBoardOption()
{
	var boardIDSelected = $('#boardID option:selected').val();
	if(boardIDSelected==-1)
	{
		$("#boradList").hide();
		$("#createnewBorad").show();
	}
	else
	{
		if(userBoardAccessList[boardIDArray.indexOf(boardIDSelected)]=='1')
		{
			$("#ediBoardOption").show();
		}
		else
		{
			$("#ediBoardOption").hide();
		}
		if(boardStartTimeArray[boardIDArray.indexOf(boardIDSelected)]!='')
		{
			$(".toDoStartTimeArea").show();
			$("#toDoStartTime").val(boardStartTimeArray[boardIDArray.indexOf(boardIDSelected)]);
			if($('#toDoType option:selected').val()==1)
			{
				$('#toDoType').val(3);
			}
		}
	}
}
function updateTime()
{
	var currentDate= new Date();
	var hr=currentDate.getHours();
	var min=currentDate.getMinutes();
	var sec=currentDate.getSeconds();
	if(min<10) min = '0'+min;
	if(sec<10) sec = '0'+sec;
	$('#currentHour').html(hr);
	$('#currentMinute').html(min);
	$('#currentSecond').html(sec);
	if($('#hour').val()==hr&&$('#minute').val()==min&&sec==0) openColorboxiFrame('/alarm.php');
	setTimeout("updateTime()",500);
}
function clockStatusCheck()
{
	if($('#hour').val()!=0) $('#hour').val(0);
}