﻿// Shortcut to document.getElementById.
function $(id) {
	return document.getElementById(id);
}

// Check if a variable is defined or not.
function isDefined(object, variable){
	return (typeof(eval(object)[variable]) == 'undefined')? false : true;
}

// Cross-browser event handlers.
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}

function removeEvent(obj, evType, fn) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evType, fn, false);
        return true;
    } else if (obj.detachEvent) {
        obj.detachEvent("on" + evType, fn);
        return true;
    } else {
        return false;
    }
}

function fireEvent(obj, evType){
	if(obj){
		if( document.createEvent ) {
			var eObj = document.createEvent('MouseEvents');
			eObj.initEvent(evType, true, false);
			obj.dispatchEvent(eObj);
		}
		else if (document.createEventObject) {
			obj.fireEvent('on'+evType);
		}
	}
}

// quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]);
function quickElement() {
    var obj = document.createElement(arguments[0]);
    if (arguments[2] != '' && arguments[2] != null) {
        var textNode = document.createTextNode(arguments[2]);
        obj.appendChild(textNode);
    }
    var len = arguments.length;
    for (var i = 3; i < len; i += 2) {
        obj.setAttribute(arguments[i], arguments[i+1]);
    }
    arguments[1].appendChild(obj);
    return obj;
}

// Cookie set and get functions.
function now(){return (new Date).getTime();}

function setCookie(name, value, expires, path)
{
	if(!expires) expires = -1;
	if(!path) path = "/";
    var d = "" + name + "=" + value;

	var e;
    if (expires < 0) {
        e = "";
    }
    else if (expires == 0) {
        var f = new Date(1970, 1, 1);
        e = ";expires=" + f.toUTCString();
    }
    else {
        var f = new Date(now() + expires * 1000);
        e = ";expires=" + f.toUTCString();
    }

    document.cookie = name + "=" + value + ";path=" + path + e;
};

function getCookie(a)
{
    var b = String(document.cookie);
    var c = b.indexOf(a + "=");

    if (c != -1) {
        var d = b.indexOf(";", c);
        return b.substring(c + a.length + 1, d == -1 ? b.length : d);
    }

    return "";
};

// ----------------------------------------------------------------------------
// Cross-browser xmlhttp object
// from http://jibbering.com/2002/4/httprequest.html
// ----------------------------------------------------------------------------
function createXmlHttp() {
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	return xmlhttp;
}

var xmlhttp = createXmlHttp();

// Get the window size.
function getWindowSize() {
  var x = 0, y = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    x = window.innerWidth;
    y = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    x = document.body.clientWidth;
    y = document.body.clientHeight;
  }
  return {x:x, y:y}
}

// Get position of an element.
function getPosition(e){
	var left = 0;
	var top  = 0;

	if(e.offsetParent){
		while (e.offsetParent){
			left += e.offsetLeft;
			top  += e.offsetTop;
			e     = e.offsetParent;
		}

		left += e.offsetLeft;
		top  += e.offsetTop;
	}else if(e.x){
		left += e.x;
		top  += e.y;
	}else{
		if(e.currentStyle&&e.currentStyle.position=='fixed'){
			var bakE = e;
			bakE.style.position = 'absolute';
			
			while (e.offsetParent){
				left += e.offsetLeft;
				top  += e.offsetTop;
				e     = e.offsetParent;
			}

			left += e.offsetLeft;
			top  += e.offsetTop;
			
			bakE.style.position = 'fixed';
		}else{
			if(e.style && e.style.left){
				left += e.style.posLeft;
			}
			if(e.style && e.style.top){
				top += e.style.posTop;
			}
			if(e.style && e.style.right){
				left += au_getWindowSize().x - e.style.posRight;
			}
			if(e.style && e.style.bottom){alert(1);
				top += au_getWindowSize().y - e.style.posBottom;
			}
		}
	}

	return {x:left, y:top};
}

// ----------------------------------------------------------------------------
// Find-position functions by PPK
// See http://www.quirksmode.org/js/findpos.html
// ----------------------------------------------------------------------------
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

//-----------------------------------------------------------------------------
// Date object extensions
// ----------------------------------------------------------------------------
Date.prototype.getCorrectYear = function() {
    // Date.getYear() is unreliable --
    // see http://www.quirksmode.org/js/introdate.html#year
    var y = this.getYear() % 100;
    return (y < 38) ? y + 2000 : y + 1900;
}

Date.prototype.getTwoDigitMonth = function() {
    return (this.getMonth() < 9) ? '0' + (this.getMonth()+1) : (this.getMonth()+1);
}

Date.prototype.getTwoDigitDate = function() {
    return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
}

Date.prototype.getTwoDigitHour = function() {
    return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
}

Date.prototype.getTwoDigitMinute = function() {
    return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
}

Date.prototype.getISODate = function() {
    return this.getCorrectYear() + '-' + this.getTwoDigitMonth() + '-' + this.getTwoDigitDate();
}

Date.prototype.getHourMinute = function() {
    return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
}

// ----------------------------------------------------------------------------
// String object extensions
// ----------------------------------------------------------------------------
String.prototype.pad_left = function(pad_length, pad_string) {
    var new_string = this;
    for (var i = 0; new_string.length < pad_length; i++) {
        new_string = pad_string + new_string;
    }
    return new_string;
}

// ----------------------------------------------------------------------------
// Get the computed style for and element
// ----------------------------------------------------------------------------
function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

function _Void() { return; }

// ----------------------------------------------------------------------------
// Add all class of input objects with their type
// ----------------------------------------------------------------------------
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
addEvent(window, 'load', function(){
		var inputs = document.getElementsByTagName('input')
		for(var i=0; i<inputs.length; i++){
			inputs[i].className += ' ' + inputs[i].type
			}
		}
	)
@end @*/
