/*

    Base Javascript for mapping application
    
    This handles many of the miscelaneous methods that are used throughout the application

*/

// -------------------------- GLOBALS

var IMS2_browserActionWaitTime = 150; // ms

// -------------------------- FUNCTIONS


//test to see if browser is Internet Explorer
function IMS2_isMSIE()
{
	if(document.all){
	    return(true);
	}
	return(false);
}

function IMS2_xmlToString(xmlChunk){
	var xmlStr = "";
	try{
		if(self.isInternetExplorer()){
			xmlStr = xmlChunk.xml;
		}else{
			var objXMLSerializer = new XMLSerializer;
			xmlStr = objXMLSerializer.serializeToString(xmlChunk);
		}
	}
	catch(e){
		xmlStr = null;
	}
	return(xmlStr);
}

//set up request method for web service calls
//since the different browsers handle these requests differently, this method determines and implements the correct request object
function IMS2_createXMLHTTPRequest(){
    reqObj = null;
    try{
        if(window.XMLHttpRequest){
            reqObj = new XMLHttpRequest();
        }else if(window.ActiveXObject){
            try
		    {
			    reqObj = new ActiveXObject("Microsoft.XMLHTTP");
      	    }
      	    catch(e)
      	    {
      		    try
      		    {
      			    reqObj = new ActiveXObject("Msxml2.XMLHTTP");
      		    }
      		    catch(ex)
      		    {;}
		    }
        }
    }catch(e){;}
    return(reqObj);
}

//find where an element resides on the UI
function IMS2_GetElementPosition(elm){
    var x = 0;
    var y = 0;
    while(elm){
        x += elm.offsetLeft;
        y += elm.offsetTop;
        elm = elm.offsetParent;
    }
    return( [x,y] );
}

//get the size of an element
function IMS2_GetElementDimension(elm){
    var w = 0;
    var h = 0;
    if(elm){
        w = elm.offsetWidth;
        h = elm.offsetHeight;
    }
    return( [w,h] );
}

function IMS2_GetElementWidth(elm){
    return(elm.offsetWidth);
}

function IMS2_GetElementHeight(elm){
    return(elm.offsetHeight);
}

function IMS2_GetDocumentWidth(){
    if(window.innerWidth){
        return(window.innerWidth);
    }else if(document.documentElement){
        return(document.documentElement.clientWidth);
    }
    return(document.body.clientWidth);
}

function IMS2_GetDocumentHeight(){
    if(window.innerHeight){
        return(window.innerHeight);
    }else if(document.documentElement){
        return(document.documentElement.clientHeight);
    }
    return(document.body.clientHeight);
}

//return current time
function IMS2_GetTick(){
    var now = new Date();
    if(now){
        return(now.valueOf());
    }
    return(0);
}


// used to assign mouse events to UI elements dynamically
// evnt must be closure or pointer
function IMS2_AttachEvent(elm,trigger,evnt){
    if(elm){
        try{
            var fftrigger = trigger;
            if("mousewheel" == fftrigger){
                fftrigger = "DOMMouseScroll";
            }
            elm.addEventListener(trigger, evnt, false);
        }catch(e){
            try{
                elm.attachEvent("on" + trigger,evnt);
            }catch(e){
                elm["on" + trigger] = evnt;
            }
        }
    }
}

function IMS2_CorrectEvent(e){
    if(window.event){
        return(window.event);
    }
    return(e);
}

//modify an element's transparency - browser dependent
//this method can be called on intervals to create a fade effect
function IMS2_SetElmOpacity(elm,val){
    if(elm){
        if(IMS2_isMSIE()){
            elm.style["filter"] = "Alpha(Opacity=" + (val * 100) + ")";
        }else{
            elm.style["MozOpacity"] = val;
        }
    }
}

function IMS2_IsStyleString(str){
    if(str){
        if((str.indexOf(":") >= 0) || (str.indexOf(";") >= 0)){
            return(true);
        }
    }
    return(false);
}

function IMS2_HasLeftBoundRange(pos,low,high,tol){
    if(pos < low){
        if((low - pos) > tol){
            return(true);
        }
    }else if(pos > high){
        if((pos - high) > tol){
            return(true);
        }
    }
    return(false);
}

//delete item from array at index
function RemoveAt(array,index){
	var ar = new Array();
	ar = array.splice(index,1);
	return ar;
}