﻿// Common Use Methodes
//********************************************
//this function recursevly retrive a parent object 
//and calculate an absolute position of given object
function getParentObjTop(obj)
{
    var yPos = 0;
    while (obj != null)
    {
        yPos += obj.offsetTop
        obj = obj.offsetParent;
    }   
    return yPos;
}
//this function recursevly retrive a parent object 
//and calculate an absolute position of given object
function getParentObjLeft(obj)
{
    var xPos = 0;
    while (obj != null)
    {
        xPos += obj.offsetLeft
        obj = obj.offsetParent;
    }   
    return xPos;
}

//Get object by id
function $(id)
{
    if (typeof id == 'string')
        return document.getElementById(id);
    else
        return id;
}
//get object by id
function $N(name)
{
    if (typeof name == 'string')
        return document.getElementsByTagName(name);
    else
        return null;
}
//return false if id is not an object
function $Null(el)
{
    return (el == undefined || el==null);
}


//Get a Browser ViewPort height
function $vpHeight() 
{
    return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}

//get offset Left position
function $X(name)
{
    var el=$(name);
	if(el)
	{
		var off=el.offsetLeft;
		var pObj=el.offsetParent;
		if(pObj) off+=$X(pObj);
		return off;
	}
	else return 0;
}
//get offset Top position
function $Y(name)
{
    var el=$(name);
	if(el)
	{
		var off=el.offsetTop;
		var pObj=el.offsetParent;
		if(pObj) off+=$Y(pObj);
		return off;
	}
	else return 0;
}
//get object's width
function $W(name)
{
    var el=$(name);
	if(el)
	{
	    //el.style.width=el.offsetWidth;
		return el.clientWidth;
    }
	else return 0;
}
//get object's height
function $H(name)
{
    var el=$(name);
	if(el)
	{
	    //el.style.height=el.offsetHeight;
		return el.clientHeight;
	}
	else return 0;
}
//attaches events and functions
function $AddEvt(obj,evt,func)
{
	if (document.addEventListener)
		obj.addEventListener(evt,func,false);
	else if (document.attachEvent) 
		obj.attachEvent("on" + evt,func);
	else
		eval(obj+".on" + evt + "=" + func);
}
function $Cancel(e) 
{
    if (window.event) 
    {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) 
    {
        e.preventDefault();
        e.stopPropagation();
    }
}
function $ScrollTop()
{
    if (document.body && document.body.scrollTop)
        return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    if (window.pageYOffset)
        return window.pageYOffset;
    return 0;
}
//get query string param
function $QGet(el,q)
{
    var val="";
    if(typeof(q)=="undefined") q=document.location.search;
    
    var qArr=q.split(el + "=");
    if(qArr.length>1)
    {
        val=qArr[1];
        if(val.indexOf("&")>-1) val=val.slice(0,val.indexOf("&"));
    }
    return val;
}
function $QSet(el,val,q)
{
    if(el!="")
    {
        if(typeof(q)!="string") q=document.location.search;
        
        q=q.replace("?","");
        
        if(q.indexOf(el + "=")==-1)
        {
            if(q.length>0) q+="&";
	        q+=el + "=" + val;
        }
        else
        {
	        var left="",right="";

	        left=q.substr(0,q.indexOf(el + "=")-1);

	        right=q.substr(q.indexOf(el + "="));
	        right=(right.indexOf("&")==-1)?"":right.substr(right.indexOf("&"));

            q=left;
            if(q.length>0) q+="&";
            q+=el + "=" + val;
            q+=right;
        }
        //if(q.indexOf("?")==-1) q = q.replace(/&/,"?");
    }
    return q;
}
//set or get cookie
function $Cookie(n,v,d)
{
    if(n && v)
    {//set cookie
        var expires;
        if(d)
        {
            var date = new Date();
            date.setTime(date.getTime()+(d*24*60*60*1000));
            expires = "; expires="+date.toGMTString();
        }
        else expires = "";
        document.cookie = n+"="+v+expires+"; path=/";
    }
    else if(n)
    {//get cookie
        n = n + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(n) == 0)
                return c.substring(n.length,c.length);
        }
    }
    return null;
}
function $Hide(obj)
{
    obj=$(obj);
    if(obj)obj.style.display="none";
}
function $Show(obj)
{
    obj=$(obj);
    if(obj)obj.style.display="block";
}
function $CallBackError(arg, context)
{
    alert('An unexpected error has occurred.');
}
function $Sort(col)
{
    document.location=$QSet("sort",col);
}
function $Pg(num,context)
{
    document.location=$QSet("pg",num);
}
function $Submit(){}
//********************************************
function $isIE()
{
    return (document.all)?true:false;
}
// return an elment css property value
function getStyle(el,styleProp)
{
    var x = null;
    if (typeof (el) == "string") 
        x = document.getElementById(el);
    else
        x = el;

    var y = 0;
    if (x.currentStyle)
	    y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
	    y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	
    return y;

}

function trim(stringToTrim)
{
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim)
{
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim)
{
    return stringToTrim.replace(/\s+$/, "");
}

