
// ============================================================
//
// ============================================================

function $(el) {
	return document.getElementById(el);
}

// ============================================================
//
// ============================================================

function str2int(str) {
	if (str == null || typeof(str) == 'undefined') {
		return 0;
	} else if (typeof(str)=="string") {
		val = parseInt(str.replace(/[^0-9\-]/gi, ''),10);
		return isNaN(val) ? 0 : val;
	} else {
		return str;
	}
}

// ============================================================
//
// ============================================================

function checkDatePart(val, mode, alt) {

	val = str2int(val);
	if (val == 0) {
		val = alt;
	} else {

		var arrDate = dateParts();

		if (mode == 'day') {
			val = Math.max(1, Math.min(31, val));
			val = fillDigits(val, 2);

		} else if (mode == 'month') {
			val = Math.max(1, Math.min(12, val));
			val = fillDigits(val, 2);

		} else if (mode == 'year') {
			val = Math.max(1980, Math.min(arrDate['year'], val));
		}

	}
	return val;
}

// ============================================================
//
// ============================================================

function dateParts() {
	var now = new Date();

	var arrDate 		= new Array();
	arrDate['year'] 	= now.getFullYear();
	arrDate['month'] 	= now.getMonth() + 1;
	arrDate['day'] 		= now.getDate();

	return arrDate;
}

// ============================================================
//
// ============================================================

function fillDigits(val, numDigits) {
	var str = '';
	for(var i=0; i<numDigits; i++) {
		str += 0;
	}

	str += val.toString();

	str = str.substr(str.length - numDigits, numDigits);

	return str;
}

// ============================================================
//
// ============================================================

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported)
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

// ============================================================
// php-compatible url-encode (alle non-alphanumerieke tekens behalve de .)
// NB: escape() zou ook kunnen, echter deze functie werkt beter dan escape()
// ============================================================

function urlenc(str) {
	var ret = "";
	//alert('ok');
	for (i=0; i < str.length; i++) {
		c = (str.substr(i,1));	// neem 1 char vd string
		// indien char geen alfanumeriek of . teken is
		if (c.search(/[^0-9a-zA-Z\.]/g) != -1) {
			// neem ascii-code van char en converteer naar hexadecimaal en maak die string uppercase
			ret += "%" + c.charCodeAt(0).toString(16).toUpperCase();
		} else {
			ret += c;
		}
	}
	return ret;
}

// ============================================================
// retourneer absolute top en left corner van relatief object
// handig voor positioneren absolute layers bij bijv. een input-object of span
// ============================================================

function obj_curtop(obj) {
	actb_toreturn = 0;
	while(obj){
		actb_toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return actb_toreturn;
}

function obj_curleft(obj) {
	actb_toreturn = 0;
	while(obj){
		actb_toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return actb_toreturn;
}

// ============================================================
// catch event
// document.onmousedown = function (mouseEvent) {
// document.onmousedown = handleEvent;
// ============================================================

function handleEvent(mEvent) {
	if (!mEvent) {
		var e = window.event.srcElement;	// Internet Explorer
	} else if (mEvent.srcElement) {
		var e = mEvent.srcElement;	// Internet Explorer
	} else if (mEvent.target) {
		var e = mEvent.target;		// Netscape and Firefox
	}

	return e;
	//alert(e.nodeName);
}

// ============================================================
// JS 22-9-2011: trim() new version
// ============================================================
function trim(s) {
	return s.replace(/^\s+|\s+$/g,""); // s.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};


