﻿// JScript File
//	Contains often used methods

// -- Tring processing
//  add String.trim function
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
//  add String.endsWith function
String.prototype.endsWith = function (s) {
    return s && this.length >= s.length && (this.substring(this.length - s.length, this.length) == s);
}
//  add String.replaceAll function
String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1,'g'), s2);
}

// -- 
//	Add innerText property
if(typeof(HTMLElement) != 'undefined')
{
	if(!HTMLElement.innerHTML)
	{
		HTMLElement.prototype.__defineGetter__("innerText", function () {
			return this.textContent;
		});
	}
}

// -- Get an object providing id
function getObj(id)
{
	if(id && id.length)
		return document.getElementById(id);
	return null;
}

// -- Create object with tag or tag2
function newO(tag, tag2)
{
	try
	{
		return document.createElement(tag);
	}
	catch(e)
	{
		return document.createElement(tag2);
	}
}
// -- Date functions
//	Check if string d is a valid date
//	Return date object if date is valid
function isValidDate(d)
{
	if(!d || !d.length)
	{
		return true;
	}
    var delim = '-';
    if(d.indexOf('/') > 0)
        delim = '/';
    var ds = d.split(delim);
    if(ds.length == 3)
    {
        var day = parseInt(ds[0], 10);
        var month = parseInt(ds[1], 10);
        var year = parseInt(ds[2], 10);
        if(!isNaN(day) && !isNaN(month) && !isNaN(year))
        {
            var tmpDate = new Date(year, month-1, day);
            if(tmpDate.getFullYear() == year && tmpDate.getMonth() == (month - 1) && tmpDate.getDate() == day)
            {
                return new Date(year, month - 1, day, 0, 0, 0, 0);
            }
        }
    }
    return false;
}
//	Conver a date to a data string, delimited by delim
function getDateString(dt, delim)
{
	var d = dt.getDate();
	if(d < 10)
		d = '0' + d;
	var m = dt.getMonth() + 1;
	if(m < 10)
		m = '0' + m;
	var y = dt.getFullYear();
	return d + delim + m + delim + y;
}

//	Check if an email is valid
function isValidEmail(email)
{
	if(!email || !email.length)
	{
		return true;
	}
	var patt = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	return patt.test(email);
}
//	--
//	Show an object
function showObj(obj, dis)
{
	if(!dis)
		dis = '';
	with(obj.style)
	{
		display = dis;
		visibility = 'visible';
	}
}
//	Hide an object
function hideObj(obj)
{
	with(obj.style)
	{
		display = 'none';
		visibility = 'hidden';
	}
}
