/***************************************************************
*
* Name         : Core class
* Desciption   :
* Author       : Canh Chan
* Created on   : 
* 
* ************************************************************/

var core = null;
var Core = function(){};

Core.prototype =
{

    /*
    * Function: getObject(strObjectName)
    * Description: get obeject in document by id
    * Author: Canh Chan
    */
    getObject: function(strObjectID) {
        return document.getElementById(strObjectID);
    },
    getObjectByName: function(strObjectName) {
        return document.getElementsByName(strObjectName);
    },
    /*
    * Function: getObjectID(obj)
    * Description: get id from object
    * Author: DoNguyen
    */    
    getObjectID: function(obj) {
        return obj.id;
    },

    /*
    * Function: trim(str)
    * Description: Removes leading and ending more than one whitespaces
    * Author: Canh Chan
    */
    trim: function(str) {
        return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
    },
    /*
    * Function: trimAll(str)
    * Description: removes all space
    * Author: Canh Chan
    */
    trimAll: function(str) {
        return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{1,}/, '');
    },

    showHide: function(obj, isVisiblity) {
        try {
            obj = document.getElementById(obj);
            //
            if (obj)
            {
                if (isVisiblity)
                    obj.style.display = "block";
                else
                    obj.style.display = "none";
            }
        }
        catch (e) {
            //alert("Error function showHide: Object not found: " + e);
        }
    },
    
    isInteger: function(s)
    {
        return Math.ceil(s) == Math.floor(s);
    },

    getInsideWindowHeight: function() {
        var intHeight = 0;
        if (typeof (window.innerWidth) == 'number') {
            intHeight = window.innerHeight;
        }
        else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            intHeight = document.documentElement.clientHeight;
        }
        else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            intHeight = document.body.clientHeight;
        }
        return intHeight;
    },

    getInsideWindowWidth: function() {

        var intWidth = 0;

        if (window.innerWidth) {
            intWidth = window.innerWidth;
        }
        else if (document.body && document.body.clientWidth) {
            intWidth = document.body.clientWidth;
        }
        else {
            intWidth = document.body.parentElement.clientWidth;
        }

        return intWidth;
    },
    // Retrieve the rendered height of an element
    getObjectHeight: function(obj) {
        var elem = document.getElementById(obj);
        var result = 0;
        if (elem.offsetHeight) {
            result = elem.offsetHeight;
        } else if (elem.clip && elem.clip.height) {
            result = elem.clip.height;
        } else if (elem.style && elem.style.pixelHeight) {
            result = elem.style.pixelHeight;
        } else if (elem.style && elem.style.height) {
            result = elem.style.height;
        }
        return parseInt(result);
    },
    // Set the height for an element
    setObjectHeight: function(obj,heightValue) {
        var elem = document.getElementById(obj);
        var valuePX = heightValue + "px";
        if (elem.style) {
            elem.style.height = valuePX;
        } else if (elem.clip) {
            elem.clip.height = valuePX;
        } else if (elem.style) {
            elem.style.pixelHeight = valuePX;
        }
    },
    // Retrieve the rendered width of an element
    getObjectWidth: function(obj) {
        var elem = document.getElementById(obj);
        var result = 0;
        if (elem.offsetWidth) {
            result = elem.offsetWidth;
        } else if (elem.clip && elem.clip.width) {
            result = elem.clip.width;
        } else if (elem.style && elem.style.pixelWidth) {
            result = elem.style.pixelWidth;
        } else if (elem.style && elem.style.width) {
            result = elem.style.width;
        }
        return parseInt(result);
    },
    // Set the width for an element
    setObjectWidth: function(obj,widthValue) {
        var elem = document.getElementById(obj);
        var valuePX = widthValue + "px";
        if (elem.style) {
            elem.style.width = valuePX;
        } else if (elem.clip) {
            elem.clip.width = valuePX;
        } else if (elem.style) {
            elem.style.pixelWidth = valuePX;
        }
    },
    getObjectScrollHieght: function (obj)
    {
        var elem = document.getElementById(obj);
        
        return elem.scrollHeight;
    }   
    , 

    moveTo: function(strObjectID, intX, intY) {
        var obj = this.getObject(strObjectID);

        if (obj != null) {
            obj.style.top = intY + "px";
            obj.style.left = intX + "px";
        }
    },

    centerOnWindow: function(strObjectID) {                
        this.moveTo(strObjectID,(this.getInsideWindowHeight() - this.getObjectHeight(strObjectID))/2 ,
            (this.getInsideWindowWidth() - this.getObjectWidth(strObjectID))/2 );
    },

    findPos: function(obj) {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            curleft = obj.offsetLeft
            curtop = obj.offsetTop
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
            }
        }
        return [curleft, curtop];
    },
    
    
    scrollLeft: function() {
	    return this.filterResults (
		    window.pageXOffset ? window.pageXOffset : 0,
		    document.documentElement ? document.documentElement.scrollLeft : 0,
		    document.body ? document.body.scrollLeft : 0
	    );
    },
    
    scrollTop: function() {
        return this.filterResults(
		    window.pageYOffset ? window.pageYOffset : 0,
		    document.documentElement ? document.documentElement.scrollTop : 0,
		    document.body ? document.body.scrollTop : 0
	    );
    },
    
    filterResults: function (n_win, n_docel, n_body) {
	    var n_result = n_win ? n_win : 0;
	    if (n_docel && (!n_result || (n_result > n_docel)))
		    n_result = n_docel;
	    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
    },


    getScroll: function()
	{
		// window scroll factors
	    var scrollX = 0, scrollY = 0;
	    if (document.body && typeof document.body.scrollTop != "undefined") {
	        scrollX += document.body.scrollLeft;
	        scrollY += document.body.scrollTop;
	        if (document.body.parentNode && 
	            typeof document.body.parentNode.scrollTop != "undefined") {
	            scrollX += document.body.parentNode.scrollLeft;
	            scrollY += document.body.parentNode.scrollTop;
	        }
	    } else if (typeof window.pageXOffset != "undefined") {
	        scrollX += window.pageXOffset;
	        scrollY += window.pageYOffset;
	    }
	    //
	    return {x:scrollX, y:scrollY};
	},
    

    /***************************************************
    *
    * Validate function
    *
    ***************************************************/

    isValidTextBoxValue: function(strID, strMessage) {
        var objTextBox = this.getObject(strID);

        if (objTextBox != null) {
            if (this.trimAll(objTextBox.value).length == 0) {
                alert(strMessage);

                return false;
            }
        }

        return true;
    },
    
    isValidEmail: function(strEmail)
	 { 	
	 	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	 	//
	 	if (strEmail==null) {
	       return false;
	    }
	    if (strEmail.length==0) {
	        return false;
	    }    
	    // check to make sure all characters are valid
		for (var i=0; i < strEmail.length; i++) {
			var letter = strEmail.charAt(i).toLowerCase();
		    if (validchars.indexOf(letter) != -1)
		      continue;
		    return false;
		}
		//
	    if (strEmail.indexOf("@") < 1) { //  must contain @, and it must not be the first character
	        return false;
	    } else if (strEmail.lastIndexOf(".") <= strEmail.indexOf("@")) {  // last dot must be after the @
	        return false;
	    } else if (strEmail.indexOf("@") == strEmail.length) {  // @ must not be the last character
	        return false;
	    } else if (strEmail.indexOf("..") >=0) { // two periods in a row is not valid
			return false;
	    } else if (strEmail.indexOf(".") == strEmail.length) {  // . must not be the last character
			return false;
	    } else if (strEmail.indexOf(".") == strEmail.indexOf("@")-1) {  // . must not be the last character
			return false;
	    }
	    
	    return true;
	 },
	 
	isValidDate: function(day,month,year)
    {
       if (day<1 || month<1 || year<1)
        return false; 
       var DateVal = month + "/" + day + "/" + year;
       var now = new Date();
       var flagDate = new Date(DateVal);
       if(year != flagDate.getFullYear() || month != (flagDate.getMonth()+1) || day != flagDate.getDate() || flagDate>now)
       {
          return false;
       }
       return true;
    },
	isNumeric: function(sText)
    {
       var ValidChars = "0123456789.";
       var IsNumber=true;
       var Char;
       for (i = 0; i < sText.length && IsNumber == true; i++) 
       { 
          Char = sText.charAt(i); 
          if (ValidChars.indexOf(Char) == -1) 
             {
             IsNumber = false;
             }
       }
       return IsNumber;
    },

	getAbsoluteY: function( oElement )
    {
        var iReturnValue = 0;
        while( oElement != null ) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
        }
        return iReturnValue;
    },
        
    trim: function (str)
    {
        if (str==null)
            return "";
        return str.replace(/^\s*|\s*$/g,"");
    },
    
    getParentObject: function( strObject )
    {
        return window.opener.document.getElementById(strObject);
    },
    
    getCurrentPath: function(strString)
    {
        for (var i=strString.length;i>=1;i--)
        {
            if (strString.substr(i-1,1)=="/")
            {
                return strString.substr(0,i);
            }
        }
    }
}
    

core = new Core(); 

