﻿var CLUtility=new  Object();
CLUtility.getElementXY=function(obj)
{
 var top=0;
  var left=0;
  var objpos=new Object();
  while(obj!=null && obj.tagName.toUpperCase()!="BODY")
  {
        
       if(obj.style.display.toUpperCase()!="NONE")
       {
         top=parseInt(obj.offsetTop)+top;
		 left=parseInt(obj.offsetLeft)+left;
	   }
		 obj=obj.offsetParent;
  }
  objpos.top=top;
  objpos.left=left;
  return objpos;
}
CLUtility.serverErrorMsg = {
    parameterError: { code: 1, msg: 'parameterError' },
    loginNotFounded: { code: 2, msg: 'not account founded' },
    loginWrongPassword: { code: 3, msg: 'wrong password' }
   

}
CLUtility.formatEvent = function (oEvent) {

    if (typeof oEvent.charCode == "undefined") {
        oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
       // oEvent.isChar = (oEvent.charCode > 0);
    }
    
    if (oEvent.srcElement && !oEvent.target) {
        oEvent.eventPhase = 2;
        oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
        oEvent.pageY = oEvent.clientY + document.body.scrollTop;
        
        if (!oEvent.preventDefault) {
                oEvent.preventDefault = function () {
                    this.returnValue = false;
                };
        }

        if (oEvent.type == "mouseout") {
            oEvent.relatedTarget = oEvent.toElement;
        } else if (oEvent.type == "mouseover") {
            oEvent.relatedTarget = oEvent.fromElement;
        }

        if (!oEvent.stopPropagation) {
                oEvent.stopPropagation = function () {
                    this.cancelBubble = true;
                };
        }
        
        oEvent.target = oEvent.srcElement;
        oEvent.time = (new Date).getTime();
    
    }
    
    return oEvent;
};
CLUtility.util = {
    getUndefinedValue: function(value) {
        if (value == undefined) {
            return "&nbsp;";
        }
        else {
            return value;
        }
    }
}
CLUtility.validation = {
whitespace:" \t\n\r",
defaultEmptyOK: false,
   reportError:function(sender,message)
   {
      alert(message);
      try
      {
        sender.focus();
      }
      catch(Error){};
      
   },
   checknone:function(obj)
   {
        return true;
   },
   checkempty:function(obj)
   {
        return true;
    },
    isWhitespace:function(s)
    {   var i;
        if (this.isEmpty(s)) return true;
        for (i = 0; i < s.length; i++)
        {   
            var c = s.charAt(i);
            if (this.whitespace.indexOf(c) == -1) return false;
        }
        return true;
    },
   isEmail:function(s)
   {
       if (this.isEmpty(s))
           if (this.isEmail.arguments.length == 1) return this.defaultEmptyOK;
           else return (this.isEmail.arguments[1] == true);

       // is s whitespace?
       if (this.isWhitespace(s)) return false;

       // there must be >= 1 character before @, so we
       // start looking at character position 1 
       // (i.e. second character)
       var i = 1;
       var sLength = s.length;

       // look for @
       while ((i < sLength) && (s.charAt(i) != "@")) {
           i++
       }

       if ((i >= sLength) || (s.charAt(i) != "@")) return false;
       else i += 2;

       // look for .
       while ((i < sLength) && (s.charAt(i) != ".")) {
           i++
       }

       // there must be at least one character after the .
       if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
       else return true;
   },
   isEmpty: function(s)
  {  
     return ((s == null) || (s.length == 0))
  },
   checkdigitonly:function(obj)
   {
        return true;
   },
   drpcheckvaluezero:function(obj)
   {
        return true;
   }
}
CLUtility.GetValue={

    inputtextgetvalue:function(obj)
    {
        return obj.obj.value;
    },
    drplistGetSingleValue:function(obj)
    {
         var value;
        if(obj.isFromValue)
        {
            value=obj.obj.options[obj.obj.selectedIndex].value;
        }
        else 
        {
            value=obj.obj.options[obj.obj.selectedIndex].text;
        }
        return value;
    }
    
    
}
CLUtility.DOMElement = {
    addRow: function(tbl) {
        var tr = document.createElement("tr");
        tbl.tbody.appendChild(tr);
        return tr;
    },
    addCell: function(tr) {
        var td = document.createElement("td");
        tr.appendChild(td);
        return td;
    },
    createTableObject: function() {
        var tbl = new Object();
        var mytable = document.createElement("table");
        var mytablebody = document.createElement("tbody");
        var mycurrent_row = document.createElement("tr");
        var mycurrent_cell = document.createElement("td");
        mycurrent_row.appendChild(mycurrent_cell);
        mytablebody.appendChild(mycurrent_row);
        mytable.appendChild(mytablebody);
        tbl.table = mytable;
        tbl.table.cellPadding = 0;
        tbl.table.cellSpacing = 0;
        tbl.width = "100%";
        tbl.tr = mycurrent_row;
        tbl.td = mycurrent_cell;
        tbl.tbody = mytablebody;
        return tbl;
    },
    ConstrainImageProportion: function(obj, maxWidth, maxHeight) {
        var img = new Image();
        img.src = obj.src;
        var ratio = 1;
        if (img.height > img.width) {
            ratio = img.width / img.height;
            obj.width = maxWidth * ratio;
        } else {
            ratio = img.height / img.width;
            obj.height = maxHeight * ratio;
        }
        obj.style.display = "block";
    },
    getElementSize: function(element) {
        var height = 0, width = 0;

        try {
            var elementid = element.id;

            if (document.defaultView) //mozilla
            {
                docObj = document.getElementById(elementid);
                height = document.defaultView.getComputedStyle(docObj, "").getPropertyValue("height");
                if (height.indexOf("p") >= 0)
                    height = height.substring(0, height.indexOf("p"))

                width = document.defaultView.getComputedStyle(docObj, "").getPropertyValue("width");
                if (width.indexOf("p") >= 0)
                    width = width.substring(0, width.indexOf("p"))
            }
            else if (document.layers) {
                height = document.layers[elementid].document.height;
                width = document.layers[elementid].document.width;
            }
            else if (document.all)	// IE
            {
                if (element.clientWidth > 0) {
                    width = element.clientWidth;
                }
                else {
                    width = document.all[elementid].offsetWidth;
                }
                if (element.clientHeight > 0) {
                    height = element.clientHeight;
                }
                else {
                    height = document.all[elementid].offsetHeight;
                }
            }
        }
        catch (oExc) { }

        return { height: height, width: width };
    },
    getElementPosition: function(element) {
        var x = 0, y = 0;
        if (navigator.userAgent.toLowerCase().indexOf('mac') != -1) {
            while (element != null) {
                try {
                    x += element.offsetLeft;
                    y += element.offsetTop;
                    element = element.offsetParent;
                }
                catch (exception) { return { x: 0, y: 0 }; }
            }
        }
        else {
            while (element != null) {
                try {
                    x += element.offsetLeft - element.scrollLeft;
                    y += element.offsetTop - element.scrollTop;
                    element = element.offsetParent;
                }
                catch (exception) { return { x: 0, y: 0 }; }
            }
        }
        return { x: x, y: y };
    },
    createElement: function(tagName) {
        return document.createElement(tagName);
    },
    getBrowserWindowSize: 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 { 'w': myWidth, 'h': myHeight };
    }
}
CLUtility.EventUtil = {
    mouseXY: { _x: 0, _y: 0 },
    addEventHandler: function(oTarget, sEventType, fnHandler, obj) {
        if (oTarget.addEventListener) {
            if (typeof (fnHandler) == "function") {
                oTarget.addEventListener(sEventType, fnHandler, false);
            }
            else if (typeof (fnHandler) == "string") {
                oTarget.addEventListener(sEventType, function() { eval(fnHandler + "(obj,event)"); }, false);
            }
            oTarget.object = obj;
        } else if (oTarget.attachEvent) {
            if (typeof (fnHandler) == "function") {
                oTarget.attachEvent("on" + sEventType, fnHandler);
            }
            else if (typeof (fnHandler) == "string") {
                oTarget.attachEvent("on" + sEventType, function() { eval(fnHandler + "(obj,event)"); });
            }
            oTarget.object = obj;
        } else {
            oTarget["on" + sEventType] = fnHandler;
        }
    },


    removeEventHandler: function(oTarget, sEventType, fnHandler) {

        if (oTarget.removeEventListener) {
            oTarget.removeEventListener(sEventType, fnHandler, false);
        } else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, fnHandler);

        } else {
            oTarget["on" + sEventType] = null;
        }
    },
    formatEvent: function(oEvent) {

        if (typeof oEvent.charCode == "undefined") {
            oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
            //oEvent.isChar = (oEvent.charCode > 0);
        }

        if (oEvent.srcElement && !oEvent.target) {
            oEvent.eventPhase = 2;
            oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
            oEvent.pageY = oEvent.clientY + document.body.scrollTop;

            if (!oEvent.preventDefault) {
                oEvent.preventDefault = function() {
                    this.returnValue = false;
                };
            }

            if (oEvent.type == "mouseout") {
                oEvent.relatedTarget = oEvent.toElement;
            } else if (oEvent.type == "mouseover") {
                oEvent.relatedTarget = oEvent.fromElement;
            }

            if (!oEvent.stopPropagation) {
                oEvent.stopPropagation = function() {
                    this.cancelBubble = true;
                };
            }

            oEvent.target = oEvent.srcElement;
            oEvent.time = (new Date).getTime();

        }

        return oEvent;
    },
    getEvent: function() {
        if (window.event) {
            return this.formatEvent(window.event);
        } else {
            return CLUtility.EventUtil.getEvent.caller.arguments[0];
        }
    },
    mousemove: function(e) {
        try {
            if (document.all) {
                CLUtility.EventUtil.mouseXY._x = event.clientX + document.body.scrollLeft;
                CLUtility.EventUtil.mouseXY._y = event.clientY + document.body.scrollTop;
            }
            else {
                CLUtility.EventUtil.mouseXY._x = e.pageX;
                CLUtility.EventUtil.mouseXY._y = e.pageY;
            }

            if (CLUtility.EventUtil.mouseXY._x < 0) { CLUtility.EventUtil.mouseXY._x = 0; }
            if (CLUtility.EventUtil.mouseXY._y < 0) { CLUtility.EventUtil.mouseXY._y = 0; }
        }
        catch (Error) {

        }
        
        return true;
    },
    cursorOnElement: function(element) {
        var x = CLUtility.EventUtil.mouseXY._x;
        var y = CLUtility.EventUtil.mouseXY._y;
        var size = CLUtility.DOMElement.getElementSize(element);
        var pos = CLUtility.DOMElement.getElementPosition(element);
        //document.title = x + "  " + y + "  " + pos.x + "   " + pos.y + "    " + size.width + "    " + size.height;
        var startX = parseInt(pos.x);
        var endX = startX + parseInt(size.width);
        var startY = parseInt(pos.y);
        var endY = startY + parseInt(size.height);
        return (x >= startX && x <= endX && y >= startY && y <= endY);
    }

}
if (!document.all)
	document.captureEvents(Event.MOUSEMOVE); //mozilla

window.onmousemove = document.onmousemove = CLUtility.EventUtil.mousemove; // IE/Opera
CLUtility.Cookie = {

    setCookie: function(name, value, expires, path, domain, secure) {
        // set time, it's in milliseconds
        var today = new Date();
        today.setTime(today.getTime());
        // if the expires variable is set, make the correct expires time, the
        // current script below will set it for x number of days, to make it
        // for hours, delete * 24, for minutes, delete * 60 * 24
        if (expires) {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        //alert( 'today ' + today.toGMTString() );// this is for testing purpose only
        var expires_date = new Date(today.getTime() + (expires));
        //alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

        document.cookie = name + "=" + escape(value) +
		    ((expires) ? ";expires=" + expires_date.toGMTString() : "") + //expires.toGMTString()
		    ((path) ? ";path=" + path : "") +
		    ((domain) ? ";domain=" + domain : "") +
		    ((secure) ? ";secure" : "");
    },
    getCookie: function(check_name) {
        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;
        }
    },
    deleteCookie: function(name, path, domain) {
        if (CLUtility.Cookie.getCookie(name)) document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";

    },
    isCookieEnabled: function() {
        return navigator.cookieEnabled;
    }

}
