﻿
    var Utils_LastTimeStamp;
    var Utils_CurrentTimeStamp;
    var Utils_TimeStampString = '';
    
    function AddDebugTimeStamp(Message, Alert, Reset)
    {
        Utils_LastTimeStamp = Utils_CurrentTimeStamp;
        Utils_CurrentTimeStamp = new Date();

        if (Utils_LastTimeStamp != null)
            Utils_TimeStampString += (Number(Utils_CurrentTimeStamp) - Number(Utils_LastTimeStamp));
        else
            Utils_TimeStampString += "0";

        if (Message != null)
            Utils_TimeStampString += Message;
            
        Utils_TimeStampString += '\r\n';

        if (Alert)
            alert(Utils_TimeStampString);
            
        if (Reset)
            Utils_TimeStampString = '';
    }

    function isIE(MajorVersion)
    {
        var nAgt = navigator.userAgent; var verOffset = -1;
        if ((verOffset = nAgt.indexOf("MSIE")) == -1) return false;
        if (MajorVersion == null) return true;
        return MajorVersion == parseInt(nAgt.substring(verOffset + 5));
    }
    function isFF() { return navigator.userAgent.indexOf("Firefox") > -1; }

    function AlertXML(xmlDoc){
        if(isIE()){
            alert(xmlDoc.xml);
        }
        
        if(isFF()){
            var ser = new XMLSerializer()
            alert(ser.serializeToString(xmlDoc.documentElement));
        }
    }
    
    function loadXML(xmlFile)
    {
        var xmlDoc = null;

        try //Internet Explorer
        {
          xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        }
        catch(e)
        {
            try //Firefox, Mozilla, Opera, etc.
            {
              xmlDoc=document.implementation.createDocument("","",null);
            }
            catch(e) {alert(e.message)}
        }
        try 
        {
            xmlDoc.async=false;
            xmlDoc.load(xmlFile);
            //return(xmlDoc);
        }
        catch(e) {alert(e.message)}
        
        return(xmlDoc);
        
    }

    function SendXmlRequest(ServiceName, PostData, Processor) {

        try {
            //Internet Explorer ( including Performance Stations )
            var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            try {
                //Firefox, Mozilla, Opera, etc.
                var xmlHttp = new XMLHttpRequest();
                //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            } catch(e2) {
                alert(e2.message);
            }
        }
           
          xmlHttp.open("POST", "/Service.aspx", true);
          xmlHttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8");
          xmlHttp.setRequestHeader("Cache-Control","no-cache");
        
          xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState === 4) {
              if (xmlHttp.status === 200) {
              
                var ContentType = xmlHttp.getResponseHeader('Content-Type').split(';')[0];
                
                switch( ContentType ){
                    case 'text/xml':
                    
                        if( ServiceName != "AuthenticateMember" && ServiceName != "AuthenticateProfile" && getNodeValue( xmlHttp.responseXML, "Authentication" ) == "Invalid" )
                            window.location = "Login.aspx";
                        else if( Processor )
                            Processor(xmlHttp.responseXML);
                        break;
                        
                    case 'text/html':
                        Processor(xmlHttp.responseText);
                        break;
                }
              
              } else {
                alert("Error Processing Request: " + xmlHttp.responseText);
              }
            }
          };
          
          xmlHttp.send(PostData +"&ServiceName="+ ServiceName);
    }

    function SendXmlRequest2(ServiceName, PostData, Processor) {

        try {
            //Internet Explorer ( including Performance Stations )
            var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            try {
                //Firefox, Mozilla, Opera, etc.
                var xmlHttp = new XMLHttpRequest();
                //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
            } catch(e2) {
                alert(e2.message);
            }
        }
           
          xmlHttp.open("POST", "http://my2.samepagemusic.com/Service.aspx", true);
          xmlHttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded; charset=UTF-8");
          xmlHttp.setRequestHeader("Cache-Control","no-cache");
        
          xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState === 4) {
              if (xmlHttp.status === 200) {
              
                var ContentType = xmlHttp.getResponseHeader('Content-Type').split(';')[0];
                
                switch( ContentType ){
                    case 'text/xml':
                    
                        if( ServiceName != "AuthenticateMember" && ServiceName != "AuthenticateProfile" && getNodeValue( xmlHttp.responseXML, "Authentication" ) == "Invalid" )
                            window.location = "http://my2.samepagemusic.com/Login.aspx";
                        else if( Processor )
                            Processor(xmlHttp.responseXML);
                        break;
                        
                    case 'text/html':
                        Processor(xmlHttp.responseText);
                        break;
                }
              
              } else {
                alert("Error Processing Request: " + xmlHttp.responseText);
              }
            }
          };
          
          xmlHttp.send(PostData +"&ServiceName="+ ServiceName);
    }

    function include(filename){
    
        var head = document.getElementsByTagName('head')[0];
    	
        script = document.createElement('script');
        script.src = filename;
        script.type = 'text/javascript';
    	
        head.appendChild(script)
    }

    function getNodeValue(parentNode, childNode) {
    
        var nodes = parentNode.getElementsByTagName(childNode);
    
        if( nodes.length == 0 )
            return '';
        else if( nodes[0].childNodes.length > 0 )
            return nodes[0].childNodes[0].nodeValue;
        else
            return '';
    
    }
    
    function highlightRow(el, color, inDB) {

        //Highlights the row if the mouse is moved over it
        //el is the row
        //color is the color to highlight
        //inDB lets us know if we need to highlight the elements as well

        //if (!curTarget) {
            //el.style.backgroundColor = '#909090';
        
            if (inDB) {
                for (var i=0; i<el.cells.length; i++) {
        
                   el.cells[i].style.backgroundColor = color;
                   //el.style.backgroundColor = color;
                }
            }
            else {
                el.style.backgroundColor = color;
            }
            
        //}

    }

    function unhighlightRow(el, color, inDB) {

        //Removes the highlights from the row if the mouse is moved out of it
        //el is the row
        //color is the color to reset to
        //inDB lets us know if we need to remove the highlights from the elements as well

        //el.style.backgroundColor = '#CCCCCC';
        
        if (inDB) {
            for (var i=0; i<el.cells.length; i++) {
        
                el.cells[i].style.backgroundColor = color;
                //el.style.backgroundColor = color;
            }
        }
        else {
            el.style.backgroundColor = color;
        }
    }

    function addEvent(el, eventName, functionToAdd){
        if ( typeof window.addEventListener != "undefined" )
            el.addEventListener( eventName, functionToAdd, false );
        else if ( typeof window.attachEvent != "undefined" ){
            if ( el.tagName == "TR" ){
                if ( eventName == "mouseover" ){
                    if ( el.onmouseover != null ){
                        var currentMouseOver = el.mouseover;
                        el.onmouseover = function(e){
                            currentMouseOver(e);
                            return functionToAdd(e);
                        }
                    } else {
                        el.onmouseover = functionToAdd;
                    }
                } else if ( eventName == "mouseout" ) {
                    if ( el.onmouseout != null ){
                        var currentMouseOut = el.mouseout;
                        el.onmouseout = function(e){
                            currentMouseOut(e);
                            return functionToAdd(e);
                        }
                    } else {
                        el.onmouseout = functionToAdd;
                    }
                } else {
                    el.attachEvent( 'on'+eventName, functionToAdd );
                }
            } else {
                el.attachEvent( 'on'+eventName, functionToAdd );
            }
        }
    }
    
    function getElementPosition(e){
	    var left = 0;
	    var top  = 0;
	    while (e.offsetParent){
		    left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		    top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		    e     = e.offsetParent;
	    }


	    left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	    top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

	    return {x:left, y:top};

    }

    function getMouseCoords(ev){
	    if(ev.pageX || ev.pageY){
		    return {x:ev.pageX, y:ev.pageY};
	    }
	    return {
		    x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		    y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	    };
    }

    function getMouseOffset(target, ev){
	    ev = ev || window.event;

	    var docPos    = getElementPosition(target);
	    var mousePos  = getMouseCoords(ev);
	    return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
    }

    function validateDate(date) {
        var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
        return date.match(RegExPattern);
    }

    function ClearChildNodes( element ){
        while( element.firstChild ){
            element.removeChild( element.firstChild );
        }
    }
    
    function Select_SetValue( ddlElement, value ){
    
        for(var i=0; i<ddlElement.options.length; i++) {

            if (ddlElement.options[i].getAttribute("value") == value) {
                ddlElement.options.selectedIndex = i;
                break;
            }
        }
    }
    
    function Select_GetText( ddlElement ) { return ddlElement.options[ddlElement.selectedIndex].text; }
    function isNumeric(x) { return x.match(/\d/); }
    function NewGuid() { return "00000000-0000-0000-0000-000000000000"; }
    function XOR(a, b) { return a ? !b : b; }
