

//// New Window in middle of screen ////

var win             = null;
var preventNavOn    = false;

function newWindow(mypage,myname,w,h,scroll)
{
    LeftPosition = (screen.width) ? (screen.width-w)/4 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/4 : 0;

    if (w != "" && h != "")
	{
        settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
    }
	else
	{
        settings = 'scrollbars='+scroll+',resizable';
    }

    win = window.open(mypage,myname,settings);
}


//// Inventory Inquiry Validation ////

function sendEmail() {
					
	var d = document.frmSS;
	if (d.Email.value.length <= 0)
	{
		alert('Please enter your EMAIL ADDRESS.');
		d.Email.focus();
	}
	else if (d.Email.value.indexOf('@') <= 0 || d.Email.value.indexOf('.') <= 0)
	{
		alert('Please enter a valid EMAIL ADDRESS (an \"@\" AND \".\" are needed).\n(ex: go@virb.com)');
		d.Email.focus();
	}
	else
	{
		d.emailsubmit.disabled=true;
		d.submit();
	}

}


//// Pulsating SAVE CHANGES button ////

function pulsateSave(id) {
	
	if (document.getElementById(id)) {
		if (document.getElementById(id).src != '/_graphics/_backend/btn_save_changes_ani.gif')
		{
			document.getElementById(id).src = '/_graphics/_backend/btn_save_changes_ani.gif';
		}
	} 
	
	if( preventNavOn == true )
	    preventNav();

}

function preventNav()
{
    window.onbeforeunload = function(evt) { 
	    e = (window.event) ? window.event : evt;
        msg = 'If so, your changes will be lost.';
        e.returnValue = msg;
    
        return msg;
    }
}

function clearSaveState()
{
    window.onbeforeunload = null;
}

function checkSaveState()
{
    theInputs = document.getElementsByTagName('input');
    
    for (var i = 0; i < theInputs.length; i++)
    {
        if (theInputs[i].src.match('_graphics/_backend/btn_save_changes_ani.gif'))
        {
            if (confirm('You have made changes to this page.  Are you sure you want to proceed without saving?'))
            {
                return true;
            }               
            else
            {
                return false;
            }
        }
    }
    
    return true;
}    

function getFormElements(id) {
	
	inputs      = document.getElementsByTagName('input');
	textareas   = document.getElementsByTagName('textarea');
	selectboxes = document.getElementsByTagName('select');
	
	for (var i=0; i<inputs.length; i++)
	{
		var th_input = inputs[i];
		th_input.onchange = function () { alert('hello') };
	}
}


//// SHOW A HIDDEN LAYER ////

function toggleLayer(theshown,thehidden,thehidden2,signup,thestyle)
{
	var d = document;

	if (!thestyle)
	{
		thestyle = 'block';
	}
	
	if (theshown!='')
	{
		if (d.getElementById(theshown).style.display=='none' || (d.getElementById(theshown).style.display == '' && thestyle != 'table-row'))
		{
			if (thestyle == 'table-row')
			{
                d.getElementById(theshown).style.display = '';			    
			}
			else
			{
                d.getElementById(theshown).style.display = thestyle;   
			}
		}
		else
		{
			if (!signup)
			{
				d.getElementById(theshown).style.display = 'none';
			}
		}
	}
	
	if (thehidden)
	{
		d.getElementById(thehidden).style.display = 'none';
	}
	
	return false;

	if (thehidden2)
	{
		d.getElementById(thehidden2).style.display = 'none';
	}
}

function toggleLayerSimple (element, style)
{
    var e = document.getElementById(element);
    
    if( !style )
    {
        style = '';
    }
    
    if( e )
    {
        if( e.style.display == 'none' ) 
            e.style.display = style;
        else
            e.style.display = 'none';
    }
}


//// CATEGORY OPTIONS ////

function showOptions(theshown)
{
	document.getElementById(theshown).style.display = 'inline';
}

function hideOptions(thehidden)
{
	document.getElementById(thehidden).style.display = 'none';
}


//// MAKE TEXT DARK ////

function SignupFocus(whichId,insertText)
{
    if (document.getElementById(whichId))
    {
        var d = document.getElementById(whichId);

        if (d.value==insertText)
        {
            d.style.color='#333333';
            d.value='';
        }
    }
}

//// RESET TEXT - MAKE IT LIGHT ////

function SignupBlur(whichId,insertText,color)
{
    if (document.getElementById(whichId))
    {
        var d = document.getElementById(whichId);

        if (d.value=='')
        {
            if (!color)
            {
                d.style.color='#aaaaaa';
            }
            d.value=insertText;
        }
    }
}


//// CITY / POSTAL CODE SWITCHER ////

function showPostal_or_City()
{
	if (document.getElementById('choose_country'))
	{
		var country = document.getElementById('choose_country').options[document.getElementById('choose_country').selectedIndex].value;
	
		if (country == '235' || country == '43')
		{
			if (document.getElementById('postal_code').style.display != 'none' || document.getElementById('postal_code').style.display != '')
			{
				toggleLayer('postal_code','city','','true');
			}
		}
		else if (country == '' || country == 0)
		{
			toggleLayer('','postal_code','city');
		}
		else
		{
			if (document.getElementById('city').style.display == 'none' || document.getElementById('city').style.display == '')
			{
				toggleLayer('city','postal_code');
			}		
		}
	}
}


//// PREVIEW A URL (replace spaces with null) ////

var tagTimeout;

function previewURL(signupType,whichId,exception)
{   
    window.clearTimeout(tagTimeout);
    tagTimeout = window.setTimeout("previewURLTimeout('" + signupType + "', '" + whichId + "', '" + exception + "')", 500);
/*        setTimeout(previewURLTimeout(signupType,whichId,exception), 3000);*/

    if (document.getElementById('loading_image'))
    {
        document.getElementById('loading_image').style.display = 'block';
    }
}

function previewURLTimeout(signupType,whichId,exception)
{
    if (document.getElementById('preview_url').innerHTML != exception)
	{
	    x_urlAvailability(document.getElementById(whichId).value.replace(/(\n|\r|' '|[^a-zA-Z0-9_])/g,'').toLowerCase(), signupType, urlAvailability_cb);
	}
}

function urlAvailability_cb(result)
{
    document.getElementById('preview_url').innerHTML = result;
    
    if (document.getElementById('loading_image'))
    {
        document.getElementById('loading_image').style.display = 'none';
    }
}

function resetValues(thelink)
{
	document.getElementById('preview_url').innerHTML = 'http://virb.com/<strong>'+ thelink +'</strong>';
	document.getElementById('link').value = thelink;
}


//// INPUT FIELD SWITCHER ////

var originalText = "";

function textHide(label,edit)
{
	if (!edit)
	{
		originalText = label.innerHTML;
		inputField = label.parentNode.getElementsByTagName("input");
		divTag = label.parentNode.getElementsByTagName("div");
		label.style.display = "none";
		 
        label.parentNode.className = 'cat_title_extend'; 

		if (divTag[1])
		{
    		divTag[1].style.display = "none";
    	}

		if (divTag[2])
		{
    		divTag[2].style.display = "none";
    	}
	}
	else
	{
		inputField = label.parentNode.parentNode.getElementsByTagName("input");
		h3Tag = label.parentNode.parentNode.getElementsByTagName("h3");
		divTag = label.parentNode.parentNode.getElementsByTagName("div");
		originalText = h3Tag[0].innerHTML;
		h3Tag[0].style.display = "none";
		
        label.parentNode.parentNode.className = 'cat_title_extend'; 
        
		if (divTag[1])
		{
    		divTag[1].style.display = "none";
    	}

		if (divTag[2])
		{
    		divTag[2].style.display = "none";
    	}
	}
	
	if (!inputField[0]) 
	{
		alert('this is not a input field');
			
	}
	
	for (var i = 0; i < inputField.length; i++)
	{
		if (i == 0)
		{
			inputField[i].style.display = "block";
		}
		else
		{
			inputField[i].style.display = "inline";
		}
		
		if (inputField[i].type == "text")
		{
			inputField[i].size = inputField[i].value.length;
		}
	}
	
	inputField[0].focus();
}

function inputHide(inputField,cancel) 
{
	if (inputField.value != '')
	{
        textValue = inputField.parentNode.getElementsByTagName("h3");
        divTag = inputField.parentNode.getElementsByTagName("div");

        if (!cancel)
        {
            textValue[0].innerHTML = inputField.value;
        }
        else
        {
            textValue[0].innerHTML = originalText;
            inputText = inputField.parentNode.getElementsByTagName("input");
            inputText[0].value = originalText;
        }

        inputArray = inputField.parentNode.getElementsByTagName("input");
        for (var i=0; i<inputArray.length; i++)
        {
	        inputArray[i].style.display = "none";
        }
        
	   textValue[0].style.display = "block";
	   divTag[1].style.display    = "block";
       divTag[2].style.display    = "block";
       inputField.parentNode.className = 'cat_title'; 
	}
}

function textareaControl(textarea) 
{
	charLength = textarea.value.length;
	rowLength = Math.ceil(charLength/40);
	textarea.rows = rowLength;	
}


///// Choose Account Type /////

function accountChooseInit()
{
	var rows = document.getElementById('choose_account').getElementsByTagName('input');
	for ( var i=0; i < rows.length; i++ )
	{
		rows[i].onclick = function()
		{
			document.getElementById('display_name_text').innerHTML = this.parentNode.title;
			this.parentNode.getElementsByTagName('input')[0].checked = 'true';
			swapBars(this.parentNode.id.slice(5));
			
			if (this.title == 'Display Name')
			{
				document.getElementById('personal_info').style.display = 'block';
				document.getElementById('name_info').style.display = 'block';
                
                if (document.getElementById('music_org'))
                {
                    document.getElementById('music_org').style.display = 'none';
                }
            }
			else
			{
				document.getElementById('personal_info').style.display = 'none';
				document.getElementById('name_info').style.display = 'none';

                if (document.getElementById('music_org'))
                {
                    document.getElementById('music_org').style.display = 'block';
                }
			}
			
			// document.getElementById('music_org_input').checked = false;
		}
	}
	
	var types = document.getElementById('choose_account').getElementsByTagName('strong');
	for ( var i=0; i < types.length; i++ )
	{
		types[i].onclick = function()
        {
			document.getElementById('display_name_text').innerHTML = this.parentNode.title;
			this.parentNode.getElementsByTagName('input')[0].checked = 'true';
			swapBars(this.parentNode.id.slice(5));

			if (this.parentNode.id.slice(5) == 'personal_on')
			{
				document.getElementById('personal_info').style.display = 'block';
				document.getElementById('name_info').style.display = 'block';

				if (document.getElementById('music_org'))
				{
					document.getElementById('music_org').style.display = 'none';
				}
			}
			else
			{
				document.getElementById('personal_info').style.display = 'none';
				document.getElementById('name_info').style.display = 'none';

				if (document.getElementById('music_org'))
				{
					document.getElementById('music_org').style.display = 'block';
				}
			}
			return false;
		}
	}
}

function swapAccount(accountType)
{
    document.getElementById('account_type_'+ accountType).checked = 'true';
   
    if (accountType == 'personal')
	{
		document.getElementById('personal_info').style.display = 'block';
        document.getElementById('name_info').style.display = 'block';

        if (document.getElementById('music_org'))
        {
            document.getElementById('music_org').style.display = 'none';
        }
    }
    else
	{
		document.getElementById('personal_info').style.display = 'none';
		document.getElementById('name_info').style.display = 'none';

        if (document.getElementById('music_org'))
        {
            document.getElementById('music_org').style.display = 'block';
        }
	}
	
	// document.getElementById('music_org_input').checked = false;    
}

function swapBars(accountType)
{
	accountType = accountType.replace(/_on/, '');
	var bars = document.getElementById('choose_account').getElementsByTagName('li');
	bars[0].id = 'type_personal';
	bars[2].id = 'type_music';
	bars[4].id = 'type_music_org';
	bars[6].id = 'type_org';
	document.getElementById('type_' + accountType).id = 'type_' + accountType + '_on';
}


///// Table Row Highlights /////

var marked_row = new Array;

function highlightRowsInit()
{
	var rows = document.getElementsByTagName('tr');
	for ( var i=0; i < rows.length; i++ )
	{
		// Highlight row on mouseover...
		rows[i].onmouseover = function()
		{
            this.className += ' tr_hover';
        }
		rows[i].onmouseout = function()
		{
            this.className = this.className.replace( ' tr_hover', '' );
        }

		// Do not set click events if not wanted
        if (rows[i].className.search(/noclick/) != -1) {
            continue;
        }

		// Mark the row on click ...
        rows[i].onmousedown = function()
		{
            var unique_id;
            var checkbox;

            checkbox = this.getElementsByTagName( 'input' )[0];
            if ( checkbox && checkbox.type == 'checkbox' )
			{
                unique_id = checkbox.name + checkbox.value;
            }
			else if ( this.id.length > 0 )
			{
                unique_id = this.id;
            }
			else
			{
                return;
            }

            if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] )
			{
                marked_row[unique_id] = true;
            }
			else
			{
                marked_row[unique_id] = false;
            }

            if ( marked_row[unique_id] )
			{
                this.className += ' tr_marked';
            }
			else
			{
                this.className = this.className.replace(' tr_marked', '');
            }

            if ( checkbox && checkbox.disabled == false )
			{
                checkbox.checked = marked_row[unique_id];
            }
        }
		// .. and checkbox clicks
        var checkbox = rows[i].getElementsByTagName('input')[0];
        if ( checkbox )
		{
            checkbox.onclick = function()
			{
                this.checked = ! this.checked;
            }
        }
	}
}

function deleteCategory(albumId)
{
    albumContents = document.getElementById('album_' + albumId).getElementsByTagName('li');
    
    if(albumContents.length > 0)
    {
        alert('You must remove everything in this album before you can delete it')
    }
    else
    {
        if (confirm('Are you sure you want to delete this album?'))
        {
            document.getElementById('albumAction').value   = 'deleteCategory';
            document.getElementById('albumId').value       = albumId;
            document.albumsForm.submit();
        }        
    }
}


function editCategory(albumId,albumName)
{
    document.getElementById('albumAction').value  = 'editCategory';
    document.getElementById('albumId').value      = albumId;
    document.getElementById('albumName').value    = document.getElementById(albumName).value;
    document.albumsForm.submit();
}

var resizeId          = '';	 
var resizeContainerId = '';

function resizeFlash(height, width)
{
    var strippedHeight = height.substring(0,(height.length - 2));
        
    if (document.getElementById('disclaimer_wrapper') && strippedHeight < 5)
    {
        document.getElementById('disclaimer_wrapper').style.display = 'block';        
    }
    
    if(height != '')
    {
        // For IE so the Flash Movie resizes properly
        document.getElementById(resizeId).style.height = height;

        document.getElementById(resizeContainerId).style.marginBottom = '30px';
        document.getElementById(resizeContainerId).style.height = height;
    }   
    
    if(width)
    {   
/*        var strippedWidth = width.substring(0,(width.length - 2));
        
        if (strippedWidth < 430)
        {
            width = '430px';
        }
*/        
        document.getElementById(resizeId).style.width          = width;
        document.getElementById(resizeContainerId).style.width = width;        
    }
}       

function displayBrowse(browseType, elementId, containerId) 
{
    if (document.getElementById('accept_disclaimer').checked == false)
    {
        alert('You must agree to the terms of uploading media content before continuing');

        return false;
    }
    else
    {
        if (document.getElementById('disclaimer_wrapper'))
        {
            document.getElementById('disclaimer_wrapper').style.display = 'none';
        }
        resizeId          = elementId;	 
        resizeContainerId = containerId;

        if (navigator.appName.indexOf("Microsoft") != -1) 
        {
            flash = window[elementId];
        }
        else 
        {
            flash = window.document[elementId];
        }

        var flashInfo = flash[browseType]();        
    }
}

var initialUrl  = '';
var prevElement = '';

function cancelLink(element)
{    
    if (initialUrl == '')
    {    
        initialUrl = element.href;
    }
    else if (prevElement != element && prevElement.href != '')
    {
        prevElement.href = initialUrl;
        initialUrl       = element.href;
        prevElement      = element;
    }
    
    if (dragging == true)
    {
        element.href = 'javascript:;';
        dragging     = false;
        prevElement  = element;
    }
    else
    {
        if (element.href != initialUrl)
        {
            element.href = initialUrl;
        }
    }
}

function findPosition( oLink ) 
{
    if( oLink.offsetParent ) 
    {
        for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) 
        {
            posX += oLink.offsetLeft;
            posY += oLink.offsetTop;
        }

        return [ posX, posY ];
    } 
    else
    {
        return [ oLink.x, oLink.y ];
    }
}

function stripDoubleQuotes(str)
{
    str = str.replace(/\"/g, '\'');
    
    return str;
}

function addSlashes(str) 
{
    str = str.replace(/\'/g,'\\\'');
/*  str = str.replace(/\"/g,'\\\"');
    str = str.replace(/\\/g,'\\\\');
    str = str.replace(/\0/g,'\\0');
*/    
    return str;
}

function stripSlashes(str) 
{
    str = str.replace(/\\'/g,'\'');
    str = str.replace(/\\"/g,'"');
    str = str.replace(/\\\\/g,'\\');
    str = str.replace(/\\0/g,'\0');
    
    return str;
}

function checkFileExtension(fileType, fileName)
{
    var imageExtensions = new Array('.jpg', '.gif', '.png', '.jpeg');
    var videoExtensions = new Array('.flv', '.avi', '.mov', '.mpg', '.mpeg', '.mp4', '.m4v', '.wmv', '.asf');
    var audioExtensions = new Array('.mp3');
    
    if (document.getElementById('disclaimer_wrapper'))
    {
        document.getElementById('disclaimer_wrapper').style.display = 'none';
    }
    
    switch (fileType)
    {
        case 'image':
            for (var i = 0; i < imageExtensions.length; i++)
            {
                if(fileName.substr((fileName.length-imageExtensions[i].length), imageExtensions[i].length).toLowerCase() == imageExtensions[i])
                {
                    document.getElementById('file_data').innerHTML                 = fileName;
                    document.getElementById('file_data_wrap').style.display        = 'block';
                    document.getElementById('submit_single_file').style.display    = 'block';
                    document.getElementById('uploading_graphic').style.visibility  = 'hidden';
                    document.getElementById('submit_single_file').style.display    = 'block'; 
                    document.getElementById('submit_single_file').style.visibility = ''; 
                                        
                    return true;
                }
            }   
            
            break
        case 'video':
            for (var i = 0; i < videoExtensions.length; i++)
            {   
                if(fileName.substr((fileName.length - videoExtensions[i].length), videoExtensions[i].length).toLowerCase() == videoExtensions[i])
                {   
                    document.getElementById('file_data').innerHTML                 = fileName;
                    document.getElementById('file_data_wrap').style.display        = 'block';
                    document.getElementById('submit_single_file').style.display    = 'block';
                    document.getElementById('uploading_graphic').style.visibility  = 'hidden';
                    document.getElementById('submit_single_file').style.display    = 'block'; 
                    document.getElementById('submit_single_file').style.visibility = ''; 
                                                                
                    return true;
                }                
            }   
            
            break
        case 'audio':
            for (var i = 0; i < audioExtensions.length; i++)
            {
                if(fileName.substr((fileName.length-audioExtensions[i].length), audioExtensions[i].length).toLowerCase() == audioExtensions[i])
                {
                    if (document.getElementById('file_data'))
                    {
                        document.getElementById('file_data').innerHTML                 = fileName;
                    }
                    document.getElementById('file_data_wrap').style.display        = 'block';
                    document.getElementById('submit_single_file').style.display    = 'block';
                    if (document.getElementById('uploading_graphic'))
                    {
                        document.getElementById('uploading_graphic').style.visibility  = 'hidden';                        
                    }
                    document.getElementById('submit_single_file').style.display    = 'block'; 
                    document.getElementById('submit_single_file').style.visibility = ''; 
                                        
                    return true;
                }                
            }   
            
            break
        default:
            
    }
    
    splitFileName = fileName.split('.');
    
    if (document.getElementById('file_data'))
    {
        document.getElementById('file_data').innerHTML = 'The extension .'+splitFileName[(splitFileName.length - 1)]+' is not an acceptable file type.';        
    }

    if (document.getElementById('file_data'))
    {
        document.getElementById('file_data').innerHTML = '<input type="file" name="file_upload_btn" value="" onchange="checkFileExtension(\'audio\', this.value);" /> The extension .'+splitFileName[(splitFileName.length - 1)]+' is not an acceptable file type.';        
    }

    if (document.getElementById('file_data_wrap'))
    {
        document.getElementById('file_data_wrap').style.display     = 'block';
    }
    
    if (document.getElementById('submit_single_file'))
    {
        document.getElementById('submit_single_file').style.display = 'none';        
    }
                                                                             
    if (document.getElementById('uploading_graphic'))
    {
        document.getElementById('uploading_graphic').style.visibility = 'hidden';   
    }
    
    return false;
}



function nonFlashDisclaimer()
{
    if (document.getElementById('accept_disclaimer').checked == false)
    {
        if (confirm('By uploading this file you are certifying that you have the right to distribute all images/videos/audio that you upload and that they are not nudity, gore or depicting hate.'))
        {
            if (document.getElementById('uploading_graphic'))
            {
                document.getElementById('uploading_graphic').style.visibility  = '';
            }
            
            /*                    document.getElementById('uploading_graphic').style.display = 'block';*/
            document.file_upload_form.submit();

            return true;
        }
        else
        {
            return false;   
        }
    }
    else
    {
        if (document.getElementById('uploading_graphic'))
        {
            document.getElementById('uploading_graphic').style.visibility  = '';
        }

        /*        document.getElementById('uploading_graphic').style.display = 'block';*/
        document.file_upload_form.submit();   
        
        return true;
    }
}

function characterCount(textarea, countField, limit)
{
    if (textarea.value.length > limit)
    {
        // if too long...trim it!
        //textarea.value = textarea.value.substring(0, limit);
        document.getElementById(countField).innerHTML = textarea.value.length;
        document.getElementById(countField).style.color = '#cc0000';
    }
    else
    {
        // otherwise, update 'characters left' counter
        document.getElementById(countField).innerHTML = textarea.value.length;
        document.getElementById(countField).style.color = '#444444';        
    }
}