
var AUTH_COOKIE_NAME = "VEOH_GUIDE_AUTH";

function getAuthToken()
{
	return getCookie( AUTH_COOKIE_NAME );
}

// toggle visibility 
function toggleVisibility(targetId) {
    if (document.getElementById) {
        target = document.getElementById(targetId);
    	if (target.style.visibility == "hidden"){
    		target.style.visibility = "visible";
    	} else {
    		target.style.visibility = "hidden";
    	}
    }
}

function submitForm ( formObj )
{
  formObj.submit();
}

function checkAll(theForm) { // check all the checkboxes in the list
  for (var i=0;i<theForm.elements.length;i++) {
    var e = theForm.elements[i];
		var eName = e.name;
    	if (eName != 'allbox' && 
            (e.type.indexOf("checkbox") == 0)) {
        	e.checked = theForm.allbox.checked;		
		}
	} 
}

/*  This function is to select all options in a multi-valued <select> */
function selectAll(elementId) {
    var element = document.getElementById(elementId);
	len = element.length;
	if (len != 0) {
		for (i = 0; i < len; i++) {
			element.options[i].selected = true;
		}
	}
}

/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix) 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) 
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) 
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// This function is for stripping leading and trailing spaces
function trim(str) { 
    if (str != null) {
        var i; 
        for (i=0; i<str.length; i++) {
            if (str.charAt(i)!=" ") {
                str=str.substring(i,str.length); 
                break;
            } 
        } 
    
        for (i=str.length-1; i>=0; i--) {
            if (str.charAt(i)!=" ") {
                str=str.substring(0,i+1); 
                break;
            } 
        } 
        
        if (str.charAt(0)==" ") {
            return ""; 
        } else {
            return str; 
        }
    }
} 

function confirmDelete(obj) {   
    var msg = "Are you sure you want to delete this " + obj + "?";
    ans = confirm(msg);
    if (ans) {
        return true;
    } else {
        return false;
    }
}

function disableSubmit(submit)
{
	frm.submit.disabled = 1;
}

/*
 Next two functions are for selecting permalink text
*/
function selectText( strField )
{
	document.getElementById( strField ).select();
}

function getKey(e, textId)
{
	var evtObj;
	
	if(window.event)
	{
		evtObj = window.event;
	}
	else
	{
		evtObj = e;
	}
	
	//If Crtl+C is pressed allow it
	if(evtObj.keyCode == 67 || evtObj.keyCode == 99 || evtObj.keyCode == 84 
		|| evtObj.keyCode == 116 || evtObj.keyCode == 78 || evtObj.keyCode == 110)
	{
		if(evtObj.ctrlKey)
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

function confirmLeave( link )
{
	answer = confirm( "WARNING: Your are now leaving Veoh. Are you sure you want to leave?" );
	
	if( answer != 0 )
	{
		window.location.href = link;
	}
}

// Show the document's title on the status bar
window.defaultStatus=document.title;


var IFrameObj; // our IFrame object
function callToServer( url ) 
{
	if( !document.createElement ) 
	{
		return true
	};

	var IFrameDoc;
	if( !IFrameObj && document.createElement ) 
	{
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// callToServer() is called
		try 
		{
			var tempIFrame=document.createElement( 'iframe' );
			tempIFrame.setAttribute( 'id', 'RSIFrame' );
			tempIFrame.style.border = '0px';
			tempIFrame.style.width = '0px';
			tempIFrame.style.height = '0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if( document.frames ) 
			{
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames[ 'RSIFrame' ];
			}
		}catch( exception ) 
		{
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML = '\<iframe id="RSIFrame" style="';
			iframeHTML += 'border:0px;';
			iframeHTML += 'width:0px;';
			iframeHTML += 'height:0px;';
			iframeHTML += '"><\/iframe>';
			
			document.body.innerHTML += iframeHTML;
			
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			
			IFrameObj.document.location.replace = function(location) 
			{
				this.iframe.src = location;
			}
		}
	}
  
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) 
	{
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('callToServer( ' + url + ' )',10);
		return false;
	}
  
  	try
  	{
		if( IFrameObj.contentDocument ) 
		{
			// For NS6
			IFrameDoc = IFrameObj.contentDocument; 
		}
		else if( IFrameObj.contentWindow ) 
		{
			// For IE5.5 and IE6
			IFrameDoc = IFrameObj.contentWindow.document;
		} 
		else if( IFrameObj.document ) 
		{
			// For IE5
			IFrameDoc = IFrameObj.document;
		} 
		else 
		{
			return true;
		}
	}catch( exception )
	{
	}
	
	try
	{
		IFrameDoc.location.replace( url );
	}catch( exception )
	{
	}
	
	return false;
}



/* Breaks apart long lines with no spaces so that they wrap according
   to the 'wrapWidth' onto multiple lines */
function wrapText(wrapWidth, elementId) {
	if (document.getElementById(elementId)) {
		var textToFix = document.getElementById(elementId).innerHTML;
		if (textToFix != '' && textToFix != null) { // is not an empty block of text
			var correctedText = '';
			var flag = false;
			var test = '';
			lines = Math.floor(textToFix.length / wrapWidth);
		
			for (var i=0; i < lines; i++) {
				test = textToFix.substring(0, wrapWidth);
				for (var k=0; k < test.length; k++) {
					if(test.charAt(k) == ' ') {
						flag = true;	// line contains a space
						break;
					}
				}
				
				if (flag) {
					correctedText += textToFix.substring(0, k+1);
					temp = textToFix.substring(k+1, textToFix.length);
					textToFix = temp;
				}
				else {	
					for (var j=0; j < wrapWidth; j++) {
						correctedText += textToFix.charAt(j);
					}
					correctedText += '\n';
					temp = textToFix.substring(wrapWidth, textToFix.length);
					textToFix = temp;
				}
				
				flag = false;
			}
			correctedText += textToFix;
			document.getElementById(elementId).innerHTML = correctedText;
		}
	}
}

function checkIfEmailBad(theString)
{
	var regexPattern = /([a-zA-Z0-9_-]+)\@([a-zA-Z0-9_-]+)\.(([a-zA-Z0-9_-]+)(?![;,\n]))/;
	
	//If it doesn't fit the pattern it was bad
	if(!regexPattern.test(theString))
	{
		return true;
	}
	
// Else it was good
return false;
}


function checkEnter(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable
	
	if(e && e.which)
	{ 	//if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{		
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13)
	{ 
		return true;
	}
	else
	{
		return false;
	}	

}



// Drop Down Menus

function showDrop(id) {
	var d = document.getElementById(id);
	if (d) {
		d.style.display='block';
	}
}

function hideDrop(id) {
	var d = document.getElementById(id);
	if (d) {
		d.style.display='none';
	}
}



function showSMenu(i) {
	var mainID = 'topnav_' + i;
	var mainE = document.getElementById(mainID);
	if (mainE) {
		newID = mainID + 'hover';
		mainE.id = newID;
	}
	showDrop(i + '_drop');
}

function hideSMenu(i) {
	var mainID = 'topnav_' + i;
	var mainE = document.getElementById(mainID + 'hover');
	if (mainE) {
		mainE.id = mainID;
	}
	hideDrop(i + '_drop');
}

// toggle visibility for search options
function toggleSearch(targetId) {
    if (document.getElementById(targetId)) {
        target = document.getElementById(targetId);
		targetList = target.getElementsByTagName('li');
		for (i=0; i < targetList.length; i++) {
			if (targetList[i].style.display == "none"){
				targetList[i].style.display = "block";
			} else {
				targetList[i].style.display = "none";
			}
		}		
    }
}
function switchSearch(id, type) {
	switch(type)
	{  
	case 's':
		setSearchTypeCookie('s');
		document.getElementById(id).type.value='s';
		if(document.getElementById(id).query.value == 'Search Videos' || 
			document.getElementById(id).query.value == 'Search Channels' || 
			document.getElementById(id).query.value == 'Search People' ||
			document.getElementById(id).query.value == '')
		{
			document.getElementById(id).query.value = 'Search Series'; 
		}
		else{
			selectText('query')
		}
		
		document.getElementById('searchVideos').className='';
		document.getElementById('searchSeries').className='selected';
		document.getElementById('searchChannels').className='';
		document.getElementById('searchPeople').className='';
		break
	case 'c':
		setSearchTypeCookie('c');
		document.getElementById(id).type.value='c';
		if(document.getElementById(id).query.value == 'Search Videos' || 
			document.getElementById(id).query.value == 'Search Series' || 
			document.getElementById(id).query.value == 'Search People' ||
			document.getElementById(id).query.value == '')
		{
			document.getElementById(id).query.value = 'Search Channels'; 
		}
		else{
			selectText('query')
		}
		
		document.getElementById('searchVideos').className='';
		document.getElementById('searchSeries').className='';
		document.getElementById('searchChannels').className='selected';
		document.getElementById('searchPeople').className='';
		break
	case 'u':
		setSearchTypeCookie('u');
		document.getElementById(id).type.value='u';
		if(document.getElementById(id).query.value == 'Search Videos' || 
			document.getElementById(id).query.value == 'Search Series' || 
			document.getElementById(id).query.value == 'Search Channels' ||
			document.getElementById(id).query.value == '')
		{
			document.getElementById(id).query.value = 'Search People'; 
		}
		else{
			selectText('query')
		}
		
		document.getElementById('searchVideos').className='';
		document.getElementById('searchSeries').className='';
		document.getElementById('searchChannels').className='';
		document.getElementById('searchPeople').className='selected';
		break
	default:
		setSearchTypeCookie('');
		document.getElementById(id).type.value='';
		if(document.getElementById(id).query.value == 'Search Series' || 
			document.getElementById(id).query.value == 'Search Channels' || 
			document.getElementById(id).query.value == 'Search People' ||
			document.getElementById(id).query.value == '')
		{
			document.getElementById(id).query.value = 'Search Videos'; 
		}
		else{
			selectText('query')
		}
	
		document.getElementById('searchVideos').className='selected';
		document.getElementById('searchSeries').className='';
		document.getElementById('searchChannels').className='';
		document.getElementById('searchPeople').className='';
		break  
	}
}

function fieldClear(id) {
	if(document.getElementById(id).query.value == 'Search Videos' || 
		document.getElementById(id).query.value == 'Search Series' || 
		document.getElementById(id).query.value == 'Search Channels' || 
		document.getElementById(id).query.value == 'Search People' )
	{
		document.getElementById(id).query.value = ''; 
	}
	else{
		selectText('query')
	}
}

/* Facebook - general*/
function sendToFacebook() {
	u=location.href;
	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');
	void(0);
}