/* $Header: /cvs01/Spinfish/beelinersurveys.com/resource/web/shared/js/platform.js,v 1.1 2007/11/12 03:34:28 heiko Exp $ */
/*
 * Platform.js
 *
 * Created on Sep 26, 2003 1:41:35 PM by administrator
 *
 * (C) Copyright ROG, Inc. Sep 26, 2003
 */
 
/**
 * Class Platform 
 * 
 * Provides platform independant DOM access.
 */

/**
 * Create a new platform object.
 */
function Platform() {

	if (document.all) {
		this.init = this.initMS;
	}
	else if (document.getElementById) {
		this.init = this.initMZ;
	}
	else if (document.layers) {
		this.init = this.initNS;
	}
	else {
		this.init = this.initDefault;
	}	
	this.initCommon();
	this.init();
}

/*
 * Initialize the common parts of the platform object.
 */
Platform.prototype.initCommon = function() {
}

/**
 * Initialize a new platform object for a Microsoft compatible browser.
 */
Platform.prototype.initMS = function() {

	this.getElement = function(id) {
		return document.all[id];
	}
	
    this.getHeight = function(element) {
    	return element.offsetHeight;
    }
    
    this.getVisibility = function(id) {
        var elm = document.all[id];
        if (elm == null) return "";
        return elm.style.visibility;
    }
    
    this.isVisible = function(id) {
        var elm = document.all[id];
        if (elm == null) return false;
        return elm.style.visibility != "hidden";
    }
    
    this.getWidth = function(element) {
    	return element.offsetWidth;
    }
    
    this.getX = function(element) {
    	return element.offsetLeft;
    }
    
    this.getY = function(element) {
    	return element.offsetTop;
    }
    
    this.getWindowHeight = function() {
    	return document.body.clientHeight;
    }
    
    this.getWindowWidth = function() {
    	return document.body.clientWidth;
    }
    
    this.setHeight = function(element, h) {
    	element.style.pixelHeight = h;
    }
    
    this.setX = function(element, x) {
    	element.style.pixelLeft = x;
    }
    
    this.setY = function(element, y) {
    	element.style.pixelTop = y;
    }
    
    this.setVisible = function(id, visible) {
    	var elm = document.all[id];
    	if (elm == null) return;
    	elm.style.visibility = visible ? "" : "hidden";
        elm.style.display = visible ? "" : "none";
    }
    
    this.setWidth = function(element, w) {
    	element.style.setPixelWidth = w;
    }
    
    this.getInnerText = function(element) {
    
    	return element.innerText;
    }
    
    this.setInnerText = function(element, text) {
	   
		element.innerText = text;
    }

    this.getChildWindow = function(element) {
    	var cw;
    	
    	cw = element.contentWindow;
    	return cw;
    }
    	
}

/**
 * Initialize a new platform object for a Mozilla compatible browser.
 */
Platform.prototype.initMZ = function() {

	this.getElement = function(id) {
		return document.getElementById(id);
	}
	
    this.getVisibility = function(id) {
        var elm = document.getElementById(id);
        if (elm == null) return "";
        return elm.style.visibility == "collapse" ? "hidden" : elm.style.visibility;
    }
    
    this.isVisible = function(id) {
        var elm = document.getElementById(id);
        if (elm == null) return "";
        return elm.style.visibility == "visible";
    }
    
    this.getX = function(element) {
    	return element.offsetLeft;
    }
    
    this.getY = function(element) {
    	return element.offsetTop;
    }
    
    this.getWidth = function(element) {
    	return element.offsetWidth;
    }
    
    this.getHeight = function(element) {
    	return element.offsetHeight;
    }
    
    this.getWindowHeight = function() {
    	return document.body.clientHeight;
    }
    
    this.getWindowWidth = function() {
    	return document.body.clientWidth;
    }
    
    this.setHeight = function(element, h) {
    	element.style.height = h;
    }
    
    this.setVisible = function(id, visible) {
        var elm = document.getElementById(id);
        if (elm == null) return;
        elm.style.visibility = visible ? "visible" : "collapse";
        elm.style.display = visible ? "" : "none";
    }
    
    this.setWidth = function(element, w) {
    	element.style.width = w;
    }
    
    this.setX = function(element, x) {
    	element.style.left = x + "px";
    }
    
    this.setY = function(element, y) {
    	element.style.top = y + "px";
    }
    
    this.getInnerText = function(element) {
    
    	return element.childNodes[0].nodeValue.toString();
    }
    
    this.setInnerText = function(element, text) {
    	var newNode;
    	var r;
	   
		newNode = document.createTextNode(text);
		element.replaceChild(newNode, element.childNodes[0]);
    }

    this.getChildWindow = function(element) {
    	var cw;
    	
    	cw = element.contentWindow;
    	if (cw == null) {
    		cw = element.contentDocument.defaultView;
    	}
    	return cw;
    }
    	
	/**
	 * Emulate the innerText() method from IE for Mozilla browsers
	 */    
	/*HTMLElement.prototype.__defineGetter__("innerText", function () {
	   var r = this.ownerDocument.createRange();
	   r.selectNodeContents(this);
	   return r.toString();
	});*/

}

/*
 * Initialize a new platform object for an old Netscape browser.
 */
Platform.prototype.initNS = function() {

	this.getElement = function(id) {
		return document.layers[id];
	}
    this.getVisibility = function(id) {
        var l = document.layers[id];
        if (l == null) return "";
        var v = l.visibility;
        return v == "show" ? "visible" : v == "hide" ? "hidden" : v;
    }
    this.isVisible = function(id) {
        var l = document.layers[id];
        if (l == null) return "";
        var v = l.visibility;
        return v == "show";
    }
    this.setVisible = function(id, visible) {
        var l = document.layers[id];
        if (l == null) return "";
        l.visibility = visible ? "show" : "hide";
    }

    this.getX = function(element) {
    	return element.offsetLeft;
    }
    
    this.getY = function(element) {
    	return element.offsetTop;
    }
    
    this.getWidth = function(element) {
    	return element.offsetWidth;
    }
    
    this.getHeight = function(element) {
    	return element.offsetHeight;
    }
    
    this.setX = function(element, x) {
    	element.style.pixelLeft = x;
    }
    
    this.setY = function(element, y) {
    	element.style.pixelTop = y;
    }
    
    this.getWindowHeight = function() {
    	return document.body.clientHeight;
    }
    
    this.getWindowWidth = function() {
    	return document.body.clientWidth;
    }
    
    this.getInnerText = function(element) {
    
    	// TODO: element.innerText;
    	return "";
    }
    
    this.setInnerText = function(element, text) {
	   
		// TODO: element.innerText = text;
    }

    this.getChildWindow = function(element) {
    	var cw;
    	
    	cw = element.contentWindow;
    	if (cw == null) {
    		cw = element.contentDocument.defaultView;
    	}
    	return cw;
    }
    	
}

/*
 * Initialize a new platform object for an unsupported browser type.
 */
Platform.prototype.initDefault = function() {

	this.getElement = function(id) {
		return null;
	}
	this.getVisibility = function(id) {
		return "";
	}
	this.isVisible = function(id) {
		return true;
	}
	this.setVisible = function(id, visible) {
	}

    this.getX = function(element) {
    	return -1;
    }
    
    this.getY = function(element) {
    	return -1;
    }
    
    this.getWidth = function(element) {
    	return -1;
    }
    
    this.getHeight = function(element) {
    	return -1;
    }
    
    this.setX = function(element, x) {
    }
    
    this.setY = function(element, y) {
    }
    
    this.getWindowHeight = function() {
    	return -1;
    }
    
    this.getWindowWidth = function() {
    	return -1;
    }
    
    this.getInnerText = function(element) {
    
    	// TODO: element.innerText;
    	return "";
    }
    
    this.setInnerText = function(element, text) {
	   
		// TODO: element.innerText = text;
    }

}

Platform.prototype.isFormChanged = function(form) {
    var elements = form.elements;

    for (var i = 0; i < elements.length; i++) {
        if (elements[i].type == "text") {
            if (elements[i].value != elements[i].defaultValue) return true;
        }
        else if (elements[i].type == "textarea") {
            if (elements[i].value != elements[i].defaultValue) return true;
        }
        else if (elements[i].type == "select-one") {
            var options = elements[i].options;
            for (j = 0; j < options.length; j++) {
                if (options[j].selected != options[j].defaultSelected) return true;
            }
        }
        else if (elements[i].type == "select-multiple") {
            var options = elements[i].options;
            for (j = 0; j < options.length; j++) {
                if (options[j].selected != options[j].defaultSelected) return true;
            }
        }
        else if (form.elements[i].type == "radio") {
            if (form.elements[i].checked != form.elements[i].defaultChecked) return true;
        }
        else if (form.elements[i].type == "checkbox") {
            if (form.elements[i].checked != form.elements[i].defaultChecked) return true;
        }
        else if (form.elements[i].type == "hidden") {
        	// ignore these types
        }
        else {
            // unsupported element type
            alert("What is a " + form.elements[i].type + "?");
        }
    }
    return false;
}

/** create the only platform object for this document. */
var platform = new Platform();

