﻿/*
	Comsite Ajax Library - http://www.ComSite.org
 	by Jay Kappel, founder of ComSite.

	Permission is freely granted to copy, modify, merge, publish, distribute,
	sublicense, and/or sell copies of this Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. IN NO EVENT SHALL 
	THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
	CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
	DEALINGS IN THE SOFTWARE.
*/

// Dependencies for csAjaxUtils.js:

/// <reference path="csAjax.js" />


    csAjax.utils = {};
    csAjax.utils.loaded = true;

    csAjax.utils.instanciate = function() {
        if (csAjax.eventTracker) if (!csAjax.eventTracker('utils','afterInstanciate',this)) return;
        return true;
    }
        

    csAjax.utils.isUrl = function(sUrl) {
        if (!sUrl || typeof sUrl != 'string') return false;
        sUrl = sUrl.toLowerCase();
        return (sUrl.substring(0,7)=='http://' || sUrl.substring(0,8)=='https://');
    }
    
    
    csAjax.utils.propExists = function() {
      for(var c=0; c < arguments.length; ++c) {
        if (typeof(arguments[c])=='undefined') return false;
      }
      return true;
    }    
    
    
    csAjax.utils.getParent = function(obj, parentTagName, pMember, pValue) {
        pValue = pValue || false;
        
        // assignment operator is intentional
        while (obj = obj.parentNode) {
            try {
                if (obj.tagName.toUpperCase() == parentTagName.toUpperCase()) {
                    if (pValue) {
                        if (obj[pMember]==pValue) return obj;
                    } else {
                        if (obj[pMember]) return obj;
                    }
                }
            
            } catch(e) {}
        }
        return null;
    }
    
    
    csAjax.utils.getPageHeight = function() {
/*		if(document.all)
			return parseInt(window.document.body.offsetHeight);
		else 
			return parseInt(window.innerHeight); */			
	    
	    if(!window.innerHeight) { // IE
		    if(!(document.documentElement.clientHeight == 0)) {
			    return document.documentElement.clientHeight; //strict mode
		    } else {
			    return document.body.clientHeight; //quirks mode
		    }
	    } else { //w3c
		    return window.innerHeight;
	    }
    }
    
    
    csAjax.utils.getPageWidth = function() {
/*		if(document.all)
			return parseInt(window.document.body.offsetWidth);
		else 
			return parseInt(window.innerWidth); */

	    if(!window.innerWidth) { // IE
		    if(!(document.documentElement.clientWidth == 0)) {
			    return document.documentElement.clientWidth; //strict mode
		    } else {
			    return document.body.clientWidth; //quirks mode
		    }
	    } else { //w3c
		    return window.innerWidth;
	    }
    }
    

    csAjax.utils.getScrollPosX = function() {
	    if(!window.pageXOffset)	{ //IE
		    if(!(document.documentElement.scrollLeft == 0)) {
			    return document.documentElement.scrollLeft; //strict mode
		    } else {
			    return document.body.scrollLeft; //quirks mode
		    }
	    } else {
		    return window.pageXOffset; //w3c
	    }
    }
    

    csAjax.utils.getScrollPosY = function() {
	    if(!window.pageYOffset)	{ //IE
		    if(!(document.documentElement.scrollTop == 0)) {
			    return document.documentElement.scrollTop; //strict mode
		    } else {
			    return document.body.scrollTop; //quirks mode
		    }
	    } else {
		    return window.pageYOffset; //w3c
	    }
    }

    csAjax.utils.isPointIn = function(point, rect) {
        if (!point.x || !point.y) {
            if (csAjax.debug) alert('Please pass a valid point object with x & y properties.');
            return false;
        }
        
        if (!rect.top || !rect.bottom || !rect.left || !rect.right) {
            if (csAjax.debug) alert('Please pass a valid rectangle object with top, bottom, left, & right properties.');
            return false;
        }
        
        if (point.x < rect.left || point.x > rect.right) return false;
        if (point.y < rect.top || point.y > rect.bottom) return false;
        return true;
    }
    
    csAjax.utils.getBounds = function(obj) {
        var pos = csAjax.utils.findPos(obj);

        var right  = pos.left + pos.width;
        var bottom = pos.top + pos.height;
        
        return { top: pos.top, left: pos.left, width: pos.width, height: pos.height, right: right, bottom: bottom };        
    }
    	
    
    csAjax.utils.getExtendedBounds = function(jsWin, padding) {
        padding = padding || 5;
        var pos = csAjax.utils.findPos(jsWin.elms.window);

        pos.top -= (jsWin.elms.borderTC.offsetHeight + padding);
        pos.left -= (jsWin.elms.borderCL.offsetWidth + padding);
        pos.width += jsWin.elms.borderCL.offsetWidth + jsWin.elms.borderCR.offsetWidth + (padding*2);
        pos.height += jsWin.elms.borderTC.offsetHeight + jsWin.elms.borderBC.offsetHeight + (padding*2);
        
        var right  = pos.left + pos.width;
        var bottom = pos.top + pos.height;
        
        return { top: pos.top, left: pos.left, width: pos.width, height: pos.height, right: right, bottom: bottom };        
    }
    
	csAjax.utils.utilityPanel = function(vis, options) {  
		// Pass true to gray out screen, false to ungray  
		// options are optional.  This is a JSON object with the following (optional) properties  
		// opacity: 0-100  Lower number = less grayout higher = more of a blackout   
		// zindex: #  HTML elements with a higher zindex appear on top of the gray out  
		// bgcolor: (#xxxxxx)  Standard RGB Hex color code  
		// top, left, height, width  Standard Location parameters
		// control:  the id of a control to cover
		// ID:  the id for the panel
		
		// csGrayOut(true, {zindex:'50', bgcolor:'#0000FF', opacity:'70'});  
		// Because options is JSON opacity/zindex/bgcolor are all optional and can appear  
		// in any order.  Pass only the properties you need to set.  
		options = options || {};
		
		var panelID = options.id || 'grayScreenObject';   
		var zindex = options.zindex || csAjax.page.windowZindex;  
		var opacity = options.opacity || 60;  
		var opaque = (opacity / 100);
		var bgColor = options.bgColor || '#FFFFFF'; 
		var bgImage = options.bgImage || 'url(images/transparent.gif)'; 
		var locTop = options.top || '0px';
		var locLeft = options.left || '0px';
		var locHeight = options.height || '100%';
		var locWidth = options.width || '100%';
		var cursor = options.cursor || 'not-allowed';
		
		if (options.control) {
			var obj = document.getElementById(options.control);
			if (obj) {
				locWidth  = locWidth.replace('100%',obj.offsetWidth+'');
				locHeight = locHeight.replace('100%',obj.offsetHeight+'');

				var pos = csAjax.utils.findPos(obj);
				locLeft   = pos.left;
				locTop    = pos.top;
			}
		}

		var oScreen=document.getElementById(panelID);  
		
		if (!oScreen) {    
			// The dark layer doesn't exist, it's never been created.  So we'll    
			// create it here and apply some basic styles.    
			// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917    
			var oBody = document.getElementsByTagName("body")[0];    
			oScreen = document.createElement('div');           

			// Create the layer.        
			oScreen.style.position	='absolute';    	// Position absolutely        
			oScreen.style.top		=locTop;         	// Set the location
			oScreen.style.left		=locLeft;
			oScreen.style.height	=locHeight;			// And the size
			oScreen.style.width		=locWidth;
			oScreen.style.overflow	='hidden';			// Try to avoid making scroll bars                    
			oScreen.style.display	='none';         	// Start out Hidden
			oScreen.style.cursor	=cursor;			// Set a cursor for the area    
			oScreen.id				=panelID;      		// Name it so we can find it later    

			oBody.appendChild(oScreen);           		// Add it to the web page    
		}  
		
		if (vis) {
		   //set the shader to cover the entire page and make it visible.
		   oScreen.style.opacity			=opaque;
		   oScreen.style.MozOpacity			=opaque;
		   oScreen.style.filter				='alpha(opacity='+opacity+')';
		   oScreen.style.zIndex				=zindex;
		   oScreen.style.backgroundColor	=bgColor;
		   oScreen.style.backgroundImage    =bgImage;
		   oScreen.style.width				=locWidth;
		   oScreen.style.height				=locHeight;
		   oScreen.style.display			='block'; 
		} else {
			oScreen.style.display           ='none';
		}
		
		return oScreen;
	}	


	csAjax.utils.findPos = function(obj) {
	    /// <summary> pass in an object to get it's x,y coordinates.</summary>
		var curleft = curtop = 0;
		var width = obj.offsetWidth;
		var height = obj.offsetHeight;
		
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;

            // assignment operator is intentional
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return { top: curtop, left: curleft, width: width, height: height };
	}

    
    csAjax.utils.falseFunction = function() {
        return false;
    }


    csAjax.utils.getMaxZindex = function() {
        var divs = document.getElementsByTagName('div');
        var maxIndex=0;
        
        for (var c=0; c < divs.length; c++) {
            var div = divs[c], zIndex=0;
            if (div.style) {
                if (div.style.zIndex) zIndex=div.style.zIndex;
                if (zIndex > maxIndex) maxIndex=zIndex;
            }
        }
        return ++maxIndex;
    }  
    
    csAjax.utils.nvl = function(val, newVal) {
        if (typeof val=='undefined') return newVal;
        if (typeof val=='boolean') return val;
        if (!val) return newVal;
        return val;
    }
    
    
    csAjax.utils.openWindowDefault = function(url, windowType, winName, options) {
        if (!windowType) windowType='Default';
        if (!winName) winName='';
        options = options || {};
        
        options.url=url;
        options.name=winName;
        options.width='800px';
        options.height='500px';
    		
	    switch(windowType.toLowerCase()) {
		    case 'small':
		        if (winName.length==0) options.name = 'popupSmall';
		        options.width = '370px';
		        options.height = '222px';
		        break;
		    case 'medium':
		        if (winName.length==0) options.name = 'popupMedium';
		        options.width = '500px';
		        options.height = '300px';
		        break;
		    default:
		        if (winName.length==0) options.name = 'popupDefault';
		        options.scrollbars='yes';
		        options.resize='yes';
			    break;
	    }
        
	    return csAjax.utils.openWindow(options);
    }


    csAjax.utils.openWindow = function(options) {
	    options = options || {};
	    var leftPos = (screen.width) ? (screen.width-options.width)/2 : 0;
	    var topPos = (screen.height) ? (screen.height-options.height)/2 : 0;
    	
    	if (!options.url) {
    	    alert('openWindow requeres a URL to be passed!');
    	    return;
    	}

    	var winName = options.name || 'popupWin';
        var settings='';
            	
	    settings += 'top='+topPos;
	    settings += ',left='+leftPos;
	    
	    if (options.height) settings += ',height='+options.height;
	    if (options.width) settings += ',width='+options.width;
	    settings += ',resizable=' + (options.resize || 'no');
	    settings += ',scrollbars=' + (options.scrollbars || 'no');
	    settings += ',toolbar=' + (options.toolbar || 'no');
	    settings += ',menubar=' + (options.menubar || 'no');
	    settings += ',copyhistory=' + (options.history || 'no');
	    settings += ',location=' + (options.location || 'no');
	    settings += ',directories=' + (options.directories || 'no');
	    settings += ',status=' + (options.status || 'no');
    	
    	// mozilla only
    	settings += ',minimizable=' + (options.minimizable || 'no');
    	settings += ',dependent=' + (options.dependent || 'yes');
    	settings += ',dialog=' + (options.dependent || 'yes');
    	
	    // Opent the window and wait on it!
	    var popWin = window.open(options.url,winName,settings);
	    if (!popWin) {
	        alert('openWindow was not able to open the window.\nPlease check your popup blocker settings.');
	        return;
	    } else {
            // Perform callback?
            if (options.callback) {
                if (window.addEventListener){
                    popWin.addEventListener('load', options.callback, true);
                } else {
                    popWin.attachEvent('onload', options.callback, true);
                }
	        }

            if (popWin.focus) popWin.focus();
            return popWin;
        }
    }    
