/*
DAPI FUNCTION LIBRARY
Created by jonas.bohlin@halogen.com
*/
function dapi_try(sObj){
	if (oClient.ns5up || oClient.ie5up){
		if (typeof(dapi_tryFunc)=="undefined") dapi_tryFunc = new Function("sObj","try { eval(sObj);throw true; }catch(sRet){if (typeof(sRet)==\"boolean\" && sRet) {return true} else {return false}}")
		return dapi_tryFunc(sObj)
	} else return true
}

function getParam(sParam,sUrl) {
	sUrl = sUrl || document.location.toString()
	if (typeof(window.aQuerys)=="undefined"){
		window.aQuerys = new Array()
	}
	if (typeof(window.aQuerys[sUrl])=="undefined"){
		window.aQuerys[sUrl] = new Array()
		iQStart = sUrl.indexOf('?')
		sQuery = (iQStart>-1)?sUrl.substring(iQStart+1):false;
		if (!sQuery) return null
		else {
			var aTempq = sQuery.split("&")
			for (q=0;q<aTempq.length;q++){
				aParam = aTempq[q].split("=")
				aQuerys[sUrl][aParam[0]] = aParam[1]
	}	}	}
	sRet = aQuerys[sUrl][sParam]
	return sRet || null
}

function dapi_watchResize(){
	if (document.layers) window.onresize = new Function("if ("+window.innnerWidth+"!=window.innnerWidth || "+window.innerHeight+"!=window.innerHeight){ location.reload() }")
}

function getConfirm(sType){
	sMsg = eval(sType+"_msg")
	sTrue = eval(sType+"_true")
	sFalse = eval(sType+"_false")
	if (typeof(sMsg)=="string"){
		bConfirm = confirm(MSG_prefix+sMsg)
		if (bConfirm){
			bConfirm = eval(sTrue)
		} else {
			bConfirm = eval(sFalse)
		}
	}
	return bConfirm
}

function giveMessage(sMsg){
	alert(MSG_prefix+sMsg)
}

window.DELAY=500
window.aTimeouts = new Array()
function killTimeout(sId,sTell){
	if (aTimeouts[sId]){
		clearTimeout(aTimeouts[sId])
		aTimeouts[sId] = null
		return true
	} else {
		return false
	}
}

function storeTimeout(sId,sFunc,iDelay){
	iDelay = iDelay || DELAY
	if (aTimeouts[sId]) {
		clearTimeout(aTimeouts[sId])
	}
	aTimeouts[sId] = setTimeout(sFunc+";aTimeouts[\""+sId+"\"]=null",iDelay)
}

function isObject(obj){
	if (typeof(obj) == "object"){
		return true
	} else return false
}

function isFloat(num){
	var numInt = parseInt(num) || 0
	var numFloat = parseFloat(num) || 0
	if (numFloat>numInt) return true
	else return false
}

Number.prototype.inverse = inverse
function inverse(){
	return -1*this
}

function checkDivExists(sDivId){
	if (oClient.ns4){
		sType = document.layers[sDivId]
	} else if (oClient.ie4){
		sType = document.all[sDivId]
	} else if (oClient.dom){
		sType = document.getElementById(sDivId)
	}
	bExists = (sType!=null && typeof(sType)!="undefined")
	return bExists
}


	
if (typeof(window.aGlobalEvents)=="undefined"){
	window.aGlobalEvents = new Array()
	window.aGlobalEventTypes = new Array()	
}	
window.GlobalEvent_Re1 = /\}$|\}\n$/
window.GlobalEvent_ReEOF = /\}\n*$/
window.GlobalEvent_ReFARGS = /\(.*\).*\{/
window.GlobalEvent_ReSOF_BODY = /\{/
window.GlobalEvent_ReEV_RET = /var bEventReturn;/
window.GlobalEvent_ReSPACE = /\s/g;
function GlobalEvent(sId,iState,sType,oFunc,bAddTop){
	if (!sType){
		sStoreType = aGlobalEvents[sId]
		if (!sStoreType){
			return false
		}
		if (aGlobalEvents[sStoreType][sId][0]!=-1) aGlobalEvents[sStoreType][sId][0] = iState
		return true
	} else if (sType && typeof(aGlobalEvents[sId])=="undefined"){
		if (sType.indexOf(".")!=-1) {
			sStoreType = "E"+sType.replace(/\./,"")
			oObj = eval(sType.split(".")[0])
			sType = sType.split(".")[1] 		
		} else {
			sStoreType = "E"+sType
			oObj = null
		}
		aGlobalEvents[sId] = sStoreType //Assign the event type to the ID		
	} else {
		alert("sId "+sId+" already exists...\nmust be unique.")
		return false
	}
	if (sType && typeof(aGlobalEvents[sStoreType])=="undefined"){
		aGlobalEvents[sStoreType] = new Array()
	}
	if (!sId || (typeof(iState)=="undefined") || !sType || !oFunc){
		alert("error, can't proceed\n"+sId+","+iState+","+sType+","+oFunc)
		return false
	}
	aGlobalEvents[sStoreType][sId] = [iState,null]
	if (typeof(oFunc)=="string"){
		aGlobalEvents[sStoreType][sId][1] = new Function("e",oFunc)
	}else{ //if the function is passed as an existing function. (no argmuments can be passed.)
		aGlobalEvents[sStoreType][sId][1] = oFunc
	}
	/* Prepare for dapi event manipulation */
	oCurrentEvent = GlobalEvent_Event(sType,oObj)//eval(document+"."+sType)//document[sType]//eval("document."+sType)
	if (typeof(oCurrentEvent)!="function"){
		sEvent = "function anonymous(e){\n/*DAPI HAS CREATED THIS EVENT*/var bEventReturn;\ne = e || event;\n}"
	} else {
		sEventArg = "e"
		sEvent = oCurrentEvent.toString()
		//make sure an argument is passed to the event function, if not, insert it.
		sFirstLine = sEvent.substring(sEvent.indexOf("(")+1,sEvent.indexOf(")")).replace(GlobalEvent_ReSPACE,"")
		sArgs = sFirstLine.split(",")
		bHasArgs = (sArgs[0]!="" && typeof(sArgs[0])!="undefined")
		if (bHasArgs){			//retrieve first arugment for reuse
			sEventArg = sArgs[0]
		} else {//insert "e" argument
			sEvent = sEvent.replace(GlobalEvent_ReFARGS,"(e){")
		}
		if (!GlobalEvent_ReEV_RET.test(sEvent)){
			sEvent = sEvent.replace(GlobalEvent_ReSOF_BODY,"{\ne = "+sEventArg+" || event;\nvar bEventReturn;\n")
			sEvent = sEvent.replace(GlobalEvent_ReEOF,"\n/*DAPI HAS MODIFIED THIS CODE*/\n}")			
		}
	}
	// figure out if return is present, or has default
	iReturnAt = sEvent.indexOf("return ")
	iEventReturn = sEvent.indexOf("return bEventReturn")
	if (iReturnAt!=-1 && iEventReturn==-1){
		//pick up any old return values into bEventReturn
		sEvent = sEvent.replace("return ","bEventReturn = ")
	} else if (iEventReturn>-1){
		// remove the the return so that it can be placed at the bottom of the function
		sEvent = sEvent.replace("return bEventReturn","")
	}
	if (bAddTop){
		sAddStr = "var bEventReturn;\nif (aGlobalEvents[\""+sStoreType+"\"][\""+sId+"\"][0]==1){\nbLocalReturn = aGlobalEvents[\""+sStoreType+"\"][\""+sId+"\"][1](e);\nif (bEventReturn!=false && typeof(bLocalReturn)==\"boolean\") bEventReturn=bLocalReturn;\n}\n"
		sEvent = sEvent.replace(GlobalEvent_ReEV_RET,sAddStr)
		sEvent = sEvent.replace(GlobalEvent_ReEOF,"\nreturn bEventReturn\n}")
	} else {
		sAddStr = "if (aGlobalEvents[\""+sStoreType+"\"][\""+sId+"\"][0]==1){\nbLocalReturn = aGlobalEvents[\""+sStoreType+"\"][\""+sId+"\"][1](e);\nif (bEventReturn!=false && typeof(bLocalReturn)==\"boolean\") bEventReturn=bLocalReturn;\n}\nreturn bEventReturn\n}"
		sEvent = sEvent.replace(GlobalEvent_ReEOF,sAddStr)
	}
	if (GlobalEvent_Event(sType,oObj,sEvent)) return true
	else return false
}

function GlobalEvent_Event(sEvent,oObj,sAssign){
	var oObj = oObj || document
	bIe4Mac = (oClient.ie4 && oClient.mac)
	if (!bIe4Mac){
		if (!sAssign) rObj = oObj[sEvent]
		else {
			eval("oObj['"+sEvent+"'] ="+ sAssign); 
			rObj = true
		}
	} else {
		//ie4 on mac is _sensetive_ to non-existing events and throws errors when accessing them.
		sType = typeof(document[sEvent])
		if (!sAssign){
			if (sType=="function") rObj = oObj[sEvent]
			else rObj = oObj[sEvent] = new Function() //make a function out of it for future use :)
		} else {
			if (sType!="function") document[sEvent] = new Function() // make sure it is a function before making the eval!
			eval("oObj['"+sEvent+"'] ="+ sAssign); 
			rObj = true
		}	
	}
	return rObj
}

aLoadScripts = new Array()
function AddLoadScript(sPath,sVersion){
	for (i=0;i<aLoadScripts.length;i++){
		if (aLoadScripts[i][1]==sPath) return 2 //already set to be loaded... ignore
	}
	if (typeof(sPath)!="string" || (sVersion && typeof(sVersion)!="string")) return 0
	sVersion = sVersion || "javascript1.2"
	sStr = "<SCR"+"IPT LANGUAGE=\""+sVersion+"\" SRC=\""+sPath+"\"></SCR"+"IPT>"
	aLoadScripts[aLoadScripts.length] = [sStr,sPath,sVersion]
	return 1
}

function LoadScripts(){
	sScripts = ""
	for (i=0;i<aLoadScripts.length;i++){
		sScripts += "\n" + aLoadScripts[i][0]
	}
	if (sScripts!="") document.write(sScripts)
}