﻿// JScript File

function countSelected()
{
    var s = document.getElementsByName("selMixes");
    var count=0;
    for (i=0;i<s.length;i++){
        if(s[i].checked)count++;
    }
    return count;
}

function countSelectedItems(elementName)
{
    var s = document.getElementsByName(elementName);
    var count=0;
    for (i=0;i<s.length;i++){
        if(s[i].checked)count++;
    }
    return count;
}

function secToMinSec(seconds) 
{
    var hrs = seconds / 3600;
    hrs = Math.floor(hrs);
    seconds = seconds - (hrs*3600);
    var mins = seconds / 60;
    mins = Math.floor(mins);
    seconds = seconds - (mins*60);	

    if(hrs>0)
    {
	    return(padInt(mins) + ":" + padInt(seconds));
    }
    else
    {
	    return(padInt(hrs) + ":" + padInt(mins) + ":" + padInt(seconds));
    }
}


function isNewImg(seconds) 
{
    if(seconds>0)
    {
	    return("Yes");
    }
    else
    {
	    return("No");
    }
}



function secondsToString(seconds)
{

//test with value of 4 seconds

    if((seconds==null)||(isNaN(seconds)))
    {
        return "Never";
    }

	if(seconds < 31536000)
		years=0;
	else
		years = parseInt(seconds / 31536000);


	tmp = seconds - (years*31536000);

	
	if(tmp<86400)
		days=0;
	else
		days = parseInt(tmp / 86400);


	tmp = tmp - (days * 86400);

	
	if(tmp<3600)
		hours=0;
	else
		hours = parseInt(tmp / 3600);


	tmp = tmp - (hours * 3600);


	if(tmp<60)
		mins=0;
	else
		mins = parseInt(tmp / 60);


	tmp = tmp - (mins * 60);


	secs = tmp;


	var str = "";



	//only show seconds if all others are zero (and seconds>0)
	//only show minutes if days and years are zero (and minutes>0)
	//only show hours if years are zero (and hours>0)

	if(years>0)
	{
	    if(years>1)
		    str += years + " years ";
		else
		    str += years + " year ";
	}
	if(days>0)
	{
	    if(days>1)
		    str += days + " days ";
		else
		    str += days + " day ";
	}
	if((hours>0) && (years==0))
	{
	    if(hours>1)
		    str += hours + " hours ";
        else
            str += hours + " hour ";		    
    }
	if((mins>0) && (days==0) && (years==0))
	{
	    if(mins>1)
		    str += mins + " minutes ";
		else
		    str += mins + " minute ";
	}
	if((secs>0) && (mins==0) && (hours==0) && (days==0) && (years==0))
	{
	    if(secs>1)
		    str += secs + " seconds ";
		else
	        str += secs + " second ";	
	}
	

	    str+=" ago";
	    return str;

}



function padInt(n)
{
    n+="";
    var l = n.length;
    if (l>=2) return n;
    else return "0"+n;
}


function blankStatus()
{
   window.status = ' ';
   return true;
}


///////////////////////////////////////////////////////////////////////////////////////



function validateRange()
{
    var s = document.frmInput.txtInput.value;
    var A = document.frmInput.txtA.value;
    var B = document.frmInput.txtB.value;

    switch (isIntegerInRange(s, A, B))
    {
     case true:
        alert(s + " is in range from " + A + " to " + B)
        break;
     case false:
        alert(s + " is not in range from " + A + " to " + B)
    }
}



//limit field length
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}

function EnsureSubString(s, len, def)
{
    if (s.length >= len)
        return s.substring(0, len);
    else return def;
}

function EnsureStringPart(s, partnum, token, def)
{
    var parts = s.split(token);
    if (parts.length > partnum)
    {
        return parts[partnum];
    }
    else
    {
        return def;
    }
}



// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
function isIntegerInRange (s, a, b)
{   
    if (isEmpty(s))
    {
     if (isIntegerInRange.arguments.length == 1) 
        return false;
     else return (isIntegerInRange.arguments[1] == true);
    }

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}



function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}



function tagify(tagStr)
{
    var tags=tagStr.split(",");
    var links='';
    for (i=0;i<tags.length;i++)
    {
        tags[i]=trimString(tags[i]);
        //if(i<tags.length-1)
        //    links+="<a href='tag_" + tags[i] + ".aspx'>"+tags[i]+"</a>,&nbsp;&nbsp;";
        //else
        //    links+="<a href='tag_" + tags[i] + ".aspx'>"+tags[i]+"</a>&nbsp;&nbsp;";
            
        if(i<tags.length-1)
            links+="<a href='tag_" + tags[i] + "'>"+tags[i]+"</a>,&nbsp;&nbsp;";
        else
            links+="<a href='tag_" + tags[i] + "'>"+tags[i]+"</a>&nbsp;&nbsp;";
    }
    return links;
}

    
function trimString(sInString) 
{
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
}


function setPageTitle(newTitle)
{
    document.title = newTitle;
}


///////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
//Widget mini players


//This happens when a user clicks on the play .jpg image on the track list row.
function playTrack(key) 
{

    //This tells all the track play images to go to the stopped state.
    elem = document.getElementById(key);
	
    if(elem.state==2) //Is playing so stop it
    {
        stopSpecificTrack(key);
        return;
    }
    else if (elem.state==1) //just stop waiting
    {
        stopSpecificTrack(key);
        elem.src="images/widget_play.gif";
        elem.state=0;
        return;  
    }
    else
    {
        setAllTracksToStopped();
        elem.src="images/waiting.gif";
        elem.state=1;
        //Need to put code in here to tell the flash widget to
        //play the track identified by "key"
        onFlashReady(key); // is this how its done?
        //Where key is the track filename key. For example, for the track key value of abcdefghij
        //The widget would start to play
    }
}




//This is the callback function that the flash widget calls when it
//begins to play the mp3 file. The key parameter is the same as the
//one that is passed to it above in sendToAS(key);
function trackIsPlaying(key)
{
    elem = document.getElementById(key);
    elem.src="images/widget_stop.gif";
    elem.state=2;
}



//The flash widget doesn't need to use this method at all so u can ignore it.
function setAllTracksToStopped() 
{
    elems = document.getElementsByName("playerbutton");
	
    var i=0;
    for(i=0;i<elems.length;i++)
    {
        elems[i].src="images/widget_play.gif";
        elems[i].state=0;
    }
    
    stopAS();
}

//The flash widget doesn't need to use this method at all so u can ignore it.
function stopSpecificTrack(key) 
{
    elem = document.getElementById(key);
    elem.src="images/widget_play.gif";
    elem.state=0;
    
    stopAS();
}

function trackIsStopped(key)
{
    elem = document.getElementById(key);
    elem.src="images/widget_play.gif";
    elem.state=0;
}



// call this function first passing the key
function onFlashReady(key)
{                      
    sendToAS(key);
}


// the following 2 functions get called as a result
function sendToAS(value)
{          
    thisMovie("trackPreview").callAS(value);
}

function thisMovie(movieName) 
{
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        return window[movieName];
    } 
    else 
    {
        return document[movieName];
    }
}
	 
function stopAS()
{       
    thisMovie("trackPreview").stopPlayback();
}  

//////////////////////////////////////////////////////////////////////////////////////////
//Help tip show/hide

function showPageHelp()
{
    var div1 = document.getElementById("pageHelpContent");
    if(div1.style.display=="block")
    {
        div1.style.display="none";
    }
    else
    {
        div1.style.display="block";
    }
}


function ReverseString(s)
{
    var news = "";
    var i = s.length;
    i = i - 1;

    for (var x = i; x >= 0; x--)
    {
        news = news + s.charAt(x);
    }
    return news;
}

function unscrambleEmail(aTagId, p1, p2, p3, p4)
{
    var elem = document.getElementById(aTagId);
    elem.href = ReverseString(p1 + "." + p2 + "@" + p3 + p4);
    elem.innerHTML = ReverseString(p1 + "." + p2 + "@" + p3);
}

function bio_cuttoff(maxlen, biotext, userid)
{
    if (biotext.length > maxlen - 5)
    {
        return biotext + '... <a href="userprofile' + userid + '.aspx">Read more</a>';
    }
    else
    {

        return biotext;
    }
}
