var el = function (id) { return document.getElementById(id); }

// ----------------------------------------------------------------------------------------------------------------------------------------

function intval( mixed_var, base ) {
	var tmp;
	var type = typeof( mixed_var );
  	if(type == 'boolean') {
		if (mixed_var == true) return 1;
		else return 0;
	}
	else if(type == 'string') {
		tmp = parseInt(mixed_var * 1);
		if(isNaN(tmp) || !isFinite(tmp)) return 0;
		else return tmp.toString(base || 10);
	}
	else {
		if(type == 'number' && isFinite(mixed_var) ) return Math.floor(mixed_var);
		else return 0;
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function findPosX(obj) {
	var top = 0, left = 0;

	var myTarget=obj;
	while(myTarget!= document.body) {
		top += myTarget.offsetTop;
		left += myTarget.offsetLeft;
		myTarget = myTarget.offsetParent;
	}

	return left;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function findPosY(obj) {
	var top = 0, left = 0;

	var myTarget=obj;
	while(myTarget!= document.body) {
		top += myTarget.offsetTop;
		left += myTarget.offsetLeft;
		myTarget = myTarget.offsetParent;
	}

	return top;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function showDIV(divObj) {
	if (divObj!=null) {
		if (divObj.style.visibility!='visible') {
			divObj.style.visibility='visible';
			divObj.style.display='inline';
		}
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function hideDIV(divObj) {
	if (divObj!=null) {
		if (divObj.style.visibility!='hidden') {
			divObj.style.visibility='hidden';
			divObj.style.display='none';
		}
	}
}

// ----------------------------------------------------------------------------------------------------------------------------------------

var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;

function pad(str, len, pad, dir) {

	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
	if (typeof(dir) == "undefined") { var dir = STR_PAD_RIGHT; }
	
	if (len + 1 >= str.length) {

		switch (dir){

			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
			break;

			case STR_PAD_BOTH:
				var right = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
			break;

			default:
				str = str + Array(len + 1 - str.length).join(pad);
			break;

		} // switch

	}

	return str;

}

// ----------------------------------------------------------------------------------------------------------------------------------------

function str_ireplace ( search, replace, subject ) {
    var i, k = '';
    var searchl = 0;

    search += '';
    searchl = search.length;
    if (!(replace instanceof Array)) {
        replace = new Array(replace);
        if (search instanceof Array) {
            // If search is an array and replace is a string,
            // then this replacement string is used for every value of search
            while (searchl > replace.length) {
                replace[replace.length] = replace[0];
            }
        }
    }

    if (!(search instanceof Array)) {
        search = new Array(search);
    }
    while (search.length>replace.length) {
        // If replace has fewer values than search,
        // then an empty string is used for the rest of replacement values
        replace[replace.length] = '';
    }

    if (subject instanceof Array) {
        // If subject is an array, then the search and replace is performed
        // with every entry of subject , and the return value is an array as well.
        for (k in subject) {
            subject[k] = str_ireplace(search, replace, subject[k]);
        }
        return subject;
    }

    searchl = search.length;
    for (i = 0; i < searchl; i++) {
        reg = new RegExp(search[i], 'gi');
        subject = subject.replace(reg, replace[i]);
    }

    return subject;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

function trim (str, charlist) { var whitespace, l = 0, i = 0; str += ''; if (!charlist) { whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000"; } else { charlist += ''; whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');  } l = str.length; for (i = 0; i < l; i++) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.substring(i); break;}} l = str.length; for (i = l - 1; i >= 0; i--) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.substring(0, i + 1);break;}}return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';}

// ----------------------------------------------------------------------------------------------------------------------------------------

function substr( f_string, f_start, f_length ) { f_string += ''; if(f_start < 0) { f_start += f_string.length; } if(f_length == undefined) { f_length = f_string.length; } else if(f_length < 0){ f_length += f_string.length; } else { f_length += f_start; } if(f_length < f_start) { f_length = f_start; } return f_string.substring(f_start, f_length); }

// ----------------------------------------------------------------------------------------------------------------------------------------

function strlen(string){var str=string+'';var i=0,chr='',lgth=0;var getWholeChar=function(str,i){var code=str.charCodeAt(i);var next='',prev='';if(0xD800<=code&&code<=0xDBFF){if(str.length<=(i+1)){throw'High surrogate without following low surrogate';} next=str.charCodeAt(i+1);if(0xDC00>next||next>0xDFFF){throw'High surrogate without following low surrogate';} return str[i]+str[i+1];}else if(0xDC00<=code&&code<=0xDFFF){if(i===0){throw'Low surrogate without preceding high surrogate';} prev=str.charCodeAt(i-1);if(0xD800>prev||prev>0xDBFF){throw'Low surrogate without preceding high surrogate';} return false;} return str[i];};for(i=0,lgth=0;i<str.length;i++){if((chr=getWholeChar(str,i))===false){continue;} lgth++;} return lgth;}

// ----------------------------------------------------------------------------------------------------------------------------------------

function mktime() {
    var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;

    if (argc > 0){
        d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
    }
 
    var dateManip = {
        0: function(tt){ return d.setHours(tt); },
        1: function(tt){ return d.setMinutes(tt); },
        2: function(tt){ var set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
        3: function(tt){ var set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
        4: function(tt){ return d.setDate(tt+mb); },
        5: function(tt){ return d.setYear(tt+ma); }
    };
    
    for( i = 0; i < argc; i++ ){
        no = parseInt(argv[i]*1);
        if (isNaN(no)) {
            return false;
        } else {
            // arg is number, let's manipulate date object
            if(!dateManip[i](no)){
                // failed
                return false;
            }
        }
    }

    return Math.floor(d.getTime()/1000);
}

//----------------------------------------------------------------------------------------------------------------------------------------

function strpos( haystack, needle, offset){
    // Find position of first occurrence of a string
    //
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_strpos/
    // +       version: 810.612
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14

    var i = (haystack+'').indexOf( needle, offset );
    return i===-1 ? false : i;
}

// ----------------------------------------------------------------------------------------------------------------------------------------

