
function fdump() { }


function ajaxObject() 
{
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
      try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHttp;
}

/* --START functions for "search" -- */
function fn_toggleArtistCheck(artist_id,division_id)
{
  var xmlHttp = ajaxObject();
  xmlHttp.onreadystatechange=function()
    {
      if(xmlHttp.readyState==4)
      {
        var resp = xmlHttp.responseText.split(':')
        if (resp[1]=='on') {
          document.getElementById('aSelected'+resp[0]).style.visibility='visible';
        } 
        else {
          document.getElementById('aSelected'+resp[0]).style.visibility='hidden';
        }
      }
    }
  xmlHttp.open("GET","toggleArtist?artist_id="+artist_id+'&division_id='+division_id,true);
  xmlHttp.send(null);
}
/* --END function sfor "search" -- */


/* --START functions for "artist/view" -- */
function fn_artistPreloadImages() {
	document.aArtistImages = [];
	for (i=0; i < document.artist_URLs.length; i++) {
		var img = new Image();
		img.src = document.artist_URLs[i];
		img.name = 'artist_image';
		document.aArtistImages.push(img);
	}
	document.iCurrentArtistImage = 0;
}
function fn_toggleField(id,show) {
	 if (show) {document.getElementById(id).style.removeProperty('display')}
	 else {document.getElementById(id).style.setProperty('display','none',null)}
}

function fn_artistChangeImage(change) 
{
	var isExtreme = (document.iCurrentArtistImage == 0)? 1 : (document.iCurrentArtistImage == document.aArtistImages.length-1)? -1 : 0;
	var iTarget;
	if (isExtreme*change == -1) { // Moving before beginning or past end
		iTarget = (change < 0)? document.aArtistImages.length-1 : 0;
	} else {
		iTarget = document.iCurrentArtistImage+change;
	}
	oParent = document.getElementById('avImage')
	oImg = fn_getContainedTagByName(oParent,'img')

	if (oImg.name != 'artist_image') {alert ("ERROR: Couldn't find artist image to replace.")
	} else {
		oImg.src = document.aArtistImages[iTarget].src;
                oImg.setAttribute('photoid',document.artist_IDs[iTarget]);
		document.iCurrentArtistImage = iTarget;
		document.getElementById('avImageIndex').firstChild.data = (iTarget+1)+''		
	}
}
/* --END functions for "artist view" -- */


function fn_addClassesById(sId,aClasses) {
	var oTarget = document.getElementById(sId);
	fn_addClassesFromObject(oTarget,aClasses);
}
function fn_addClassesToObject(oTarget,aArgs) {
	try {var aClass = oTarget.className.split(' ');}
	catch(e){var aClass=[];}
	//var aArgs = fn_argumentsToArray(arguments).splice(1);
	//alert("Classes to add: "+aArgs+"\nExisting classes: "+aClass+"\nArguments: "+arguments);
	for (iAdd in aArgs) {
		//alert("iAdd :" +iAdd+"\n");
		if (typeof(aArgs[iAdd]) == 'string') aClass.push(aArgs[iAdd]);
	}
	//alert("Totalling classes: "+aClass);
	oTarget.className = aClass.join(' ')+'';
}
function fn_removeClassesById(sId,aClasses) {
	var oTarget = document.getElementById(sId);
	fn_removeClassesFromObject(oTarget,aClasses);
}
function fn_removeClassesFromObject(oTarget,aArgs) {
	try	{
		var aClass = oTarget.className.split(' ');
	}
	catch(e) {
		var aClass=[];
	}
	//var aArgs = fn_argumentsToArray(arguments).splice(1);
	for (iRemove in aArgs) {
		if (aClass.getIndex(aArgs[iRemove]) >= 0) {
			delete aClass[aClass.getIndex(aArgs[iRemove])]
		}
	}
	if (aArgs.length < 1) {
		aClass=[];
	}
	oTarget.className = aClass.join(' ')+'';
}
/*function fn_argumentsToArray(args) { //Doesn't work in Opera
	var aResult = []
	alert("arguments: "+arguments+"\narguments.length :"+arguments.length);
	for (var i = 0; i<=args.length-1;++i) {
		aResult.push(args[i]);
	}
	return aResult;
}*/
function fn_setPropertyById(sId,property,val) {
	var oTarget = document.getElementById(sId);
	var sProp = oTarget[property];
	oTarget[property] = val;
	//DEBUG:
	//alert("setPropertyById('"+sId+"','"+property+"','"+val+"'\nProperty before: "+sProp+"\nResult: "+oTarget[property]);
}
/* Array.getIndex augumentation by Mick White */
Array.prototype.getIndex=function(val){
	for(var i=0;i<this.length;i++){
		if(this[i]==val) return i;
	}
	return -1;
}
function fn_dumpObject(obj) {
	var aOutput = [];
	for (var a in obj) {
		val = new String(obj[a])
		aOutput.push(a+": "+val.slice(0,30));
	}
	alert(aOutput.join('\n'));
}

function fn_getContainedTagByName(oParent,sNodeName) {
	var aChildren = oParent.childNodes;
	//alert(oParent.nodeName+" has "+oParent.childNodes.length+ " children: "+aChildren[0]);
	for (var iC = 0; iC < aChildren.length; iC++){ //(iC in aChildren) {
		//alert(iC+" : "+aChildren[iC]+ " ("aChildren[iC].nodeName.toLowerCase()+")\n");
		//if (iC != 'length') {
		//alert(" -> "+toString(aChildren[iC]));//.nodeName.toLowerCase()+"\n");
		//fn_dumpObj(aChildren[iC]);
		if (aChildren[iC].nodeName.toLowerCase() == sNodeName) {
			//dump("FOUND: "+aChildren[iC].nodeName);
			return aChildren[iC];
		} else {
			var branchResult = fn_getContainedTagByName(aChildren[iC],sNodeName);
			if (branchResult) {
			return branchResult;}
		} 
	}
	return null;
}

function popup(url){
  features = 'toolbar=0,scrollbars=1,resizable=1, width=500,height=400';
  window.open(url, "popup", features);
}

function popup_chpw(url){
  features = 'toolbar=0,scrollbars=1, width=200,height=200';
  window.open(url, "password", features);
}

function fn_newWin(url,toolbar){
  features = 'toolbar='+toolbar+',scrollbars=1,resizable=1';
  window.open(url, "browser_window", features);
}

var popupi = 1;
function fn_popup(mypage, w, h, scroll) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=1';
  win = window.open(mypage, "popup"+popupi, winprops);
  popupi = popupi + 1;
//  if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function fn_switchTab(hide,show,index) {
  document.getElementsByName('tab')[0].value=index;
  document.getElementById(hide).style.display='none';
  document.getElementById(show).style.display='block';
}

function fn_smsCounter(field,cntfield,tcntfield,maxlimit) {
  if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
  else // otherwise, update 'characters left' counter
    cntfield.value = field.value.length;
  if (field.value.length >= 160)
    tcntfield.value = 2;
  else
    tcntfield.value = 1;
}

function fn_cloneAndInsertBefore(source_id,insert_id) {
	var o_source = document.getElementById(source_id);
	var o_insert = document.getElementById(insert_id);
	var o_clone = o_source.cloneNode(true);
	o_insert.parentNode.insertBefore(o_clone,o_insert);
}

function fn_cutBranchAtParentNodeName(o_child,s_parentNodeName) {
	
	var oToRemove = o_child;
	do {
		oToRemove = oToRemove.parentNode;
	} while (oToRemove.nodeName.toLowerCase() != s_parentNodeName)
	var oRemoved = oToRemove.parentNode.removeChild(oToRemove);
	delete oRemoved;
}

/* --END General useful functions -- */
/* ----------------------------------- */

function fn_activateEditor()
{
    document.getElementById('userContent').style.visibility = 'hidden';
    document.getElementById('userEditor').style.visibility = 'visible';
}

function fn_closeEditor()
{
    document.getElementById('userContent').style.visibility = 'visible';
    document.getElementById('userEditor').style.visibility = 'hidden';    
}

