//open popup window
var n;

function newwinByFeature(theURL,winName,features){
	n=window.open(theURL,winName,features);
	n.focus();
}

//open centered popup window of specified width and height, auto scrollbars
function newwin(theURL,winName,w,h){
	var nLeft=(screen.width)?(screen.width-w)/2:100;
	var nTop=(screen.height)?(screen.height-h)/2:100;
	var features='width='+ w + ',height=' + h + ',left=' + nLeft + ',top=' + nTop + ',scrollbars=auto'; 
	n=window.open(theURL,winName,features);
	n.focus();
}

//open centered popup window of specified width and height, with scrollbars
function newwinscroll(theURL,winName,w,h){
	var nLeft=(screen.width)?(screen.width-w)/2:100;
	var nTop=(screen.height)?(screen.height-h)/2:100;
	var features='width='+ w + ',height=' + h + ',left=' + nLeft + ',top=' + nTop + ',scrollbars=yes'; 
	n=window.open(theURL,winName,features);
	n.focus();
}

//open centered popup window of full width and height, with scrollbars
function newwinmaximized(theURL,winName,w,h){
	var features='width='+ w + ',height=' + h + ',left=0,top=0,scrollbars=yes,resizable=yes,fullscreen=no'; 
	n=window.open(theURL,winName,features);
	n.focus();
}


//restrict keyboard input to [0-9]
function checknum(){
	if (document.all){
		if (event.keyCode < 48 || event.keyCode > 57){
			event.returnValue = false;
		}
	}
}

//restrict keyboard input to [0-9] and decimal
function checknumdecimal(){
	if (document.all){
		if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode!=46)){
			event.returnValue = false;
		}
	}
}

//restrict keyboard input to [0-9] and decimal and comma (44)
function checknumcomma(){
	if(document.all){
		if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode!=46) && (event.keyCode!=44)){
			event.returnValue = false;
		}
	}
}
//insert commas
function commaFormat(amount){
	amount = cleanup(new String(amount));
	var delimiter = ",";
	var a = amount.split('.',2);
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var d = '';
	if (a.length > 1) {
		d = a[1];
	}
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3){
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

//format as currency
function currencyFormat(amount) {
	amount = cleanup(new String(amount));
	var delimiter = ",";
	var a = amount.split('.',2);
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var d = 0;
	if (a.length > 1) {
		if (a[1].length >= 3) {
			d = parseInt(a[1].substr(0,3));
			d = Math.round(d/10);
		}
		else {
			d = parseInt(a[1]);
		}
	}
	if (d == 100) {
		i++;
		d = 0;
	}
	var s = i + "." + d;
	if (s.indexOf('.') == s.length - 2) {
		s += '0';
	}	
	s = '$' + commaFormat(s);
	return s;
}

//format with commas and rounded to the nearest integer
function customFormat(amount) {
	amount = cleanup(new String(amount));
	var delimiter = ",";
	var a = amount.split('.',2);
	var i = parseInt(a[0]);
	if(isNaN(i)) { return '&nbsp;'; }
	var d = 0;
	if (a.length > 1) {
		if (parseInt(a[1].substr(0,1)) > 0) {
			d++;
		}
	}
	i = i + d;
	var s = new String(i);
	s = commaFormat(s);
	return s;
}

function captureMousePosition(x) {
	//NS4
    if (document.layers) {
        xMousePos = event.pageX;
        yMousePos = event.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
		eval("document." + x + ".left = xMousePos - 20");
		eval("document." + x + ".top = yMousePos - 80");
		return;
    }
	//IE
	else if (document.all) {
		xMousePos = window.event.x;
		yMousePos = window.event.y;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
		eval(x + ".style.left = xMousePos - 20");
		eval(x + ".style.top = yMousePos - 80");
		return;
    }
	//NS6
    else if (document.getElementById){
        xMousePos = event.pageX;
        yMousePos = event.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
		eval("document." + x + ".left = xMousePos - 20");
		eval("document." + x + ".top = yMousePos - 80");
		return;
    }
}

//validate date
function validateDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}