var isAll=(document.all) ? true : false;
var isLay=(document.layers) ? true : false;
var isbyId=(document.getElementById && !document.all) ? true : false;


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function openAssetViewer(arrangementId,selectedAssetId,assetTypeId){
	var url = 'index.cfm?event=admin.manage.asset.viewer&arrangementId=' + arrangementId + '&selectedAssetId=' + selectedAssetId + '&assetTypeId=' + assetTypeId;
	var title = 'assetViewer';
	var args = 'width=600,height=400,top=100,left=100,resizable,scrollbars';
	openWindow(url,title,args)
}

function onSubmitDisable(oForm,sSubmitId){
	var oSubmitEl = 0;
	if(oForm[sSubmitId]){
		oSubmitEl = oForm[sSubmitId];
		oForm[sSubmitId].disabled = true;
	}
}

function openWindow(url,title,args){
	/*
	alwaysLowered (n)
	alwaysRaised (n)
	dependent (n)
	directories
	height
	hotkeys (n)
	innerHeight (n)
	innerWidth (n)
	location
	menubar
	outerHeight (n)
	outerWidth (n)
	resizable
	screenX (n)
	screenY (n)
	scrollbars
	status
	titlebar (n)
	toolbar
	width
	z-lock (n)
	*/
	window.open(url,title,args)
}

// needs support for GetElementById()
function enlarge(el){
	var thumbImg = returnDomElement(el);
	var mainImg = returnDomElement('gridMain');
	var thumbImgSrc = thumbImg.src;
	var mainImgSrc = mainImg.src;
	mainImg.src = thumbImg.src.replace('thumb','main');
}

function confirmSubmit(message){
	return confirm(message)
}

function returnDomElement(ref){
	if(ref && isObject(ref)){
		return ref;
	}else{
		if(isAll){
			if(document.all[ref]){
				return document.all[ref];
			}else{
				return false;
			}
		}else if(isLay){
			if(document.layers[ref]){
				return document.layers[ref];
			}else{
				return false;
			}
		}else if(isbyId){
			if(document.getElementById([ref])){
				return document.getElementById([ref]);
			}else{
				return false;
			}
		}
	}
}


function hideLayer(ref){
	var oLayerRef=returnDomElement(ref)
	if(isAll && oLayerRef){
		oLayerRef.style.visibility='hidden';
	}
	if(isLay && oLayerRef){
		oLayerRef.visibility='hide';
	}
	if(isbyId && oLayerRef){
		oLayerRef.style.display='none';
	}
	return false;
}


function showLayer(ref){
	var oLayerRef=returnDomElement(ref)
	if(isAll && oLayerRef){
		oLayerRef.style.visibility='visible';
	}
	if(isLay && oLayerRef){
		oLayerRef.visibility='show';
	}
	if(isbyId && oLayerRef){
		oLayerRef.style.display='block';
	}
	return false;
}


function writeToLayer(ref,sTxt){
	var oLayerRef=returnDomElement(ref)
	if(isAll && oLayerRef){
		oLayerRef.innerHTML=sTxt;
	}
	if(isLay && oLayerRef){
		oLayerRef.document.write(sTxt);
		oLayerRef.document.close();
	}
	if(isbyId && oLayerRef){
		var oRange=document.createRange();
		var oDomFrag=oRange.createContextualFragment(sTxt);
		oRange.setStartBefore(oLayerRef);
		while (oLayerRef.hasChildNodes()){
			oLayerRef.removeChild(oLayerRef.lastChild);
		}
		oLayerRef.appendChild(oDomFrag);
  	}
	return false;
}


function selectTextRange(ref){
	var oLayerRef=returnDomElement(ref);
	if(oLayerRef.createTextRange!=null){
		var oTxtRange=oLayerRef.createTextRange();
		oTxtRange.select();
	}
	return false;
}


function addToFavorite(){
	if(window.external){
		window.external.AddFavorite(location.href,document.title);
	}else{
		// maybe tell the user that this doesn't work
		// alert('')
		return false;
	}
}

function getUrl(url){
	window.location.href = url;
}

function clearField(ref,sDefaultValue){
	var oLayerRef=returnDomElement(ref);
	var sValue=oLayerRef.value;
	if(oLayerRef && sValue==sDefaultValue){
		oLayerRef.value="";
		selectTextRange(oLayerRef);
	}else{
		selectTextRange(oLayerRef);
	}
	return false;
}


function changeBgColour(){
	if(isLay || isAll){
		with(changeBgColour){
			// Loop through all arguments, two at a time.
			for(i=0;i<(arguments.length-1);i+=2){
				if(isLay){
					returnDomElement(arguments[i]).document.bgColor=arguments[i+1];
				}else if(isAll){
					returnDomElement(arguments[i]).style.backgroundColor=arguments[i+1];
				}
			}
		}
	}
}


function autoFormFocus(nForm){
	if(!nForm){
		nForm=0;
	}
	if(document.forms[nForm]){
		var nCount;
		var aFormElements=document.forms[nForm].elements;
		aFormElements[nForm].focus();
	}else{
		return false;
	}
}


function swapImage(ref,sSrc){
	var oLayerRef=returnDomElement(ref);
	if(oLayerRef){
		oLayerRef.src=sSrc;
	}
	return false;
}


function urlEncode(sString){
	return escape(sString);
}


function urlDecode(sString){
	return unescape(sString);
}


function isArray(a){
    return isObject(a) && a.constructor==Array;
}


function isBoolean(a){
    return typeof a=='boolean';
}


function trim(sString){
	while(sString.substring(0,1) == ' '){
		sString = sString.substring(1,sString.length);
	}
	while(sString.substring(sString.length-1,sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function isEmpty(o){
    var i,v;
    if (isObject(o)){
        for (i in o){
            v=o[i];
            if (isUndefined(v) && isFunction(v)){
                return false;
            }
        }
    }
    return true;
}


function isFunction(a){
    return typeof a=='function';
}


function isNull(a){
    return typeof a=='object' && !a;
}


function isNumber(a){
    return typeof a=='number' && isFinite(a);
}


function isObject(a){
    return (a && typeof a=='object') || isFunction(a);
}


function isString(a){
    return typeof a=='string';
}


function isUndefined(a){
    return typeof a=='undefined';
}

function addTitleToHeader(title){
	var docTitle = document.title;
	if(docTitle && title != '') document.title = docTitle + ' - ' + title;
}
