﻿function getCookie(c_name) {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
}

function preloadImage(){
    var myimages = new Array();
    for (var x=0; x<preloadImage.arguments.length; x++){
    myimages[x] = new Image();
    myimages[x].src = preloadImage.arguments[x];
    }
}

function preloadTabImages() {
    var tab;
    for (var x=0; x<preloadTabImages.arguments.length; x++){
        tab=preloadTabImages.arguments[x];
        preloadImage("/images/tab_"+tab+".jpg", "/images/tab_"+tab+"_over.jpg", "/images/tab_"+tab+"_active.jpg");
    }
}


function EnterKeyPressed(e) {
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        
        return (keycode==13)?true:false;
}

function toggleBlocks(block1,block2) {
    var node1 = document.getElementById(block1);
    var node2 = document.getElementById(block2);
	if (node1.style.display=='none') {
	    node1.style.display='block';
	    node2.style.display='none';
	 } else {
	    node1.style.display='none';
	    node2.style.display='block';
	 }
}

function browser_name() { 
    var browserName = ""; 

    var ua = navigator.userAgent.toLowerCase(); 
    if ( ua.indexOf( "safari" ) != -1 ) { 
        browserName = "safari"; 
    } 
    if ( ua.indexOf( "msie" ) != -1 ) { 
        browserName = "msie"; 
    }
    return browserName; 
};

function refreshPage() {
    document.location.reload(); //assume that there was no form post
}

function popup(url,width,height) {
  var b_name=browser_name();

  if(b_name=="msie" )
  {
   fw=window.open(url,'Print',"toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=yes,left=0,top=0,width="+width+",height="+height);     
   }
   else
   {
    fw=window.open(url,'Print',"toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=yes,left=200,top=200,width="+width+",height="+height); 
   }
   
   fw.focus();
}

function setInputBackground(input,bgImage) {
    if (input.value.length==0) {
        input.style.backgroundImage="url("+bgImage+")";
        input.style.backgroundRepeat="no-repeat";
    } else {
        input.style.backgroundImage="none";
    }
}

function RadComboBoxSearch(combobox) {
    var combo = $find(combobox); 
    var q = combo.get_text();
    if (q.length==0) {
        alert("Please input a search string.");
    } else {
       document.body.focus(); //has to unfocus the input box, so that the autocomp can work after a page back
       document.location.href="/search?q=" + encodeURIComponent(q).replace("'","%27");
    }
    return false; //prevent form postback
}

function PrepareForAutoComp(sender, eventArgs) {
    if (eventArgs.get_text().length < 3) eventArgs.set_cancel(true);
}

function removeHTMLTags(str){
	return str.replace(/<\/?[^>]+(>|$)/g, "");
}

function requestFriendship(toUserId,lnk) {
   if (checkUserStatus('1')) {
       SSC.CPC.Web.UserServices.RequestFriendship(toUserId);
       lnk.onclick='';
       lnk.style.color='#000';
       lnk.innerHTML='(You have sent a friend request to this user)';
       
       var requestBatch = new RequestBatch();
       requestBatch.AddToRequest(new AddFriendAction(new UserKey(toUserId.toString())));    
       requestBatch.BeginRequest(serverUrl, pluckCallBackFoo);
   }
}

function openUpcomingEventDetail(eventId) {
    popup('/calendars/UpcomingEvent?eventId='+eventId,600,400);
}

function openGroupEventDetail(eventKey) {
    popup('/calendars/UpcomingEvent?groupEventKey='+eventKey,600,400);
}

function getMyActivityCount() {
   var requestBatch = new RequestBatch();     
   requestBatch.AddToRequest(new PrivateMessagePage('Inbox',1,1,'Unread'));    
   requestBatch.BeginRequest(serverUrl, addPendingFriendsCount);    
}

function addPendingFriendsCount(responseBatch) {
    var msgCount=0;
    if (responseBatch.Responses.length>0) {
        var privateMessagePage = responseBatch.Responses[0].PrivateMessagePage;
        if (privateMessagePage!=null) {
            msgCount=privateMessagePage.NumberOfMessages;
        }
    }
    SSC.CPC.Web.UserServices.GetMyActivityCount(msgCount,displayMyActivityCount);
}

function displayMyActivityCount(activityCount) {
    if (activityCount>0) {
        document.getElementById('MyActivityCount').innerHTML='&nbsp;('+activityCount+')'
    }
}

function checkUserStatus(reqStatusIds) {
    var userstatus=getCookie('userstatus');
    if ((','+reqStatusIds+',').indexOf(','+userstatus+',')<0) {
        if (userstatus=='2') {
            alert('Sorry, you don\'t yet have permission to do that.');
        } else {
            alert('Please join CardioExchange to be able to use all features.');
        }
        return false;
    } else {
        return true;
    }
}

function pluckCallBackFoo(responseBack) {}

function articlePopup(url) {popup(url,750,550); }

function shareArticle(id,contentType) {
    if (checkUserStatus('1')) {
        popup('/shareArticle/' + id + '?type='+contentType,800,500);
    }
}

function inviteFriends() {
    if (checkUserStatus('1')) {
        popup('/users/inviteFriends', 600, 370);
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
