// JavaScript Document

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}

function closeWindow() {
	window.top.close();
}

function goBack() {
	history.go(-1);
}

function setFocus(frm) {
	frm.elements[0].focus();
}

function getNumberOfDays(month, year) {
	var nrOfDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	if (month == 2) {
		if (year % 4 == 0) {
			if ((year % 100 == 0) && (year % 400 != 0)) {
				return 28;
			} else {
				return 29;
			}
		} else {
			return 28;
		}
	} else {
		return nrOfDays[month - 1];
	}
}

function isDate(day, month, year) {
	if (isEmptyString(day) || isEmptyString(month) || isEmptyString(year))
		return false;
	if (!isInteger(day) || !isInteger(month) || !isInteger(year))
		return false;
	if (day <= 0 || month <= 0 || month > 12 || year < 0 || year > 9999)
		return false;
	if (day > getNumberOfDays(month, year))
		return false;

	return true;
}

function isEmail(str) {
	return (str.indexOf('@') > 0);
}

function isEmptyString(str) {
	for (var i = 0; i < str.length; i++)
		if (str.charAt(i) != ' ')
			return false;
			
	return true;
}

function isFloat(str) {
	var nwStr = str.replace(',', '.');
	var val = parseFloat(nwStr);
	return (!isNaN(val));
}

function isInteger(str) {
	return !isNaN(str);
}

function isValidNumber(str) {
	for (i=0;i<str.length;i++) {
		CheckNum = parseInt(str.substr(i,1));
		if (isNaN(CheckNum)) {
			return false;
		}
	}
	return true;
}


function isTime(hour, minute) {
	if (!isInteger(hour) || !isInteger(minute))
		return false;
	if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60)
		return false;

	return true;
}

function openPopupWindow(url, w, h, centered, name) {
	strSettings = 'toolbar=no,location=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=' + w + ',height=' + h;

	if (centered) {
		strSettings += ',left=' + ((screen.width - w) / 2);
		strSettings += ',top=' + ((screen.height - h) / 2);
	}

	if (name)
    	return window.open(url, name, strSettings);
	else
    	return window.open(url, '', strSettings);
}

function openModalPopupWindow(url, w, h) {
	strSettings = 'dialogWidth:' + w + 'px; dialogHeight:' + h + 'px';
	showModalDialog(url, '', strSettings);
}
