//DHTML APIs
//Global variables for browser type testing
var isCSS, isW3C, isIE4, isNN4, isIE6CSS
//initialise these of upload
function initDHTMLAPI(){
	if(document.images){
		isCSS=(document.body && document.body.style)?true:false;
		isW3C=(isCSS && document.getElementById)?true:false;
		isIE4=(isCSS && document.all)?true:false;
		isNN4=(document.layers)?true:false;
		isIE6CSS=(document.compatMode && document.compatMode.indexOf("CSS1")>=0)?true:false;
	}
}

//A.  FUNCTIONS FOR GETTING REFERENCES
// A.1 without style property
function getRawObject(obj){
	var theObj;
	if (typeof obj=="string"){
		if (isW3C){
			theObj=document.getElementById(obj);
		}else if (isIE4){
				theObj=document.all(obj);
		}else if (isNN4){
				theObj=document.layers[obj]  //seekLayer(document,obj);
		}
	}else{
		//pass through without reference
		theObj=obj;
	}
	return theObj
}

//A.2 with style property
function getObject(obj){
	var theObj=getRawObject(obj);
	if (theObj&&isCSS){
		theObj=theObj.style
	}
	return theObj;
}

//B. FUNCTIONS FOR GETTING DIMENSIONS AND POSITIONS
//B.1 object Height
function getObjectHeight(obj){
	var elem=getRawObject(obj);
	var result=0;
	if (elem.offsetHeight){
		result=elem.offsetHeight;
	} else if (elem.clip&&elem.clip.height){
		result=elem.clip.height;
	} else if (elem.style&&elem.style.pixelHeight){
		result=elem.style.pixelHeight;
	}
	return parseInt(result);
}

//B.2 Object Width
function getObjectWidth(obj){
	var elem=getRawObject(obj);
	var result=0;
	if (elem.offsetWidth){
		if (elem.scrollWidth&&(elem.offSetWidth!=elem.scrollWidth)){
			result=elem.scrollWidth;
		}else {
			result=elem.offsetWidth;
		}
	} else if (elem.clip&&elem.clip.width){
		result=elem.clip.width;
	} else if (elem.style&&elem.style.pixelWidth){
		result=elem.style.pixelWidth;
	}
	return parseInt(result);
}			

//C. CODE FOR POSITIONING OBJECTS
//C.1 Moving an object to a specific cooordinate
function shiftTo(obj,x,y){
	var theObj = getObject(obj);
	if (theObj){
		if(isCSS){
		//equalise incorrect numeric type
		var units = (typeof theObj.left=="string")?"px":0;
		theObj.left=x+units;
		theObj.top=y+units;
		}else if (isNN4){
			theObj.moveTo(x,y)
		}		
	}
}

//C2. Code for moving an object
function shiftBy(obj,deltaX,deltaY){
	var theObj=getObject(obj);
	if (theObj){
		if (isCSS){
			//equalise incorrect numeric value
			var units=(typeof theObj.left=="string")?"px":0;
			theObj.left=getObjectLeft(obj)+deltaX +units;
			theObj.top=getObjectTop(obj)+deltaY+units;
		}else if(isNN4){
			theObj.moveBy(deltaX,deltaY);
		}
	}
}

//C.2 Code for centering object
//Correction for IE/MAC
var fudgeFactor={top:-1,left:-1};

function centerIt(layerName){
	//obj is what is to be positioned
	var obj=getRawObject(layerName);
	
	//set fudgeFactor values first time through
	if (fudgeFactor.top==-1){
		if((typeof obj.offsetTop=="number")&&obj.offsetTop>0){
			fudgeFactor.top=obj.offsetTop;
			fudgeFactor.left=obj.offsetLeft;
	   }else{
		fudgeFactor.top=0;
		fudgeFactor.left=0;
		}
		if (obj.offsetWidth && obj.scrollWidth){
			if (obj.offsetWidth!=obj.scrollWidth){
				obj.style.width=obj.scrollWidth;
			}
		}
	 }
	
	var x=Math.round((getInsideWindowWidth()/2)-(getObjectWidth(obj)/2));
	var y=Math.round((getInsideWindowHeight()/2)-(getObjectHeight(obj)/2));
	shiftTo(obj,x-fudgeFactor.left,y-fudgeFactor.top);
	show(obj);
}

//NN4 Redraw bug
function handleResize(){
if(isNN4){
	location.reload();
	}else{
	centerIt("banner");
	}
}

//D. GET WINDOW DIMENSIONS AND OBJECT POSITIONS
//D.1 Window Width
function getInsideWindowWidth(){
	if (window.innerWidth){
		return window.innerWidth;
	}else if(isIE6CSS){
		//get clientwidth
		return document.body.parentElement.clientWidth;
	}else if (document.body&&document.body.clientWidth){
			return document.body.clientWidth;
	}
	return 0;
}

//D.2 Window Height
function getInsideWindowHeight(){
	if (window.innerHeight){
		return window.innerHeight;
	}else if(isIE6CSS){
		//get clientheight
		return document.body.parentElement.clientHeight;
	}else if (document.body&&document.body.clientHeight){
			return document.body.clientHeight;
	}
	return 0;
}

function findPosX(oldObj){
	obj = getRawObject(oldObj);
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	
	return curleft;
}

function findPosY(oldObj){
	obj = getRawObject(oldObj);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


//get left coordinate
function getObjectLeft(obj){
	var elem=getRawObject(obj);
	var result=0;
	if (document.defaultView){
		var style=document.defaultView;
		var cssDec1=style.getComputedStyle(elem,"");
		result=cssDec1.getPropertyValue("left");
	}else if (elem.currentStyle){
		result=elem.currentStyle.left;
	}else if(elem.style){
		result=elem.style.left;
	}else if(isNN4){
		result=elem.left;
	}
	return parseInt(result);
}

//get top coordinate
function getObjectTop(obj){
	var elem=getRawObject(obj);
	var result=0;
	if (document.defaultView){
		var style=document.defaultView;
		var cssDec1=style.getComputedStyle(elem,"");
		result=cssDec1.getPropertyValue("top");
	}else if (elem.currentStyle){
		result=elem.currentStyle.top;
	}else if(elem.style){
		result=elem.style.top;
	}else if(isNN4){
		result=elem.top;
	}
	return parseInt(result);
}

//E. SHOW AND HIDE OBJECTS
function show(obj){
	var theObj=getObject(obj);
	if (theObj){
		theObj.visibility="visible";
	}
}
				
function hide(obj){
	var theObj=getObject(obj);
	if (theObj){
		theObj.visibility="hidden";
	}
}

function scrollX(){
	return (document.all)?document.body.scrollLeft:window.pageXOffset; 
}

function scrollY(){
	return (document.all)?document.body.scrollTop:window.pageYOffset; 
}

function setAlpha(obj, alpha) {
	if( typeof(obj) == 'string' ) obj = document.getElementById(obj);
	var object = obj.style;
    object.opacity = (alpha / 100);
    object.MozOpacity = (alpha / 100);
    object.KhtmlOpacity = (alpha / 100);
    object.filter = "alpha(opacity=" + alpha + ")";
}

function addSlashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripSlashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}
