﻿
/******************************
    Force out of frames on any web site that links to this web site
*******************************/

if(self != top)
	top.location = window.location.href;

/******************************
    Top Navigation Quick Search
*******************************/

/* Removes the default value text in the a text box when it gets the focus */
// Need to make this work with other languages (i.e. not only for 'Search...')
function TxtBoxClear(input, value, focus) 
{
	if(!focus && input.value == "")
	    input.value = value;
	if(focus && input.value == value)
	    input.value = "";
}

function QuickSearch()
{
    
    if($get("searchBox").value != "Search..." && $get("searchBox").value != "")
        return true;
    
    return false;
}

/***********************
    LOGIN
************************/

// Store the page that will be redirected to after a successful login. Is set by submitModalLoginForm(.) and used by processLoginResult(...)
var onLoginCompletionGoToPage;


// Function called by 'Log In' button on modal login. That form is never submitted, it is all handled here instead (in order to prevent a page refresh), using an AJAX login method
function submitModalLoginForm( onCompletionGoToPage )
{
	onLoginCompletionGoToPage = onCompletionGoToPage;
	
	// Reset the failureText message to not be displayed - check processLoginResult() function for relevent code
	document.getElementById("failureText").style.display = 'none';
	
	// Get the input data - IDs are dependent upon names of ASP.net controls. Not ideal, but...
	var username = document.getElementById("ctl00_loginStatusLinks_loginView_username").value;
	var password = document.getElementById("ctl00_loginStatusLinks_loginView_password").value;
	var isPersistent = document.getElementById("ctl00_loginStatusLinks_loginView_RememberMe").checked;
	
	// Required Field Validation - as we are bypassing the standard form submit, we need to do this validation manually
	var isRequiredFieldEmpty = false;
	if( username == null || username.length == 0 )
	{
		document.getElementById("ctl00_loginStatusLinks_loginView_usernameReqVal").style.display = 'inline';
		isRequiredFieldEmpty = true;
	}
	if( password == null || password.length == 0 )
	{
		document.getElementById("ctl00_loginStatusLinks_loginView_passwordReqVal").style.display = 'inline';
		isRequiredFieldEmpty = true;
	}
	
	// If good, proceed with login
	if( !isRequiredFieldEmpty )
	{
		// Communicate with the server using this AJAX function, attempt to complete the login. Function processLoginResult() below handles the response from the server
		// Check documentation here: http://ajax.asp.net/docs/ClientReference/Sys.Services/AuthenticationServiceClass/AuthenticationServiceLoginMethod.aspx
		Sys.Services.AuthenticationService.login( username, password, isPersistent, null, null, processLoginResult, onError, username );
	}
}

// A sub-function of submitModalLoginForm(). Executed when the AJAX Sys.Services.AuthenticationService.login() function returns.
function processLoginResult(result, userContext, methodName)
{
	// 'result' is a boolean indicating whether the login was successful or not
   if( result )
   {
		// Redirect to the onLoginCompletionGoToPage if one is passed through
		if( onLoginCompletionGoToPage != null && onLoginCompletionGoToPage.length > 0 )
			window.location = onLoginCompletionGoToPage;
		// If no onLoginCompletionGoToPage passed through, then just stay on the same page, removing the login box and modal status
		else
			//$find('modalLoginBehavior').hide();
			location.reload();
   }
   else
   {
	   // Display the error message that lets them know of the server-side login error
	   document.getElementById("failureText").style.display = 'block';
   }
}

// A sub-function of submitModalLoginForm(). If some sort of server error disrupts the AJAX, this function will execute
function onError(result, userContext, methodName)
{
   alert("There was an error while trying to login");
   window.location = "/Login.aspx";
}

/***********************
    Comments Control
************************/
var linkMsgs = new Array();

linkMsgs["OpenComment"] = "Post";
linkMsgs["CloseComment"] = "Hide";
linkMsgs["OpenReply"] = "Post a Reply";
linkMsgs["CloseReply"] = "Hide Reply Message";
linkMsgs["OpenReport"] = "Report this Comment";
linkMsgs["CloseReport"] = "Hide this report comment";

function ClearComment(panel, textbox, validator, link, mode) 
{
    
    // Check to find out if the link say close/hide, then we know that the panel is open and we just need to close it since they don't want to send a reply message
    if($get(link).innerHTML == linkMsgs["CloseReply"] || $get(link).innerHTML == linkMsgs["CloseComment"] || $get(link).innerHTML == linkMsgs["CloseReport"])
       $get(panel).style.display = "none";
    else 
        $get(panel).style.display = "block";  
    
    // Set the form elements back to form state
    $get(textbox).value = "";
    $get(validator).style.display = "none";
    
    try { $get("postthanksyoumsg").innerHTML = ""; } catch (ex) { }
    
    if(mode.toLowerCase() == "comment")
        $get(link).innerHTML = ($get(link).innerHTML == linkMsgs["OpenComment"])?linkMsgs["CloseComment"]:linkMsgs["OpenComment"];
    else if(mode.toLowerCase() == "reply")
        $get(link).innerHTML = ($get(link).innerHTML == linkMsgs["OpenReply"])?linkMsgs["CloseReply"]:linkMsgs["OpenReply"];
    else if(mode.toLowerCase() == "report")
        $get(link).innerHTML = ($get(link).innerHTML == linkMsgs["OpenReport"])?linkMsgs["CloseReport"]:linkMsgs["OpenReport"];
    
}

function DeleteComment()
{
    
    if(confirm("Are you sure that you would like to delete this comment?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	return false;
    
}

/***********************
    News
************************/

function deleteNews() 
{
	
	if(confirm("Are you sure that you would like to delete this news item?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	
	return false;
	
}

function deleteNewsImg() 
{
	
	if(confirm("Are you sure that you would like to delete this news image?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	
	return false;
	
}

function OpenArchiveMonth(div)
{
   
   if($get(div).style.display == "" || $get(div).style.display == "none")
    $get(div).style.display = "block";
   else
    $get(div).style.display = "none";
    
}

/***********************
    Faq
************************/

function deleteFaq() 
{
	
	if(confirm("Are you sure that you would like to delete this faq?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	
	return false;
	
}

/***********************
    Bookmark
************************/

function deleteBookmark() 
{
	if(confirm("Are you sure that you would like to remove this favourite video?"))
		return true;
	
	// For IE
	try { window.event.returnValue = false; } catch (Exception) { }
	
	return false;
}

/***********************
    Video
************************/

function deleteVideo() 
{
	if(confirm("Are you sure that you would like to delete this video?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	
	return false;
}

function SetVideoToBeEncoded()
{
    
    if(confirm("Have you encoded the video and created a screenshot for this video.\n\nWould you like to continue?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	return false;
    
}

/***********************
    Fans & Friends
************************/

function deleteFanOrFriend(type) 
{
	if(confirm("Are you sure that you would like to delete this " + type + "?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	return false;
}

/***********************
    Upload Photo
************************/

var uploadButtons = 2;
function AddAnotherButton()
{    
    if(uploadButtons < 4)
    {
        $get("photo" + uploadButtons).style.display = "block";
        uploadButtons++;
    }
    
    if(uploadButtons == 4)
        $get("addMorePhotosLink").style.display = "none";
}

function deleteImage() 
{
	
	if(confirm("Are you sure that you would like to delete this image?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	
	return false;
	
}

/***********************
    User
************************/

function deleteUser() 
{
	
	if(confirm("Are you sure that you would like to delete this user?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	
	return false;
	
}

/***********************
    Bad Word
************************/

function DeleteBadWord()
{
    
    if(confirm("Are you sure that you would like to delete this word?"))
		return true;
	
	// stops IE from submitting the form
	try { window.event.returnValue = false; } catch (Exception) { }
	return false;
    
}

function MyVideoRedirect(username, videoid)
{
    window.location = "/MyVideos.aspx?username=" + username + "&id=" + videoid;
}

/***********************
    Password Generation
************************/

function random_char(charlist)
{
	var num = Math.floor(Math.random() * charlist.length);
	return charlist.charAt(num);
}

function random_pass()
{
	var length = 8;
	var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

	var pass = "";
	for (var i = 0; i < length; i++)
		pass = pass + random_char(chars);
		
	return pass;
}

/***********************
    Video Rating
************************/

function CustomRating(RatingID)
{
  
  for(var i = 0; i < 5; i++)
  {
      switch(i)
      {
        case 0:
            SetRatingMsg(RatingID + "_Star_" + (i + 1).toString(), "I'm Not Laughing");
            break;
        case 1:
            SetRatingMsg(RatingID + "_Star_" + (i + 1).toString(), "Not the Worst");
            break;
        case 2:
            SetRatingMsg(RatingID + "_Star_" + (i + 1).toString(), "Not Bad :D");
            break;
        case 3:
            SetRatingMsg(RatingID + "_Star_" + (i + 1).toString(), "Pretty Funny");
            break;
        case 4:
            SetRatingMsg(RatingID + "_Star_" + (i + 1).toString(), "Brilliant!");
            break;
        default:
            break;
      }
      
  }
  
}

function SetRatingMsg(element, msg)
{
    document.getElementById(element).onmouseover = function () { 
        document.getElementById("divRatingMsg").innerHTML = msg;
    }
    document.getElementById(element).onmouseout = function () { 
        document.getElementById("divRatingMsg").innerHTML = "&nbsp;";
    }
    document.getElementById(element).title = "Click to vote!";
}

/***********************
    Select Text inside Div tag
************************/

function selectText(e)
{
    if(typeof(e) == 'string')
       e = document.getElementById(e);
    if(!e) return;
            
    // ie version
    if (document.selection) 
    {
        var r = document.body.createTextRange();
        r.moveToElementText(e);
        r.setEndPoint("EndToEnd", r);
        r.moveStart('character', 0);
        r.select();
    }
    
    // firefox version
    else if(window.getSelection)
    {
        var s = window.getSelection();
        var r = document.createRange();
        r.setStartBefore(e);
        r.setEndAfter(e);
        s.addRange(r);
    }
}

function fbs_click() 
{
    var u = location.href;
    var t = document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
}

/***********************
    UTILITIES
************************/

// Function to make sure new onload functions are successfully added to any previous ones
function addOnLoadEvent( func )
{
	var oldOnLoad = window.onload;
	if( typeof window.onload != 'function' )
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if( oldOnLoad )
			{
				oldOnLoad();
			}
			func();
		}
	}
}

