var helpWin
var bResize = true
var audio = null
var audioFile

ie = document.all?1:0

try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

function getRef(divID)
{
    if( document.layers ) { //Netscape layers
        return document.layers[divID]; }
    if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
        return document.getElementById(divID); }
    if( document.all ) { //Proprietary DOM; IE4
        return document.all[divID]; }
    if( document[divID] ) { //Netscape alternative
        return document[divID]; }
    return false;
}

function SetupKeys()
{
	getRef( "ctl00_CPH_passwordEditor_tbPassword" ).value = getRef( "xfmp66" ).value;
	getRef( "ctl00_CPH_passwordEditor_tbConfirmPassword" ).value = getRef( "d67jkl" ).value;
}

function SetHiddenField( fieldName, val )
{
	field = document.getElementsByName( fieldName )[ 0 ];
	if ( field )
		field.value = val;
}

function SetFocus(obj)
{
	if ( obj != null )
	{
		var setFocus = true;
		var sobj = obj;
		while ( sobj != null )
		{
			if ( sobj.style.display == "none" )
			{
				//alert("hidden");
				setFocus = false;
				break;
			}
			sobj = sobj.parentElement;
		}
	
		if ( setFocus )
		{
			//alert("set");
			obj.focus();
			if ( obj.createTextRange )
			{
				var range = obj.createTextRange();
				range.move("textedit");
				range.select();
			}
		}
	}
}

function insertText( text )
{
	var obj = getRef( "message" );
	obj.value = obj.value + text;
	obj.focus();
}

function insertCode( start, end, middle )
{
	var obj = getRef( "message" );
	obj.value = obj.value + start + middle + end;
	obj.focus();
}

function insertInto( el, start, end ) 
{ 
    if ( el.setSelectionRange )
    { 
        el.focus(); 
        var pos = el.selectionStart + start.length;
        el.value = el.value.substring(0,el.selectionStart) + start + el.value.substring(el.selectionStart,el.selectionEnd) + end + el.value.substring(el.selectionEnd,el.value.length); 
        el.setSelectionRange(pos,pos);
    } 
    else if (document.selection && document.selection.createRange) 
    { 
        el.focus(); 
        var range = document.selection.createRange(); 
        var pos = range.text.length - end.length - range.text.length;
        range.text = start + range.text + end; 
        //range.collapse(true);
        range.moveStart('character', pos);
        range.moveEnd('character', pos);
        range.select();    
    } 
}

function insertTabText( text )
{
    insertInto( document.getElementById( "ctl00_CPH_tbSource" ), text, "" );
	return false;
}

function insertTabCode( start, end )
{
    insertInto( document.getElementById( "ctl00_CPH_tbSource" ), start, end );
	return false;
}

function checkEnter( e, link )
{ 
	var characterCode, source;

	if ( e && e.which )
	{ 
		link = actionLink;
		characterCode = e.which;
		source = e.target;
	}
	else
	{
		characterCode = event.keyCode;
		source = event.srcElement;
	}

	if ( characterCode == 13 )
	{ 
		StoreScrollPos();
		__doPostBack( link, '' );
		return false ;
	}
	
	return true;
}

function StoreScrollPos()
{
	SetHiddenField( "scroll", document.body.scrollTop );
}

function getobjPageTop(oObject) 
{
	var y, obj;
	y = 0;
  
	obj = oObject;
	while (obj.offsetParent != null) 
	{
		y += obj.offsetTop;
		obj = obj.offsetParent;
    }
    y += obj.offsetTop;
    return y;
}

function getobjPageLeft(oObject) 
{
	var x, obj;
	x = 0;
  
	obj = oObject;
	while (obj.offsetParent != null) 
	{
		x += obj.offsetLeft;
		obj = obj.offsetParent;
    }
    x += obj.offsetTop;
    return x;
}

var stage = 0;
var fade = 102;
var fadein = false;

function SetFadeColor( obj, c )
{
	var rgbCol = "rgb(" + c + "," + c + "," + c + ")";
	obj.style.color = rgbCol;
	for ( var i = 0; i < obj.childNodes.length; i++ )
	{
		if ( obj.childNodes[ i ].nodeType == 1 )
			obj.childNodes[ i ].style.color = rgbCol;
	}
}

function StartInfo()
{
	var info = document.getElementById("infoSpan");
	info.innerHTML = newsList[ stage ];
	stage++;
	window.setTimeout( 'UpdateInfo()', 5000 );
}

function UpdateInfo()
{
	var info = document.getElementById("infoSpan");
	
	if ( fadein )
	{
		fade--;
		SetFadeColor( info, fade );
		
		if ( fade == 102 )
		{
			fadein = false;
			window.setTimeout( 'UpdateInfo()', 5000 );
		}
		else
			window.setTimeout( 'UpdateInfo()', 10 );
	}
	else
	{
		fade++;
		SetFadeColor( info, fade );
		
		if ( fade == 247)
		{
			info.visibility = "none";
			info.innerHTML = newsList[ stage ];
			SetFadeColor( info, fade );
			info.visibility = "visible";
			stage++;
			if ( stage == newsList.length )
				stage = 0;
				
			window.setTimeout( 'UpdateInfo()', 250 );
			fadein = true;
		}
		else
			window.setTimeout( 'UpdateInfo()', 10 );
	}
}

//DHTML Window script- Copyright Dynamic Drive (http://www.dynamicdrive.com)
//For full source code, documentation, and terms of usage,
//Visit http://www.dynamicdrive.com/dynamicindex9/dhtmlwindow.htm

var dragapproved=false
var minrestore=0
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all
var popupWindow
var sequence = 0;

function iecompattest(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function drag_drop(e)
{
	if (ie5&&dragapproved&&event.button==1)
	{
		popupWindow.style.left=tempx+event.clientX-offsetx+"px"
		popupWindow.style.top=tempy+event.clientY-offsety+"px"
	}
	else if (ns6&&dragapproved)
	{
		popupWindow.style.left=tempx+e.clientX-offsetx+"px"
		popupWindow.style.top=tempy+e.clientY-offsety+"px"
	}
	return false;
}

function stopdrag()
{
	dragapproved=false;
	document.onmousemove=null;
	document.onmouseup=null;
	//document.getElementById("popupContent").style.display="" //extra
}

function initializedrag(e)
{
	offsetx=ie5? event.clientX : e.clientX
	offsety=ie5? event.clientY : e.clientY
	//document.getElementById("popupContent").style.display="none" //extra
	tempx=parseInt(popupWindow.style.left)
	tempy=parseInt(popupWindow.style.top)

	dragapproved=true
	document.onmousemove=drag_drop
	document.onmouseup=stopdrag
}

function DisplayPopup(title,url,width,height)
{
	if (!ie5&&!ns6)
		window.open(url,"","width="+width+",height="+height+",status=no,scrollbars=no,toolbar=no,resizable=no,location=no")
	else
	{
		popupWindow = document.getElementById("popupWindow")
		popupWindow.style.width=(width+2)+"px"
		popupWindow.style.height=(height+20)+"px"
		document.getElementById("popupTitleLeft").innerHTML = title;
		if ( popupWindow.style.display != 'block' )
		{
			//alert(document.body.clientWidth);
			popupWindow.style.left = ( document.body.clientWidth - width - 30 ) + "px"
			popupWindow.style.top=ns6? window.pageYOffset*1+130+"px" : iecompattest().scrollTop*1+130+"px"
		}
		var frame = getRef( "cframe" );
		var innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document; 
		innerDoc.location.replace( url+"&seq="+sequence );
		
		//document.getElementById("cframe").location.replace( url+"&seq="+sequence );
		sequence++;
		popupWindow.style.display='block'
	}
}

function XmlHttp() {}

XmlHttp.Create = function ()
{
	try
	{
		if (window.XMLHttpRequest)
		{
			var req = new XMLHttpRequest();

			//	Ensure that the readyState is supported
			if (req.readyState == null)
			{
				req.readyState = 1;
				req.addEventListener("load", function () {
					req.readyState = 4;
					if (typeof req.onreadystatechange == "function")
						req.onreadystatechange();
				}, false);
			}
			
			return req;
		}

		if (window.ActiveXObject)
		{
			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) {}
	// fell through
	throw new Error("Your browser does not support XmlHttp objects");
};

function getXmlHttpPrefix()
{
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var tempObject;
	for (var i = 0; i < prefixes.length; i++)
	{
		try
		{
			tempObject = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

function LoadHTML(href,target)
{
	var xmlHttp = XmlHttp.Create();
	
	try
	{
		var html = GetHTML( href );			
		//alert(target);
		getRef( target ).innerHTML = html;
			
	}
	catch (e)
	{
		document.body.innerHTML = href + "<br/>" + e.message + "<br><br>" + xmlHttp.responseText;
	}
	
	return null;
}

function GetHTML( href )
{
	var xmlHttp = XmlHttp.Create();
	
	try
	{
		//alert(href);
		xmlHttp.open("GET", href, false);
		xmlHttp.send(null);
		
		var html = xmlHttp.responseText;
		//alert(html);
		if ( html == null )
			html = "";
			
		return html;			
	}
	catch (e)
	{
		document.body.innerHTML = href + "<br/>" + e.message + "<br><br>" + xmlHttp.responseText;
	}
	
	return null;
}

function LoadHTMLAsync(href,target)
{
	var xmlHttp = XmlHttp.Create();
	
	/*try
	{*/
		//alert(href);
		xmlHttp.open("GET", href, true);
		alert(1);
		xmlhttp.onreadystatechange=function() 
		{
		alert(11);
			if (xmlhttp.readyState==4) 
			{
				alert(xmlhttp.responseText)

				/*var html = xmlHttp.responseText;
				//alert(html);
				if ( html == null )
					html = "";
					
				//alert(target);
				getRef( target ).innerHTML = html;*/
			}
		}	
		alert(2);
		xmlHttp.send(null);
		alert(3);
		
	/*}
	catch (e)
	{
		document.body.innerHTML = href + "<br/>" + e.message + "<br><br>" + xmlHttp.responseText;
	}*/
	
	return null;
}


function PlayAudio( fileName, src )
{
	StopAudio();
	getRef( src + "_play" ).src = "../images/load.gif" 
	audio = src;
	audioFile = fileName;
	window.setTimeout( 'LoadAudio()', 0 );
	
}

function LoadAudio()
{
	LoadHTML( "../Music/Play.aspx?file=" + audioFile, "audioPlayer" );
	getRef( audio + "_play" ).style.display = "none";
	getRef( audio + "_play" ).src = "../images/play.gif" 
	getRef( audio + "_stop" ).style.display = "block";
}

function StopAudio()
{
	if ( audio != null )
	{
		getRef( audio + "_stop" ).style.display = "none";
		getRef( audio + "_play" ).style.display = "block";
		playerid.stop();
	}
}

function setOpacity( object, opacity ) 
{
	object.style.opacity = (opacity / 100);
	object.style.MozOpacity = (opacity / 100);
	object.style.KhtmlOpacity = (opacity / 100);
	object.style.filter = "alpha(opacity=" + opacity + ")";
}

function ShowFile( href, target )
{
	/*var html = GetHTML( href );
	alert( html );
	obj = getRef( target );
	obj.innerHTML = "<div class='pageTop'></div><div class='pageMiddle'>" + 
		html + "</div><div class='pageBottom'></div>";*/
	getRef( target ).src = href;
	
}

function trace( msg )
{
	var obj = getRef( "debug" );
	obj.innerHTML = msg + "<br>" + obj.innerHTML;
}

var currentPopup;
var currentPopupBack;

function PopupDrop(e)
{
	if (ie5&&dragapproved&&event.button==1)
	{
		currentPopup.style.left=tempx+event.clientX-offsetx+"px"
		currentPopup.style.top=tempy+event.clientY-offsety+"px"
	}
	else if (ns6&&dragapproved)
	{
		currentPopup.style.left=tempx+e.clientX-offsetx+"px"
		currentPopup.style.top=tempy+e.clientY-offsety+"px"
	}
	return false;
}

function PopupStop()
{
	dragapproved=false;
	document.onmousemove=null;
	document.onmouseup=null;
}

function PopupDrag(e)
{
	offsetx=ie5? event.clientX : e.clientX
	offsety=ie5? event.clientY : e.clientY
	tempx=parseInt(currentPopup.style.left)
	tempy=parseInt(currentPopup.style.top)

	dragapproved=true
	document.onmousemove=PopupDrop
	document.onmouseup=PopupStop
}

function PopupCreate( target, id, style, width, height, title, subtitle, content )
{
	var size = "style='width: " + width + "px'";
	if ( height != null )
		size += "; height: " + height + "px'";
		
	getRef( target ).innerHTML = "<div id='" + id + "' class='popup" + style + "' " + size + " onSelectStart='return false'>"
		+ "<div class='popup" + style + "Title' onMousedown='PopupDrag(event)'><div class='popup" + style + "Close' onclick='PopupClose()'><img src='../images/close2_lo.gif' border='0' onmouseover=\"this.src='../images/close2_hi.gif'\" onmouseout=\"this.src='../images/close2_lo.gif'\"></div><div id='popupTitle'><div id='mainTitle'>" + title + "</div><div id='subTitle'>" + subtitle + "</div></div></div>"
		+ "<div class='popup" + style + "Content'>" + content + "</div></div>";
	//alert(getRef( target ).innerHTML);
	currentPopup = getRef( id );
	return currentPopup;
}

function PopupCreateEx( target, id, width, height, content )
{
	getRef( target ).innerHTML = "<div id='" + id + "' class='popupEx' sxtyle='width: " + width + "px; xheight: " + height + "px'>" + content + "</div>";
	currentPopup = getRef( id );
    currentPopup.style.display = "block";
    
    currentPopupBack = getRef( 'popupBack' );
    if ( currentPopupBack != null )
        currentPopupBack.style.display = "block";
        
    PopupCentre( currentPopup, width, height );
    
	return currentPopup;
}

function PopupTitle( title, subtitle )
{
	getRef( 'mainTitle' ).innerHTML = title;
	getRef( 'subTitle' ).innerHTML = subtitle;
}

function PopupClose()
{
	currentPopup.style.display = "none";
    if ( currentPopupBack != null )
	    currentPopupBack.style.display = "none";
}

function PopupCentre( popup, w, h )
{
	var width = 0, height = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		width = window.innerWidth;
		height = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		//IE 4 compatible
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	
	var scrollX = 0, scrollY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) 
	{
		//Netscape compliant
		scrollY = window.pageYOffset;
		scrollX = window.pageXOffset;
	} 
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
	{
		//DOM compliant
		scrollY = document.body.scrollTop;
		scrollX = document.body.scrollLeft;
	} 
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
	{
		//IE6 standards compliant mode
		scrollY = document.documentElement.scrollTop;
		scrollX = document.documentElement.scrollLeft;
	}	

	//alert(width+" "+height+" "+scrollX+" "+scrollY);
	//alert(popup.offsetWidth+" "+popup.offsetHeight);
	
	w = popup.offsetWidth;
	h = popup.offsetHeight;
	
	if ( popup.style.display == "block" )
	{
		//if ( popup.offsetLeft - scrollX < 0 || popup.offsetTop - scrollY < 0 || popup.offsetLeft - scrollX + w > width || popup.offsetTop - scrollY + h > height )
		{
			popup.style.left = scrollX + ( ( width - w ) / 2 ) + 'px';
			popup.style.top = scrollY + ( ( height - h ) / 2 ) + 'px';
		}
	}
	else
	{
		popup.style.left = scrollX + ( ( width - w ) / 2 );
		popup.style.top = scrollY + ( ( height - h ) / 2 );
	}
}

function PageQuery(q) 
{
	if(q.length > 1) 
		this.q = q.substring(1, q.length);
	else 
		this.q = null;
	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	
	this.getKeyValuePairs = function() 
	{ 
		return this.keyValuePairs; 
	}
	
	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}

	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() 
	{ 
		return this.keyValuePairs.length; 
	} 
}

function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

function setOption( id, val )
{
	var select = getRef( id );
	if ( select == null )
	    return;
	
	//alert(id+","+val);
	var len = select.options.length;
	for ( var i = 0; i < len; i++ ) 
	{
		if ( select.options[ i ].value == val ) 
		{
			select.options[ i ].selected = true;
			break;
		}
	}
}

function setOptionFromQuery( id, val, def )
{
	var query = queryString( val );
	if ( query == 'false' ) 
		query = def;

    setOption( id, query );
}

function setEditFromQuery( id, val, def )
{
	var query = queryString( val );
	if ( query == 'false' ) 
		query = def;

	var edit = getRef( id );
	edit.value = query;
}

var blip = 0;
var blipControl;

function BlipLink( id )
{
	blipControl = getRef( id );
	if ( blip == 0 )
		window.setTimeout( 'UpdateBlip()', 50 );
}

function UpdateBlip()
{
	blip++;
	if ( blip % 2 == 1 )
		blipControl.className = "taskBlip";
	else
		blipControl.className = "task";
		
	if ( blip == 4 )
		blip = 0;
	else
		window.setTimeout( 'UpdateBlip()', 1000 );
}

//
// Guitar Tab Section
//

function ViewTab( id, print, transpose, chord )
{
    if ( id == -1 )
	    id = queryString( "tab" );
	var instrumentId = getRef( "ctl00_CPH_options_selInstrument" );
	var transposeId = getRef( "ctl00_CPH_options_selTranspose" );
	var instrument = instrumentId.options[ instrumentId.selectedIndex ].value;
	
	if ( transpose == null )
	{
	    if ( transposeId != null )
	        transpose = transposeId.options[ transposeId.selectedIndex ].value;
	}
	
	var url = "ViewTab.aspx";
	var group = queryString( "group" );
	if ( group != "" )
	    url += "?group=" + group + "&";
	else
	    url += "&"
	url += "tab=" + id + "&i=" + instrument;
	if ( transpose != null )
	    url += "&t=" + transpose;
	if ( print )
	    url += "&p=1";
	if ( chord )
	    url += "&chord=" + queryString( "chord" );
	    
    //alert(url);
	location.href = url;
	return false;
}

function EasyKey( key )
{
    ViewTab( -1, false, key );
}

function SetTabSettings( instrument, transpose )
{
    /*if ( instrument == -1 )
	    setOptionFromQuery( "ctl00_CPH_options_selInstrument", "i", 0 );
	else*/
	    setOption( "ctl00_CPH_options_selInstrument", instrument );
    /*if ( transpose == -99 )
	    setOptionFromQuery( "ctl00_CPH_options_selTranspose", "t", 0 );
	else*/
	    setOption( "ctl00_CPH_options_selTranspose", transpose );
}

function ConfirmTabRemove()
{
    return confirm( "Are you sure you want to remove this tab from this virtual group?\n\nIMPORTANT NOTE: This will NOT delete the tab but simply remove it from this group" );
}

var alertIcon = null;
var alertSavedIcon = null;
var alertTimer = 0;

function AlertBox( id, msg )
{
    try
    {
        getRef( id + "Message" ).innerHTML = msg;
        alertIcon = getRef( id + "Icon" );
        getRef( id + "Title" ).style.display = "block";
        getRef( id + "Body" ).style.display = "block";
        if ( alertTimer == 0 )
        {
            alertIconSaved = alertIcon.src;
            alertTimer++;
	        window.setTimeout( 'AlertBoxFlash("' + id + '")', 500 );
	    }
	}
	catch( err )
	{
	    alert( "Error in AlertBox: " + err.message );
	}
}

function AlertBoxHide( id )
{
    getRef( id + "Title" ).style.display = "none";
    getRef( id + "Body" ).style.display = "none";
}

function AlertBoxFlash( id )
{
    if ( alertTimer % 2 == 1 )
    {
        getRef( id + "Message" ).style.color = "#ccc";
        alertIcon.src = "../images/blank.gif";
    }
    else
    {
        getRef( id + "Message" ).style.color = "#666";
        alertIcon.src = alertIconSaved;
    }
    
    alertTimer++;
    if ( alertTimer <= 6 )
	    window.setTimeout( 'AlertBoxFlash("' + id + '")', 500 );
	else
	    alertTimer = 0;
}
