﻿var gClipboard = '';
function Browser() {

    this._msie = null;
    this.is_Msie = function() {
        if (this._msie == null)
            this._msie = /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
        return this._msie;
    }

    this._msie5 = null;
    this.is_Msie5 = function() {
        if (this._msie5 == null)
            this._msie5 = this.is_Msie() && /msie 5\.0/i.test(navigator.userAgent);
        return this._msie5;
    }

    this._msie6 = null;
    this.is_Msie6 = function() {
        if (this._msie6 == null)
            this._msie6 = this.is_Msie() && /msie 6\.0/i.test(navigator.userAgent);
        return this._msie6;
    }

    this._msie7 = null;
    this.is_Msie7 = function() {
        if (this._msie7 == null)
            this._msie7 = this.is_Msie() && /msie 7\.0/i.test(navigator.userAgent);
        return this._msie7;
    }
    
    this._opera = null;
    this.is_Opera = function() {
        if (this._opera == null)
            this._opera = /opera/i.test(navigator.userAgent);
        return this._opera;
    }

    this._safari = null;
    this.is_Safari = function() {
        if (this._safari == null)
            this._safari = /safari/i.test(navigator.userAgent);
        return this._safari;
    }

    this._firefox = null;
    this.is_Firefox = function() {
        if (this._firefox == null)
            this._firefox = /firefox/i.test(navigator.userAgent);
        return this._firefox;
    }

    this._khtml = null;
    this.is_Khtml = function() {
        if (this._khtml == null)
            this._khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);
        return this._khtml;
    }
    
    this._chrome = null;
    this.is_Chrome = function() {
        if (this._chrome == null)
            this._chrome = /Chrome/i.test(navigator.userAgent);
        return this._chrome;
    }
}
var gBrowser = new Browser();

function Util() {

    this.get_ElementPosition = function(aElem, aParent) {
        var offsetTrail = aElem;
        var offsetLeft = 0;
        var offsetTop = 0;
        while (offsetTrail && offsetTrail != aParent) {
            offsetLeft += offsetTrail.offsetLeft;
            offsetTop += offsetTrail.offsetTop;
            offsetTrail = offsetTrail.offsetParent;
        }
        if (navigator.userAgent.indexOf('Mac') != -1 &&
			typeof document.body.leftMargin != 'undefined') {
            offsetLeft += document.body.leftMargin;
            offsetTop += document.body.topMargin;
        }
        return { left: offsetLeft, top: offsetTop };
    }

    this.get_Event = function(aEvent) {
        return aEvent ? aEvent : (window.event ? event : null);
    }

    this.StopEvent = function(aEvent) {
        if (this.get_Event(aEvent)) {
            if (gBrowser.is_Msie()) {
                aEvent.cancelBubble = true;
                aEvent.returnValue = false;
            } else {
                aEvent.preventDefault();
                aEvent.stopPropagation();
            }
        }
    }

    this.AddEvent = function(aEvent, aEventName, aFunc) {
        if (this.get_Event(aEvent)) {
            if (gBrowser.is_Msie()) {
                aEvent.attachEvent('on' + aEventName, aFunc);
            } else {
                aEvent.addEventListener(aEventName, aFunc, true);
            }
        }
    }

    this.RemoveEvent = function(aEvent, aEventName, aFunc) {
        if (this.get_Event(aEvent)) {
            if (gBrowser.is_Msie()) {
                aEvent.detachEvent('on' + aEventName, aFunc);
            } else {
                aEvent.removeEventListener(aEventName, aFunc, true);
            }
        }
    }

    this.get_PropertyValue = function(aCommand, aObjType, aArr) {
        //alert(aArr);
        if (aCommand == 'Fonts')
            return aObjType == 1 ? aArr[5] : (aObjType == 8 ? aArr[11] : null);
        else if (aCommand == 'FontSizes')
            return aObjType == 1 ? aArr[6] : (aObjType == 8 ? aArr[12] : null);
        else if (aCommand == 'Bold')
            return aObjType == 1 ? aArr[7] == 'bold' : (aObjType == 8 ? aArr[13] == 'bold' : null);
        else if (aCommand == 'Italic')
            return aObjType == 1 ? aArr[8] == 'italic' : (aObjType == 8 ? aArr[14]  == 'italic' : null);
        else if (aCommand == 'Underline')
            return aObjType == 1 ? aArr[9].indexOf('underline') >= 0 : (aObjType == 8 ? aArr[15].indexOf('underline') >= 0 : null);
        else if (aCommand == 'Strikethrough')
            return aObjType == 1 ? aArr[9].indexOf('line-through') >= 0 : (aObjType == 8 ? aArr[15].indexOf('line-through') >= 0 : null);
        else if (aCommand == 'AlignLeft')
            return aObjType == 1 ? aArr[11] == 'left' : (aObjType == 0 || aObjType == 8 ? aArr[0] == 'left' : null);
        else if (aCommand == 'AlignCenter')
            return aObjType == 1 ? aArr[11] == 'center' : (aObjType == 0 || aObjType == 8 ? aArr[0] == 'center' : null);
        else if (aCommand == 'AlignRight')
            return aObjType == 1 ? aArr[11] == 'right' : (aObjType == 0 || aObjType == 8 ? aArr[0] == 'right' : null);
        else if (aCommand == 'AlignJustify')
            return aObjType == 1 ? aArr[11] == 'justify' : (aObjType == 0 || aObjType == 8 ? aArr[0] == 'justify' : null);
        else if (aCommand == 'AlignTop')
            return aObjType == 0 || aObjType == 8 ? aArr[1] == 'top' : null;
        else if (aCommand == 'AlignMiddle')
            return aObjType == 0 || aObjType == 8 ? aArr[1] == 'middle' : null;
        else if (aCommand == 'AlignBottom')
            return aObjType == 0 || aObjType == 8 ? aArr[1] == 'bottom' : null;
        else if (aCommand == 'Width')
            return aObjType == 0 || aObjType == 8 ? (aArr[2] != '' ? parseInt(aArr[2]) : '') : (aObjType == 1 || aObjType == 6 ? (aArr[1] != '' ? parseInt(aArr[1]) : '') : (aObjType == 2 || aObjType == 7 || aObjType == 3 || aObjType == 4 || aObjType == 5 ? '' : null));
        else if (aCommand == 'Height')
            return aObjType == 0 || aObjType == 8 ? (aArr[3] != '' ? parseInt(aArr[3]) : '') : (aObjType == 1 || aObjType == 6 ? (aArr[2] != '' ? parseInt(aArr[2]) : '') : (aObjType == 2 || aObjType == 7 || aObjType == 3 || aObjType == 4 || aObjType == 5 ? '' : null));
        else if (aCommand == 'ForeColor')
            return aObjType == 1 ? aArr[4] : (aObjType == 8 ? aArr[10] : null);
        else if (aCommand == 'BackColor')
            return aObjType == 1 || aObjType == 3 || aObjType == 8 ? aArr[3] : (aObjType == 0 || aObjType == 4 ? aArr[4] : null);
        else if (aCommand == 'BorderWidth')
            return aObjType == 0 || aObjType == 8 ? aArr[5] : (aObjType == 1 ? aArr[10] : null);
        else if (aCommand == 'BorderStyle')
            return aObjType == 0 || aObjType == 8 ? aArr[6] : (aObjType == 1 ? aArr[12] : null);
        else if (aCommand == 'BorderColor') {
            return aObjType == 0 || aObjType == 8 ? (aArr[7] == 'red' || aArr[7] == 'gainsboro' ? '' : aArr[7]) : (aObjType == 1 ? aArr[13] == 'red' || aArr[13] == 'gainsboro' ? '' : aArr[13] : null);
        }
    }

    this.ValidatorEMail = function(aSource, aArg) {
        var lfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        aArg.IsValid = aArg.Value == '-' || aArg.Value.substr(0, 7) == 'nomail_' || lfilter.test(aArg.Value);
    }
    
	this.ValidatorPhone = function(aSource, aArg) {
	    var lfilter = /^(\+|)((\(\d{3}\)\s{0,1})|)(\d+(\s|-){0,1}){0,3}\d{3}(\s{1}(e|x|ex|ext)\s{0,1}\d+|)(\s*,\s*(\+|)((\(\d{3}\)\s{0,1})|)(\d+(\s|-){0,1}){0,3}\d{3}(\s{1}(e|x|ex|ext)\s{0,1}\d+|))*$/; //(\d{3}(-|\s))     (\+\d)*\s*((\(\d{3}\)\s*|(\d{3}-)))*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}
	    aArg.IsValid = lfilter.test(aArg.Value);
	}
	
	this.ValidatorUrl = function(aSource, aArg) {
	    var lfilter = /([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
	    aArg.IsValid = lfilter.test(aArg.Value);
	}
	
	this.DeleteCookie = function(aName, aPath, aDomain) {
        if (this.GetCookie(aName)) 
        {
            document.cookie = aName + "=" + ((aPath) ? ";path=" + aPath : "") + ((aDomain) ? ";domain=" + aDomain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
    }

    this.GetCookie = function( aName ) {
        var start = document.cookie.indexOf( aName + "=" );
        var len = start + aName.length + 1;
        if ( ( !start ) && ( aName != document.cookie.substring( 0, aName.length ) ) )
        {
            return null;
        }
        if ( start == -1 ) return null;
        var end = document.cookie.indexOf( ";", len );
        if ( end == -1 ) end = document.cookie.length;
        return unescape( document.cookie.substring( len, end ) );
    }
    
    this.GetInnerText = function(aObj) {
        return typeof(aObj.innerText) != 'undefined' ? aObj.innerText : aObj.textContent;
    }
    
    this.SetInnerText = function(aObj, aValue) {
        if(typeof(aObj.innerText) != 'undefined')
            aObj.innerText = aValue;
        else
            aObj.textContent = aValue;
    }
    
    this.GetFirstChild = function(aObj) {
        return aObj.firstChild && aObj.firstChild.tagName ? aObj.firstChild : (aObj.firstChild ? aObj.firstChild.nextSibling : null);
    }
    
    this.GetCookie = function(aName) {
        check_name = aName;
	    // first we'll split this cookie up into name/value pairs
	    // note: document.cookie only returns name=value, not the other components
	    var a_all_cookies = document.cookie.split( ';' );
	    var a_temp_cookie = '';
	    var cookie_name = '';
	    var cookie_value = '';
	    var b_cookie_found = false; // set boolean t/f default f

	    for ( i = 0; i < a_all_cookies.length; i++ )
	    {
		    // now we'll split apart each name=value pair
		    a_temp_cookie = a_all_cookies[i].split( '=' );


		    // and trim left/right whitespace while we're at it
		    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		    // if the extracted name matches passed check_name
		    if ( cookie_name == check_name )
		    {
			    b_cookie_found = true;
			    // we need to handle case where cookie has no value but exists (no = sign, that is):
			    if ( a_temp_cookie.length > 1 )
			    {
				    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			    }
			    // note that in cases where cookie is initialized but no value, null is returned
			    return cookie_value;
			    break;
		    }
		    a_temp_cookie = null;
		    cookie_name = '';
	    }
	    if ( !b_cookie_found )
	    {
		    return null;
	    }
    }
    
    this.DeleteCookie = function(aName, aPath, aDomain) {
        if ( this.GetCookie( aName ) ) document.cookie = aName + "=" +
        ( ( aPath ) ? ";path=" + aPath : "") +
        ( ( aDomain ) ? ";domain=" + aDomain : "" ) +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
    
    this.CopyToClipboard = function(aStr) {
        if(window.clipboardData)  
        {  
             window.clipboardData.setData('text',aStr);  
        }  
        else  
        {  
             var clipboarddiv=document.getElementById('divclipboard');  
             if(clipboarddiv==null)  
             {  
                clipboarddiv=document.createElement('div');  
                clipboarddiv.setAttribute("name", "divclipboard");  
                clipboarddiv.setAttribute("id", "divclipboard");  
                clipboarddiv.style.display = 'none';
                document.body.appendChild(clipboarddiv);  
             }  
             clipboarddiv.innerHTML='<iframe src="ClipboardSimulate.aspx?copy=' + encodeURIComponent(aStr) + '"></iframe>';  
       }  
       return false;  
    }
    
    this.PasteFromClipboard = function() {
        if(window.clipboardData)  
        {  
            if(window["GetClipboardContent"])
                GetClipboardContent(window.clipboardData.getData('text'));
        }  
        else  
        {  
             var clipboarddiv=document.getElementById('divclipboard');  
             if(clipboarddiv)
             {  
                 clipboarddiv.innerHTML='<iframe src="ClipboardSimulate.aspx?paste=1"></iframe>';  
             }  
       }  
       return false;  
    }
    
    this.WindowSize = function() {
      var myWidth = 0, myHeight = 0;
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }
      return {width: myWidth, height: myHeight};
    }
}
var gUtil = new Util();