// by Simon Willison
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function getTarget(x) {
	x = x || window.event;
	return x.target || x.srcElement;
}

function grabFile(file, container) {
  	var request = getHTTPObject();
  	if (request) {
    	request.onreadystatechange = function() {
      		parseResponse(request, file, container);
    	};
    	request.open("GET", file, true);
    	request.send(null);
  	}
}

function parseResponse(request, file, container) {
  	if (request.readyState == 4) {
    	if (request.status == 200 || request.status == 304) {     		
			document.getElementById(container).innerHTML = request.responseText;
    	}
  	}
}


// by snook.ca
function getElementsByClassName(classname) {
	if (!document.getElementsByTagName) return false;    
	var a = [];
    var re = new RegExp('(^| )' + classname + '( |$)');
    var els = document.getElementsByTagName("*");
    for (var i=0,j=els.length; i<j; i++)
        if (re.test(els[i].className)) a.push(els[i]);
    return a;
}

// form field function to highlight on focus
function focusFields() {
	if (!document.getElementsByTagName) return false;
	var formfields = document.getElementsByTagName("input");
	for (var i=0; i < formfields.length; i++) {
		formfields[i].onfocus = function() {
			this.select();
		}
	}
}

// John Resig
function getStyle(elem, name ) { 
	if (elem.style[name]) {
		return elem.style[name]; 
	} else if (elem.currentStyle) {
		 return elem.currentStyle[name]; 
	} else if (document.defaultView && document.defaultView.getComputedStyle) { 
		name = name.replace(/([A-Z])/g,"-$1"); 
		name=name.toLowerCase(); 
		var s = document.defaultView.getComputedStyle(elem,""); 
		return s && s.getPropertyValue(name); 
	} else 
		return null; 
} 
//

function getHTTPObject() {
	var xhr = false;
  	if (window.XMLHttpRequest) {
    	xhr = new XMLHttpRequest();
  	} else if (window.ActiveXObject) {
    	try {
      		xhr = new ActiveXObject("Msxml2.XMLHTTP");
    	} catch(e) {
      		try {
        		xhr = new ActiveXObject("Microsoft.XMLHTTP");
      		} catch(e) {
        		xhr = false;
      		}
    	}
  	}
  	return xhr;
}

String.prototype.trim = function() {
    return this.replace( /^\s+|\s+$/, "" );
}

function addClassName (elem, className) {
    removeClassName (elem, className);
    elem.className = (elem.className + " " + className).trim();
}

function removeClassName (elem, className) {
    elem.className = elem.className.replace(className, "").trim();
}

addLoadEvent(focusFields);
addLoadEvent(getPageSize);
