﻿//friendsAlerts.js
    function AcceptInvitation(userIdNumber , userId ,  invitationKey)
    {

        //Create an ajax call 
        var AjaxRequest;
        var invitationStatus;
        
        AjaxRequest =  CreateAjaxRequest(AjaxAlertsUrl +"&userIdNumber=" + userIdNumber ,"POST","userId=" + userId 
                                         + "&invitationKey=" + invitationKey);
                                   
    
        AjaxRequest.OnLoaded = function() 
        { 
           invitationStatus = AjaxRequest.GetText();
         
            if (invitationStatus != "")
            {
              hide("#pnlInvitation"+userIdNumber);
              show("#pnlInvitationStatus"+userIdNumber);
              $("#lblInvitationStatus"+userIdNumber).text(invitationStatus);
              AjaxRequest = null;
            }
        }
        AjaxRequest.OnFailure = function()
        {
            AjaxRequest = null;
        }
        
    }

    function IgnoreInvitation( userIdNumber)
    {

       //Create an ajax call 
        var AjaxRequest;
        AjaxRequest =  CreateAjaxRequest(AjaxAlertsUrl + "&userIdNumber=" + userIdNumber  ,"Get","" );
        var invitationStatus;
        AjaxRequest.OnLoaded = function() 
        { 
            invitationStatus = AjaxRequest.GetText();
         
            if (invitationStatus != "")
            {
                hide("#pnlInvitation"+userIdNumber);
                show("#pnlInvitationStatus"+userIdNumber);
                $("#lblInvitationStatus"+userIdNumber).text(invitationStatus);
                AjaxRequest = null;
            }
        }
        AjaxRequest.OnFailure = function()
        {
            AjaxRequest = null;
        }
      

    }

    function BlockInvitation( userIdNumber, userID)
    {
        //Create an ajax call 
        var AjaxRequest;
        var invitationStatus;
        
        AjaxRequest =  CreateAjaxRequest(AjaxAlertsUrl + "&userIdNumber=" + userIdNumber ,"POST","action=block&userName="+ userID);
    
        AjaxRequest.OnLoaded = function() 
        { 
            invitationStatus = AjaxRequest.GetText();
         
            if (invitationStatus != "")
            {
                hide("#pnlInvitation"+userIdNumber);
                show("#pnlInvitationStatus"+userIdNumber);
                $("#lblInvitationStatus"+userIdNumber).text(invitationStatus);
                AjaxRequest = null;
            }
        }
        AjaxRequest.OnFailure = function()
        {
            AjaxRequest = null;
        }
        

        
    }
    

//SearchResults.js

//Validate the forms and the emails, if they are valid, call an ajax page to send the emails.
function CallSendEmailsPage(type, itemID, itemUrl, itemTitle,itemThumbnailUrl, isLoggedIn,lang)
  {      
    var userName = document.getElementById('txtUserName').value;
    var userEmail = document.getElementById('txtUserEmail').value;
    var emailList = document.getElementById('txtEmails').value;
    var userMessage = document.getElementById('txtYourMessage').value;
        
    var data = "";
    var i;
    var isValid = true;
    
    //VALIDATE TYPE.
    if((type != null) && (type != "") )
    {
        data = data + "type=" + type;
    }
    else
    {
        isValid = false;
    }

    //VALIDATE TITLE.
    if((itemThumbnailUrl != null) && (itemThumbnailUrl != "") )
    {
        data = data + "&thumbnail=" + itemThumbnailUrl;
    }
    else
    {
        if(type != 'Other')
        {
            isValid = false;
        }
    }

    //VALIDATE TITLE.
    if((itemTitle != null) && (itemTitle != "") )
    {
        data = data + "&itemtitle=" + itemTitle;
    }
    else
    {
       isValid = false;
    }
    
    //VALIDATE THE URL
    if((itemUrl != null) && (itemUrl != "") )
    {
        data = data + "&itemurl=" + itemUrl;
    }
    else
    {
      isValid = false;
    }
    
    //VALIDATE ITEMID.
    if((itemID != null))
    {
        data = data + "&item=" + itemID;
    }
    else
    {
       isValid = false;
    }
    
    if(isLoggedIn == 'False')
    {
        //VALIDATE USER NAME.
        if((userName != null) && (userName != '') )
        {
            data = data + "&username=" + userName;
        }
        else
        {
            document.getElementById("errUserNameEmpty").style.display = 'block';
            isValid = false;
        }
        
        //VALIDATE USER EMAIL.
        if((userEmail != null) && (userEmail != '') )
        {
            if(validateEmail(userEmail))
            {
                data = data + "&useremail=" + userEmail;
            }
            else
            {
                document.getElementById("errUserEmailIncorrect").style.display = 'block';
                isValid = false;
            }
        }
        else
        {
            document.getElementById("errUserEmailEmpty").style.display = 'block';
            isValid = false;
        }
    }
    
    //VALIDATE EMAILS.
    if((emailList != null) && (emailList != '') )
    {
        var emailArr;
        var validEmails = "";

        emailList = emailList.replace('\n',',');
        emailList = emailList.replace(' ',',');
        emailList = emailList.replace(/(^\s+)(\s+$)/,"");

        emailArr = emailList.split(',');
        
        for(i=0; i < emailArr.length; i++) 
        {
            var email = emailArr[i];
            //email = email.replace(/(^\s+)(\s+$)/, "");
            //email = email.replace("/\\s*/g", " ");
            email = email.replace(/ /g,"");
            
            if(email != '')
            {
                if(validateEmail(email) == true)
                {
                    validEmails = validEmails + email + ',';
                }
                else
                {
                    document.getElementById("errEmailsInvalid").style.display = 'block';
                    isValid = false;
                }
            }
        }

       validEmails = rtrim(validEmails, ',');
             
       if((validEmails !=null) && (validEmails != ''))
       {
         data = data + "&emails=" + validEmails;
       }
       else
       {
          document.getElementById("errEmailsInvalid").style.display = 'block';
          isValid = false;
       }
    }
    else
    {
        document.getElementById("errEmails").style.display = 'block';
        isValid = false;
    }
    
    if((lang)!= null)
    {
        data = data + "&lang=" + lang;
    }
    
    if((userMessage)!= null)
    {
        data = data + "&usermessage=" + userMessage;
    }
 
    if(isValid == true)
    {
            //CALL AJAX PAGE
            var FilesAJAX = new JeeranRemoteScripting();
	        FilesAJAX.SetRequestMethod("POST");
	        FilesAJAX.SetRequestURL(ajaxSendItemUrl);
	        FilesAJAX.SetData(data);
    	
	        FilesAJAX.SetRequestAsynch(true);
            FilesAJAX.InitializeRequester();
                ShowDiv('divLoading');
	        FilesAJAX.SendRequest();
	        //waiting?

	        FilesAJAX.OnLoaded = function()
	        {
                FilesAJAX.GetText();
            
                HideDiv('divSendForm');
                ShowDiv('divSuccess');
                HideDiv('divLoading');
	            FilesAJAX = null;
	        }
    	
	        FilesAJAX.OnFailure = function()
	        {
	            FilesAJAX = null;
	            HideDiv('divSendForm');
	            ShowDiv('divError');
	            HideDiv('divLoading');
	            return "Failure";
	        }
	 }
}

function validateEmail(elementValue)
{      
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue); 
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function rtrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}  

function HideDiv(DivName) 
{
    document.getElementById(DivName).style.visibility = "hidden";
    document.getElementById(DivName).style.display = "none";
}
function ShowDiv(DivName) 
{
    document.getElementById(DivName).style.visibility = "visible";
    document.getElementById(DivName).style.display = "";
}





var channelsPageNumber = 1; 
function getnext5() {
    
    channelsPageNumber = channelsPageNumber + 1;
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', "contCol" + channelsPageNumber);
    newdiv.setAttribute('class', "contCol1");
    newdiv.innerHTML = "";
    document.getElementById("channelscontainer").appendChild(newdiv)
    document.getElementById('Displaybtn').style.marginTop = '40px';
    LoadAjaxInBox(newdiv.id, ajaxpage, 'GET', 'type=' + channelsPageNumber, '', true);
    document.getElementById('Displaybtn').style.marginTop = '40px';
    itemcount = itemcount - numberofitem;
    if (itemcount <= 0) {
        document.getElementById("Displaybtn").style.display = "none";
    }
    else if (itemcount <= numberofitem) {
        document.getElementById("Displaymsg").innerHTML = DisplayMsg.replace("[count]", itemcount)
    }

}



function changeTabsconnectednow(elemId1, elemId2, elemId1Class, elemId2Class) {
    var panel1 = $("#" + elemId1).attr('href');
    var panel2 = $("#" + elemId2).attr('href');

    $("#" + elemId2).removeClass(elemId1Class);
    $("#" + elemId1).addClass(elemId2Class);


    show(panel1);
    hide(panel2);


}

function LoadSeeMoreFeaturedPulse(DivID,loadingText) {
    PageCount = PageCount + 1;
    
    //Create an ajax call 
    var AjaxRequest;

    AjaxRequest = CreateAjaxRequest(FeaturedPulseAjaxUrl + "&PageCount=" + PageCount, "GET", '');
    //SetTextOfLayer(DivID, "<div style='position: relative; top:40px; margin: auto; height:31px; width:150px; line-height:31px; font-size: 10px;'><img src='/im/j2/ajax/loading.gif' style='height: auto; width: auto; border:0; padding:0;' />&nbsp;" + loadingText + "</div>");
    show("#LoadingDiv");
    
    AjaxRequest.OnLoaded = function() {
    
        hide("#LoadingDiv");
        j$Obj(DivID).innerHTML += AjaxRequest.GetText();

        if (PageCount * PageSize > TotalCount) {
        hide('#divShowMore');
        }
        AjaxRequest = null;

    }
    AjaxRequest.OnFailure = function() {
        AjaxRequest = null;
    }
}

