var isIE = 0;

if (document.all)
{
  isIE = 1;
}

/*

Pop permission dialog if given permission not allowed

Used by: cmpPublish2.php

*/

var isIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;



/*

  Detect and deal with keystrokes

*/



function isset(varname){
  return(typeof(window[varname])!='undefined');
}

function fireOnclick(objID)
{
var target=document.getElementById(objID);
if(document.dispatchEvent) { // W3C
    var oEvent = document.createEvent( "MouseEvents" );
    oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target);
    target.dispatchEvent( oEvent );
    }
else if(document.fireEvent) { // IE
    target.fireEvent("onclick");
    }   
}


function bubbleSort(arr, key)
{
  var x, y, holder;
  // The Bubble Sort method.
  for(x = 0; x < arr.length; x++) {
    for(y = 0; y < (arr.length-1); y++) {
      if (eval(arr[y][key]) > eval(arr[y+1][key])) {
//      alert('swap: ' + arr[y][key] + ' for ' + arr[y+1][key]);
        holder = arr[y+1];
        arr[y+1] = arr[y];
        arr[y] = holder;
      }
    }
  }

  return arr;
}

function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = obj.constructor(); // changed

    for(var key in obj)
        temp[key] = clone(obj[key]);
    return temp;
}


function arrayContains(arryA, val)
{
  for (var i = 0; i < arryA.length; i++)
  {
    if (arryA[i] == val)
    {
      return true;
    }
  }
  return false;
}


function trim(str)
{
  return str.replace(/^\s+|\s+$/g,"");
}

/*

Getter/setters: checkbox

*/

function getRand(max)
{
  return Math.floor(Math.random() * max);
}


function checkBoxToBit(chk)
{
  if (chk.checked) { return 1; }
  return 0;
}

function bitToCheckbox(chk, bit)
{
  if (bit)
  {
    chk.checked = true;
    return;
  }
  chk.checked = false;
}

/*

Getter/setters: select box

*/
  
function getSelectedOption(selectbox)
{
  return selectbox[selectbox.selectedIndex].value;
}

function setSelectedOption(selectbox, num)
{
  selectbox.selectedIndex = num;
}

function addOption(selectbox,text,value )
{
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  selectbox.options.add(optn);
}

function selOption(in_element, in_value)
{
  var list = document.getElementById(in_element);
  if (list && list.options.length)
  {
    for(var i=0; i<list.options.length; i++)
    {
      if(list.options[i].value == in_value)
      {
        list.selectedIndex = i;
        return;
      }
    }
  }
}

this.Sleep = function ZZzzzZZzzzzzzZZZz(naptime){
  naptime = naptime;
  var sleeping = true;
  var now = new Date();
  var alarm;
  var startingMSeconds = now.getTime();
  while(sleeping){
     alarm = new Date();
     alarmMSeconds = alarm.getTime();
     if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
  }      
}
   
/*

Getter/setters: radio

*/
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

   
   
function isIE()
{
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
   

var isDebug = 1;

function arrayDump(arr,level)
{
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += arrayDump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
  alert('[[[[[[[[[\n' + dumped_text + '\n]]]]]]]]]');
}

function debugOut(str)
{
  if (isDebug)
  {
    alert('[[[[[[[[[\n' + str + '\n]]]]]]]]]');
  }
}

/*

GreyBox show.  Requires parent document have: gbCloseCancel, gbCloseOK
Don't forget: proxomitron interferes with greybox!

*/

function getBetween(Str, strLeft, strRight)
{
  var strLeftPos = Str.indexOf(strLeft);
  if (strLeftPos == -1) { return ""; }

  var Tmp = Str.substr(strLeftPos + strLeft.length);
  var strRightPos = Tmp.indexOf(strRight);
  if (strRightPos == -1) { return ""; }

  return Tmp.substr(0, strRightPos);
}

function setBetween(str, replace, strLeft, strRight)
{
  var orig = getBetween(str, strLeft, strRight);
  
  var full = strLeft + orig + strRight;
  replace = strLeft + replace + strRight;
  str = str.replace(full, replace);
  return str;
}

function getTimestamp()
{
  var currentTime = new Date();
  return (currentTime.getMonth() + 1) + '/' + currentTime.getDate() + '/' + (currentTime.getYear() + 1900) + " " + currentTime.getHours() + ":" + currentTime.getMinutes();
}

function goURL(url)
{
  window.location = url;
}

function changeClass (elementID, newClass)
{
	var element = document.getElementById(elementID);
	
	element.setAttribute("class", newClass); //For Most Browsers
	element.setAttribute("className", newClass); //For IE; harmless to other browsers.
}

function launchHelpURL(url)
{
  var heiMax = 600;

  var heiMinus = screen.height - 100;
  if (heiMinus < 600) { heiMax = heiMinus; }

  showContainer('/help/help.php?url=' + url, "help", 770, heiMax);
}

function launchHelp(keyword)
{
  var heiMax = 600;

  var heiMinus = screen.height - 100;
  if (heiMinus < 600) { heiMax = heiMinus; }

  showContainer('http://www.musicdnacentral.com/help/help.php?title=' + keyword, "help", 770, heiMax);
}

function showContainer(url, nam, wid, hei)
{

  childWindow = open(url, nam, 'scrollbars=yes,resizable=yes,width=' + wid + ",height=" + hei);
//alert(wid + ',' + hei);
  if (childWindow.opener == null)
  {
    childWindow.opener = self;
  }
}

function gbShow(caption, url, height, width)
{
  GB_showCenter(caption, url, height, width);
}

function goHelp(url)
{
  //gbShow('', url, curHeight - 80, curWidth - 128);
  var title = url;

  var rnd = Math.floor(Math.random() * 10000);
  if (url.match(/\?/))
  {
    url += '&';
  }
  else
  {
    url += '?';
  }
  url += "rnd=" + rnd
  
  GB_show(title, url, curHeight - 80, curWidth - 40);
  return false;

}

function goHidden(url)
{
  var rnd = Math.floor(Math.random() * 10000);
  var fd = document.getElementById('iframeHidden');
  fd.src = url + "&rnd=" + rnd;
}

/*

Required by: greybox

*/

var curHeight = -1;
var curWidth = -1;

getSizeBrowser();

function getSizeBrowser()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  curHeight = myHeight;
  curWidth = myWidth;
}

var myHeight = -1;
function resizeBrowser()
{
  getSizeBrowser();
  
  var h = myHeight;
  //if (h != curHeight)
  //{
    //var cc = document.getElementById('ifEditCode');
    //cc.height = h - 52;
  //}
  curHeight = h;
}