﻿// JScript File

var appPath;
var siteID;
var newChatMessagesFlag = true;

/***********************************
setting user has rating on or off
************************************/
function SetUserHasRating(v)
{
    parent.frames['frm_MainFrame'].GoopDate.EditUserDetails.SetUserHasRating(v); 
}

/***********************************
Valid NickName on EditUserDetails Form
************************************/
function ValidNickName(name)
{
    parent.document.body.style.cursor = 'wait';
    var img = document.getElementById('img_'+name);
    var textBox = document.getElementById('txt_'+name);
    var div = document.getElementById('div_'+name);
    var result;
    
	if (textBox.value.length==0) {
		alert('יש להזין כינוי');
		parent.document.body.style.cursor = 'auto';
		return false;
	}
	 
     result = parent.frames['frm_MainFrame'].GoopDate.EditUserDetails.ValidNickName(textBox.value); 
        
    if (result.value.length>0)
    {
       img.src = 'Static/Common/Images/false.gif';
       img.style.display = 'block';
       div.innerHTML = result.value;
       div.style.color = 'Red';
     } 
    else
    {
         img.src = 'Static/Common/Images/true.gif';
         img.style.display = 'block';
         div.innerText = 'פנוי';
         div.style.color = 'Green';
     }
    parent.document.body.style.cursor = 'auto'; 
}

/*********************************************
Valid UserName and NickName in Registration form
*********************************************/
function ValidName(name)
{
    parent.document.body.style.cursor = 'wait';
	var img = document.getElementById('img_'+name);
	var textBox = document.getElementById('txt_'+name);
	var div = document.getElementById('div_'+name);
	var lbl = document.getElementById('lbl_'+name);
	lbl.innerText = ''; 
	var result;
	
	if (textBox.value.length==0) {
		if (name == 'UserName')
			alert('יש להזין שם משתמש');
		else
			alert('יש להזין כינוי');
		parent.document.body.style.cursor = 'auto';
		return false;
	}
    
    if (name == 'UserName')
		result = parent.frames['frm_MainFrame'].GoopDate.Registration.ValidUserName(textBox.value);
    else
    	result = parent.frames['frm_MainFrame'].GoopDate.Registration.ValidNickName(textBox.value);
    	
    if (result.value.length>0)
    {
		img.src = 'Static/Common/Images/false.gif';
		img.style.display = 'block';
		div.innerHTML = result.value;
		div.style.color = 'Red';
     } 
    else
    {
		img.src = 'Static/Common/Images/true.gif';
		img.style.display = 'block';
		div.innerText = 'פנוי';
		div.style.color = 'Green';
     }
    parent.document.body.style.cursor = 'auto'; 
}

/*************************************
Shows or hides a user's friend in UserCard
**************************************/
function HideShowUser(obj)
{
    parent.frames['frm_MainFrame'].GoopDate.Friends.HideShowUserInCard(obj.UserID, obj.checked);
}


/*****************************************
Timer for check new user content alerts: 
messages, replies, chats...
*****************************************/
var oldMsgsWin;
var msgsTimer = null;
var trMessagesId, tdMessagesId;
var trRequestsId, tdRequestsId;
var trRepliesId, tdRepliesId;
var trChatsId, tdChatsId;
var chatWin;
function MessagesTimer(appPath, time)
{   
	var trMessages = document.getElementById(trMessagesId);
	var tdMessages = document.getElementById(tdMessagesId);
	var trRequests = document.getElementById(trRequestsId);
	var tdRequests = document.getElementById(tdRequestsId);
	var trReplies = document.getElementById(trRepliesId);
	var tdReplies = document.getElementById(tdRepliesId);
	var trChats = document.getElementById(trChatsId);
	var tdChats = document.getElementById(tdChatsId);
		
    //check for new content alerts: messages, replies, chats
    var response = parent.frames['frm_RightFrame'].GoopDate.AccountStatus.CheckNewContentAlerts();
    
    if (response.value != null) 
    {
		// new messages
		var messageCount = response.value[0];
		if (messageCount > 0) // there are some new messages
		{
		   if (trMessages != null)
		   { 
				trMessages.style.display = 'block';
				tdMessages.innerHTML = '<a class="ContentAlert" href="' + appPath + 'Web/UserData/MailBox.aspx?Type=1" target="frm_MainFrame">יש לך ' + messageCount + ' הודעות חדשות</a>';
				oldMsgsWin = OpenPopUp('PopUps/NewMessages.aspx',455,260); 
		   }
		}

		// new friend requests
		var requestsCount = response.value[1];
		if (requestsCount > 0) // there are some new new replies
		{
		   if (trRequests != null)
		   { 
				trRequests.style.display = 'block';
				tdRequests.innerHTML = '<a class="ContentAlert" href="' + appPath + 'Web/UserData/FriendRequests.aspx?type=2" target="frm_MainFrame">יש לך ' + requestsCount +' בקשות חברות</a>';
		   }
		}
		    
		// new replies
		var repliesCount = response.value[2];
		if (repliesCount > 0) // there are some new new replies
		{
		   if (trReplies != null)
		   { 
				trReplies.style.display = 'block';
				tdReplies.innerHTML = '<a class="ContentAlert" href="' + appPath + 'Web/SearchResults/RepliesSearch.aspx?showOwner=true" target="frm_MainFrame">יש לך ' + repliesCount +' תגובות חדשות</a>';
		   }
		}
		
		// new chats
        if (response.value.length > 3) // there are new chats
        {            
			for (i=3; i<response.value.length; i++)
			{ 
				if (chatWin==null || chatWin.closed)
					chatWin = OpenNamePopUp('PopUps/AddToChat.aspx?UID='+response.value[i],'ChatWith_'+response.value[i],395,260); 
			} 
			trChats.style.display = 'block';
			if (chatWin==null) // window didn't open
				tdChats.innerHTML = '<a class="ContentAlert" href="javascript:void(0);" onclick="OpenNamePopUp(\'PopUps/AddToChat.aspx?UID='+response.value[response.value.length-1]+'\',\'ChatWith_'+response.value[response.value.length-1]+'\',395,260);">יש לך שיחה אישית חדשה</a>';
			else
				tdChats.innerHTML = '<a class="ContentAlert" href="javascript:void(0);">יש לך שיחה אישית חדשה</a>';
        }
    }
    
    msgsTimer = setTimeout("MessagesTimer('" + appPath + "'," + time + ")",time*1000);
}

/******************************************
Timer for check current chat status
******************************************/
function ChatStatusTimer(appPath,time,senderID,receiverID,count)
{
    if (count <= 8)
    {
       var status = GoopDate.ChatInvite.CheckChatStatus(senderID,receiverID);

       //if invited user not interested or invited user close window or timeout 
       if (status.value == 2 || status.value == 6 || count == 8)
       {
           document.getElementById('tdWaitForAnswer').style.display = 'none';
           document.getElementById('tdNegativeAnswer').style.display = 'block'; 
           document.getElementById('btn_CancelChat').value = 'x סגור חלון';
       }
       else
       {
            //if invited user not enter to chat
           if (status.value != 5)
           { 
                count++;
                setTimeout("ChatStatusTimer('" + appPath + "'," + time + "," + senderID + "," +  receiverID + "," + count +")",time*1000);
           }
           else
           {
                window.open('ChatForm.aspx?SenderID=' + senderID + '&ReceiverID=' + receiverID,'_self');
           }
       } 
    }
}

/***************************************
Function CloseNewMessagesWindow
***************************************/
function CloseNewMessagesWindow(appPath, time)
{
	GoopDate.NewMessages.CloseNewMessages();
    opener.document.getElementById(opener.trMessagesId).style.display='none'; 
    window.close();
}

/***************************************
Function HideRepliesAlert
***************************************/
function HideRepliesAlert()
{
	if (document.getElementById(trRepliesId) != null)
		document.getElementById(trRepliesId).style.display='none';
}

/***************************************
Function HideRequestsAlert
***************************************/
function HideRequestsAlert()
{
	if (document.getElementById(trRequestsId) != null)
		document.getElementById(trRequestsId).style.display='none';
}

/***************************************
Function NotInterested from new messages window
***************************************/
function NotInterested(msgId, userId)
{
	var result = GoopDate.NewMessages.NotInterested(msgId, userId);
	// hide the message TR
	document.getElementById('tr_'+msgId).style.display = 'none';
	// close the window if needed
	if (result.value) self.close();
}

/***************************************
Toggles the users SMS settings for new msgs on or off.
called from the mailbox
***************************************/
function ToggleSMS_Msg_Notif(cb)
{
	 var result = parent.frames['frm_MainFrame'].GoopDate.MailBox.ToggleSmsNotifs(cb.checked);
	 
	// redirect the user if needed
	 if (result.value == 1)
		parent.frames['frm_MainFrame'].location.href="Web/UserData/RegisterSMS.aspx";

}

